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