f0cb1329644581a408f90199bd00c7293d7d974f
[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 =cut
74
75 sub insert {
76   my $self = shift;
77   my $error = $self->SUPER::insert;
78   return $error if $error;
79
80  if ( $conf->exists('invoice_send_receipts') ) {
81     my $send_error = $self->cust_bill->send;
82     warn "Error sending receipt: $send_error\n" if $send_error;
83   }
84
85   '';
86 }
87
88 =item delete
89
90 Deletes this payment application, unless the closed flag for the parent payment
91 (see L<FS::cust_pay>) is set.
92
93 =cut
94
95 sub delete {
96   my $self = shift;
97   return "Can't delete application for closed payment"
98     if $self->cust_pay->closed =~ /^Y/i;
99   $self->SUPER::delete(@_);
100 }
101
102 =item replace OLD_RECORD
103
104 Currently unimplemented (accounting reasons).
105
106 =cut
107
108 sub replace {
109    return "Can't (yet?) modify cust_bill_pay records!";
110 }
111
112 =item check
113
114 Checks all fields to make sure this is a valid payment.  If there is an error,
115 returns the error, otherwise returns false.  Called by the insert method.
116
117 =cut
118
119 sub check {
120   my $self = shift;
121
122   my $error = 
123     $self->ut_numbern('billpaynum')
124     || $self->ut_number('invnum')
125     || $self->ut_number('paynum')
126     || $self->ut_money('amount')
127     || $self->ut_numbern('_date')
128   ;
129   return $error if $error;
130
131   return "amount must be > 0" if $self->amount <= 0;
132   
133   return "Unknown invoice"
134     unless my $cust_bill =
135       qsearchs( 'cust_bill', { 'invnum' => $self->invnum } );
136
137   return "Unknown payment"
138     unless my $cust_pay = 
139       qsearchs( 'cust_pay', { 'paynum' => $self->paynum } );
140
141   $self->_date(time) unless $self->_date;
142
143   return "Cannot apply more than remaining value of invoice"
144     unless $self->amount <= $cust_bill->owed;
145
146   return "Cannot apply more than remaining value of payment"
147     unless $self->amount <= $cust_pay->unapplied;
148
149   $self->SUPER::check;
150 }
151
152 =item cust_pay 
153
154 Returns the payment (see L<FS::cust_pay>)
155
156 =cut
157
158 sub cust_pay {
159   my $self = shift;
160   qsearchs( 'cust_pay', { 'paynum' => $self->paynum } );
161 }
162
163 =item cust_bill 
164
165 Returns the invoice (see L<FS::cust_bill>)
166
167 =cut
168
169 sub cust_bill {
170   my $self = shift;
171   qsearchs( 'cust_bill', { 'invnum' => $self->invnum } );
172 }
173
174 =back
175
176 =head1 BUGS
177
178 Delete and replace methods.
179
180 the checks for over-applied payments could be better done like the ones in
181 cust_bill_credit
182
183 =head1 SEE ALSO
184
185 L<FS::cust_pay>, L<FS::cust_bill>, L<FS::Record>, schema.html from the
186 base documentation.
187
188 =cut
189
190 1;
191