RT#37064: Add action link to manually refund a payment
[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;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 my $error = '';
37 if ( $payby =~ /^(CARD|CHEK)$/ ) { 
38   my %options = ();
39   my $bop = $FS::payby::payby2bop{$1};
40   $cgi->param('refund') =~ /^(\d*)(\.\d{2})?$/
41     or die "illegal refund amount ". $cgi->param('refund');
42   my $refund = "$1$2";
43   $cgi->param('paynum') =~ /^(\d*)$/ or die "Illegal paynum!";
44   my $paynum = $1;
45   my $reason = $cgi->param('reason');
46   my $paydate = $cgi->param('exp_year'). '-'. $cgi->param('exp_month'). '-01';
47   $options{'paydate'} = $paydate if $paydate =~ /^\d{2,4}-\d{1,2}-01$/;
48   $error = $cust_main->realtime_refund_bop( $bop, 'amount' => $refund,
49                                                   'paynum' => $paynum,
50                                                   'reason' => $reason,
51                                                   %options );
52 } else {
53   my %hash = map {
54     $_, scalar($cgi->param($_))
55   } fields('cust_refund');
56   my $paynum = $cgi->param('paynum');
57   $paynum =~ /^(\d*)$/ or die "Illegal paynum!";
58   if ($paynum) {
59     my $cust_pay = qsearchs('cust_pay',{ 'paynum' => $paynum });
60     die "Could not find paynum $paynum" unless $cust_pay;
61     $error = $cust_pay->refund(\%hash);
62   } else {
63     my $new = new FS::cust_refund ( \%hash );
64     $error = $new->insert;
65   }
66 }
67
68 </%init>