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