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