RT# 76093 - Added ability to charge a processing fee when taking a payment on the...
[freeside.git] / httemplate / misc / payment.cgi
index 093494a..7911a5d 100644 (file)
@@ -1,5 +1,7 @@
-<& /elements/header.html, mt("Process [_1] payment",$type{$payby})  &>
-<& /elements/small_custview.html, $cust_main, '', '', popurl(2) . "view/cust_main.cgi" &>
+<& /elements/header-cust_main.html, view=>'payment_history', cust_main=>$cust_main &>
+
+<h2><% emt("Process [_1] payment",$type{$payby}) %></h2>
+
 <FORM NAME="OneTrueForm" ACTION="process/payment.cgi" METHOD="POST" onSubmit="document.OneTrueForm.process.disabled=true">
 <INPUT TYPE="hidden" NAME="custnum"   VALUE="<% $custnum %>">
 <INPUT TYPE="hidden" NAME="payby"     VALUE="<% $payby %>">
 
 <& /elements/init_overlib.html &>
 
-<% ntable('#cccccc') %>
-  <TR>
-    <TH ALIGN="right"><% mt('Payment amount') |h %></TH>
-    <TD COLSPAN=7>
-      <TABLE><TR><TD BGCOLOR="#ffffff">
-        <% $money_char %><INPUT NAME     = "amount"
-                                ID       = "amount"
-                                TYPE     = "text"
-                                VALUE    = "<% $amount %>"
-                                SIZE     = 8
-                                STYLE    = "text-align:right;"
-%                               if ( $fee ) {
-                                  onChange   = "amount_changed(this)"
-                                  onKeyDown  = "amount_changed(this)"
-                                  onKeyUp    = "amount_changed(this)"
-                                  onKeyPress = "amount_changed(this)"
-%                               }
-                         >
-      </TD><TD BGCOLOR="#cccccc">
-%        if ( $fee ) {
-           <INPUT TYPE="hidden" NAME="fee_pkgpart" VALUE="<% $fee_pkg->pkgpart %>">
-           <INPUT TYPE="hidden" NAME="fee" VALUE="<% $fee_display eq 'add' ? $fee : '' %>">
-           <B><FONT SIZE='+1'><% $fee_op %></FONT>
-              <% $money_char . $fee %>
-           </B>
-           <% $fee_pkg->pkg |h %>
-           <B><FONT SIZE='+1'>=</FONT></B>
-      </TD><TD ID="ajax_total_cell" BGCOLOR="#dddddd" STYLE="border:1px solid blue">
-           <FONT SIZE="+1"><% length($amount) ? $money_char. sprintf('%.2f', ($fee_display eq 'add') ? $amount + $fee : $amount - $fee ) : '' %> <% $fee_display eq 'add' ? 'TOTAL' : 'AVAILABLE' %></FONT>
-  
-%        }
-      </TD></TR></TABLE>
-    </TD>
-  </TR>
-
-% if ( $fee ) {
-
-    <SCRIPT TYPE="text/javascript">
-
-      function amount_changed(what) {
-
-
-        var total = '';
-        if ( what.value.length ) {
-          total = parseFloat(what.value) <% $fee_op %> <% $fee %>;
-          /* total = Math.round(total*100)/100; */
-          total = '<% $money_char %>' + total.toFixed(2);
-        }
-
-        var total_cell = document.getElementById('ajax_total_cell');
-        total_cell.innerHTML = '<FONT SIZE="+1">' + total + ' <% $fee_display eq 'add' ? 'TOTAL' : 'AVAILABLE' %></FONT>';
-
-      }
-
-    </SCRIPT>
-
-% }
-
-<& /elements/tr-select-discount_term.html,
-             'custnum' => $custnum,
-             'amount_id' => 'amount',
-&>
-
-% if ( $payby eq 'CARD' ) {
-%
-%   my( $payinfo, $paycvv, $month, $year ) = ( '', '', '', '' );
-%   my $payname = $cust_main->first. ' '. $cust_main->getfield('last');
-%   if ( $cust_main->payby =~ /^(CARD|DCRD)$/ ) {
-%     $payinfo = $cust_main->paymask;
-%     $paycvv = $cust_main->paycvv;
-%     ( $month, $year ) = $cust_main->paydate_monthyear;
-%     $payname = $cust_main->payname if $cust_main->payname;
-%   }
-
-    <TR>
-      <TH ALIGN="right"><% mt('Card number') |h %></TH>
-      <TD COLSPAN=7>
-        <TABLE>
-          <TR>
-            <TD>
-              <INPUT TYPE="text" NAME="payinfo" SIZE=20 MAXLENGTH=19 VALUE="<%$payinfo%>"> </TD>
-            <TH><% mt('Exp.') |h %></TH>
-            <TD>
-              <SELECT NAME="month">
-% for ( ( map "0$_", 1 .. 9 ), 10 .. 12 ) { 
-
-                  <OPTION<% $_ == $month ? ' SELECTED' : '' %>><% $_ %>
-% } 
-
-              </SELECT>
-            </TD>
-            <TD> / </TD>
-            <TD>
-              <SELECT NAME="year">
-% my @a = localtime; for ( $a[5]+1900 .. $a[5]+1915 ) { 
-
-                  <OPTION<% $_ == $year ? ' SELECTED' : '' %>><% $_ %>
-% } 
-
-              </SELECT>
-            </TD>
-          </TR>
-        </TABLE>
-      </TD>
-    </TR>
-    <TR>
-      <TH ALIGN="right"><% mt('CVV2') |h %></TH>
-      <TD><INPUT TYPE="text" NAME="paycvv" VALUE="<% $paycvv %>" SIZE=4 MAXLENGTH=4>
-          (<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>)
-      </TD>
-    </TR>
-    <TR>
-      <TH ALIGN="right"><% mt('Exact name on card') |h %></TH>
-      <TD><INPUT TYPE="text" SIZE=32 MAXLENGTH=80 NAME="payname" VALUE="<%$payname%>"></TD>
-    </TR>
-
-    <& /elements/location.html,
-                  'object'         => $cust_main, #XXX errors???
-                  'no_asterisks'   => 1,
-                  'address1_label' => emt('Card billing address'),
+<TABLE class="fsinnerbox">
+
+  <& /elements/tr-select-payment_options.html,
+       'cust_main'          => $cust_main,
+       'process-pkgpart'    => 
+          scalar($conf->config('manual_process-pkgpart', $cust_main->agentnum)),
+       'process-display'    => scalar($conf->config('manual_process-display')),
+       'process-skip_first' => $conf->exists('manual_process-skip_first'),
+       'num_payments'       => scalar($cust_main->cust_pay), 
+       'surcharge_percentage' =>
+         ( $payby eq 'CARD'
+             ? scalar($conf->config('credit-card-surcharge-percentage', $cust_main->agentnum))
+             : 0
+         ),
+       'surcharge_flatfee' =>
+         ( $payby eq 'CARD'
+             ? scalar($conf->config('credit-card-surcharge-flatfee', $cust_main->agentnum))
+             : 0
+         ),
+         'processing_fee' => scalar($conf->config('processing-fee', $cust_main->agentnum)),
+  &>
+
+% if ( $conf->exists('part_pkg-term_discounts') ) {
+    <& /elements/tr-select-discount_term.html,
+         'custnum'   => $custnum,
+         'amount_id' => 'amount',
     &>
+% }
 
-% } elsif ( $payby eq 'CHEK' ) {
-%
-%   my( $account, $aba, $branch, $payname, $ss, $paytype, $paystate,
-%       $stateid, $stateid_state )
-%     = ( '', '', '', '', '', '', '', '', '' );
-%   if ( $cust_main->payby =~ /^(CHEK|DCHK)$/ ) {
-%     $cust_main->paymask =~ /^([\dx]+)\@([\d\.x]*)$/i
-%       or die "unparsable payinfo ". $cust_main->payinfo;
-%     ($account, $aba) = ($1, $2);
-%     ($branch,$aba) = split('\.',$aba)
-%       if $conf->config('echeck-country') eq 'CA';
-%     $payname = $cust_main->payname;
-%     $ss = $cust_main->ss;
-%     $paytype = $cust_main->getfield('paytype');
-%     $paystate = $cust_main->getfield('paystate');
-%     $stateid = $cust_main->getfield('stateid');
-%     $stateid_state = $cust_main->getfield('stateid_state');
-%   }
-%
-%  #false laziness w/{edit,view}/cust_main/billing.html
-%  my $routing_label = $conf->config('echeck-country') eq 'US'
-%                        ? 'ABA/Routing number'
-%                        : 'Routing number';
-%  my $routing_size      = $conf->config('echeck-country') eq 'CA' ? 4 : 10;
-%  my $routing_maxlength = $conf->config('echeck-country') eq 'CA' ? 3 : 9;
-
-    <INPUT TYPE="hidden" NAME="month" VALUE="12">
-    <INPUT TYPE="hidden" NAME="year" VALUE="2037">
-    <TR>
-      <TD ALIGN="right"><% mt('Account number') |h %></TD>
-      <TD><INPUT TYPE="text" SIZE=10 NAME="payinfo1" VALUE="<%$account%>"></TD>
-      <TD ALIGN="right"><% mt('Type') |h %></TD>
-      <TD><SELECT NAME="paytype"><% join('', map { qq!<OPTION VALUE="$_" !.($paytype eq $_ ? 'SELECTED' : '').">$_</OPTION>" } @FS::cust_main::paytypes) %></SELECT></TD>
-    </TR>
-    <TR>
-      <TD ALIGN="right"><% mt($routing_label) |h %></TD>
-      <TD>
-        <INPUT TYPE="text" SIZE="<% $routing_size %>" MAXLENGTH="<% $routing_maxlength %>" NAME="payinfo2" VALUE="<%$aba%>">
-        (<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>)
-      </TD>
-    </TR>
-%   if ( $conf->config('echeck-country') eq 'CA' ) {
-      <TR>
-        <TD ALIGN="right"><% mt('Branch number') |h %></TD>
-        <TD>
-          <INPUT TYPE="text" NAME="payinfo3" VALUE="<%$branch%>" SIZE=6 MAXLENGTH=5>
-        </TD>
-      </TR>
-%   }
-    <TR>
-      <TD ALIGN="right"><% mt('Bank name') |h %></TD>
-      <TD><INPUT TYPE="text" NAME="payname" VALUE="<%$payname%>"></TD>
-    </TR>
-
-%   if ( $conf->exists('show_bankstate') ) {
-      <TR>
-        <TD ALIGN="right"><% mt('Bank state') |h %></TD>
-        <TD><& /elements/select-state.html,
-                         'disable_empty' => 0,
-                         'empty_label'   => emt('(choose)'),
-                         'state'         => $paystate,
-                         'country'       => $cust_main->country,
-                         'prefix'        => 'pay',
-            &>
-        </TD>
-      </TR>
-%   } else {
-      <INPUT TYPE="hidden" NAME="paystate" VALUE="<% $paystate %>">
-%   }
-
-%   if ( $conf->exists('show_ss') ) {
-      <TR>
-        <TD ALIGN="right">
-          <% mt('Account holder') |h %><BR>
-          <% mt('Social security or tax ID #') |h %> 
-        </TD>
-        <TD><INPUT TYPE="text" NAME="ss" VALUE="<% $ss %>"></TD>
-      </TR>
-%   } else {
-      <INPUT TYPE="hidden" NAME="ss" VALUE="<% $ss %>"></TD>
-%   }
-
-%   if ( $conf->exists('show_stateid') ) {
-      <TR>
-        <TD ALIGN="right">
-          <% mt('Account holder') |h %><BR>
-          <% mt("Driver's license or state ID #") |h %> 
-        </TD>
-        <TD><INPUT TYPE="text" NAME="stateid" VALUE="<% $stateid %>"></TD>
-        <TD ALIGN="right"><% mt('State') |h %></TD>
-        <TD><& /elements/select-state.html,
-                         'disable_empty' => 0,
-                         'empty_label'   => emt('(choose)'),
-                         'state'         => $stateid_state,
-                         'country'       => $cust_main->country,
-                         'prefix'        => 'stateid_',
-            &>
-        </TD>
-      </TR>
-%   } else {
-      <INPUT TYPE="hidden" NAME="stateid" VALUE="<% $stateid %>">
-      <INPUT TYPE="hidden" NAME="stateid_state" VALUE="<% $stateid_state %>">
-%   }
-
-% } #end CARD/CHEK-specific section
-
-
-<TR>
-  <TD COLSPAN=2>
-    <INPUT TYPE="checkbox" CHECKED NAME="save" VALUE="1">
-    <% mt('Remember this information') |h %>
-  </TD>
-</TR>
-
+% my $disallow_no_auto_apply = 0;
 % if ( $conf->exists("batch-enable")
 %      || grep $payby eq $_, $conf->config('batch-enable_payby')
 %    ) {
 %
 %     if ( grep $payby eq $_, $conf->config('realtime-disable_payby') ) {
+%       $disallow_no_auto_apply = 1;
 
           <INPUT TYPE="hidden" NAME="batch" VALUE="1">
 
 %     } else {
 
           <TR>
-            <TD COLSPAN=2>
-              <INPUT TYPE="checkbox" NAME="batch" VALUE="1">
-              <% mt('Add to current batch') |h %> 
+            <TH ALIGN="right">&nbsp;&nbsp;&nbsp;<% mt('Add to current batch') |h %></TH>
+            <TD>
+              <INPUT TYPE="checkbox" NAME="batch" VALUE="1" ID="batch_checkbox" ONCHANGE="change_batch_checkbox()">
             </TD>
           </TR>
 
 %     }
 % }
 
-<TR>
-  <TD COLSPAN=2>
-    <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; }">
-    <% mt("Charge future payments to this [_1] automatically",$type{$payby}) |h %> 
+% unless ($disallow_no_auto_apply) {
+%   # false laziness with edit/cust_pay.cgi
+
+<TR ID="apply_box_row">
+  <TH ALIGN="right"><% mt('Auto-apply to invoices') |h %></TH>
+  <TD>
+    <SELECT NAME="apply" ID="apply_box">
+      <OPTION VALUE="yes" SELECTED><% mt('yes') |h %></OPTION> 
+      <OPTION VALUE=""><% mt('not now') |h %></OPTION>
+      <OPTION VALUE="never"><% mt('never') |h %></OPTION>
+    </SELECT>
   </TD>
 </TR>
 
+% # this can go away if no_auto_apply handling gets added to batch payment processing
+<SCRIPT>
+function change_batch_checkbox () {
+  if (document.getElementById('batch_checkbox').checked) {
+    document.getElementById('apply_box').disabled = true;
+    document.getElementById('apply_box_row').style.display = 'none';
+  } else {
+    document.getElementById('apply_box').disabled = false;
+    document.getElementById('apply_box_row').style.display = '';
+  }
+}
+</SCRIPT>
+
+% }
+
+<SCRIPT TYPE="text/javascript">
+  function cust_payby_changed (what) {
+    var custpaybynum = what.options[what.selectedIndex].value
+    if ( custpaybynum == '' || custpaybynum == '0' ) {
+       //what.form.payinfo.disabled = false;
+       $('#cust_payby').slideDown();
+    } else {
+       //what.form.payinfo.value = '';
+       //what.form.payinfo.disabled = true;
+       $('#cust_payby').slideUp();
+    }
+  }
+
+  function enableAmountField() {
+    document.getElementById('amount').disabled = false;
+  }
+
+</SCRIPT>
+
+% #can't quite handle CARD/CHEK on the same page yet, but very close
+% #does it make sense from a UI/usability perspective?
+%
+% my @cust_payby = ();
+% if ( $payby eq 'CARD' ) {
+%   @cust_payby = $cust_main->cust_payby('CARD','DCRD');
+% } elsif ( $payby eq 'CHEK' ) {
+%   @cust_payby = $cust_main->cust_payby('CHEK','DCHK');
+% } else {
+%   die "unknown payby $payby";
+% }
+%
+% my $custpaybynum = length(scalar($cgi->param('custpaybynum')))
+%                      ? scalar($cgi->param('custpaybynum'))
+%                      : scalar(@cust_payby) && $cust_payby[0]->custpaybynum;
+
+<& /elements/tr-select-cust_payby.html,
+     'cust_payby' => \@cust_payby,
+     'curr_value' => $custpaybynum,
+     'onchange'   => 'cust_payby_changed(this)',
+&>
+
 </TABLE>
+<BR>
+<DIV ID="cust_payby"
+  <% $custpaybynum ? 'STYLE="display:none"'
+                   : ''
+  %>
+>
+<TABLE class="fsinnerbox">
+
+<& /elements/cust_payby_new.html,
+     'cust_payby' => \@cust_payby,
+     'curr_value' => $custpaybynum,
+&>
+
+</TABLE>
+</DIV>
 
 <BR>
-<INPUT TYPE="submit" NAME="process" VALUE="<% mt('Process payment') |h %>">
+<INPUT TYPE="submit" NAME="process" ID="process" VALUE="<% mt('Process payment') |h %>" disabled="disabled" onclick="enableAmountField()">
 </FORM>
 
-<& /elements/footer.html &>
+<SCRIPT TYPE="text/javascript">
+
+$(document).ready(function (){
+    validate();
+    $('<% $validate_select_fields %>').change(validate);
+    $('<% $validate_input_fields %>').keyup(validate);
+});
+
+function validate(){
+    if (
+      $('#amount').val() > 0 && (
+        ( $('#custpaybynum').val() > 0 ) ||
+% if ($payby eq "CHEK") {
+        ( $('input[name=payinfo1]').val().length > 0 &&
+          $('input[name=payinfo2]').val().length > 0 &&
+          $('input[name=payname]').val().length > 0  &&
+          $('select[name=paytype]').val().length > 0
+        )
+% }
+% elsif ($payby eq "CARD") {
+        ( $('input[name=payinfo]').val().length > 0 &&
+          $('input[name=paycvv]').val().length > 0 &&
+          $('input[name=payname]').val().length > 0 &&
+          $('#city').val().length > 0 &&
+          $('#city').val().length > 0 &&
+          $('#state').val().length > 0 &&
+          $('#country').val().length > 0
+        )
+% }
+      )
+     ) {
+        $("#process").prop("disabled", false);
+    }
+    else {
+        $("#process").prop("disabled", true);
+    }
+}
+
+</SCRIPT>
+
+<& /elements/footer-cust_main.html &>
+<%once>
+
+my %weight = (
+  1 => 'Primary',
+  2 => 'Secondary',
+  3 => 'Tertiary',
+  4 => 'Fourth',
+  5 => 'Fifth',
+  6 => 'Sixth',
+  7 => 'Seventh',
+);
+
+</%once>
 <%init>
 
 die "access denied"
@@ -291,6 +219,17 @@ $cgi->param('payby') =~ /^(CARD|CHEK)$/
   or die "unknown payby ". $cgi->param('payby');
 my $payby = $1;
 
+my $validate_select_fields = "#payment_option, #invoice, #custpaybynum, ";
+my $validate_input_fields  = "#amount, input[name=payname], ";
+if ($payby eq "CHEK") {
+  $validate_input_fields  .= "input[name=payinfo1], input[name=payinfo2]";
+  $validate_select_fields .= "select[name=paytype] ";
+}
+elsif ($payby eq "CARD") {
+  $validate_input_fields  .= "input[name=payinfo], input[name=paycvv], input[name=address1], #city, #zip";
+  $validate_select_fields .= "#state, #country ";
+}
+
 $cgi->param('custnum') =~ /^(\d+)$/
   or die "illegal custnum ". $cgi->param('custnum');
 my $custnum = $1;
@@ -304,53 +243,6 @@ my $payinfo = '';
 
 my $conf = new FS::Conf;
 
-my $money_char = $conf->config('money_char') || '$';
-
-#false laziness w/selfservice make_payment.html shortcut for one-country
-my %states = map { $_->state => 1 }
-               qsearch('cust_main_county', {
-                 'country' => $conf->config('countrydefault') || 'US'
-               } );
-my @states = sort { $a cmp $b } keys %states;
-
-my $fee = '';
-my $fee_pkg = '';
-my $fee_display = '';
-my $fee_op = '';
-my $num_payments = scalar($cust_main->cust_pay);
-#handle old cust_main.pm (remove...)
-$num_payments = scalar( @{ [ $cust_main->cust_pay ] } )
-  unless defined $num_payments;
-if ( $conf->config('manual_process-pkgpart')
-     and ! $conf->exists('manual_process-skip_first') || $num_payments
-   )
-{
-
-  $fee_display = $conf->config('manual_process-display') || 'add';
-  $fee_op = $fee_display eq 'add' ? '+' : '-';
-
-  $fee_pkg =
-    qsearchs('part_pkg', { pkgpart=>$conf->config('manual_process-pkgpart') } );
-
-  #well ->unit_setup or ->calc_setup both call for a $cust_pkg
-  # (though ->unit_setup doesn't use it...)
-  $fee = $fee_pkg->option('setup_fee')
-    if $fee_pkg; #in case.. better than dying with a perl traceback
-
-}
-
-my $amount = '';
-if ( $balance > 0 ) {
-  $amount = $balance;
-  $amount += $fee
-    if $fee && $fee_display eq 'subtract';
-
-  my $cc_surcharge_pct = $conf->config('credit-card-surcharge-percentage');
-  $amount += $amount * $cc_surcharge_pct/100 if $cc_surcharge_pct > 0;
-
-  $amount = sprintf("%.2f", $amount);
-}
-
 my $payunique = "webui-payment-". time. "-$$-". rand() * 2**32;
 
 </%init>