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