1 package FS::cust_credit;
4 use vars qw(@ISA @EXPORT_OK);
6 use FS::UID qw(getotaker);
7 use FS::Record qw(fields qsearchs);
9 @ISA = qw(FS::Record Exporter);
10 @EXPORT_OK = qw(fields);
14 FS::cust_credit - Object methods for cust_credit records
20 $record = create FS::cust_credit \%hash;
21 $record = create FS::cust_credit { 'column' => 'value' };
23 $error = $record->insert;
25 $error = $new_record->replace($old_record);
27 $error = $record->delete;
29 $error = $record->check;
33 An FS::cust_credit object represents a credit. 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">.
67 my($proto,$hashref)=@_;
69 #now in FS::Record::new
71 #foreach $field (fields('cust_credit')) {
72 # $hashref->{$field}='' unless defined $hashref->{$field};
75 $proto->new('cust_credit',$hashref);
80 Adds this credit to the database ("Posts" the credit). If there is an error,
81 returns the error, otherwise returns false.
83 When adding new invoices, credited must be amount (or null, in which case it is
84 automatically set to amount).
91 $self->setfield('credited',$self->amount) if $self->credited eq '';
92 return "credited != amount!"
93 unless $self->credited == $self->amount;
101 Currently unimplemented.
106 return "Can't remove credit!"
111 =item replace OLD_RECORD
113 Replaces the OLD_RECORD with this one in the database. If there is an error,
114 returns the error, otherwise returns false.
116 Only credited may be changed. Credited is normally updated by creating and
117 inserting a refund (see L<FS::cust_refund>).
123 return "(Old) Not a cust_credit record!" unless $old->table eq "cust_credit";
124 return "Can't change crednum!"
125 unless $old->getfield('crednum') eq $new->getfield('crednum');
126 return "Can't change custnum!"
127 unless $old->getfield('custnum') eq $new->getfield('custnum');
128 return "Can't change date!"
129 unless $old->getfield('_date') eq $new->getfield('_date');
130 return "Can't change amount!"
131 unless $old->getfield('amount') eq $new->getfield('amount');
132 return "(New) credited can't be > (new) amount!"
133 if $new->getfield('credited') > $new->getfield('amount');
141 Checks all fields to make sure this is a valid credit. If there is an error,
142 returns the error, otherwise returns false. Called by the insert and replace
149 return "Not a cust_credit record!" unless $self->table eq "cust_credit";
150 my($recref) = $self->hashref;
152 $recref->{crednum} =~ /^(\d*)$/ or return "Illegal crednum";
153 $recref->{crednum} = $1;
155 $recref->{custnum} =~ /^(\d+)$/ or return "Illegal custnum";
156 $recref->{custnum} = $1;
157 return "Unknown customer"
158 unless qsearchs('cust_main',{'custnum'=>$recref->{custnum}});
160 $recref->{_date} =~ /^(\d*)$/ or return "Illegal date";
161 $recref->{_date} = $recref->{_date} ? $1 : time;
163 $recref->{amount} =~ /^(\d+(\.\d\d)?)$/ or return "Illegal amount";
164 $recref->{amount} = $1;
166 $recref->{credited} =~ /^(\-?\d+(\.\d\d)?)$/ or return "Illegal credited";
167 $recref->{credited} = $1;
169 #$recref->{otaker} =~ /^(\w+)$/ or return "Illegal otaker";
170 #$recref->{otaker} = $1;
171 $self->otaker(getotaker);
173 $self->ut_textn('reason');
183 It doesn't properly override FS::Record yet.
187 L<FS::Record>, L<FS::cust_refund>, L<FS::cust_bill>, schema.html from the base
192 ivan@sisd.com 98-mar-17
194 pod, otaker from FS::UID ivan@sisd.com 98-sep-21