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