0892f7984c68698015b6928262f164f472b1c6f4
[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 "Unknown credit"
111     unless my $cust_credit = 
112       qsearchs( 'cust_credit', { 'crednum' => $self->crednum } );
113
114   return "Unknown invoice"
115     unless my $cust_bill =
116       qsearchs( 'cust_bill', { 'invnum' => $self->invnum } );
117
118   $self->_date(time) unless $self->_date;
119
120   return "Cannot apply more than remaining value of credit memo"
121     unless $self->amount <= $cust_credit->credited;
122
123   return "Cannot apply more than remaining value of invoice"
124     unless $self->amount <= $cust_bill->owed;
125
126   ''; #no error
127 }
128
129 =item sub cust_credit
130
131 Returns the credit (see L<FS::cust_credit>)
132
133 =cut
134
135 sub cust_credit {
136   my $self = shift;
137   qsearchs( 'cust_credit', { 'crednum' => $self->crednum } );
138 }
139
140 =back
141
142 =head1 VERSION
143
144 $Id: cust_credit_bill.pm,v 1.3 2001-09-02 01:27:11 ivan Exp $
145
146 =head1 BUGS
147
148 The delete method.
149
150 =head1 SEE ALSO
151
152 L<FS::Record>, L<FS::cust_refund>, L<FS::cust_bill>, L<cust_credit>,
153 schema.html from the base documentation.
154
155 =cut
156
157 1;
158