delete payments
[freeside.git] / FS / FS / cust_bill_pay.pm
1 package FS::cust_bill_pay;
2
3 use strict;
4 use vars qw( @ISA );
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 =head1 NAME
12
13 FS::cust_bill_pay - Object methods for cust_bill_pay records
14
15 =head1 SYNOPSIS 
16
17   use FS::cust_bill_pay;
18
19   $record = new FS::cust_bill_pay \%hash;
20   $record = new FS::cust_bill_pay { 'column' => 'value' };
21
22   $error = $record->insert;
23
24   $error = $new_record->replace($old_record);
25
26   $error = $record->delete;
27
28   $error = $record->check;
29
30 =head1 DESCRIPTION
31
32 An FS::cust_bill_pay object represents the application of a payment to a
33 specific invoice.  FS::cust_bill_pay inherits from FS::Record.  The following
34 fields are currently supported:
35
36 =over 4
37
38 =item billpaynum - primary key (assigned automatically)
39
40 =item invnum - Invoice (see L<FS::cust_bill>)
41
42 =item paynum - Payment (see L<FS::cust_pay>)
43
44 =item amount - Amount of the payment to apply to the specific invoice.
45
46 =item _date - specified as a UNIX timestamp; see L<perlfunc/"time">.  Also see
47 L<Time::Local> and L<Date::Parse> for conversion functions.
48
49 =back
50
51 =head1 METHODS
52
53 =over 4 
54
55 =item new HASHREF
56
57 Creates a new record.  To add the record to the database, see L<"insert">.
58
59 =cut
60
61 sub table { 'cust_bill_pay'; }
62
63 =item insert
64
65 Adds this record to the database.  If there is an error, returns the error,
66 otherwise returns false.
67
68 =cut
69
70 sub insert {
71   my $self = shift;
72
73   local $SIG{HUP} = 'IGNORE';
74   local $SIG{INT} = 'IGNORE';
75   local $SIG{QUIT} = 'IGNORE';
76   local $SIG{TERM} = 'IGNORE';
77   local $SIG{TSTP} = 'IGNORE';
78   local $SIG{PIPE} = 'IGNORE';
79
80   my $oldAutoCommit = $FS::UID::AutoCommit;
81   local $FS::UID::AutoCommit = 0;
82   my $dbh = dbh;
83
84   my $error = $self->check;
85   return $error if $error;
86
87   $error = $self->SUPER::insert;
88
89   my $cust_pay = qsearchs('cust_pay', { 'paynum' => $self->paynum } ) or do {
90     $dbh->rollback if $oldAutoCommit;
91     return "unknown cust_pay.paynum: ". $self->paynum;
92   };
93
94   my $pay_total = 0;
95   $pay_total += $_ foreach map { $_->amount }
96     qsearch('cust_bill_pay', { 'paynum' => $self->paynum } );
97
98   if ( sprintf("%.2f", $pay_total) > sprintf("%.2f", $cust_pay->paid) ) {
99     $dbh->rollback if $oldAutoCommit;
100     return "total cust_bill_pay.amount $pay_total for paynum ". $self->paynum.
101            " greater than cust_pay.paid ". $cust_pay->paid;
102   }
103
104   my $cust_bill = qsearchs('cust_bill', { 'invnum' => $self->invnum } ) or do {
105     $dbh->rollback if $oldAutoCommit;
106     return "unknown cust_bill.invnum: ". $self->invnum;
107   };
108
109   my $bill_total = 0;
110   $bill_total += $_ foreach map { $_->amount }
111     qsearch('cust_bill_pay', { 'invnum' => $self->invnum } );
112   $bill_total += $_ foreach map { $_->amount } 
113     qsearch('cust_credit_bill', { 'invnum' => $self->invnum } );
114   if ( sprintf("%.2f", $bill_total) > sprintf("%.2f", $cust_bill->charged) ) {
115     $dbh->rollback if $oldAutoCommit;
116     return "total cust_bill_pay.amount and cust_credit_bill.amount $bill_total".
117            " for invnum ". $self->invnum.
118            " greater than cust_bill.charged ". $cust_bill->charged;
119   }
120
121   $dbh->commit or die $dbh->errstr if $oldAutoCommit;
122
123   '';
124 }
125
126 =item delete
127
128 Deletes this payment application, unless the closed flag for the parent payment
129 (see L<FS::cust_pay>) is set.
130
131 =cut
132
133 sub delete {
134   my $self = shift;
135   return "Can't delete application for closed payment"
136     if $self->cust_pay->closed =~ /^Y/i;
137   $self->SUPER::delete(@_);
138 }
139
140 =item replace OLD_RECORD
141
142 Currently unimplemented (accounting reasons).
143
144 =cut
145
146 sub replace {
147    return "Can't (yet?) modify cust_bill_pay records!";
148 }
149
150 =item check
151
152 Checks all fields to make sure this is a valid payment.  If there is an error,
153 returns the error, otherwise returns false.  Called by the insert method.
154
155 =cut
156
157 sub check {
158   my $self = shift;
159
160   my $error = 
161     $self->ut_numbern('billpaynum')
162     || $self->ut_number('invnum')
163     || $self->ut_number('paynum')
164     || $self->ut_money('amount')
165     || $self->ut_numbern('_date')
166   ;
167   return $error if $error;
168
169   return "amount must be > 0" if $self->amount <= 0;
170
171   $self->_date(time) unless $self->_date;
172
173   ''; #no error
174 }
175
176 =item cust_pay 
177
178 Returns the payment (see L<FS::cust_pay>)
179
180 =cut
181
182 sub cust_pay {
183   my $self = shift;
184   qsearchs( 'cust_pay', { 'paynum' => $self->paynum } );
185 }
186
187 =item cust_bill 
188
189 Returns the invoice (see L<FS::cust_bill>)
190
191 =cut
192
193 sub cust_bill {
194   my $self = shift;
195   qsearchs( 'cust_bill', { 'invnum' => $self->invnum } );
196 }
197
198 =back
199
200 =head1 VERSION
201
202 $Id: cust_bill_pay.pm,v 1.12 2002-02-07 22:29:34 ivan Exp $
203
204 =head1 BUGS
205
206 Delete and replace methods.
207
208 the checks for over-applied payments could be better done like the ones in
209 cust_bill_credit
210
211 =head1 SEE ALSO
212
213 L<FS::cust_pay>, L<FS::cust_bill>, L<FS::Record>, schema.html from the
214 base documentation.
215
216 =cut
217
218 1;
219