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 @invoicing_list = split( /\s*\,\s*/, $cgi->param('invoicing_list') );
33 #push @invoicing_list, 'POST' if $cgi->param('invoicing_list_POST');
34 #push @invoicing_list, 'FAX' if $cgi->param('invoicing_list_FAX');
35 #$cgi->param('invoicing_list', join(',', @invoicing_list) );
37 my $agentnum = $cgi->param('agentnum');
39 # is this actually used? if so, we need to clone locations...
40 # but I can't find anything that sets this parameter to a non-empty value
41 # yes, fec48523d3cf056da08813f9b2b7d633b27aaf8d for #16582 is where it came in,
42 # for "duplicate address checking for new customers". afaict still in
43 # edit/cust_main/bottomfixup.html (and working?)
44 $cgi->param('duplicate_of_custnum') =~ /^(\d+)$/;
45 my $duplicate_of = $1;
47 # if this is enabled, enforce it
48 if ( $conf->exists('agent-ship_address', $cgi->param('agentnum')) ) {
49 my $agent = FS::agent->by_key($cgi->param('agentnum'));
50 my $agent_cust_main = $agent->agent_cust_main;
51 if ( $agent_cust_main ) {
52 my $agent_location = $agent_cust_main->ship_location;
53 foreach (qw(address1 city state zip country latitude longitude district)) {
54 $cgi->param("ship_$_", $agent_location->get($_));
60 for my $pre (qw(bill ship)) {
62 foreach (qw( latitude longitude)) {
63 my $coord = $cgi->param($pre.'_'.$_);
64 $coord =~ s/\N{DEGREE SIGN}\s*$//;
65 $cgi->param($pre.'_'.$_, $coord);
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 $locations{$pre} = FS::cust_location->new(\%hash);
77 if ( ($cgi->param('same') || '') eq 'Y' ) {
78 $locations{ship} = $locations{bill};
81 #create new record object
82 # but explicitly avoid setting ship_ fields
84 my $new = new FS::cust_main ( {
85 (map { ( $_, scalar($cgi->param($_)) ) } (fields('cust_main'))),
86 (map { ( "ship_$_", '' ) } (FS::cust_main->location_fields))
89 warn Dumper( $new ) if $DEBUG > 1;
91 if ( $duplicate_of ) {
92 # then negate all changes to the customer; the only change we should
93 # make is to order a package, if requested
94 $new = qsearchs('cust_main', { 'custnum' => $duplicate_of })
95 # this should never happen
96 or die "nonexistent existing customer (custnum $duplicate_of)";
99 for my $pre (qw(bill ship)) {
100 $new->set($pre.'_location', $locations{$pre});
101 $new->set($pre.'_locationnum', $locations{$pre}->locationnum);
104 if ( $cgi->param('no_credit_limit') ) {
105 $new->setfield('credit_limit', '');
108 $new->tagnum( [ $cgi->param('tagnum') ] );
110 $error ||= $new->set_national_id_from_cgi( $cgi );
112 my %usedatetime = ( 'birthdate' => 1,
113 'spouse_birthdate' => 1,
114 'anniversary_date' => 1,
117 foreach my $dfield (qw(
118 signupdate birthdate spouse_birthdate anniversary_date
121 if ( $cgi->param($dfield) && $cgi->param($dfield) =~ /^([ 0-9\-\/]{0,10})$/) {
126 if ( exists $usedatetime{$dfield} && $usedatetime{$dfield} ) {
128 my $format = $conf->config('date_format') || "%m/%d/%Y";
129 my $parser = DateTime::Format::Strptime->new( pattern => $format,
130 time_zone => 'floating',
132 my $dt = $parser->parse_datetime($value);
134 $parsed = $dt->epoch;
136 $error ||= "Invalid $dfield: $value";
141 $parsed = parse_datetime($value)
142 or $error ||= "Invalid $dfield: $value";
146 $new->setfield( $dfield, $parsed );
147 $cgi->param( $dfield, $parsed );
153 $new->setfield('paid', $cgi->param('paid') )
154 if $cgi->param('paid');
157 if ( $curuser->access_right('Edit customer tax exemptions') ) {
158 my @exempt_groups = grep /\S/, $conf->config('tax-cust_exempt-groups');
159 my @tax_exempt = grep { $cgi->param("tax_$_") eq 'Y' } @exempt_groups;
160 $options{'tax_exemption'} = {
161 map { $_ => scalar($cgi->param("tax_$_".'_num')) } @tax_exempt
165 $options{'cust_payby_params'} = scalar($cgi->Vars);
167 if ( $cgi->param('residential_commercial') eq 'Residential' ) {
169 my $email = $cgi->param('invoice_email') || '';
170 if ( length($email) == 0 and $conf->exists('cust_main-require_invoicing_list_email', $agentnum) ) {
171 $error = 'Email address required';
174 $options{'invoicing_list'} = [ split(/\s*,\s*/, $email) ];
175 # XXX really should include the phone numbers in here also
179 # contact UI is enabled
180 $options{'contact_params'} = scalar($cgi->Vars);
182 if ($conf->exists('cust_main-require_invoicing_list_email', $agentnum)) {
184 foreach my $prefix (grep /^contactnum\d+$/, $cgi->param) {
185 if ( length($cgi->param($prefix . '_emailaddress'))
186 and $cgi->param($prefix . '_invoice_dest') ) {
191 $error = "At least one contact must receive email invoices"
197 # kind of a hack, but some tax data vendors require a status and others
199 my $vendor = $conf->config('tax_data_vendor');
200 if ( $vendor eq 'avalara' or $vendor eq 'suretax' ) {
201 if ( ! $cgi->param('taxstatusnum') ) {
202 $error ||= 'Tax status required';
206 #perhaps this stuff should go to cust_main.pm
207 if ( $new->custnum eq '' or $duplicate_of ) {
212 if ( $cgi->param('pkgpart_svcpart') ) {
214 my $x = $cgi->param('pkgpart_svcpart');
215 $x =~ /^(\d+)_(\d+)$/ or die "illegal pkgpart_svcpart $x\n";
216 my($pkgpart, $svcpart) = ($1, $2);
217 my $part_pkg = qsearchs('part_pkg', { 'pkgpart' => $pkgpart } );
218 #false laziness: copied from FS::cust_pkg::order (which should become a
219 #FS::cust_main method)
222 # $part_pkg{$pkgpart} is true iff $custnum may purchase $pkgpart
223 my $agent = qsearchs('agent',{'agentnum'=> $new->agentnum });
226 # $pkgpart_href->{PKGPART} is true iff $custnum may purchase $pkgpart
227 my $pkgpart_href = $agent->pkgpart_hashref
231 # this should wind up in FS::cust_pkg!
232 $error ||= "Agent ". $new->agentnum. " (type ". $agent->typenum.
233 ") can't purchase pkgpart ". $pkgpart
234 #unless $part_pkg{ $pkgpart };
235 unless $pkgpart_href->{ $pkgpart }
236 || $agent->agentnum == $part_pkg->agentnum;
238 $error = 'Select agent';
241 $cust_pkg = new FS::cust_pkg ( {
242 #later 'custnum' => $custnum,
243 'pkgpart' => $pkgpart,
244 'locationnum' => scalar($cgi->param('locationnum')),
245 'salesnum' => scalar($cgi->param('salesnum')),
249 my $part_svc = qsearchs('part_svc', { 'svcpart' => $svcpart } );
250 my $svcdb = $part_svc->svcdb;
252 if ( $svcdb eq 'svc_acct' ) {
255 'svcpart' => $svcpart,
256 'username' => scalar($cgi->param('username')),
257 '_password' => scalar($cgi->param('_password')),
258 'popnum' => scalar($cgi->param('popnum')),
260 $svc_acct{'domsvc'} = $cgi->param('domsvc')
261 if $cgi->param('domsvc');
263 $svc = new FS::svc_acct \%svc_acct;
265 #and just in case you were silly
266 $svc->svcpart($svcpart);
267 $svc->username($cgi->param('username'));
268 $svc->_password($cgi->param('_password'));
269 $svc->popnum($cgi->param('popnum'));
271 } elsif ( $svcdb eq 'svc_phone' ) {
274 'svcpart' => $svcpart,
275 map { $_ => scalar($cgi->param($_)) }
276 qw( countrycode phonenum sip_password pin phone_name )
279 $svc = new FS::svc_phone \%svc_phone;
281 } elsif ( $svcdb eq 'svc_dsl' ) {
284 'svcpart' => $svcpart,
285 ( map { $_ => scalar($cgi->param("ship_$_")) || scalar($cgi->param($_))}
286 qw( first last company )
288 ( map { $_ => scalar($cgi->param($_)) }
289 qw( loop_type phonenum password isp_chg isp_prev vendor_qual_id )
291 'desired_due_date' => time, #XXX enter?
292 'vendor_order_type' => 'NEW',
294 $svc = new FS::svc_dsl \%svc_dsl;
297 die "$svcdb not handled on new customer yet";
304 tie my %hash, 'Tie::RefHash';
305 %hash = ( $cust_pkg => [ $svc ] ) if $cust_pkg;
306 if ( $duplicate_of ) {
307 # order the package and service normally
308 $error ||= $new->order_pkgs( \%hash ) if $cust_pkg;
311 # create the customer
312 $error ||= $new->insert( \%hash,
314 prospectnum => scalar($cgi->param('prospectnum')),
317 my $conf = new FS::Conf;
318 if ( $conf->exists('backend-realtime') && ! $error ) {
320 my $berror = $new->bill
321 || $new->apply_payments_and_credits
322 || $new->collect( 'realtime' => 1 );
323 warn "Warning, error billing during backend-realtime: $berror" if $berror;
328 } else { #create old record object
330 my $old = qsearchs( 'cust_main', { 'custnum' => $new->custnum } );
331 $error ||= "Old record not found!" unless $old;
333 if ($new->ss =~ /xx/) {
336 if ($new->stateid =~ /^xxx/) {
337 $new->stateid($old->stateid);
340 if ( ! $conf->exists('cust_main-edit_signupdate') or
341 ! $new->signupdate ) {
342 $new->signupdate($old->signupdate);
345 warn "$me calling $new -> replace( $old )" if $DEBUG;
346 local($FS::cust_main::DEBUG) = $DEBUG if $DEBUG;
347 local($FS::Record::DEBUG) = $DEBUG if $DEBUG;
349 local($Data::Dumper::Sortkeys) = 1;
350 warn Dumper({ new => $new, old => $old, options => \%options}) if $DEBUG;
352 $error ||= $new->replace( $old, %options );
354 warn "$me returned from replace" if $DEBUG;