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