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