costs for one-time charges, RT#31429
[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 $param->{"custnum"} =~ /^(\d+)$/
28   or $error .= "Illegal customer number " . $param->{"custnum"} . "  ";
29 my $custnum = $1;
30
31 my $cust_main = FS::cust_main->by_key($custnum)
32   or die "custnum $custnum not found";
33
34 exists($curuser->agentnums_href->{$cust_main->agentnum})
35   or die "access denied";
36
37 my $message;
38
39 if ( $param->{'pkgnum'} =~ /^(\d+)$/ ) { #modifying an existing one-time charge
40   $message = "One-time charge changed";
41   my $pkgnum = $1;
42   die "access denied"
43     unless $curuser->access_right('Modify one-time charge');
44
45   my $cust_pkg = FS::cust_pkg->by_key($1)
46     or die "pkgnum $pkgnum not found";
47
48   my $part_pkg = $cust_pkg->part_pkg;
49   die "pkgnum $pkgnum is not a one-time charge" unless $part_pkg->freq eq '0';
50
51   my ($amount, $setup_cost, $quantity);
52   if ( $cgi->param('amount') =~ /^\s*(\d*(\.\d{1,2})*)\s*$/ ) {
53     $amount = sprintf('%.2f', $1);
54   }
55   if ( $cgi->param('setup_cost') =~ /^\s*(\d*(\.\d{1,2})*)\s*$/ ) {
56     $setup_cost = sprintf('%.2f', $1);
57   }
58   if ( $cgi->param('quantity') =~ /^\s*(\d*)\s*$/ ) {
59     $quantity = $1 || 1;
60   }
61
62   my $start_date = $cgi->param('start_date')
63                      ? parse_datetime($cgi->param('start_date'))
64                      : time;
65
66   $error = $cust_pkg->modify_charge(
67       'pkg'               => scalar($cgi->param('pkg')),
68       'classnum'          => scalar($cgi->param('classnum')),
69       'additional'        => \@description,
70       'adjust_commission' => ($cgi->param('adjust_commission') ? 1 : 0),
71       'amount'            => $amount,
72       'setup_cost'        => $setup_cost,
73       'quantity'          => $quantity,
74       'start_date'        => $start_date,
75   );
76
77 } else { # the usual case: new one-time charge
78
79   $message = "One-time charge added";
80
81   $param->{"amount"} =~ /^\s*(\d*(?:\.?\d{1,2}))\s*$/
82     or $error .= "Illegal amount " . $param->{"amount"} . "  ";
83   my $amount = $1;
84
85   my $setup_cost = '';
86   $param->{"setup_cost"} =~ /^\s*(\d*(?:\.?\d{1,2}))\s*$/
87     or $error .= "Illegal setup_cost " . $param->{"setup_cost"} . "  ";
88   my $setup_cost = $1;
89
90   my $quantity = 1;
91   if ( $cgi->param('quantity') =~ /^\s*(\d+)\s*$/ ) {
92     $quantity = $1;
93   }
94
95   $param->{'tax_override'} =~ /^\s*([,\d]*)\s*$/
96     or $error .= "Illegal tax override " . $param->{"tax_override"} . "  ";
97   my $override = $1;
98
99   if ( $param->{'taxclass'} eq '(select)' ) {
100     $error .= "Must select a tax class.  "
101       unless ($conf->exists('enable_taxproducts') &&
102                ( $override || $param->{taxproductnum} )
103              );
104     $cgi->param('taxclass', '');
105   }
106
107   unless ( $error ) {
108     my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
109       or $error .= "Unknown customer number $custnum.  ";
110
111     $error ||= $cust_main->charge( {
112       'amount'        => $amount,
113       'setup_cost'    => $setup_cost,
114       'quantity'      => $quantity,
115       'bill_now'      => scalar($cgi->param('bill_now')),
116       'invoice_terms' => scalar($cgi->param('invoice_terms')),
117       'start_date'    => ( scalar($cgi->param('start_date'))
118                              ? parse_datetime($cgi->param('start_date'))
119                              : ''
120                          ),
121       'no_auto'       => scalar($cgi->param('no_auto')),
122       'pkg'           => scalar($cgi->param('pkg')),
123       'setuptax'      => scalar($cgi->param('setuptax')),
124       'taxclass'      => scalar($cgi->param('taxclass')),
125       'taxproductnum' => scalar($cgi->param('taxproductnum')),
126       'tax_override'  => $override,
127       'classnum'      => scalar($cgi->param('classnum')),
128       'additional'    => \@description,
129     } );
130   }
131 }
132
133 </%init>