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