RT# 74435 - Fixed error with refund link on payment history page not allowing batch...
[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 if ( $error ) {
43   # do nothing
44 } elsif ( $payby =~ /^(CARD|CHEK)$/ ) { 
45   my %options = ();
46   my $bop = $FS::payby::payby2bop{$1};
47
48   my %payby2fields = (
49   'CARD' => [ qw( address1 address2 city county state zip country ) ],
50   'CHEK' => [ qw( ss paytype paystate stateid stateid_state ) ],
51   );
52   my %type = ( 'CARD' => 'credit card',
53              'CHEK' => 'electronic check (ACH)',
54              );
55
56 ##
57 # now run the refund
58 ##
59
60   $cgi->param('refund') =~ /^(\d*)(\.\d{2})?$/
61     or die "illegal refund amount ". $cgi->param('refund');
62   my $refund = "$1$2";
63   $cgi->param('paynum') =~ /^(\d*)$/ or die "Illegal paynum!";
64   my $paynum = $1;
65   #my $paydate;
66   my $paydate = $cgi->param('exp_year'). '-'. $cgi->param('exp_month'). '-01';
67   #unless ($paynum) {
68   #  if ($cust_payby->paydate) { $paydate = "$year-$month-01"; }
69   #  else { $paydate = "2037-12-01"; }
70   #}
71
72   if ( $cgi->param('batch') ) {
73     $paydate = "2037-12-01" unless $paydate;
74     $error ||= $cust_main->batch_card(
75                                      'payby'    => $payby,
76                                      'amount'   => $refund,
77                                      #'payinfo'  => $payinfo,
78                                      #'paydate'  => $paydate,
79                                      #'payname'  => $payname,
80                                      'paycode'  => 'C',
81                                      map { $_ => scalar($cgi->param($_)) }
82                                        @{$payby2fields{$payby}}
83                                    );
84     errorpage($error) if $error;
85
86     my %hash = map {
87       $_, scalar($cgi->param($_))
88     } fields('cust_refund');
89
90     my $new = new FS::cust_refund ( { 'paynum' => $paynum,
91                                       %hash,
92                                   } );
93     $error = $new->insert;
94
95   # if not a batch refund run realtime.
96   } else {
97     $options{'paydate'} = $paydate if $paydate =~ /^\d{2,4}-\d{1,2}-01$/;
98     $error = $cust_main->realtime_refund_bop( $bop, 'amount' => $refund,
99                                                   'paynum' => $paynum,
100                                                   'reasonnum' => $reasonnum,
101                                                   %options );
102   }
103 } else { # run cash refund.
104   my %hash = map {
105     $_, scalar($cgi->param($_))
106   } fields('cust_refund');
107   my $paynum = $cgi->param('paynum');
108   $paynum =~ /^(\d*)$/ or die "Illegal paynum!";
109   if ($paynum) {
110     my $cust_pay = qsearchs('cust_pay',{ 'paynum' => $paynum });
111     die "Could not find paynum $paynum" unless $cust_pay;
112     $error = $cust_pay->refund(\%hash);
113   } else {
114     my $new = new FS::cust_refund ( \%hash );
115     $error = $new->insert;
116   }
117 }
118
119 </%init>