cust_bill_pay and cust_credit_refund.
[freeside.git] / FS / FS / cust_credit_refund.pm
1 package cust_credit_refund;
2
3 use strict;
4 use vars qw( @ISA );
5 use FS::Record qw( qsearch qsearchs dbh );
6 #use FS::UID qw(getotaker);
7 #use FS::cust_credit
8
9 @ISA = qw( FS::Record );
10
11 =head1 NAME
12
13 FS::cust_credit_refund - Object methods for cust_bill_pay records
14
15 =head1 SYNOPSIS 
16
17   use FS::cust_credit_refund;
18
19   $record = new FS::cust_credit_refund \%hash;
20   $record = new FS::cust_credit_refund { 'column' => 'value' };
21
22   $error = $record->insert;
23
24   $error = $new_record->replace($old_record);
25
26   $error = $record->delete;
27
28   $error = $record->check;
29
30 =head1 DESCRIPTION
31
32 An FS::cust_credit_refund represents the application of a refund to a specific
33 credit.  FS::cust_credit_refund inherits from FS::Record.  The following fields
34 are currently supported:
35
36 =over 4
37
38 =item creditrefundnum - primary key (assigned automatically)
39
40 =item crednum - Credit (see L<FS::cust_credit>)
41
42 =item refundnum - Refund (see L<FS::cust_refund>)
43
44 =item amount - Amount of the refund to apply to the specific credit.
45
46 =item _date - specified as a UNIX timestamp; see L<perlfunc/"time">.  Also see
47 L<Time::Local> and L<Date::Parse> for conversion functions.
48
49 =back
50
51 =head1 METHODS
52
53 =over 4 
54
55 =item new HASHREF
56
57 Creates a new record.  To add the record to the database, see L<"insert">.
58
59 sub table { 'cust_credit_refund'; }
60
61 =item insert
62
63 Adds this record to the database.  If there is an error, returns the error,
64 otherwise returns false.
65
66 =cut
67
68 sub insert {
69   my $self = shift;
70
71   local $SIG{HUP} = 'IGNORE';
72   local $SIG{INT} = 'IGNORE';
73   local $SIG{QUIT} = 'IGNORE';
74   local $SIG{TERM} = 'IGNORE';
75   local $SIG{TSTP} = 'IGNORE';
76   local $SIG{PIPE} = 'IGNORE';
77
78   my $oldAutoCommit = $FS::UID::AutoCommit;
79   local $FS::UID::AutoCommit = 0;
80   my $dbh = dbh;
81
82   my $error = $self->check;
83   return $error if $error;
84
85   $error = $self->SUPER::insert;
86
87   my $cust_refund =
88     qsearchs('cust_refund', { 'refundnum' => $self->refundnum } )
89   or do {
90     $dbh->rollback if $oldAutoCommit;
91     return "unknown cust_refund.refundnum: ". $self->refundnum
92   };
93
94   my $refund_total = 0;
95   $refund_total += $_ foreach map { $_->amount }
96     qsearch('cust_credit_refund', { 'refundnum' => $self->refundnum } );
97
98   if ( $refund_total > $cust_refund->refund ) {
99     $dbh->rollback if $oldAutoCommit;
100     return "total cust_credit_refund.amount $refund_total for refundnum ".
101            $self->refundnum.
102            " greater than cust_refund.refund ". $cust_refund->refund;
103   }
104
105   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
106
107   '';
108 }
109
110 =item delete
111
112 Currently unimplemented (accounting reasons).
113
114 =cut
115
116 sub delete {
117   return "Can't (yet?) delete cust_credit_refund records!";
118 }
119
120 =item replace OLD_RECORD
121
122 Currently unimplemented (accounting reasons).
123
124 =cut
125
126 sub replace {
127    return "Can't (yet?) modify cust_credit_refund records!";
128 }
129
130 =item check
131
132 Checks all fields to make sure this is a valid payment.  If there is an error,
133 returns the error, otherwise returns false.  Called by the insert method.
134
135 =cut
136
137 sub check {
138   my $self = shift;
139
140   my $error = 
141     $self->ut_numbern('creditrefundnum')
142     || $self->ut_number('crednum')
143     || $self->ut_numner('refundnum')
144     || $self->ut_money('amount')
145     || $self->ut_numbern('_date')
146   ;
147   return $error if $error;
148
149   $self->_date(time) unless $self->_date;
150
151   return "unknown cust_credit.crednum: ". $self->crednum
152     unless qsearchs( 'cust_credit', { 'crednum' => $self->crednum } );
153
154   ''; #no error
155 }
156
157 =back
158
159 =head1 VERSION
160
161 $Id: cust_credit_refund.pm,v 1.1 2001-09-01 20:11:07 ivan Exp $
162
163 =head1 BUGS
164
165 Delete and replace methods.
166
167 =head1 SEE ALSO
168
169 L<FS::cust_credit>, L<FS::cust_refund>, L<FS::Record>, schema.html from the
170 base documentation.
171
172 =cut
173
174 1;
175