07797d0c4346b6dbf56651ccb5ecd806087c58d5
[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+)$/ ) {
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, $quantity, $start_date);
52   if ( $cgi->param('amount') =~ /^\s*(\d*(\.\d{1,2})*)\s*$/ ) {
53     $amount = sprintf('%.2f', $1);
54   }
55   if ( $cgi->param('quantity') =~ /^\s*(\d*)\s*$/ ) {
56     $quantity = $1 || 1;
57   }
58   if ( $cgi->param('start_date') ) {
59     $start_date = parse_datetime($cgi->param('start_date'));
60   } else {
61     $start_date = time;
62   }
63
64   $error = $cust_pkg->modify_charge(
65       'pkg'               => scalar($cgi->param('pkg')),
66       'classnum'          => scalar($cgi->param('classnum')),
67       'additional'        => \@description,
68       'adjust_commission' => ($cgi->param('adjust_commission') ? 1 : 0),
69       'amount'            => $amount,
70       'quantity'          => $quantity,
71       'start_date'        => $start_date,
72   );
73
74 } else {
75   $message = "One-time charge added";
76   # the usual case: new one-time charge
77   $param->{"amount"} =~ /^\s*(\d*(?:\.?\d{1,2}))\s*$/
78     or $error .= "Illegal amount " . $param->{"amount"} . "  ";
79   my $amount = $1;
80
81   my $quantity = 1;
82   if ( $cgi->param('quantity') =~ /^\s*(\d+)\s*$/ ) {
83     $quantity = $1;
84   }
85
86   $param->{'tax_override'} =~ /^\s*([,\d]*)\s*$/
87     or $error .= "Illegal tax override " . $param->{"tax_override"} . "  ";
88   my $override = $1;
89
90   if ( $param->{'taxclass'} eq '(select)' ) {
91     $error .= "Must select a tax class.  "
92       unless ($conf->exists('enable_taxproducts') &&
93                ( $override || $param->{taxproductnum} )
94              );
95     $cgi->param('taxclass', '');
96   }
97
98   unless ( $error ) {
99     my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
100       or $error .= "Unknown customer number $custnum.  ";
101
102     $error ||= $cust_main->charge( {
103       'amount'        => $amount,
104       'quantity'      => $quantity,
105       'bill_now'      => scalar($cgi->param('bill_now')),
106       'invoice_terms' => scalar($cgi->param('invoice_terms')),
107       'start_date'    => ( scalar($cgi->param('start_date'))
108                              ? parse_datetime($cgi->param('start_date'))
109                              : ''
110                          ),
111       'no_auto'       => scalar($cgi->param('no_auto')),
112       'pkg'           => scalar($cgi->param('pkg')),
113       'setuptax'      => scalar($cgi->param('setuptax')),
114       'taxclass'      => scalar($cgi->param('taxclass')),
115       'taxproductnum' => scalar($cgi->param('taxproductnum')),
116       'tax_override'  => $override,
117       'classnum'      => scalar($cgi->param('classnum')),
118       'additional'    => \@description,
119     } );
120   }
121 }
122
123 </%init>