2 % $cgi->param('error', $error);
4 <% $cgi->redirect(popurl(2). "cust_main.cgi?". $cgi->query_string ) %>
8 <% $cgi->redirect(popurl(3). "view/cust_main.cgi?". $new->custnum) %>
13 my $me = '[edit/process/cust_main.cgi]';
20 unless $FS::CurrentUser::CurrentUser->access_right('Edit customer');
26 $cgi->param('tax','') unless defined $cgi->param('tax');
28 $cgi->param('refnum', (split(/:/, ($cgi->param('refnum'))[0] ))[0] );
30 #my $payby = $cgi->param('payby');
31 my $payby = $cgi->param('select'); # XXX key
37 $payby = $noauto{$payby}
38 if ! $cgi->param('payauto') && exists $noauto{$payby};
40 $cgi->param('payby', $payby);
43 if ( $payby eq 'CHEK' || $payby eq 'DCHK' ) {
44 $cgi->param('payinfo',
45 $cgi->param('payinfo1'). '@'. $cgi->param('payinfo2') );
47 $cgi->param('paydate',
48 $cgi->param( 'exp_month' ). '-'. $cgi->param( 'exp_year' ) );
51 my @invoicing_list = split( /\s*\,\s*/, $cgi->param('invoicing_list') );
52 push @invoicing_list, 'POST' if $cgi->param('invoicing_list_POST');
53 push @invoicing_list, 'FAX' if $cgi->param('invoicing_list_FAX');
54 $cgi->param('invoicing_list', join(',', @invoicing_list) );
57 #create new record object
59 my $new = new FS::cust_main ( {
61 $_, scalar($cgi->param($_))
62 # } qw(custnum agentnum last first ss company address1 address2 city county
63 # state zip daytime night fax payby payinfo paydate payname tax
68 if ( defined($cgi->param('same')) && $cgi->param('same') eq "Y" ) {
69 $new->setfield("ship_$_", '') foreach qw(
70 last first company address1 address2 city county state zip
71 country daytime night fax
75 if ( $cgi->param('birthdate') && $cgi->param('birthdate') =~ /^([ 0-9\-\/]{0,10})$/) {
76 my $conf = new FS::Conf;
77 my $format = $conf->config('date_format') || "%m/%d/%Y";
78 my $parser = DateTime::Format::Strptime->new(pattern => $format,
79 time_zone => 'floating',
81 my $dt = $parser->parse_datetime($1);
83 $new->setfield('birthdate', $dt->epoch);
84 $cgi->param('birthdate', $dt->epoch);
86 # $error ||= $cgi->param('birthdate') . " is an invalid birthdate:" . $parser->errmsg;
87 $error ||= "Invalid birthdate: " . $cgi->param('birthdate') . ".";
88 $cgi->param('birthdate', '');
92 $new->setfield('paid', $cgi->param('paid') )
93 if $cgi->param('paid');
95 #perhaps this stuff should go to cust_main.pm
98 if ( $new->custnum eq '' ) {
100 if ( $cgi->param('pkgpart_svcpart') ) {
101 my $x = $cgi->param('pkgpart_svcpart');
102 $x =~ /^(\d+)_(\d+)$/ or die "illegal pkgpart_svcpart $x\n";
103 my($pkgpart, $svcpart) = ($1, $2);
104 #false laziness: copied from FS::cust_pkg::order (which should become a
105 #FS::cust_main method)
108 # $part_pkg{$pkgpart} is true iff $custnum may purchase $pkgpart
109 my $agent = qsearchs('agent',{'agentnum'=> $new->agentnum });
111 #foreach $type_pkgs ( qsearch('type_pkgs',{'typenum'=> $agent->typenum }) ) {
112 # my($pkgpart)=$type_pkgs->pkgpart;
113 # $part_pkg{$pkgpart}++;
115 # $pkgpart_href->{PKGPART} is true iff $custnum may purchase $pkgpart
116 my $pkgpart_href = $agent->pkgpart_hashref;
119 # this should wind up in FS::cust_pkg!
120 $error ||= "Agent ". $new->agentnum. " (type ". $agent->typenum. ") can't ".
121 "purchase pkgpart ". $pkgpart
122 #unless $part_pkg{ $pkgpart };
123 unless $pkgpart_href->{ $pkgpart };
125 $cust_pkg = new FS::cust_pkg ( {
126 #later 'custnum' => $custnum,
127 'pkgpart' => $pkgpart,
129 #$error ||= $cust_pkg->check;
131 #$cust_svc = new FS::cust_svc ( { 'svcpart' => $svcpart } );
133 #$error ||= $cust_svc->check;
136 'svcpart' => $svcpart,
137 'username' => $cgi->param('username'),
138 '_password' => $cgi->param('_password'),
139 'popnum' => $cgi->param('popnum'),
141 $svc_acct{'domsvc'} = $cgi->param('domsvc')
142 if $cgi->param('domsvc');
144 $svc_acct = new FS::svc_acct \%svc_acct;
146 #and just in case you were silly
147 $svc_acct->svcpart($svcpart);
148 $svc_acct->username($cgi->param('username'));
149 $svc_acct->_password($cgi->param('_password'));
150 $svc_acct->popnum($cgi->param('popnum'));
152 #$error ||= $svc_acct->check;
154 } elsif ( $cgi->param('username') ) { #good thing to catch
155 $error = "Can't assign username without a package!";
159 tie my %hash, 'Tie::RefHash';
160 %hash = ( $cust_pkg => [ $svc_acct ] ) if $cust_pkg;
161 $error ||= $new->insert( \%hash, \@invoicing_list );
163 my $conf = new FS::Conf;
164 if ( $conf->exists('backend-realtime') && ! $error ) {
166 my $berror = $new->bill
167 || $new->apply_payments_and_credits
168 || $new->collect( 'realtime' => 1 );
169 warn "Warning, error billing during backend-realtime: $berror" if $berror;
173 } else { #create old record object
175 my $old = qsearchs( 'cust_main', { 'custnum' => $new->custnum } );
176 $error ||= "Old record not found!" unless $old;
177 if ( length($old->paycvv) && $new->paycvv =~ /^\s*\*+\s*$/ ) {
178 $new->paycvv($old->paycvv);
180 if ($new->ss =~ /xx/) {
183 if ($new->stateid =~ /^xxx/) {
184 $new->stateid($old->stateid);
186 if ($new->payby =~ /^(CARD|DCRD)$/ && $new->payinfo =~ /xx/) {
187 $new->payinfo($old->payinfo);
188 } elsif ($new->payby =~ /^(CHEK|DCHK)$/ && $new->payinfo =~ /xx/) {
189 #fix for #3085 "edit of customer's routing code only surprisingly causes
190 #nothing to happen...
191 # this probably won't do the right thing when we don't have the
192 # public key (can't actually get the real $old->payinfo)
193 my($new_account, $new_aba) = split('@', $new->payinfo);
194 my($old_account, $old_aba) = split('@', $old->payinfo);
195 $new_account = $old_account if $new_account =~ /xx/;
196 $new_aba = $old_aba if $new_aba =~ /xx/;
197 $new->payinfo($new_account.'@'.$new_aba);
200 warn "$me calling $new -> replace( $old, \ @invoicing_list )" if $DEBUG;
201 local($FS::cust_main::DEBUG) = $DEBUG if $DEBUG;
202 local($FS::Record::DEBUG) = $DEBUG if $DEBUG;
204 $error ||= $new->replace($old, \@invoicing_list);
206 warn "$me returned from replace" if $DEBUG;