add per-customer "Invoice reports" link, RT#13802
[freeside.git] / httemplate / view / cust_main / payment_history.html
1 <TABLE>
2   <TR>
3     <TD ALIGN="left">
4
5 %# payment links
6
7 % my $s = 0;
8 % if ( $payby{'BILL'} && $curuser->access_right(['Post payment', 'Post check payment' ]) ) { 
9   <% $s++ ? ' | ' : '' %>
10   <% include('/elements/popup_link-cust_main.html',
11                'label'       => 'Enter check payment',
12                'action'      => "${p}edit/cust_pay.cgi?popup=1;payby=BILL",
13                'cust_main'   => $cust_main,
14                'actionlabel' => 'Enter check payment',
15                'width'       => 392,
16                #default# 'height' => 336,
17             )
18   %>
19 % } 
20
21 % if ( $payby{'CASH'} && $curuser->access_right(['Post payment', 'Post cash payment']) ) { 
22   <% $s++ ? ' | ' : '' %>
23   <% include('/elements/popup_link-cust_main.html',
24                'label'       => 'Enter cash payment',
25                'action'      => "${p}edit/cust_pay.cgi?popup=1;payby=CASH",
26                'cust_main'   => $cust_main,
27                'actionlabel' => 'Enter cash payment',
28                'width'       => 392,
29                #default# 'height' => 336,
30             )
31   %>
32 % } 
33
34 % if ( $payby{'WEST'} && $curuser->access_right('Post payment') ) { 
35   <% $s++ ? ' | ' : '' %>
36   <A HREF="<% $p %>edit/cust_pay.cgi?payby=WEST;custnum=<% $custnum %>">Enter Western Union payment</A>
37 % } 
38
39 <BR>
40 % $s=0;
41
42 % if ( ( $payby{'CARD'} || $payby{'DCRD'} )
43 %        && $curuser->access_right(['Process payment', 'Process credit card payment'])
44 %        && ! $cust_main->is_encrypted($cust_main->payinfo)
45 %      ) {
46   <% $s++ ? ' | ' : '' %>
47   <A HREF="<% $p %>misc/payment.cgi?payby=CARD;custnum=<% $custnum %>">Process credit card payment</A>
48 % } 
49
50 % if ( ( $payby{'CHEK'} || $payby{'DCHK'} )
51 %        && $curuser->access_right(['Process payment', 'Process Echeck payment'])
52 %        && ! $cust_main->is_encrypted($cust_main->payinfo)
53 %      ) {
54   <% $s++ ? ' | ' : '' %>
55   <A HREF="<% $p %>misc/payment.cgi?payby=CHEK;custnum=<% $custnum %>">Process electronic check (ACH) payment</A>
56 % } 
57
58 % if ( $payby{'MCRD'} && $curuser->access_right('Post payment') ) { 
59   <% $s++ ? ' | ' : '' %>
60   <A HREF="<% $p %>edit/cust_pay.cgi?payby=MCRD;custnum=<% $custnum %>">Post manual (offline/POS) credit card payment</A>
61 % } 
62
63 <BR>
64
65 %# credit link
66
67 % if ( $curuser->access_right('Post credit') ) { 
68   <% include('/elements/popup_link-cust_main.html',
69                'label'       => 'Enter credit',
70                'action'      => "${p}edit/cust_credit.cgi",
71                'cust_main'   => $cust_main,
72                'actionlabel' => 'Enter credit',
73                'width'       => 616, #make room for reasons #540 default
74                #default# 'height' => 336,
75             )
76   %>
77   <BR>
78 % } 
79
80 %# refund links
81
82 % $s = 0;
83 % if ( $payby{'BILL'} && $curuser->access_right(['Post refund', 'Post check refund']) ) { 
84   <% $s++ ? ' | ' : '' %>
85   <% include('/elements/popup_link-cust_main.html',
86                'label'       => 'Enter check refund',
87                'action'      => "${p}edit/cust_refund.cgi?popup=1;payby=BILL",
88                'cust_main'   => $cust_main,
89                'actionlabel' => 'Enter check refund',
90                'width'       => 392,
91                #default# 'height' => 336,
92             )
93   %>
94 % } 
95
96 % if ( $payby{'CASH'} && $curuser->access_right(['Post refund', 'Post cash refund']) ) { 
97   <% $s++ ? ' | ' : '' %>
98   <% include('/elements/popup_link-cust_main.html',
99                'label'       => 'Enter cash refund',
100                'action'      => "${p}edit/cust_refund.cgi?popup=1;payby=CASH",
101                'cust_main'   => $cust_main,
102                'actionlabel' => 'Enter cash refund',
103                'width'       => 392,
104                #default# 'height' => 336,
105             )
106   %>
107 % } 
108
109 %# someday, perhaps.  very few gateways let you do unlinked refunds at all.
110 %# Authorize.net makes you sign a special form
111 %#
112 %#    % if ( ( $payby{'CARD'} || $payby{'DCRD'} )
113 %#    %        && $curuser->access_right('Process refund')
114 %#    %        && ! $cust_main->is_encrypted($cust_main->payinfo)
115 %#    %      ) {
116 %#      <% $s++ ? ' | ' : '' %>
117 %#      <A HREF="<% $p %>misc/refund.cgi?payby=CARD;custnum=<% $custnum %>">Process credit card refund</A>
118 %#    % } 
119 %#    
120 %#    % if ( ( $payby{'CHEK'} || $payby{'DCHK'} )
121 %#    %        && $curuser->access_right('Process refund')
122 %#    %        && ! $cust_main->is_encrypted($cust_main->payinfo)
123 %#    %      ) {
124 %#      <% $s++ ? ' | ' : '' %>
125 %#      <A HREF="<% $p %>misc/refund.cgi?payby=CHEK;custnum=<% $custnum %>">Process electronic check (ACH) refund</A>
126 %#    % } 
127
128 % if ( $payby{'MCRD'} && $curuser->access_right('Post refund') ) { 
129   <% $s++ ? ' | ' : '' %>
130   <A HREF="<% $p %>edit/cust_refund.cgi?payby=MCRD;custnum=<% $custnum %>">Post manual (offline/POS) credit card refund</A>
131 % } 
132
133     </TD>
134     <TD ALIGN="right" VALIGN="top">
135
136 %# invoice reports
137 % if ( $curuser->access_right('List invoices') ) { 
138   <A HREF="<% $p %>search/report_cust_bill.html?custnum=<% $custnum %>">Invoice reports</A>
139 % } 
140 <BR>
141
142 %# XXX payments, credits, refund reports
143
144 %# tax exemption link
145
146 % my $view_exemptions = $curuser->access_right('View customer tax exemptions');
147 % my $add_adjustment = ( $conf->exists('enable_tax_adjustments')
148 %                       && $curuser->access_right('Add customer tax adjustment')
149 %                      );
150 % if ( $view_exemptions || $add_adjustment ) {
151
152 %   if ( $view_exemptions ) {
153       <A HREF="<% $p %>search/cust_tax_exempt_pkg.cgi?custnum=<% $custnum %>">View tax exemptions</A>
154       <% $add_adjustment ? '|' : '' %>
155 %   } 
156
157 %   if ( $add_adjustment ) {
158       <% include('/elements/popup_link.html', {
159            'action' => $p.'edit/cust_tax_adjustment.html?custnum='. $cust_main->custnum,
160            'label'  => 'Add tax adjustment',
161            'actionlabel' => 'Add tax adjustment',
162            #'color'  => '#333399',
163            #'width' => 763,
164            'height' => 200,
165          })
166       %>
167       |
168       <A HREF="<% $p %>search/cust_tax_adjustment.html?custnum=<% $custnum %>">View tax adjustments</A>
169 %   } 
170
171   <BR>
172 % }
173
174 %# batched payment links
175
176 % if ( ( $conf->exists('batch-enable') || $conf->config('batch-enable_payby') )
177 %      && $curuser->access_right('View customer batched payments')
178 %    )
179 % { 
180     View batched payments:
181 %   foreach my $status (qw( Queued In-transit Complete All )) {
182       <A HREF="<% $p %>search/cust_pay_batch.cgi?status=<% $status{$status} %>;custnum=<% $custnum %>"><% $status %></A> 
183       <% $status ne 'All' ? '|' : '' %>
184 %   }
185     <BR>
186 % } 
187
188 %# pending payment links
189
190 % if ( $curuser->access_right('View customer pending payments')
191 %      && scalar($cust_main->cust_pay_pending)
192 %    )
193 % {
194     <A HREF="<% $p %>search/cust_pay_pending.html?magic=_date;statusNOT=done;custnum=<% $custnum %>">View pending payments</A><BR>
195 % }
196
197     </TD>
198   </TR>
199   <TR>
200     <TD COLSPAN=2>
201
202 %# and now the table
203
204 <% include("/elements/table-grid.html") %>
205 % my $bgcolor1 = '#eeeeee';
206 %   my $bgcolor2 = '#ffffff';
207 %   my $bgcolor = '';
208
209 <TR>
210   <TH CLASS="grid" BGCOLOR="#cccccc">Date</TH>
211   <TH CLASS="grid" BGCOLOR="#cccccc">Description</TH>
212   <TH CLASS="grid" BGCOLOR="#cccccc"><FONT SIZE=-1>Invoice</FONT></TH>
213   <TH CLASS="grid" BGCOLOR="#cccccc"><FONT SIZE=-1>Payment</FONT></TH>
214   <TH CLASS="grid" BGCOLOR="#cccccc"><FONT SIZE=-1>In-house<BR>Credit</FONT></TH>
215   <TH CLASS="grid" BGCOLOR="#cccccc"><FONT SIZE=-1>Refund</FONT></TH>
216   <TH CLASS="grid" BGCOLOR="#cccccc"><FONT SIZE=-1>Balance</FONT></TH>
217 </TR>
218
219 %#display payment history
220
221 %my $money_char = $conf->config('money_char') || '$';
222 %
223 %sub balance_forward_row {
224 %  my( $b, $date, $money_char ) = @_;
225 %  ( my $balance_forward = $money_char. $b ) =~ s/^\$\-/-&nbsp;\$/;
226
227    <TR ID="balance_forward_row">
228      <TD CLASS="grid" BGCOLOR="#dddddd">
229        <% time2str($date_format, $date) %>
230      </TD>
231
232      <TD CLASS="grid" BGCOLOR="#dddddd">
233        <I>Starting balance on <% time2str($date_format, $date) %></I>
234        (<A HREF="javascript:void(0);" onClick="show_history();">show prior history</A>)
235      </TD>
236
237      <TD CLASS="grid" BGCOLOR="#dddddd"></TD>
238      <TD CLASS="grid" BGCOLOR="#dddddd"></TD>
239      <TD CLASS="grid" BGCOLOR="#dddddd"></TD>
240      <TD CLASS="grid" BGCOLOR="#dddddd"></TD>
241      <TD CLASS="grid" BGCOLOR="#dddddd" ALIGN="right"><I><% $balance_forward %></I></TD>
242
243    </TR>
244 %}
245 %
246 %my $balance = 0;
247 %my %target = ();
248 %
249 %my $years =  $conf->config('payment_history-years') || 2;
250 %my $older_than = time - $years * 31556926; #60*60*24*365.2422
251 %my $hidden = 0;
252 %my $seen = 0;
253 %my $old_history = 0;
254 %my $lastdate = 0;
255 %
256 %foreach my $item ( sort { $a->{'date'} <=> $b->{'date'} } @history ) {
257 %
258 %  $lastdate = $item->{'date'};
259 %
260 %  my $display;
261 %  if ( $item->{'date'} < $older_than ) {
262 %    $display = ' STYLE="display:none" ';
263 %    $hidden = 1;
264 %  } else {
265 %
266 %    $display = '';
267 %
268 %    if ( $hidden && ! $seen++ ) {
269 %      balance_forward_row($balance, $item->{'date'}, $money_char);
270 %    }
271 %
272 %  }
273 %
274 %  if ( $bgcolor eq $bgcolor1 ) {
275 %    $bgcolor = $bgcolor2;
276 %  } else {
277 %    $bgcolor = $bgcolor1;
278 %  }
279 %
280 %  my $charge  = exists($item->{'charge'})
281 %                  ? sprintf("$money_char\%.2f", $item->{'charge'})
282 %                  : '';
283 %
284 %  my $payment = exists($item->{'payment'})
285 %                  ? sprintf("-&nbsp;$money_char\%.2f", $item->{'payment'})
286 %                  : '';
287 %
288 %  $payment ||= sprintf( "<DEL>-&nbsp;$money_char\%.2f</DEL>",
289 %                        $item->{'void_payment'}
290 %                      )
291 %    if exists($item->{'void_payment'});
292 %
293 %  my $credit  = exists($item->{'credit'})
294 %                  ? sprintf("-&nbsp;$money_char\%.2f", $item->{'credit'})
295 %                  : '';
296 %
297 %  my $refund  = exists($item->{'refund'})
298 %                  ? sprintf("$money_char\%.2f", $item->{'refund'})
299 %                  : '';
300 %
301 %  my $target = exists($item->{'target'}) ? $item->{'target'} : '';
302 %
303 %  $balance += $item->{'charge'}  if exists $item->{'charge'};
304 %  $balance -= $item->{'payment'} if exists $item->{'payment'};
305 %  $balance -= $item->{'credit'}  if exists $item->{'credit'};
306 %  $balance += $item->{'refund'}  if exists $item->{'refund'};
307 %  $balance = sprintf("%.2f", $balance);
308 %  $balance =~ s/^\-0\.00$/0.00/; #yay ieee fp
309 %  ( my $showbalance = $money_char. $balance ) =~ s/^\$\-/-&nbsp;\$/;
310 %
311 %
312
313
314   <TR <% $display ? $display.' ID="old_history'.$old_history++.'"'  : ''%>>
315     <TD VALIGN="top" CLASS="grid" BGCOLOR="<% $bgcolor %>">
316 % unless ( !$target || $target{$target}++ ) { 
317
318         <A NAME="<% $target %>">
319 % } 
320
321       <% time2str($date_format, $item->{'date'}) %>
322 % if ( $target && $target{$target} == 1 ) { 
323
324         </A>
325 % } 
326
327       </FONT>
328     </TD>
329     <TD CLASS="grid" BGCOLOR="<% $bgcolor %>">
330       <% $item->{'desc'} %>
331     </TD>
332     <TD VALIGN="top" ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
333       <% $charge  %>
334     </TD>
335     <TD VALIGN="top" ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
336       <% $payment %>
337     </TD>
338     <TD VALIGN="top" ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
339       <% $credit  %>
340     </TD>
341     <TD VALIGN="top" ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
342       <% $refund  %>
343     </TD>
344     <TD VALIGN="top" ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
345       <% $showbalance %>
346     </TD>
347   </TR>
348 % } 
349
350 %if ( scalar(@history) && $hidden && ! $seen++ ) {
351 %  balance_forward_row($balance, $lastdate, $money_char);
352 %}
353
354 </TABLE>
355     </TD>
356   </TR>
357 </TABLE>
358
359 <SCRIPT TYPE="text/javascript">
360
361 function show_history () {
362   //alert('showing history!');
363
364   var balance_forward_row = document.getElementById('balance_forward_row');
365
366   balance_forward_row.style.display = 'none';
367   for ( var i = 0; i < <% $old_history %>; i++ ) {
368     var oldRow = document.getElementById('old_history'+i);
369     oldRow.style.display = '';
370   }
371
372 }
373
374 </SCRIPT>
375
376 <%init>
377
378 my( $cust_main ) = @_;
379 my $custnum = $cust_main->custnum;
380
381 my $conf = new FS::Conf;
382 my $date_format = $conf->config('date_format') || '%m/%d/%Y';
383
384 my $curuser = $FS::CurrentUser::CurrentUser;
385
386 my @payby = grep /\w/, $conf->config('payby');
387 #@payby = (qw( CARD DCRD CHEK DCHK LECB BILL CASH WEST COMP ))
388 @payby = (qw( CARD DCRD CHEK DCHK LECB BILL CASH COMP ))
389   unless @payby;
390 my %payby = map { $_=>1 } @payby;
391
392 my %status = (
393   'Queued'     => 'O', #Open
394   'In-transit' => 'I',
395   'Complete'   => 'R', #Resolved
396   'All'        => '',
397 );
398
399 #get payment history
400 my @history = ();
401
402 my %opt = (
403   ( map { $_ => scalar($conf->config($_)) }
404         qw( card_refund-days date_format )
405   ),
406   ( map { $_ => $conf->exists($_) } 
407         qw( deleteinvoices deletepayments deleterefunds pkg-balances )
408   )
409 );
410
411 #invoices
412 foreach my $cust_bill ($cust_main->cust_bill) {
413   push @history, {
414     'date'   => $cust_bill->_date,
415     'desc'   => include('payment_history/invoice.html', $cust_bill, %opt ),
416     'charge' => $cust_bill->charged,
417   };
418 }
419
420 #statements
421 foreach my $cust_statement ($cust_main->cust_statement) {
422   push @history, {
423     'date'   => $cust_statement->_date,
424     'desc'   => include('payment_history/statement.html', $cust_statement, %opt ),
425     #'charge' => $cust_bill->charged,
426   };
427 }
428
429 #payments (some false laziness w/credits)
430 foreach my $cust_pay ($cust_main->cust_pay) {
431   push @history, {
432     'date'    => $cust_pay->_date,
433     'desc'    => include('payment_history/payment.html', $cust_pay, %opt ),
434     'payment' => $cust_pay->paid,
435     #'target'  => $target, #XXX
436   };
437 }
438
439 #pending payments 
440 foreach my $cust_pay_pending ($cust_main->cust_pay_pending) {
441   push @history, {
442     'date'    => $cust_pay_pending->_date,
443     'desc'    => include('payment_history/pending_payment.html', $cust_pay_pending, %opt ),
444     'void_payment' => $cust_pay_pending->paid, 
445   };
446 }
447
448
449 #voided payments
450 foreach my $cust_pay_void ($cust_main->cust_pay_void) {
451   push @history, {
452     'date'   => $cust_pay_void->_date,
453     'desc'   => include('payment_history/voided_payment.html', $cust_pay_void, %opt ),
454     'void_payment' => $cust_pay_void->paid,
455   };
456
457 }
458
459 #declined payments
460 foreach my $cust_pay_pending ($cust_main->cust_pay_pending_attempt) {
461   push @history, {
462     'date'    => $cust_pay_pending->_date,
463     'desc'    => include('payment_history/attempted_payment.html', $cust_pay_pending, %opt ),
464     'void_payment' => $cust_pay_pending->paid, #??
465     #'target'  => $target, #XXX
466   };
467 }
468
469 #credits (some false laziness w/payments)
470 foreach my $cust_credit ($cust_main->cust_credit) {
471   push @history, {
472     'date'   => $cust_credit->_date,
473     'desc'   => include('payment_history/credit.html', $cust_credit, %opt ),
474     'credit' => $cust_credit->amount,
475   };
476
477 }
478
479 #refunds
480 foreach my $cust_refund ($cust_main->cust_refund) {
481   push @history, {
482     'date'   => $cust_refund->_date,
483     'desc'   => include('payment_history/refund.html', $cust_refund),
484     'refund' => $cust_refund->refund,
485   };
486
487 }
488
489 </%init>