protect set_usage and reset_usage here, too
[freeside.git] / httemplate / misc / process / recharge_svc.html
1 %if ($error) {
2 %  $cgi->param('error', $error);
3 <% $cgi->redirect(popurl(2). "recharge_svc.html?". $cgi->query_string ) %>
4 %} else {
5 <% header("Package recharged") %>
6   <SCRIPT TYPE="text/javascript">
7     window.top.location.reload();
8   </SCRIPT>
9   </BODY></HTML>
10 %}
11 <%init>
12
13 my $conf = new FS::Conf;
14
15 die "access denied"
16   unless $FS::CurrentUser::CurrentUser->access_right('Recharge customer service');
17
18 #untaint svcnum
19 my $svcnum = $cgi->param('svcnum');
20 $svcnum =~ /^(\d+)$/ || die "Illegal svcnum";
21 $svcnum = $1;
22
23 #untaint prepaid
24 my $prepaid = $cgi->param('prepaid');
25 $prepaid =~ /^(\w*)$/;
26 $prepaid = $1;
27
28 #untaint payby
29 my $payby = $cgi->param('payby');
30 $payby =~ /^([A-Z]*)$/;
31 $payby = $1;
32
33 my $error = '';
34 my $svc_acct = qsearchs( 'svc_acct', {'svcnum'=>$svcnum} );
35 $error = "Can't recharge service $svcnum. " unless $svc_acct;
36
37 my $cust_main = $svc_acct->cust_svc->cust_pkg->cust_main;
38
39 my $oldAutoCommit = $FS::UID::AutoCommit;
40 local $FS::UID::AutoCommit = 0;
41 my $dbh = dbh;
42
43 unless ($error) {
44
45   #should probably use payby.pm but whatever
46   if ($payby eq 'PREP') {
47     $error = $cust_main->recharge_prepay( $prepaid );
48   } elsif ( $payby =~ /^(CARD|DCRD|CHEK|DCHK|LECB|BILL|COMP)$/ ) {
49     my $part_pkg = $svc_acct->cust_svc->cust_pkg->part_pkg;
50     my $amount = $part_pkg->option('recharge_amount', 1);
51     my %rhash = map { $_ =~ /^recharge_(.*)$/; $1, $part_pkg->option($_) }
52       grep { $part_pkg->option($_, 1) }
53       qw ( recharge_seconds recharge_upbytes recharge_downbytes
54            recharge_totalbytes );
55
56     my $description = "Recharge";
57     $description .= " $rhash{seconds}s" if $rhash{seconds};
58     $description .= " $rhash{upbytes} up" if $rhash{upbytes};
59     $description .= " $rhash{downbytes} down" if $rhash{downbytes};
60     $description .= " $rhash{totalbytes} total" if $rhash{totalbytes};
61
62     $error = $cust_main->charge($amount, "Recharge " . $svc_acct->label,
63                                 $description, $part_pkg->taxclass);
64
65     $error ||= "invalid $_" foreach grep { $rhash{$_} !~ /^\d*$/ } keys %rhash;
66     if ($part_pkg->option('recharge_reset', 1)) {
67       $error ||= $svc_acct->set_usage(\%rhash, 'null' => 1);
68     }else{
69       $error ||= $svc_acct->recharge(\%rhash);
70     }
71
72     my $old_balance = $cust_main->balance;
73     $error ||= $cust_main->bill;
74     $error ||= $cust_main->apply_payments_and_credits;
75     my $bill_error = $cust_main->collect('realtime' => 1) unless $error;
76     $error ||= "Failed to collect - $bill_error"
77       if $cust_main->balance > $old_balance && $cust_main->balance > 0
78           && $payby ne 'BILL';
79
80   } else {
81     $error = "fatal error - unknown payby: $payby";
82   }
83
84 }
85
86 if ($error) {
87   $dbh->rollback if $oldAutoCommit;
88 } else {
89   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
90 }
91
92 </%init>