finish component-izing overlib links
[freeside.git] / httemplate / view / cust_main / payment_history.html
1 <BR><BR><A NAME="history"><FONT SIZE="+2">Payment History</FONT></A><BR>
2
3 % my $s = 0;
4 % if ( $payby{'BILL'} && $curuser->access_right('Post payment') ) { 
5   <% $s++ ? ' | ' : '' %>
6   <% include('/elements/popup_link-cust_main.html',
7                'label'       => 'Enter check payment',
8                'action'      => "${p}edit/cust_pay.cgi?popup=1;payby=BILL",
9                'cust_main'   => $cust_main,
10                'actionlabel' => 'Enter check payment',
11                'width'       => 392,
12                #default# 'height' => 336,
13             )
14   %>
15 % } 
16
17 % if ( $payby{'CASH'} && $curuser->access_right('Post payment') ) { 
18   <% $s++ ? ' | ' : '' %>
19   <% include('/elements/popup_link-cust_main.html',
20                'label'       => 'Enter cash payment',
21                'action'      => "${p}edit/cust_pay.cgi?popup=1;payby=CASH",
22                'cust_main'   => $cust_main,
23                'actionlabel' => 'Enter cash payment',
24                'width'       => 392,
25                #default# 'height' => 336,
26             )
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 %>">Enter Western Union payment</A>
33 % } 
34
35 % if ( ( $payby{'CARD'} || $payby{'DCRD'} )
36 %        && $curuser->access_right('Process payment')
37 %        && ! $cust_main->is_encrypted($cust_main->payinfo)
38 %      ) {
39   <% $s++ ? ' | ' : '' %>
40   <A HREF="<% $p %>misc/payment.cgi?payby=CARD;custnum=<% $custnum %>">Process credit card payment</A>
41 % } 
42
43 % if ( ( $payby{'CHEK'} || $payby{'DCHK'} )
44 %        && $curuser->access_right('Process payment')
45 %        && ! $cust_main->is_encrypted($cust_main->payinfo)
46 %      ) {
47   <% $s++ ? ' | ' : '' %>
48   <A HREF="<% $p %>misc/payment.cgi?payby=CHEK;custnum=<% $custnum %>">Process electronic check (ACH) payment</A>
49 % } 
50
51 % if ( $payby{'MCRD'} && $curuser->access_right('Post payment') ) { 
52   <% $s++ ? ' | ' : '' %>
53   <A HREF="<% $p %>edit/cust_pay.cgi?payby=MCRD;custnum=<% $custnum %>">Post manual (offline) credit card payment</A>
54 % } 
55
56 <BR>
57
58 % if ( $curuser->access_right('Post credit') ) { 
59   <% include('/elements/popup_link-cust_main.html',
60                'label'       => 'Enter credit',
61                'action'      => "${p}edit/cust_credit.cgi",
62                'cust_main'   => $cust_main,
63                'actionlabel' => 'Enter credit',
64                'width'       => 392,
65                #default# 'height' => 336,
66             )
67   %>
68   <BR>
69 % } 
70
71 % if ( $curuser->access_right('View customer tax exemptions') ) { 
72   <A HREF="<% $p %>search/cust_tax_exempt_pkg.cgi?custnum=<% $custnum %>">View tax exemptions</A>
73   <BR>
74 % } 
75
76 % if ( $conf->exists('batch-enable')
77 %      && $curuser->access_right('View customer batched payments')
78 %    ) { 
79   View batched payments:
80 %   foreach my $status (qw( Queued In-transit Complete All )) {
81       <A HREF="<% $p %>search/cust_pay_batch.cgi?status=<% $status{$status} %>;custnum=<% $custnum %>"><% $status %></A> 
82       <% $status ne 'All' ? '|' : '' %>
83 %   }
84   <BR>
85 % } 
86
87 %#get payment history
88 %my @history = ();
89 %
90 %#invoices
91 %foreach my $cust_bill ($cust_main->cust_bill) {
92 %  my $pre = ( $cust_bill->owed > 0 )
93 %              ? '<B><FONT SIZE="+1" COLOR="#FF0000">Open '
94 %              : '';
95 %  my $post = ( $cust_bill->owed > 0 ) ? '</FONT></B>' : '';
96 %  my $invnum = $cust_bill->invnum;
97 %  my $link = $curuser->access_right('View invoices')
98 %               ? qq!<A HREF="${p}view/cust_bill.cgi?$invnum">!
99 %               : '';
100 %  my $events = '';
101 %  if ( $cust_bill->num_cust_event
102 %       && (    $curuser->access_right('Billing event reports')
103 %            || $curuser->access_right('View customer billing events')
104 %          )
105 %     ) {
106 %    $events =
107 %      qq!<BR><FONT SIZE="-1"><A HREF="${p}search/cust_event.html?invnum=!.
108 %      $cust_bill->invnum. '">(&nbsp;View invoice events&nbsp;)</A></FONT>';
109 %  }
110 %  push @history, {
111 %    'date'   => $cust_bill->_date,
112 %    'desc'   => $link. $pre.
113 %                "Invoice #$invnum (Balance \$". $cust_bill->owed. ')'.
114 %                $post. ( $link ? '</A>' : '' ). $events,
115 %    'charge' => $cust_bill->charged,
116 %  };
117 %}
118 %
119 %#payments (some false laziness w/credits)
120 %foreach my $cust_pay ($cust_main->cust_pay) {
121 %
122 %  my $payby = $cust_pay->payby;
123 %
124 %  my $payinfo;
125 %  if ( $payby eq 'CARD' ) {
126 %    $payinfo = $cust_pay->paymask;
127 %  } elsif ( $payby eq 'CHEK' ) {
128 %    my( $account, $aba ) = split('@', $cust_pay->paymask );
129 %    $payinfo = "ABA $aba, Acct #$account";
130 %  } else {
131 %    $payinfo = $cust_pay->payinfo;
132 %  }
133 %  my @cust_bill_pay = $cust_pay->cust_bill_pay;
134 %  my @cust_pay_refund = $cust_pay->cust_pay_refund;
135 %
136 %  my $target = "$payby$payinfo";
137 %  $payby =~ s/^BILL$/Check #/ if $payinfo;
138 %  $payby =~ s/^CHEK$/Electronic check /;
139 %  $payby =~ s/^PREP$/Prepaid card /;
140 %  $payby =~ s/^CARD$/Credit card #/; 
141 %  $payby =~ s/^COMP$/Complimentary by /; 
142 %  $payby =~ s/^CASH$/Cash/;
143 %  $payby =~ s/^WEST$/Western Union/;
144 %  $payby =~ s/^MCRD$/Manual credit card/;
145 %  $payby =~ s/^BILL$//;
146 %  my $info = $payby ? "($payby$payinfo)" : '';
147 %
148 %  my( $pre, $post, $desc, $apply, $ext ) = ( '', '', '', '', '' );
149 %  if (    scalar(@cust_bill_pay)   == 0
150 %       && scalar(@cust_pay_refund) == 0 ) {
151 %    #completely unapplied
152 %    $pre = '<B><FONT COLOR="#FF0000">Unapplied ';
153 %    $post = '</FONT></B>';
154 %    if ( $curuser->access_right('Apply payment') ) {
155 %      $apply = ' ('. include( '/elements/popup_link.html',
156 %                                'label'       => 'apply',
157 %                                'action'      => "${p}edit/cust_bill_pay.cgi?".
158 %                                                 $cust_pay->paynum,
159 %                                'actionlabel' => 'Apply payment',
160 %                                'width'       => 392,
161 %                                #default# 'height' => 336,
162 %                            ).
163 %                ')';
164 %    }
165 %  } elsif (    scalar(@cust_bill_pay)   == 1
166 %            && scalar(@cust_pay_refund) == 0
167 %            && $cust_pay->unapplied == 0     ) {
168 %    #applied to one invoice, the usual situation
169 %    $desc = ' applied to Invoice #'. $cust_bill_pay[0]->invnum;
170 %  } elsif (    scalar(@cust_bill_pay)   == 0
171 %            && scalar(@cust_pay_refund) == 1
172 %            && $cust_pay->unapplied == 0     ) {
173 %    #applied to one refund
174 %    $desc = ' refunded on '. time2str("%D", $cust_pay_refund[0]->_date);
175 %  } else {
176 %    #complicated
177 %    $desc = '<BR>';
178 %    foreach my $app ( sort { $a->_date <=> $b->_date }
179 %                           ( @cust_bill_pay, @cust_pay_refund ) ) {
180 %      if ( $app->isa('FS::cust_bill_pay') ) {
181 %        $desc .= '&nbsp;&nbsp;'.
182 %                 '$'. $app->amount.
183 %                 ' applied to Invoice #'. $app->invnum.
184 %                 '<BR>';
185 %                 #' on '. time2str("%D", $cust_bill_pay->_date).
186 %      } elsif ( $app->isa('FS::cust_pay_refund') ) {
187 %        $desc .= '&nbsp;&nbsp;'.
188 %                 '$'. $app->amount.
189 %                 ' refunded on '. time2str("%D", $app->_date).
190 %                 '<BR>';
191 %      } else {
192 %        die "$app is not a FS::cust_bill_pay or FS::cust_pay_refund";
193 %      }
194 %    }
195 %    if ( $cust_pay->unapplied > 0 ) {
196 %      $desc .= '&nbsp;&nbsp;'.
197 %               '<B><FONT COLOR="#FF0000">$'.
198 %               $cust_pay->unapplied. ' unapplied</FONT></B>';
199 %      if ( $curuser->access_right('Apply payment') ) {
200 %        $desc = ' ('. include( '/elements/popup_link.html',
201 %                                 'label'      => 'apply',
202 %                                 'action'     => "${p}edit/cust_bill_pay.cgi?".
203 %                                                 $cust_pay->paynum,
204 %                                 'actionlabel' => 'Apply payment',
205 %                                 'width'      => 392,
206 %                                 #default# 'height' => 336,
207 %                            ).
208 %                ')';
209 %      }
210 %      $desc .= '<BR>';
211 %    }
212 %  }
213 %
214 %  my $view =
215 %    ' ('. include('/elements/popup_link.html',
216 %                    'label'     => 'view receipt',
217 %                    'action'    => "${p}view/cust_pay.html?link=popup;paynum=".
218 %                                    $cust_pay->paynum,
219 %                    'actionlabel' => 'Payment Receipt',
220 %                 ).
221 %     ')';
222 %
223 %  my $refund = '';
224 %  my $refund_days = $conf->config('card_refund-days') || 120;
225 %  if (    $cust_pay->closed !~ /^Y/i
226 %       && $cust_pay->payby =~ /^(CARD|CHEK)$/
227 %       && time-$cust_pay->_date < $refund_days*86400
228 %       && $cust_pay->unrefunded > 0
229 %       && $curuser->access_right('Refund payment')
230 %  ) {
231 %    $refund = qq! (<A HREF="${p}edit/cust_refund.cgi?payby=$1;!.
232 %              qq!paynum=!. $cust_pay->paynum. '"'.
233 %              qq! TITLE="Send a refund for this payment to the payment gateway"!.
234 %              qq!>refund</A>)!;
235 %  }
236 %
237 %  my $void = '';
238 %  if (    $cust_pay->closed !~ /^Y/i
239 %       && (    ( $cust_pay->payby eq 'CARD'
240 %                 && $curuser->access_right('Credit card void')
241 %               )
242 %            || ( $cust_pay->payby eq 'CHEK'
243 %                 && $curuser->access_right('Echeck void')
244 %               )
245 %            || ( $cust_pay->payby !~ /^(CARD|CHEK)$/
246 %                 && $curuser->access_right('Regular void')
247 %               )
248 %          )
249 %     )
250 %  {
251 %    $void = qq! (<A HREF="javascript:areyousure('!.
252 %            qq!${p}misc/void-cust_pay.cgi?!. $cust_pay->paynum.
253 %            qq!', 'Are you sure you want to void this payment?')"!.
254 %            qq! TITLE="Void this payment from the database!.
255 %              ( $cust_pay->payby =~ /^(CARD|CHEK)$/
256 %                ? ' (do not send anything to the payment gateway)'
257 %                : '' 
258 %              ). '"'.
259 %            qq!>void</A>)!;
260 %  }
261 %
262 %  my $delete = '';
263 %  if ( $cust_pay->closed !~ /^Y/i
264 %       && $conf->exists('deletepayments')
265 %       && $curuser->access_right('Delete payment')
266 %     )
267 %  {
268 %    $delete = qq! (<A HREF="javascript:areyousure('!.
269 %              qq!${p}misc/delete-cust_pay.cgi?!. $cust_pay->paynum.
270 %              qq!', 'Are you sure you want to delete this payment?')"!.
271 %              qq! TITLE="Delete this payment from the database completely - not recommended"!.
272 %              qq!>delete</A>)!;
273 %  }
274 %
275 %  my $unapply = '';
276 %  if (    $cust_pay->closed !~ /^Y/i
277 %       && scalar(@cust_bill_pay)           
278 %       && $curuser->access_right('Unapply payment')
279 %     )
280 %  {
281 %    $unapply = qq! (<A HREF="javascript:areyousure('!.
282 %               qq!${p}misc/unapply-cust_pay.cgi?!. $cust_pay->paynum.
283 %               qq!', 'Are you sure you want to unapply this payment?')"!.
284 %               qq! TITLE="Keep this payment, but dissociate it from the invoices it is currently applied against"!.
285 %               qq!>unapply</A>)!;
286 %  }
287 %
288 %  my $otaker = $cust_pay->otaker;
289 %  $otaker = '<i>auto billing</i>'          if $otaker eq 'fs_daily';
290 %  $otaker = '<i>customer self-service</i>' if $otaker eq 'fs_selfservice';
291 %
292 %  push @history, {
293 %    'date'    => $cust_pay->_date,
294 %    'desc'    => $pre. "Payment$post by $otaker $info$desc".
295 %                 "$view$apply$refund$void$delete$unapply",
296 %    'payment' => $cust_pay->paid,
297 %    'target'  => $target,
298 %  };
299 %}
300 %
301 %#voided payments
302 %foreach my $cust_pay_void ($cust_main->cust_pay_void) {
303 %
304 %  my $payby = $cust_pay_void->payby;
305 %  my $payinfo = $payby eq 'CARD'
306 %                  ? $cust_pay_void->paymask
307 %                  : $cust_pay_void->payinfo;
308 %
309 %  $payby =~ s/^BILL$/Check #/ if $payinfo;
310 %  $payby =~ s/^CHEK$/Electronic check /;
311 %  $payby =~ s/^BILL$//;
312 %  $payby =~ s/^(CARD|COMP)$/$1 /;
313 %  my $info = $payby ? " ($payby$payinfo)" : '';
314 %
315 %  my $unvoid = '';
316 %  if ( $cust_pay_void->closed !~ /^Y/i
317 %       && $curuser->access_right('Unvoid')
318 %     )
319 %  {
320 %    $unvoid = qq! (<A HREF="javascript:areyousure('!.
321 %              qq!${p}misc/unvoid-cust_pay_void.cgi?!. $cust_pay_void->paynum.
322 %              qq!', 'Are you sure you want to unvoid this payment?')"!.
323 %              qq! TITLE="Unvoid this payment from the database!.
324 %                ( $cust_pay_void->payby =~ /^(CARD|CHEK)$/
325 %                  ? ' (do not send anything to the payment gateway)'
326 %                  : '' 
327 %                ). '"'.
328 %              qq!>unvoid</A>)!;
329 %  }
330 %
331 %  push @history, {
332 %    'date'   => $cust_pay_void->_date,
333 %    'desc'   => "<DEL>Payment $info</DEL> <I>voided ".
334 %                time2str("%D", $cust_pay_void->void_date).
335 %                " by ". $cust_pay_void->otaker. '</i>'. $unvoid,
336 %    'void_payment' => $cust_pay_void->paid,
337 %  };
338 %
339 %}
340 %
341 %#credits (some false laziness w/payments)
342 %foreach my $cust_credit ($cust_main->cust_credit) {
343 %
344 %  my @cust_credit_bill = $cust_credit->cust_credit_bill;
345 %  my @cust_credit_refund = $cust_credit->cust_credit_refund;
346 %
347 %  my( $pre, $post, $desc, $apply, $ext ) = ( '', '', '', '', '' );
348 %  if (    scalar(@cust_credit_bill)   == 0
349 %       && scalar(@cust_credit_refund) == 0 ) {
350 %    #completely unapplied
351 %    $pre = '<B><FONT COLOR="#FF0000">Unapplied ';
352 %    $post = '</FONT></B>';
353 %    if ( $curuser->access_right('Apply credit') ) {
354 %      $apply = ' ('. include( '/elements/popup_link.html',
355 %                                'label'    => 'apply',
356 %                                'action'   => "${p}edit/cust_credit_bill.cgi?".
357 %                                              $cust_credit->crednum,
358 %                                'actionlabel' => 'Apply credit',
359 %                                'width'    => 392,
360 %                                #default# 'height' => 336,
361 %                            ).
362 %                ')';
363 %    }
364 %  } elsif (    scalar(@cust_credit_bill)   == 1
365 %            && scalar(@cust_credit_refund) == 0
366 %            && $cust_credit->credited == 0      ) {
367 %    #applied to one invoice, the usual situation
368 %    $desc = ' applied to Invoice #'. $cust_credit_bill[0]->invnum;
369 %  } elsif (    scalar(@cust_credit_bill)   == 0
370 %            && scalar(@cust_credit_refund) == 1
371 %            && $cust_credit->credited == 0      ) {
372 %    #applied to one refund
373 %    $desc = ' refunded on '.  time2str("%D", $cust_credit_refund[0]->_date);
374 %  } else {
375 %    #complicated
376 %    $desc = '<BR>';
377 %    foreach my $app ( sort { $a->_date <=> $b->_date }
378 %                           ( @cust_credit_bill, @cust_credit_refund ) ) {
379 %      if ( $app->isa('FS::cust_credit_bill') ) {
380 %        $desc .= '&nbsp;&nbsp;'.
381 %                 '$'. $app->amount.
382 %                 ' applied to Invoice #'. $app->invnum.
383 %                 '<BR>';
384 %                 #' on '. time2str("%D", $app->_date).
385 %      } elsif ( $app->isa('FS::cust_credit_refund') ) {
386 %        $desc .= '&nbsp;&nbsp;'.
387 %                 '$'. $app->amount.
388 %                 ' refunded on '. time2str("%D", $app->_date).
389 %                 '<BR>';
390 %      } else {
391 %        die "$app is not a FS::cust_credit_bill or a FS::cust_credit_refund";
392 %      }
393 %    }
394 %    if ( $cust_credit->credited > 0 ) {
395 %      $desc .= '&nbsp;&nbsp;<B><FONT COLOR="#FF0000">$'.
396 %               $cust_credit->credited. ' unapplied</FONT></B>';
397 %      if ( $curuser->access_right('Apply credit') ) {
398 %        $desc = ' ('. include( '/elements/popup_link.html',
399 %                                'label'       => 'apply',
400 %                                'action'      => "${p}edit/cust_credit_bill.cgi?".
401 %                                                 $cust_credit->crednum,
402 %                                'actionlabel' => 'Apply credit',
403 %                                'width'       => 392,
404 %                                #default# 'height' => 336,
405 %                            ).
406 %                ')';
407 %      }
408 %      $desc .= '<BR>';
409 %    }
410 %  }
411 %#
412 %  my $delete = '';
413 %  if ( $cust_credit->closed !~ /^Y/i
414 %
415 %       #s'pose deleting a credit isn't bad like deleting a payment
416 %       # and this needs to be generally available until we have credit voiding..
417 %       #&& $conf->exists('deletecredits')
418 %
419 %       && $curuser->access_right('Delete credit')
420 %     )
421 %  {
422 %    $delete = qq! (<A HREF="javascript:areyousure('!.
423 %              qq!${p}misc/delete-cust_credit.cgi?!. $cust_credit->crednum.
424 %              qq!', 'Are you sure you want to delete this credit?')">!.
425 %              qq!delete</A>)!;
426 %  }
427 %  
428 %  my $unapply = '';
429 %  if (    $cust_credit->closed !~ /^Y/i
430 %       && scalar(@cust_credit_bill)
431 %       && $curuser->access_right('Unapply credit')
432 %     )
433 %  {
434 %    $unapply = qq! (<A HREF="javascript:areyousure('!.
435 %               qq!${p}misc/unapply-cust_credit.cgi?!. $cust_credit->crednum.
436 %               qq!', 'Are you sure you want to unapply this credit?')">!.
437 %               qq!unapply</A>)!;
438 %  }
439 %  
440 %  push @history, {
441 %    'date'   => $cust_credit->_date,
442 %    'desc'   => $pre. "Credit$post by ". $cust_credit->otaker.
443 %                ( $cust_credit->reason
444 %                   ? ' ('. $cust_credit->reason. ')'
445 %                   : ''
446 %               ).
447 %                "$desc$apply$delete$unapply",
448 %    'credit' => $cust_credit->amount,
449 %  };
450 %
451 %}
452 %
453 %#refunds
454 %foreach my $cust_refund ($cust_main->cust_refund) {
455 %
456 %  my $payby = $cust_refund->payby;
457 %  my $payinfo = $payby eq 'CARD'
458 %                  ? $cust_refund->paymask
459 %                  : $cust_refund->payinfo;
460 %
461 %  $payby =~ s/^BILL$/Check #/ if $payinfo;
462 %  $payby =~ s/^CHEK$/Electronic check /;
463 %  $payby =~ s/^(CARD|COMP)$/$1 /;
464 %
465 %  my $delete = '';
466 %  if ( $cust_refund->closed !~ /^Y/i
467 %       && $conf->exists('deleterefunds')
468 %       && $curuser->access_right('Delete refund')
469 %     )
470 %  {
471 %    $delete = qq! (<A HREF="javascript:areyousure('!.
472 %              qq!${p}misc/delete-cust_refund.cgi?!. $cust_refund->refundnum.
473 %              qq!', 'Are you sure you want to delete this refund?')"!.
474 %              qq! TITLE="Delete this refund from the database completely - not recommended"!.
475 %              qq!>delete</A>)!;
476 %  }
477 %
478 %  push @history, {
479 %    'date'   => $cust_refund->_date,
480 %    'desc'   => "Refund ($payby$payinfo) by ". $cust_refund->otaker. "<BR>".
481 %                $delete,
482 %    'refund' => $cust_refund->refund,
483 %  };
484 %
485 %}
486 %
487 %
488
489
490 <% include("/elements/table-grid.html") %>
491 % my $bgcolor1 = '#eeeeee';
492 %   my $bgcolor2 = '#ffffff';
493 %   my $bgcolor = '';
494 %
495
496
497 <TR>
498   <TH CLASS="grid" BGCOLOR="#cccccc">Date</TH>
499   <TH CLASS="grid" BGCOLOR="#cccccc">Description</TH>
500   <TH CLASS="grid" BGCOLOR="#cccccc"><FONT SIZE=-1>Charge</FONT></TH>
501   <TH CLASS="grid" BGCOLOR="#cccccc"><FONT SIZE=-1>Payment</FONT></TH>
502   <TH CLASS="grid" BGCOLOR="#cccccc"><FONT SIZE=-1>In-house<BR>Credit</FONT></TH>
503   <TH CLASS="grid" BGCOLOR="#cccccc"><FONT SIZE=-1>Refund</FONT></TH>
504   <TH CLASS="grid" BGCOLOR="#cccccc"><FONT SIZE=-1>Balance</FONT></TH>
505 </TR>
506 %
507 %#display payment history
508 %
509 %sub balance_forward_row {
510 %  my( $b, $date ) = @_;
511 %  my $conf = new FS::Conf;
512 %  my $money_char = $conf->config('money_char') || '$';
513 %  ( my $balance_forward = $money_char. $b ) =~ s/^\$\-/-&nbsp;\$/;
514
515    <TR ID="balance_forward_row">
516      <TD CLASS="grid" BGCOLOR="#dddddd">
517        <% time2str("%D",$date) %>
518      </TD>
519
520      <TD CLASS="grid" BGCOLOR="#dddddd">
521        <I>Starting balance on <% time2str("%D",$date) %></I>
522        (<A HREF="javascript:void(0);" onClick="show_history();">show prior history</A>)
523      </TD>
524
525      <TD CLASS="grid" BGCOLOR="#dddddd"></TD>
526      <TD CLASS="grid" BGCOLOR="#dddddd"></TD>
527      <TD CLASS="grid" BGCOLOR="#dddddd"></TD>
528      <TD CLASS="grid" BGCOLOR="#dddddd"></TD>
529      <TD CLASS="grid" BGCOLOR="#dddddd"><I><% $balance_forward %></I></TD>
530
531    </TR>
532 %}
533 %
534 %my $balance = 0;
535 %my %target = ();
536 %my $money_char = $conf->config('money_char') || '$';
537 %
538 %my $years =  $conf->config('payment_history-years') || 2;
539 %my $older_than = time - $years * 31556736; #60*60*24*365.24
540 %my $hidden = 0;
541 %my $seen = 0;
542 %my $old_history = 0;
543 %my $lastdate = 0;
544 %
545 %foreach my $item ( sort { $a->{'date'} <=> $b->{'date'} } @history ) {
546 %
547 %  $lastdate = $item->{'date'};
548 %
549 %  my $display;
550 %  if ( $item->{'date'} < $older_than ) {
551 %    $display = ' STYLE="display:none" ';
552 %    $hidden = 1;
553 %  } else {
554 %
555 %    $display = '';
556 %
557 %    if ( $hidden && ! $seen++ ) {
558 %      balance_forward_row($balance, $item->{'date'});
559 %    }
560 %
561 %  }
562 %
563 %  if ( $bgcolor eq $bgcolor1 ) {
564 %    $bgcolor = $bgcolor2;
565 %  } else {
566 %    $bgcolor = $bgcolor1;
567 %  }
568 %
569 %  my $charge  = exists($item->{'charge'})
570 %                  ? sprintf("$money_char\%.2f", $item->{'charge'})
571 %                  : '';
572 %
573 %  my $payment = exists($item->{'payment'})
574 %                  ? sprintf("-&nbsp;$money_char\%.2f", $item->{'payment'})
575 %                  : '';
576 %
577 %  $payment ||= sprintf( "<DEL>-&nbsp;$money_char\%.2f</DEL>",
578 %                        $item->{'void_payment'}
579 %                      )
580 %    if exists($item->{'void_payment'});
581 %
582 %  my $credit  = exists($item->{'credit'})
583 %                  ? sprintf("-&nbsp;$money_char\%.2f", $item->{'credit'})
584 %                  : '';
585 %
586 %  my $refund  = exists($item->{'refund'})
587 %                  ? sprintf("$money_char\%.2f", $item->{'refund'})
588 %                  : '';
589 %
590 %  my $target = exists($item->{'target'}) ? $item->{'target'} : '';
591 %
592 %  $balance += $item->{'charge'}  if exists $item->{'charge'};
593 %  $balance -= $item->{'payment'} if exists $item->{'payment'};
594 %  $balance -= $item->{'credit'}  if exists $item->{'credit'};
595 %  $balance += $item->{'refund'}  if exists $item->{'refund'};
596 %  $balance = sprintf("%.2f", $balance);
597 %  $balance =~ s/^\-0\.00$/0.00/; #yay ieee fp
598 %  ( my $showbalance = $money_char. $balance ) =~ s/^\$\-/-&nbsp;\$/;
599 %
600 %
601
602
603   <TR <% $display ? $display.' ID="old_history'.$old_history++.'"'  : ''%>>
604     <TD CLASS="grid" BGCOLOR="<% $bgcolor %>">
605 % unless ( !$target || $target{$target}++ ) { 
606
607         <A NAME="<% $target %>">
608 % } 
609
610       <% time2str("%D",$item->{'date'}) %>
611 % if ( $target && $target{$target} == 1 ) { 
612
613         </A>
614 % } 
615
616       </FONT>
617     </TD>
618     <TD CLASS="grid" BGCOLOR="<% $bgcolor %>">
619       <% $item->{'desc'} %>
620     </TD>
621     <TD ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
622       <% $charge  %>
623     </TD>
624     <TD ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
625       <% $payment %>
626     </TD>
627     <TD ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
628       <% $credit  %>
629     </TD>
630     <TD ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
631       <% $refund  %>
632     </TD>
633     <TD ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
634       <% $showbalance %>
635     </TD>
636   </TR>
637 % } 
638
639 %if ( scalar(@history) && $hidden && ! $seen++ ) {
640 %  balance_forward_row($balance, $lastdate);
641 %}
642
643 </TABLE>
644
645 <SCRIPT TYPE="text/javascript">
646
647 function show_history () {
648   //alert('showing history!');
649
650   var balance_forward_row = document.getElementById('balance_forward_row');
651
652   balance_forward_row.style.display = 'none';
653   for ( var i = 0; i < <% $old_history %>; i++ ) {
654     var oldRow = document.getElementById('old_history'+i);
655     oldRow.style.display = '';
656   }
657
658 }
659
660 </SCRIPT>
661
662 <%init>
663
664 my( $cust_main ) = @_;
665 my $custnum = $cust_main->custnum;
666
667 my $conf = new FS::Conf;
668
669 my $curuser = $FS::CurrentUser::CurrentUser;
670
671 my @payby = grep /\w/, $conf->config('payby');
672 #@payby = (qw( CARD DCRD CHEK DCHK LECB BILL CASH WEST COMP ))
673 @payby = (qw( CARD DCRD CHEK DCHK LECB BILL CASH COMP ))
674   unless @payby;
675 my %payby = map { $_=>1 } @payby;
676
677 my %status = (
678   'Queued'     => 'O', #Open
679   'In-transit' => 'I',
680   'Complete'   => 'R', #Resolved
681   'All'        => '',
682 );
683
684 </%init>