0bbc656ea4657785d8eac903dfd1b96896042d49
[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
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 sub insert {
78   my $self = shift;
79   my $error = $self->SUPER::insert(@_);
80   return $error if $error;
81
82   if ( $conf->exists('invoice_send_receipts') ) {
83     my $send_error = $self->cust_bill->send;
84     warn "Error sending receipt: $send_error\n" if $send_error;
85   }
86
87   '';
88 }
89
90 =item delete
91
92 Currently unimplemented.
93
94 =cut
95
96 sub delete {
97   return "Can't unapply credit!"
98 }
99
100 =item replace OLD_RECORD
101
102 Application of credits may not be modified.
103
104 =cut
105
106 sub replace {
107   return "Can't modify application of credit!"
108 }
109
110 =item check
111
112 Checks all fields to make sure this is a valid credit application.  If there
113 is an error, returns the error, otherwise returns false.  Called by the insert
114 and replace methods.
115
116 =cut
117
118 sub check {
119   my $self = shift;
120
121   my $error =
122     $self->ut_numbern('creditbillnum')
123     || $self->ut_number('crednum')
124     || $self->ut_number('invnum')
125     || $self->ut_numbern('_date')
126     || $self->ut_money('amount')
127   ;
128   return $error if $error;
129
130   return "amount must be > 0" if $self->amount <= 0;
131
132   return "Unknown credit"
133     unless my $cust_credit = 
134       qsearchs( 'cust_credit', { 'crednum' => $self->crednum } );
135
136   return "Unknown invoice"
137     unless my $cust_bill =
138       qsearchs( 'cust_bill', { 'invnum' => $self->invnum } );
139
140   $self->_date(time) unless $self->_date;
141
142   return "Cannot apply more than remaining value of credit"
143     unless $self->amount <= $cust_credit->credited;
144
145   return "Cannot apply more than remaining value of invoice"
146     unless $self->amount <= $cust_bill->owed;
147
148   $self->SUPER::check;
149 }
150
151 =item sub cust_credit
152
153 Returns the credit (see L<FS::cust_credit>)
154
155 =cut
156
157 sub cust_credit {
158   my $self = shift;
159   qsearchs( 'cust_credit', { 'crednum' => $self->crednum } );
160 }
161
162 =item cust_bill 
163
164 Returns the invoice (see L<FS::cust_bill>)
165
166 =cut
167
168 sub cust_bill {
169   my $self = shift;
170   qsearchs( 'cust_bill', { 'invnum' => $self->invnum } );
171 }
172
173 =back
174
175 =head1 BUGS
176
177 The delete method.
178
179 =head1 SEE ALSO
180
181 L<FS::Record>, L<FS::cust_refund>, L<FS::cust_bill>, L<FS::cust_credit>,
182 schema.html from the base documentation.
183
184 =cut
185
186 1;
187