query billing events to see affected objects, #15142
[freeside.git] / httemplate / view / part_event-targets.html
1 <& /elements/header-popup.html,
2      {
3        'title'   => 'Event query - '.$part_event->event,
4      }
5 &>
6 % if ( $objects > 0 ) {
7   <% emt("[quant,_1,$label]", $objects) %>
8 %   if ( $part_event->eventtable ne 'cust_main' ) {
9   <% emt("belonging to [quant,_1,customer]", $customers) %>
10 %   }
11 <BR><BR>
12 <TABLE class="grid" cellspacing=0 width="100%" style="font-size:80%">
13   <TR style='background-color:#cccccc'>
14 %   foreach my $header ('Trigger', @cust_header, @header) {
15     <TH><% $header %></TH>
16 %   }
17   </TR>
18
19 %   my @rowcolors = ('ffffff','eeeeee');
20 %   my $row = 0;
21   <TR style="background-color:#<% $rowcolors[$row++ % 2] %>">
22 %   foreach my $object (@targets) {
23 %     # now works for all eventtables, including cust_pkg
24 %     my $link = $p . 'view/' . $part_event->eventtable . '.cgi?' .
25 %        $object->$pkey;
26     <TD><A target="_blank" href="<% $link %>">
27     <% ucfirst $label %> #<% $object->$pkey %></A></TD>
28
29 %     my $cust_main = $object->cust_main; # via Mixin
30 %     my $i = 0; # hack to avoid messing with cust_aligns/colors/styles
31 %     foreach (@cust_fields) {
32 %       if ($cust_header[$i] eq 'Cust. Status') {
33     <TD style="text-align:center;
34                font-weight:bold;
35                color:#<% $cust_main->cust_statuscolor %>"><% $_->($cust_main) %></TD>
36 %       }
37 %       else {
38     <TD><% $_->($cust_main) %></TD>
39 %       }
40 %     $i++;
41 %     } #foreach @cust_fields
42
43 %     foreach (@fields) {
44     <TD><% ref($_) eq 'CODE' ? $_->($object) : $object->$_ %></TD>
45 %     }
46   </TR>
47 %   } #foreach $object
48
49 </TABLE>
50
51 % } #object > 0
52 % else {
53
54 <% emt("No matching ${label}s found.") %>
55
56 %}
57 <& /elements/footer.html &>
58 <%once>
59 use List::MoreUtils qw(uniq);
60 </%once>
61 <%init>
62
63 my $curuser = $FS::CurrentUser::CurrentUser;
64 die "access denied"
65   unless $curuser->access_right('Edit billing events')
66         || $curuser->access_right('Edit global billing events');
67
68 my ($eventpart) = $cgi->keywords;
69 $eventpart =~ /^\d+$/ or die 'illegal eventpart';
70
71 my $part_event = FS::part_event->by_key($eventpart)
72   or die "Event definition $eventpart not found.\n";
73 my @targets = $part_event->targets;
74 my $total = @targets;
75
76 # in imitation of search/elements/search-html.html
77 my @header;
78 my @fields;
79 my ($pkey, $label);
80 $pkey = dbdef->table($part_event->eventtable)->primary_key;
81
82 for ($part_event->eventtable) {
83   if (/^cust_main$/) {
84     # very likely to appear in events
85     my %paybys = FS::payby->cust_payby2longname;
86     push @header, 'Balance', 'Payment Method';
87     push @fields, 'balance', sub{ $paybys{$_[0]->payby} };
88     $label = 'customer';
89   }
90   elsif (/^cust_bill$/) {
91     push @header, 'Invoice Date', 'Amount', 'Balance';
92     push @fields, date_format('_date'), 'charged', 'owed';
93     $label = 'invoice';
94   }
95   elsif (/^cust_statement$/) {
96     push @header, 'Statement Date', 'Amount', 'Balance';
97     push @fields, date_format('_date'), 'charged', 'owed';
98     $label = 'statement';
99   }
100   elsif (/^cust_pkg$/) {
101     push @header, 'Package', 'Next Bill', 'Frequency';
102     push @fields, sub {$_[0]->part_pkg->pkg}, date_format('bill'), 
103         sub {$_[0]->part_pkg->freq_pretty};
104     $label = 'package';
105   }
106   elsif (/^svc_acct$/) {
107     push @header, 'Username', 'Domain';
108     push @fields, 'username', 'domain';
109     $label = 'service';
110   }
111   else {}
112 }
113
114 my @cust_header = FS::UI::Web::cust_header();
115 my @cust_fields = FS::UI::Web::cust_fields_subs();
116
117 my $objects = scalar(@targets);
118 my $customers = uniq(map {$_->cust_main->custnum} @targets);
119
120 sub date_format {
121   my $column = shift;
122   sub { my $obj = shift;
123         my $value = $obj->get($column);
124         $value ? time2str('%b %d %Y', $value) : '';
125       };
126 }
127
128 </%init>