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