recommend HTML::Mason
[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
78   local $SIG{HUP} = 'IGNORE';
79   local $SIG{INT} = 'IGNORE';
80   local $SIG{QUIT} = 'IGNORE';
81   local $SIG{TERM} = 'IGNORE';
82   local $SIG{TSTP} = 'IGNORE';
83   local $SIG{PIPE} = 'IGNORE';
84
85   my $oldAutoCommit = $FS::UID::AutoCommit;
86   local $FS::UID::AutoCommit = 0;
87   my $dbh = dbh;
88
89   my $error = $self->check;
90   return $error if $error;
91
92   $error = $self->SUPER::insert;
93
94   my $cust_pay = qsearchs('cust_pay', { 'paynum' => $self->paynum } ) or do {
95     $dbh->rollback if $oldAutoCommit;
96     return "unknown cust_pay.paynum: ". $self->paynum;
97   };
98
99   my $pay_total = 0;
100   $pay_total += $_ foreach map { $_->amount }
101     qsearch('cust_bill_pay', { 'paynum' => $self->paynum } );
102
103   if ( sprintf("%.2f", $pay_total) > sprintf("%.2f", $cust_pay->paid) ) {
104     $dbh->rollback if $oldAutoCommit;
105     return "total cust_bill_pay.amount $pay_total for paynum ". $self->paynum.
106            " greater than cust_pay.paid ". $cust_pay->paid;
107   }
108
109   my $cust_bill = $self->cust_bill;
110   unless ( $cust_bill ) {
111     $dbh->rollback if $oldAutoCommit;
112     return "unknown cust_bill.invnum: ". $self->invnum;
113   };
114
115   my $bill_total = 0;
116   $bill_total += $_ foreach map { $_->amount }
117     qsearch('cust_bill_pay', { 'invnum' => $self->invnum } );
118   $bill_total += $_ foreach map { $_->amount } 
119     qsearch('cust_credit_bill', { 'invnum' => $self->invnum } );
120   if ( sprintf("%.2f", $bill_total) > sprintf("%.2f", $cust_bill->charged) ) {
121     $dbh->rollback if $oldAutoCommit;
122     return "total cust_bill_pay.amount and cust_credit_bill.amount $bill_total".
123            " for invnum ". $self->invnum.
124            " greater than cust_bill.charged ". $cust_bill->charged;
125   }
126
127   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
128
129   if ( $conf->exists('invoice_send_receipts') ) {
130     my $send_error = $cust_bill->send;
131     warn "Error sending receipt: $send_error\n" if $send_error;
132   }
133
134   '';
135 }
136
137 =item delete
138
139 Deletes this payment application, unless the closed flag for the parent payment
140 (see L<FS::cust_pay>) is set.
141
142 =cut
143
144 sub delete {
145   my $self = shift;
146   return "Can't delete application for closed payment"
147     if $self->cust_pay->closed =~ /^Y/i;
148   $self->SUPER::delete(@_);
149 }
150
151 =item replace OLD_RECORD
152
153 Currently unimplemented (accounting reasons).
154
155 =cut
156
157 sub replace {
158    return "Can't (yet?) modify cust_bill_pay records!";
159 }
160
161 =item check
162
163 Checks all fields to make sure this is a valid payment.  If there is an error,
164 returns the error, otherwise returns false.  Called by the insert method.
165
166 =cut
167
168 sub check {
169   my $self = shift;
170
171   my $error = 
172     $self->ut_numbern('billpaynum')
173     || $self->ut_number('invnum')
174     || $self->ut_number('paynum')
175     || $self->ut_money('amount')
176     || $self->ut_numbern('_date')
177   ;
178   return $error if $error;
179
180   return "amount must be > 0" if $self->amount <= 0;
181
182   $self->_date(time) unless $self->_date;
183
184   $self->SUPER::check;
185 }
186
187 =item cust_pay 
188
189 Returns the payment (see L<FS::cust_pay>)
190
191 =cut
192
193 sub cust_pay {
194   my $self = shift;
195   qsearchs( 'cust_pay', { 'paynum' => $self->paynum } );
196 }
197
198 =item cust_bill 
199
200 Returns the invoice (see L<FS::cust_bill>)
201
202 =cut
203
204 sub cust_bill {
205   my $self = shift;
206   qsearchs( 'cust_bill', { 'invnum' => $self->invnum } );
207 }
208
209 =back
210
211 =head1 BUGS
212
213 Delete and replace methods.
214
215 the checks for over-applied payments could be better done like the ones in
216 cust_bill_credit
217
218 =head1 SEE ALSO
219
220 L<FS::cust_pay>, L<FS::cust_bill>, L<FS::Record>, schema.html from the
221 base documentation.
222
223 =cut
224
225 1;
226