1 package FS::cust_credit;
5 use FS::UID qw( getotaker );
6 use FS::Record qw( qsearch qsearchs );
9 use FS::cust_credit_bill;
11 @ISA = qw( FS::Record );
15 FS::cust_credit - Object methods for cust_credit records
21 $record = new FS::cust_credit \%hash;
22 $record = new FS::cust_credit { 'column' => 'value' };
24 $error = $record->insert;
26 $error = $new_record->replace($old_record);
28 $error = $record->delete;
30 $error = $record->check;
34 An FS::cust_credit object represents a credit; the equivalent of a negative
35 B<cust_bill> record (see L<FS::cust_bill>). FS::cust_credit inherits from
36 FS::Record. The following fields are currently supported:
40 =item crednum - primary key (assigned automatically for new credits)
42 =item custnum - customer (see L<FS::cust_main>)
44 =item amount - amount of the credit
46 =item _date - specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
47 L<Time::Local> and L<Date::Parse> for conversion functions.
49 =item otaker - order taker (assigned automatically, see L<FS::UID>)
61 Creates a new credit. To add the credit to the database, see L<"insert">.
65 sub table { 'cust_credit'; }
69 Adds this credit to the database ("Posts" the credit). If there is an error,
70 returns the error, otherwise returns false.
74 Currently unimplemented.
79 return "Can't remove credit!"
82 =item replace OLD_RECORD
84 Credits may not be modified; there would then be no record the credit was ever
90 return "Can't modify credit!"
95 Checks all fields to make sure this is a valid credit. If there is an error,
96 returns the error, otherwise returns false. Called by the insert and replace
105 $self->ut_numbern('crednum')
106 || $self->ut_number('custnum')
107 || $self->ut_numbern('_date')
108 || $self->ut_money('amount')
109 || $self->ut_textn('reason');
111 return $error if $error;
113 return "Unknown customer"
114 unless qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
116 $self->_date(time) unless $self->_date;
118 $self->otaker(getotaker);
125 Depreciated. See the cust_credit_refund method.
127 #Returns all refunds (see L<FS::cust_refund>) for this credit.
133 croak "FS::cust_credit->cust_pay depreciated; see ".
134 "FS::cust_credit->cust_credit_refund";
136 #sort { $a->_date <=> $b->_date }
137 # qsearch( 'cust_refund', { 'crednum' => $self->crednum } )
141 =item cust_credit_refund
143 Returns all refund applications (see L<FS::cust_credit_refund>) for this credit.
147 sub cust_credit_refund {
149 sort { $a->_date <=> $b->_date }
150 qsearch( 'cust_credit_refund', { 'crednum' => $self->crednum } )
154 =item cust_credit_bill
156 Returns all application to invoices (see L<FS::cust_credit_bill>) for this
161 sub cust_credit_bill {
163 sort { $a->_date <=> $b->_date }
164 qsearch( 'cust_credit_bill', { 'crednum' => $self->crednum } )
170 Returns the amount of this credit that is still outstanding; which is
171 amount minus all refund applications (see L<FS::cust_credit_refund>) and
172 applications to invoices (see L<FS::cust_credit_bill>).
178 my $amount = $self->amount;
179 $amount -= $_->amount foreach ( $self->cust_credit_refund );
180 $amount -= $_->amount foreach ( $self->cust_credit_bill );
181 sprintf( "%.2f", $amount );
188 $Id: cust_credit.pm,v 1.11 2001-09-02 07:49:52 ivan Exp $
196 L<FS::Record>, L<FS::cust_credit_refund>, L<FS::cust_refund>,
197 L<FS::cust_credit_bill> L<FS::cust_bill>, schema.html from the base