things are starting to work again, sorta.
[freeside.git] / FS / FS / cust_credit_bill.pm
1 package FS::cust_credit_bill;
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;
10 use FS::cust_bill;
11
12 @ISA = qw( FS::Record );
13
14 =head1 NAME
15
16 FS::cust_credit_bill - Object methods for cust_credit_bill records
17
18 =head1 SYNOPSIS
19
20   use FS::cust_credit_bill;
21
22   $record = new FS::cust_credit_bill \%hash;
23   $record = new FS::cust_credit_bill { 'column' => 'value' };
24
25   $error = $record->insert;
26
27   $error = $new_record->replace($old_record);
28
29   $error = $record->delete;
30
31   $error = $record->check;
32
33 =head1 DESCRIPTION
34
35 An FS::cust_credit_bill object represents application of a credit (see
36 L<FS::cust_credit>) to a customer bill (see L<FS::cust_bill>).  FS::cust_credit
37 inherits from FS::Record.  The following fields are currently supported:
38
39 =over 4
40
41 =item crednum - primary key; credit being applied 
42
43 =item invnum - invoice to which credit is applied (see L<FS::cust_bill>)
44
45 =item amount - amount of the credit applied
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 =back
51
52 =head1 METHODS
53
54 =over 4
55
56 =item new HASHREF
57
58 Creates a new cust_credit_bill.  To add the cust_credit_bill to the database,
59 see L<"insert">.
60
61 =cut
62
63 sub table { 'cust_credit_bill'; }
64
65 =item insert
66
67 Adds this cust_credit_bill to the database ("Posts" all or part of a credit).
68 If there is an error, returns the error, otherwise returns false.
69
70 =item delete
71
72 Currently unimplemented.
73
74 =cut
75
76 sub delete {
77   return "Can't unapply credit!"
78 }
79
80 =item replace OLD_RECORD
81
82 Application of credits may not be modified.
83
84 =cut
85
86 sub replace {
87   return "Can't modify application of credit!"
88 }
89
90 =item check
91
92 Checks all fields to make sure this is a valid credit application.  If there
93 is an error, returns the error, otherwise returns false.  Called by the insert
94 and replace methods.
95
96 =cut
97
98 sub check {
99   my $self = shift;
100
101   my $error =
102     $self->ut_numbern('creditbillnum')
103     || $self->ut_number('crednum')
104     || $self->ut_number('invnum')
105     || $self->ut_numbern('_date')
106     || $self->ut_money('amount')
107   ;
108   return $error if $error;
109
110   return "amount must be > 0" if $self->amount == 0;
111
112   return "Unknown credit"
113     unless my $cust_credit = 
114       qsearchs( 'cust_credit', { 'crednum' => $self->crednum } );
115
116   return "Unknown invoice"
117     unless my $cust_bill =
118       qsearchs( 'cust_bill', { 'invnum' => $self->invnum } );
119
120   $self->_date(time) unless $self->_date;
121
122   return "Cannot apply more than remaining value of credit memo"
123     unless $self->amount <= $cust_credit->credited;
124
125   return "Cannot apply more than remaining value of invoice"
126     unless $self->amount <= $cust_bill->owed;
127
128   ''; #no error
129 }
130
131 =item sub cust_credit
132
133 Returns the credit (see L<FS::cust_credit>)
134
135 =cut
136
137 sub cust_credit {
138   my $self = shift;
139   qsearchs( 'cust_credit', { 'crednum' => $self->crednum } );
140 }
141
142 =back
143
144 =head1 VERSION
145
146 $Id: cust_credit_bill.pm,v 1.4 2001-09-02 07:49:52 ivan Exp $
147
148 =head1 BUGS
149
150 The delete method.
151
152 =head1 SEE ALSO
153
154 L<FS::Record>, L<FS::cust_refund>, L<FS::cust_bill>, L<cust_credit>,
155 schema.html from the base documentation.
156
157 =cut
158
159 1;
160