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