0ce5ac6143037421ccfe4d5261eee591db08dbaa
[freeside.git] / FS / FS / cust_credit.pm
1 package FS::cust_credit;
2
3 use strict;
4 use vars qw( @ISA $conf $unsuspendauto );
5 use FS::UID qw( dbh getotaker );
6 use FS::Record qw( qsearch qsearchs );
7 use FS::cust_main;
8 use FS::cust_refund;
9 use FS::cust_credit_bill;
10
11 @ISA = qw( FS::Record );
12
13 #ask FS::UID to run this stuff for us later
14 $FS::UID::callback{'FS::cust_credit'} = sub { 
15
16   $conf = new FS::Conf;
17   $unsuspendauto = $conf->exists('unsuspendauto');
18
19 };
20
21 =head1 NAME
22
23 FS::cust_credit - Object methods for cust_credit records
24
25 =head1 SYNOPSIS
26
27   use FS::cust_credit;
28
29   $record = new FS::cust_credit \%hash;
30   $record = new FS::cust_credit { 'column' => 'value' };
31
32   $error = $record->insert;
33
34   $error = $new_record->replace($old_record);
35
36   $error = $record->delete;
37
38   $error = $record->check;
39
40 =head1 DESCRIPTION
41
42 An FS::cust_credit object represents a credit; the equivalent of a negative
43 B<cust_bill> record (see L<FS::cust_bill>).  FS::cust_credit inherits from
44 FS::Record.  The following fields are currently supported:
45
46 =over 4
47
48 =item crednum - primary key (assigned automatically for new credits)
49
50 =item custnum - customer (see L<FS::cust_main>)
51
52 =item amount - amount of the credit
53
54 =item _date - specified as a UNIX timestamp; see L<perlfunc/"time">.  Also see
55 L<Time::Local> and L<Date::Parse> for conversion functions.
56
57 =item otaker - order taker (assigned automatically, see L<FS::UID>)
58
59 =item reason - text
60
61 =item closed - books closed flag, empty or `Y'
62
63 =back
64
65 =head1 METHODS
66
67 =over 4
68
69 =item new HASHREF
70
71 Creates a new credit.  To add the credit to the database, see L<"insert">.
72
73 =cut
74
75 sub table { 'cust_credit'; }
76
77 =item insert
78
79 Adds this credit to the database ("Posts" the credit).  If there is an error,
80 returns the error, otherwise returns false.
81
82 =cut
83
84 sub insert {
85   my $self = shift;
86
87   local $SIG{HUP} = 'IGNORE';
88   local $SIG{INT} = 'IGNORE';
89   local $SIG{QUIT} = 'IGNORE';
90   local $SIG{TERM} = 'IGNORE';
91   local $SIG{TSTP} = 'IGNORE';
92   local $SIG{PIPE} = 'IGNORE';
93
94   my $oldAutoCommit = $FS::UID::AutoCommit;
95   local $FS::UID::AutoCommit = 0;
96   my $dbh = dbh;
97
98   my $cust_main = qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
99   my $old_balance = $cust_main->balance;
100
101   my $error = $self->SUPER::insert;
102   if ( $error ) {
103     $dbh->rollback if $oldAutoCommit;
104     return "error inserting $self: $error";
105   }
106
107   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
108
109   #false laziness w/ cust_credit::insert
110   if ( $unsuspendauto && $old_balance && $cust_main->balance <= 0 ) {
111     my @errors = $cust_main->unsuspend;
112     #return 
113     # side-fx with nested transactions?  upstack rolls back?
114     warn "WARNING:Errors unsuspending customer ". $cust_main->custnum. ": ".
115          join(' / ', @errors)
116       if @errors;
117   }
118   #eslaf
119
120   '';
121
122 }
123
124 =item delete
125
126 Currently unimplemented.
127
128 =cut
129
130 sub delete {
131   my $self = shift;
132   return "Can't delete closed credit" if $self->closed =~ /^Y/i;
133   $self->SUPER::delete(@_);
134 }
135
136 =item replace OLD_RECORD
137
138 Credits may not be modified; there would then be no record the credit was ever
139 posted.
140
141 =cut
142
143 sub replace {
144   return "Can't modify credit!"
145 }
146
147 =item check
148
149 Checks all fields to make sure this is a valid credit.  If there is an error,
150 returns the error, otherwise returns false.  Called by the insert and replace
151 methods.
152
153 =cut
154
155 sub check {
156   my $self = shift;
157
158   my $error =
159     $self->ut_numbern('crednum')
160     || $self->ut_number('custnum')
161     || $self->ut_numbern('_date')
162     || $self->ut_money('amount')
163     || $self->ut_textn('reason')
164     || $self->ut_enum('closed', [ '', 'Y' ])
165   ;
166   return $error if $error;
167
168   return "amount must be > 0 " if $self->amount <= 0;
169
170   return "Unknown customer"
171     unless qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
172
173   $self->_date(time) unless $self->_date;
174
175   $self->otaker(getotaker);
176
177   ''; #no error
178 }
179
180 =item cust_refund
181
182 Depreciated.  See the cust_credit_refund method.
183
184 #Returns all refunds (see L<FS::cust_refund>) for this credit.
185
186 =cut
187
188 sub cust_refund {
189   use Carp;
190   croak "FS::cust_credit->cust_pay depreciated; see ".
191         "FS::cust_credit->cust_credit_refund";
192   #my $self = shift;
193   #sort { $a->_date <=> $b->_date }
194   #  qsearch( 'cust_refund', { 'crednum' => $self->crednum } )
195   #;
196 }
197
198 =item cust_credit_refund
199
200 Returns all refund applications (see L<FS::cust_credit_refund>) for this credit.
201
202 =cut
203
204 sub cust_credit_refund {
205   my $self = shift;
206   sort { $a->_date <=> $b->_date }
207     qsearch( 'cust_credit_refund', { 'crednum' => $self->crednum } )
208   ;
209 }
210
211 =item cust_credit_bill
212
213 Returns all application to invoices (see L<FS::cust_credit_bill>) for this
214 credit.
215
216 =cut
217
218 sub cust_credit_bill {
219   my $self = shift;
220   sort { $a->_date <=> $b->_date }
221     qsearch( 'cust_credit_bill', { 'crednum' => $self->crednum } )
222   ;
223 }
224
225 =item credited
226
227 Returns the amount of this credit that is still outstanding; which is
228 amount minus all refund applications (see L<FS::cust_credit_refund>) and
229 applications to invoices (see L<FS::cust_credit_bill>).
230
231 =cut
232
233 sub credited {
234   my $self = shift;
235   my $amount = $self->amount;
236   $amount -= $_->amount foreach ( $self->cust_credit_refund );
237   $amount -= $_->amount foreach ( $self->cust_credit_bill );
238   sprintf( "%.2f", $amount );
239 }
240
241 =back
242
243 =head1 VERSION
244
245 $Id: cust_credit.pm,v 1.15 2002-01-28 06:57:23 ivan Exp $
246
247 =head1 BUGS
248
249 The delete method.
250
251 =head1 SEE ALSO
252
253 L<FS::Record>, L<FS::cust_credit_refund>, L<FS::cust_refund>,
254 L<FS::cust_credit_bill> L<FS::cust_bill>, schema.html from the base
255 documentation.
256
257 =cut
258
259 1;
260