This commit was generated by cvs2svn to compensate for changes in r6252,
[freeside.git] / FS / FS / cust_bill_pay.pm
1 package FS::cust_bill_pay;
2
3 use strict;
4 use vars qw( @ISA $conf );
5 use FS::Record qw( qsearchs );
6 use FS::cust_bill_ApplicationCommon;
7 use FS::cust_bill;
8 use FS::cust_pay;
9
10 @ISA = qw( FS::cust_bill_ApplicationCommon );
11
12 #ask FS::UID to run this stuff for us later
13 FS::UID->install_callback( sub { 
14   $conf = new FS::Conf;
15 } );
16
17 =head1 NAME
18
19 FS::cust_bill_pay - Object methods for cust_bill_pay records
20
21 =head1 SYNOPSIS 
22
23   use FS::cust_bill_pay;
24
25   $record = new FS::cust_bill_pay \%hash;
26   $record = new FS::cust_bill_pay { 'column' => 'value' };
27
28   $error = $record->insert;
29
30   $error = $new_record->replace($old_record);
31
32   $error = $record->delete;
33
34   $error = $record->check;
35
36 =head1 DESCRIPTION
37
38 An FS::cust_bill_pay object represents the application of a payment to a
39 specific invoice.  FS::cust_bill_pay inherits from
40 FS::cust_bill_ApplicationCommon and FS::Record.  The following fields are
41 currently supported:
42
43 =over 4
44
45 =item billpaynum - primary key (assigned automatically)
46
47 =item invnum - Invoice (see L<FS::cust_bill>)
48
49 =item paynum - Payment (see L<FS::cust_pay>)
50
51 =item amount - Amount of the payment to apply to the specific invoice.
52
53 =item _date - specified as a UNIX timestamp; see L<perlfunc/"time">.  Also see
54 L<Time::Local> and L<Date::Parse> for conversion functions.
55
56 =back
57
58 =head1 METHODS
59
60 =over 4 
61
62 =item new HASHREF
63
64 Creates a new record.  To add the record to the database, see L<"insert">.
65
66 =cut
67
68 sub table { 'cust_bill_pay'; }
69
70 sub _app_source_name   { 'payment'; }
71 sub _app_source_table { 'cust_pay'; }
72 sub _app_lineitem_breakdown_table { 'cust_bill_pay_pkg'; }
73 sub _app_part_pkg_weight_column { 'pay_weight'; }
74
75 =item insert
76
77 Adds this record to the database.  If there is an error, returns the error,
78 otherwise returns false.
79
80 =item delete
81
82 Deletes this payment application, unless the closed flag for the parent payment
83 (see L<FS::cust_pay>) is set.
84
85 =cut
86
87 sub delete {
88   my $self = shift;
89   return "Can't delete application for closed payment"
90     if $self->cust_pay->closed =~ /^Y/i;
91   return "Can't delete application for closed invoice"
92     if $self->cust_bill->closed =~ /^Y/i;
93   $self->SUPER::delete(@_);
94 }
95
96 =item replace OLD_RECORD
97
98 Currently unimplemented (accounting reasons).
99
100 =cut
101
102 sub replace {
103    return "Can't modify application of payment!";
104 }
105
106 =item check
107
108 Checks all fields to make sure this is a valid payment application.  If there
109 is an error, returns the error, otherwise returns false.  Called by the insert
110 method.
111
112 =cut
113
114 sub check {
115   my $self = shift;
116
117   my $error = 
118     $self->ut_numbern('billpaynum')
119     || $self->ut_foreign_key('paynum', 'cust_pay', 'paynum' )
120     || $self->ut_foreign_key('invnum', 'cust_bill', 'invnum' )
121     || $self->ut_numbern('_date')
122     || $self->ut_money('amount')
123   ;
124   return $error if $error;
125
126   return "amount must be > 0" if $self->amount <= 0;
127   
128   $self->_date(time) unless $self->_date;
129
130   return "Cannot apply more than remaining value of invoice"
131     unless $self->amount <= $self->cust_bill->owed;
132
133   return "Cannot apply more than remaining value of payment"
134     unless $self->amount <= $self->cust_pay->unapplied;
135
136   $self->SUPER::check;
137 }
138
139 =item cust_pay 
140
141 Returns the payment (see L<FS::cust_pay>)
142
143 =cut
144
145 sub cust_pay {
146   my $self = shift;
147   qsearchs( 'cust_pay', { 'paynum' => $self->paynum } );
148 }
149
150 =back
151
152 =head1 BUGS
153
154 Delete and replace methods.
155
156 =head1 SEE ALSO
157
158 L<FS::cust_pay>, L<FS::cust_bill>, L<FS::Record>, schema.html from the
159 base documentation.
160
161 =cut
162
163 1;
164