credit line items interface: set credit amounts, RT#18676
[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'       => 968, #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 %                    : '';
291 %
292 %  my $payment = exists($item->{'payment'})
293 %                  ? sprintf("-&nbsp;$money_char\%.2f", $item->{'payment'})
294 %                  : '';
295 %
296 %  $payment ||= sprintf( "<DEL>-&nbsp;$money_char\%.2f</DEL>",
297 %                        $item->{'void_payment'}
298 %                      )
299 %    if exists($item->{'void_payment'});
300 %
301 %  my $credit  = exists($item->{'credit'})
302 %                  ? sprintf("-&nbsp;$money_char\%.2f", $item->{'credit'})
303 %                  : '';
304 %
305 %  my $refund  = exists($item->{'refund'})
306 %                  ? sprintf("$money_char\%.2f", $item->{'refund'})
307 %                  : '';
308 %
309 %  my $target = exists($item->{'target'}) ? $item->{'target'} : '';
310 %
311 %  $balance += $item->{'charge'}  if exists $item->{'charge'};
312 %  $balance -= $item->{'payment'} if exists $item->{'payment'};
313 %  $balance -= $item->{'credit'}  if exists $item->{'credit'};
314 %  $balance += $item->{'refund'}  if exists $item->{'refund'};
315 %  $balance = sprintf("%.2f", $balance);
316 %  $balance =~ s/^\-0\.00$/0.00/; #yay ieee fp
317 %  ( my $showbalance = $money_char. $balance ) =~ s/^\$\-/-&nbsp;\$/;
318 %
319 %
320
321
322   <TR <% $display ? $display.' ID="old_history'.$old_history++.'"'  : ''%>>
323     <TD VALIGN="top" CLASS="grid" BGCOLOR="<% $bgcolor %>">
324 % unless ( !$target || $target{$target}++ ) { 
325
326         <A NAME="<% $target %>">
327 % } 
328
329       <% time2str($date_format, $item->{'date'}) %>
330 % if ( $target && $target{$target} == 1 ) { 
331
332         </A>
333 % } 
334
335       </FONT>
336     </TD>
337     <TD CLASS="grid" BGCOLOR="<% $bgcolor %>">
338       <% $item->{'desc'} %>
339     </TD>
340     <TD VALIGN="top" ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
341       <% $charge  %>
342     </TD>
343     <TD VALIGN="top" ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
344       <% $payment %>
345     </TD>
346     <TD VALIGN="top" ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
347       <% $credit  %>
348     </TD>
349     <TD VALIGN="top" ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
350       <% $refund  %>
351     </TD>
352     <TD VALIGN="top" ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
353       <% $showbalance %>
354     </TD>
355   </TR>
356 % } 
357
358 %if ( scalar(@history) && $hidden && ! $seen++ ) {
359 %  balance_forward_row($balance, $lastdate, $money_char);
360 %}
361
362 </TABLE>
363     </TD>
364   </TR>
365 </TABLE>
366
367 <SCRIPT TYPE="text/javascript">
368
369 function show_history () {
370   //alert('showing history!');
371
372   var balance_forward_row = document.getElementById('balance_forward_row');
373
374   balance_forward_row.style.display = 'none';
375   for ( var i = 0; i < <% $old_history %>; i++ ) {
376     var oldRow = document.getElementById('old_history'+i);
377     oldRow.style.display = '';
378   }
379
380 }
381
382 </SCRIPT>
383
384 <%init>
385
386 my( $cust_main ) = @_;
387 my $custnum = $cust_main->custnum;
388
389 my $conf = new FS::Conf;
390 my $date_format = $conf->config('date_format') || '%m/%d/%Y';
391
392 my $curuser = $FS::CurrentUser::CurrentUser;
393
394 my @payby = grep /\w/, $conf->config('payby');
395 #@payby = (qw( CARD DCRD CHEK DCHK LECB BILL CASH WEST COMP ))
396 @payby = (qw( CARD DCRD CHEK DCHK LECB BILL CASH COMP ))
397   unless @payby;
398 my %payby = map { $_=>1 } @payby;
399
400 my %status = (
401   'Queued'     => 'O', #Open
402   'In-transit' => 'I',
403   'Complete'   => 'R', #Resolved
404   'All'        => '',
405 );
406
407 #get payment history
408 my @history = ();
409
410 my %opt = (
411   ( map { $_ => scalar($conf->config($_)) }
412         qw( card_refund-days date_format )
413   ),
414   ( map { $_ => $conf->exists($_) } 
415         qw( deleteinvoices deletepayments deleterefunds pkg-balances )
416   )
417 );
418
419 $opt{'date_format'} ||= '%m/%d/%Y';
420
421 #legacy invoices
422 foreach my $legacy_cust_bill ($cust_main->legacy_cust_bill) {
423   push @history, {
424     'date'   => $legacy_cust_bill->_date,
425     'desc'   => include('payment_history/legacy_invoice.html', $legacy_cust_bill, %opt ),
426     'charge_nobal' => $legacy_cust_bill->charged,
427   };
428 }
429
430 #invoices
431 my $num_cust_bill = 0;
432 foreach my $cust_bill ($cust_main->cust_bill) {
433   push @history, {
434     'date'   => $cust_bill->_date,
435     'desc'   => include('payment_history/invoice.html', $cust_bill, %opt ),
436     'charge' => $cust_bill->charged,
437   };
438   $num_cust_bill++;
439 }
440
441 #statements
442 foreach my $cust_statement ($cust_main->cust_statement) {
443   push @history, {
444     'date'   => $cust_statement->_date,
445     'desc'   => include('payment_history/statement.html', $cust_statement, %opt ),
446     #'charge' => $cust_bill->charged,
447   };
448 }
449
450 #payments (some false laziness w/credits)
451 foreach my $cust_pay ($cust_main->cust_pay) {
452   push @history, {
453     'date'    => $cust_pay->_date,
454     'desc'    => include('payment_history/payment.html', $cust_pay, %opt ),
455     'payment' => $cust_pay->paid,
456     #'target'  => $target, #XXX
457   };
458 }
459
460 #pending payments 
461 foreach my $cust_pay_pending ($cust_main->cust_pay_pending) {
462   push @history, {
463     'date'    => $cust_pay_pending->_date,
464     'desc'    => include('payment_history/pending_payment.html', $cust_pay_pending, %opt ),
465     'void_payment' => $cust_pay_pending->paid, 
466   };
467 }
468
469
470 #voided payments
471 foreach my $cust_pay_void ($cust_main->cust_pay_void) {
472   push @history, {
473     'date'   => $cust_pay_void->_date,
474     'desc'   => include('payment_history/voided_payment.html', $cust_pay_void, %opt ),
475     'void_payment' => $cust_pay_void->paid,
476   };
477
478 }
479
480 #declined payments
481 foreach my $cust_pay_pending ($cust_main->cust_pay_pending_attempt) {
482   push @history, {
483     'date'    => $cust_pay_pending->_date,
484     'desc'    => include('payment_history/attempted_payment.html', $cust_pay_pending, %opt ),
485     'void_payment' => $cust_pay_pending->paid, #??
486     #'target'  => $target, #XXX
487   };
488 }
489
490 #credits (some false laziness w/payments)
491 foreach my $cust_credit ($cust_main->cust_credit) {
492   push @history, {
493     'date'   => $cust_credit->_date,
494     'desc'   => include('payment_history/credit.html', $cust_credit, %opt ),
495     'credit' => $cust_credit->amount,
496   };
497
498 }
499
500 #refunds
501 foreach my $cust_refund ($cust_main->cust_refund) {
502   push @history, {
503     'date'   => $cust_refund->_date,
504     'desc'   => include('payment_history/refund.html', $cust_refund, %opt),
505     'refund' => $cust_refund->refund,
506   };
507
508 }
509
510 sub translate_payby {
511     my ($payby,$payinfo) = (shift,shift);
512     my %payby = (
513         BILL    => $payinfo ? emt('Check #') : '',
514         CHEK    => emt('Electronic check '),
515         PREP    => emt('Prepaid card '),
516         CARD    => emt('Credit card #'),
517         COMP    => emt('Complimentary by '),
518         CASH    => emt('Cash'),
519         WEST    => emt('Western Union'),
520         MCRD    => emt('Manual credit card'),
521     );
522     $payby = (exists $payby{$payby}) ? $payby{$payby} : $payby; 
523     $payby;
524 };
525
526 sub translate_payby_refund {
527     my ($payby,$payinfo) = (shift,shift);
528     my %payby = (
529         BILL    => $payinfo ? emt('Check #') : emt('Check'),
530         CHEK    => emt('Electronic check '),
531         CARD    => emt('Credit card #'),
532         COMP    => emt('Complimentary by '),
533     );
534     $payby = (exists $payby{$payby}) ? $payby{$payby} : $payby; 
535     $payby;
536 };
537
538 sub translate_payinfo {
539     my $object = shift;
540     my $payby = $object->payby;
541     my $payinfo = $object->payinfo;
542
543     my $conf = new FS::Conf;
544
545     if ( $payby eq 'CARD' ) {
546         $payinfo = $object->paymask;
547     } elsif ( $payby eq 'CHEK' ) {
548         my( $account, $aba ) = split('@', $object->paymask );
549         $payinfo = emt("ABA [_1], Acct #[_2]",$aba,$account);
550     }
551
552     ($payby,$payinfo);
553 }
554
555 sub areyousure_link {
556     my ($url,$msg,$title,$label) = (shift,shift,shift,shift);
557     ' (<A HREF="javascript:areyousure(\''.$url.'\',\''.$msg.'\')" TITLE="'.$title.'">'.$label.'</A>)';
558 }
559
560 </%init>