refactor payment history slightly, add refund receipts, have "unapplied" refunds...
[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   <% 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) credit card payment</A>
54 % } 
55
56 <BR>
57
58 % if ( $curuser->access_right('Post credit') ) { 
59   <% include('/elements/popup_link-cust_main.html',
60                'label'       => 'Enter credit',
61                'action'      => "${p}edit/cust_credit.cgi",
62                'cust_main'   => $cust_main,
63                'actionlabel' => 'Enter credit',
64                'width'       => 392,
65                #default# 'height' => 336,
66             )
67   %>
68   <BR>
69 % } 
70
71 % if ( $curuser->access_right('View customer tax exemptions') ) { 
72   <A HREF="<% $p %>search/cust_tax_exempt_pkg.cgi?custnum=<% $custnum %>">View tax exemptions</A>
73   <BR>
74 % } 
75
76 % if ( $conf->exists('batch-enable')
77 %      && $curuser->access_right('View customer batched payments')
78 %    ) { 
79   View batched payments:
80 %   foreach my $status (qw( Queued In-transit Complete All )) {
81       <A HREF="<% $p %>search/cust_pay_batch.cgi?status=<% $status{$status} %>;custnum=<% $custnum %>"><% $status %></A> 
82       <% $status ne 'All' ? '|' : '' %>
83 %   }
84   <BR>
85 % } 
86
87
88 <% include("/elements/table-grid.html") %>
89 % my $bgcolor1 = '#eeeeee';
90 %   my $bgcolor2 = '#ffffff';
91 %   my $bgcolor = '';
92 %
93
94
95 <TR>
96   <TH CLASS="grid" BGCOLOR="#cccccc">Date</TH>
97   <TH CLASS="grid" BGCOLOR="#cccccc">Description</TH>
98   <TH CLASS="grid" BGCOLOR="#cccccc"><FONT SIZE=-1>Charge</FONT></TH>
99   <TH CLASS="grid" BGCOLOR="#cccccc"><FONT SIZE=-1>Payment</FONT></TH>
100   <TH CLASS="grid" BGCOLOR="#cccccc"><FONT SIZE=-1>In-house<BR>Credit</FONT></TH>
101   <TH CLASS="grid" BGCOLOR="#cccccc"><FONT SIZE=-1>Refund</FONT></TH>
102   <TH CLASS="grid" BGCOLOR="#cccccc"><FONT SIZE=-1>Balance</FONT></TH>
103 </TR>
104 %
105 %#display payment history
106 %
107 %sub balance_forward_row {
108 %  my( $b, $date ) = @_;
109 %  my $conf = new FS::Conf;
110 %  my $money_char = $conf->config('money_char') || '$';
111 %  ( my $balance_forward = $money_char. $b ) =~ s/^\$\-/-&nbsp;\$/;
112
113    <TR ID="balance_forward_row">
114      <TD CLASS="grid" BGCOLOR="#dddddd">
115        <% time2str("%D",$date) %>
116      </TD>
117
118      <TD CLASS="grid" BGCOLOR="#dddddd">
119        <I>Starting balance on <% time2str("%D",$date) %></I>
120        (<A HREF="javascript:void(0);" onClick="show_history();">show prior history</A>)
121      </TD>
122
123      <TD CLASS="grid" BGCOLOR="#dddddd"></TD>
124      <TD CLASS="grid" BGCOLOR="#dddddd"></TD>
125      <TD CLASS="grid" BGCOLOR="#dddddd"></TD>
126      <TD CLASS="grid" BGCOLOR="#dddddd"></TD>
127      <TD CLASS="grid" BGCOLOR="#dddddd"><I><% $balance_forward %></I></TD>
128
129    </TR>
130 %}
131 %
132 %my $balance = 0;
133 %my %target = ();
134 %my $money_char = $conf->config('money_char') || '$';
135 %
136 %my $years =  $conf->config('payment_history-years') || 2;
137 %my $older_than = time - $years * 31556736; #60*60*24*365.24
138 %my $hidden = 0;
139 %my $seen = 0;
140 %my $old_history = 0;
141 %my $lastdate = 0;
142 %
143 %foreach my $item ( sort { $a->{'date'} <=> $b->{'date'} } @history ) {
144 %
145 %  $lastdate = $item->{'date'};
146 %
147 %  my $display;
148 %  if ( $item->{'date'} < $older_than ) {
149 %    $display = ' STYLE="display:none" ';
150 %    $hidden = 1;
151 %  } else {
152 %
153 %    $display = '';
154 %
155 %    if ( $hidden && ! $seen++ ) {
156 %      balance_forward_row($balance, $item->{'date'});
157 %    }
158 %
159 %  }
160 %
161 %  if ( $bgcolor eq $bgcolor1 ) {
162 %    $bgcolor = $bgcolor2;
163 %  } else {
164 %    $bgcolor = $bgcolor1;
165 %  }
166 %
167 %  my $charge  = exists($item->{'charge'})
168 %                  ? sprintf("$money_char\%.2f", $item->{'charge'})
169 %                  : '';
170 %
171 %  my $payment = exists($item->{'payment'})
172 %                  ? sprintf("-&nbsp;$money_char\%.2f", $item->{'payment'})
173 %                  : '';
174 %
175 %  $payment ||= sprintf( "<DEL>-&nbsp;$money_char\%.2f</DEL>",
176 %                        $item->{'void_payment'}
177 %                      )
178 %    if exists($item->{'void_payment'});
179 %
180 %  my $credit  = exists($item->{'credit'})
181 %                  ? sprintf("-&nbsp;$money_char\%.2f", $item->{'credit'})
182 %                  : '';
183 %
184 %  my $refund  = exists($item->{'refund'})
185 %                  ? sprintf("$money_char\%.2f", $item->{'refund'})
186 %                  : '';
187 %
188 %  my $target = exists($item->{'target'}) ? $item->{'target'} : '';
189 %
190 %  $balance += $item->{'charge'}  if exists $item->{'charge'};
191 %  $balance -= $item->{'payment'} if exists $item->{'payment'};
192 %  $balance -= $item->{'credit'}  if exists $item->{'credit'};
193 %  $balance += $item->{'refund'}  if exists $item->{'refund'};
194 %  $balance = sprintf("%.2f", $balance);
195 %  $balance =~ s/^\-0\.00$/0.00/; #yay ieee fp
196 %  ( my $showbalance = $money_char. $balance ) =~ s/^\$\-/-&nbsp;\$/;
197 %
198 %
199
200
201   <TR <% $display ? $display.' ID="old_history'.$old_history++.'"'  : ''%>>
202     <TD CLASS="grid" BGCOLOR="<% $bgcolor %>">
203 % unless ( !$target || $target{$target}++ ) { 
204
205         <A NAME="<% $target %>">
206 % } 
207
208       <% time2str("%D",$item->{'date'}) %>
209 % if ( $target && $target{$target} == 1 ) { 
210
211         </A>
212 % } 
213
214       </FONT>
215     </TD>
216     <TD CLASS="grid" BGCOLOR="<% $bgcolor %>">
217       <% $item->{'desc'} %>
218     </TD>
219     <TD ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
220       <% $charge  %>
221     </TD>
222     <TD ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
223       <% $payment %>
224     </TD>
225     <TD ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
226       <% $credit  %>
227     </TD>
228     <TD ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
229       <% $refund  %>
230     </TD>
231     <TD ALIGN="right" CLASS="grid" BGCOLOR="<% $bgcolor %>">
232       <% $showbalance %>
233     </TD>
234   </TR>
235 % } 
236
237 %if ( scalar(@history) && $hidden && ! $seen++ ) {
238 %  balance_forward_row($balance, $lastdate);
239 %}
240
241 </TABLE>
242
243 <SCRIPT TYPE="text/javascript">
244
245 function show_history () {
246   //alert('showing history!');
247
248   var balance_forward_row = document.getElementById('balance_forward_row');
249
250   balance_forward_row.style.display = 'none';
251   for ( var i = 0; i < <% $old_history %>; i++ ) {
252     var oldRow = document.getElementById('old_history'+i);
253     oldRow.style.display = '';
254   }
255
256 }
257
258 </SCRIPT>
259
260 <%init>
261
262 my( $cust_main ) = @_;
263 my $custnum = $cust_main->custnum;
264
265 my $conf = new FS::Conf;
266
267 my $curuser = $FS::CurrentUser::CurrentUser;
268
269 my @payby = grep /\w/, $conf->config('payby');
270 #@payby = (qw( CARD DCRD CHEK DCHK LECB BILL CASH WEST COMP ))
271 @payby = (qw( CARD DCRD CHEK DCHK LECB BILL CASH COMP ))
272   unless @payby;
273 my %payby = map { $_=>1 } @payby;
274
275 my %status = (
276   'Queued'     => 'O', #Open
277   'In-transit' => 'I',
278   'Complete'   => 'R', #Resolved
279   'All'        => '',
280 );
281
282 #get payment history
283 my @history = ();
284
285 #invoices
286 foreach my $cust_bill ($cust_main->cust_bill) {
287   push @history, {
288     'date'   => $cust_bill->_date,
289     'desc'   => include('payment_history/invoice.html', $cust_bill),
290     'charge' => $cust_bill->charged,
291   };
292 }
293
294 #payments (some false laziness w/credits)
295 foreach my $cust_pay ($cust_main->cust_pay) {
296   push @history, {
297     'date'    => $cust_pay->_date,
298     'desc'    => include('payment_history/payment.html', $cust_pay),
299     'payment' => $cust_pay->paid,
300     #'target'  => $target, #XXX
301   };
302 }
303
304 #voided payments
305 foreach my $cust_pay_void ($cust_main->cust_pay_void) {
306   push @history, {
307     'date'   => $cust_pay_void->_date,
308     'desc'   => include('payment_history/voided_payment.html', $cust_pay_void),
309     'void_payment' => $cust_pay_void->paid,
310   };
311
312 }
313
314 #credits (some false laziness w/payments)
315 foreach my $cust_credit ($cust_main->cust_credit) {
316   push @history, {
317     'date'   => $cust_credit->_date,
318     'desc'   => include('payment_history/credit.html', $cust_credit),
319     'credit' => $cust_credit->amount,
320   };
321
322 }
323
324 #refunds
325 foreach my $cust_refund ($cust_main->cust_refund) {
326   push @history, {
327     'date'   => $cust_refund->_date,
328     'desc'   => include('payment_history/refund.html', $cust_refund),
329     'refund' => $cust_refund->refund,
330   };
331
332 }
333
334 </%init>