don't allow $0.00 in credits/payments/refunds
[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 =back
62
63 =head1 METHODS
64
65 =over 4
66
67 =item new HASHREF
68
69 Creates a new credit.  To add the credit to the database, see L<"insert">.
70
71 =cut
72
73 sub table { 'cust_credit'; }
74
75 =item insert
76
77 Adds this credit to the database ("Posts" the credit).  If there is an error,
78 returns the error, otherwise returns false.
79
80 =cut
81
82 sub insert {
83   my $self = shift;
84
85   local $SIG{HUP} = 'IGNORE';
86   local $SIG{INT} = 'IGNORE';
87   local $SIG{QUIT} = 'IGNORE';
88   local $SIG{TERM} = 'IGNORE';
89   local $SIG{TSTP} = 'IGNORE';
90   local $SIG{PIPE} = 'IGNORE';
91
92   my $oldAutoCommit = $FS::UID::AutoCommit;
93   local $FS::UID::AutoCommit = 0;
94   my $dbh = dbh;
95
96   my $cust_main = qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
97   my $old_balance = $cust_main->balance;
98
99   my $error = $self->SUPER::insert;
100   if ( $error ) {
101     $dbh->rollback if $oldAutoCommit;
102     return "error inserting $self: $error";
103   }
104
105   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
106
107   #false laziness w/ cust_credit::insert
108   if ( $unsuspendauto && $old_balance && $cust_main->balance <= 0 ) {
109     my @errors = $cust_main->unsuspend;
110     #return 
111     # side-fx with nested transactions?  upstack rolls back?
112     warn "WARNING:Errors unsuspending customer ". $cust_main->custnum. ": ".
113          join(' / ', @errors)
114       if @errors;
115   }
116   #eslaf
117
118   '';
119
120 }
121
122 =item delete
123
124 Currently unimplemented.
125
126 =cut
127
128 sub delete {
129   return "Can't remove credit!"
130 }
131
132 =item replace OLD_RECORD
133
134 Credits may not be modified; there would then be no record the credit was ever
135 posted.
136
137 =cut
138
139 sub replace {
140   return "Can't modify credit!"
141 }
142
143 =item check
144
145 Checks all fields to make sure this is a valid credit.  If there is an error,
146 returns the error, otherwise returns false.  Called by the insert and replace
147 methods.
148
149 =cut
150
151 sub check {
152   my $self = shift;
153
154   my $error =
155     $self->ut_numbern('crednum')
156     || $self->ut_number('custnum')
157     || $self->ut_numbern('_date')
158     || $self->ut_money('amount')
159     || $self->ut_textn('reason');
160   ;
161   return $error if $error;
162
163   return "amount must be > 0 " if $self->amount == 0;
164
165   return "Unknown customer"
166     unless qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
167
168   $self->_date(time) unless $self->_date;
169
170   $self->otaker(getotaker);
171
172   ''; #no error
173 }
174
175 =item cust_refund
176
177 Depreciated.  See the cust_credit_refund method.
178
179 #Returns all refunds (see L<FS::cust_refund>) for this credit.
180
181 =cut
182
183 sub cust_refund {
184   use Carp;
185   croak "FS::cust_credit->cust_pay depreciated; see ".
186         "FS::cust_credit->cust_credit_refund";
187   #my $self = shift;
188   #sort { $a->_date <=> $b->_date }
189   #  qsearch( 'cust_refund', { 'crednum' => $self->crednum } )
190   #;
191 }
192
193 =item cust_credit_refund
194
195 Returns all refund applications (see L<FS::cust_credit_refund>) for this credit.
196
197 =cut
198
199 sub cust_credit_refund {
200   my $self = shift;
201   sort { $a->_date <=> $b->_date }
202     qsearch( 'cust_credit_refund', { 'crednum' => $self->crednum } )
203   ;
204 }
205
206 =item cust_credit_bill
207
208 Returns all application to invoices (see L<FS::cust_credit_bill>) for this
209 credit.
210
211 =cut
212
213 sub cust_credit_bill {
214   my $self = shift;
215   sort { $a->_date <=> $b->_date }
216     qsearch( 'cust_credit_bill', { 'crednum' => $self->crednum } )
217   ;
218 }
219
220 =item credited
221
222 Returns the amount of this credit that is still outstanding; which is
223 amount minus all refund applications (see L<FS::cust_credit_refund>) and
224 applications to invoices (see L<FS::cust_credit_bill>).
225
226 =cut
227
228 sub credited {
229   my $self = shift;
230   my $amount = $self->amount;
231   $amount -= $_->amount foreach ( $self->cust_credit_refund );
232   $amount -= $_->amount foreach ( $self->cust_credit_bill );
233   sprintf( "%.2f", $amount );
234 }
235
236 =back
237
238 =head1 VERSION
239
240 $Id: cust_credit.pm,v 1.13 2002-01-22 15:57:33 ivan Exp $
241
242 =head1 BUGS
243
244 The delete method.
245
246 =head1 SEE ALSO
247
248 L<FS::Record>, L<FS::cust_credit_refund>, L<FS::cust_refund>,
249 L<FS::cust_credit_bill> L<FS::cust_bill>, schema.html from the base
250 documentation.
251
252 =cut
253
254 1;
255