add some debugging control
[freeside.git] / httemplate / edit / process / cust_main.cgi
index 4ba30c4..8de2092 100755 (executable)
@@ -1,4 +1,20 @@
-<%
+% 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 = '';
 
@@ -46,6 +62,9 @@ my $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
@@ -53,6 +72,23 @@ 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');
 
@@ -96,12 +132,16 @@ if ( $new->custnum eq '' ) {
 
     #$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;
 
     #and just in case you were silly
     $svc_acct->svcpart($svcpart);
@@ -123,10 +163,9 @@ if ( $new->custnum eq '' ) {
   my $conf = new FS::Conf;
   if ( $conf->exists('backend-realtime') && ! $error ) {
 
-    my $berror = $new->bill;
-    $new->apply_payments;
-    $new->apply_credits;
-    $berror ||= $new->collect;
+    my $berror =    $new->bill
+                 || $new->apply_payments_and_credits
+                 || $new->collect( 'realtime' => 1 );
     warn "Warning, error billing during backend-realtime: $berror" if $berror;
 
   }
@@ -135,19 +174,27 @@ if ( $new->custnum eq '' ) {
 
   my $old = qsearchs( 'cust_main', { 'custnum' => $new->custnum } ); 
   $error ||= "Old record not found!" unless $old;
-  if ( defined dbdef->table('cust_main')->column('paycvv')
-       && length($old->paycvv)
-       && $new->paycvv =~ /^\s*\*+\s*$/ ) {
+  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 { 
-  print $cgi->redirect(popurl(3). "view/cust_main.cgi?". $new->custnum);
-} 
-%>
+</%init>