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