don't re-my var, quiet warning
[freeside.git] / FS / FS / cust_credit_bill.pm
1 package FS::cust_credit_bill;
2
3 use strict;
4 use vars qw( @ISA $conf );
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 #ask FS::UID to run this stuff for us later
15 FS::UID->install_callback( sub { 
16   $conf = new FS::Conf;
17 } );
18
19 =head1 NAME
20
21 FS::cust_credit_bill - Object methods for cust_credit_bill records
22
23 =head1 SYNOPSIS
24
25   use FS::cust_credit_bill;
26
27   $record = new FS::cust_credit_bill \%hash;
28   $record = new FS::cust_credit_bill { 'column' => 'value' };
29
30   $error = $record->insert;
31
32   $error = $new_record->replace($old_record);
33
34   $error = $record->delete;
35
36   $error = $record->check;
37
38 =head1 DESCRIPTION
39
40 An FS::cust_credit_bill object represents application of a credit (see
41 L<FS::cust_credit>) to an invoice (see L<FS::cust_bill>).  FS::cust_credit_bill
42 inherits from FS::Record.  The following fields are currently supported:
43
44 =over 4
45
46 =item creditbillnum - primary key
47
48 =item crednum - credit being applied 
49
50 =item invnum - invoice to which credit is applied (see L<FS::cust_bill>)
51
52 =item amount - amount of the credit applied
53
54 =item _date - specified as a UNIX timestamp; see L<perlfunc/"time">.  Also see
55 L<Time::Local> and L<Date::Parse> for conversion functions.
56
57 =back
58
59 =head1 METHODS
60
61 =over 4
62
63 =item new HASHREF
64
65 Creates a new cust_credit_bill.  To add the cust_credit_bill to the database,
66 see L<"insert">.
67
68 =cut
69
70 sub table { 'cust_credit_bill'; }
71
72 =item insert
73
74 Adds this cust_credit_bill to the database ("Posts" all or part of a credit).
75 If there is an error, returns the error, otherwise returns false.
76
77 =item delete
78
79 Currently unimplemented.
80
81 =cut
82
83 sub delete {
84   my $self = shift;
85   return "Can't delete application for closed credit"
86     if $self->cust_credit->closed =~ /^Y/i;
87   $self->SUPER::delete(@_);
88 }
89
90 =item replace OLD_RECORD
91
92 Application of credits may not be modified.
93
94 =cut
95
96 sub replace {
97   return "Can't modify application of credit!"
98 }
99
100 =item check
101
102 Checks all fields to make sure this is a valid credit application.  If there
103 is an error, returns the error, otherwise returns false.  Called by the insert
104 and replace methods.
105
106 =cut
107
108 sub check {
109   my $self = shift;
110
111   my $error =
112     $self->ut_numbern('creditbillnum')
113     || $self->ut_number('crednum')
114     || $self->ut_number('invnum')
115     || $self->ut_numbern('_date')
116     || $self->ut_money('amount')
117   ;
118   return $error if $error;
119
120   return "amount must be > 0" if $self->amount <= 0;
121
122   return "Unknown credit"
123     unless my $cust_credit = 
124       qsearchs( 'cust_credit', { 'crednum' => $self->crednum } );
125
126   return "Unknown invoice"
127     unless my $cust_bill =
128       qsearchs( 'cust_bill', { 'invnum' => $self->invnum } );
129
130   $self->_date(time) unless $self->_date;
131
132   return "Cannot apply more than remaining value of credit"
133     unless $self->amount <= $cust_credit->credited;
134
135   return "Cannot apply more than remaining value of invoice"
136     unless $self->amount <= $cust_bill->owed;
137
138   $self->SUPER::check;
139 }
140
141 =item sub cust_credit
142
143 Returns the credit (see L<FS::cust_credit>)
144
145 =cut
146
147 sub cust_credit {
148   my $self = shift;
149   qsearchs( 'cust_credit', { 'crednum' => $self->crednum } );
150 }
151
152 =item cust_bill 
153
154 Returns the invoice (see L<FS::cust_bill>)
155
156 =cut
157
158 sub cust_bill {
159   my $self = shift;
160   qsearchs( 'cust_bill', { 'invnum' => $self->invnum } );
161 }
162
163 =back
164
165 =head1 BUGS
166
167 The delete method.
168
169 =head1 SEE ALSO
170
171 L<FS::Record>, L<FS::cust_refund>, L<FS::cust_bill>, L<FS::cust_credit>,
172 schema.html from the base documentation.
173
174 =cut
175
176 1;
177