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