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