5af34c09c7d836309c7527ede930e5e0d1a62cd5
[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 $view = qq! (<A HREF="javascript:void(0);" onClick="overlib( OLiframeContent('${p}view/cust_pay.html?link=popup;paynum=!.
175 %             $cust_pay->paynum.
176 %             qq!', 540, 336, 'view_cust_pay_popup' ), CAPTION, 'Payment Receipt', STICKY, AUTOSTATUSCAP, MIDX, 0, MIDY, 0, DRAGGABLE, CLOSECLICK ); return false;">view receipt</A>)!;
177 %
178 %  my $refund = '';
179 %  my $refund_days = $conf->config('card_refund-days') || 120;
180 %  if (    $cust_pay->closed !~ /^Y/i
181 %       && $cust_pay->payby =~ /^(CARD|CHEK)$/
182 %       && time-$cust_pay->_date < $refund_days*86400
183 %       && $cust_pay->unrefunded > 0
184 %       && $curuser->access_right('Refund payment')
185 %  ) {
186 %    $refund = qq! (<A HREF="${p}edit/cust_refund.cgi?payby=$1;!.
187 %              qq!paynum=!. $cust_pay->paynum. '"'.
188 %              qq! TITLE="Send a refund for this payment to the payment gateway"!.
189 %              qq!>refund</A>)!;
190 %  }
191 %
192 %  my $void = '';
193 %  if (    $cust_pay->closed !~ /^Y/i
194 %       && (    ( $cust_pay->payby eq 'CARD'
195 %                 && $curuser->access_right('Credit card void')
196 %               )
197 %            || ( $cust_pay->payby eq 'CHEK'
198 %                 && $curuser->access_right('Echeck void')
199 %               )
200 %            || ( $cust_pay->payby !~ /^(CARD|CHEK)$/
201 %                 && $curuser->access_right('Regular void')
202 %               )
203 %          )
204 %     )
205 %  {
206 %    $void = qq! (<A HREF="javascript:areyousure('!.
207 %            qq!${p}misc/void-cust_pay.cgi?!. $cust_pay->paynum.
208 %            qq!', 'Are you sure you want to void this payment?')"!.
209 %            qq! TITLE="Void this payment from the database!.
210 %              ( $cust_pay->payby =~ /^(CARD|CHEK)$/
211 %                ? ' (do not send anything to the payment gateway)'
212 %                : '' 
213 %              ). '"'.
214 %            qq!>void</A>)!;
215 %  }
216 %
217 %  my $delete = '';
218 %  if ( $cust_pay->closed !~ /^Y/i
219 %       && $conf->exists('deletepayments')
220 %       && $curuser->access_right('Delete payment')
221 %     )
222 %  {
223 %    $delete = qq! (<A HREF="javascript:areyousure('!.
224 %              qq!${p}misc/delete-cust_pay.cgi?!. $cust_pay->paynum.
225 %              qq!', 'Are you sure you want to delete this payment?')"!.
226 %              qq! TITLE="Delete this payment from the database completely - not recommended"!.
227 %              qq!>delete</A>)!;
228 %  }
229 %
230 %  my $unapply = '';
231 %  if (    $cust_pay->closed !~ /^Y/i
232 %       && scalar(@cust_bill_pay)           
233 %       && $curuser->access_right('Unapply payment')
234 %     )
235 %  {
236 %    $unapply = qq! (<A HREF="javascript:areyousure('!.
237 %               qq!${p}misc/unapply-cust_pay.cgi?!. $cust_pay->paynum.
238 %               qq!', 'Are you sure you want to unapply this payment?')"!.
239 %               qq! TITLE="Keep this payment, but dissociate it from the invoices it is currently applied against"!.
240 %               qq!>unapply</A>)!;
241 %  }
242 %
243 %  push @history, {
244 %    'date'    => $cust_pay->_date,
245 %    'desc'    => $pre. "Payment$post$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 %    $apply = qq! (<A HREF="javascript:void(0);" onClick="overlib( OLiframeContent('${p}edit/cust_credit_bill.cgi?!.
305 %             $cust_credit->crednum.
306 %             qq!', 392, 336, 'cust_credit_bill_popup' ), CAPTION, 'Apply credit', STICKY, AUTOSTATUSCAP, MIDX, 0, MIDY, 0, DRAGGABLE, CLOSECLICK ); return false;">apply</A>)!;
307 %  } elsif (    scalar(@cust_credit_bill)   == 1
308 %            && scalar(@cust_credit_refund) == 0
309 %            && $cust_credit->credited == 0      ) {
310 %    #applied to one invoice, the usual situation
311 %    $desc = ' applied to Invoice #'. $cust_credit_bill[0]->invnum;
312 %  } elsif (    scalar(@cust_credit_bill)   == 0
313 %            && scalar(@cust_credit_refund) == 1
314 %            && $cust_credit->credited == 0      ) {
315 %    #applied to one refund
316 %    $desc = ' refunded on '.  time2str("%D", $cust_credit_refund[0]->_date);
317 %  } else {
318 %    #complicated
319 %    $desc = '<BR>';
320 %    foreach my $app ( sort { $a->_date <=> $b->_date }
321 %                           ( @cust_credit_bill, @cust_credit_refund ) ) {
322 %      if ( $app->isa('FS::cust_credit_bill') ) {
323 %        $desc .= '&nbsp;&nbsp;'.
324 %                 '$'. $app->amount.
325 %                 ' applied to Invoice #'. $app->invnum.
326 %                 '<BR>';
327 %                 #' on '. time2str("%D", $app->_date).
328 %      } elsif ( $app->isa('FS::cust_credit_refund') ) {
329 %        $desc .= '&nbsp;&nbsp;'.
330 %                 '$'. $app->amount.
331 %                 ' refunded on '. time2str("%D", $app->_date).
332 %                 '<BR>';
333 %      } else {
334 %        die "$app is not a FS::cust_credit_bill or a FS::cust_credit_refund";
335 %      }
336 %    }
337 %    if ( $cust_credit->credited > 0 ) {
338 %      $desc .= '&nbsp;&nbsp;<B><FONT COLOR="#FF0000">$'.
339 %               $cust_credit->credited. ' unapplied</FONT></B>'.
340 %               qq! (<A HREF="javascript:void(0);" onClick="overlib( OLiframeContent('${p}edit/cust_credit_bill.cgi?!.
341 %               $cust_credit->crednum.
342 %               qq!', 392, 336, 'cust_credit_bill_popup' ), CAPTION, 'Apply credit', STICKY, AUTOSTATUSCAP, MIDX, 0, MIDY, 0, DRAGGABLE, CLOSECLICK ); return false;">apply</A>)!.
343 %               '<BR>';
344 %    }
345 %  }
346 %#
347 %  my $delete = '';
348 %  if ( $cust_credit->closed !~ /^Y/i
349 %
350 %       #s'pose deleting a credit isn't bad like deleting a payment
351 %       # and this needs to be generally available until we have credit voiding..
352 %       #&& $conf->exists('deletecredits')
353 %
354 %       && $curuser->access_right('Delete credit')
355 %     )
356 %  {
357 %    $delete = qq! (<A HREF="javascript:areyousure('!.
358 %              qq!${p}misc/delete-cust_credit.cgi?!. $cust_credit->crednum.
359 %              qq!', 'Are you sure you want to delete this credit?')">!.
360 %              qq!delete</A>)!;
361 %  }
362 %  
363 %  my $unapply = '';
364 %  if (    $cust_credit->closed !~ /^Y/i
365 %       && scalar(@cust_credit_bill)
366 %       && $curuser->access_right('Unapply credit')
367 %     )
368 %  {
369 %    $unapply = qq! (<A HREF="javascript:areyousure('!.
370 %               qq!${p}misc/unapply-cust_credit.cgi?!. $cust_credit->crednum.
371 %               qq!', 'Are you sure you want to unapply this credit?')">!.
372 %               qq!unapply</A>)!;
373 %  }
374 %  
375 %  push @history, {
376 %    'date'   => $cust_credit->_date,
377 %    'desc'   => $pre. "Credit$post by ". $cust_credit->otaker.
378 %                ( $cust_credit->reason
379 %                   ? ' ('. $cust_credit->reason. ')'
380 %                   : ''
381 %               ).
382 %                "$desc$apply$delete$unapply",
383 %    'credit' => $cust_credit->amount,
384 %  };
385 %
386 %}
387 %
388 %#refunds
389 %foreach my $cust_refund ($cust_main->cust_refund) {
390 %
391 %  my $payby = $cust_refund->payby;
392 %  my $payinfo = $payby eq 'CARD'
393 %                  ? $cust_refund->paymask
394 %                  : $cust_refund->payinfo;
395 %
396 %  $payby =~ s/^BILL$/Check #/ if $payinfo;
397 %  $payby =~ s/^CHEK$/Electronic check /;
398 %  $payby =~ s/^(CARD|COMP)$/$1 /;
399 %
400 %  my $delete = '';
401 %  if ( $cust_refund->closed !~ /^Y/i
402 %       && $conf->exists('deleterefunds')
403 %       && $curuser->access_right('Delete refund')
404 %     )
405 %  {
406 %    $delete = qq! (<A HREF="javascript:areyousure('!.
407 %              qq!${p}misc/delete-cust_refund.cgi?!. $cust_refund->refundnum.
408 %              qq!', 'Are you sure you want to delete this refund?')"!.
409 %              qq! TITLE="Delete this refund from the database completely - not recommended"!.
410 %              qq!>delete</A>)!;
411 %  }
412 %
413 %  push @history, {
414 %    'date'   => $cust_refund->_date,
415 %    'desc'   => "Refund ($payby$payinfo) by ". $cust_refund->otaker. "<BR>".
416 %                $delete,
417 %    'refund' => $cust_refund->refund,
418 %  };
419 %
420 %}
421 %
422 %
423
424
425 <% include("/elements/table-grid.html") %>
426 % my $bgcolor1 = '#eeeeee';
427 %   my $bgcolor2 = '#ffffff';
428 %   my $bgcolor = '';
429 %
430
431
432 <TR>
433   <TH CLASS="grid" BGCOLOR="#cccccc">Date</TH>
434   <TH CLASS="grid" BGCOLOR="#cccccc">Description</TH>
435   <TH CLASS="grid" BGCOLOR="#cccccc"><FONT SIZE=-1>Charge</FONT></TH>
436   <TH CLASS="grid" BGCOLOR="#cccccc"><FONT SIZE=-1>Payment</FONT></TH>
437   <TH CLASS="grid" BGCOLOR="#cccccc"><FONT SIZE=-1>In-house<BR>Credit</FONT></TH>
438   <TH CLASS="grid" BGCOLOR="#cccccc"><FONT SIZE=-1>Refund</FONT></TH>
439   <TH CLASS="grid" BGCOLOR="#cccccc"><FONT SIZE=-1>Balance</FONT></TH>
440 </TR>
441 %
442 %#display payment history
443 %
444 %sub balance_forward_row {
445 %  my( $b, $date ) = @_;
446 %  my $conf = new FS::Conf;
447 %  my $money_char = $conf->config('money_char') || '$';
448 %  ( my $balance_forward = $money_char. $b ) =~ s/^\$\-/-&nbsp;\$/;
449
450    <TR ID="balance_forward_row">
451      <TD CLASS="grid" BGCOLOR="#dddddd">
452        <% time2str("%D",$date) %>
453      </TD>
454
455      <TD CLASS="grid" BGCOLOR="#dddddd">
456        <I>Starting balance on <% time2str("%D",$date) %></I>
457        (<A HREF="javascript:void(0);" onClick="show_history();">show prior history</A>)
458      </TD>
459
460      <TD CLASS="grid" BGCOLOR="#dddddd"></TD>
461      <TD CLASS="grid" BGCOLOR="#dddddd"></TD>
462      <TD CLASS="grid" BGCOLOR="#dddddd"></TD>
463      <TD CLASS="grid" BGCOLOR="#dddddd"></TD>
464      <TD CLASS="grid" BGCOLOR="#dddddd"><I><% $balance_forward %></I></TD>
465
466    </TR>
467 %}
468 %
469 %my $balance = 0;
470 %my %target = ();
471 %my $money_char = $conf->config('money_char') || '$';
472 %
473 %my $years =  $conf->config('payment_history-years') || 2;
474 %my $older_than = time - $years * 31556736; #60*60*24*365.24
475 %my $hidden = 0;
476 %my $seen = 0;
477 %my $old_history = 0;
478 %my $lastdate = 0;
479 %
480 %foreach my $item ( sort { $a->{'date'} <=> $b->{'date'} } @history ) {
481 %
482 %  $lastdate = $item->{'date'};
483 %
484 %  my $display;
485 %  if ( $item->{'date'} < $older_than ) {
486 %    $display = ' STYLE="display:none" ';
487 %    $hidden = 1;
488 %  } else {
489 %
490 %    $display = '';
491 %
492 %    if ( $hidden && ! $seen++ ) {
493 %      balance_forward_row($balance, $item->{'date'});
494 %    }
495 %
496 %  }
497 %
498 %  if ( $bgcolor eq $bgcolor1 ) {
499 %    $bgcolor = $bgcolor2;
500 %  } else {
501 %    $bgcolor = $bgcolor1;
502 %  }
503 %
504 %  my $charge  = exists($item->{'charge'})
505 %                  ? sprintf("$money_char\%.2f", $item->{'charge'})
506 %                  : '';
507 %
508 %  my $payment = exists($item->{'payment'})
509 %                  ? sprintf("-&nbsp;$money_char\%.2f", $item->{'payment'})
510 %                  : '';
511 %
512 %  $payment ||= sprintf( "<DEL>-&nbsp;$money_char\%.2f</DEL>",
513 %                        $item->{'void_payment'}
514 %                      )
515 %    if exists($item->{'void_payment'});
516 %
517 %  my $credit  = exists($item->{'credit'})
518 %                  ? sprintf("-&nbsp;$money_char\%.2f", $item->{'credit'})
519 %                  : '';
520 %
521 %  my $refund  = exists($item->{'refund'})
522 %                  ? sprintf("$money_char\%.2f", $item->{'refund'})
523 %                  : '';
524 %
525 %  my $target = exists($item->{'target'}) ? $item->{'target'} : '';
526 %
527 %  $balance += $item->{'charge'}  if exists $item->{'charge'};
528 %  $balance -= $item->{'payment'} if exists $item->{'payment'};
529 %  $balance -= $item->{'credit'}  if exists $item->{'credit'};
530 %  $balance += $item->{'refund'}  if exists $item->{'refund'};
531 %  $balance = sprintf("%.2f", $balance);
532 %  $balance =~ s/^\-0\.00$/0.00/; #yay ieee fp
533 %  ( my $showbalance = $money_char. $balance ) =~ s/^\$\-/-&nbsp;\$/;
534 %
535 %
536
537
538   <TR <% $display ? $display.' ID="old_history'.$old_history++.'"'  : ''%>>
539     <TD CLASS="grid" BGCOLOR="<% $bgcolor %>">
540 % unless ( !$target || $target{$target}++ ) { 
541
542         <A NAME="<% $target %>">
543 % } 
544
545       <% time2str("%D",$item->{'date'}) %>
546 % if ( $target && $target{$target} == 1 ) { 
547
548         </A>
549 % } 
550
551       </FONT>
552     </TD>
553     <TD CLASS="grid" BGCOLOR="<% $bgcolor %>">
554       <% $item->{'desc'} %>
555     </TD>
556     <TD ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
557       <% $charge  %>
558     </TD>
559     <TD ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
560       <% $payment %>
561     </TD>
562     <TD ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
563       <% $credit  %>
564     </TD>
565     <TD ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
566       <% $refund  %>
567     </TD>
568     <TD ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
569       <% $showbalance %>
570     </TD>
571   </TR>
572 % } 
573
574 %if ( scalar(@history) && $hidden && ! $seen++ ) {
575 %  balance_forward_row($balance, $lastdate);
576 %}
577
578 </TABLE>
579
580 <SCRIPT TYPE="text/javascript">
581
582 function show_history () {
583   //alert('showing history!');
584
585   var balance_forward_row = document.getElementById('balance_forward_row');
586
587   balance_forward_row.style.display = 'none';
588   for ( var i = 0; i < <% $old_history %>; i++ ) {
589     var oldRow = document.getElementById('old_history'+i);
590     oldRow.style.display = '';
591   }
592
593 }
594
595 </SCRIPT>
596
597 <%init>
598
599 my( $cust_main ) = @_;
600 my $custnum = $cust_main->custnum;
601
602 my $conf = new FS::Conf;
603
604 my $curuser = $FS::CurrentUser::CurrentUser;
605
606 my @payby = grep /\w/, $conf->config('payby');
607 #@payby = (qw( CARD DCRD CHEK DCHK LECB BILL CASH WEST COMP ))
608 @payby = (qw( CARD DCRD CHEK DCHK LECB BILL CASH COMP ))
609   unless @payby;
610 my %payby = map { $_=>1 } @payby;
611
612 my %status = (
613   'Queued'     => 'O', #Open
614   'In-transit' => 'I',
615   'Complete'   => 'R', #Resolved
616   'All'        => '',
617 );
618
619 </%init>