44605bf421e665118f9c4ce9744d346f017d1f69
[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 $cgi->param('custnum') =~ /^(\d*)$/ or die "Illegal custnum!";
25 my $custnum = $1;
26 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
27   or die "unknown custnum $custnum";
28
29 my $link    = $cgi->param('popup') ? 'popup' : '';
30
31 my $payby = $cgi->param('payby');
32
33 die "access denied"
34   unless $FS::CurrentUser::CurrentUser->refund_access_right($payby);
35
36 $cgi->param('reasonnum') =~ /^(-?\d+)$/ or die "Illegal reasonnum";
37 my ($reasonnum, $error) = $m->comp('/misc/process/elements/reason');
38 $cgi->param('reasonnum', $reasonnum) unless $error;
39
40 if ( $error ) {
41   # do nothing
42 } elsif ( $payby =~ /^(CARD|CHEK)$/ ) { 
43   my %options = ();
44   my $bop = $FS::payby::payby2bop{$1};
45
46   my %payby2fields = (
47   'CARD' => [ qw( address1 address2 city county state zip country ) ],
48   'CHEK' => [ qw( ss paytype paystate stateid stateid_state ) ],
49   );
50   my %type = ( 'CARD' => 'credit card',
51              'CHEK' => 'electronic check (ACH)',
52              );
53
54 ##
55 # now run the refund
56 ##
57
58   $cgi->param('refund') =~ /^(\d*)(\.\d{2})?$/
59     or die "illegal refund amount ". $cgi->param('refund');
60   my $refund = "$1$2";
61   $cgi->param('paynum') =~ /^(\d*)$/ or die "Illegal paynum!";
62   my $paynum = $1;
63   my $paydate = $cgi->param('exp_year'). '-'. $cgi->param('exp_month'). '-01';
64
65   if ( $cgi->param('batch') ) {
66     $paydate = "2037-12-01" unless $paydate;
67     $error ||= $cust_main->batch_card(
68                                      'payby'    => $payby,
69                                      'amount'   => $refund,
70                                      #'payinfo'  => $payinfo,
71                                      #'paydate'  => $paydate,
72                                      #'payname'  => $payname,
73                                      'paycode'  => 'C',
74                                      map { $_ => scalar($cgi->param($_)) }
75                                        @{$payby2fields{$payby}}
76                                    );
77     errorpage($error) if $error;
78
79     my %hash = map {
80       $_, scalar($cgi->param($_))
81     } fields('cust_refund');
82
83     my $new = new FS::cust_refund ( { 'paynum' => $paynum,
84                                       %hash,
85                                   } );
86     $error = $new->insert;
87
88   # if not a batch refund run realtime.
89   } else {
90     $options{'paydate'} = $paydate if $paydate =~ /^\d{2,4}-\d{1,2}-01$/;
91     $error = $cust_main->realtime_refund_bop( $bop, 'amount' => $refund,
92                                                   'paynum' => $paynum,
93                                                   'reasonnum' => $reasonnum,
94                                                   %options );
95   }
96 } else { # run cash refund.
97   my %hash = map {
98     $_, scalar($cgi->param($_))
99   } fields('cust_refund');
100   my $paynum = $cgi->param('paynum');
101   $paynum =~ /^(\d*)$/ or die "Illegal paynum!";
102   if ($paynum) {
103     my $cust_pay = qsearchs('cust_pay',{ 'paynum' => $paynum });
104     die "Could not find paynum $paynum" unless $cust_pay;
105     $error = $cust_pay->refund(\%hash);
106   } else {
107     my $new = new FS::cust_refund ( \%hash );
108     $error = $new->insert;
109   }
110 }
111
112 </%init>