fix not allowing "on-demand" card or ACH, RT#5238 RT#5237 RT#5230
[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 $error = '';
23
24 #unmunge stuff
25
26 $cgi->param('tax','') unless defined $cgi->param('tax');
27
28 $cgi->param('refnum', (split(/:/, ($cgi->param('refnum'))[0] ))[0] );
29
30 my $payby = $cgi->param('payby');
31
32 my %noauto = (
33   'CARD' => 'DCRD',
34   'CHEK' => 'DCHK',
35 );
36 $payby = $noauto{$payby}
37   if ! $cgi->param('payauto') && exists $noauto{$payby};
38
39 $cgi->param('payby', $payby);
40
41 if ( $payby ) {
42   if ( $payby eq 'CHEK' || $payby eq 'DCHK' ) {
43     $cgi->param('payinfo',
44       $cgi->param('payinfo1'). '@'. $cgi->param('payinfo2') );
45   }
46   $cgi->param('paydate',
47     $cgi->param( 'exp_month' ). '-'. $cgi->param( 'exp_year' ) );
48 }
49
50 my @invoicing_list = split( /\s*\,\s*/, $cgi->param('invoicing_list') );
51 push @invoicing_list, 'POST' if $cgi->param('invoicing_list_POST');
52 push @invoicing_list, 'FAX' if $cgi->param('invoicing_list_FAX');
53 $cgi->param('invoicing_list', join(',', @invoicing_list) );
54
55
56 #create new record object
57
58 my $new = new FS::cust_main ( {
59   map {
60     $_, scalar($cgi->param($_))
61 #  } qw(custnum agentnum last first ss company address1 address2 city county
62 #       state zip daytime night fax payby payinfo paydate payname tax
63 #       otaker refnum)
64   } fields('cust_main')
65 } );
66
67 if ( defined($cgi->param('same')) && $cgi->param('same') eq "Y" ) {
68   $new->setfield("ship_$_", '') foreach qw(
69     last first company address1 address2 city county state zip
70     country daytime night fax
71   );
72 }
73
74 if ( $cgi->param('birthdate') && $cgi->param('birthdate') =~ /^([ 0-9\-\/]{0,10})$/) {
75   my $conf = new FS::Conf;
76   my $format = $conf->config('date_format') || "%m/%d/%Y";
77   my $parser = DateTime::Format::Strptime->new(pattern => $format,
78                                                time_zone => 'floating',
79                                               );
80   my $dt =  $parser->parse_datetime($1);
81   if ($dt) {
82     $new->setfield('birthdate', $dt->epoch);
83     $cgi->param('birthdate', $dt->epoch);
84   } else {
85 #    $error ||= $cgi->param('birthdate') . " is an invalid birthdate:" . $parser->errmsg;
86     $error ||= "Invalid birthdate: " . $cgi->param('birthdate') . ".";
87     $cgi->param('birthdate', '');
88   }
89 }
90
91 $new->setfield('paid', $cgi->param('paid') )
92   if $cgi->param('paid');
93
94 #perhaps this stuff should go to cust_main.pm
95 if ( $new->custnum eq '' ) {
96
97   my $cust_pkg = '';
98   my $svc;
99
100   if ( $cgi->param('pkgpart_svcpart') ) {
101
102     my $x = $cgi->param('pkgpart_svcpart');
103     $x =~ /^(\d+)_(\d+)$/ or die "illegal pkgpart_svcpart $x\n";
104     my($pkgpart, $svcpart) = ($1, $2);
105     my $part_pkg = qsearchs('part_pkg', { 'pkgpart' => $pkgpart } );
106     #false laziness: copied from FS::cust_pkg::order (which should become a
107     #FS::cust_main method)
108     my(%part_pkg);
109     # generate %part_pkg
110     # $part_pkg{$pkgpart} is true iff $custnum may purchase $pkgpart
111     my $agent = qsearchs('agent',{'agentnum'=> $new->agentnum });
112
113     if ( $agent ) {
114       # $pkgpart_href->{PKGPART} is true iff $custnum may purchase $pkgpart
115       my $pkgpart_href = $agent->pkgpart_hashref
116         if $agent;
117       #eslaf
118
119       # this should wind up in FS::cust_pkg!
120       $error ||= "Agent ". $new->agentnum. " (type ". $agent->typenum.
121                  ") can't purchase pkgpart ". $pkgpart
122         #unless $part_pkg{ $pkgpart };
123         unless $pkgpart_href->{ $pkgpart }
124             || $agent->agentnum == $part_pkg->agentnum;
125     } else {
126       $error = 'Select agent';
127     }
128
129     $cust_pkg = new FS::cust_pkg ( {
130       #later         'custnum' => $custnum,
131       'pkgpart' => $pkgpart,
132     } );
133     #$error ||= $cust_pkg->check;
134
135     #$cust_svc = new FS::cust_svc ( { 'svcpart' => $svcpart } );
136
137     #$error ||= $cust_svc->check;
138
139     my $part_svc = qsearchs('part_svc', { 'svcpart' => $svcpart } );
140     my $svcdb = $part_svc->svcdb;
141
142     if ( $svcdb eq 'svc_acct' ) {
143
144       my %svc_acct = (
145                        'svcpart'   => $svcpart,
146                        'username'  => scalar($cgi->param('username')),
147                        '_password' => scalar($cgi->param('_password')),
148                        'popnum'    => scalar($cgi->param('popnum')),
149                      );
150       $svc_acct{'domsvc'} = $cgi->param('domsvc')
151         if $cgi->param('domsvc');
152
153       $svc = new FS::svc_acct \%svc_acct;
154
155       #and just in case you were silly
156       $svc->svcpart($svcpart);
157       $svc->username($cgi->param('username'));
158       $svc->_password($cgi->param('_password'));
159       $svc->popnum($cgi->param('popnum'));
160
161     } elsif ( $svcdb eq 'svc_phone' ) {
162
163       my %svc_phone = (
164                         'svcpart' => $svcpart,
165                         map { $_ => scalar($cgi->param($_)) }
166                           qw( countrycode phonenum sip_password pin phone_name )
167                       );
168
169       $svc = new FS::svc_phone \%svc_phone;
170
171     } else {
172       die "$svcdb not handled on new customer yet";
173     }
174
175     #$error ||= $svc_acct->check;
176
177   }
178
179   use Tie::RefHash;
180   tie my %hash, 'Tie::RefHash';
181   %hash = ( $cust_pkg => [ $svc ] ) if $cust_pkg;
182   $error ||= $new->insert( \%hash, \@invoicing_list );
183
184   my $conf = new FS::Conf;
185   if ( $conf->exists('backend-realtime') && ! $error ) {
186
187     my $berror =    $new->bill
188                  || $new->apply_payments_and_credits
189                  || $new->collect( 'realtime' => 1 );
190     warn "Warning, error billing during backend-realtime: $berror" if $berror;
191
192   }
193   
194 } else { #create old record object
195
196   my $old = qsearchs( 'cust_main', { 'custnum' => $new->custnum } ); 
197   $error ||= "Old record not found!" unless $old;
198   if ( length($old->paycvv) && $new->paycvv =~ /^\s*\*+\s*$/ ) {
199     $new->paycvv($old->paycvv);
200   }
201   if ($new->ss =~ /xx/) {
202     $new->ss($old->ss);
203   }
204   if ($new->stateid =~ /^xxx/) {
205     $new->stateid($old->stateid);
206   }
207   if ($new->payby =~ /^(CARD|DCRD)$/ && $new->payinfo =~ /xx/) {
208     $new->payinfo($old->payinfo);
209   } elsif ($new->payby =~ /^(CHEK|DCHK)$/ && $new->payinfo =~ /xx/) {
210     #fix for #3085 "edit of customer's routing code only surprisingly causes
211     #nothing to happen...
212     # this probably won't do the right thing when we don't have the
213     # public key (can't actually get the real $old->payinfo)
214     my($new_account, $new_aba) = split('@', $new->payinfo);
215     my($old_account, $old_aba) = split('@', $old->payinfo);
216     $new_account = $old_account if $new_account =~ /xx/;
217     $new_aba     = $old_aba     if $new_aba     =~ /xx/;
218     $new->payinfo($new_account.'@'.$new_aba);
219   }
220
221   warn "$me calling $new -> replace( $old, \ @invoicing_list )" if $DEBUG;
222   local($FS::cust_main::DEBUG) = $DEBUG if $DEBUG;
223   local($FS::Record::DEBUG)    = $DEBUG if $DEBUG;
224
225   $error ||= $new->replace($old, \@invoicing_list);
226
227   warn "$me returned from replace" if $DEBUG;
228   
229 }
230
231 </%init>