RT# 82988 - Fixed so only formats that can handle electronic refunds can download...
[freeside.git] / httemplate / edit / process / cust_refund.cgi
1 %if ( $error ) {
2 %  $cgi->param('error', $error);
3 <% $cgi->redirect(popurl(2). "cust_refund.cgi?". $cgi->query_string ) %>
4 %} else {
5 %
6 %  if ( $link eq 'popup' ) {
7 %
8 <% header('Refund entered') %>
9     <SCRIPT TYPE="text/javascript">
10       window.top.location.reload();
11     </SCRIPT>
12
13     </BODY></HTML>
14 %  } else {
15 <% $cgi->redirect(popurl(3). "view/cust_main.cgi?custnum=$custnum;show=payment_history") %>
16 %  }
17 %}
18 <%init>
19
20 die "access denied"
21   unless $FS::CurrentUser::CurrentUser->access_right('Refund payment')
22       || $FS::CurrentUser::CurrentUser->access_right('Post refund');
23
24 my $conf = new FS::Conf;
25
26 $cgi->param('custnum') =~ /^(\d*)$/ or die "Illegal custnum!";
27 my $custnum = $1;
28 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
29   or die "unknown custnum $custnum";
30
31 my $link    = $cgi->param('popup') ? 'popup' : '';
32
33 my $payby = $cgi->param('payby');
34
35 die "access denied"
36   unless $FS::CurrentUser::CurrentUser->refund_access_right($payby);
37
38 $cgi->param('reasonnum') =~ /^(-?\d+)$/ or die "Illegal reasonnum";
39 my ($reasonnum, $error) = $m->comp('/misc/process/elements/reason');
40 $cgi->param('reasonnum', $reasonnum) unless $error;
41
42 $error = "No batch download format configured that allows electronic refunds" unless (FS::pay_batch->can_handle_electronic_refunds && !$error);
43
44 if ( $error ) {
45   # do nothing
46 } elsif ( $payby =~ /^(CARD|CHEK)$/ ) { 
47   my %options = ();
48   my $bop = $FS::payby::payby2bop{$1};
49
50   my %payby2fields = (
51   'CARD' => [ qw( address1 address2 city county state zip country ) ],
52   'CHEK' => [ qw( ss paytype paystate stateid stateid_state ) ],
53   );
54   my %type = ( 'CARD' => 'credit card',
55              'CHEK' => 'electronic check (ACH)',
56              );
57
58 my( $cust_pay, $cust_payby, $payinfo, $paycvv, $month, $year, $payname );
59 my $paymask = '';
60
61 ## get cust pay info if paynum exists
62 if ( $cgi->param('paynum') > 0) {
63   $cust_pay = qsearchs({
64     'table'     => 'cust_pay',
65     'hashref'   => { 'paynum' => $cgi->param('paynum') },
66     'select'    => 'cust_pay.*, cust_pay_batch.payname ',
67     'addl_from' => "left join cust_pay_batch on cust_pay_batch.batchnum = cust_pay.batchnum and cust_pay_batch.custnum = $custnum ",
68   });
69 }
70
71 ##
72 # now run the refund
73 ##
74
75   $cgi->param('refund') =~ /^(\d*)(\.\d{2})?$/
76     or die "illegal refund amount ". $cgi->param('refund');
77   my $refund = "$1$2";
78   $cgi->param('paynum') =~ /^(\d*)$/ or die "Illegal paynum!";
79   my $paynum = $1;
80   my $paydate = $cgi->param('exp_year'). '-'. $cgi->param('exp_month'). '-01';
81
82   if ( $cgi->param('batch') ) {
83     $paydate = "2037-12-01" unless $paydate;
84     $error ||= $cust_main->batch_card(
85                                      'payby'    => $payby,
86                                      'amount'   => $refund,
87                                      #'payinfo'  => $payinfo,
88                                      #'paydate'  => $paydate,
89                                      #'payname'  => $payname,
90                                      'paycode'  => 'C',
91                                      map { $_ => scalar($cgi->param($_)) }
92                                        @{$payby2fields{$payby}}
93                                    );
94     errorpage($error) if $error;
95
96     my %hash = map {
97       $_, scalar($cgi->param($_))
98     } fields('cust_refund');
99
100     ## unapply payment before creating refund.
101     while ( $cust_pay && $cust_pay->unapplied < $refund ) {
102       my @cust_bill_pay = $cust_pay->cust_bill_pay;
103       last unless @cust_bill_pay;
104       my $cust_bill_pay = pop @cust_bill_pay;
105       my $error = $cust_bill_pay->delete;
106       last if $error;
107     }
108
109     my $new = new FS::cust_refund ( { 'paynum' => $paynum,
110                                       %hash,
111                                   } );
112     $error = $new->insert;
113
114   # if not a batch refund run realtime.
115   } else {
116     $options{'paydate'} = $paydate if $paydate =~ /^\d{2,4}-\d{1,2}-01$/;
117     $error = $cust_main->realtime_refund_bop( $bop, 'amount' => $refund,
118                                                   'paynum' => $paynum,
119                                                   'reasonnum' => $reasonnum,
120                                                   %options );
121   }
122 } else { # run cash refund.
123   my %hash = map {
124     $_, scalar($cgi->param($_))
125   } fields('cust_refund');
126   my $paynum = $cgi->param('paynum');
127   $paynum =~ /^(\d*)$/ or die "Illegal paynum!";
128   if ($paynum) {
129     my $cust_pay = qsearchs('cust_pay',{ 'paynum' => $paynum });
130     die "Could not find paynum $paynum" unless $cust_pay;
131     $error = $cust_pay->refund(\%hash);
132   } else {
133     my $new = new FS::cust_refund ( \%hash );
134     $error = $new->insert;
135   }
136 }
137
138 </%init>