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