1 package FS::cust_credit;
4 use vars qw( @ISA $conf $unsuspendauto );
5 use FS::UID qw( dbh getotaker );
6 use FS::Record qw( qsearch qsearchs );
9 use FS::cust_credit_bill;
11 @ISA = qw( FS::Record );
13 #ask FS::UID to run this stuff for us later
14 $FS::UID::callback{'FS::cust_credit'} = sub {
17 $unsuspendauto = $conf->exists('unsuspendauto');
23 FS::cust_credit - Object methods for cust_credit records
29 $record = new FS::cust_credit \%hash;
30 $record = new FS::cust_credit { 'column' => 'value' };
32 $error = $record->insert;
34 $error = $new_record->replace($old_record);
36 $error = $record->delete;
38 $error = $record->check;
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:
48 =item crednum - primary key (assigned automatically for new credits)
50 =item custnum - customer (see L<FS::cust_main>)
52 =item amount - amount of the credit
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.
57 =item otaker - order taker (assigned automatically, see L<FS::UID>)
61 =item closed - books closed flag, empty or `Y'
71 Creates a new credit. To add the credit to the database, see L<"insert">.
75 sub table { 'cust_credit'; }
79 Adds this credit to the database ("Posts" the credit). If there is an error,
80 returns the error, otherwise returns false.
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';
94 my $oldAutoCommit = $FS::UID::AutoCommit;
95 local $FS::UID::AutoCommit = 0;
98 my $cust_main = qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
99 my $old_balance = $cust_main->balance;
101 my $error = $self->SUPER::insert;
103 $dbh->rollback if $oldAutoCommit;
104 return "error inserting $self: $error";
107 #false laziness w/ cust_credit::insert
108 if ( $unsuspendauto && $old_balance && $cust_main->balance <= 0 ) {
109 my @errors = $cust_main->unsuspend;
111 # side-fx with nested transactions? upstack rolls back?
112 warn "WARNING:Errors unsuspending customer ". $cust_main->custnum. ": ".
118 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
126 Currently unimplemented.
132 return "Can't delete closed credit" if $self->closed =~ /^Y/i;
133 $self->SUPER::delete(@_);
136 =item replace OLD_RECORD
138 Credits may not be modified; there would then be no record the credit was ever
144 #return "Can't modify credit!"
146 return "Can't modify closed credit" if $self->closed =~ /^Y/i;
147 $self->SUPER::replace(@_);
152 Checks all fields to make sure this is a valid credit. If there is an error,
153 returns the error, otherwise returns false. Called by the insert and replace
162 $self->ut_numbern('crednum')
163 || $self->ut_number('custnum')
164 || $self->ut_numbern('_date')
165 || $self->ut_money('amount')
166 || $self->ut_textn('reason')
167 || $self->ut_enum('closed', [ '', 'Y' ])
169 return $error if $error;
171 return "amount must be > 0 " if $self->amount <= 0;
173 return "Unknown customer"
174 unless qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
176 $self->_date(time) unless $self->_date;
178 $self->otaker(getotaker);
185 Depreciated. See the cust_credit_refund method.
187 #Returns all refunds (see L<FS::cust_refund>) for this credit.
193 croak "FS::cust_credit->cust_pay depreciated; see ".
194 "FS::cust_credit->cust_credit_refund";
196 #sort { $a->_date <=> $b->_date }
197 # qsearch( 'cust_refund', { 'crednum' => $self->crednum } )
201 =item cust_credit_refund
203 Returns all refund applications (see L<FS::cust_credit_refund>) for this credit.
207 sub cust_credit_refund {
209 sort { $a->_date <=> $b->_date }
210 qsearch( 'cust_credit_refund', { 'crednum' => $self->crednum } )
214 =item cust_credit_bill
216 Returns all application to invoices (see L<FS::cust_credit_bill>) for this
221 sub cust_credit_bill {
223 sort { $a->_date <=> $b->_date }
224 qsearch( 'cust_credit_bill', { 'crednum' => $self->crednum } )
230 Returns the amount of this credit that is still outstanding; which is
231 amount minus all refund applications (see L<FS::cust_credit_refund>) and
232 applications to invoices (see L<FS::cust_credit_bill>).
238 my $amount = $self->amount;
239 $amount -= $_->amount foreach ( $self->cust_credit_refund );
240 $amount -= $_->amount foreach ( $self->cust_credit_bill );
241 sprintf( "%.2f", $amount );
248 The delete method. The replace method.
252 L<FS::Record>, L<FS::cust_credit_refund>, L<FS::cust_refund>,
253 L<FS::cust_credit_bill> L<FS::cust_bill>, schema.html from the base