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