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
61 #create new record object
62
63 my $new = new FS::cust_main ( {
64   map {
65     $_, scalar($cgi->param($_))
66   } fields('cust_main')
67 } );
68
69 $new->invoice_noemail( ($cgi->param('invoice_email') eq 'Y') ? '' : 'Y' );
70
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)";
79 }
80
81 if ( defined($cgi->param('same')) && $cgi->param('same') eq "Y" ) {
82   $new->setfield("ship_$_", '') foreach qw(
83     last first company address1 address2 city county state zip
84     country daytime night fax
85   );
86 }
87
88 if ( $cgi->param('no_credit_limit') ) {
89   $new->setfield('credit_limit', '');
90 }
91
92 $new->tagnum( [ $cgi->param('tagnum') ] );
93
94 if ( my $id_country = $conf->config('national_id-country') ) {
95   if ( $id_country eq 'MY' ) {
96
97     if ( $cgi->param('national_id1') =~ /\S/ ) {
98       my $nric = $cgi->param('national_id1');
99       $nric =~ s/\s//g;
100       if ( $nric =~ /^(\d{6})\-?(\d{2})\-?(\d{4})$/ ) {
101         $new->national_id( "$1-$2-$3" );
102       } else {
103         $error ||= "Illegal NRIC: ". $cgi->param('national_id1');
104       }
105     } elsif ( $cgi->param('national_id2') =~ /\S/ ) {
106       my $oldic = $cgi->param('national_id2');
107       $oldic =~ s/\s//g;
108       if ( $oldic =~ /^\w\d{9}$/ ) {
109         $new->national_id($oldic);
110       } else {
111         $error ||= "Illegal Old IC/Passport: ". $cgi->param('national_id2');
112       }
113     } else {
114       $error ||= 'Either NRIC or Old IC/Passport is required';
115     }
116     
117   } else {
118     warn "unknown national_id-country $id_country";
119   }
120 } elsif ( $cgi->param('national_id0') ) {
121   $new->national_id( $cgi->param('national_id0') );
122 }
123
124 my %usedatetime = ( 'birthdate'        => 1,
125                     'spouse_birthdate' => 1,
126                     'anniversary_date' => 1,
127                   );
128
129 foreach my $dfield (qw(
130   signupdate birthdate spouse_birthdate anniversary_date
131 )) {
132
133   if ( $cgi->param($dfield) && $cgi->param($dfield) =~ /^([ 0-9\-\/]{0,10})$/) {
134
135     my $value = $1;
136     my $parsed = '';
137
138     if ( exists $usedatetime{$dfield} && $usedatetime{$dfield} ) {
139
140       my $format = $conf->config('date_format') || "%m/%d/%Y";
141       my $parser = DateTime::Format::Strptime->new( pattern   => $format,
142                                                     time_zone => 'floating',
143                                                   );
144       my $dt = $parser->parse_datetime($value);
145       if ( $dt ) {
146         $parsed = $dt->epoch;
147       } else {
148         $error ||= "Invalid $dfield: $value";
149       }
150
151     } else {
152
153       $parsed = parse_datetime($value)
154         or $error ||= "Invalid $dfield: $value";
155
156     }
157
158     $new->setfield( $dfield, $parsed );
159     $cgi->param(    $dfield, $parsed );
160
161   }
162
163 }
164
165 $new->setfield('paid', $cgi->param('paid') )
166   if $cgi->param('paid');
167
168 my @exempt_groups = grep /\S/, $conf->config('tax-cust_exempt-groups');
169 my @tax_exempt = grep { $cgi->param("tax_$_") eq 'Y' } @exempt_groups;
170 my %tax_exempt = map { $_ => scalar($cgi->param("tax_$_".'_num')) } @tax_exempt;
171
172 #perhaps this stuff should go to cust_main.pm
173 if ( $new->custnum eq '' or $duplicate_of ) {
174
175   my $cust_pkg = '';
176   my $svc;
177
178   if ( $cgi->param('pkgpart_svcpart') ) {
179
180     my $x = $cgi->param('pkgpart_svcpart');
181     $x =~ /^(\d+)_(\d+)$/ or die "illegal pkgpart_svcpart $x\n";
182     my($pkgpart, $svcpart) = ($1, $2);
183     my $part_pkg = qsearchs('part_pkg', { 'pkgpart' => $pkgpart } );
184     #false laziness: copied from FS::cust_pkg::order (which should become a
185     #FS::cust_main method)
186     my(%part_pkg);
187     # generate %part_pkg
188     # $part_pkg{$pkgpart} is true iff $custnum may purchase $pkgpart
189     my $agent = qsearchs('agent',{'agentnum'=> $new->agentnum });
190
191     if ( $agent ) {
192       # $pkgpart_href->{PKGPART} is true iff $custnum may purchase $pkgpart
193       my $pkgpart_href = $agent->pkgpart_hashref
194         if $agent;
195       #eslaf
196
197       # this should wind up in FS::cust_pkg!
198       $error ||= "Agent ". $new->agentnum. " (type ". $agent->typenum.
199                  ") can't purchase pkgpart ". $pkgpart
200         #unless $part_pkg{ $pkgpart };
201         unless $pkgpart_href->{ $pkgpart }
202             || $agent->agentnum == $part_pkg->agentnum;
203     } else {
204       $error = 'Select agent';
205     }
206
207     $cust_pkg = new FS::cust_pkg ( {
208       #later         'custnum' => $custnum,
209       'pkgpart'     => $pkgpart,
210       'locationnum' => scalar($cgi->param('locationnum')),
211     } );
212
213
214     my $part_svc = qsearchs('part_svc', { 'svcpart' => $svcpart } );
215     my $svcdb = $part_svc->svcdb;
216
217     if ( $svcdb eq 'svc_acct' ) {
218
219       my %svc_acct = (
220                        'svcpart'   => $svcpart,
221                        'username'  => scalar($cgi->param('username')),
222                        '_password' => scalar($cgi->param('_password')),
223                        'popnum'    => scalar($cgi->param('popnum')),
224                      );
225       $svc_acct{'domsvc'} = $cgi->param('domsvc')
226         if $cgi->param('domsvc');
227
228       $svc = new FS::svc_acct \%svc_acct;
229
230       #and just in case you were silly
231       $svc->svcpart($svcpart);
232       $svc->username($cgi->param('username'));
233       $svc->_password($cgi->param('_password'));
234       $svc->popnum($cgi->param('popnum'));
235
236     } elsif ( $svcdb eq 'svc_phone' ) {
237
238       my %svc_phone = (
239         'svcpart' => $svcpart,
240         map { $_ => scalar($cgi->param($_)) }
241             qw( countrycode phonenum sip_password pin phone_name )
242       );
243
244       $svc = new FS::svc_phone \%svc_phone;
245
246     } elsif ( $svcdb eq 'svc_dsl' ) {
247
248       my %svc_dsl = (
249         'svcpart' => $svcpart,
250         ( map { $_ => scalar($cgi->param("ship_$_")) || scalar($cgi->param($_))}
251               qw( first last company )
252         ),
253         ( map { $_ => scalar($cgi->param($_)) }
254               qw( loop_type phonenum password isp_chg isp_prev vendor_qual_id )
255         ),
256         'desired_due_date'  => time, #XXX enter?
257         'vendor_order_type' => 'NEW',
258       );
259       $svc = new FS::svc_dsl \%svc_dsl;
260
261     } else {
262       die "$svcdb not handled on new customer yet";
263     }
264
265   }
266
267
268   use Tie::RefHash;
269   tie my %hash, 'Tie::RefHash';
270   %hash = ( $cust_pkg => [ $svc ] ) if $cust_pkg;
271   if ( $duplicate_of ) {
272     # order the package and service normally
273     $error ||= $new->order_pkgs( \%hash ) if $cust_pkg;
274   }
275   else {
276     # create the customer
277     $error ||= $new->insert( \%hash, \@invoicing_list,
278                            'tax_exemption'=> \%tax_exempt,
279                            'prospectnum'  => scalar($cgi->param('prospectnum')),
280                            );
281
282     my $conf = new FS::Conf;
283     if ( $conf->exists('backend-realtime') && ! $error ) {
284
285       my $berror =    $new->bill
286                    || $new->apply_payments_and_credits
287                    || $new->collect( 'realtime' => 1 );
288       warn "Warning, error billing during backend-realtime: $berror" if $berror;
289
290     }
291   } #if $duplicate_of
292   
293 } else { #create old record object
294
295   my $old = qsearchs( 'cust_main', { 'custnum' => $new->custnum } ); 
296   $error ||= "Old record not found!" unless $old;
297   if ( length($old->paycvv) && $new->paycvv =~ /^\s*\*+\s*$/ ) {
298     $new->paycvv($old->paycvv);
299   }
300   if ($new->ss =~ /xx/) {
301     $new->ss($old->ss);
302   }
303   if ($new->stateid =~ /^xxx/) {
304     $new->stateid($old->stateid);
305   }
306   if ( $new->payby =~ /^(CARD|DCRD)$/
307        && (    $new->payinfo =~ /xx/
308             || $new->payinfo =~ /^\s*N\/A\s+\(tokenized\)\s*$/
309           )
310      )
311   {
312     $new->payinfo($old->payinfo);
313
314   } elsif ( $new->payby =~ /^(CHEK|DCHK)$/ && $new->payinfo =~ /xx/ ) {
315     #fix for #3085 "edit of customer's routing code only surprisingly causes
316     #nothing to happen...
317     # this probably won't do the right thing when we don't have the
318     # public key (can't actually get the real $old->payinfo)
319     my($new_account, $new_aba) = split('@', $new->payinfo);
320     my($old_account, $old_aba) = split('@', $old->payinfo);
321     $new_account = $old_account if $new_account =~ /xx/;
322     $new_aba     = $old_aba     if $new_aba     =~ /xx/;
323     $new->payinfo($new_account.'@'.$new_aba);
324   }
325
326   if ( ! $conf->exists('cust_main-edit_signupdate') or
327        ! $new->signupdate ) {
328     $new->signupdate($old->signupdate);
329   }
330
331   warn "$me calling $new -> replace( $old, \ @invoicing_list )" if $DEBUG;
332   local($FS::cust_main::DEBUG) = $DEBUG if $DEBUG;
333   local($FS::Record::DEBUG)    = $DEBUG if $DEBUG;
334
335   $error ||= $new->replace( $old, \@invoicing_list,
336                             'tax_exemption' => \%tax_exempt,
337                           );
338
339   warn "$me returned from replace" if $DEBUG;
340   
341 }
342
343 </%init>