2 % $cgi->param('error', $error);
3 % # workaround for create_uri_query's mangling of unicode characters,
4 % # false laziness with FS::Record::ut_coord
5 % use charnames ':full';
6 % for my $pre (qw(bill ship)) {
7 % foreach (qw( latitude longitude)) {
8 % my $coord = $cgi->param($pre.'_'.$_);
9 % $coord =~ s/\N{DEGREE SIGN}\s*$//;
10 % $cgi->param($pre.'_'.$_, $coord);
13 % my $query = $m->scomp('/elements/create_uri_query', 'secure'=>1);
14 <% $cgi->redirect(popurl(2). "cust_main.cgi?$query" ) %>
18 <% $cgi->redirect(popurl(3). "view/cust_main.cgi?". $new->custnum) %>
23 my $me = '[edit/process/cust_main.cgi]';
29 my $curuser = $FS::CurrentUser::CurrentUser;
30 die "access denied" unless $curuser->access_right('Edit customer');
32 my $conf = new FS::Conf;
38 $cgi->param('tax','') unless defined $cgi->param('tax');
40 $cgi->param('refnum', (split(/:/, ($cgi->param('refnum'))[0] ))[0] );
42 my $payby = $cgi->param('payby');
48 $payby = $noauto{$payby}
49 if ! $cgi->param('payauto') && exists $noauto{$payby};
51 $cgi->param('payby', $payby);
54 if ( $payby eq 'CHEK' || $payby eq 'DCHK' ) {
55 my $payinfo = $cgi->param('payinfo1'). '@';
56 $payinfo .= $cgi->param('payinfo3').'.'
57 if $conf->config('echeck-country') eq 'CA';
58 $payinfo .= $cgi->param('payinfo2');
59 $cgi->param('payinfo',$payinfo);
61 $cgi->param('paydate',
62 $cgi->param( 'exp_month' ). '-'. $cgi->param( 'exp_year' ) );
65 my @invoicing_list = split( /\s*\,\s*/, $cgi->param('invoicing_list') );
66 push @invoicing_list, 'POST' if $cgi->param('invoicing_list_POST');
67 push @invoicing_list, 'FAX' if $cgi->param('invoicing_list_FAX');
68 $cgi->param('invoicing_list', join(',', @invoicing_list) );
70 # is this actually used? if so, we need to clone locations...
71 # but I can't find anything that sets this parameter to a non-empty value
72 # yes, fec48523d3cf056da08813f9b2b7d633b27aaf8d for #16582 is where it came in,
73 # for "duplicate address checking for new customers". afaict still in
74 # edit/cust_main/bottomfixup.html (and working?)
75 $cgi->param('duplicate_of_custnum') =~ /^(\d+)$/;
76 my $duplicate_of = $1;
78 # if this is enabled, enforce it
79 if ( $conf->exists('agent-ship_address', $cgi->param('agentnum')) ) {
80 my $agent = FS::agent->by_key($cgi->param('agentnum'));
81 my $agent_cust_main = $agent->agent_cust_main;
82 if ( $agent_cust_main ) {
83 my $agent_location = $agent_cust_main->ship_location;
84 foreach (qw(address1 city state zip country latitude longitude district)) {
85 $cgi->param("ship_$_", $agent_location->get($_));
91 for my $pre (qw(bill ship)) {
94 foreach ( FS::cust_main->location_fields ) {
95 $hash{$_} = scalar($cgi->param($pre.'_'.$_));
97 $hash{'custnum'} = $cgi->param('custnum');
98 warn Dumper \%hash if $DEBUG;
99 $locations{$pre} = FS::cust_location->new(\%hash);
102 if ( ($cgi->param('same') || '') eq 'Y' ) {
103 $locations{ship} = $locations{bill};
106 #create new record object
107 # but explicitly avoid setting ship_ fields
109 my $new = new FS::cust_main ( {
110 (map { ( $_, scalar($cgi->param($_)) ) } (fields('cust_main'))),
111 (map { ( "ship_$_", '' ) } (FS::cust_main->location_fields))
114 $new->invoice_noemail( ($cgi->param('invoice_email') eq 'Y') ? '' : 'Y' );
116 if ( $duplicate_of ) {
117 # then negate all changes to the customer; the only change we should
118 # make is to order a package, if requested
119 $new = qsearchs('cust_main', { 'custnum' => $duplicate_of })
120 # this should never happen
121 or die "nonexistent existing customer (custnum $duplicate_of)";
124 for my $pre (qw(bill ship)) {
125 $new->set($pre.'_location', $locations{$pre});
126 $new->set($pre.'_locationnum', $locations{$pre}->locationnum);
129 if ( $cgi->param('no_credit_limit') ) {
130 $new->setfield('credit_limit', '');
133 $new->tagnum( [ $cgi->param('tagnum') ] );
135 $error ||= $new->set_national_id_from_cgi( $cgi );
137 my %usedatetime = ( 'birthdate' => 1,
138 'spouse_birthdate' => 1,
139 'anniversary_date' => 1,
142 foreach my $dfield (qw(
143 signupdate birthdate spouse_birthdate anniversary_date
146 if ( $cgi->param($dfield) && $cgi->param($dfield) =~ /^([ 0-9\-\/]{0,10})$/) {
151 if ( exists $usedatetime{$dfield} && $usedatetime{$dfield} ) {
153 my $format = $conf->config('date_format') || "%m/%d/%Y";
154 my $parser = DateTime::Format::Strptime->new( pattern => $format,
155 time_zone => 'floating',
157 my $dt = $parser->parse_datetime($value);
159 $parsed = $dt->epoch;
161 $error ||= "Invalid $dfield: $value";
166 $parsed = parse_datetime($value)
167 or $error ||= "Invalid $dfield: $value";
171 $new->setfield( $dfield, $parsed );
172 $cgi->param( $dfield, $parsed );
178 $new->setfield('paid', $cgi->param('paid') )
179 if $cgi->param('paid');
182 if ( $curuser->access_right('Edit customer tax exemptions') ) {
183 my @exempt_groups = grep /\S/, $conf->config('tax-cust_exempt-groups');
184 my @tax_exempt = grep { $cgi->param("tax_$_") eq 'Y' } @exempt_groups;
185 $options{'tax_exemption'} = {
186 map { $_ => scalar($cgi->param("tax_$_".'_num')) } @tax_exempt
190 #perhaps this stuff should go to cust_main.pm
191 if ( $new->custnum eq '' or $duplicate_of ) {
196 if ( $cgi->param('pkgpart_svcpart') ) {
198 my $x = $cgi->param('pkgpart_svcpart');
199 $x =~ /^(\d+)_(\d+)$/ or die "illegal pkgpart_svcpart $x\n";
200 my($pkgpart, $svcpart) = ($1, $2);
201 my $part_pkg = qsearchs('part_pkg', { 'pkgpart' => $pkgpart } );
202 #false laziness: copied from FS::cust_pkg::order (which should become a
203 #FS::cust_main method)
206 # $part_pkg{$pkgpart} is true iff $custnum may purchase $pkgpart
207 my $agent = qsearchs('agent',{'agentnum'=> $new->agentnum });
210 # $pkgpart_href->{PKGPART} is true iff $custnum may purchase $pkgpart
211 my $pkgpart_href = $agent->pkgpart_hashref
215 # this should wind up in FS::cust_pkg!
216 $error ||= "Agent ". $new->agentnum. " (type ". $agent->typenum.
217 ") can't purchase pkgpart ". $pkgpart
218 #unless $part_pkg{ $pkgpart };
219 unless $pkgpart_href->{ $pkgpart }
220 || $agent->agentnum == $part_pkg->agentnum;
222 $error = 'Select agent';
225 $cust_pkg = new FS::cust_pkg ( {
226 #later 'custnum' => $custnum,
227 'pkgpart' => $pkgpart,
228 'locationnum' => scalar($cgi->param('locationnum')),
229 'salesnum' => scalar($cgi->param('salesnum')),
233 my $part_svc = qsearchs('part_svc', { 'svcpart' => $svcpart } );
234 my $svcdb = $part_svc->svcdb;
236 if ( $svcdb eq 'svc_acct' ) {
239 'svcpart' => $svcpart,
240 'username' => scalar($cgi->param('username')),
241 '_password' => scalar($cgi->param('_password')),
242 'popnum' => scalar($cgi->param('popnum')),
244 $svc_acct{'domsvc'} = $cgi->param('domsvc')
245 if $cgi->param('domsvc');
247 $svc = new FS::svc_acct \%svc_acct;
249 #and just in case you were silly
250 $svc->svcpart($svcpart);
251 $svc->username($cgi->param('username'));
252 $svc->_password($cgi->param('_password'));
253 $svc->popnum($cgi->param('popnum'));
255 } elsif ( $svcdb eq 'svc_phone' ) {
258 'svcpart' => $svcpart,
259 map { $_ => scalar($cgi->param($_)) }
260 qw( countrycode phonenum sip_password pin phone_name )
263 $svc = new FS::svc_phone \%svc_phone;
265 } elsif ( $svcdb eq 'svc_dsl' ) {
268 'svcpart' => $svcpart,
269 ( map { $_ => scalar($cgi->param("ship_$_")) || scalar($cgi->param($_))}
270 qw( first last company )
272 ( map { $_ => scalar($cgi->param($_)) }
273 qw( loop_type phonenum password isp_chg isp_prev vendor_qual_id )
275 'desired_due_date' => time, #XXX enter?
276 'vendor_order_type' => 'NEW',
278 $svc = new FS::svc_dsl \%svc_dsl;
281 die "$svcdb not handled on new customer yet";
288 tie my %hash, 'Tie::RefHash';
289 %hash = ( $cust_pkg => [ $svc ] ) if $cust_pkg;
290 if ( $duplicate_of ) {
291 # order the package and service normally
292 $error ||= $new->order_pkgs( \%hash ) if $cust_pkg;
295 # create the customer
296 $error ||= $new->insert( \%hash, \@invoicing_list,
298 prospectnum => scalar($cgi->param('prospectnum')),
301 my $conf = new FS::Conf;
302 if ( $conf->exists('backend-realtime') && ! $error ) {
304 my $berror = $new->bill
305 || $new->apply_payments_and_credits
306 || $new->collect( 'realtime' => 1 );
307 warn "Warning, error billing during backend-realtime: $berror" if $berror;
312 } else { #create old record object
314 my $old = qsearchs( 'cust_main', { 'custnum' => $new->custnum } );
315 $error ||= "Old record not found!" unless $old;
317 if ( length($old->paycvv) && $new->paycvv =~ /^\s*\*+\s*$/ ) {
318 $new->paycvv($old->paycvv);
320 if ($new->ss =~ /xx/) {
323 if ($new->stateid =~ /^xxx/) {
324 $new->stateid($old->stateid);
326 if ( $new->payby =~ /^(CARD|DCRD)$/
327 && ( $new->payinfo =~ /xx/
328 || $new->payinfo =~ /^\s*N\/A\s+\(tokenized\)\s*$/
332 $new->payinfo($old->payinfo);
333 $new->paymask($old->paymask) if ! $new->paymask || $new->paymask =~ /^N\/A/;
335 } elsif ( $new->payby =~ /^(CHEK|DCHK)$/ && $new->payinfo =~ /xx/ ) {
336 #fix for #3085 "edit of customer's routing code only surprisingly causes
337 #nothing to happen...
338 # this probably won't do the right thing when we don't have the
339 # public key (can't actually get the real $old->payinfo)
340 my($new_account, $new_aba) = split('@', $new->payinfo);
341 my($old_account, $old_aba) = split('@', $old->payinfo);
342 $new_account = $old_account if $new_account =~ /xx/;
343 $new_aba = $old_aba if $new_aba =~ /xx/;
344 $new->payinfo($new_account.'@'.$new_aba);
347 if ( ! $conf->exists('cust_main-edit_signupdate') or
348 ! $new->signupdate ) {
349 $new->signupdate($old->signupdate);
352 warn "$me calling $new -> replace( $old, \ @invoicing_list )" if $DEBUG;
353 local($FS::cust_main::DEBUG) = $DEBUG if $DEBUG;
354 local($FS::Record::DEBUG) = $DEBUG if $DEBUG;
356 local($Data::Dumper::Sortkeys) = 1;
357 warn Dumper({ new => $new, old => $old }) if $DEBUG;
359 $error ||= $new->replace( $old, \@invoicing_list,
363 warn "$me returned from replace" if $DEBUG;
367 unless ( $error ) { #XXX i should be transactional... all in the insert
370 $error = $new->process_o2m( 'table' => 'contact',
371 'fields' => FS::contact->cgi_contact_fields,
372 'params' => scalar($cgi->Vars),