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 # 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 # if this is enabled, enforce it
66 if ( $conf->exists('agent-ship_address', $cgi->param('agentnum')) ) {
67   my $agent = FS::agent->by_key($cgi->param('agentnum'));
68   my $agent_cust_main = $agent->agent_cust_main;
69   if ( $agent_cust_main ) {
70     my $agent_location = $agent_cust_main->ship_location;
71     foreach (qw(address1 city state zip country latitude longitude district)) {
72       $cgi->param("ship_$_", $agent_location->get($_));
73     }
74   }
75 }
76
77 my %locations;
78 for my $pre (qw(bill ship)) {
79
80   my %hash;
81   foreach ( FS::cust_main->location_fields ) {
82     $hash{$_} = scalar($cgi->param($pre.'_'.$_));
83   }
84   $hash{'custnum'} = $cgi->param('custnum');
85   warn Dumper \%hash if $DEBUG;
86   $locations{$pre} = FS::cust_location->new(\%hash);
87 }
88
89 if ( ($cgi->param('same') || '') eq 'Y' ) {
90   $locations{ship} = $locations{bill};
91 }
92
93 #create new record object
94 # but explicitly avoid setting ship_ fields
95
96 my $new = new FS::cust_main ( {
97   map { ( $_, scalar($cgi->param($_)) ) } (fields('cust_main')),
98   map { ( "ship_$_", '' ) } (FS::cust_main->location_fields)
99 } );
100
101 $new->invoice_noemail( ($cgi->param('invoice_email') eq 'Y') ? '' : 'Y' );
102
103 if ( $duplicate_of ) {
104   # then negate all changes to the customer; the only change we should
105   # make is to order a package, if requested
106   $new = qsearchs('cust_main', { 'custnum' => $duplicate_of })
107   # this should never happen
108     or die "nonexistent existing customer (custnum $duplicate_of)";
109 }
110
111 for my $pre (qw(bill ship)) {
112   $new->set($pre.'_location', $locations{$pre});
113   $new->set($pre.'_locationnum', $locations{$pre}->locationnum);
114 }
115
116 if ( $cgi->param('no_credit_limit') ) {
117   $new->setfield('credit_limit', '');
118 }
119
120 $new->tagnum( [ $cgi->param('tagnum') ] );
121
122 $error ||= $new->set_national_id_from_cgi( $cgi );
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 %options = ();
169 if ( $curuser->access_right('Edit customer tax exemptions') ) { 
170   my @exempt_groups = grep /\S/, $conf->config('tax-cust_exempt-groups');
171   my @tax_exempt = grep { $cgi->param("tax_$_") eq 'Y' } @exempt_groups;
172   $options{'tax_exemption'} = {
173     map { $_ => scalar($cgi->param("tax_$_".'_num')) } @tax_exempt
174   };
175 }
176
177 #perhaps this stuff should go to cust_main.pm
178 if ( $new->custnum eq '' or $duplicate_of ) {
179
180   my $cust_pkg = '';
181   my $svc;
182
183   if ( $cgi->param('pkgpart_svcpart') ) {
184
185     my $x = $cgi->param('pkgpart_svcpart');
186     $x =~ /^(\d+)_(\d+)$/ or die "illegal pkgpart_svcpart $x\n";
187     my($pkgpart, $svcpart) = ($1, $2);
188     my $part_pkg = qsearchs('part_pkg', { 'pkgpart' => $pkgpart } );
189     #false laziness: copied from FS::cust_pkg::order (which should become a
190     #FS::cust_main method)
191     my(%part_pkg);
192     # generate %part_pkg
193     # $part_pkg{$pkgpart} is true iff $custnum may purchase $pkgpart
194     my $agent = qsearchs('agent',{'agentnum'=> $new->agentnum });
195
196     if ( $agent ) {
197       # $pkgpart_href->{PKGPART} is true iff $custnum may purchase $pkgpart
198       my $pkgpart_href = $agent->pkgpart_hashref
199         if $agent;
200       #eslaf
201
202       # this should wind up in FS::cust_pkg!
203       $error ||= "Agent ". $new->agentnum. " (type ". $agent->typenum.
204                  ") can't purchase pkgpart ". $pkgpart
205         #unless $part_pkg{ $pkgpart };
206         unless $pkgpart_href->{ $pkgpart }
207             || $agent->agentnum == $part_pkg->agentnum;
208     } else {
209       $error = 'Select agent';
210     }
211
212     $cust_pkg = new FS::cust_pkg ( {
213       #later         'custnum' => $custnum,
214       'pkgpart'     => $pkgpart,
215       'locationnum' => scalar($cgi->param('locationnum')),
216       'salesnum'    => scalar($cgi->param('salesnum')),
217     } );
218
219
220     my $part_svc = qsearchs('part_svc', { 'svcpart' => $svcpart } );
221     my $svcdb = $part_svc->svcdb;
222
223     if ( $svcdb eq 'svc_acct' ) {
224
225       my %svc_acct = (
226                        'svcpart'   => $svcpart,
227                        'username'  => scalar($cgi->param('username')),
228                        '_password' => scalar($cgi->param('_password')),
229                        'popnum'    => scalar($cgi->param('popnum')),
230                      );
231       $svc_acct{'domsvc'} = $cgi->param('domsvc')
232         if $cgi->param('domsvc');
233
234       $svc = new FS::svc_acct \%svc_acct;
235
236       #and just in case you were silly
237       $svc->svcpart($svcpart);
238       $svc->username($cgi->param('username'));
239       $svc->_password($cgi->param('_password'));
240       $svc->popnum($cgi->param('popnum'));
241
242     } elsif ( $svcdb eq 'svc_phone' ) {
243
244       my %svc_phone = (
245         'svcpart' => $svcpart,
246         map { $_ => scalar($cgi->param($_)) }
247             qw( countrycode phonenum sip_password pin phone_name )
248       );
249
250       $svc = new FS::svc_phone \%svc_phone;
251
252     } elsif ( $svcdb eq 'svc_dsl' ) {
253
254       my %svc_dsl = (
255         'svcpart' => $svcpart,
256         ( map { $_ => scalar($cgi->param("ship_$_")) || scalar($cgi->param($_))}
257               qw( first last company )
258         ),
259         ( map { $_ => scalar($cgi->param($_)) }
260               qw( loop_type phonenum password isp_chg isp_prev vendor_qual_id )
261         ),
262         'desired_due_date'  => time, #XXX enter?
263         'vendor_order_type' => 'NEW',
264       );
265       $svc = new FS::svc_dsl \%svc_dsl;
266
267     } else {
268       die "$svcdb not handled on new customer yet";
269     }
270
271   }
272
273
274   use Tie::RefHash;
275   tie my %hash, 'Tie::RefHash';
276   %hash = ( $cust_pkg => [ $svc ] ) if $cust_pkg;
277   if ( $duplicate_of ) {
278     # order the package and service normally
279     $error ||= $new->order_pkgs( \%hash ) if $cust_pkg;
280   }
281   else {
282     # create the customer
283     $error ||= $new->insert( \%hash, \@invoicing_list,
284                              %options,
285                              prospectnum => scalar($cgi->param('prospectnum')),
286                            );
287
288     my $conf = new FS::Conf;
289     if ( $conf->exists('backend-realtime') && ! $error ) {
290
291       my $berror =    $new->bill
292                    || $new->apply_payments_and_credits
293                    || $new->collect( 'realtime' => 1 );
294       warn "Warning, error billing during backend-realtime: $berror" if $berror;
295
296     }
297   } #if $duplicate_of
298   
299 } else { #create old record object
300
301   my $old = qsearchs( 'cust_main', { 'custnum' => $new->custnum } ); 
302   $error ||= "Old record not found!" unless $old;
303
304   if ( length($old->paycvv) && $new->paycvv =~ /^\s*\*+\s*$/ ) {
305     $new->paycvv($old->paycvv);
306   }
307   if ($new->ss =~ /xx/) {
308     $new->ss($old->ss);
309   }
310   if ($new->stateid =~ /^xxx/) {
311     $new->stateid($old->stateid);
312   }
313   if ( $new->payby =~ /^(CARD|DCRD)$/
314        && (    $new->payinfo =~ /xx/
315             || $new->payinfo =~ /^\s*N\/A\s+\(tokenized\)\s*$/
316           )
317      )
318   {
319     $new->payinfo($old->payinfo);
320
321   } elsif ( $new->payby =~ /^(CHEK|DCHK)$/ && $new->payinfo =~ /xx/ ) {
322     #fix for #3085 "edit of customer's routing code only surprisingly causes
323     #nothing to happen...
324     # this probably won't do the right thing when we don't have the
325     # public key (can't actually get the real $old->payinfo)
326     my($new_account, $new_aba) = split('@', $new->payinfo);
327     my($old_account, $old_aba) = split('@', $old->payinfo);
328     $new_account = $old_account if $new_account =~ /xx/;
329     $new_aba     = $old_aba     if $new_aba     =~ /xx/;
330     $new->payinfo($new_account.'@'.$new_aba);
331   }
332
333   if ( ! $conf->exists('cust_main-edit_signupdate') or
334        ! $new->signupdate ) {
335     $new->signupdate($old->signupdate);
336   }
337
338   warn "$me calling $new -> replace( $old, \ @invoicing_list )" if $DEBUG;
339   local($FS::cust_main::DEBUG) = $DEBUG if $DEBUG;
340   local($FS::Record::DEBUG)    = $DEBUG if $DEBUG;
341
342   local($Data::Dumper::Sortkeys) = 1;
343   warn Dumper({ new => $new, old => $old }) if $DEBUG;
344
345   $error ||= $new->replace( $old, \@invoicing_list,
346                             %options,
347                           );
348
349   warn "$me returned from replace" if $DEBUG;
350   
351 }
352
353 unless ( $error ) { #XXX i guess i should be transactional... all in the insert
354                     # or replace call
355   my @contact_fields = qw( classnum first last title comment emailaddress );
356   foreach my $phone_type ( qsearch({table=>'phone_type', order_by=>'weight'}) ) {
357     push @contact_fields, 'phonetypenum'.$phone_type->phonetypenum;
358   }
359
360   $error = $new->process_o2m( 'table'  => 'contact',
361                               'fields' => \@contact_fields,
362                               'params' => scalar($cgi->Vars),
363                             );
364 }
365
366 </%init>