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]';
19 my $curuser = $FS::CurrentUser::CurrentUser;
20 die "access denied" unless $curuser->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;
65 # if this is enabled, enforce it
66 if ( $conf->exists('agent-ship_address', $cgi->param('agentnum')) ) {
67 my $agent = FS::agent->by_key($cgi->param('agentnum'));
68 my $agent_cust_main = $agent->agent_cust_main;
69 if ( $agent_cust_main ) {
70 my $agent_location = $agent_cust_main->ship_location;
71 foreach (qw(address1 city state zip country latitude longitude district)) {
72 $cgi->param("ship_$_", $agent_location->get($_));
78 for my $pre (qw(bill ship)) {
81 foreach ( FS::cust_main->location_fields ) {
82 $hash{$_} = scalar($cgi->param($pre.'_'.$_));
84 $hash{'custnum'} = $cgi->param('custnum');
85 warn Dumper \%hash if $DEBUG;
86 # if we can qsearchs it, then it's unchanged, so use that
87 $locations{$pre} = qsearchs('cust_location', \%hash)
88 || FS::cust_location->new( \%hash );
92 if ( ($cgi->param('same') || '') eq 'Y' ) {
93 $locations{ship} = $locations{bill};
96 #create new record object
97 # but explicitly avoid setting ship_ fields
99 my $new = new FS::cust_main ( {
100 map { ( $_, scalar($cgi->param($_)) ) } (fields('cust_main')),
101 map { ( "ship_$_", '' ) } (FS::cust_main->location_fields)
104 $new->invoice_noemail( ($cgi->param('invoice_email') eq 'Y') ? '' : 'Y' );
106 if ( $duplicate_of ) {
107 # then negate all changes to the customer; the only change we should
108 # make is to order a package, if requested
109 $new = qsearchs('cust_main', { 'custnum' => $duplicate_of })
110 # this should never happen
111 or die "nonexistent existing customer (custnum $duplicate_of)";
114 for my $pre (qw(bill ship)) {
115 $new->set($pre.'_location', $locations{$pre});
116 $new->set($pre.'_locationnum', $locations{$pre}->locationnum);
119 if ( $cgi->param('no_credit_limit') ) {
120 $new->setfield('credit_limit', '');
123 $new->tagnum( [ $cgi->param('tagnum') ] );
125 $error ||= $new->set_national_id_from_cgi( $cgi );
127 my %usedatetime = ( 'birthdate' => 1,
128 'spouse_birthdate' => 1,
129 'anniversary_date' => 1,
132 foreach my $dfield (qw(
133 signupdate birthdate spouse_birthdate anniversary_date
136 if ( $cgi->param($dfield) && $cgi->param($dfield) =~ /^([ 0-9\-\/]{0,10})$/) {
141 if ( exists $usedatetime{$dfield} && $usedatetime{$dfield} ) {
143 my $format = $conf->config('date_format') || "%m/%d/%Y";
144 my $parser = DateTime::Format::Strptime->new( pattern => $format,
145 time_zone => 'floating',
147 my $dt = $parser->parse_datetime($value);
149 $parsed = $dt->epoch;
151 $error ||= "Invalid $dfield: $value";
156 $parsed = parse_datetime($value)
157 or $error ||= "Invalid $dfield: $value";
161 $new->setfield( $dfield, $parsed );
162 $cgi->param( $dfield, $parsed );
168 $new->setfield('paid', $cgi->param('paid') )
169 if $cgi->param('paid');
172 if ( $curuser->access_right('Edit customer tax exemptions') ) {
173 my @exempt_groups = grep /\S/, $conf->config('tax-cust_exempt-groups');
174 my @tax_exempt = grep { $cgi->param("tax_$_") eq 'Y' } @exempt_groups;
175 $options{'tax_exemption'} = {
176 map { $_ => scalar($cgi->param("tax_$_".'_num')) } @tax_exempt
180 #perhaps this stuff should go to cust_main.pm
181 if ( $new->custnum eq '' or $duplicate_of ) {
186 if ( $cgi->param('pkgpart_svcpart') ) {
188 my $x = $cgi->param('pkgpart_svcpart');
189 $x =~ /^(\d+)_(\d+)$/ or die "illegal pkgpart_svcpart $x\n";
190 my($pkgpart, $svcpart) = ($1, $2);
191 my $part_pkg = qsearchs('part_pkg', { 'pkgpart' => $pkgpart } );
192 #false laziness: copied from FS::cust_pkg::order (which should become a
193 #FS::cust_main method)
196 # $part_pkg{$pkgpart} is true iff $custnum may purchase $pkgpart
197 my $agent = qsearchs('agent',{'agentnum'=> $new->agentnum });
200 # $pkgpart_href->{PKGPART} is true iff $custnum may purchase $pkgpart
201 my $pkgpart_href = $agent->pkgpart_hashref
205 # this should wind up in FS::cust_pkg!
206 $error ||= "Agent ". $new->agentnum. " (type ". $agent->typenum.
207 ") can't purchase pkgpart ". $pkgpart
208 #unless $part_pkg{ $pkgpart };
209 unless $pkgpart_href->{ $pkgpart }
210 || $agent->agentnum == $part_pkg->agentnum;
212 $error = 'Select agent';
215 $cust_pkg = new FS::cust_pkg ( {
216 #later 'custnum' => $custnum,
217 'pkgpart' => $pkgpart,
218 'locationnum' => scalar($cgi->param('locationnum')),
222 my $part_svc = qsearchs('part_svc', { 'svcpart' => $svcpart } );
223 my $svcdb = $part_svc->svcdb;
225 if ( $svcdb eq 'svc_acct' ) {
228 'svcpart' => $svcpart,
229 'username' => scalar($cgi->param('username')),
230 '_password' => scalar($cgi->param('_password')),
231 'popnum' => scalar($cgi->param('popnum')),
233 $svc_acct{'domsvc'} = $cgi->param('domsvc')
234 if $cgi->param('domsvc');
236 $svc = new FS::svc_acct \%svc_acct;
238 #and just in case you were silly
239 $svc->svcpart($svcpart);
240 $svc->username($cgi->param('username'));
241 $svc->_password($cgi->param('_password'));
242 $svc->popnum($cgi->param('popnum'));
244 } elsif ( $svcdb eq 'svc_phone' ) {
247 'svcpart' => $svcpart,
248 map { $_ => scalar($cgi->param($_)) }
249 qw( countrycode phonenum sip_password pin phone_name )
252 $svc = new FS::svc_phone \%svc_phone;
254 } elsif ( $svcdb eq 'svc_dsl' ) {
257 'svcpart' => $svcpart,
258 ( map { $_ => scalar($cgi->param("ship_$_")) || scalar($cgi->param($_))}
259 qw( first last company )
261 ( map { $_ => scalar($cgi->param($_)) }
262 qw( loop_type phonenum password isp_chg isp_prev vendor_qual_id )
264 'desired_due_date' => time, #XXX enter?
265 'vendor_order_type' => 'NEW',
267 $svc = new FS::svc_dsl \%svc_dsl;
270 die "$svcdb not handled on new customer yet";
277 tie my %hash, 'Tie::RefHash';
278 %hash = ( $cust_pkg => [ $svc ] ) if $cust_pkg;
279 if ( $duplicate_of ) {
280 # order the package and service normally
281 $error ||= $new->order_pkgs( \%hash ) if $cust_pkg;
284 # create the customer
285 $error ||= $new->insert( \%hash, \@invoicing_list,
287 prospectnum => scalar($cgi->param('prospectnum')),
290 my $conf = new FS::Conf;
291 if ( $conf->exists('backend-realtime') && ! $error ) {
293 my $berror = $new->bill
294 || $new->apply_payments_and_credits
295 || $new->collect( 'realtime' => 1 );
296 warn "Warning, error billing during backend-realtime: $berror" if $berror;
301 } else { #create old record object
303 my $old = qsearchs( 'cust_main', { 'custnum' => $new->custnum } );
304 $error ||= "Old record not found!" unless $old;
306 if ( length($old->paycvv) && $new->paycvv =~ /^\s*\*+\s*$/ ) {
307 $new->paycvv($old->paycvv);
309 if ($new->ss =~ /xx/) {
312 if ($new->stateid =~ /^xxx/) {
313 $new->stateid($old->stateid);
315 if ( $new->payby =~ /^(CARD|DCRD)$/
316 && ( $new->payinfo =~ /xx/
317 || $new->payinfo =~ /^\s*N\/A\s+\(tokenized\)\s*$/
321 $new->payinfo($old->payinfo);
323 } elsif ( $new->payby =~ /^(CHEK|DCHK)$/ && $new->payinfo =~ /xx/ ) {
324 #fix for #3085 "edit of customer's routing code only surprisingly causes
325 #nothing to happen...
326 # this probably won't do the right thing when we don't have the
327 # public key (can't actually get the real $old->payinfo)
328 my($new_account, $new_aba) = split('@', $new->payinfo);
329 my($old_account, $old_aba) = split('@', $old->payinfo);
330 $new_account = $old_account if $new_account =~ /xx/;
331 $new_aba = $old_aba if $new_aba =~ /xx/;
332 $new->payinfo($new_account.'@'.$new_aba);
335 if ( ! $conf->exists('cust_main-edit_signupdate') or
336 ! $new->signupdate ) {
337 $new->signupdate($old->signupdate);
340 warn "$me calling $new -> replace( $old, \ @invoicing_list )" if $DEBUG;
341 local($FS::cust_main::DEBUG) = $DEBUG if $DEBUG;
342 local($FS::Record::DEBUG) = $DEBUG if $DEBUG;
344 local($Data::Dumper::Sortkeys) = 1;
345 warn Dumper({ new => $new, old => $old }) if $DEBUG;
347 $error ||= $new->replace( $old, \@invoicing_list,
351 warn "$me returned from replace" if $DEBUG;
355 unless ( $error ) { #XXX i guess i should be transactional... all in the insert
357 my @contact_fields = qw( classnum first last title comment emailaddress );
358 foreach my $phone_type ( qsearch({table=>'phone_type', order_by=>'weight'}) ) {
359 push @contact_fields, 'phonetypenum'.$phone_type->phonetypenum;
362 $error = $new->process_o2m( 'table' => 'contact',
363 'fields' => \@contact_fields,
364 'params' => scalar($cgi->Vars),