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');
22 my $conf = new FS::Conf;
28 $cgi->param('tax','') unless defined $cgi->param('tax');
30 $cgi->param('refnum', (split(/:/, ($cgi->param('refnum'))[0] ))[0] );
32 my $payby = $cgi->param('payby');
38 $payby = $noauto{$payby}
39 if ! $cgi->param('payauto') && exists $noauto{$payby};
41 $cgi->param('payby', $payby);
44 if ( $payby eq 'CHEK' || $payby eq 'DCHK' ) {
45 my $payinfo = $cgi->param('payinfo1'). '@';
46 $payinfo .= $cgi->param('payinfo3').'.'
47 if $conf->config('echeck-country') eq 'CA';
48 $payinfo .= $cgi->param('payinfo2');
49 $cgi->param('payinfo',$payinfo);
51 $cgi->param('paydate',
52 $cgi->param( 'exp_month' ). '-'. $cgi->param( 'exp_year' ) );
55 my @invoicing_list = split( /\s*\,\s*/, $cgi->param('invoicing_list') );
56 push @invoicing_list, 'POST' if $cgi->param('invoicing_list_POST');
57 push @invoicing_list, 'FAX' if $cgi->param('invoicing_list_FAX');
58 $cgi->param('invoicing_list', join(',', @invoicing_list) );
60 # is this actually used? if so, we need to clone locations...
61 # but I can't find anything that sets this parameter to a non-empty value
62 $cgi->param('duplicate_of_custnum') =~ /^(\d+)$/;
63 my $duplicate_of = $1;
66 for my $pre (qw(bill ship)) {
69 foreach ( FS::cust_main->location_fields ) {
70 $hash{$_} = scalar($cgi->param($pre.'_'.$_));
72 $hash{'custnum'} = $cgi->param('custnum');
73 warn Dumper \%hash if $DEBUG;
74 # if we can qsearchs it, then it's unchanged, so use that
75 $locations{$pre} = qsearchs('cust_location', \%hash)
76 || FS::cust_location->new( \%hash );
80 if ( ($cgi->param('same') || '') eq 'Y' ) {
81 $locations{ship} = $locations{bill};
84 #create new record object
85 # but explicitly avoid setting ship_ fields
87 my $new = new FS::cust_main ( {
88 map { ( $_, scalar($cgi->param($_)) ) } (fields('cust_main')),
89 map { ( "ship_$_", '' ) } (FS::cust_main->location_fields)
92 $new->invoice_noemail( ($cgi->param('invoice_email') eq 'Y') ? '' : 'Y' );
94 if ( $duplicate_of ) {
95 # then negate all changes to the customer; the only change we should
96 # make is to order a package, if requested
97 $new = qsearchs('cust_main', { 'custnum' => $duplicate_of })
98 # this should never happen
99 or die "nonexistent existing customer (custnum $duplicate_of)";
102 for my $pre (qw(bill ship)) {
103 $new->set($pre.'_location', $locations{$pre});
104 $new->set($pre.'_locationnum', $locations{$pre}->locationnum);
107 if ( $cgi->param('no_credit_limit') ) {
108 $new->setfield('credit_limit', '');
111 $new->tagnum( [ $cgi->param('tagnum') ] );
113 $error ||= $new->set_national_id_from_cgi( $cgi );
115 my %usedatetime = ( 'birthdate' => 1,
116 'spouse_birthdate' => 1,
117 'anniversary_date' => 1,
120 foreach my $dfield (qw(
121 signupdate birthdate spouse_birthdate anniversary_date
124 if ( $cgi->param($dfield) && $cgi->param($dfield) =~ /^([ 0-9\-\/]{0,10})$/) {
129 if ( exists $usedatetime{$dfield} && $usedatetime{$dfield} ) {
131 my $format = $conf->config('date_format') || "%m/%d/%Y";
132 my $parser = DateTime::Format::Strptime->new( pattern => $format,
133 time_zone => 'floating',
135 my $dt = $parser->parse_datetime($value);
137 $parsed = $dt->epoch;
139 $error ||= "Invalid $dfield: $value";
144 $parsed = parse_datetime($value)
145 or $error ||= "Invalid $dfield: $value";
149 $new->setfield( $dfield, $parsed );
150 $cgi->param( $dfield, $parsed );
156 $new->setfield('paid', $cgi->param('paid') )
157 if $cgi->param('paid');
159 my @exempt_groups = grep /\S/, $conf->config('tax-cust_exempt-groups');
160 my @tax_exempt = grep { $cgi->param("tax_$_") eq 'Y' } @exempt_groups;
161 my %tax_exempt = map { $_ => scalar($cgi->param("tax_$_".'_num')) } @tax_exempt;
163 #perhaps this stuff should go to cust_main.pm
164 if ( $new->custnum eq '' or $duplicate_of ) {
169 if ( $cgi->param('pkgpart_svcpart') ) {
171 my $x = $cgi->param('pkgpart_svcpart');
172 $x =~ /^(\d+)_(\d+)$/ or die "illegal pkgpart_svcpart $x\n";
173 my($pkgpart, $svcpart) = ($1, $2);
174 my $part_pkg = qsearchs('part_pkg', { 'pkgpart' => $pkgpart } );
175 #false laziness: copied from FS::cust_pkg::order (which should become a
176 #FS::cust_main method)
179 # $part_pkg{$pkgpart} is true iff $custnum may purchase $pkgpart
180 my $agent = qsearchs('agent',{'agentnum'=> $new->agentnum });
183 # $pkgpart_href->{PKGPART} is true iff $custnum may purchase $pkgpart
184 my $pkgpart_href = $agent->pkgpart_hashref
188 # this should wind up in FS::cust_pkg!
189 $error ||= "Agent ". $new->agentnum. " (type ". $agent->typenum.
190 ") can't purchase pkgpart ". $pkgpart
191 #unless $part_pkg{ $pkgpart };
192 unless $pkgpart_href->{ $pkgpart }
193 || $agent->agentnum == $part_pkg->agentnum;
195 $error = 'Select agent';
198 $cust_pkg = new FS::cust_pkg ( {
199 #later 'custnum' => $custnum,
200 'pkgpart' => $pkgpart,
201 'locationnum' => scalar($cgi->param('locationnum')),
205 my $part_svc = qsearchs('part_svc', { 'svcpart' => $svcpart } );
206 my $svcdb = $part_svc->svcdb;
208 if ( $svcdb eq 'svc_acct' ) {
211 'svcpart' => $svcpart,
212 'username' => scalar($cgi->param('username')),
213 '_password' => scalar($cgi->param('_password')),
214 'popnum' => scalar($cgi->param('popnum')),
216 $svc_acct{'domsvc'} = $cgi->param('domsvc')
217 if $cgi->param('domsvc');
219 $svc = new FS::svc_acct \%svc_acct;
221 #and just in case you were silly
222 $svc->svcpart($svcpart);
223 $svc->username($cgi->param('username'));
224 $svc->_password($cgi->param('_password'));
225 $svc->popnum($cgi->param('popnum'));
227 } elsif ( $svcdb eq 'svc_phone' ) {
230 'svcpart' => $svcpart,
231 map { $_ => scalar($cgi->param($_)) }
232 qw( countrycode phonenum sip_password pin phone_name )
235 $svc = new FS::svc_phone \%svc_phone;
237 } elsif ( $svcdb eq 'svc_dsl' ) {
240 'svcpart' => $svcpart,
241 ( map { $_ => scalar($cgi->param("ship_$_")) || scalar($cgi->param($_))}
242 qw( first last company )
244 ( map { $_ => scalar($cgi->param($_)) }
245 qw( loop_type phonenum password isp_chg isp_prev vendor_qual_id )
247 'desired_due_date' => time, #XXX enter?
248 'vendor_order_type' => 'NEW',
250 $svc = new FS::svc_dsl \%svc_dsl;
253 die "$svcdb not handled on new customer yet";
260 tie my %hash, 'Tie::RefHash';
261 %hash = ( $cust_pkg => [ $svc ] ) if $cust_pkg;
262 if ( $duplicate_of ) {
263 # order the package and service normally
264 $error ||= $new->order_pkgs( \%hash ) if $cust_pkg;
267 # create the customer
268 $error ||= $new->insert( \%hash, \@invoicing_list,
269 'tax_exemption'=> \%tax_exempt,
270 'prospectnum' => scalar($cgi->param('prospectnum')),
273 my $conf = new FS::Conf;
274 if ( $conf->exists('backend-realtime') && ! $error ) {
276 my $berror = $new->bill
277 || $new->apply_payments_and_credits
278 || $new->collect( 'realtime' => 1 );
279 warn "Warning, error billing during backend-realtime: $berror" if $berror;
284 } else { #create old record object
286 my $old = qsearchs( 'cust_main', { 'custnum' => $new->custnum } );
287 $error ||= "Old record not found!" unless $old;
289 if ( length($old->paycvv) && $new->paycvv =~ /^\s*\*+\s*$/ ) {
290 $new->paycvv($old->paycvv);
292 if ($new->ss =~ /xx/) {
295 if ($new->stateid =~ /^xxx/) {
296 $new->stateid($old->stateid);
298 if ( $new->payby =~ /^(CARD|DCRD)$/
299 && ( $new->payinfo =~ /xx/
300 || $new->payinfo =~ /^\s*N\/A\s+\(tokenized\)\s*$/
304 $new->payinfo($old->payinfo);
306 } elsif ( $new->payby =~ /^(CHEK|DCHK)$/ && $new->payinfo =~ /xx/ ) {
307 #fix for #3085 "edit of customer's routing code only surprisingly causes
308 #nothing to happen...
309 # this probably won't do the right thing when we don't have the
310 # public key (can't actually get the real $old->payinfo)
311 my($new_account, $new_aba) = split('@', $new->payinfo);
312 my($old_account, $old_aba) = split('@', $old->payinfo);
313 $new_account = $old_account if $new_account =~ /xx/;
314 $new_aba = $old_aba if $new_aba =~ /xx/;
315 $new->payinfo($new_account.'@'.$new_aba);
318 if ( ! $conf->exists('cust_main-edit_signupdate') or
319 ! $new->signupdate ) {
320 $new->signupdate($old->signupdate);
323 warn "$me calling $new -> replace( $old, \ @invoicing_list )" if $DEBUG;
324 local($FS::cust_main::DEBUG) = $DEBUG if $DEBUG;
325 local($FS::Record::DEBUG) = $DEBUG if $DEBUG;
327 local($Data::Dumper::Sortkeys) = 1;
328 warn Dumper({ new => $new, old => $old }) if $DEBUG;
330 $error ||= $new->replace( $old, \@invoicing_list,
331 'tax_exemption' => \%tax_exempt,
334 warn "$me returned from replace" if $DEBUG;
338 unless ( $error ) { #XXX i guess i should be transactional... all in the insert
340 my @contact_fields = qw( classnum first last title comment emailaddress );
341 foreach my $phone_type ( qsearch({table=>'phone_type', order_by=>'weight'}) ) {
342 push @contact_fields, 'phonetypenum'.$phone_type->phonetypenum;
345 $error = $new->process_o2m( 'table' => 'contact',
346 'fields' => \@contact_fields,
347 'params' => scalar($cgi->Vars),