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