fix date parsing when using international dates (package date edit), RT#8027
[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     $cgi->param('payinfo',
46       $cgi->param('payinfo1'). '@'. $cgi->param('payinfo2') );
47   }
48   $cgi->param('paydate',
49     $cgi->param( 'exp_month' ). '-'. $cgi->param( 'exp_year' ) );
50 }
51
52 my @invoicing_list = split( /\s*\,\s*/, $cgi->param('invoicing_list') );
53 push @invoicing_list, 'POST' if $cgi->param('invoicing_list_POST');
54 push @invoicing_list, 'FAX' if $cgi->param('invoicing_list_FAX');
55 $cgi->param('invoicing_list', join(',', @invoicing_list) );
56
57
58 #create new record object
59
60 my $new = new FS::cust_main ( {
61   map {
62     $_, scalar($cgi->param($_))
63 #  } qw(custnum agentnum last first ss company address1 address2 city county
64 #       state zip daytime night fax payby payinfo paydate payname tax
65 #       otaker refnum)
66   } fields('cust_main')
67 } );
68
69 if ( defined($cgi->param('same')) && $cgi->param('same') eq "Y" ) {
70   $new->setfield("ship_$_", '') foreach qw(
71     last first company address1 address2 city county state zip
72     country daytime night fax
73   );
74 }
75
76 my %usedatetime = ( 'birthdate' => 1 );
77
78 foreach my $dfield (qw( birthdate signupdate )) {
79
80   if ( $cgi->param($dfield) && $cgi->param($dfield) =~ /^([ 0-9\-\/]{0,10})$/) {
81
82     my $value = $1;
83     my $parsed = '';
84
85     if ( exists $usedatetime{$dfield} && $usedatetime{$dfield} ) {
86
87       my $format = $conf->config('date_format') || "%m/%d/%Y";
88       my $parser = DateTime::Format::Strptime->new( pattern   => $format,
89                                                     time_zone => 'floating',
90                                                   );
91       my $dt = $parser->parse_datetime($value);
92       if ( $dt ) {
93         $parsed = $dt->epoch;
94       } else {
95     #    $error ||= $cgi->param('birthdate') . " is an invalid birthdate:" . $parser->errmsg;
96         $error ||= "Invalid $dfield: $value";
97       }
98
99     } else {
100
101       $parsed = parse_datetime($value)
102         or $error ||= "Invalid $dfield: $value";
103
104     }
105
106     $new->setfield( $dfield, $parsed );
107     $cgi->param(    $dfield, $parsed );
108
109   }
110
111 }
112
113 $new->setfield('paid', $cgi->param('paid') )
114   if $cgi->param('paid');
115
116 my @exempt_groups = grep /\S/, $conf->config('tax-cust_exempt-groups');
117 my @tax_exempt = grep { $cgi->param("tax_$_") eq 'Y' } @exempt_groups;
118
119 #perhaps this stuff should go to cust_main.pm
120 if ( $new->custnum eq '' ) {
121
122   my $cust_pkg = '';
123   my $svc;
124
125   if ( $cgi->param('pkgpart_svcpart') ) {
126
127     my $x = $cgi->param('pkgpart_svcpart');
128     $x =~ /^(\d+)_(\d+)$/ or die "illegal pkgpart_svcpart $x\n";
129     my($pkgpart, $svcpart) = ($1, $2);
130     my $part_pkg = qsearchs('part_pkg', { 'pkgpart' => $pkgpart } );
131     #false laziness: copied from FS::cust_pkg::order (which should become a
132     #FS::cust_main method)
133     my(%part_pkg);
134     # generate %part_pkg
135     # $part_pkg{$pkgpart} is true iff $custnum may purchase $pkgpart
136     my $agent = qsearchs('agent',{'agentnum'=> $new->agentnum });
137
138     if ( $agent ) {
139       # $pkgpart_href->{PKGPART} is true iff $custnum may purchase $pkgpart
140       my $pkgpart_href = $agent->pkgpart_hashref
141         if $agent;
142       #eslaf
143
144       # this should wind up in FS::cust_pkg!
145       $error ||= "Agent ". $new->agentnum. " (type ". $agent->typenum.
146                  ") can't purchase pkgpart ". $pkgpart
147         #unless $part_pkg{ $pkgpart };
148         unless $pkgpart_href->{ $pkgpart }
149             || $agent->agentnum == $part_pkg->agentnum;
150     } else {
151       $error = 'Select agent';
152     }
153
154     $cust_pkg = new FS::cust_pkg ( {
155       #later         'custnum' => $custnum,
156       'pkgpart' => $pkgpart,
157     } );
158     #$error ||= $cust_pkg->check;
159
160     #$cust_svc = new FS::cust_svc ( { 'svcpart' => $svcpart } );
161
162     #$error ||= $cust_svc->check;
163
164     my $part_svc = qsearchs('part_svc', { 'svcpart' => $svcpart } );
165     my $svcdb = $part_svc->svcdb;
166
167     if ( $svcdb eq 'svc_acct' ) {
168
169       my %svc_acct = (
170                        'svcpart'   => $svcpart,
171                        'username'  => scalar($cgi->param('username')),
172                        '_password' => scalar($cgi->param('_password')),
173                        'popnum'    => scalar($cgi->param('popnum')),
174                      );
175       $svc_acct{'domsvc'} = $cgi->param('domsvc')
176         if $cgi->param('domsvc');
177
178       $svc = new FS::svc_acct \%svc_acct;
179
180       #and just in case you were silly
181       $svc->svcpart($svcpart);
182       $svc->username($cgi->param('username'));
183       $svc->_password($cgi->param('_password'));
184       $svc->popnum($cgi->param('popnum'));
185
186     } elsif ( $svcdb eq 'svc_phone' ) {
187
188       my %svc_phone = (
189                         'svcpart' => $svcpart,
190                         map { $_ => scalar($cgi->param($_)) }
191                           qw( countrycode phonenum sip_password pin phone_name )
192                       );
193
194       $svc = new FS::svc_phone \%svc_phone;
195
196     } else {
197       die "$svcdb not handled on new customer yet";
198     }
199
200     #$error ||= $svc_acct->check;
201
202   }
203
204   use Tie::RefHash;
205   tie my %hash, 'Tie::RefHash';
206   %hash = ( $cust_pkg => [ $svc ] ) if $cust_pkg;
207   $error ||= $new->insert( \%hash, \@invoicing_list,
208                            'tax_exemption' => \@tax_exempt,
209                          );
210
211   my $conf = new FS::Conf;
212   if ( $conf->exists('backend-realtime') && ! $error ) {
213
214     my $berror =    $new->bill
215                  || $new->apply_payments_and_credits
216                  || $new->collect( 'realtime' => 1 );
217     warn "Warning, error billing during backend-realtime: $berror" if $berror;
218
219   }
220   
221 } else { #create old record object
222
223   my $old = qsearchs( 'cust_main', { 'custnum' => $new->custnum } ); 
224   $error ||= "Old record not found!" unless $old;
225   if ( length($old->paycvv) && $new->paycvv =~ /^\s*\*+\s*$/ ) {
226     $new->paycvv($old->paycvv);
227   }
228   if ($new->ss =~ /xx/) {
229     $new->ss($old->ss);
230   }
231   if ($new->stateid =~ /^xxx/) {
232     $new->stateid($old->stateid);
233   }
234   if ($new->payby =~ /^(CARD|DCRD)$/ && $new->payinfo =~ /xx/) {
235     $new->payinfo($old->payinfo);
236   } elsif ($new->payby =~ /^(CHEK|DCHK)$/ && $new->payinfo =~ /xx/) {
237     #fix for #3085 "edit of customer's routing code only surprisingly causes
238     #nothing to happen...
239     # this probably won't do the right thing when we don't have the
240     # public key (can't actually get the real $old->payinfo)
241     my($new_account, $new_aba) = split('@', $new->payinfo);
242     my($old_account, $old_aba) = split('@', $old->payinfo);
243     $new_account = $old_account if $new_account =~ /xx/;
244     $new_aba     = $old_aba     if $new_aba     =~ /xx/;
245     $new->payinfo($new_account.'@'.$new_aba);
246   }
247
248   warn "$me calling $new -> replace( $old, \ @invoicing_list )" if $DEBUG;
249   local($FS::cust_main::DEBUG) = $DEBUG if $DEBUG;
250   local($FS::Record::DEBUG)    = $DEBUG if $DEBUG;
251
252   $error ||= $new->replace( $old, \@invoicing_list,
253                             'tax_exemption' => \@tax_exempt,
254                           );
255
256   warn "$me returned from replace" if $DEBUG;
257   
258 }
259
260 </%init>