basic customer view tabs, RT#5586
[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'       => 392,
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 % if ( $curuser->access_right('View customer tax exemptions') ) { 
131   <A HREF="<% $p %>search/cust_tax_exempt_pkg.cgi?custnum=<% $custnum %>">View tax exemptions</A>
132   <BR>
133 % } 
134
135 %# batched payment links
136
137 % if ( ( $conf->exists('batch-enable') || $conf->config('batch-enable_payby') )
138 %      && $curuser->access_right('View customer batched payments')
139 %    )
140 % { 
141     View batched payments:
142 %   foreach my $status (qw( Queued In-transit Complete All )) {
143       <A HREF="<% $p %>search/cust_pay_batch.cgi?status=<% $status{$status} %>;custnum=<% $custnum %>"><% $status %></A> 
144       <% $status ne 'All' ? '|' : '' %>
145 %   }
146     <BR>
147 % } 
148
149 %# pending payment links
150
151 % if ( $curuser->access_right('View customer pending payments')
152 %      && scalar($cust_main->cust_pay_pending)
153 %    )
154 % {
155     <A HREF="<% $p %>search/cust_pay_pending.html?magic=_date;statusNOT=done;custnum=<% $custnum %>">View pending payments</A><BR>
156 % }
157
158 %# and now the table
159
160 <% include("/elements/table-grid.html") %>
161 % my $bgcolor1 = '#eeeeee';
162 %   my $bgcolor2 = '#ffffff';
163 %   my $bgcolor = '';
164
165 <TR>
166   <TH CLASS="grid" BGCOLOR="#cccccc">Date</TH>
167   <TH CLASS="grid" BGCOLOR="#cccccc">Description</TH>
168   <TH CLASS="grid" BGCOLOR="#cccccc"><FONT SIZE=-1>Invoice</FONT></TH>
169   <TH CLASS="grid" BGCOLOR="#cccccc"><FONT SIZE=-1>Payment</FONT></TH>
170   <TH CLASS="grid" BGCOLOR="#cccccc"><FONT SIZE=-1>In-house<BR>Credit</FONT></TH>
171   <TH CLASS="grid" BGCOLOR="#cccccc"><FONT SIZE=-1>Refund</FONT></TH>
172   <TH CLASS="grid" BGCOLOR="#cccccc"><FONT SIZE=-1>Balance</FONT></TH>
173 </TR>
174
175 %#display payment history
176
177 %my $money_char = $conf->config('money_char') || '$';
178 %
179 %sub balance_forward_row {
180 %  my( $b, $date, $money_char ) = @_;
181 %  ( my $balance_forward = $money_char. $b ) =~ s/^\$\-/-&nbsp;\$/;
182
183    <TR ID="balance_forward_row">
184      <TD CLASS="grid" BGCOLOR="#dddddd">
185        <% time2str("%D",$date) %>
186      </TD>
187
188      <TD CLASS="grid" BGCOLOR="#dddddd">
189        <I>Starting balance on <% time2str("%D",$date) %></I>
190        (<A HREF="javascript:void(0);" onClick="show_history();">show prior history</A>)
191      </TD>
192
193      <TD CLASS="grid" BGCOLOR="#dddddd"></TD>
194      <TD CLASS="grid" BGCOLOR="#dddddd"></TD>
195      <TD CLASS="grid" BGCOLOR="#dddddd"></TD>
196      <TD CLASS="grid" BGCOLOR="#dddddd"></TD>
197      <TD CLASS="grid" BGCOLOR="#dddddd" ALIGN="right"><I><% $balance_forward %></I></TD>
198
199    </TR>
200 %}
201 %
202 %my $balance = 0;
203 %my %target = ();
204 %
205 %my $years =  $conf->config('payment_history-years') || 2;
206 %my $older_than = time - $years * 31556736; #60*60*24*365.24
207 %my $hidden = 0;
208 %my $seen = 0;
209 %my $old_history = 0;
210 %my $lastdate = 0;
211 %
212 %foreach my $item ( sort { $a->{'date'} <=> $b->{'date'} } @history ) {
213 %
214 %  $lastdate = $item->{'date'};
215 %
216 %  my $display;
217 %  if ( $item->{'date'} < $older_than ) {
218 %    $display = ' STYLE="display:none" ';
219 %    $hidden = 1;
220 %  } else {
221 %
222 %    $display = '';
223 %
224 %    if ( $hidden && ! $seen++ ) {
225 %      balance_forward_row($balance, $item->{'date'}, $money_char);
226 %    }
227 %
228 %  }
229 %
230 %  if ( $bgcolor eq $bgcolor1 ) {
231 %    $bgcolor = $bgcolor2;
232 %  } else {
233 %    $bgcolor = $bgcolor1;
234 %  }
235 %
236 %  my $charge  = exists($item->{'charge'})
237 %                  ? sprintf("$money_char\%.2f", $item->{'charge'})
238 %                  : '';
239 %
240 %  my $payment = exists($item->{'payment'})
241 %                  ? sprintf("-&nbsp;$money_char\%.2f", $item->{'payment'})
242 %                  : '';
243 %
244 %  $payment ||= sprintf( "<DEL>-&nbsp;$money_char\%.2f</DEL>",
245 %                        $item->{'void_payment'}
246 %                      )
247 %    if exists($item->{'void_payment'});
248 %
249 %  my $credit  = exists($item->{'credit'})
250 %                  ? sprintf("-&nbsp;$money_char\%.2f", $item->{'credit'})
251 %                  : '';
252 %
253 %  my $refund  = exists($item->{'refund'})
254 %                  ? sprintf("$money_char\%.2f", $item->{'refund'})
255 %                  : '';
256 %
257 %  my $target = exists($item->{'target'}) ? $item->{'target'} : '';
258 %
259 %  $balance += $item->{'charge'}  if exists $item->{'charge'};
260 %  $balance -= $item->{'payment'} if exists $item->{'payment'};
261 %  $balance -= $item->{'credit'}  if exists $item->{'credit'};
262 %  $balance += $item->{'refund'}  if exists $item->{'refund'};
263 %  $balance = sprintf("%.2f", $balance);
264 %  $balance =~ s/^\-0\.00$/0.00/; #yay ieee fp
265 %  ( my $showbalance = $money_char. $balance ) =~ s/^\$\-/-&nbsp;\$/;
266 %
267 %
268
269
270   <TR <% $display ? $display.' ID="old_history'.$old_history++.'"'  : ''%>>
271     <TD CLASS="grid" BGCOLOR="<% $bgcolor %>">
272 % unless ( !$target || $target{$target}++ ) { 
273
274         <A NAME="<% $target %>">
275 % } 
276
277       <% time2str("%D",$item->{'date'}) %>
278 % if ( $target && $target{$target} == 1 ) { 
279
280         </A>
281 % } 
282
283       </FONT>
284     </TD>
285     <TD CLASS="grid" BGCOLOR="<% $bgcolor %>">
286       <% $item->{'desc'} %>
287     </TD>
288     <TD ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
289       <% $charge  %>
290     </TD>
291     <TD ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
292       <% $payment %>
293     </TD>
294     <TD ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
295       <% $credit  %>
296     </TD>
297     <TD ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
298       <% $refund  %>
299     </TD>
300     <TD ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
301       <% $showbalance %>
302     </TD>
303   </TR>
304 % } 
305
306 %if ( scalar(@history) && $hidden && ! $seen++ ) {
307 %  balance_forward_row($balance, $lastdate, $money_char);
308 %}
309
310 </TABLE>
311
312 <SCRIPT TYPE="text/javascript">
313
314 function show_history () {
315   //alert('showing history!');
316
317   var balance_forward_row = document.getElementById('balance_forward_row');
318
319   balance_forward_row.style.display = 'none';
320   for ( var i = 0; i < <% $old_history %>; i++ ) {
321     var oldRow = document.getElementById('old_history'+i);
322     oldRow.style.display = '';
323   }
324
325 }
326
327 </SCRIPT>
328
329 <%init>
330
331 my( $cust_main ) = @_;
332 my $custnum = $cust_main->custnum;
333
334 my $conf = new FS::Conf;
335
336 my $curuser = $FS::CurrentUser::CurrentUser;
337
338 my @payby = grep /\w/, $conf->config('payby');
339 #@payby = (qw( CARD DCRD CHEK DCHK LECB BILL CASH WEST COMP ))
340 @payby = (qw( CARD DCRD CHEK DCHK LECB BILL CASH COMP ))
341   unless @payby;
342 my %payby = map { $_=>1 } @payby;
343
344 my %status = (
345   'Queued'     => 'O', #Open
346   'In-transit' => 'I',
347   'Complete'   => 'R', #Resolved
348   'All'        => '',
349 );
350
351 #get payment history
352 my @history = ();
353
354 my %opt =
355   ( map { $_ => scalar($conf->config($_)) }
356         qw( card_refund-days )
357   ),
358   ( map { $_ => $conf->exists($_) } 
359         qw( deletepayments deleterefunds )
360   );
361
362 #invoices
363 foreach my $cust_bill ($cust_main->cust_bill) {
364   push @history, {
365     'date'   => $cust_bill->_date,
366     'desc'   => include('payment_history/invoice.html', $cust_bill, %opt ),
367     'charge' => $cust_bill->charged,
368   };
369 }
370
371 #payments (some false laziness w/credits)
372 foreach my $cust_pay ($cust_main->cust_pay) {
373   push @history, {
374     'date'    => $cust_pay->_date,
375     'desc'    => include('payment_history/payment.html', $cust_pay, %opt ),
376     'payment' => $cust_pay->paid,
377     #'target'  => $target, #XXX
378   };
379 }
380
381 #voided payments
382 foreach my $cust_pay_void ($cust_main->cust_pay_void) {
383   push @history, {
384     'date'   => $cust_pay_void->_date,
385     'desc'   => include('payment_history/voided_payment.html', $cust_pay_void),
386     'void_payment' => $cust_pay_void->paid,
387   };
388
389 }
390
391 #credits (some false laziness w/payments)
392 foreach my $cust_credit ($cust_main->cust_credit) {
393   push @history, {
394     'date'   => $cust_credit->_date,
395     'desc'   => include('payment_history/credit.html', $cust_credit),
396     'credit' => $cust_credit->amount,
397   };
398
399 }
400
401 #refunds
402 foreach my $cust_refund ($cust_main->cust_refund) {
403   push @history, {
404     'date'   => $cust_refund->_date,
405     'desc'   => include('payment_history/refund.html', $cust_refund),
406     'refund' => $cust_refund->refund,
407   };
408
409 }
410
411 </%init>