Update httemplate/elements/selectlayers.html
[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   <BR>
74 % } 
75
76 %# refund links
77
78 % $s = 0;
79 % if ( $payby{'BILL'} && $curuser->access_right(['Post refund', 'Post check refund']) ) { 
80   <% $s++ ? ' | ' : '' %>
81   <& /elements/popup_link-cust_main.html,
82                'label'       => emt('Enter check refund'),
83                'action'      => "${p}edit/cust_refund.cgi?popup=1;payby=BILL",
84                'cust_main'   => $cust_main,
85                'actionlabel' => emt('Enter check refund'),
86                'width'       => 392,
87   &>
88 % } 
89
90 % if ( $payby{'CASH'} && $curuser->access_right(['Post refund', 'Post cash refund']) ) { 
91   <% $s++ ? ' | ' : '' %>
92   <& /elements/popup_link-cust_main.html,
93                'label'       => emt('Enter cash refund'),
94                'action'      => "${p}edit/cust_refund.cgi?popup=1;payby=CASH",
95                'cust_main'   => $cust_main,
96                'actionlabel' => emt('Enter cash refund'),
97                'width'       => 392,
98   &>
99 % } 
100
101 %# someday, perhaps.  very few gateways let you do unlinked refunds at all.
102 %# Authorize.net makes you sign a special form
103 %#
104 %#    % if ( ( $payby{'CARD'} || $payby{'DCRD'} )
105 %#    %        && $curuser->access_right('Process refund')
106 %#    %        && ! $cust_main->is_encrypted($cust_main->payinfo)
107 %#    %      ) {
108 %#      <% $s++ ? ' | ' : '' %>
109 %#      <A HREF="<% $p %>misc/refund.cgi?payby=CARD;custnum=<% $custnum %>">Process credit card refund</A>
110 %#    % } 
111 %#    
112 %#    % if ( ( $payby{'CHEK'} || $payby{'DCHK'} )
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=CHEK;custnum=<% $custnum %>">Process electronic check (ACH) refund</A>
118 %#    % } 
119
120 % if ( $payby{'MCRD'} && $curuser->access_right('Post refund') ) { 
121   <% $s++ ? ' | ' : '' %>
122   <A HREF="<% $p %>edit/cust_refund.cgi?payby=MCRD;custnum=<% $custnum %>"><% mt('Post manual (offline/POS) credit card refund') |h %></A>
123 % } 
124
125     </TD>
126     <TD ALIGN="right" VALIGN="top">
127
128 %# invoice reports, combined statement
129 % if ( $curuser->access_right('List invoices') ) { 
130 %   if ( $conf->exists('cust_main-print_statement_link')
131 %        and $num_cust_bill > 0 ) {
132   <A HREF="<% $p %>view/cust_main_statement-pdf.cgi?<% $custnum %>"><%
133   mt('Print a current statement') |h %></A>
134   <BR>
135 %   }
136   <A HREF="<% $p %>search/report_cust_bill.html?custnum=<% $custnum %>"><% mt('Invoice reports') |h %></A>
137 % } 
138 <BR>
139
140 %# XXX payments, credits, refund reports
141
142 %# tax exemption link
143
144 % my $view_exemptions = $curuser->access_right('View customer tax exemptions');
145 % my $add_adjustment = ( $conf->exists('enable_tax_adjustments')
146 %                       && $curuser->access_right('Add customer tax adjustment')
147 %                      );
148 % if ( $view_exemptions || $add_adjustment ) {
149
150 %   if ( $view_exemptions ) {
151       <A HREF="<% $p %>search/cust_tax_exempt_pkg.cgi?custnum=<% $custnum %>"><% mt('View tax exemptions') |h %></A>
152       <% $add_adjustment ? '|' : '' %>
153 %   } 
154
155 %   if ( $add_adjustment ) {
156       <& /elements/popup_link.html, {
157            'action' => $p.'edit/cust_tax_adjustment.html?custnum='. $cust_main->custnum,
158            'label'  => emt('Add tax adjustment'),
159            'actionlabel' => emt('Add tax adjustment'),
160            'height' => 200,
161          }
162       &>
163       |
164       <A HREF="<% $p %>search/cust_tax_adjustment.html?custnum=<% $custnum %>"><% mt('View tax adjustments') |h %></A>
165 %   } 
166
167   <BR>
168 % }
169
170 %# batched payment links
171
172 % if ( ( $conf->exists('batch-enable') || $conf->config('batch-enable_payby') )
173 %      && $curuser->access_right('View customer batched payments')
174 %    )
175 % { 
176     <% mt('View batched payments:') |h %> 
177 %   foreach my $status (qw( Queued In-transit Complete All )) {
178       <A HREF="<% $p %>search/cust_pay_batch.cgi?status=<% $status{$status} %>;custnum=<% $custnum %>"><% mt($status) |h %></A> 
179       <% $status ne 'All' ? '|' : '' %>
180 %   }
181     <BR>
182 % } 
183
184 %# pending payment links
185
186 % if ( $curuser->access_right('View customer pending payments')
187 %      && scalar($cust_main->cust_pay_pending)
188 %    )
189 % {
190     <A HREF="<% $p %>search/cust_pay_pending.html?magic=_date;statusNOT=done;custnum=<% $custnum %>"><% mt('View pending payments') |h %></A><BR>
191 % }
192
193     </TD>
194   </TR>
195   <TR>
196     <TD COLSPAN=2>
197
198 %# and now the table
199
200 <& /elements/table-grid.html &>
201 % my $bgcolor1 = '#eeeeee';
202 %   my $bgcolor2 = '#ffffff';
203 %   my $bgcolor = '';
204
205 <TR>
206   <TH CLASS="grid" BGCOLOR="#cccccc"><% mt('Date') |h %></TH>
207   <TH CLASS="grid" BGCOLOR="#cccccc"><% mt('Description') |h %></TH>
208   <TH CLASS="grid" BGCOLOR="#cccccc"><FONT SIZE=-1><% mt('Invoice') |h %></FONT></TH>
209   <TH CLASS="grid" BGCOLOR="#cccccc"><FONT SIZE=-1><% mt('Payment') |h %></FONT></TH>
210   <TH CLASS="grid" BGCOLOR="#cccccc"><FONT SIZE=-1><% mt('In-house Credit') |h %></FONT></TH>
211   <TH CLASS="grid" BGCOLOR="#cccccc"><FONT SIZE=-1><% mt('Refund') |h %></FONT></TH>
212   <TH CLASS="grid" BGCOLOR="#cccccc"><FONT SIZE=-1><% mt('Balance') |h %></FONT></TH>
213 </TR>
214
215 %#display payment history
216
217 %my $money_char = $conf->config('money_char') || '$';
218 %
219 %sub balance_forward_row {
220 %  my( $b, $date, $money_char ) = @_;
221 %  ( my $balance_forward = $money_char. $b ) =~ s/^\$\-/-&nbsp;\$/;
222
223    <TR ID="balance_forward_row">
224      <TD CLASS="grid" BGCOLOR="#dddddd">
225        <% time2str($date_format, $date) %>
226      </TD>
227
228      <TD CLASS="grid" BGCOLOR="#dddddd">
229        <I><% mt("Starting balance on [_1]", time2str($date_format, $date) ) |h %></I>
230        (<A HREF="javascript:void(0);" onClick="show_history();"><% mt('show prior history') |h %></A>)
231      </TD>
232
233      <TD CLASS="grid" BGCOLOR="#dddddd"></TD>
234      <TD CLASS="grid" BGCOLOR="#dddddd"></TD>
235      <TD CLASS="grid" BGCOLOR="#dddddd"></TD>
236      <TD CLASS="grid" BGCOLOR="#dddddd"></TD>
237      <TD CLASS="grid" BGCOLOR="#dddddd" ALIGN="right"><I><% $balance_forward %></I></TD>
238
239    </TR>
240 %}
241 %
242 %my $balance = 0;
243 %my %target = ();
244 %
245 %my $years =  $conf->config('payment_history-years') || 2;
246 %my $older_than = time - $years * 31556926; #60*60*24*365.2422
247 %my $hidden = 0;
248 %my $seen = 0;
249 %my $old_history = 0;
250 %my $lastdate = 0;
251 %
252 %foreach my $item ( sort { $a->{'date'} <=> $b->{'date'} } @history ) {
253 %
254 %  $lastdate = $item->{'date'};
255 %
256 %  my $display;
257 %  if ( $item->{'date'} < $older_than ) {
258 %    $display = ' STYLE="display:none" ';
259 %    $hidden = 1;
260 %  } else {
261 %
262 %    $display = '';
263 %
264 %    if ( $hidden && ! $seen++ ) {
265 %      balance_forward_row($balance, $item->{'date'}, $money_char);
266 %    }
267 %
268 %  }
269 %
270 %  if ( $bgcolor eq $bgcolor1 ) {
271 %    $bgcolor = $bgcolor2;
272 %  } else {
273 %    $bgcolor = $bgcolor1;
274 %  }
275 %
276 %  my $charge  = exists($item->{'charge'})
277 %                  ? sprintf("$money_char\%.2f", $item->{'charge'})
278 %                  : exists($item->{'charge_nobal'})
279 %                    ? sprintf("$money_char\%.2f", $item->{'charge_nobal'})
280 %                    : exists($item->{'void_charge'})
281 %                      ? sprintf("<DEL>$money_char\%.2f</DEL>", $item->{'void_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 $opt{'date_format'} ||= '%m/%d/%Y';
412
413 #legacy invoices
414 foreach my $legacy_cust_bill ($cust_main->legacy_cust_bill) {
415   push @history, {
416     'date'   => $legacy_cust_bill->_date,
417     'desc'   => include('payment_history/legacy_invoice.html', $legacy_cust_bill, %opt ),
418     'charge_nobal' => $legacy_cust_bill->charged,
419   };
420 }
421
422 #invoices
423 my $num_cust_bill = 0;
424 foreach my $cust_bill ($cust_main->cust_bill) {
425   push @history, {
426     'date'   => $cust_bill->_date,
427     'desc'   => include('payment_history/invoice.html', $cust_bill, %opt ),
428     'charge' => $cust_bill->charged,
429   };
430   $num_cust_bill++;
431 }
432
433 #voided invoices
434 foreach my $cust_bill_void ($cust_main->cust_bill_void) {
435   push @history, {
436     'date'        => $cust_bill_void->_date,
437     'desc'        => include('payment_history/voided_invoice.html', $cust_bill_void, %opt ),
438     'void_charge' => $cust_bill_void->charged,
439   };
440 }
441
442 #statements
443 foreach my $cust_statement ($cust_main->cust_statement) {
444   push @history, {
445     'date'   => $cust_statement->_date,
446     'desc'   => include('payment_history/statement.html', $cust_statement, %opt ),
447     #'charge' => $cust_bill->charged,
448   };
449 }
450
451 #payments (some false laziness w/credits)
452 foreach my $cust_pay ($cust_main->cust_pay) {
453   push @history, {
454     'date'    => $cust_pay->_date,
455     'desc'    => include('payment_history/payment.html', $cust_pay, %opt ),
456     'payment' => $cust_pay->paid,
457     #'target'  => $target, #XXX
458   };
459 }
460
461 #pending payments 
462 foreach my $cust_pay_pending ($cust_main->cust_pay_pending) {
463   push @history, {
464     'date'    => $cust_pay_pending->_date,
465     'desc'    => include('payment_history/pending_payment.html', $cust_pay_pending, %opt ),
466     'void_payment' => $cust_pay_pending->paid, 
467   };
468 }
469
470
471 #voided payments
472 foreach my $cust_pay_void ($cust_main->cust_pay_void) {
473   push @history, {
474     'date'   => $cust_pay_void->_date,
475     'desc'   => include('payment_history/voided_payment.html', $cust_pay_void, %opt ),
476     'void_payment' => $cust_pay_void->paid,
477   };
478
479 }
480
481 #declined payments
482 foreach my $cust_pay_pending ($cust_main->cust_pay_pending_attempt) {
483   push @history, {
484     'date'    => $cust_pay_pending->_date,
485     'desc'    => include('payment_history/attempted_payment.html', $cust_pay_pending, %opt ),
486     'void_payment' => $cust_pay_pending->paid, #??
487     #'target'  => $target, #XXX
488   };
489 }
490
491 #credits (some false laziness w/payments)
492 foreach my $cust_credit ($cust_main->cust_credit) {
493   push @history, {
494     'date'   => $cust_credit->_date,
495     'desc'   => include('payment_history/credit.html', $cust_credit, %opt ),
496     'credit' => $cust_credit->amount,
497   };
498
499 }
500
501 #refunds
502 foreach my $cust_refund ($cust_main->cust_refund) {
503   push @history, {
504     'date'   => $cust_refund->_date,
505     'desc'   => include('payment_history/refund.html', $cust_refund, %opt),
506     'refund' => $cust_refund->refund,
507   };
508
509 }
510
511 sub translate_payby {
512     my ($payby,$payinfo) = (shift,shift);
513     my %payby = (
514         FS::payby->payby2shortname,
515         BILL    => $payinfo ? emt('Check #') : '',
516         CHEK    => emt('Electronic check '),
517         PREP    => emt('Prepaid card '),
518         CARD    => emt('Credit card #'),
519         COMP    => emt('Complimentary by '),
520         #CASH    => emt('Cash'),
521         #WEST    => emt('Western Union'),
522         #MCRD    => emt('Manual credit card'),
523     );
524     $payby = (exists $payby{$payby}) ? $payby{$payby} : $payby; 
525     $payby;
526 };
527
528 sub translate_payby_refund {
529     my ($payby,$payinfo) = (shift,shift);
530     my %payby = (
531         FS::payby->payby2shortname,
532         BILL    => $payinfo ? emt('Check #') : emt('Check'),
533         CHEK    => emt('Electronic check '),
534         CARD    => emt('Credit card #'),
535         COMP    => emt('Complimentary by '),
536     );
537     $payby = (exists $payby{$payby}) ? $payby{$payby} : $payby; 
538     $payby;
539 };
540
541 sub translate_payinfo {
542     my $object = shift;
543     my $payby = $object->payby;
544     my $payinfo = $object->payinfo;
545
546     my $conf = new FS::Conf;
547
548     if ( $payby eq 'CARD' ) {
549         $payinfo = $object->paymask;
550     } elsif ( $payby eq 'CHEK' ) {
551         my( $account, $aba ) = split('@', $object->paymask );
552         $payinfo = emt("ABA [_1], Acct #[_2]",$aba,$account);
553     }
554
555     ($payby,$payinfo);
556 }
557
558 sub areyousure_link {
559     my ($url,$msg,$title,$label) = (shift,shift,shift,shift);
560     ' (<A HREF="javascript:areyousure(\''.$url.'\',\''.$msg.'\')" TITLE="'.$title.'">'.$label.'</A>)';
561 }
562
563 </%init>