allow changing package class of one-time charges post-billing, #25342
[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("One-time charge added")) %>
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 if ( $param->{'pkgnum'} =~ /^(\d+)$/ ) {
38   my $pkgnum = $1;
39   die "access denied"
40     unless $curuser->access_right('Modify one-time charge');
41
42   my $cust_pkg = FS::cust_pkg->by_key($1)
43     or die "pkgnum $pkgnum not found";
44
45   my $part_pkg = $cust_pkg->part_pkg;
46   die "pkgnum $pkgnum is not a one-time charge" unless $part_pkg->freq eq '0';
47
48   $error = $cust_pkg->modify_charge(
49       'pkg'               => scalar($cgi->param('pkg')),
50       'classnum'          => scalar($cgi->param('classnum')),
51       'additional'        => \@description,
52       'adjust_commission' => ($cgi->param('adjust_commission') ? 1 : 0),
53   );
54
55 } else {
56   # the usual case: new one-time charge
57   $param->{"amount"} =~ /^\s*(\d*(?:\.?\d{1,2}))\s*$/
58     or $error .= "Illegal amount " . $param->{"amount"} . "  ";
59   my $amount = $1;
60
61   my $quantity = 1;
62   if ( $cgi->param('quantity') =~ /^\s*(\d+)\s*$/ ) {
63     $quantity = $1;
64   }
65
66   $param->{'tax_override'} =~ /^\s*([,\d]*)\s*$/
67     or $error .= "Illegal tax override " . $param->{"tax_override"} . "  ";
68   my $override = $1;
69
70   if ( $param->{'taxclass'} eq '(select)' ) {
71     $error .= "Must select a tax class.  "
72       unless ($conf->exists('enable_taxproducts') &&
73                ( $override || $param->{taxproductnum} )
74              );
75     $cgi->param('taxclass', '');
76   }
77
78   unless ( $error ) {
79     my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
80       or $error .= "Unknown customer number $custnum.  ";
81
82     $error ||= $cust_main->charge( {
83       'amount'        => $amount,
84       'quantity'      => $quantity,
85       'bill_now'      => scalar($cgi->param('bill_now')),
86       'invoice_terms' => scalar($cgi->param('invoice_terms')),
87       'start_date'    => ( scalar($cgi->param('start_date'))
88                              ? parse_datetime($cgi->param('start_date'))
89                              : ''
90                          ),
91       'no_auto'       => scalar($cgi->param('no_auto')),
92       'pkg'           => scalar($cgi->param('pkg')),
93       'setuptax'      => scalar($cgi->param('setuptax')),
94       'taxclass'      => scalar($cgi->param('taxclass')),
95       'taxproductnum' => scalar($cgi->param('taxproductnum')),
96       'tax_override'  => $override,
97       'classnum'      => scalar($cgi->param('classnum')),
98       'additional'    => \@description,
99     } );
100   }
101 }
102
103 </%init>