a050e302131a48b46200296131a13c94c7f6dcb0
[freeside.git] / site_perl / 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( qsearchs );
7 use FS::cust_main;
8
9 @ISA = qw( FS::Record );
10
11 =head1 NAME
12
13 FS::cust_credit - Object methods for cust_credit records
14
15 =head1 SYNOPSIS
16
17   use FS::cust_credit;
18
19   $record = new FS::cust_credit \%hash;
20   $record = new FS::cust_credit { 'column' => 'value' };
21
22   $error = $record->insert;
23
24   $error = $new_record->replace($old_record);
25
26   $error = $record->delete;
27
28   $error = $record->check;
29
30 =head1 DESCRIPTION
31
32 An FS::cust_credit object represents a credit.  FS::cust_credit inherits from
33 FS::Record.  The following fields are currently supported:
34
35 =over 4
36
37 =item crednum - primary key (assigned automatically for new credits)
38
39 =item custnum - customer (see L<FS::cust_main>)
40
41 =item amount - amount of the credit
42
43 =item credited - how much of this credit that is still outstanding, which is
44 amount minus all refunds (see L<FS::cust_refund>).
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 When adding new invoices, credited must be amount (or null, in which case it is
73 automatically set to amount).
74
75 =cut
76
77 sub insert {
78   my $self = shift;
79
80   $self->credited($self->amount) if $self->credited eq '';
81   return "credited != amount!"
82     unless $self->credited == $self->amount;
83
84   $self->SUPER::insert;
85 }
86
87 =item delete
88
89 Currently unimplemented.
90
91 =cut
92
93 sub delete {
94   return "Can't remove credit!"
95 }
96
97 =item replace OLD_RECORD
98
99 Replaces the OLD_RECORD with this one in the database.  If there is an error,
100 returns the error, otherwise returns false.
101
102 Only credited may be changed.  Credited is normally updated by creating and
103 inserting a refund (see L<FS::cust_refund>).
104
105 =cut
106
107 sub replace {
108   my ( $new, $old ) = ( shift, shift );
109
110   return "Can't change custnum!" unless $old->custnum == $new->custnum;
111   #return "Can't change date!" unless $old->_date eq $new->_date;
112   return "Can't change date!" unless $old->_date == $new->_date;
113   return "Can't change amount!" unless $old->amount == $new->amount;
114   return "(New) credited can't be > (new) amount!"
115     if $new->credited > $new->amount;
116
117   $new->SUPER::replace($old);
118 }
119
120 =item check
121
122 Checks all fields to make sure this is a valid credit.  If there is an error,
123 returns the error, otherwise returns false.  Called by the insert and replace
124 methods.
125
126 =cut
127
128 sub check {
129   my $self = shift;
130
131   my $error =
132     $self->ut_numbern('crednum')
133     || $self->ut_number('custnum')
134     || $self->ut_numbern('_date')
135     || $self->ut_money('amount')
136     || $self->ut_money('credited')
137     || $self->ut_textn('reason');
138   ;
139   return $error if $error;
140
141   return "Unknown customer"
142     unless qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
143
144   $self->_date(time) unless $self->_date;
145
146   $self->otaker(getotaker);
147
148   ''; #no error
149 }
150
151 =back
152
153 =head1 VERSION
154
155 $Id: cust_credit.pm,v 1.3 1999-01-18 21:58:04 ivan Exp $
156
157 =head1 BUGS
158
159 The delete method.
160
161 =head1 SEE ALSO
162
163 L<FS::Record>, L<FS::cust_refund>, L<FS::cust_bill>, schema.html from the base
164 documentation.
165
166 =head1 HISTORY
167
168 ivan@sisd.com 98-mar-17
169
170 pod, otaker from FS::UID ivan@sisd.com 98-sep-21
171
172 $Log: cust_credit.pm,v $
173 Revision 1.3  1999-01-18 21:58:04  ivan
174 esthetic: eq and ne were used in a few places instead of == and !=
175
176 Revision 1.2  1998/12/29 11:59:38  ivan
177 mostly properly OO, some work still to be done with svc_ stuff
178
179
180 =cut
181
182 1;
183