1 package FS::cust_credit;
5 use FS::UID qw( getotaker );
6 use FS::Record qw( qsearchs );
9 @ISA = qw( FS::Record );
13 FS::cust_credit - Object methods for cust_credit records
19 $record = new FS::cust_credit \%hash;
20 $record = new FS::cust_credit { 'column' => 'value' };
22 $error = $record->insert;
24 $error = $new_record->replace($old_record);
26 $error = $record->delete;
28 $error = $record->check;
32 An FS::cust_credit object represents a credit; the equivalent of a negative
33 B<cust_bill> record (see L<FS::cust_bill>). FS::cust_credit inherits from
34 FS::Record. The following fields are currently supported:
38 =item crednum - primary key (assigned automatically for new credits)
40 =item custnum - customer (see L<FS::cust_main>)
42 =item amount - amount of the credit
44 =item credited - how much of this credit that is still outstanding, which is
45 amount minus all refunds (see L<FS::cust_refund>).
47 =item _date - specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
48 L<Time::Local> and L<Date::Parse> for conversion functions.
50 =item otaker - order taker (assigned automatically, see L<FS::UID>)
62 Creates a new credit. To add the credit to the database, see L<"insert">.
66 sub table { 'cust_credit'; }
70 Adds this credit to the database ("Posts" the credit). If there is an error,
71 returns the error, otherwise returns false.
73 When adding new invoices, credited must be amount (or null, in which case it is
74 automatically set to amount).
82 return $error if $error = $self->ut_money('credited')
83 || $self->ut_money('amount');
85 $self->credited($self->amount) if $self->credited == 0
86 || $self->credited eq '';
87 return "credited != amount!"
88 unless $self->credited == $self->amount;
95 Currently unimplemented.
100 return "Can't remove credit!"
103 =item replace OLD_RECORD
105 Replaces the OLD_RECORD with this one in the database. If there is an error,
106 returns the error, otherwise returns false.
108 Only credited may be changed. Credited is normally updated by creating and
109 inserting a refund (see L<FS::cust_refund>).
114 my ( $new, $old ) = ( shift, shift );
116 return "Can't change custnum!" unless $old->custnum == $new->custnum;
117 #return "Can't change date!" unless $old->_date eq $new->_date;
118 return "Can't change date!" unless $old->_date == $new->_date;
119 return "Can't change amount!" unless $old->amount == $new->amount;
120 return "(New) credited can't be > (new) amount!"
121 if $new->credited > $new->amount;
123 $new->SUPER::replace($old);
128 Checks all fields to make sure this is a valid credit. If there is an error,
129 returns the error, otherwise returns false. Called by the insert and replace
138 $self->ut_numbern('crednum')
139 || $self->ut_number('custnum')
140 || $self->ut_numbern('_date')
141 || $self->ut_money('amount')
142 || $self->ut_money('credited')
143 || $self->ut_textn('reason');
145 return $error if $error;
147 return "Unknown customer"
148 unless qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
150 $self->_date(time) unless $self->_date;
152 $self->otaker(getotaker);
161 $Id: cust_credit.pm,v 1.2 2001-02-11 17:17:39 ivan Exp $
169 L<FS::Record>, L<FS::cust_refund>, L<FS::cust_bill>, schema.html from the base