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