8ee18214148c16c0be764590c755857256cc280f
[freeside.git] / httemplate / edit / process / quick-charge.cgi
1 % if ( $error ) {
2 %   $cgi->param('error', $error );
3 <% $cgi->redirect($p.'quick-charge.html?'. $cgi->query_string) %>
4 % } else {
5 <% header(emt($message)) %>
6   <SCRIPT TYPE="text/javascript">
7     window.top.location.reload();
8   </SCRIPT>
9   </BODY></HTML>
10 % }
11 <%init>
12
13 my $curuser = $FS::CurrentUser::CurrentUser;
14 die "access denied"
15   unless $curuser->access_right('One-time charge');
16
17 my $error = '';
18 my $conf = new FS::conf;
19 my $param = $cgi->Vars;
20
21 my @description = ();
22 for ( my $row = 0; exists($param->{"description$row"}); $row++ ) {
23   push @description, $param->{"description$row"}
24     if ($param->{"description$row"} =~ /\S/);
25 }
26
27 my( $cust_main, $prospect_main, $quotation ) = ( '', '', '' );
28 if ( $cgi->param('quotationnum') =~ /^(\d+)$/ ) {
29   $quotation = FS::quotation->by_key($1) or die "quotationnum $1 not found";
30 }
31 if ( $param->{"custnum"} =~ /^(\d+)$/ ) {
32   $cust_main = FS::cust_main->by_key($1) or die "custnum $1 not found";
33   exists($curuser->agentnums_href->{$cust_main->agentnum})
34     or die "access denied";
35 }
36 if ( $param->{"prospectnum"} =~ /^(\d+)$/ ) {
37   $prospect_main = FS::prospect_main->by_key($1) or die "prospectnum $1 not found";
38   exists($curuser->agentnums_href->{$prospect_main->agentnum})
39     or die "access denied";
40 }
41
42 my $message;
43
44 if ( $param->{'pkgnum'} =~ /^(\d+)$/ ) { #modifying an existing one-time charge
45   $message = "One-time charge changed";
46   my $pkgnum = $1;
47   die "access denied"
48     unless $curuser->access_right('Modify one-time charge');
49
50   my $cust_pkg = FS::cust_pkg->by_key($1)
51     or die "pkgnum $pkgnum not found";
52
53   my $part_pkg = $cust_pkg->part_pkg;
54   die "pkgnum $pkgnum is not a one-time charge" unless $part_pkg->freq eq '0';
55
56   my ($amount, $setup_cost, $quantity);
57   if ( $cgi->param('amount') =~ /^\s*(\d*(\.\d{1,2})*)\s*$/ ) {
58     $amount = sprintf('%.2f', $1);
59   }
60   if ( $cgi->param('setup_cost') =~ /^\s*(\d*(\.\d{1,2})*)\s*$/ ) {
61     $setup_cost = sprintf('%.2f', $1);
62   }
63   if ( $cgi->param('quantity') =~ /^\s*(\d*)\s*$/ ) {
64     $quantity = $1 || 1;
65   }
66
67   my $start_date = $cgi->param('start_date')
68                      ? parse_datetime($cgi->param('start_date'))
69                      : time;
70    
71   $param->{'tax_override'} =~ /^\s*([,\d]*)\s*$/
72     or $error .= "Illegal tax override " . $param->{"tax_override"} . "  ";
73   my $override = $1;
74  
75   if ( $param->{'taxclass'} eq '(select)' ) {
76     $error .= "Must select a tax class.  "
77       unless ($conf->config('tax_data_vendor') &&
78                ( $override || $param->{taxproductnum} )
79              );
80     $cgi->param('taxclass', '');
81   }
82
83   $error = $cust_pkg->modify_charge(
84       'pkg'               => scalar($cgi->param('pkg')),
85       'classnum'          => scalar($cgi->param('classnum')),
86       'additional'        => \@description,
87       'adjust_commission' => ($cgi->param('adjust_commission') ? 1 : 0),
88       'amount'            => $amount,
89       'setup_cost'        => $setup_cost,
90       'setuptax'          => scalar($cgi->param('setuptax')),
91       'taxclass'          => scalar($cgi->param('taxclass')),
92       'taxproductnum'     => scalar($cgi->param('taxproductnum')),
93       'tax_override'      => $override,
94       'quantity'          => $quantity,
95       'start_date'        => $start_date,
96       'separate_bill'     => scalar($cgi->param('separate_bill')),
97   );
98
99 } else { # the usual case: new one-time charge
100
101   $message = "One-time charge added";
102
103   $param->{"amount"} =~ /^\s*(\d*(?:\.?\d{1,2}))\s*$/
104     or $error .= "Illegal amount " . $param->{"amount"} . "  ";
105   my $amount = $1;
106
107   my $setup_cost = '';
108   if ( $param->{setup_cost} =~ /\S/ ) {
109     $param->{setup_cost} =~ /^\s*(\d*(?:\.?\d{1,2}))\s*$/
110       or $error .= "Illegal setup_cost " . $param->{setup_cost} . "  ";
111     $setup_cost = $1;
112   }
113
114   my $quantity = 1;
115   if ( $cgi->param('quantity') =~ /^\s*(\d+)\s*$/ ) {
116     $quantity = $1;
117   }
118
119   $param->{'tax_override'} =~ /^\s*([,\d]*)\s*$/
120     or $error .= "Illegal tax override " . $param->{"tax_override"} . "  ";
121   my $override = $1;
122
123   if ( $param->{'taxclass'} eq '(select)' ) {
124     $error .= "Must select a tax class.  "
125       unless ($conf->config('tax_data_vendor') &&
126                ( $override || $param->{taxproductnum} )
127              );
128     $cgi->param('taxclass', '');
129   }
130
131   my %charge = (
132     'amount'        => $amount,
133     'setup_cost'    => $setup_cost,
134     'quantity'      => $quantity,
135     'bill_now'      => scalar($cgi->param('bill_now')),
136     'invoice_terms' => scalar($cgi->param('invoice_terms')),
137     'start_date'    => ( scalar($cgi->param('start_date'))
138                            ? parse_datetime($cgi->param('start_date'))
139                            : ''
140                        ),
141     'no_auto'       => scalar($cgi->param('no_auto')),
142     'separate_bill' => scalar($cgi->param('separate_bill')),
143     'pkg'           => scalar($cgi->param('pkg')),
144     'setuptax'      => scalar($cgi->param('setuptax')),
145     'taxclass'      => scalar($cgi->param('taxclass')),
146     'taxproductnum' => scalar($cgi->param('taxproductnum')),
147     'tax_override'  => $override,
148     'classnum'      => scalar($cgi->param('classnum')),
149     'additional'    => \@description,
150   );
151
152   if ( $quotation ) {
153     $error ||= $quotation->charge( \%charge );
154   } else {
155     $error ||= $cust_main->charge( \%charge );
156   }
157
158 }
159
160 </%init>