more udpates for the new world of unapplied stuff. yay.
[freeside.git] / FS / FS / cust_credit.pm
1 package FS::cust_credit;
2
3 use strict;
4 use vars qw( @ISA );
5 use FS::UID qw( getotaker );
6 use FS::Record qw( qsearch qsearchs );
7 use FS::cust_main;
8 use FS::cust_refund;
9 use FS::cust_credit_bill;
10
11 @ISA = qw( FS::Record );
12
13 =head1 NAME
14
15 FS::cust_credit - Object methods for cust_credit records
16
17 =head1 SYNOPSIS
18
19   use FS::cust_credit;
20
21   $record = new FS::cust_credit \%hash;
22   $record = new FS::cust_credit { 'column' => 'value' };
23
24   $error = $record->insert;
25
26   $error = $new_record->replace($old_record);
27
28   $error = $record->delete;
29
30   $error = $record->check;
31
32 =head1 DESCRIPTION
33
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:
37
38 =over 4
39
40 =item crednum - primary key (assigned automatically for new credits)
41
42 =item custnum - customer (see L<FS::cust_main>)
43
44 =item amount - amount of the credit
45
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.
48
49 =item otaker - order taker (assigned automatically, see L<FS::UID>)
50
51 =item reason - text
52
53 =back
54
55 =head1 METHODS
56
57 =over 4
58
59 =item new HASHREF
60
61 Creates a new credit.  To add the credit to the database, see L<"insert">.
62
63 =cut
64
65 sub table { 'cust_credit'; }
66
67 =item insert
68
69 Adds this credit to the database ("Posts" the credit).  If there is an error,
70 returns the error, otherwise returns false.
71
72 =item delete
73
74 Currently unimplemented.
75
76 =cut
77
78 sub delete {
79   return "Can't remove credit!"
80 }
81
82 =item replace OLD_RECORD
83
84 Credits may not be modified; there would then be no record the credit was ever
85 posted.
86
87 =cut
88
89 sub replace {
90   return "Can't modify credit!"
91 }
92
93 =item check
94
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
97 methods.
98
99 =cut
100
101 sub check {
102   my $self = shift;
103
104   my $error =
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');
110   ;
111   return $error if $error;
112
113   return "Unknown customer"
114     unless qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
115
116   $self->_date(time) unless $self->_date;
117
118   $self->otaker(getotaker);
119
120   ''; #no error
121 }
122
123 =item cust_refund
124
125 Depreciated.  See the cust_credit_refund method.
126
127 #Returns all refunds (see L<FS::cust_refund>) for this credit.
128
129 =cut
130
131 sub cust_refund {
132   use Carp;
133   croak "FS::cust_credit->cust_pay depreciated; see ".
134         "FS::cust_credit->cust_credit_refund";
135   #my $self = shift;
136   #sort { $a->_date <=> $b->_date }
137   #  qsearch( 'cust_refund', { 'crednum' => $self->crednum } )
138   #;
139 }
140
141 =item cust_credit_refund
142
143 Returns all refund applications (see L<FS::cust_credit_refund>) for this credit.
144
145 =cut
146
147 sub cust_credit_refund {
148   my $self = shift;
149   sort { $a->_date <=> $b->_date }
150     qsearch( 'cust_credit_refund', { 'crednum' => $self->crednum } )
151   ;
152 }
153
154 =item cust_credit_bill
155
156 Returns all application to invoices (see L<FS::cust_credit_bill>) for this
157 credit.
158
159 =cut
160
161 sub cust_credit_bill {
162   my $self = shift;
163   sort { $a->_date <=> $b->_date }
164     qsearch( 'cust_credit_bill', { 'crednum' => $self->crednum } )
165   ;
166 }
167
168 =item credited
169
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>).
173
174 =cut
175
176 sub credited {
177   my $self = shift;
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 );
182 }
183
184 =back
185
186 =head1 VERSION
187
188 $Id: cust_credit.pm,v 1.11 2001-09-02 07:49:52 ivan Exp $
189
190 =head1 BUGS
191
192 The delete method.
193
194 =head1 SEE ALSO
195
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
198 documentation.
199
200 =cut
201
202 1;
203