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