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 @invoicing_list = split( /\s*\,\s*/, $cgi->param('invoicing_list') );
43 #push @invoicing_list, 'POST' if $cgi->param('invoicing_list_POST');
44 #push @invoicing_list, 'FAX' if $cgi->param('invoicing_list_FAX');
45 #$cgi->param('invoicing_list', join(',', @invoicing_list) );
47 my $agentnum = $cgi->param('agentnum');
49 # is this actually used? if so, we need to clone locations...
50 # but I can't find anything that sets this parameter to a non-empty value
51 # yes, fec48523d3cf056da08813f9b2b7d633b27aaf8d for #16582 is where it came in,
52 # for "duplicate address checking for new customers". afaict still in
53 # edit/cust_main/bottomfixup.html (and working?)
54 $cgi->param('duplicate_of_custnum') =~ /^(\d+)$/;
55 my $duplicate_of = $1;
57 # if this is enabled, enforce it
58 if ( $conf->exists('agent-ship_address', $cgi->param('agentnum')) ) {
59 my $agent = FS::agent->by_key($cgi->param('agentnum'));
60 my $agent_cust_main = $agent->agent_cust_main;
61 if ( $agent_cust_main ) {
62 my $agent_location = $agent_cust_main->ship_location;
63 foreach (qw(address1 city state zip country latitude longitude district)) {
64 $cgi->param("ship_$_", $agent_location->get($_));
70 for my $pre (qw(bill ship)) {
73 foreach ( FS::cust_main->location_fields ) {
74 $hash{$_} = scalar($cgi->param($pre.'_'.$_));
76 $hash{'custnum'} = $cgi->param('custnum');
77 warn Dumper \%hash if $DEBUG;
78 $locations{$pre} = FS::cust_location->new(\%hash);
81 if ( ($cgi->param('same') || '') eq 'Y' ) {
82 $locations{ship} = $locations{bill};
85 #create new record object
86 # but explicitly avoid setting ship_ fields
88 my $new = new FS::cust_main ( {
89 (map { ( $_, scalar($cgi->param($_)) ) } (fields('cust_main'))),
90 (map { ( "ship_$_", '' ) } (FS::cust_main->location_fields))
93 warn Dumper( $new ) if $DEBUG > 1;
95 if ( $duplicate_of ) {
96 # then negate all changes to the customer; the only change we should
97 # make is to order a package, if requested
98 $new = qsearchs('cust_main', { 'custnum' => $duplicate_of })
99 # this should never happen
100 or die "nonexistent existing customer (custnum $duplicate_of)";
103 for my $pre (qw(bill ship)) {
104 $new->set($pre.'_location', $locations{$pre});
105 $new->set($pre.'_locationnum', $locations{$pre}->locationnum);
108 if ( $cgi->param('no_credit_limit') ) {
109 $new->setfield('credit_limit', '');
112 $new->tagnum( [ $cgi->param('tagnum') ] );
114 $error ||= $new->set_national_id_from_cgi( $cgi );
116 my %usedatetime = ( 'birthdate' => 1,
117 'spouse_birthdate' => 1,
118 'anniversary_date' => 1,
121 foreach my $dfield (qw(
122 signupdate birthdate spouse_birthdate anniversary_date
125 if ( $cgi->param($dfield) && $cgi->param($dfield) =~ /^([ 0-9\-\/]{0,10})$/) {
130 if ( exists $usedatetime{$dfield} && $usedatetime{$dfield} ) {
132 my $format = $conf->config('date_format') || "%m/%d/%Y";
133 my $parser = DateTime::Format::Strptime->new( pattern => $format,
134 time_zone => 'floating',
136 my $dt = $parser->parse_datetime($value);
138 $parsed = $dt->epoch;
140 $error ||= "Invalid $dfield: $value";
145 $parsed = parse_datetime($value)
146 or $error ||= "Invalid $dfield: $value";
150 $new->setfield( $dfield, $parsed );
151 $cgi->param( $dfield, $parsed );
157 $new->setfield('paid', $cgi->param('paid') )
158 if $cgi->param('paid');
161 if ( $curuser->access_right('Edit customer tax exemptions') ) {
162 my @exempt_groups = grep /\S/, $conf->config('tax-cust_exempt-groups');
163 my @tax_exempt = grep { $cgi->param("tax_$_") eq 'Y' } @exempt_groups;
164 $options{'tax_exemption'} = {
165 map { $_ => scalar($cgi->param("tax_$_".'_num')) } @tax_exempt
169 $options{'cust_payby_params'} = scalar($cgi->Vars);
171 if ( $cgi->param('residential_commercial') eq 'Residential' ) {
173 my $email = $cgi->param('invoice_email') || '';
174 if ( length($email) == 0 and $conf->exists('cust_main-require_invoicing_list_email', $agentnum) ) {
175 $error = 'Email address required';
178 $options{'invoicing_list'} = [ split(/\s*,\s*/, $email) ];
179 # XXX really should include the phone numbers in here also
183 # contact UI is enabled
184 $options{'contact_params'} = scalar($cgi->Vars);
186 if ($conf->exists('cust_main-require_invoicing_list_email', $agentnum)) {
188 foreach my $prefix (grep /^contactnum\d+$/, $cgi->param) {
189 if ( length($cgi->param($prefix . '_emailaddress'))
190 and $cgi->param($prefix . '_invoice_dest') ) {
195 $error = "At least one contact must receive email invoices"
201 # kind of a hack, but some tax data vendors require a status and others
203 my $vendor = $conf->config('tax_data_vendor');
204 if ( $vendor eq 'avalara' or $vendor eq 'suretax' ) {
205 if ( ! $cgi->param('taxstatusnum') ) {
206 $error ||= 'Tax status required';
210 #perhaps this stuff should go to cust_main.pm
211 if ( $new->custnum eq '' or $duplicate_of ) {
216 if ( $cgi->param('pkgpart_svcpart') ) {
218 my $x = $cgi->param('pkgpart_svcpart');
219 $x =~ /^(\d+)_(\d+)$/ or die "illegal pkgpart_svcpart $x\n";
220 my($pkgpart, $svcpart) = ($1, $2);
221 my $part_pkg = qsearchs('part_pkg', { 'pkgpart' => $pkgpart } );
222 #false laziness: copied from FS::cust_pkg::order (which should become a
223 #FS::cust_main method)
226 # $part_pkg{$pkgpart} is true iff $custnum may purchase $pkgpart
227 my $agent = qsearchs('agent',{'agentnum'=> $new->agentnum });
230 # $pkgpart_href->{PKGPART} is true iff $custnum may purchase $pkgpart
231 my $pkgpart_href = $agent->pkgpart_hashref
235 # this should wind up in FS::cust_pkg!
236 $error ||= "Agent ". $new->agentnum. " (type ". $agent->typenum.
237 ") can't purchase pkgpart ". $pkgpart
238 #unless $part_pkg{ $pkgpart };
239 unless $pkgpart_href->{ $pkgpart }
240 || $agent->agentnum == $part_pkg->agentnum;
242 $error = 'Select agent';
245 $cust_pkg = new FS::cust_pkg ( {
246 #later 'custnum' => $custnum,
247 'pkgpart' => $pkgpart,
248 'locationnum' => scalar($cgi->param('locationnum')),
249 'salesnum' => scalar($cgi->param('salesnum')),
253 my $part_svc = qsearchs('part_svc', { 'svcpart' => $svcpart } );
254 my $svcdb = $part_svc->svcdb;
256 if ( $svcdb eq 'svc_acct' ) {
259 'svcpart' => $svcpart,
260 'username' => scalar($cgi->param('username')),
261 '_password' => scalar($cgi->param('_password')),
262 'popnum' => scalar($cgi->param('popnum')),
264 $svc_acct{'domsvc'} = $cgi->param('domsvc')
265 if $cgi->param('domsvc');
267 $svc = new FS::svc_acct \%svc_acct;
269 #and just in case you were silly
270 $svc->svcpart($svcpart);
271 $svc->username($cgi->param('username'));
272 $svc->_password($cgi->param('_password'));
273 $svc->popnum($cgi->param('popnum'));
275 } elsif ( $svcdb eq 'svc_phone' ) {
278 'svcpart' => $svcpart,
279 map { $_ => scalar($cgi->param($_)) }
280 qw( countrycode phonenum sip_password pin phone_name )
283 $svc = new FS::svc_phone \%svc_phone;
285 } elsif ( $svcdb eq 'svc_dsl' ) {
288 'svcpart' => $svcpart,
289 ( map { $_ => scalar($cgi->param("ship_$_")) || scalar($cgi->param($_))}
290 qw( first last company )
292 ( map { $_ => scalar($cgi->param($_)) }
293 qw( loop_type phonenum password isp_chg isp_prev vendor_qual_id )
295 'desired_due_date' => time, #XXX enter?
296 'vendor_order_type' => 'NEW',
298 $svc = new FS::svc_dsl \%svc_dsl;
301 die "$svcdb not handled on new customer yet";
308 tie my %hash, 'Tie::RefHash';
309 %hash = ( $cust_pkg => [ $svc ] ) if $cust_pkg;
310 if ( $duplicate_of ) {
311 # order the package and service normally
312 $error ||= $new->order_pkgs( \%hash ) if $cust_pkg;
315 # create the customer
316 $error ||= $new->insert( \%hash,
318 prospectnum => scalar($cgi->param('prospectnum')),
321 my $conf = new FS::Conf;
322 if ( $conf->exists('backend-realtime') && ! $error ) {
324 my $berror = $new->bill
325 || $new->apply_payments_and_credits
326 || $new->collect( 'realtime' => 1 );
327 warn "Warning, error billing during backend-realtime: $berror" if $berror;
332 } else { #create old record object
334 my $old = qsearchs( 'cust_main', { 'custnum' => $new->custnum } );
335 $error ||= "Old record not found!" unless $old;
337 if ($new->ss =~ /xx/) {
340 if ($new->stateid =~ /^xxx/) {
341 $new->stateid($old->stateid);
344 if ( ! $conf->exists('cust_main-edit_signupdate') or
345 ! $new->signupdate ) {
346 $new->signupdate($old->signupdate);
349 warn "$me calling $new -> replace( $old )" if $DEBUG;
350 local($FS::cust_main::DEBUG) = $DEBUG if $DEBUG;
351 local($FS::Record::DEBUG) = $DEBUG if $DEBUG;
353 local($Data::Dumper::Sortkeys) = 1;
354 warn Dumper({ new => $new, old => $old, options => \%options}) if $DEBUG;
356 $error ||= $new->replace( $old, %options );
358 warn "$me returned from replace" if $DEBUG;