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 Returns all refunds (see L<FS::cust_refund>) for this credit.
131 sort { $a->_date <=> $b->_date }
132 qsearch( 'cust_refund', { 'crednum' => $self->crednum } )
136 =item cust_credit_bill
138 Returns all application to invoices (see L<FS::cust_credit_bill>) for this
143 sub cust_credit_bill {
145 sort { $a->_date <=> $b->_date }
146 qsearch( 'cust_credit_bill', { 'crednum' => $self->crednum } )
152 Returns the amount of this credit that is still outstanding; which is
153 amount minus all refunds (see L<FS::cust_refund>) and applications to
154 invoices (see L<FS::cust_credit_bill>).
160 my $amount = $self->amount;
161 $amount -= $_->refund foreach ( $self->cust_refund );
162 $amount -= $_->amount foreach ( $self->cust_credit_bill );
163 sprintf( "%.2f", $amount );
170 $Id: cust_credit.pm,v 1.9 2001-09-01 21:52:19 jeff Exp $
178 L<FS::Record>, L<FS::cust_refund>, L<FS::cust_bill>, schema.html from the base