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) );
61 #create new record object
63 my $new = new FS::cust_main ( {
65 $_, scalar($cgi->param($_))
69 $cgi->param('duplicate_of_custnum') =~ /^(\d+)$/;
70 my $duplicate_of = $1;
71 if ( $duplicate_of ) {
72 # then negate all changes to the customer; the only change we should
73 # make is to order a package, if requested
74 $new = qsearchs('cust_main', { 'custnum' => $duplicate_of })
75 # this should never happen
76 or die "nonexistent existing customer (custnum $duplicate_of)";
79 if ( defined($cgi->param('same')) && $cgi->param('same') eq "Y" ) {
80 $new->setfield("ship_$_", '') foreach qw(
81 last first company address1 address2 city county state zip
82 country daytime night fax
86 if ( $cgi->param('no_credit_limit') ) {
87 $new->setfield('credit_limit', '');
90 $new->tagnum( [ $cgi->param('tagnum') ] );
92 my %usedatetime = ( 'birthdate' => 1,
93 'spouse_birthdate' => 1,
96 foreach my $dfield (qw( birthdate spouse_birthdate signupdate )) {
98 if ( $cgi->param($dfield) && $cgi->param($dfield) =~ /^([ 0-9\-\/]{0,10})$/) {
103 if ( exists $usedatetime{$dfield} && $usedatetime{$dfield} ) {
105 my $format = $conf->config('date_format') || "%m/%d/%Y";
106 my $parser = DateTime::Format::Strptime->new( pattern => $format,
107 time_zone => 'floating',
109 my $dt = $parser->parse_datetime($value);
111 $parsed = $dt->epoch;
113 $error ||= "Invalid $dfield: $value";
118 $parsed = parse_datetime($value)
119 or $error ||= "Invalid $dfield: $value";
123 $new->setfield( $dfield, $parsed );
124 $cgi->param( $dfield, $parsed );
130 $new->setfield('paid', $cgi->param('paid') )
131 if $cgi->param('paid');
133 my @exempt_groups = grep /\S/, $conf->config('tax-cust_exempt-groups');
134 my @tax_exempt = grep { $cgi->param("tax_$_") eq 'Y' } @exempt_groups;
135 my %tax_exempt = map { $_ => scalar($cgi->param("tax_$_".'_num')) } @tax_exempt;
137 #perhaps this stuff should go to cust_main.pm
138 if ( $new->custnum eq '' or $duplicate_of ) {
143 if ( $cgi->param('pkgpart_svcpart') ) {
145 my $x = $cgi->param('pkgpart_svcpart');
146 $x =~ /^(\d+)_(\d+)$/ or die "illegal pkgpart_svcpart $x\n";
147 my($pkgpart, $svcpart) = ($1, $2);
148 my $part_pkg = qsearchs('part_pkg', { 'pkgpart' => $pkgpart } );
149 #false laziness: copied from FS::cust_pkg::order (which should become a
150 #FS::cust_main method)
153 # $part_pkg{$pkgpart} is true iff $custnum may purchase $pkgpart
154 my $agent = qsearchs('agent',{'agentnum'=> $new->agentnum });
157 # $pkgpart_href->{PKGPART} is true iff $custnum may purchase $pkgpart
158 my $pkgpart_href = $agent->pkgpart_hashref
162 # this should wind up in FS::cust_pkg!
163 $error ||= "Agent ". $new->agentnum. " (type ". $agent->typenum.
164 ") can't purchase pkgpart ". $pkgpart
165 #unless $part_pkg{ $pkgpart };
166 unless $pkgpart_href->{ $pkgpart }
167 || $agent->agentnum == $part_pkg->agentnum;
169 $error = 'Select agent';
172 $cust_pkg = new FS::cust_pkg ( {
173 #later 'custnum' => $custnum,
174 'pkgpart' => $pkgpart,
175 'locationnum' => scalar($cgi->param('locationnum')),
179 my $part_svc = qsearchs('part_svc', { 'svcpart' => $svcpart } );
180 my $svcdb = $part_svc->svcdb;
182 if ( $svcdb eq 'svc_acct' ) {
185 'svcpart' => $svcpart,
186 'username' => scalar($cgi->param('username')),
187 '_password' => scalar($cgi->param('_password')),
188 'popnum' => scalar($cgi->param('popnum')),
190 $svc_acct{'domsvc'} = $cgi->param('domsvc')
191 if $cgi->param('domsvc');
193 $svc = new FS::svc_acct \%svc_acct;
195 #and just in case you were silly
196 $svc->svcpart($svcpart);
197 $svc->username($cgi->param('username'));
198 $svc->_password($cgi->param('_password'));
199 $svc->popnum($cgi->param('popnum'));
201 } elsif ( $svcdb eq 'svc_phone' ) {
204 'svcpart' => $svcpart,
205 map { $_ => scalar($cgi->param($_)) }
206 qw( countrycode phonenum sip_password pin phone_name )
209 $svc = new FS::svc_phone \%svc_phone;
211 } elsif ( $svcdb eq 'svc_dsl' ) {
214 'svcpart' => $svcpart,
215 ( map { $_ => scalar($cgi->param("ship_$_")) || scalar($cgi->param($_))}
216 qw( first last company )
218 ( map { $_ => scalar($cgi->param($_)) }
219 qw( loop_type phonenum password isp_chg isp_prev vendor_qual_id )
221 'desired_due_date' => time, #XXX enter?
222 'vendor_order_type' => 'NEW',
224 $svc = new FS::svc_dsl \%svc_dsl;
227 die "$svcdb not handled on new customer yet";
234 tie my %hash, 'Tie::RefHash';
235 %hash = ( $cust_pkg => [ $svc ] ) if $cust_pkg;
236 if ( $duplicate_of ) {
237 # order the package and service normally
238 $error ||= $new->order_pkgs( \%hash ) if $cust_pkg;
241 # create the customer
242 $error ||= $new->insert( \%hash, \@invoicing_list,
243 'tax_exemption'=> \%tax_exempt,
244 'prospectnum' => scalar($cgi->param('prospectnum')),
247 my $conf = new FS::Conf;
248 if ( $conf->exists('backend-realtime') && ! $error ) {
250 my $berror = $new->bill
251 || $new->apply_payments_and_credits
252 || $new->collect( 'realtime' => 1 );
253 warn "Warning, error billing during backend-realtime: $berror" if $berror;
258 } else { #create old record object
260 my $old = qsearchs( 'cust_main', { 'custnum' => $new->custnum } );
261 $error ||= "Old record not found!" unless $old;
262 if ( length($old->paycvv) && $new->paycvv =~ /^\s*\*+\s*$/ ) {
263 $new->paycvv($old->paycvv);
265 if ($new->ss =~ /xx/) {
268 if ($new->stateid =~ /^xxx/) {
269 $new->stateid($old->stateid);
271 if ( $new->payby =~ /^(CARD|DCRD)$/
272 && ( $new->payinfo =~ /xx/
273 || $new->payinfo =~ /^\s*N\/A\s+\(tokenized\)\s*$/
277 $new->payinfo($old->payinfo);
279 } elsif ( $new->payby =~ /^(CHEK|DCHK)$/ && $new->payinfo =~ /xx/ ) {
280 #fix for #3085 "edit of customer's routing code only surprisingly causes
281 #nothing to happen...
282 # this probably won't do the right thing when we don't have the
283 # public key (can't actually get the real $old->payinfo)
284 my($new_account, $new_aba) = split('@', $new->payinfo);
285 my($old_account, $old_aba) = split('@', $old->payinfo);
286 $new_account = $old_account if $new_account =~ /xx/;
287 $new_aba = $old_aba if $new_aba =~ /xx/;
288 $new->payinfo($new_account.'@'.$new_aba);
291 if ( ! $conf->exists('cust_main-edit_signupdate') or
292 ! $new->signupdate ) {
293 $new->signupdate($old->signupdate);
296 warn "$me calling $new -> replace( $old, \ @invoicing_list )" if $DEBUG;
297 local($FS::cust_main::DEBUG) = $DEBUG if $DEBUG;
298 local($FS::Record::DEBUG) = $DEBUG if $DEBUG;
300 $error ||= $new->replace( $old, \@invoicing_list,
301 'tax_exemption' => \%tax_exempt,
304 warn "$me returned from replace" if $DEBUG;
308 unless ( $error ) { #XXX i guess i should be transactional... all in the insert
310 my @contact_fields = qw( classnum first last title comment emailaddress );
311 foreach my $phone_type ( qsearch({table=>'phone_type', order_by=>'weight'}) ) {
312 push @contact_fields, 'phonetypenum'.$phone_type->phonetypenum;
315 $error = $new->process_o2m( 'table' => 'contact',
316 'fields' => \@contact_fields,
317 'params' => scalar($cgi->Vars),