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