Have lineitem-specific applications happen in all cases; add weightsto control
[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_bill_ApplicationCommon;
8 use FS::cust_bill;
9 use FS::cust_credit;
10
11 @ISA = qw( FS::cust_bill_ApplicationCommon );
12
13 #ask FS::UID to run this stuff for us later
14 FS::UID->install_callback( sub { 
15   $conf = new FS::Conf;
16 } );
17
18 =head1 NAME
19
20 FS::cust_credit_bill - Object methods for cust_credit_bill records
21
22 =head1 SYNOPSIS
23
24   use FS::cust_credit_bill;
25
26   $record = new FS::cust_credit_bill \%hash;
27   $record = new FS::cust_credit_bill { 'column' => 'value' };
28
29   $error = $record->insert;
30
31   $error = $new_record->replace($old_record);
32
33   $error = $record->delete;
34
35   $error = $record->check;
36
37 =head1 DESCRIPTION
38
39 An FS::cust_credit_bill object represents application of a credit (see
40 L<FS::cust_credit>) to an invoice (see L<FS::cust_bill>).  FS::cust_credit_bill
41 inherits from FS::cust_bill_ApplicationCommon and FS::Record.  The following
42 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 sub _app_source_name  { 'credit'; }
73 sub _app_source_table { 'cust_credit'; }
74 sub _app_lineitem_breakdown_table { 'cust_credit_bill_pkg'; }
75 sub _app_part_pkg_weight_column { 'credit_weight'; }
76
77 =item insert
78
79 Adds this cust_credit_bill to the database ("Posts" all or part of a credit).
80 If there is an error, returns the error, otherwise returns false.
81
82 =item delete
83
84 Currently unimplemented.
85
86 =cut
87
88 sub delete {
89   my $self = shift;
90   return "Can't delete application for closed credit"
91     if $self->cust_credit->closed =~ /^Y/i;
92   return "Can't delete application for closed invoice"
93     if $self->cust_bill->closed =~ /^Y/i;
94   $self->SUPER::delete(@_);
95 }
96
97 =item replace OLD_RECORD
98
99 Application of credits may not be modified.
100
101 =cut
102
103 sub replace {
104   return "Can't modify application of credit!"
105 }
106
107 =item check
108
109 Checks all fields to make sure this is a valid credit application.  If there
110 is an error, returns the error, otherwise returns false.  Called by the insert
111 and replace methods.
112
113 =cut
114
115 sub check {
116   my $self = shift;
117
118   my $error =
119     $self->ut_numbern('creditbillnum')
120     || $self->ut_foreign_key('crednum', 'cust_credit', 'crednum')
121     || $self->ut_foreign_key('invnum', 'cust_bill', 'invnum' )
122     || $self->ut_numbern('_date')
123     || $self->ut_money('amount')
124   ;
125   return $error if $error;
126
127   return "amount must be > 0" if $self->amount <= 0;
128
129   $self->_date(time) unless $self->_date;
130
131   return "Cannot apply more than remaining value of credit"
132     unless $self->amount <= $self->cust_credit->credited;
133
134   return "Cannot apply more than remaining value of invoice"
135     unless $self->amount <= $self->cust_bill->owed;
136
137   $self->SUPER::check;
138 }
139
140 =item sub cust_credit
141
142 Returns the credit (see L<FS::cust_credit>)
143
144 =cut
145
146 sub cust_credit {
147   my $self = shift;
148   qsearchs( 'cust_credit', { 'crednum' => $self->crednum } );
149 }
150
151 =back
152
153 =head1 BUGS
154
155 The delete method.
156
157 This probably should have been called cust_bill_credit.
158
159 =head1 SEE ALSO
160
161 L<FS::Record>, L<FS::cust_bill>, L<FS::cust_credit>,
162 schema.html from the base documentation.
163
164 =cut
165
166 1;
167