62215419ce5183d776719180658f0ece4b8cc984
[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 an invoice (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 creditbillnum - primary key
42
43 =item crednum - credit being applied 
44
45 =item invnum - invoice to which credit is applied (see L<FS::cust_bill>)
46
47 =item amount - amount of the credit applied
48
49 =item _date - specified as a UNIX timestamp; see L<perlfunc/"time">.  Also see
50 L<Time::Local> and L<Date::Parse> for conversion functions.
51
52 =back
53
54 =head1 METHODS
55
56 =over 4
57
58 =item new HASHREF
59
60 Creates a new cust_credit_bill.  To add the cust_credit_bill to the database,
61 see L<"insert">.
62
63 =cut
64
65 sub table { 'cust_credit_bill'; }
66
67 =item insert
68
69 Adds this cust_credit_bill to the database ("Posts" all or part of a credit).
70 If there is an error, 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 unapply credit!"
80 }
81
82 =item replace OLD_RECORD
83
84 Application of credits may not be modified.
85
86 =cut
87
88 sub replace {
89   return "Can't modify application of credit!"
90 }
91
92 =item check
93
94 Checks all fields to make sure this is a valid credit application.  If there
95 is an error, returns the error, otherwise returns false.  Called by the insert
96 and replace methods.
97
98 =cut
99
100 sub check {
101   my $self = shift;
102
103   my $error =
104     $self->ut_numbern('creditbillnum')
105     || $self->ut_number('crednum')
106     || $self->ut_number('invnum')
107     || $self->ut_numbern('_date')
108     || $self->ut_money('amount')
109   ;
110   return $error if $error;
111
112   return "amount must be > 0" if $self->amount <= 0;
113
114   return "Unknown credit"
115     unless my $cust_credit = 
116       qsearchs( 'cust_credit', { 'crednum' => $self->crednum } );
117
118   return "Unknown invoice"
119     unless my $cust_bill =
120       qsearchs( 'cust_bill', { 'invnum' => $self->invnum } );
121
122   $self->_date(time) unless $self->_date;
123
124   return "Cannot apply more than remaining value of credit"
125     unless $self->amount <= $cust_credit->credited;
126
127   return "Cannot apply more than remaining value of invoice"
128     unless $self->amount <= $cust_bill->owed;
129
130   ''; #no error
131 }
132
133 =item sub cust_credit
134
135 Returns the credit (see L<FS::cust_credit>)
136
137 =cut
138
139 sub cust_credit {
140   my $self = shift;
141   qsearchs( 'cust_credit', { 'crednum' => $self->crednum } );
142 }
143
144 =back
145
146 =head1 VERSION
147
148 $Id: cust_credit_bill.pm,v 1.7 2002-01-24 16:58:47 ivan Exp $
149
150 =head1 BUGS
151
152 The delete method.
153
154 =head1 SEE ALSO
155
156 L<FS::Record>, L<FS::cust_refund>, L<FS::cust_bill>, L<FS::cust_credit>,
157 schema.html from the base documentation.
158
159 =cut
160
161 1;
162