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