Event fee reporting, RT#81713
[freeside.git] / httemplate / search / cust_event_fee.html
1 <& elements/search.html,
2                  'title'       => 'Billing event fees',
3                  'html_init'   => include('.init'),
4                  'menubar'     => $menubar,
5                  'name'        => 'billing events',
6                  'query'       => $sql_query,
7                  'count_query' => $count_sql,
8                  'header'      => [ 'Event',
9                                     'Event date',
10                                     'Fee',
11                                     'Invoice',
12                                     'Invoice date',
13                                     FS::UI::Web::cust_header(),
14                                   ],
15                  'fields' => [
16                                'event',
17                                sub { time2str("%b %d %Y %T", $_[0]->_date) },
18                                'itemdesc',
19                                $inv_sub,
20                                sub { my $d = $_[0]->fee_cust_bill_date;
21                                      $d ? time2str("%b %d %Y %T", $d) : '' },
22                                \&FS::UI::Web::cust_fields,
23                              ],
24                 'align' => 'lrlrr'.FS::UI::Web::cust_aligns(),
25                 'links' => [
26                               '',
27                               '',
28                               '',
29                               $inv_link,
30                               $inv_link,
31                               ( map { $_ ne 'Cust. Status' ? $link_cust : '' }
32                                     FS::UI::Web::cust_header()
33                               ),
34                             ],
35                  'color' => [ 
36                               '',
37                               '',
38                               '',
39                               '',
40                               '',
41                               FS::UI::Web::cust_colors(),
42                             ],
43                  'style' => [ 
44                               '',
45                               '',
46                               '',
47                               '',
48                               '',
49                               FS::UI::Web::cust_styles(),
50                             ],
51 &>
52 <%once>
53
54 my $inv_sub = sub {
55   my $cust_event_fee = shift;
56   my $fee_invnum = $cust_event_fee->fee_invnum;
57   $fee_invnum || ( $cust_event_fee->nextbill ? '(next bill)' : '(not yet)' );
58 };
59
60 my $inv_link = sub {
61   $_[0]->fee_invnum
62     ?  [ "${p}view/cust_bill.cgi?", 'fee_invnum' ]
63     : '';
64 };
65
66 my $link_cust = sub {
67   my $cust_event_fee = shift;
68   $cust_event_fee->custnum
69     ? [ "${p}view/cust_main.cgi?", 'custnum' ]
70     : '';
71 };
72
73 </%once>
74 <%shared>
75 my @scalars = qw(); #qw( agentnum status custnum invnum pkgnum failed );
76 my @lists = qw( eventpart );
77 my %search;
78 </%shared>
79 <%init>
80
81 my $curuser = $FS::CurrentUser::CurrentUser;
82
83 die "access denied"
84   unless $curuser->access_right('Billing event reports');
85
86 for my $param (@scalars) {
87   $search{$param} = scalar( $cgi->param($param) )
88     if $cgi->param($param);
89 }
90
91 #lists
92 foreach my $param (@lists) {
93   $search{$param} = [ $cgi->param($param) ];
94 }
95
96 my($beginning, $ending) = FS::UI::Web::parse_beginning_ending($cgi);
97 $search{'beginning'} = $beginning;
98 $search{'ending'}    = $ending;
99
100 my $where = ' WHERE '. FS::cust_event->search_sql_where( \%search );
101
102 if ( $cgi->param('billpkgnum') eq 'NULL' ) {
103   $where .= ' AND billpkgnum IS NULL';
104 } elsif ( $cgi->param('billpkgnum') eq 'NOT NULL' ) {
105   $where .= ' AND billpkgnum IS NOT NULL';
106 }
107
108 my $join = '
109   LEFT JOIN cust_event USING (eventnum)
110   LEFT JOIN cust_bill_pkg USING (billpkgnum)
111   LEFT JOIN cust_bill AS fee_cust_bill USING (invnum)
112   LEFT JOIN part_fee ON (cust_event_fee.feepart = part_fee.feepart )
113   '. FS::cust_event->join_sql();
114
115 my $sql_query = {
116   'table'     => 'cust_event_fee',
117   'select'    => join(', ',
118                     'cust_event_fee.*',
119                     'cust_event.*',
120                     'part_event.*',
121                     'cust_bill_pkg.invnum AS fee_invnum',
122                     'fee_cust_bill._date AS fee_cust_bill_date',
123                     'part_fee.itemdesc',
124                     'cust_main.custnum',
125                     FS::UI::Web::cust_sql_fields(),
126                   ),
127   'hashref'   => {}, 
128   'extra_sql' => $where,
129   'order_by'  => 'ORDER BY eventfeenum ASC', #'ORDER BY _date ASC',
130   'addl_from' => $join,
131 };
132
133 my $count_sql = "SELECT COUNT(*) FROM cust_event_fee $join $where";
134 warn join(',', FS::UI::Web::cust_sql_fields() );
135
136 my $conf = new FS::Conf;
137
138 my $menubar = [];
139
140 if ( $curuser->access_right('Delete fees') ) {
141
142 #XXX delete fee link
143
144 #  push @$menubar, 'Re-print these events' =>
145 #                    "javascript:confirm_print_process()",
146 #                  'Re-email these events' =>
147 #                    "javascript:confirm_email_process()",
148 #                ;
149 #
150 #  push @$menubar, 'Re-fax these events' =>
151 #                    "javascript:confirm_fax_process()"
152 #    if $conf->exists('hylafax');
153
154 }
155
156 </%init>
157 <%def .init>
158 % # action is part of the target URL, don't need to pass it as a param
159 % foreach my $action (qw(print email fax)) {
160 <& /elements/progress-init.html,
161   $action.'_form',
162   [ @scalars, @lists, 'beginning', 'ending' ],
163   "../misc/${action}_events.cgi",
164   { 'message' => "Invoices re-${action}ed" }, #would be nice to show the number of them, but...
165   $action.'_', #key
166 &>
167 <FORM NAME="<% $action %>_form">
168 %   foreach my $param (@scalars, 'beginning', 'ending') {
169   <INPUT TYPE="hidden" NAME="<% $param %>" VALUE="<% $search{$param} |h %>">
170 %   }
171 %   foreach my $param (@lists) {
172 %     foreach my $value (@{ $search{$param} }) {
173   <INPUT TYPE="hidden" NAME="<% $param %>" VALUE="<% $value |h %>">
174 %     }
175 %   }
176 </FORM>
177 % } # foreach $action
178 <SCRIPT TYPE="text/javascript">
179
180 function confirm_print_process() {
181   if ( ! confirm("Are you sure you want to reprint these invoices?") ) {
182     return;
183   }
184   print_process();
185 }
186 function confirm_email_process() {
187   if ( ! confirm("Are you sure you want to re-email these invoices?") ) {
188     return;
189   }
190   email_process();
191 }
192 function confirm_fax_process() {
193   if ( ! confirm("Are you sure you want to re-fax these invoices?") ) {
194     return;
195   }
196   fax_process();
197 }
198 </SCRIPT>
199 </%def>