one-time charges on quotations, RT#25561
[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   $error = $cust_pkg->modify_charge(
72       'pkg'               => scalar($cgi->param('pkg')),
73       'classnum'          => scalar($cgi->param('classnum')),
74       'additional'        => \@description,
75       'adjust_commission' => ($cgi->param('adjust_commission') ? 1 : 0),
76       'amount'            => $amount,
77       'setup_cost'        => $setup_cost,
78       'quantity'          => $quantity,
79       'start_date'        => $start_date,
80   );
81
82 } else { # the usual case: new one-time charge
83
84   $message = "One-time charge added";
85
86   $param->{"amount"} =~ /^\s*(\d*(?:\.?\d{1,2}))\s*$/
87     or $error .= "Illegal amount " . $param->{"amount"} . "  ";
88   my $amount = $1;
89
90   my $setup_cost = '';
91   if ( $param->{setup_cost} =~ /\S/ ) {
92     $param->{setup_cost} =~ /^\s*(\d*(?:\.?\d{1,2}))\s*$/
93       or $error .= "Illegal setup_cost " . $param->{setup_cost} . "  ";
94     $setup_cost = $1;
95   }
96
97   my $quantity = 1;
98   if ( $cgi->param('quantity') =~ /^\s*(\d+)\s*$/ ) {
99     $quantity = $1;
100   }
101
102   $param->{'tax_override'} =~ /^\s*([,\d]*)\s*$/
103     or $error .= "Illegal tax override " . $param->{"tax_override"} . "  ";
104   my $override = $1;
105
106   if ( $param->{'taxclass'} eq '(select)' ) {
107     $error .= "Must select a tax class.  "
108       unless ($conf->exists('enable_taxproducts') &&
109                ( $override || $param->{taxproductnum} )
110              );
111     $cgi->param('taxclass', '');
112   }
113
114   my %charge = (
115     'amount'        => $amount,
116     'setup_cost'    => $setup_cost,
117     'quantity'      => $quantity,
118     'bill_now'      => scalar($cgi->param('bill_now')),
119     'invoice_terms' => scalar($cgi->param('invoice_terms')),
120     'start_date'    => ( scalar($cgi->param('start_date'))
121                            ? parse_datetime($cgi->param('start_date'))
122                            : ''
123                        ),
124     'no_auto'       => scalar($cgi->param('no_auto')),
125     'pkg'           => scalar($cgi->param('pkg')),
126     'setuptax'      => scalar($cgi->param('setuptax')),
127     'taxclass'      => scalar($cgi->param('taxclass')),
128     'taxproductnum' => scalar($cgi->param('taxproductnum')),
129     'tax_override'  => $override,
130     'classnum'      => scalar($cgi->param('classnum')),
131     'additional'    => \@description,
132   );
133
134   if ( $quotation ) {
135     $error ||= $quotation->charge( \%charge );
136   } else {
137     $error ||= $cust_main->charge( \%charge );
138   }
139
140 }
141
142 </%init>