4c5195717740d0f82afbf421bcd1f65bb13d02d5
[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 sub table { 'cust_credit_refund'; }
61
62 =item insert
63
64 Adds this record to the database.  If there is an error, returns the error,
65 otherwise returns false.
66
67 =cut
68
69 sub insert {
70   my $self = shift;
71
72   local $SIG{HUP} = 'IGNORE';
73   local $SIG{INT} = 'IGNORE';
74   local $SIG{QUIT} = 'IGNORE';
75   local $SIG{TERM} = 'IGNORE';
76   local $SIG{TSTP} = 'IGNORE';
77   local $SIG{PIPE} = 'IGNORE';
78
79   my $oldAutoCommit = $FS::UID::AutoCommit;
80   local $FS::UID::AutoCommit = 0;
81   my $dbh = dbh;
82
83   my $error = $self->check;
84   return $error if $error;
85
86   $error = $self->SUPER::insert;
87
88   my $cust_refund =
89     qsearchs('cust_refund', { 'refundnum' => $self->refundnum } )
90   or do {
91     $dbh->rollback if $oldAutoCommit;
92     return "unknown cust_refund.refundnum: ". $self->refundnum
93   };
94
95   my $refund_total = 0;
96   $refund_total += $_ foreach map { $_->amount }
97     qsearch('cust_credit_refund', { 'refundnum' => $self->refundnum } );
98
99   if ( $refund_total > $cust_refund->refund ) {
100     $dbh->rollback if $oldAutoCommit;
101     return "total cust_credit_refund.amount $refund_total for refundnum ".
102            $self->refundnum.
103            " greater than cust_refund.refund ". $cust_refund->refund;
104   }
105
106   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
107
108   '';
109 }
110
111 =item delete
112
113 Currently unimplemented (accounting reasons).
114
115 =cut
116
117 sub delete {
118   return "Can't (yet?) delete cust_credit_refund records!";
119 }
120
121 =item replace OLD_RECORD
122
123 Currently unimplemented (accounting reasons).
124
125 =cut
126
127 sub replace {
128    return "Can't (yet?) modify cust_credit_refund records!";
129 }
130
131 =item check
132
133 Checks all fields to make sure this is a valid payment.  If there is an error,
134 returns the error, otherwise returns false.  Called by the insert method.
135
136 =cut
137
138 sub check {
139   my $self = shift;
140
141   my $error = 
142     $self->ut_numbern('creditrefundnum')
143     || $self->ut_number('crednum')
144     || $self->ut_number('refundnum')
145     || $self->ut_money('amount')
146     || $self->ut_numbern('_date')
147   ;
148   return $error if $error;
149
150   $self->_date(time) unless $self->_date;
151
152   return "unknown cust_credit.crednum: ". $self->crednum
153     unless qsearchs( 'cust_credit', { 'crednum' => $self->crednum } );
154
155   ''; #no error
156 }
157
158 =item cust_refund
159
160 Returns the refund (see L<FS::cust_refund>)
161
162 =cut
163
164 sub cust_refund {
165   my $self = shift;
166   qsearchs( 'cust_refund', { 'refundnum' => $self->refundnum } );
167 }
168
169 =back
170
171 =head1 VERSION
172
173 $Id: cust_credit_refund.pm,v 1.3 2001-09-02 05:38:13 ivan Exp $
174
175 =head1 BUGS
176
177 Delete and replace methods.
178
179 the checks for over-applied refunds could be better done like the ones in
180 cust_bill_credit
181
182 =head1 SEE ALSO
183
184 L<FS::cust_credit>, L<FS::cust_refund>, L<FS::Record>, schema.html from the
185 base documentation.
186
187 =cut
188
189 1;
190