add some debugging control
[freeside.git] / httemplate / edit / process / cust_main.cgi
index 625a850..8de2092 100755 (executable)
@@ -1,22 +1,22 @@
-<%
-# $Id: cust_main.cgi,v 1.5 2001-10-20 12:18:00 ivan Exp $
-
-use strict;
-use vars qw( $cgi $payby @invoicing_list $new $custnum $error );
-use vars qw( $cust_pkg $cust_svc $svc_acct );
-use CGI;
-use CGI::Carp qw(fatalsToBrowser);
-use FS::UID qw(cgisuidsetup getotaker);
-use FS::CGI qw( popurl );
-use FS::Record qw( qsearch qsearchs fields );
-use FS::cust_main;
-use FS::type_pkgs;
-use FS::agent;
-
-$cgi = new CGI;
-&cgisuidsetup($cgi);
-
-$error = '';
+% if ( $error ) {
+%   $cgi->param('error', $error);
+%
+<% $cgi->redirect(popurl(2). "cust_main.cgi?". $cgi->query_string ) %>
+%
+% } else { 
+%
+<% $cgi->redirect(popurl(3). "view/cust_main.cgi?". $new->custnum) %>
+%
+% }
+<%once>
+
+my $me = '[edit/process/cust_main.cgi]';
+my $DEBUG = 0;
+
+</%once>
+<%init>
+
+my $error = '';
 
 #unmunge stuff
 
@@ -24,34 +24,36 @@ $cgi->param('tax','') unless defined $cgi->param('tax');
 
 $cgi->param('refnum', (split(/:/, ($cgi->param('refnum'))[0] ))[0] );
 
-$cgi->param('state') =~ /^(\w*)( \(([\w ]+)\))? ?\/ ?(\w+)$/
-  or die "Oops, illegal \"state\" param: ". $cgi->param('state');
-$cgi->param('state', $1);
-$cgi->param('county', $3 || '');
-$cgi->param('country', $4);
-
-$cgi->param('ship_state') =~ /^(\w*)( \(([\w ]+)\))? ?\/ ?(\w+)$/
-  or $cgi->param('ship_state') =~ /^(((())))$/
-  or die "Oops, illegal \"ship_state\" param: ". $cgi->param('ship_state');
-$cgi->param('ship_state', $1);
-$cgi->param('ship_county', $3 || '');
-$cgi->param('ship_country', $4);
-
-if ( $payby = $cgi->param('payby') ) {
-  $cgi->param('payinfo', $cgi->param( $payby. '_payinfo' ) );
+#my $payby = $cgi->param('payby');
+my $payby = $cgi->param('select'); # XXX key
+
+my %noauto = (
+  'CARD' => 'DCRD',
+  'CHEK' => 'DCHK',
+);
+$payby = $noauto{$payby}
+  if ! $cgi->param('payauto') && exists $noauto{$payby};
+
+$cgi->param('payby', $payby);
+
+if ( $payby ) {
+  if ( $payby eq 'CHEK' || $payby eq 'DCHK' ) {
+    $cgi->param('payinfo',
+      $cgi->param('payinfo1'). '@'. $cgi->param('payinfo2') );
+  }
   $cgi->param('paydate',
-  $cgi->param( $payby. '_month' ). '-'. $cgi->param( $payby. '_year' ) );
-  $cgi->param('payname', $cgi->param( $payby. '_payname' ) );
+    $cgi->param( 'exp_month' ). '-'. $cgi->param( 'exp_year' ) );
 }
 
-$cgi->param('otaker', &getotaker );
-
-@invoicing_list = split( /\s*\,\s*/, $cgi->param('invoicing_list') );
+my @invoicing_list = split( /\s*\,\s*/, $cgi->param('invoicing_list') );
 push @invoicing_list, 'POST' if $cgi->param('invoicing_list_POST');
+push @invoicing_list, 'FAX' if $cgi->param('invoicing_list_FAX');
+$cgi->param('invoicing_list', join(',', @invoicing_list) );
+
 
 #create new record object
 
-$new = new FS::cust_main ( {
+my $new = new FS::cust_main ( {
   map {
     $_, scalar($cgi->param($_))
 #  } qw(custnum agentnum last first ss company address1 address2 city county
@@ -60,6 +62,9 @@ $new = new FS::cust_main ( {
   } fields('cust_main')
 } );
 
+ delete( $new->hashref->{'agent_custid'} )
+   unless $new->hashref->{'agent_custid'};
+
 if ( defined($cgi->param('same')) && $cgi->param('same') eq "Y" ) {
   $new->setfield("ship_$_", '') foreach qw(
     last first company address1 address2 city county state zip
@@ -67,14 +72,34 @@ if ( defined($cgi->param('same')) && $cgi->param('same') eq "Y" ) {
   );
 }
 
+if ( $cgi->param('birthdate') && $cgi->param('birthdate') =~ /^([ 0-9\-\/]{0,10})$/) {
+  my $conf = new FS::Conf;
+  my $format = $conf->config('date_format') || "%m/%d/%Y";
+  my $parser = DateTime::Format::Strptime->new(pattern => $format,
+                                               time_zone => 'floating',
+                                              );
+  my $dt =  $parser->parse_datetime($1);
+  if ($dt) {
+    $new->setfield('birthdate', $dt->epoch);
+    $cgi->param('birthdate', $dt->epoch);
+  } else {
+#    $error ||= $cgi->param('birthdate') . " is an invalid birthdate:" . $parser->errmsg;
+    $error ||= "Invalid birthdate: " . $cgi->param('birthdate') . ".";
+    $cgi->param('birthdate', '');
+  }
+}
+
+$new->setfield('paid', $cgi->param('paid') )
+  if $cgi->param('paid');
+
 #perhaps this stuff should go to cust_main.pm
-$cust_pkg = '';
-$svc_acct = '';
+my $cust_pkg = '';
+my $svc_acct = '';
 if ( $new->custnum eq '' ) {
 
   if ( $cgi->param('pkgpart_svcpart') ) {
     my $x = $cgi->param('pkgpart_svcpart');
-    $x =~ /^(\d+)_(\d+)$/;
+    $x =~ /^(\d+)_(\d+)$/ or die "illegal pkgpart_svcpart $x\n";
     my($pkgpart, $svcpart) = ($1, $2);
     #false laziness: copied from FS::cust_pkg::order (which should become a
     #FS::cust_main method)
@@ -92,7 +117,7 @@ if ( $new->custnum eq '' ) {
     #eslaf
 
     # this should wind up in FS::cust_pkg!
-    $error ||= "Agent ". $new->agentnum. " (type ". $agent->typenum. ") can't".
+    $error ||= "Agent ". $new->agentnum. " (type ". $agent->typenum. ") can't ".
                "purchase pkgpart ". $pkgpart
       #unless $part_pkg{ $pkgpart };
       unless $pkgpart_href->{ $pkgpart };
@@ -101,28 +126,30 @@ if ( $new->custnum eq '' ) {
       #later         'custnum' => $custnum,
       'pkgpart' => $pkgpart,
     } );
-    $error ||= $cust_pkg->check;
+    #$error ||= $cust_pkg->check;
 
     #$cust_svc = new FS::cust_svc ( { 'svcpart' => $svcpart } );
 
     #$error ||= $cust_svc->check;
 
-    $svc_acct = new FS::svc_acct ( {
-                                     'svcpart'   => $svcpart,
-                                     'username'  => $cgi->param('username'),
-                                     '_password' => $cgi->param('_password'),
-                                     'popnum'    => $cgi->param('popnum'),
-                                   } );
+    my %svc_acct = (
+                     'svcpart'   => $svcpart,
+                     'username'  => $cgi->param('username'),
+                     '_password' => $cgi->param('_password'),
+                     'popnum'    => $cgi->param('popnum'),
+                   );
+    $svc_acct{'domsvc'} = $cgi->param('domsvc')
+      if $cgi->param('domsvc');
+
+    $svc_acct = new FS::svc_acct \%svc_acct;
 
-    my $y = $svc_acct->setdefault; # arguably should be in new method
-    $error ||= $y unless ref($y);
     #and just in case you were silly
     $svc_acct->svcpart($svcpart);
     $svc_acct->username($cgi->param('username'));
     $svc_acct->_password($cgi->param('_password'));
     $svc_acct->popnum($cgi->param('popnum'));
 
-    $error ||= $svc_acct->check;
+    #$error ||= $svc_acct->check;
 
   } elsif ( $cgi->param('username') ) { #good thing to catch
     $error = "Can't assign username without a package!";
@@ -132,17 +159,42 @@ if ( $new->custnum eq '' ) {
   tie my %hash, 'Tie::RefHash';
   %hash = ( $cust_pkg => [ $svc_acct ] ) if $cust_pkg;
   $error ||= $new->insert( \%hash, \@invoicing_list );
+
+  my $conf = new FS::Conf;
+  if ( $conf->exists('backend-realtime') && ! $error ) {
+
+    my $berror =    $new->bill
+                 || $new->apply_payments_and_credits
+                 || $new->collect( 'realtime' => 1 );
+    warn "Warning, error billing during backend-realtime: $berror" if $berror;
+
+  }
+  
 } else { #create old record object
+
   my $old = qsearchs( 'cust_main', { 'custnum' => $new->custnum } ); 
   $error ||= "Old record not found!" unless $old;
+  if ( length($old->paycvv) && $new->paycvv =~ /^\s*\*+\s*$/ ) {
+    $new->paycvv($old->paycvv);
+  }
+  if ($new->ss =~ /xx/) {
+    $new->ss($old->ss);
+  }
+  if ($new->stateid =~ /^xxx/) {
+    $new->stateid($old->stateid);
+  }
+  if ($new->payby =~ /^(CARD|DCRD|CHEK|DCHK)$/ && $new->payinfo =~ /xx/) {
+    $new->payinfo($old->payinfo);
+  }
+
+  warn "$me calling $new -> replace( $old, \ @invoicing_list )" if $DEBUG;
+  local($FS::cust_main::DEBUG) = $DEBUG if $DEBUG;
+  local($FS::Record::DEBUG)    = $DEBUG if $DEBUG;
+
   $error ||= $new->replace($old, \@invoicing_list);
+
+  warn "$me returned from replace" if $DEBUG;
+  
 }
 
-if ( $error ) {
-  $cgi->param('error', $error);
-  print $cgi->redirect(popurl(2). "cust_main.cgi?". $cgi->query_string );
-} else { 
-  $custnum = $new->custnum;
-  print $cgi->redirect(popurl(3). "view/cust_main.cgi?$custnum");
-} 
-%>
+</%init>