56a2c4ddef1108b70e40ff61013cf8c99c885431
[freeside.git] / httemplate / view / cust_main / payment_history.html
1 %# payment links
2
3 % my $s = 0;
4 % if ( $payby{'BILL'} && $curuser->access_right('Post payment') ) { 
5   <% $s++ ? ' | ' : '' %>
6   <% include('/elements/popup_link-cust_main.html',
7                'label'       => 'Enter check payment',
8                'action'      => "${p}edit/cust_pay.cgi?popup=1;payby=BILL",
9                'cust_main'   => $cust_main,
10                'actionlabel' => 'Enter check payment',
11                'width'       => 392,
12                #default# 'height' => 336,
13             )
14   %>
15 % } 
16
17 % if ( $payby{'CASH'} && $curuser->access_right('Post payment') ) { 
18   <% $s++ ? ' | ' : '' %>
19   <% include('/elements/popup_link-cust_main.html',
20                'label'       => 'Enter cash payment',
21                'action'      => "${p}edit/cust_pay.cgi?popup=1;payby=CASH",
22                'cust_main'   => $cust_main,
23                'actionlabel' => 'Enter cash payment',
24                'width'       => 392,
25                #default# 'height' => 336,
26             )
27   %>
28 % } 
29
30 % if ( $payby{'WEST'} && $curuser->access_right('Post payment') ) { 
31   <% $s++ ? ' | ' : '' %>
32   <A HREF="<% $p %>edit/cust_pay.cgi?payby=WEST;custnum=<% $custnum %>">Enter Western Union payment</A>
33 % } 
34
35 % if ( ( $payby{'CARD'} || $payby{'DCRD'} )
36 %        && $curuser->access_right('Process payment')
37 %        && ! $cust_main->is_encrypted($cust_main->payinfo)
38 %      ) {
39   <% $s++ ? ' | ' : '' %>
40   <A HREF="<% $p %>misc/payment.cgi?payby=CARD;custnum=<% $custnum %>">Process credit card payment</A>
41 % } 
42
43 % if ( ( $payby{'CHEK'} || $payby{'DCHK'} )
44 %        && $curuser->access_right('Process payment')
45 %        && ! $cust_main->is_encrypted($cust_main->payinfo)
46 %      ) {
47   <% $s++ ? ' | ' : '' %>
48   <A HREF="<% $p %>misc/payment.cgi?payby=CHEK;custnum=<% $custnum %>">Process electronic check (ACH) payment</A>
49 % } 
50
51 % if ( $payby{'MCRD'} && $curuser->access_right('Post payment') ) { 
52   <% $s++ ? ' | ' : '' %>
53   <A HREF="<% $p %>edit/cust_pay.cgi?payby=MCRD;custnum=<% $custnum %>">Post manual (offline/POS) credit card payment</A>
54 % } 
55
56 <BR>
57
58 %# credit link
59
60 % if ( $curuser->access_right('Post credit') ) { 
61   <% include('/elements/popup_link-cust_main.html',
62                'label'       => 'Enter credit',
63                'action'      => "${p}edit/cust_credit.cgi",
64                'cust_main'   => $cust_main,
65                'actionlabel' => 'Enter credit',
66                'width'       => 616, #make room for reasons #540 default
67                #default# 'height' => 336,
68             )
69   %>
70   <BR>
71 % } 
72
73 %# refund links
74
75 % $s = 0;
76 % if ( $payby{'BILL'} && $curuser->access_right('Post refund') ) { 
77   <% $s++ ? ' | ' : '' %>
78   <% include('/elements/popup_link-cust_main.html',
79                'label'       => 'Enter check refund',
80                'action'      => "${p}edit/cust_refund.cgi?popup=1;payby=BILL",
81                'cust_main'   => $cust_main,
82                'actionlabel' => 'Enter check refund',
83                'width'       => 392,
84                #default# 'height' => 336,
85             )
86   %>
87 % } 
88
89 % if ( $payby{'CASH'} && $curuser->access_right('Post refund') ) { 
90   <% $s++ ? ' | ' : '' %>
91   <% include('/elements/popup_link-cust_main.html',
92                'label'       => 'Enter cash refund',
93                'action'      => "${p}edit/cust_refund.cgi?popup=1;payby=CASH",
94                'cust_main'   => $cust_main,
95                'actionlabel' => 'Enter cash refund',
96                'width'       => 392,
97                #default# 'height' => 336,
98             )
99   %>
100 % } 
101
102 %# someday, perhaps.  very few gateways let you do unlinked refunds at all.
103 %# Authorize.net makes you sign a special form
104 %#
105 %#    % if ( ( $payby{'CARD'} || $payby{'DCRD'} )
106 %#    %        && $curuser->access_right('Process refund')
107 %#    %        && ! $cust_main->is_encrypted($cust_main->payinfo)
108 %#    %      ) {
109 %#      <% $s++ ? ' | ' : '' %>
110 %#      <A HREF="<% $p %>misc/refund.cgi?payby=CARD;custnum=<% $custnum %>">Process credit card refund</A>
111 %#    % } 
112 %#    
113 %#    % if ( ( $payby{'CHEK'} || $payby{'DCHK'} )
114 %#    %        && $curuser->access_right('Process refund')
115 %#    %        && ! $cust_main->is_encrypted($cust_main->payinfo)
116 %#    %      ) {
117 %#      <% $s++ ? ' | ' : '' %>
118 %#      <A HREF="<% $p %>misc/refund.cgi?payby=CHEK;custnum=<% $custnum %>">Process electronic check (ACH) refund</A>
119 %#    % } 
120
121 % if ( $payby{'MCRD'} && $curuser->access_right('Post refund') ) { 
122   <% $s++ ? ' | ' : '' %>
123   <A HREF="<% $p %>edit/cust_refund.cgi?payby=MCRD;custnum=<% $custnum %>">Post manual (offline/POS) credit card refund</A>
124 % } 
125
126 <BR>
127
128 %# tax exemption link
129
130 % my $view_exemptions = $curuser->access_right('View customer tax exemptions');
131 % my $add_adjustment = ( $conf->exists('enable_tax_adjustments')
132 %                       && $curuser->access_right('Add customer tax adjustment')
133 %                      );
134 % if ( $view_exemptions || $add_adjustment ) {
135
136 %   if ( $view_exemptions ) {
137       <A HREF="<% $p %>search/cust_tax_exempt_pkg.cgi?custnum=<% $custnum %>">View tax exemptions</A>
138       <% $add_adjustment ? '|' : '' %>
139 %   } 
140
141 %   if ( $add_adjustment ) {
142       <% include('/elements/popup_link.html', {
143            'action' => $p.'edit/cust_tax_adjustment.html?custnum='. $cust_main->custnum,
144            'label'  => 'Add tax adjustment',
145            'actionlabel' => 'Add tax adjustment',
146            #'color'  => '#333399',
147            #'width' => 763,
148            'height' => 200,
149          })
150       %>
151       |
152       <A HREF="<% $p %>search/cust_tax_adjustment.html?custnum=<% $custnum %>">View tax adjustments</A>
153 %   } 
154
155   <BR>
156 % }
157
158 %# batched payment links
159
160 % if ( ( $conf->exists('batch-enable') || $conf->config('batch-enable_payby') )
161 %      && $curuser->access_right('View customer batched payments')
162 %    )
163 % { 
164     View batched payments:
165 %   foreach my $status (qw( Queued In-transit Complete All )) {
166       <A HREF="<% $p %>search/cust_pay_batch.cgi?status=<% $status{$status} %>;custnum=<% $custnum %>"><% $status %></A> 
167       <% $status ne 'All' ? '|' : '' %>
168 %   }
169     <BR>
170 % } 
171
172 %# pending payment links
173
174 % if ( $curuser->access_right('View customer pending payments')
175 %      && scalar($cust_main->cust_pay_pending)
176 %    )
177 % {
178     <A HREF="<% $p %>search/cust_pay_pending.html?magic=_date;statusNOT=done;custnum=<% $custnum %>">View pending payments</A><BR>
179 % }
180
181 %# and now the table
182
183 <% include("/elements/table-grid.html") %>
184 % my $bgcolor1 = '#eeeeee';
185 %   my $bgcolor2 = '#ffffff';
186 %   my $bgcolor = '';
187
188 <TR>
189   <TH CLASS="grid" BGCOLOR="#cccccc">Date</TH>
190   <TH CLASS="grid" BGCOLOR="#cccccc">Description</TH>
191   <TH CLASS="grid" BGCOLOR="#cccccc"><FONT SIZE=-1>Invoice</FONT></TH>
192   <TH CLASS="grid" BGCOLOR="#cccccc"><FONT SIZE=-1>Payment</FONT></TH>
193   <TH CLASS="grid" BGCOLOR="#cccccc"><FONT SIZE=-1>In-house<BR>Credit</FONT></TH>
194   <TH CLASS="grid" BGCOLOR="#cccccc"><FONT SIZE=-1>Refund</FONT></TH>
195   <TH CLASS="grid" BGCOLOR="#cccccc"><FONT SIZE=-1>Balance</FONT></TH>
196 </TR>
197
198 %#display payment history
199
200 %my $money_char = $conf->config('money_char') || '$';
201 %
202 %sub balance_forward_row {
203 %  my( $b, $date, $money_char ) = @_;
204 %  ( my $balance_forward = $money_char. $b ) =~ s/^\$\-/-&nbsp;\$/;
205
206    <TR ID="balance_forward_row">
207      <TD CLASS="grid" BGCOLOR="#dddddd">
208        <% time2str("%D",$date) %>
209      </TD>
210
211      <TD CLASS="grid" BGCOLOR="#dddddd">
212        <I>Starting balance on <% time2str("%D",$date) %></I>
213        (<A HREF="javascript:void(0);" onClick="show_history();">show prior history</A>)
214      </TD>
215
216      <TD CLASS="grid" BGCOLOR="#dddddd"></TD>
217      <TD CLASS="grid" BGCOLOR="#dddddd"></TD>
218      <TD CLASS="grid" BGCOLOR="#dddddd"></TD>
219      <TD CLASS="grid" BGCOLOR="#dddddd"></TD>
220      <TD CLASS="grid" BGCOLOR="#dddddd" ALIGN="right"><I><% $balance_forward %></I></TD>
221
222    </TR>
223 %}
224 %
225 %my $balance = 0;
226 %my %target = ();
227 %
228 %my $years =  $conf->config('payment_history-years') || 2;
229 %my $older_than = time - $years * 31556926; #60*60*24*365.2422
230 %my $hidden = 0;
231 %my $seen = 0;
232 %my $old_history = 0;
233 %my $lastdate = 0;
234 %
235 %foreach my $item ( sort { $a->{'date'} <=> $b->{'date'} } @history ) {
236 %
237 %  $lastdate = $item->{'date'};
238 %
239 %  my $display;
240 %  if ( $item->{'date'} < $older_than ) {
241 %    $display = ' STYLE="display:none" ';
242 %    $hidden = 1;
243 %  } else {
244 %
245 %    $display = '';
246 %
247 %    if ( $hidden && ! $seen++ ) {
248 %      balance_forward_row($balance, $item->{'date'}, $money_char);
249 %    }
250 %
251 %  }
252 %
253 %  if ( $bgcolor eq $bgcolor1 ) {
254 %    $bgcolor = $bgcolor2;
255 %  } else {
256 %    $bgcolor = $bgcolor1;
257 %  }
258 %
259 %  my $charge  = exists($item->{'charge'})
260 %                  ? sprintf("$money_char\%.2f", $item->{'charge'})
261 %                  : '';
262 %
263 %  my $payment = exists($item->{'payment'})
264 %                  ? sprintf("-&nbsp;$money_char\%.2f", $item->{'payment'})
265 %                  : '';
266 %
267 %  $payment ||= sprintf( "<DEL>-&nbsp;$money_char\%.2f</DEL>",
268 %                        $item->{'void_payment'}
269 %                      )
270 %    if exists($item->{'void_payment'});
271 %
272 %  my $credit  = exists($item->{'credit'})
273 %                  ? sprintf("-&nbsp;$money_char\%.2f", $item->{'credit'})
274 %                  : '';
275 %
276 %  my $refund  = exists($item->{'refund'})
277 %                  ? sprintf("$money_char\%.2f", $item->{'refund'})
278 %                  : '';
279 %
280 %  my $target = exists($item->{'target'}) ? $item->{'target'} : '';
281 %
282 %  $balance += $item->{'charge'}  if exists $item->{'charge'};
283 %  $balance -= $item->{'payment'} if exists $item->{'payment'};
284 %  $balance -= $item->{'credit'}  if exists $item->{'credit'};
285 %  $balance += $item->{'refund'}  if exists $item->{'refund'};
286 %  $balance = sprintf("%.2f", $balance);
287 %  $balance =~ s/^\-0\.00$/0.00/; #yay ieee fp
288 %  ( my $showbalance = $money_char. $balance ) =~ s/^\$\-/-&nbsp;\$/;
289 %
290 %
291
292
293   <TR <% $display ? $display.' ID="old_history'.$old_history++.'"'  : ''%>>
294     <TD CLASS="grid" BGCOLOR="<% $bgcolor %>">
295 % unless ( !$target || $target{$target}++ ) { 
296
297         <A NAME="<% $target %>">
298 % } 
299
300       <% time2str("%D",$item->{'date'}) %>
301 % if ( $target && $target{$target} == 1 ) { 
302
303         </A>
304 % } 
305
306       </FONT>
307     </TD>
308     <TD CLASS="grid" BGCOLOR="<% $bgcolor %>">
309       <% $item->{'desc'} %>
310     </TD>
311     <TD ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
312       <% $charge  %>
313     </TD>
314     <TD ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
315       <% $payment %>
316     </TD>
317     <TD ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
318       <% $credit  %>
319     </TD>
320     <TD ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
321       <% $refund  %>
322     </TD>
323     <TD ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
324       <% $showbalance %>
325     </TD>
326   </TR>
327 % } 
328
329 %if ( scalar(@history) && $hidden && ! $seen++ ) {
330 %  balance_forward_row($balance, $lastdate, $money_char);
331 %}
332
333 </TABLE>
334
335 <SCRIPT TYPE="text/javascript">
336
337 function show_history () {
338   //alert('showing history!');
339
340   var balance_forward_row = document.getElementById('balance_forward_row');
341
342   balance_forward_row.style.display = 'none';
343   for ( var i = 0; i < <% $old_history %>; i++ ) {
344     var oldRow = document.getElementById('old_history'+i);
345     oldRow.style.display = '';
346   }
347
348 }
349
350 </SCRIPT>
351
352 <%init>
353
354 my( $cust_main ) = @_;
355 my $custnum = $cust_main->custnum;
356
357 my $conf = new FS::Conf;
358
359 my $curuser = $FS::CurrentUser::CurrentUser;
360
361 my @payby = grep /\w/, $conf->config('payby');
362 #@payby = (qw( CARD DCRD CHEK DCHK LECB BILL CASH WEST COMP ))
363 @payby = (qw( CARD DCRD CHEK DCHK LECB BILL CASH COMP ))
364   unless @payby;
365 my %payby = map { $_=>1 } @payby;
366
367 my %status = (
368   'Queued'     => 'O', #Open
369   'In-transit' => 'I',
370   'Complete'   => 'R', #Resolved
371   'All'        => '',
372 );
373
374 #get payment history
375 my @history = ();
376
377 my %opt = (
378   ( map { $_ => scalar($conf->config($_)) }
379         qw( card_refund-days )
380   ),
381   ( map { $_ => $conf->exists($_) } 
382         qw( deleteinvoices deletepayments deleterefunds pkg-balances )
383   )
384 );
385
386 #invoices
387 foreach my $cust_bill ($cust_main->cust_bill) {
388   push @history, {
389     'date'   => $cust_bill->_date,
390     'desc'   => include('payment_history/invoice.html', $cust_bill, %opt ),
391     'charge' => $cust_bill->charged,
392   };
393 }
394
395 #statements
396 foreach my $cust_statement ($cust_main->cust_statement) {
397   push @history, {
398     'date'   => $cust_statement->_date,
399     'desc'   => include('payment_history/statement.html', $cust_statement, %opt ),
400     #'charge' => $cust_bill->charged,
401   };
402 }
403
404 #payments (some false laziness w/credits)
405 foreach my $cust_pay ($cust_main->cust_pay) {
406   push @history, {
407     'date'    => $cust_pay->_date,
408     'desc'    => include('payment_history/payment.html', $cust_pay, %opt ),
409     'payment' => $cust_pay->paid,
410     #'target'  => $target, #XXX
411   };
412 }
413
414 #voided payments
415 foreach my $cust_pay_void ($cust_main->cust_pay_void) {
416   push @history, {
417     'date'   => $cust_pay_void->_date,
418     'desc'   => include('payment_history/voided_payment.html', $cust_pay_void, %opt ),
419     'void_payment' => $cust_pay_void->paid,
420   };
421
422 }
423
424 #credits (some false laziness w/payments)
425 foreach my $cust_credit ($cust_main->cust_credit) {
426   push @history, {
427     'date'   => $cust_credit->_date,
428     'desc'   => include('payment_history/credit.html', $cust_credit, %opt ),
429     'credit' => $cust_credit->amount,
430   };
431
432 }
433
434 #refunds
435 foreach my $cust_refund ($cust_main->cust_refund) {
436   push @history, {
437     'date'   => $cust_refund->_date,
438     'desc'   => include('payment_history/refund.html', $cust_refund),
439     'refund' => $cust_refund->refund,
440   };
441
442 }
443
444 </%init>