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