UX: one-time charge - clarify quotation vs. real charge, acknowledge addition instead...
[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     'bill_now'      => scalar($cgi->param('bill_now')),
127     'invoice_terms' => scalar($cgi->param('invoice_terms')),
128     'start_date'    => ( scalar($cgi->param('start_date'))
129                            ? parse_datetime($cgi->param('start_date'))
130                            : ''
131                        ),
132     'no_auto'       => scalar($cgi->param('no_auto')),
133     'separate_bill' => scalar($cgi->param('separate_bill')),
134     'pkg'           => scalar($cgi->param('pkg')),
135     'setuptax'      => scalar($cgi->param('setuptax')),
136     'taxclass'      => scalar($cgi->param('taxclass')),
137     'taxproductnum' => scalar($cgi->param('taxproductnum')),
138     'tax_override'  => $override,
139     'classnum'      => scalar($cgi->param('classnum')),
140     'additional'    => \@description,
141   );
142
143   if ( $quotation ) {
144     $error ||= $quotation->charge( \%charge );
145   } else {
146     $error ||= $cust_main->charge( \%charge );
147   }
148
149 }
150
151 my @redirect = ();
152 if ( $error ) {
153   $cgi->param('error', $error );
154   @redirect = ( $p.'quick-charge.html?'. $cgi->query_string );
155 } elsif ( $quotation ) {
156   @redirect = (
157     -uri    => $fsurl.'view/quotation.html?' . $quotation->quotationnum,
158     -cookie => CGI::Cookie->new( -name    => 'freeside_status',
159                                  -value   => mt('One-time charge added to quotation'),
160                                  -expires => '+5m',
161                                ),
162   );
163 } else {
164   @redirect = (
165     -uri    => $fsurl.'view/cust_main.cgi?custnum='. $cust_main->custnum.
166                ';show=last',
167     -cookie => CGI::Cookie->new( -name    => 'freeside_status',
168                                  -value   => mt('One-time charge ordered'),
169                                  -expires => '+5m',
170                                ),
171   );
172 }
173
174 </%init>