2 % $cgi->param('error', $error);
3 <% $cgi->redirect(popurl(2). "cust_refund.cgi?". $cgi->query_string ) %>
6 % if ( $link eq 'popup' ) {
8 <% header('Refund entered') %>
9 <SCRIPT TYPE="text/javascript">
10 window.top.location.reload();
15 <% $cgi->redirect(popurl(3). "view/cust_main.cgi?custnum=$custnum;show=payment_history") %>
21 unless $FS::CurrentUser::CurrentUser->access_right('Refund payment')
22 || $FS::CurrentUser::CurrentUser->access_right('Post refund');
24 my $conf = new FS::Conf;
26 $cgi->param('custnum') =~ /^(\d*)$/ or die "Illegal custnum!";
28 my $cust_main = qsearchs('cust_main', { 'custnum' => $custnum } )
29 or die "unknown custnum $custnum";
31 my $link = $cgi->param('popup') ? 'popup' : '';
33 my $payby = $cgi->param('payby');
36 unless $FS::CurrentUser::CurrentUser->refund_access_right($payby);
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;
44 } elsif ( $payby =~ /^(CARD|CHEK)$/ ) {
46 my $bop = $FS::payby::payby2bop{$1};
49 'CARD' => [ qw( address1 address2 city county state zip country ) ],
50 'CHEK' => [ qw( ss paytype paystate stateid stateid_state ) ],
52 my %type = ( 'CARD' => 'credit card',
53 'CHEK' => 'electronic check (ACH)',
56 my( $cust_pay, $cust_payby, $payinfo, $paycvv, $month, $year, $payname );
59 ## get cust pay info if paynum exists
60 if ( $cgi->param('paynum') > 0) {
61 $cust_pay = qsearchs({
62 'table' => 'cust_pay',
63 'hashref' => { 'paynum' => $cgi->param('paynum') },
64 'select' => 'cust_pay.*, cust_pay_batch.payname ',
65 'addl_from' => "left join cust_pay_batch on cust_pay_batch.batchnum = cust_pay.batchnum and cust_pay_batch.custnum = $custnum ",
73 $cgi->param('refund') =~ /^(\d*)(\.\d{2})?$/
74 or die "illegal refund amount ". $cgi->param('refund');
76 $cgi->param('paynum') =~ /^(\d*)$/ or die "Illegal paynum!";
78 my $paydate = $cgi->param('exp_year'). '-'. $cgi->param('exp_month'). '-01';
80 if ( $cgi->param('batch') ) {
81 $paydate = "2037-12-01" unless $paydate;
82 $error ||= $cust_main->batch_card(
85 #'payinfo' => $payinfo,
86 #'paydate' => $paydate,
87 #'payname' => $payname,
89 map { $_ => scalar($cgi->param($_)) }
90 @{$payby2fields{$payby}}
92 errorpage($error) if $error;
95 $_, scalar($cgi->param($_))
96 } fields('cust_refund');
98 ## unapply payment before creating refund.
99 while ( $cust_pay && $cust_pay->unapplied < $refund ) {
100 my @cust_bill_pay = $cust_pay->cust_bill_pay;
101 last unless @cust_bill_pay;
102 my $cust_bill_pay = pop @cust_bill_pay;
103 my $error = $cust_bill_pay->delete;
107 my $new = new FS::cust_refund ( { 'paynum' => $paynum,
110 $error = $new->insert;
112 # if not a batch refund run realtime.
114 $options{'paydate'} = $paydate if $paydate =~ /^\d{2,4}-\d{1,2}-01$/;
115 $error = $cust_main->realtime_refund_bop( $bop, 'amount' => $refund,
117 'reasonnum' => $reasonnum,
120 } else { # run cash refund.
122 $_, scalar($cgi->param($_))
123 } fields('cust_refund');
124 my $paynum = $cgi->param('paynum');
125 $paynum =~ /^(\d*)$/ or die "Illegal paynum!";
127 my $cust_pay = qsearchs('cust_pay',{ 'paynum' => $paynum });
128 die "Could not find paynum $paynum" unless $cust_pay;
129 $error = $cust_pay->refund(\%hash);
131 my $new = new FS::cust_refund ( \%hash );
132 $error = $new->insert;