make net receipts clickable... and netreceipts != cashflow, really, so separate those...
[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( qsearchs );
6 use FS::cust_main_Mixin;
7 use FS::cust_bill_ApplicationCommon;
8 use FS::cust_bill;
9 use FS::cust_pay;
10
11 @ISA = qw( FS::cust_main_Mixin FS::cust_bill_ApplicationCommon );
12
13 #ask FS::UID to run this stuff for us later
14 FS::UID->install_callback( sub { 
15   $conf = new FS::Conf;
16 } );
17
18 =head1 NAME
19
20 FS::cust_bill_pay - Object methods for cust_bill_pay records
21
22 =head1 SYNOPSIS 
23
24   use FS::cust_bill_pay;
25
26   $record = new FS::cust_bill_pay \%hash;
27   $record = new FS::cust_bill_pay { 'column' => 'value' };
28
29   $error = $record->insert;
30
31   $error = $new_record->replace($old_record);
32
33   $error = $record->delete;
34
35   $error = $record->check;
36
37 =head1 DESCRIPTION
38
39 An FS::cust_bill_pay object represents the application of a payment to a
40 specific invoice.  FS::cust_bill_pay inherits from
41 FS::cust_bill_ApplicationCommon and FS::Record.  The following fields are
42 currently supported:
43
44 =over 4
45
46 =item billpaynum - primary key (assigned automatically)
47
48 =item invnum - Invoice (see L<FS::cust_bill>)
49
50 =item paynum - Payment (see L<FS::cust_pay>)
51
52 =item amount - Amount of the payment to apply to the specific invoice.
53
54 =item _date - specified as a UNIX timestamp; see L<perlfunc/"time">.  Also see
55 L<Time::Local> and L<Date::Parse> for conversion functions.
56
57 =back
58
59 =head1 METHODS
60
61 =over 4 
62
63 =item new HASHREF
64
65 Creates a new record.  To add the record to the database, see L<"insert">.
66
67 =cut
68
69 sub table { 'cust_bill_pay'; }
70
71 sub _app_source_name   { 'payment'; }
72 sub _app_source_table { 'cust_pay'; }
73 sub _app_lineitem_breakdown_table { 'cust_bill_pay_pkg'; }
74 sub _app_part_pkg_weight_column { 'pay_weight'; }
75
76 =item insert
77
78 Adds this record to the database.  If there is an error, returns the error,
79 otherwise returns false.
80
81 =item delete
82
83 Deletes this payment application, unless the closed flag for the parent payment
84 (see L<FS::cust_pay>) is set.
85
86 =cut
87
88 sub delete {
89   my $self = shift;
90   return "Can't delete application for closed payment"
91     if $self->cust_pay->closed =~ /^Y/i;
92   return "Can't delete application for closed invoice"
93     if $self->cust_bill->closed =~ /^Y/i;
94   $self->SUPER::delete(@_);
95 }
96
97 =item replace OLD_RECORD
98
99 Currently unimplemented (accounting reasons).
100
101 =cut
102
103 sub replace {
104    return "Can't modify application of payment!";
105 }
106
107 =item check
108
109 Checks all fields to make sure this is a valid payment application.  If there
110 is an error, returns the error, otherwise returns false.  Called by the insert
111 method.
112
113 =cut
114
115 sub check {
116   my $self = shift;
117
118   my $error = 
119     $self->ut_numbern('billpaynum')
120     || $self->ut_foreign_key('paynum', 'cust_pay', 'paynum' )
121     || $self->ut_foreign_key('invnum', 'cust_bill', 'invnum' )
122     || $self->ut_numbern('_date')
123     || $self->ut_money('amount')
124   ;
125   return $error if $error;
126
127   return "amount must be > 0" if $self->amount <= 0;
128   
129   $self->_date(time) unless $self->_date;
130
131   return "Cannot apply more than remaining value of invoice"
132     unless $self->amount <= $self->cust_bill->owed;
133
134   return "Cannot apply more than remaining value of payment"
135     unless $self->amount <= $self->cust_pay->unapplied;
136
137   $self->SUPER::check;
138 }
139
140 =item cust_pay 
141
142 Returns the payment (see L<FS::cust_pay>)
143
144 =cut
145
146 sub cust_pay {
147   my $self = shift;
148   qsearchs( 'cust_pay', { 'paynum' => $self->paynum } );
149 }
150
151 =back
152
153 =head1 BUGS
154
155 Delete and replace methods.
156
157 =head1 SEE ALSO
158
159 L<FS::cust_pay>, L<FS::cust_bill>, L<FS::Record>, schema.html from the
160 base documentation.
161
162 =cut
163
164 1;
165