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