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