add CASH and WEST payment types (payments only, not cust_main.payby)
[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 $new->setfield('paid', $cgi->param('paid') )
59   if $cgi->param('paid');
60
61 #perhaps this stuff should go to cust_main.pm
62 my $cust_pkg = '';
63 my $svc_acct = '';
64 if ( $new->custnum eq '' ) {
65
66   if ( $cgi->param('pkgpart_svcpart') ) {
67     my $x = $cgi->param('pkgpart_svcpart');
68     $x =~ /^(\d+)_(\d+)$/ or die "illegal pkgpart_svcpart $x\n";
69     my($pkgpart, $svcpart) = ($1, $2);
70     #false laziness: copied from FS::cust_pkg::order (which should become a
71     #FS::cust_main method)
72     my(%part_pkg);
73     # generate %part_pkg
74     # $part_pkg{$pkgpart} is true iff $custnum may purchase $pkgpart
75     my $agent = qsearchs('agent',{'agentnum'=> $new->agentnum });
76         #my($type_pkgs);
77         #foreach $type_pkgs ( qsearch('type_pkgs',{'typenum'=> $agent->typenum }) ) {
78         #  my($pkgpart)=$type_pkgs->pkgpart;
79         #  $part_pkg{$pkgpart}++;
80         #}
81     # $pkgpart_href->{PKGPART} is true iff $custnum may purchase $pkgpart
82     my $pkgpart_href = $agent->pkgpart_hashref;
83     #eslaf
84
85     # this should wind up in FS::cust_pkg!
86     $error ||= "Agent ". $new->agentnum. " (type ". $agent->typenum. ") can't ".
87                "purchase pkgpart ". $pkgpart
88       #unless $part_pkg{ $pkgpart };
89       unless $pkgpart_href->{ $pkgpart };
90
91     $cust_pkg = new FS::cust_pkg ( {
92       #later         'custnum' => $custnum,
93       'pkgpart' => $pkgpart,
94     } );
95     #$error ||= $cust_pkg->check;
96
97     #$cust_svc = new FS::cust_svc ( { 'svcpart' => $svcpart } );
98
99     #$error ||= $cust_svc->check;
100
101     $svc_acct = new FS::svc_acct ( {
102                                      'svcpart'   => $svcpart,
103                                      'username'  => $cgi->param('username'),
104                                      '_password' => $cgi->param('_password'),
105                                      'popnum'    => $cgi->param('popnum'),
106                                    } );
107
108     my $y = $svc_acct->setdefault; # arguably should be in new method
109     $error ||= $y unless ref($y);
110     #and just in case you were silly
111     $svc_acct->svcpart($svcpart);
112     $svc_acct->username($cgi->param('username'));
113     $svc_acct->_password($cgi->param('_password'));
114     $svc_acct->popnum($cgi->param('popnum'));
115
116     #$error ||= $svc_acct->check;
117
118   } elsif ( $cgi->param('username') ) { #good thing to catch
119     $error = "Can't assign username without a package!";
120   }
121
122   use Tie::RefHash;
123   tie my %hash, 'Tie::RefHash';
124   %hash = ( $cust_pkg => [ $svc_acct ] ) if $cust_pkg;
125   $error ||= $new->insert( \%hash, \@invoicing_list );
126 } else { #create old record object
127   my $old = qsearchs( 'cust_main', { 'custnum' => $new->custnum } ); 
128   $error ||= "Old record not found!" unless $old;
129   if ( defined dbdef->table('cust_main')->column('paycvv')
130        && length($old->paycvv)
131        && $new->paycvv =~ /^\s*\*+\s*$/ ) {
132     $new->paycvv($old->paycvv);
133   }
134   $error ||= $new->replace($old, \@invoicing_list);
135 }
136
137 if ( $error ) {
138   $cgi->param('error', $error);
139   print $cgi->redirect(popurl(2). "cust_main.cgi?". $cgi->query_string );
140 } else { 
141   print $cgi->redirect(popurl(3). "view/cust_main.cgi?". $new->custnum);
142
143 %>