RT# 74435 - fixed so payment would auto unapply when doing a batch refund
[freeside.git] / httemplate / edit / cust_refund.cgi
1 % if ( $link eq 'popup' ) { 
2   <% include('/elements/header-popup.html', $title ) %>
3 % } else { 
4   <% include("/elements/header.html", $title, '') %>
5 % } 
6
7 <% include('/elements/error.html') %>
8
9 % unless ( $link eq 'popup' ) { 
10     <% small_custview($custnum, $conf->config('countrydefault')) %>
11 % } 
12
13 <FORM NAME="RefundForm" ACTION="<% $p1 %>process/cust_refund.cgi" METHOD=POST onSubmit="document.RefundForm.submitButton.disabled=true">
14 <INPUT TYPE="hidden" NAME="popup" VALUE="<% $link %>">
15 <INPUT TYPE="hidden" NAME="refundnum" VALUE="">
16 <INPUT TYPE="hidden" NAME="custnum" VALUE="<% $custnum %>">
17 <INPUT TYPE="hidden" NAME="paynum" VALUE="<% $paynum %>">
18 <INPUT TYPE="hidden" NAME="_date" VALUE="<% $_date %>">
19 <INPUT TYPE="hidden" NAME="payby" VALUE="<% $payby %>">
20 <INPUT TYPE="hidden" NAME="paybatch" VALUE="">
21 <INPUT TYPE="hidden" NAME="credited" VALUE="">
22
23 <BR>
24
25 % if ( $cust_pay ) {
26 %
27 %  #false laziness w/FS/FS/cust_pay.pm
28 %  my $payby = FS::payby->payname($cust_pay->payby);
29 %  my $paymask = $cust_pay->paymask;
30 %  my $paydate = $cust_pay->paydate;
31 %  if ( $cgi->param('error') ) { 
32 %    $paydate = $cgi->param('exp_year'). '-'. $cgi->param('exp_month'). '-01';
33 %    $paydate = '' unless ($paydate =~ /^\d{2,4}-\d{1,2}-01$'/);
34 %  }
35
36   <BR>Payment
37   <TABLE class="fsinnerbox">
38
39     <TR>
40       <TD ALIGN="right">Amount</TD><TD BGCOLOR="#ffffff">$<% $cust_pay->paid %></TD>
41     </TR>
42
43   <TR>
44     <TD ALIGN="right">Date</TD><TD BGCOLOR="#ffffff"><% time2str($date_format, $cust_pay->_date) %></TD>
45   </TR>
46
47   <TR>
48     <TD ALIGN="right">Method</TD><TD BGCOLOR="#ffffff"><% $payby %> # <% $paymask %></TD>
49     <INPUT TYPE="hidden" NAME="custpaybynum" VALUE="<% $cust_payby->custpaybynum %>">
50   </TR>
51
52 % unless ( $paydate || $cust_pay->payby ne 'CARD' ) {  # possibly other reasons: i.e. card has since expired
53   <TR>
54     <TD ALIGN="right">Expiration</TD><TD BGCOLOR="#ffffff">
55       <% include( '/elements/select-month_year.html',
56                   'prefix' => 'exp',
57                   'selected_date' => $paydate,
58                   'empty_option' => !$paydate,
59                 ) %>
60     </TD>
61   </TR>
62 % } 
63 % if ( $cust_pay->processor ) {
64     <TR>
65       <TD ALIGN="right">Processor</TD>
66       <TD BGCOLOR="#ffffff"><% $cust_pay->processor %></TD>
67     </TR>
68 % if ( length($cust_pay->auth) ) { 
69
70       <TR>
71         <TD ALIGN="right">Authorization</TD>
72         <TD BGCOLOR="#ffffff"><% $cust_pay->auth %></TD>
73       </TR>
74 % } 
75 % if ( length($cust_pay->order_number) ) { 
76
77       <TR>
78         <TD ALIGN="right">Order number</TD>
79         <TD BGCOLOR="#ffffff"><% $cust_pay->order_number %></TD>
80       </TR>
81 % } 
82 % } # if ($cust_pay->processor)
83
84   </TABLE>
85 % }  #if $cust_pay
86
87
88 <BR>Refund
89
90 <TABLE class="fsinnerbox">
91
92   <TR>
93     <TD ALIGN="right">Date</TD>
94     <TD BGCOLOR="#ffffff"><% time2str($date_format, $_date) %></TD>
95   </TR>
96
97   <TR>
98     <TD ALIGN="right">Amount</TD>
99     <TD BGCOLOR="#ffffff">$<INPUT TYPE="text" NAME="refund" VALUE="<% $refund %>" SIZE=8 MAXLENGTH=9> by <B><% FS::payby->payname($payby) %></B></TD>
100   </TR>
101
102 % if ( $payby eq 'BILL' ) { 
103     <TR>
104       <TD ALIGN="right">Check #</TD>
105       <TD COLSPAN=2><INPUT TYPE="text" NAME="payinfo" VALUE="<% $payinfo %>" SIZE=10></TD>
106     </TR>
107     </TABLE>
108 % }
109 % elsif ($payby eq 'CHEK' || $payby eq 'CARD') {
110 %
111 <SCRIPT TYPE="text/javascript">
112   function cust_payby_changed (what) {
113     var custpaybynum = what.options[what.selectedIndex].value
114     if ( custpaybynum == '' || custpaybynum == '0' ) {
115        //what.form.payinfo.disabled = false;
116        $('#cust_payby').slideDown();
117     } else {
118        //what.form.payinfo.value = '';
119        //what.form.payinfo.disabled = true;
120        $('#cust_payby').slideUp();
121     }
122   }
123 </SCRIPT>
124 % my @cust_payby = ();
125 % if ( $payby eq 'CARD' ) {
126 %   @cust_payby = $cust_main->cust_payby('CARD','DCRD');
127 % } elsif ( $payby eq 'CHEK' ) {
128 %   @cust_payby = $cust_main->cust_payby('CHEK','DCHK');
129 % } else {
130 %   die "unknown payby $payby";
131 % }
132 %
133 % my $custpaybynum = length(scalar($cgi->param('custpaybynum')))
134 %                      ? scalar($cgi->param('custpaybynum'))
135 %                      : scalar(@cust_payby) && $cust_payby[0]->custpaybynum;
136
137 % if ($cust_pay) {
138   <INPUT TYPE="hidden" NAME="payinfo" VALUE="<% $payinfo %>" SIZE=10>
139 % }
140 % else {
141   <& /elements/tr-select-cust_payby.html,
142      'cust_payby' => \@cust_payby,
143      'curr_value' => $custpaybynum,
144      'onchange'   => 'cust_payby_changed(this)',
145   &>
146 % }
147
148 % if ( $conf->exists("batch-enable")
149 %      || grep $payby eq $_, $conf->config('batch-enable_payby')
150 % ) {
151 %     if ( grep $payby eq $_, $conf->config('realtime-disable_payby') ) {
152           <INPUT TYPE="hidden" NAME="batch" VALUE="1">
153 %     } else {
154         <TR>
155           <TD ALIGN="right"><INPUT TYPE="checkbox" NAME="batch" VALUE="1" ID="batch" <% ($batchnum || $batch) ? 'checked' : '' %> ></TD>
156           <TH ALIGN="left">&nbsp;&nbsp;&nbsp;<% mt('Add to current batch') |h %></TH>
157         </TR>
158 %     }
159 % }
160
161     </TABLE>
162 <P>
163
164 %   if ( !$cust_pay ) {
165 <DIV ID="cust_payby"
166   <% $custpaybynum ? 'STYLE="display:none"'
167                    : ''
168   %>
169 >
170 <TABLE class="fsinnerbox">
171
172     <& /elements/cust_payby_new.html,
173         'cust_payby' => \@cust_payby,
174         'curr_value' => $custpaybynum,
175     &>
176
177 </TABLE>
178 </DIV>
179 %   } # end if cust_pay
180
181 %  } else {
182     <INPUT TYPE="hidden" NAME="payinfo" VALUE="">
183     </TABLE>
184 % }
185
186 <P>
187 <TABLE class="fsinnerbox">
188 <& /elements/tr-select-reason.html,
189               'field'          => 'reasonnum',
190               'reason_class'   => 'F',
191               'control_button' => "confirm_refund_button",
192               'cgi'            => $cgi,
193 &>
194
195 </TABLE>
196
197 <BR>
198 <INPUT TYPE="submit" NAME="submitButton" ID="confirm_refund_button" VALUE="<% mt('Post refund') |h %>" DISABLED>
199
200 </FORM>
201
202 % if ( $link eq 'popup' ) { 
203     </BODY>
204     </HTML>
205 % } else { 
206     <% include('/elements/footer.html') %>
207 % } 
208
209 <%init>
210
211 my $conf = new FS::Conf;
212 my $date_format = $conf->config('date_format') || '%m/%d/%Y';
213
214 my $custnum = $cgi->param('custnum');
215 my $refund  = $cgi->param('refund');
216 my $payby   = $cgi->param('payby');
217 my $payinfo = $cgi->param('payinfo');
218 my $reason  = $cgi->param('reason');
219 my $link    = $cgi->param('popup') ? 'popup' : '';
220 my $batch   = $cgi->param('batch');
221
222 die "access denied"
223   unless $FS::CurrentUser::CurrentUser->refund_access_right($payby);
224
225 my( $paynum, $cust_pay, $batchnum, $cust_payby ) = ( '', '', '', '' );
226 if ( $cgi->param('paynum') =~ /^(\d+)$/ ) {
227   $paynum = $1;
228   $cust_pay = qsearchs('cust_pay', { paynum=>$paynum } )
229     or die "unknown payment # $paynum";
230   $refund ||= $cust_pay->unrefunded;
231   $batchnum = $cust_pay->batchnum;
232   if ( $custnum ) {
233     die "payment # $paynum is not for specified customer # $custnum"
234       unless $custnum == $cust_pay->custnum;
235   } else {
236     $custnum = $cust_pay->custnum;
237   }
238   # get custpayby
239   die "Can not find payby record!"
240   unless $cust_payby = qsearchs(
241     'cust_payby', { paymask => $cust_pay->paymask, custnum => $custnum }
242   );
243
244 }
245 die "no custnum or paynum specified!" unless $custnum;
246
247 my $cust_main = qsearchs( 'cust_main', { 'custnum'=>$custnum } );
248 die "unknown custnum $custnum" unless $cust_main;
249
250 my $_date = time;
251
252 my $p1 = popurl(1);
253
254 my $title = 'Refund '. FS::payby->payname($payby). ' payment';
255
256 </%init>