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