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