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