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