e84894b045ad5c9c3092880913381ad537b091ae
[freeside.git] / httemplate / edit / process / cust_credit_bill.cgi
1 <%
2 #<!-- $Id: cust_credit_bill.cgi,v 1.2 2001-12-18 19:30:31 ivan Exp $ -->
3
4 use strict;
5 use vars qw( $cgi $custnum $crednum $new $error );
6 use CGI;
7 use CGI::Carp qw(fatalsToBrowser);
8 use FS::UID qw(cgisuidsetup getotaker);
9 use FS::CGI qw(popurl);
10 use FS::Record qw(qsearchs fields);
11 use FS::cust_credit;
12 use FS::cust_credit_bill;
13 use FS::cust_refund;
14 use FS::cust_main;
15
16 $cgi = new CGI;
17 cgisuidsetup($cgi);
18
19 $cgi->param('crednum') =~ /^(\d*)$/ or die "Illegal crednum!";
20 $crednum = $1;
21
22 my $cust_credit = qsearchs('cust_credit', { 'crednum' => $crednum } )
23   or die "No such crednum";
24
25 my $cust_main = qsearchs('cust_main', { 'custnum' => $cust_credit->custnum } )
26   or die "Bogus credit:  not attached to customer";
27
28 my $custnum = $cust_main->custnum;
29
30 if ($cgi->param('invnum') =~ /^Refund$/) {
31   $new = new FS::cust_refund ( {
32     'reason'  => $cust_credit->reason,
33     'refund'  => $cgi->param('amount'),
34     'payby'   => 'BILL',
35     '_date'   => $cgi->param('_date'),
36     'payinfo' => 'Cash',
37     'crednum' => $crednum,
38   } );
39 } else {
40   $new = new FS::cust_credit_bill ( {
41     map {
42       $_, scalar($cgi->param($_));
43     #} qw(custnum _date amount invnum)
44     } fields('cust_credit_bill')
45   } );
46 }
47
48 $error=$new->insert;
49
50 if ( $error ) {
51   $cgi->param('error', $error);
52   print $cgi->redirect(popurl(2). "cust_credit_bill.cgi?". $cgi->query_string );
53 } else {
54   print $cgi->redirect(popurl(3). "view/cust_main.cgi?$custnum");
55 }
56
57
58 %>