cust_refund and cust_pay get custnums
[freeside.git] / FS / FS / cust_bill_pay.pm
1 package 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 ( $pay_total > $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 ( $bill_total > $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 Currently unimplemented (accounting reasons).
129
130 =cut
131
132 sub delete {
133   return "Can't (yet?) delete cust_bill_pay records!";
134 }
135
136 =item replace OLD_RECORD
137
138 Currently unimplemented (accounting reasons).
139
140 =cut
141
142 sub replace {
143    return "Can't (yet?) modify cust_bill_pay records!";
144 }
145
146 =item check
147
148 Checks all fields to make sure this is a valid payment.  If there is an error,
149 returns the error, otherwise returns false.  Called by the insert method.
150
151 =cut
152
153 sub check {
154   my $self = shift;
155
156   my $error = 
157     $self->ut_numbern('billpaynum')
158     || $self->ut_number('invnum')
159     || $self->ut_numner('paynum')
160     || $self->ut_money('amount')
161     || $self->ut_numbern('_date')
162   ;
163   return $error if $error;
164
165   $self->_date(time) unless $self->_date;
166
167
168   ''; #no error
169 }
170
171 =item cust_pay 
172
173 Returns the payment (see L<FS::cust_pay>)
174
175 =cut
176
177 sub cust_pay {
178   my $self = shift;
179   qsearchs( 'cust_pay', { 'paynum' => $self->paynum } );
180 }
181
182 =item cust_bill 
183
184 Returns the invoice (see L<FS::cust_bill>)
185
186 =cut
187
188 sub cust_bill {
189   my $self = shift;
190   qsearchs( 'cust_bill', { 'invnum' => $self->invnum } );
191 }
192
193 =back
194
195 =head1 VERSION
196
197 $Id: cust_bill_pay.pm,v 1.4 2001-09-02 02:46:55 ivan Exp $
198
199 =head1 BUGS
200
201 Delete and replace methods.
202
203 the checks for over-applied payments could be better done like the ones in
204 cust_bill_credit
205
206 =head1 SEE ALSO
207
208 L<FS::cust_pay>, L<FS::cust_bill>, L<FS::Record>, schema.html from the
209 base documentation.
210
211 =cut
212
213 1;
214