add national id # handling for my, RT#18543
[freeside.git] / httemplate / edit / process / cust_main.cgi
1 % if ( $error ) {
2 %   $cgi->param('error', $error);
3 %
4 <% $cgi->redirect(popurl(2). "cust_main.cgi?". $cgi->query_string ) %>
5 %
6 % } else { 
7 %
8 <% $cgi->redirect(popurl(3). "view/cust_main.cgi?". $new->custnum) %>
9 %
10 % }
11 <%once>
12
13 my $me = '[edit/process/cust_main.cgi]';
14 my $DEBUG = 0;
15
16 </%once>
17 <%init>
18
19 die "access denied"
20   unless $FS::CurrentUser::CurrentUser->access_right('Edit customer');
21
22 my $conf = new FS::Conf;
23
24 my $error = '';
25
26 #unmunge stuff
27
28 $cgi->param('tax','') unless defined $cgi->param('tax');
29
30 $cgi->param('refnum', (split(/:/, ($cgi->param('refnum'))[0] ))[0] );
31
32 my $payby = $cgi->param('payby');
33
34 my %noauto = (
35   'CARD' => 'DCRD',
36   'CHEK' => 'DCHK',
37 );
38 $payby = $noauto{$payby}
39   if ! $cgi->param('payauto') && exists $noauto{$payby};
40
41 $cgi->param('payby', $payby);
42
43 if ( $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);
50   }
51   $cgi->param('paydate',
52     $cgi->param( 'exp_month' ). '-'. $cgi->param( 'exp_year' ) );
53 }
54
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) );
59
60 # is this actually used?  if so, we need to clone locations...
61 # but I can't find anything that sets this parameter to a non-empty value
62 $cgi->param('duplicate_of_custnum') =~ /^(\d+)$/;
63 my $duplicate_of = $1;
64
65 my %locations;
66 for my $pre (qw(bill ship)) {
67
68   my %hash;
69   foreach ( FS::cust_main->location_fields ) {
70     $hash{$_} = scalar($cgi->param($pre.'_'.$_));
71   }
72   $hash{'custnum'} = $cgi->param('custnum');
73   warn Dumper \%hash if $DEBUG;
74   # if we can qsearchs it, then it's unchanged, so use that
75   $locations{$pre} = qsearchs('cust_location', \%hash)
76                      || FS::cust_location->new( \%hash );
77
78 }
79
80 if ( ($cgi->param('same') || '') eq 'Y' ) {
81   $locations{ship} = $locations{bill};
82 }
83
84 #create new record object
85 # but explicitly avoid setting ship_ fields
86
87 my $new = new FS::cust_main ( {
88   map { ( $_, scalar($cgi->param($_)) ) } (fields('cust_main')),
89   map { ( "ship_$_", '' ) } (FS::cust_main->location_fields)
90 } );
91
92 $new->invoice_noemail( ($cgi->param('invoice_email') eq 'Y') ? '' : 'Y' );
93
94 if ( $duplicate_of ) {
95   # then negate all changes to the customer; the only change we should
96   # make is to order a package, if requested
97   $new = qsearchs('cust_main', { 'custnum' => $duplicate_of })
98   # this should never happen
99     or die "nonexistent existing customer (custnum $duplicate_of)";
100 }
101
102 for my $pre (qw(bill ship)) {
103   $new->set($pre.'_location', $locations{$pre});
104   $new->set($pre.'_locationnum', $locations{$pre}->locationnum);
105 }
106
107 if ( $cgi->param('no_credit_limit') ) {
108   $new->setfield('credit_limit', '');
109 }
110
111 $new->tagnum( [ $cgi->param('tagnum') ] );
112
113 if ( my $id_country = $conf->config('national_id-country') ) {
114   if ( $id_country eq 'MY' ) {
115
116     if ( $cgi->param('national_id1') =~ /\S/ ) {
117       my $nric = $cgi->param('national_id1');
118       $nric =~ s/\s//g;
119       if ( $nric =~ /^(\d{6})\-?(\d{2})\-?(\d{4})$/ ) {
120         $new->national_id( "$1-$2-$3" );
121       } else {
122         $error ||= "Illegal NRIC: ". $cgi->param('national_id1');
123       }
124     } elsif ( $cgi->param('national_id2') =~ /\S/ ) {
125       my $oldic = $cgi->param('national_id2');
126       $oldic =~ s/\s//g;
127       if ( $oldic =~ /^\w\d{9}$/ ) {
128         $new->national_id($oldic);
129       } else {
130         $error ||= "Illegal Old IC/Passport: ". $cgi->param('national_id2');
131       }
132     } else {
133       $error ||= 'Either NRIC or Old IC/Passport is required';
134     }
135     
136   } else {
137     warn "unknown national_id-country $id_country";
138   }
139 } elsif ( $cgi->param('national_id0') ) {
140   $new->national_id( $cgi->param('national_id0') );
141 }
142
143 my %usedatetime = ( 'birthdate'        => 1,
144                     'spouse_birthdate' => 1,
145                     'anniversary_date' => 1,
146                   );
147
148 foreach my $dfield (qw(
149   signupdate birthdate spouse_birthdate anniversary_date
150 )) {
151
152   if ( $cgi->param($dfield) && $cgi->param($dfield) =~ /^([ 0-9\-\/]{0,10})$/) {
153
154     my $value = $1;
155     my $parsed = '';
156
157     if ( exists $usedatetime{$dfield} && $usedatetime{$dfield} ) {
158
159       my $format = $conf->config('date_format') || "%m/%d/%Y";
160       my $parser = DateTime::Format::Strptime->new( pattern   => $format,
161                                                     time_zone => 'floating',
162                                                   );
163       my $dt = $parser->parse_datetime($value);
164       if ( $dt ) {
165         $parsed = $dt->epoch;
166       } else {
167         $error ||= "Invalid $dfield: $value";
168       }
169
170     } else {
171
172       $parsed = parse_datetime($value)
173         or $error ||= "Invalid $dfield: $value";
174
175     }
176
177     $new->setfield( $dfield, $parsed );
178     $cgi->param(    $dfield, $parsed );
179
180   }
181
182 }
183
184 $new->setfield('paid', $cgi->param('paid') )
185   if $cgi->param('paid');
186
187 my @exempt_groups = grep /\S/, $conf->config('tax-cust_exempt-groups');
188 my @tax_exempt = grep { $cgi->param("tax_$_") eq 'Y' } @exempt_groups;
189 my %tax_exempt = map { $_ => scalar($cgi->param("tax_$_".'_num')) } @tax_exempt;
190
191 #perhaps this stuff should go to cust_main.pm
192 if ( $new->custnum eq '' or $duplicate_of ) {
193
194   my $cust_pkg = '';
195   my $svc;
196
197   if ( $cgi->param('pkgpart_svcpart') ) {
198
199     my $x = $cgi->param('pkgpart_svcpart');
200     $x =~ /^(\d+)_(\d+)$/ or die "illegal pkgpart_svcpart $x\n";
201     my($pkgpart, $svcpart) = ($1, $2);
202     my $part_pkg = qsearchs('part_pkg', { 'pkgpart' => $pkgpart } );
203     #false laziness: copied from FS::cust_pkg::order (which should become a
204     #FS::cust_main method)
205     my(%part_pkg);
206     # generate %part_pkg
207     # $part_pkg{$pkgpart} is true iff $custnum may purchase $pkgpart
208     my $agent = qsearchs('agent',{'agentnum'=> $new->agentnum });
209
210     if ( $agent ) {
211       # $pkgpart_href->{PKGPART} is true iff $custnum may purchase $pkgpart
212       my $pkgpart_href = $agent->pkgpart_hashref
213         if $agent;
214       #eslaf
215
216       # this should wind up in FS::cust_pkg!
217       $error ||= "Agent ". $new->agentnum. " (type ". $agent->typenum.
218                  ") can't purchase pkgpart ". $pkgpart
219         #unless $part_pkg{ $pkgpart };
220         unless $pkgpart_href->{ $pkgpart }
221             || $agent->agentnum == $part_pkg->agentnum;
222     } else {
223       $error = 'Select agent';
224     }
225
226     $cust_pkg = new FS::cust_pkg ( {
227       #later         'custnum' => $custnum,
228       'pkgpart'     => $pkgpart,
229       'locationnum' => scalar($cgi->param('locationnum')),
230     } );
231
232
233     my $part_svc = qsearchs('part_svc', { 'svcpart' => $svcpart } );
234     my $svcdb = $part_svc->svcdb;
235
236     if ( $svcdb eq 'svc_acct' ) {
237
238       my %svc_acct = (
239                        'svcpart'   => $svcpart,
240                        'username'  => scalar($cgi->param('username')),
241                        '_password' => scalar($cgi->param('_password')),
242                        'popnum'    => scalar($cgi->param('popnum')),
243                      );
244       $svc_acct{'domsvc'} = $cgi->param('domsvc')
245         if $cgi->param('domsvc');
246
247       $svc = new FS::svc_acct \%svc_acct;
248
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'));
254
255     } elsif ( $svcdb eq 'svc_phone' ) {
256
257       my %svc_phone = (
258         'svcpart' => $svcpart,
259         map { $_ => scalar($cgi->param($_)) }
260             qw( countrycode phonenum sip_password pin phone_name )
261       );
262
263       $svc = new FS::svc_phone \%svc_phone;
264
265     } elsif ( $svcdb eq 'svc_dsl' ) {
266
267       my %svc_dsl = (
268         'svcpart' => $svcpart,
269         ( map { $_ => scalar($cgi->param("ship_$_")) || scalar($cgi->param($_))}
270               qw( first last company )
271         ),
272         ( map { $_ => scalar($cgi->param($_)) }
273               qw( loop_type phonenum password isp_chg isp_prev vendor_qual_id )
274         ),
275         'desired_due_date'  => time, #XXX enter?
276         'vendor_order_type' => 'NEW',
277       );
278       $svc = new FS::svc_dsl \%svc_dsl;
279
280     } else {
281       die "$svcdb not handled on new customer yet";
282     }
283
284   }
285
286
287   use Tie::RefHash;
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;
293   }
294   else {
295     # create the customer
296     $error ||= $new->insert( \%hash, \@invoicing_list,
297                            'tax_exemption'=> \%tax_exempt,
298                            'prospectnum'  => scalar($cgi->param('prospectnum')),
299                            );
300
301     my $conf = new FS::Conf;
302     if ( $conf->exists('backend-realtime') && ! $error ) {
303
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;
308
309     }
310   } #if $duplicate_of
311   
312 } else { #create old record object
313
314   my $old = qsearchs( 'cust_main', { 'custnum' => $new->custnum } ); 
315   $error ||= "Old record not found!" unless $old;
316
317   if ( length($old->paycvv) && $new->paycvv =~ /^\s*\*+\s*$/ ) {
318     $new->paycvv($old->paycvv);
319   }
320   if ($new->ss =~ /xx/) {
321     $new->ss($old->ss);
322   }
323   if ($new->stateid =~ /^xxx/) {
324     $new->stateid($old->stateid);
325   }
326   if ( $new->payby =~ /^(CARD|DCRD)$/
327        && (    $new->payinfo =~ /xx/
328             || $new->payinfo =~ /^\s*N\/A\s+\(tokenized\)\s*$/
329           )
330      )
331   {
332     $new->payinfo($old->payinfo);
333
334   } elsif ( $new->payby =~ /^(CHEK|DCHK)$/ && $new->payinfo =~ /xx/ ) {
335     #fix for #3085 "edit of customer's routing code only surprisingly causes
336     #nothing to happen...
337     # this probably won't do the right thing when we don't have the
338     # public key (can't actually get the real $old->payinfo)
339     my($new_account, $new_aba) = split('@', $new->payinfo);
340     my($old_account, $old_aba) = split('@', $old->payinfo);
341     $new_account = $old_account if $new_account =~ /xx/;
342     $new_aba     = $old_aba     if $new_aba     =~ /xx/;
343     $new->payinfo($new_account.'@'.$new_aba);
344   }
345
346   if ( ! $conf->exists('cust_main-edit_signupdate') or
347        ! $new->signupdate ) {
348     $new->signupdate($old->signupdate);
349   }
350
351   warn "$me calling $new -> replace( $old, \ @invoicing_list )" if $DEBUG;
352   local($FS::cust_main::DEBUG) = $DEBUG if $DEBUG;
353   local($FS::Record::DEBUG)    = $DEBUG if $DEBUG;
354
355   local($Data::Dumper::Sortkeys) = 1;
356   warn Dumper({ new => $new, old => $old }) if $DEBUG;
357
358   $error ||= $new->replace( $old, \@invoicing_list,
359                             'tax_exemption' => \%tax_exempt,
360                           );
361
362   warn "$me returned from replace" if $DEBUG;
363   
364 }
365
366 unless ( $error ) { #XXX i guess i should be transactional... all in the insert
367                     # or replace call
368   my @contact_fields = qw( classnum first last title comment emailaddress );
369   foreach my $phone_type ( qsearch({table=>'phone_type', order_by=>'weight'}) ) {
370     push @contact_fields, 'phonetypenum'.$phone_type->phonetypenum;
371   }
372
373   $error = $new->process_o2m( 'table'  => 'contact',
374                               'fields' => \@contact_fields,
375                               'params' => scalar($cgi->Vars),
376                             );
377 }
378
379 </%init>