customer edit: abstract out billing info to billing.html, re-do payment type selectio...
[freeside.git] / httemplate / edit / process / cust_main.cgi
1 <%
2
3 warn join('', map { "$_ => ". $cgi->param($_). "\n" } $cgi->param );
4
5 my $error = '';
6
7 #unmunge stuff
8
9 $cgi->param('tax','') unless defined $cgi->param('tax');
10
11 $cgi->param('refnum', (split(/:/, ($cgi->param('refnum'))[0] ))[0] );
12
13 #my $payby = $cgi->param('payby');
14 my $payby = $cgi->param('select'); # XXX key
15
16 my %noauto = (
17   'CARD' => 'DCRD',
18   'CHEK' => 'DCHK',
19 );
20 $payby = $noauto{$payby}
21   if ! $cgi->param('payauto') && exists $noauto{$payby};
22
23 $cgi->param('payby', $payby);
24
25 if ( $payby ) {
26   if ( $payby eq 'CHEK' || $payby eq 'DCHK' ) {
27     $cgi->param('payinfo',
28       $cgi->param('payinfo1'). '@'. $cgi->param('payinfo2') );
29   }
30   $cgi->param('paydate',
31     $cgi->param( 'exp_month' ). '-'. $cgi->param( 'exp_year' ) );
32 }
33
34 my @invoicing_list = split( /\s*\,\s*/, $cgi->param('invoicing_list') );
35 push @invoicing_list, 'POST' if $cgi->param('invoicing_list_POST');
36 push @invoicing_list, 'FAX' if $cgi->param('invoicing_list_FAX');
37 $cgi->param('invoicing_list', join(',', @invoicing_list) );
38
39
40 #create new record object
41
42 my $new = new FS::cust_main ( {
43   map {
44     $_, scalar($cgi->param($_))
45 #  } qw(custnum agentnum last first ss company address1 address2 city county
46 #       state zip daytime night fax payby payinfo paydate payname tax
47 #       otaker refnum)
48   } fields('cust_main')
49 } );
50
51 if ( defined($cgi->param('same')) && $cgi->param('same') eq "Y" ) {
52   $new->setfield("ship_$_", '') foreach qw(
53     last first company address1 address2 city county state zip
54     country daytime night fax
55   );
56 }
57
58 #perhaps this stuff should go to cust_main.pm
59 my $cust_pkg = '';
60 my $svc_acct = '';
61 if ( $new->custnum eq '' ) {
62
63   if ( $cgi->param('pkgpart_svcpart') ) {
64     my $x = $cgi->param('pkgpart_svcpart');
65     $x =~ /^(\d+)_(\d+)$/ or die "illegal pkgpart_svcpart $x\n";
66     my($pkgpart, $svcpart) = ($1, $2);
67     #false laziness: copied from FS::cust_pkg::order (which should become a
68     #FS::cust_main method)
69     my(%part_pkg);
70     # generate %part_pkg
71     # $part_pkg{$pkgpart} is true iff $custnum may purchase $pkgpart
72     my $agent = qsearchs('agent',{'agentnum'=> $new->agentnum });
73         #my($type_pkgs);
74         #foreach $type_pkgs ( qsearch('type_pkgs',{'typenum'=> $agent->typenum }) ) {
75         #  my($pkgpart)=$type_pkgs->pkgpart;
76         #  $part_pkg{$pkgpart}++;
77         #}
78     # $pkgpart_href->{PKGPART} is true iff $custnum may purchase $pkgpart
79     my $pkgpart_href = $agent->pkgpart_hashref;
80     #eslaf
81
82     # this should wind up in FS::cust_pkg!
83     $error ||= "Agent ". $new->agentnum. " (type ". $agent->typenum. ") can't ".
84                "purchase pkgpart ". $pkgpart
85       #unless $part_pkg{ $pkgpart };
86       unless $pkgpart_href->{ $pkgpart };
87
88     $cust_pkg = new FS::cust_pkg ( {
89       #later         'custnum' => $custnum,
90       'pkgpart' => $pkgpart,
91     } );
92     #$error ||= $cust_pkg->check;
93
94     #$cust_svc = new FS::cust_svc ( { 'svcpart' => $svcpart } );
95
96     #$error ||= $cust_svc->check;
97
98     $svc_acct = new FS::svc_acct ( {
99                                      'svcpart'   => $svcpart,
100                                      'username'  => $cgi->param('username'),
101                                      '_password' => $cgi->param('_password'),
102                                      'popnum'    => $cgi->param('popnum'),
103                                    } );
104
105     my $y = $svc_acct->setdefault; # arguably should be in new method
106     $error ||= $y unless ref($y);
107     #and just in case you were silly
108     $svc_acct->svcpart($svcpart);
109     $svc_acct->username($cgi->param('username'));
110     $svc_acct->_password($cgi->param('_password'));
111     $svc_acct->popnum($cgi->param('popnum'));
112
113     #$error ||= $svc_acct->check;
114
115   } elsif ( $cgi->param('username') ) { #good thing to catch
116     $error = "Can't assign username without a package!";
117   }
118
119   use Tie::RefHash;
120   tie my %hash, 'Tie::RefHash';
121   %hash = ( $cust_pkg => [ $svc_acct ] ) if $cust_pkg;
122   $error ||= $new->insert( \%hash, \@invoicing_list );
123 } else { #create old record object
124   my $old = qsearchs( 'cust_main', { 'custnum' => $new->custnum } ); 
125   $error ||= "Old record not found!" unless $old;
126   if ( defined dbdef->table('cust_main')->column('paycvv')
127        && length($old->paycvv)
128        && $new->paycvv =~ /^\s*\*+\s*$/ ) {
129     $new->paycvv($old->paycvv);
130   }
131   $error ||= $new->replace($old, \@invoicing_list);
132 }
133
134 if ( $error ) {
135   $cgi->param('error', $error);
136   print $cgi->redirect(popurl(2). "cust_main.cgi?". $cgi->query_string );
137 } else { 
138   print $cgi->redirect(popurl(3). "view/cust_main.cgi?". $new->custnum);
139
140 %>