summaryrefslogtreecommitdiff
path: root/httemplate/view/part_event-targets.html
blob: e8b1266ef26083bdcaeca6779023b57609852404 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<& /elements/header-popup.html,
     {
       'title'   => 'Event query - '.$part_event->event,
     }
&>
<FORM STYLE="display:inline" ACTION=<%$cgi->url%> METHOD="GET">
When event is run on <& /elements/input-date-field.html, {
  'name'    => 'date',
  'value'   => $time,
  'format'  => FS::Conf->new->config('date_format') || '%m/%d/%Y',
} &>
<INPUT TYPE="hidden" NAME="eventpart" VALUE="<%$eventpart%>">
<INPUT TYPE="submit" VALUE="Refresh">
</FORM>
<BR><BR>
% if ( $objects > 0 ) {
  <% emt("[quant,_1,$label]", $objects) %>
%   if ( $part_event->eventtable ne 'cust_main' ) {
  <% emt("belonging to [quant,_1,customer]", $customers) %>
%   }
<BR><BR>
<TABLE class="grid" cellspacing=0 width="100%" style="font-size:80%">
  <TR style='background-color:#cccccc'>
%   foreach my $header ('Trigger', @cust_header, @header) {
    <TH><% $header %></TH>
%   }
  </TR>

%   my @rowcolors = ('ffffff','eeeeee');
%   my $row = 0;
%   foreach my $object (@targets) {
  <TR style="background-color:#<% $rowcolors[$row++ % 2] %>">
%     # now works for all eventtables, including cust_pkg
%     my $link = $p . 'view/' . $part_event->eventtable . '.cgi?' .
%        $object->$pkey;
    <TD><A target="_blank" href="<% $link %>">
    <% ucfirst $label %> #<% $object->$pkey %></A></TD>

%     my $cust_main = $object->cust_main; # via Mixin
%     my $i = 0; # hack to avoid messing with cust_aligns/colors/styles
%     foreach (@cust_fields) {
%       if ($cust_header[$i] eq 'Cust. Status') {
    <TD style="text-align:center;
               font-weight:bold;
               color:#<% $cust_main->cust_statuscolor %>"><% $_->($cust_main) %></TD>
%       }
%       else {
    <TD><% $_->($cust_main) %></TD>
%       }
%     $i++;
%     } #foreach @cust_fields

%     foreach (@fields) {
    <TD><% ref($_) eq 'CODE' ? $_->($object) : $object->$_ %></TD>
%     }
  </TR>
%   } #foreach $object

</TABLE>

% } #object > 0
% else {

<% emt("No matching ${label}s found.") %>

%}
<& /elements/footer.html &>
<%init>

my $curuser = $FS::CurrentUser::CurrentUser;
die "access denied"
  unless $curuser->access_right('Edit billing events')
        || $curuser->access_right('Edit global billing events');

my ($eventpart) = $cgi->param('eventpart');
$eventpart =~ /^\d+$/ or die 'illegal eventpart';

my $time = parse_datetime($cgi->param('date')) || time;

my $part_event = FS::part_event->by_key($eventpart)
  or die "Event definition $eventpart not found.\n";
my @targets = $part_event->targets('time' => $time);
my $total = @targets;

# in imitation of search/elements/search-html.html
my @header;
my @fields;
my ($pkey, $label);
$pkey = dbdef->table($part_event->eventtable)->primary_key;

for ($part_event->eventtable) {
  if (/^cust_main$/) {
    # very likely to appear in events
    my %paybys = FS::payby->cust_payby2longname;
    push @header, 'Balance', 'Payment Method';
    push @fields, 'balance', sub{ $paybys{$_[0]->payby} };
    $label = 'customer';
  }
  elsif (/^cust_bill$/) {
    push @header, 'Invoice Date', 'Amount', 'Balance';
    push @fields, date_format('_date'), 'charged', 'owed';
    $label = 'invoice';
  }
  elsif (/^cust_statement$/) {
    push @header, 'Statement Date', 'Amount', 'Balance';
    push @fields, date_format('_date'), 'charged', 'owed';
    $label = 'statement';
  }
  elsif (/^cust_pkg$/) {
    push @header, 'Package', 'Next Bill', 'Frequency';
    push @fields, sub {$_[0]->part_pkg->pkg}, date_format('bill'), 
        sub {$_[0]->part_pkg->freq_pretty};
    $label = 'package';
  }
  elsif (/^svc_acct$/) {
    push @header, 'Username', 'Domain';
    push @fields, 'username', 'domain';
    $label = 'service';
  }
  else {}
}

my @cust_header = FS::UI::Web::cust_header();
my @cust_fields = FS::UI::Web::cust_fields_subs();

my $objects = scalar(@targets);
my $customers = uniq(map {$_->cust_main->custnum} @targets);

sub date_format {
  my $column = shift;
  sub { my $obj = shift;
        my $value = $obj->get($column);
        $value ? time2str('%b %d %Y', $value) : '';
      };
}

</%init>