documentation updates from jason
[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( 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; 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:
35
36 =over 4
37
38 =item crednum - primary key (assigned automatically for new credits)
39
40 =item custnum - customer (see L<FS::cust_main>)
41
42 =item amount - amount of the credit
43
44 =item credited - how much of this credit that is still outstanding, which is
45 amount minus all refunds (see L<FS::cust_refund>).
46
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.
49
50 =item otaker - order taker (assigned automatically, see L<FS::UID>)
51
52 =item reason - text
53
54 =back
55
56 =head1 METHODS
57
58 =over 4
59
60 =item new HASHREF
61
62 Creates a new credit.  To add the credit to the database, see L<"insert">.
63
64 =cut
65
66 sub table { 'cust_credit'; }
67
68 =item insert
69
70 Adds this credit to the database ("Posts" the credit).  If there is an error,
71 returns the error, otherwise returns false.
72
73 When adding new invoices, credited must be amount (or null, in which case it is
74 automatically set to amount).
75
76 =cut
77
78 sub insert {
79   my $self = shift;
80
81   my $error;
82   return $error if $error = $self->ut_money('credited')
83                          || $self->ut_money('amount');
84
85   $self->credited($self->amount) if $self->credited == 0
86                                  || $self->credited eq '';
87   return "credited != amount!"
88     unless $self->credited == $self->amount;
89
90   $self->SUPER::insert;
91 }
92
93 =item delete
94
95 Currently unimplemented.
96
97 =cut
98
99 sub delete {
100   return "Can't remove credit!"
101 }
102
103 =item replace OLD_RECORD
104
105 Replaces the OLD_RECORD with this one in the database.  If there is an error,
106 returns the error, otherwise returns false.
107
108 Only credited may be changed.  Credited is normally updated by creating and
109 inserting a refund (see L<FS::cust_refund>).
110
111 =cut
112
113 sub replace {
114   my ( $new, $old ) = ( shift, shift );
115
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;
122
123   $new->SUPER::replace($old);
124 }
125
126 =item check
127
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
130 methods.
131
132 =cut
133
134 sub check {
135   my $self = shift;
136
137   my $error =
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');
144   ;
145   return $error if $error;
146
147   return "Unknown customer"
148     unless qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
149
150   $self->_date(time) unless $self->_date;
151
152   $self->otaker(getotaker);
153
154   ''; #no error
155 }
156
157 =back
158
159 =head1 VERSION
160
161 $Id: cust_credit.pm,v 1.2 2001-02-11 17:17:39 ivan Exp $
162
163 =head1 BUGS
164
165 The delete method.
166
167 =head1 SEE ALSO
168
169 L<FS::Record>, L<FS::cust_refund>, L<FS::cust_bill>, schema.html from the base
170 documentation.
171
172 =cut
173
174 1;
175