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