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