2 % $cgi->param('error', $error);
3 % my $query = $m->scomp('/elements/create_uri_query', 'secure'=>1);
4 <% $cgi->redirect(popurl(2). "cust_main.cgi?$query" ) %>
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) );
61 #create new record object
63 my $new = new FS::cust_main ( {
65 $_, scalar($cgi->param($_))
69 $new->invoice_noemail( ($cgi->param('invoice_email') eq 'Y') ? '' : 'Y' );
71 $cgi->param('duplicate_of_custnum') =~ /^(\d+)$/;
72 my $duplicate_of = $1;
73 if ( $duplicate_of ) {
74 # then negate all changes to the customer; the only change we should
75 # make is to order a package, if requested
76 $new = qsearchs('cust_main', { 'custnum' => $duplicate_of })
77 # this should never happen
78 or die "nonexistent existing customer (custnum $duplicate_of)";
81 # if this is enabled, enforce it
82 if ( $conf->exists('agent-ship_address', $cgi->param('agentnum')) ) {
83 my $agent = FS::agent->by_key($cgi->param('agentnum'));
84 my $agent_cust_main = $agent->agent_cust_main;
85 if ( $agent_cust_main ) {
87 $pre = 'ship_' if $agent_cust_main->has_ship_address;
88 foreach (qw(address1 city state zip country latitude longitude)) {
89 $new->set("ship_$_", $agent_cust_main->get($pre.$_));
91 foreach (qw(first last company)) {
92 $new->set("ship_$_", $cgi->param($_));
94 $cgi->param('same', '');
98 if ( defined($cgi->param('same')) && $cgi->param('same') eq "Y" ) {
99 $new->setfield("ship_$_", '') foreach qw(
100 last first company address1 address2 city county state zip
101 country daytime night fax
105 if ( $cgi->param('no_credit_limit') ) {
106 $new->setfield('credit_limit', '');
109 $new->tagnum( [ $cgi->param('tagnum') ] );
111 $error ||= $new->set_national_id_from_cgi( $cgi );
113 my %usedatetime = ( 'birthdate' => 1,
114 'spouse_birthdate' => 1,
115 'anniversary_date' => 1,
118 foreach my $dfield (qw(
119 signupdate birthdate spouse_birthdate anniversary_date
122 if ( $cgi->param($dfield) && $cgi->param($dfield) =~ /^([ 0-9\-\/]{0,10})$/) {
127 if ( exists $usedatetime{$dfield} && $usedatetime{$dfield} ) {
129 my $format = $conf->config('date_format') || "%m/%d/%Y";
130 my $parser = DateTime::Format::Strptime->new( pattern => $format,
131 time_zone => 'floating',
133 my $dt = $parser->parse_datetime($value);
135 $parsed = $dt->epoch;
137 $error ||= "Invalid $dfield: $value";
142 $parsed = parse_datetime($value)
143 or $error ||= "Invalid $dfield: $value";
147 $new->setfield( $dfield, $parsed );
148 $cgi->param( $dfield, $parsed );
154 $new->setfield('paid', $cgi->param('paid') )
155 if $cgi->param('paid');
158 if ( $curuser->access_right('Edit customer tax exemptions') ) {
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 $options{'tax_exemption'} = {
162 map { $_ => scalar($cgi->param("tax_$_".'_num')) } @tax_exempt
166 #perhaps this stuff should go to cust_main.pm
167 if ( $new->custnum eq '' or $duplicate_of ) {
172 if ( $cgi->param('pkgpart_svcpart') ) {
174 my $x = $cgi->param('pkgpart_svcpart');
175 $x =~ /^(\d+)_(\d+)$/ or die "illegal pkgpart_svcpart $x\n";
176 my($pkgpart, $svcpart) = ($1, $2);
177 my $part_pkg = qsearchs('part_pkg', { 'pkgpart' => $pkgpart } );
178 #false laziness: copied from FS::cust_pkg::order (which should become a
179 #FS::cust_main method)
182 # $part_pkg{$pkgpart} is true iff $custnum may purchase $pkgpart
183 my $agent = qsearchs('agent',{'agentnum'=> $new->agentnum });
186 # $pkgpart_href->{PKGPART} is true iff $custnum may purchase $pkgpart
187 my $pkgpart_href = $agent->pkgpart_hashref
191 # this should wind up in FS::cust_pkg!
192 $error ||= "Agent ". $new->agentnum. " (type ". $agent->typenum.
193 ") can't purchase pkgpart ". $pkgpart
194 #unless $part_pkg{ $pkgpart };
195 unless $pkgpart_href->{ $pkgpart }
196 || $agent->agentnum == $part_pkg->agentnum;
198 $error = 'Select agent';
201 $cust_pkg = new FS::cust_pkg ( {
202 #later 'custnum' => $custnum,
203 'pkgpart' => $pkgpart,
204 'locationnum' => scalar($cgi->param('locationnum')),
208 my $part_svc = qsearchs('part_svc', { 'svcpart' => $svcpart } );
209 my $svcdb = $part_svc->svcdb;
211 if ( $svcdb eq 'svc_acct' ) {
214 'svcpart' => $svcpart,
215 'username' => scalar($cgi->param('username')),
216 '_password' => scalar($cgi->param('_password')),
217 'popnum' => scalar($cgi->param('popnum')),
219 $svc_acct{'domsvc'} = $cgi->param('domsvc')
220 if $cgi->param('domsvc');
222 $svc = new FS::svc_acct \%svc_acct;
224 #and just in case you were silly
225 $svc->svcpart($svcpart);
226 $svc->username($cgi->param('username'));
227 $svc->_password($cgi->param('_password'));
228 $svc->popnum($cgi->param('popnum'));
230 } elsif ( $svcdb eq 'svc_phone' ) {
233 'svcpart' => $svcpart,
234 map { $_ => scalar($cgi->param($_)) }
235 qw( countrycode phonenum sip_password pin phone_name )
238 $svc = new FS::svc_phone \%svc_phone;
240 } elsif ( $svcdb eq 'svc_dsl' ) {
243 'svcpart' => $svcpart,
244 ( map { $_ => scalar($cgi->param("ship_$_")) || scalar($cgi->param($_))}
245 qw( first last company )
247 ( map { $_ => scalar($cgi->param($_)) }
248 qw( loop_type phonenum password isp_chg isp_prev vendor_qual_id )
250 'desired_due_date' => time, #XXX enter?
251 'vendor_order_type' => 'NEW',
253 $svc = new FS::svc_dsl \%svc_dsl;
256 die "$svcdb not handled on new customer yet";
263 tie my %hash, 'Tie::RefHash';
264 %hash = ( $cust_pkg => [ $svc ] ) if $cust_pkg;
265 if ( $duplicate_of ) {
266 # order the package and service normally
267 $error ||= $new->order_pkgs( \%hash ) if $cust_pkg;
270 # create the customer
271 $error ||= $new->insert( \%hash, \@invoicing_list,
273 prospectnum => scalar($cgi->param('prospectnum')),
276 my $conf = new FS::Conf;
277 if ( $conf->exists('backend-realtime') && ! $error ) {
279 my $berror = $new->bill
280 || $new->apply_payments_and_credits
281 || $new->collect( 'realtime' => 1 );
282 warn "Warning, error billing during backend-realtime: $berror" if $berror;
287 } else { #create old record object
289 my $old = qsearchs( 'cust_main', { 'custnum' => $new->custnum } );
290 $error ||= "Old record not found!" unless $old;
291 if ( length($old->paycvv) && $new->paycvv =~ /^\s*\*+\s*$/ ) {
292 $new->paycvv($old->paycvv);
294 if ($new->ss =~ /xx/) {
297 if ($new->stateid =~ /^xxx/) {
298 $new->stateid($old->stateid);
300 if ( $new->payby =~ /^(CARD|DCRD)$/
301 && ( $new->payinfo =~ /xx/
302 || $new->payinfo =~ /^\s*N\/A\s+\(tokenized\)\s*$/
306 $new->payinfo($old->payinfo);
308 } elsif ( $new->payby =~ /^(CHEK|DCHK)$/ && $new->payinfo =~ /xx/ ) {
309 #fix for #3085 "edit of customer's routing code only surprisingly causes
310 #nothing to happen...
311 # this probably won't do the right thing when we don't have the
312 # public key (can't actually get the real $old->payinfo)
313 my($new_account, $new_aba) = split('@', $new->payinfo);
314 my($old_account, $old_aba) = split('@', $old->payinfo);
315 $new_account = $old_account if $new_account =~ /xx/;
316 $new_aba = $old_aba if $new_aba =~ /xx/;
317 $new->payinfo($new_account.'@'.$new_aba);
320 if ( ! $conf->exists('cust_main-edit_signupdate') or
321 ! $new->signupdate ) {
322 $new->signupdate($old->signupdate);
325 warn "$me calling $new -> replace( $old, \ @invoicing_list )" if $DEBUG;
326 local($FS::cust_main::DEBUG) = $DEBUG if $DEBUG;
327 local($FS::Record::DEBUG) = $DEBUG if $DEBUG;
329 $error ||= $new->replace( $old, \@invoicing_list,
333 warn "$me returned from replace" if $DEBUG;