optimize CDR rating after timed rate perf regression, RT#15739
[freeside.git] / httemplate / misc / payment.cgi
1 <& /elements/header.html, mt("Process [_1] payment",$type{$payby})  &>
2 <& /elements/small_custview.html, $cust_main, '', '', popurl(2) . "view/cust_main.cgi" &>
3 <FORM NAME="OneTrueForm" ACTION="process/payment.cgi" METHOD="POST" onSubmit="document.OneTrueForm.process.disabled=true">
4 <INPUT TYPE="hidden" NAME="custnum"   VALUE="<% $custnum %>">
5 <INPUT TYPE="hidden" NAME="payby"     VALUE="<% $payby %>">
6 <INPUT TYPE="hidden" NAME="payunique" VALUE="<% $payunique %>">
7 <INPUT TYPE="hidden" NAME="balance"   VALUE="<% $balance %>">
8
9 <& /elements/init_overlib.html &>
10
11 <% ntable('#cccccc') %>
12   <TR>
13     <TH ALIGN="right"><% mt('Payment amount') |h %></TH>
14     <TD COLSPAN=7>
15       <TABLE><TR><TD BGCOLOR="#ffffff">
16         <% $money_char %><INPUT NAME     = "amount"
17                                 ID       = "amount"
18                                 TYPE     = "text"
19                                 VALUE    = "<% $amount %>"
20                                 SIZE     = 8
21                                 STYLE    = "text-align:right;"
22 %                               if ( $fee ) {
23                                   onChange   = "amount_changed(this)"
24                                   onKeyDown  = "amount_changed(this)"
25                                   onKeyUp    = "amount_changed(this)"
26                                   onKeyPress = "amount_changed(this)"
27 %                               }
28                          >
29       </TD><TD BGCOLOR="#cccccc">
30 %        if ( $fee ) {
31            <INPUT TYPE="hidden" NAME="fee_pkgpart" VALUE="<% $fee_pkg->pkgpart %>">
32            <INPUT TYPE="hidden" NAME="fee" VALUE="<% $fee_display eq 'add' ? $fee : '' %>">
33            <B><FONT SIZE='+1'><% $fee_op %></FONT>
34               <% $money_char . $fee %>
35            </B>
36            <% $fee_pkg->pkg |h %>
37            <B><FONT SIZE='+1'>=</FONT></B>
38       </TD><TD ID="ajax_total_cell" BGCOLOR="#dddddd" STYLE="border:1px solid blue">
39            <FONT SIZE="+1"><% length($amount) ? $money_char. sprintf('%.2f', ($fee_display eq 'add') ? $amount + $fee : $amount - $fee ) : '' %> <% $fee_display eq 'add' ? 'TOTAL' : 'AVAILABLE' %></FONT>
40   
41 %        }
42       </TD></TR></TABLE>
43     </TD>
44   </TR>
45
46 % if ( $fee ) {
47
48     <SCRIPT TYPE="text/javascript">
49
50       function amount_changed(what) {
51
52
53         var total = '';
54         if ( what.value.length ) {
55           total = parseFloat(what.value) <% $fee_op %> <% $fee %>;
56           /* total = Math.round(total*100)/100; */
57           total = '<% $money_char %>' + total.toFixed(2);
58         }
59
60         var total_cell = document.getElementById('ajax_total_cell');
61         total_cell.innerHTML = '<FONT SIZE="+1">' + total + ' <% $fee_display eq 'add' ? 'TOTAL' : 'AVAILABLE' %></FONT>';
62
63       }
64
65     </SCRIPT>
66
67 % }
68
69 <& /elements/tr-select-discount_term.html,
70              'custnum' => $custnum,
71              'amount_id' => 'amount',
72 &>
73
74 % if ( $payby eq 'CARD' ) {
75 %
76 %   my( $payinfo, $paycvv, $month, $year ) = ( '', '', '', '' );
77 %   my $payname = $cust_main->first. ' '. $cust_main->getfield('last');
78 %   if ( $cust_main->payby =~ /^(CARD|DCRD)$/ ) {
79 %     $payinfo = $cust_main->paymask;
80 %     $paycvv = $cust_main->paycvv;
81 %     ( $month, $year ) = $cust_main->paydate_monthyear;
82 %     $payname = $cust_main->payname if $cust_main->payname;
83 %   }
84
85     <TR>
86       <TH ALIGN="right"><% mt('Card number') |h %></TH>
87       <TD COLSPAN=7>
88         <TABLE>
89           <TR>
90             <TD>
91               <INPUT TYPE="text" NAME="payinfo" SIZE=20 MAXLENGTH=19 VALUE="<%$payinfo%>"> </TD>
92             <TH><% mt('Exp.') |h %></TH>
93             <TD>
94               <SELECT NAME="month">
95 % for ( ( map "0$_", 1 .. 9 ), 10 .. 12 ) { 
96
97                   <OPTION<% $_ == $month ? ' SELECTED' : '' %>><% $_ %>
98 % } 
99
100               </SELECT>
101             </TD>
102             <TD> / </TD>
103             <TD>
104               <SELECT NAME="year">
105 % my @a = localtime; for ( $a[5]+1900 .. $a[5]+1915 ) { 
106
107                   <OPTION<% $_ == $year ? ' SELECTED' : '' %>><% $_ %>
108 % } 
109
110               </SELECT>
111             </TD>
112           </TR>
113         </TABLE>
114       </TD>
115     </TR>
116     <TR>
117       <TH ALIGN="right"><% mt('CVV2') |h %></TH>
118       <TD><INPUT TYPE="text" NAME="paycvv" VALUE="<% $paycvv %>" SIZE=4 MAXLENGTH=4>
119           (<A HREF="javascript:void(0);" onClick="overlib( OLiframeContent('../docs/cvv2.html', 480, 352, 'cvv2_popup' ), CAPTION, 'CVV2 Help', STICKY, AUTOSTATUSCAP, CLOSECLICK, DRAGGABLE ); return false;"><% mt('help') |h %></A>)
120       </TD>
121     </TR>
122     <TR>
123       <TH ALIGN="right"><% mt('Exact name on card') |h %></TH>
124       <TD><INPUT TYPE="text" SIZE=32 MAXLENGTH=80 NAME="payname" VALUE="<%$payname%>"></TD>
125     </TR>
126
127     <& /elements/location.html,
128                   'object'         => $cust_main, #XXX errors???
129                   'no_asterisks'   => 1,
130                   'address1_label' => emt('Card billing address'),
131     &>
132
133 % } elsif ( $payby eq 'CHEK' ) {
134 %
135 %   my( $account, $aba, $branch, $payname, $ss, $paytype, $paystate,
136 %       $stateid, $stateid_state )
137 %     = ( '', '', '', '', '', '', '', '', '' );
138 %   if ( $cust_main->payby =~ /^(CHEK|DCHK)$/ ) {
139 %     $cust_main->paymask =~ /^([\dx]+)\@([\d\.x]*)$/i
140 %       or die "unparsable payinfo ". $cust_main->payinfo;
141 %     ($account, $aba) = ($1, $2);
142 %     ($branch,$aba) = split('\.',$aba)
143 %       if $conf->exists('cust_main-require-bank-branch');
144 %     $payname = $cust_main->payname;
145 %     $ss = $cust_main->ss;
146 %     $paytype = $cust_main->getfield('paytype');
147 %     $paystate = $cust_main->getfield('paystate');
148 %     $stateid = $cust_main->getfield('stateid');
149 %     $stateid_state = $cust_main->getfield('stateid_state');
150 %   }
151
152     <INPUT TYPE="hidden" NAME="month" VALUE="12">
153     <INPUT TYPE="hidden" NAME="year" VALUE="2037">
154     <TR>
155       <TD ALIGN="right"><% mt('Account number') |h %></TD>
156       <TD><INPUT TYPE="text" SIZE=10 NAME="payinfo1" VALUE="<%$account%>"></TD>
157       <TD ALIGN="right"><% mt('Type') |h %></TD>
158       <TD><SELECT NAME="paytype"><% join('', map { qq!<OPTION VALUE="$_" !.($paytype eq $_ ? 'SELECTED' : '').">$_</OPTION>" } @FS::cust_main::paytypes) %></SELECT></TD>
159     </TR>
160     <TR>
161       <TD ALIGN="right"><% mt('ABA/Routing number') |h %></TD>
162       <TD>
163         <INPUT TYPE="text" SIZE=10 MAXLENGTH=9 NAME="payinfo2" VALUE="<%$aba%>">
164         (<A HREF="javascript:void(0);" onClick="overlib( OLiframeContent('../docs/ach.html', 380, 240, 'ach_popup' ), CAPTION, 'ACH Help', STICKY, AUTOSTATUSCAP, CLOSECLICK, DRAGGABLE ); return false;"><% mt('help') |h %></A>)
165       </TD>
166     </TR>
167 %   if ( $conf->exists('cust_main-require-bank-branch') ) {
168       <TR>
169         <TD ALIGN="right"><% mt('Branch number') |h %></TD>
170         <TD>
171           <INPUT TYPE="text" NAME="payinfo3" VALUE="<%$branch%>">
172         </TD>
173       </TR>
174 %   }
175     <TR>
176       <TD ALIGN="right"><% mt('Bank name') |h %></TD>
177       <TD><INPUT TYPE="text" NAME="payname" VALUE="<%$payname%>"></TD>
178     </TR>
179
180 %   if ( $conf->exists('show_bankstate') ) {
181       <TR>
182         <TD ALIGN="right"><% mt('Bank state') |h %></TD>
183         <TD><& /elements/select-state.html,
184                          'disable_empty' => 0,
185                          'empty_label'   => emt('(choose)'),
186                          'state'         => $paystate,
187                          'country'       => $cust_main->country,
188                          'prefix'        => 'pay',
189             &>
190         </TD>
191       </TR>
192 %   } else {
193       <INPUT TYPE="hidden" NAME="paystate" VALUE="<% $paystate %>">
194 %   }
195
196 %   if ( $conf->exists('show_ss') ) {
197       <TR>
198         <TD ALIGN="right">
199           <% mt('Account holder') |h %><BR>
200           <% mt('Social security or tax ID #') |h %> 
201         </TD>
202         <TD><INPUT TYPE="text" NAME="ss" VALUE="<% $ss %>"></TD>
203       </TR>
204 %   } else {
205       <INPUT TYPE="hidden" NAME="ss" VALUE="<% $ss %>"></TD>
206 %   }
207
208 %   if ( $conf->exists('show_stateid') ) {
209       <TR>
210         <TD ALIGN="right">
211           <% mt('Account holder') |h %><BR>
212           <% mt("Driver's license or state ID #") |h %> 
213         </TD>
214         <TD><INPUT TYPE="text" NAME="stateid" VALUE="<% $stateid %>"></TD>
215         <TD ALIGN="right"><% mt('State') |h %></TD>
216         <TD><& /elements/select-state.html,
217                          'disable_empty' => 0,
218                          'empty_label'   => emt('(choose)'),
219                          'state'         => $stateid_state,
220                          'country'       => $cust_main->country,
221                          'prefix'        => 'stateid_',
222             &>
223         </TD>
224       </TR>
225 %   } else {
226       <INPUT TYPE="hidden" NAME="stateid" VALUE="<% $stateid %>">
227       <INPUT TYPE="hidden" NAME="stateid_state" VALUE="<% $stateid_state %>">
228 %   }
229
230 % } #end CARD/CHEK-specific section
231
232
233 <TR>
234   <TD COLSPAN=2>
235     <INPUT TYPE="checkbox" CHECKED NAME="save" VALUE="1">
236     <% mt('Remember this information') |h %>
237   </TD>
238 </TR>
239
240 % if ( $conf->exists("batch-enable")
241 %      || grep $payby eq $_, $conf->config('batch-enable_payby')
242 %    ) {
243 %
244 %     if ( grep $payby eq $_, $conf->config('realtime-disable_payby') ) {
245
246           <INPUT TYPE="hidden" NAME="batch" VALUE="1">
247
248 %     } else {
249
250           <TR>
251             <TD COLSPAN=2>
252               <INPUT TYPE="checkbox" NAME="batch" VALUE="1">
253               <% mt('Add to current batch') |h %> 
254             </TD>
255           </TR>
256
257 %     }
258 % }
259
260 <TR>
261   <TD COLSPAN=2>
262     <INPUT TYPE="checkbox"<% ( ( $payby eq 'CARD' && $cust_main->payby ne 'DCRD' ) || ( $payby eq 'CHEK' && $cust_main->payby eq 'CHEK' ) ) ? ' CHECKED' : '' %> NAME="auto" VALUE="1" onClick="if (this.checked) { document.OneTrueForm.save.checked=true; }">
263     <% mt("Charge future payments to this [_1] automatically",$type{$payby}) |h %> 
264   </TD>
265 </TR>
266
267 </TABLE>
268
269 <BR>
270 <INPUT TYPE="submit" NAME="process" VALUE="<% mt('Process payment') |h %>">
271 </FORM>
272
273 <& /elements/footer.html &>
274 <%init>
275
276 die "access denied"
277   unless $FS::CurrentUser::CurrentUser->access_right('Process payment');
278
279 my %type = ( 'CARD' => 'credit card',
280              'CHEK' => 'electronic check (ACH)',
281            );
282
283 $cgi->param('payby') =~ /^(CARD|CHEK)$/
284   or die "unknown payby ". $cgi->param('payby');
285 my $payby = $1;
286
287 $cgi->param('custnum') =~ /^(\d+)$/
288   or die "illegal custnum ". $cgi->param('custnum');
289 my $custnum = $1;
290
291 my $cust_main = qsearchs( 'cust_main', { 'custnum'=>$custnum } );
292 die "unknown custnum $custnum" unless $cust_main;
293
294 my $balance = $cust_main->balance;
295
296 my $payinfo = '';
297
298 my $conf = new FS::Conf;
299
300 my $money_char = $conf->config('money_char') || '$';
301
302 #false laziness w/selfservice make_payment.html shortcut for one-country
303 my %states = map { $_->state => 1 }
304                qsearch('cust_main_county', {
305                  'country' => $conf->config('countrydefault') || 'US'
306                } );
307 my @states = sort { $a cmp $b } keys %states;
308
309 my $fee = '';
310 my $fee_pkg = '';
311 my $fee_display = '';
312 my $fee_op = '';
313 my $num_payments = scalar($cust_main->cust_pay);
314 #handle old cust_main.pm (remove...)
315 $num_payments = scalar( @{ [ $cust_main->cust_pay ] } )
316   unless defined $num_payments;
317 if ( $conf->config('manual_process-pkgpart')
318      and ! $conf->exists('manual_process-skip_first') || $num_payments
319    )
320 {
321
322   $fee_display = $conf->config('manual_process-display') || 'add';
323   $fee_op = $fee_display eq 'add' ? '+' : '-';
324
325   $fee_pkg =
326     qsearchs('part_pkg', { pkgpart=>$conf->config('manual_process-pkgpart') } );
327
328   #well ->unit_setup or ->calc_setup both call for a $cust_pkg
329   # (though ->unit_setup doesn't use it...)
330   $fee = $fee_pkg->option('setup_fee')
331     if $fee_pkg; #in case.. better than dying with a perl traceback
332
333 }
334
335 my $amount = '';
336 if ( $balance > 0 ) {
337   $amount = $balance;
338   $amount += $fee
339     if $fee && $fee_display eq 'subtract';
340
341   my $cc_surcharge_pct = $conf->config('credit-card-surcharge-percentage');
342   $amount += $amount * $cc_surcharge_pct/100 if $cc_surcharge_pct > 0;
343
344   $amount = sprintf("%.2f", $amount);
345 }
346
347 my $payunique = "webui-payment-". time. "-$$-". rand() * 2**32;
348
349 </%init>