event refactor, landing on HEAD!
[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 %    $apply = qq! (<A HREF="javascript:void(0);" onClick="overlib( OLiframeContent('${p}edit/cust_bill_pay.cgi?!.
130 %             $cust_pay->paynum.
131 %             qq!', 392, 336, 'cust_bill_pay_popup' ), CAPTION, 'Apply payment', STICKY, AUTOSTATUSCAP, MIDX, 0, MIDY, 0, DRAGGABLE, CLOSECLICK ); return false;">apply</A>)!;
132 %
133 %  } elsif (    scalar(@cust_bill_pay)   == 1
134 %            && scalar(@cust_pay_refund) == 0
135 %            && $cust_pay->unapplied == 0     ) {
136 %    #applied to one invoice, the usual situation
137 %    $desc = ' applied to Invoice #'. $cust_bill_pay[0]->invnum;
138 %  } elsif (    scalar(@cust_bill_pay)   == 0
139 %            && scalar(@cust_pay_refund) == 1
140 %            && $cust_pay->unapplied == 0     ) {
141 %    #applied to one refund
142 %    $desc = ' refunded on '. time2str("%D", $cust_pay_refund[0]->_date);
143 %  } else {
144 %    #complicated
145 %    $desc = '<BR>';
146 %    foreach my $app ( sort { $a->_date <=> $b->_date }
147 %                           ( @cust_bill_pay, @cust_pay_refund ) ) {
148 %      if ( $app->isa('FS::cust_bill_pay') ) {
149 %        $desc .= '&nbsp;&nbsp;'.
150 %                 '$'. $app->amount.
151 %                 ' applied to Invoice #'. $app->invnum.
152 %                 '<BR>';
153 %                 #' on '. time2str("%D", $cust_bill_pay->_date).
154 %      } elsif ( $app->isa('FS::cust_pay_refund') ) {
155 %        $desc .= '&nbsp;&nbsp;'.
156 %                 '$'. $app->amount.
157 %                 ' refunded on '. time2str("%D", $app->_date).
158 %                 '<BR>';
159 %      } else {
160 %        die "$app is not a FS::cust_bill_pay or FS::cust_pay_refund";
161 %      }
162 %    }
163 %    if ( $cust_pay->unapplied > 0 ) {
164 %      $desc .= '&nbsp;&nbsp;'.
165 %               '<B><FONT COLOR="#FF0000">$'.
166 %               $cust_pay->unapplied. ' unapplied</FONT></B>'.
167 %               qq! (<A HREF="javascript:void(0);" onClick="overlib( OLiframeContent('${p}edit/cust_bill_pay.cgi?!.
168 %               $cust_pay->paynum. 
169 %               qq!', 392, 336, 'cust_bill_pay_popup' ), CAPTION, 'Apply payment', STICKY, AUTOSTATUSCAP, MIDX, 0, MIDY, 0, DRAGGABLE, CLOSECLICK ); return false;">apply</A>)!.
170 %               '<BR>';
171 %    }
172 %  }
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 %  push @history, {
240 %    'date'    => $cust_pay->_date,
241 %    'desc'    => $pre. "Payment$post$info$desc".
242 %                 "$apply$refund$void$delete$unapply",
243 %    'payment' => $cust_pay->paid,
244 %    'target'  => $target,
245 %  };
246 %}
247 %
248 %#voided payments
249 %foreach my $cust_pay_void ($cust_main->cust_pay_void) {
250 %
251 %  my $payby = $cust_pay_void->payby;
252 %  my $payinfo = $payby eq 'CARD'
253 %                  ? $cust_pay_void->paymask
254 %                  : $cust_pay_void->payinfo;
255 %
256 %  $payby =~ s/^BILL$/Check #/ if $payinfo;
257 %  $payby =~ s/^CHEK$/Electronic check /;
258 %  $payby =~ s/^BILL$//;
259 %  $payby =~ s/^(CARD|COMP)$/$1 /;
260 %  my $info = $payby ? " ($payby$payinfo)" : '';
261 %
262 %  my $unvoid = '';
263 %  if ( $cust_pay_void->closed !~ /^Y/i
264 %       && $curuser->access_right('Unvoid')
265 %     )
266 %  {
267 %    $unvoid = qq! (<A HREF="javascript:areyousure('!.
268 %              qq!${p}misc/unvoid-cust_pay_void.cgi?!. $cust_pay_void->paynum.
269 %              qq!', 'Are you sure you want to unvoid this payment?')"!.
270 %              qq! TITLE="Unvoid this payment from the database!.
271 %                ( $cust_pay_void->payby =~ /^(CARD|CHEK)$/
272 %                  ? ' (do not send anything to the payment gateway)'
273 %                  : '' 
274 %                ). '"'.
275 %              qq!>unvoid</A>)!;
276 %  }
277 %
278 %  push @history, {
279 %    'date'   => $cust_pay_void->_date,
280 %    'desc'   => "<DEL>Payment $info</DEL> <I>voided ".
281 %                time2str("%D", $cust_pay_void->void_date).
282 %                " by ". $cust_pay_void->otaker. '</i>'. $unvoid,
283 %    'void_payment' => $cust_pay_void->paid,
284 %  };
285 %
286 %}
287 %
288 %#credits (some false laziness w/payments)
289 %foreach my $cust_credit ($cust_main->cust_credit) {
290 %
291 %  my @cust_credit_bill = $cust_credit->cust_credit_bill;
292 %  my @cust_credit_refund = $cust_credit->cust_credit_refund;
293 %
294 %  my( $pre, $post, $desc, $apply, $ext ) = ( '', '', '', '', '' );
295 %  if (    scalar(@cust_credit_bill)   == 0
296 %       && scalar(@cust_credit_refund) == 0 ) {
297 %    #completely unapplied
298 %    $pre = '<B><FONT COLOR="#FF0000">Unapplied ';
299 %    $post = '</FONT></B>';
300 %    $apply = qq! (<A HREF="javascript:void(0);" onClick="overlib( OLiframeContent('${p}edit/cust_credit_bill.cgi?!.
301 %             $cust_credit->crednum.
302 %             qq!', 392, 336, 'cust_credit_bill_popup' ), CAPTION, 'Apply credit', STICKY, AUTOSTATUSCAP, MIDX, 0, MIDY, 0, DRAGGABLE, CLOSECLICK ); return false;">apply</A>)!;
303 %  } elsif (    scalar(@cust_credit_bill)   == 1
304 %            && scalar(@cust_credit_refund) == 0
305 %            && $cust_credit->credited == 0      ) {
306 %    #applied to one invoice, the usual situation
307 %    $desc = ' applied to Invoice #'. $cust_credit_bill[0]->invnum;
308 %  } elsif (    scalar(@cust_credit_bill)   == 0
309 %            && scalar(@cust_credit_refund) == 1
310 %            && $cust_credit->credited == 0      ) {
311 %    #applied to one refund
312 %    $desc = ' refunded on '.  time2str("%D", $cust_credit_refund[0]->_date);
313 %  } else {
314 %    #complicated
315 %    $desc = '<BR>';
316 %    foreach my $app ( sort { $a->_date <=> $b->_date }
317 %                           ( @cust_credit_bill, @cust_credit_refund ) ) {
318 %      if ( $app->isa('FS::cust_credit_bill') ) {
319 %        $desc .= '&nbsp;&nbsp;'.
320 %                 '$'. $app->amount.
321 %                 ' applied to Invoice #'. $app->invnum.
322 %                 '<BR>';
323 %                 #' on '. time2str("%D", $app->_date).
324 %      } elsif ( $app->isa('FS::cust_credit_refund') ) {
325 %        $desc .= '&nbsp;&nbsp;'.
326 %                 '$'. $app->amount.
327 %                 ' refunded on '. time2str("%D", $app->_date).
328 %                 '<BR>';
329 %      } else {
330 %        die "$app is not a FS::cust_credit_bill or a FS::cust_credit_refund";
331 %      }
332 %    }
333 %    if ( $cust_credit->credited > 0 ) {
334 %      $desc .= '&nbsp;&nbsp;<B><FONT COLOR="#FF0000">$'.
335 %               $cust_credit->credited. ' unapplied</FONT></B>'.
336 %               qq! (<A HREF="javascript:void(0);" onClick="overlib( OLiframeContent('${p}edit/cust_credit_bill.cgi?!.
337 %               $cust_credit->crednum.
338 %               qq!', 392, 336, 'cust_credit_bill_popup' ), CAPTION, 'Apply credit', STICKY, AUTOSTATUSCAP, MIDX, 0, MIDY, 0, DRAGGABLE, CLOSECLICK ); return false;">apply</A>)!.
339 %               '<BR>';
340 %    }
341 %  }
342 %#
343 %  my $delete = '';
344 %  if ( $cust_credit->closed !~ /^Y/i
345 %
346 %       #s'pose deleting a credit isn't bad like deleting a payment
347 %       # and this needs to be generally available until we have credit voiding..
348 %       #&& $conf->exists('deletecredits')
349 %
350 %       && $curuser->access_right('Delete credit')
351 %     )
352 %  {
353 %    $delete = qq! (<A HREF="javascript:areyousure('!.
354 %              qq!${p}misc/delete-cust_credit.cgi?!. $cust_credit->crednum.
355 %              qq!', 'Are you sure you want to delete this credit?')">!.
356 %              qq!delete</A>)!;
357 %  }
358 %  
359 %  my $unapply = '';
360 %  if (    $cust_credit->closed !~ /^Y/i
361 %       && scalar(@cust_credit_bill)
362 %       && $curuser->access_right('Unapply credit')
363 %     )
364 %  {
365 %    $unapply = qq! (<A HREF="javascript:areyousure('!.
366 %               qq!${p}misc/unapply-cust_credit.cgi?!. $cust_credit->crednum.
367 %               qq!', 'Are you sure you want to unapply this credit?')">!.
368 %               qq!unapply</A>)!;
369 %  }
370 %  
371 %  push @history, {
372 %    'date'   => $cust_credit->_date,
373 %    'desc'   => $pre. "Credit$post by ". $cust_credit->otaker.
374 %                ( $cust_credit->reason
375 %                   ? ' ('. $cust_credit->reason. ')'
376 %                   : ''
377 %               ).
378 %                "$desc$apply$delete$unapply",
379 %    'credit' => $cust_credit->amount,
380 %  };
381 %
382 %}
383 %
384 %#refunds
385 %foreach my $cust_refund ($cust_main->cust_refund) {
386 %
387 %  my $payby = $cust_refund->payby;
388 %  my $payinfo = $payby eq 'CARD'
389 %                  ? $cust_refund->paymask
390 %                  : $cust_refund->payinfo;
391 %
392 %  $payby =~ s/^BILL$/Check #/ if $payinfo;
393 %  $payby =~ s/^CHEK$/Electronic check /;
394 %  $payby =~ s/^(CARD|COMP)$/$1 /;
395 %
396 %  my $delete = '';
397 %  if ( $cust_refund->closed !~ /^Y/i
398 %       && $conf->exists('deleterefunds')
399 %       && $curuser->access_right('Delete refund')
400 %     )
401 %  {
402 %    $delete = qq! (<A HREF="javascript:areyousure('!.
403 %              qq!${p}misc/delete-cust_refund.cgi?!. $cust_refund->refundnum.
404 %              qq!', 'Are you sure you want to delete this refund?')"!.
405 %              qq! TITLE="Delete this refund from the database completely - not recommended"!.
406 %              qq!>delete</A>)!;
407 %  }
408 %
409 %  push @history, {
410 %    'date'   => $cust_refund->_date,
411 %    'desc'   => "Refund ($payby$payinfo) by ". $cust_refund->otaker. "<BR>".
412 %                $delete,
413 %    'refund' => $cust_refund->refund,
414 %  };
415 %
416 %}
417 %
418 %
419
420
421 <% include("/elements/table-grid.html") %>
422 % my $bgcolor1 = '#eeeeee';
423 %   my $bgcolor2 = '#ffffff';
424 %   my $bgcolor = '';
425 %
426
427
428 <TR>
429   <TH CLASS="grid" BGCOLOR="#cccccc">Date</TH>
430   <TH CLASS="grid" BGCOLOR="#cccccc">Description</TH>
431   <TH CLASS="grid" BGCOLOR="#cccccc"><FONT SIZE=-1>Charge</FONT></TH>
432   <TH CLASS="grid" BGCOLOR="#cccccc"><FONT SIZE=-1>Payment</FONT></TH>
433   <TH CLASS="grid" BGCOLOR="#cccccc"><FONT SIZE=-1>In-house<BR>Credit</FONT></TH>
434   <TH CLASS="grid" BGCOLOR="#cccccc"><FONT SIZE=-1>Refund</FONT></TH>
435   <TH CLASS="grid" BGCOLOR="#cccccc"><FONT SIZE=-1>Balance</FONT></TH>
436 </TR>
437 %
438 %#display payment history
439 %
440 %sub balance_forward_row {
441 %  my( $b, $date ) = @_;
442 %  my $conf = new FS::Conf;
443 %  my $money_char = $conf->config('money_char') || '$';
444 %  ( my $balance_forward = $money_char. $b ) =~ s/^\$\-/-&nbsp;\$/;
445
446    <TR ID="balance_forward_row">
447      <TD CLASS="grid" BGCOLOR="#dddddd">
448        <% time2str("%D",$date) %>
449      </TD>
450
451      <TD CLASS="grid" BGCOLOR="#dddddd">
452        <I>Starting balance on <% time2str("%D",$date) %></I>
453        (<A HREF="javascript:void(0);" onClick="show_history();">show prior history</A>)
454      </TD>
455
456      <TD CLASS="grid" BGCOLOR="#dddddd"></TD>
457      <TD CLASS="grid" BGCOLOR="#dddddd"></TD>
458      <TD CLASS="grid" BGCOLOR="#dddddd"></TD>
459      <TD CLASS="grid" BGCOLOR="#dddddd"></TD>
460      <TD CLASS="grid" BGCOLOR="#dddddd"><I><% $balance_forward %></I></TD>
461
462    </TR>
463 %}
464 %
465 %my $balance = 0;
466 %my %target = ();
467 %my $money_char = $conf->config('money_char') || '$';
468 %
469 %my $years =  $conf->config('payment_history-years') || 2;
470 %my $older_than = time - $years * 31556736; #60*60*24*365.24
471 %my $hidden = 0;
472 %my $seen = 0;
473 %my $old_history = 0;
474 %my $lastdate = 0;
475 %
476 %foreach my $item ( sort { $a->{'date'} <=> $b->{'date'} } @history ) {
477 %
478 %  $lastdate = $item->{'date'};
479 %
480 %  my $display;
481 %  if ( $item->{'date'} < $older_than ) {
482 %    $display = ' STYLE="display:none" ';
483 %    $hidden = 1;
484 %  } else {
485 %
486 %    $display = '';
487 %
488 %    if ( $hidden && ! $seen++ ) {
489 %      balance_forward_row($balance, $item->{'date'});
490 %    }
491 %
492 %  }
493 %
494 %  if ( $bgcolor eq $bgcolor1 ) {
495 %    $bgcolor = $bgcolor2;
496 %  } else {
497 %    $bgcolor = $bgcolor1;
498 %  }
499 %
500 %  my $charge  = exists($item->{'charge'})
501 %                  ? sprintf("$money_char\%.2f", $item->{'charge'})
502 %                  : '';
503 %
504 %  my $payment = exists($item->{'payment'})
505 %                  ? sprintf("-&nbsp;$money_char\%.2f", $item->{'payment'})
506 %                  : '';
507 %
508 %  $payment ||= sprintf( "<DEL>-&nbsp;$money_char\%.2f</DEL>",
509 %                        $item->{'void_payment'}
510 %                      )
511 %    if exists($item->{'void_payment'});
512 %
513 %  my $credit  = exists($item->{'credit'})
514 %                  ? sprintf("-&nbsp;$money_char\%.2f", $item->{'credit'})
515 %                  : '';
516 %
517 %  my $refund  = exists($item->{'refund'})
518 %                  ? sprintf("$money_char\%.2f", $item->{'refund'})
519 %                  : '';
520 %
521 %  my $target = exists($item->{'target'}) ? $item->{'target'} : '';
522 %
523 %  $balance += $item->{'charge'}  if exists $item->{'charge'};
524 %  $balance -= $item->{'payment'} if exists $item->{'payment'};
525 %  $balance -= $item->{'credit'}  if exists $item->{'credit'};
526 %  $balance += $item->{'refund'}  if exists $item->{'refund'};
527 %  $balance = sprintf("%.2f", $balance);
528 %  $balance =~ s/^\-0\.00$/0.00/; #yay ieee fp
529 %  ( my $showbalance = $money_char. $balance ) =~ s/^\$\-/-&nbsp;\$/;
530 %
531 %
532
533
534   <TR <% $display ? $display.' ID="old_history'.$old_history++.'"'  : ''%>>
535     <TD CLASS="grid" BGCOLOR="<% $bgcolor %>">
536 % unless ( !$target || $target{$target}++ ) { 
537
538         <A NAME="<% $target %>">
539 % } 
540
541       <% time2str("%D",$item->{'date'}) %>
542 % if ( $target && $target{$target} == 1 ) { 
543
544         </A>
545 % } 
546
547       </FONT>
548     </TD>
549     <TD CLASS="grid" BGCOLOR="<% $bgcolor %>">
550       <% $item->{'desc'} %>
551     </TD>
552     <TD ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
553       <% $charge  %>
554     </TD>
555     <TD ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
556       <% $payment %>
557     </TD>
558     <TD ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
559       <% $credit  %>
560     </TD>
561     <TD ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
562       <% $refund  %>
563     </TD>
564     <TD ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
565       <% $showbalance %>
566     </TD>
567   </TR>
568 % } 
569
570 %if ( scalar(@history) && $hidden && ! $seen++ ) {
571 %  balance_forward_row($balance, $lastdate);
572 %}
573
574 </TABLE>
575
576 <SCRIPT TYPE="text/javascript">
577
578 function show_history () {
579   //alert('showing history!');
580
581   var balance_forward_row = document.getElementById('balance_forward_row');
582
583   balance_forward_row.style.display = 'none';
584   for ( var i = 0; i < <% $old_history %>; i++ ) {
585     var oldRow = document.getElementById('old_history'+i);
586     oldRow.style.display = '';
587   }
588
589 }
590
591 </SCRIPT>
592
593 <%init>
594
595 my( $cust_main ) = @_;
596 my $custnum = $cust_main->custnum;
597
598 my $conf = new FS::Conf;
599
600 my $curuser = $FS::CurrentUser::CurrentUser;
601
602 my @payby = grep /\w/, $conf->config('payby');
603 #@payby = (qw( CARD DCRD CHEK DCHK LECB BILL CASH WEST COMP ))
604 @payby = (qw( CARD DCRD CHEK DCHK LECB BILL CASH COMP ))
605   unless @payby;
606 my %payby = map { $_=>1 } @payby;
607
608 my %status = (
609   'Queued'     => 'O', #Open
610   'In-transit' => 'I',
611   'Complete'   => 'R', #Resolved
612   'All'        => '',
613 );
614
615 </%init>