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