payment application detail report, RT#34871
[freeside.git] / FS / FS / cust_bill_pay_pkg.pm
1 package FS::cust_bill_pay_pkg;
2 use base qw( FS::cust_main_Mixin FS::Record );
3
4 use strict;
5 use FS::Conf;
6 use FS::Record qw( qsearch qsearchs );
7 use FS::cust_bill_pay;
8 use FS::cust_bill_pkg;
9
10 =head1 NAME
11
12 FS::cust_bill_pay_pkg - Object methods for cust_bill_pay_pkg records
13
14 =head1 SYNOPSIS
15
16   use FS::cust_bill_pay_pkg;
17
18   $record = new FS::cust_bill_pay_pkg \%hash;
19   $record = new FS::cust_bill_pay_pkg { '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_pkg object represents application of a payment (see
32 L<FS::cust_bill_pay>) to a specific line item within an invoice (see
33 L<FS::cust_bill_pkg>).  FS::cust_bill_pay_pkg inherits from FS::Record.  The
34 following fields are currently supported:
35
36 =over 4
37
38 =item billpaypkgnum - primary key
39
40 =item billpaynum - Payment application to the overall invoice (see L<FS::cust_bill_pay>)
41
42 =item billpkgnum -  Line item to which payment is applied (see L<FS::cust_bill_pkg>)
43
44 =item amount - Amount of the payment applied to this line item.
45
46 =item setuprecur - 'setup' or 'recur', designates whether the payment was applied to the setup or recurring portion of the line item.
47
48 =item sdate - starting date of recurring fee
49
50 =item edate - ending date of recurring fee
51
52 =back
53
54 sdate and edate are specified as UNIX timestamps; see L<perlfunc/"time">.  Also
55 see L<Time::Local> and L<Date::Parse> for conversion functions.
56
57 =head1 METHODS
58
59 =over 4
60
61 =item new HASHREF
62
63 Creates a new record.  To add the record to the database, see L<"insert">.
64
65 Note that this stores the hash reference, not a distinct copy of the hash it
66 points to.  You can ask the object for a copy with the I<hash> method.
67
68 =cut
69
70 # the new method can be inherited from FS::Record, if a table method is defined
71
72 sub table { 'cust_bill_pay_pkg'; }
73
74 =item insert
75
76 Adds this record to the database.  If there is an error, returns the error,
77 otherwise returns false.
78
79 =cut
80
81 sub insert {
82   my($self, %options) = @_;
83
84   #local $SIG{HUP} = 'IGNORE';
85   #local $SIG{INT} = 'IGNORE';
86   #local $SIG{QUIT} = 'IGNORE';
87   #local $SIG{TERM} = 'IGNORE';
88   #local $SIG{TSTP} = 'IGNORE';
89   #local $SIG{PIPE} = 'IGNORE';
90   #
91   #my $oldAutoCommit = $FS::UID::AutoCommit;
92   #local $FS::UID::AutoCommit = 0;
93   #my $dbh = dbh;
94
95   my $error = $self->SUPER::insert;
96   if ( $error ) {
97     #$dbh->rollback if $oldAutoCommit;
98     return "error inserting $self: $error";
99   }
100
101   #payment receipt
102   my $conf = new FS::Conf;
103   my $trigger =
104     $conf->config('payment_receipt-trigger',
105                     $self->cust_bill_pay->cust_bill->cust_main->agentnum,
106                  )
107     || 'cust_pay';
108   if ( $trigger eq 'cust_bill_pay_pkg' ) {
109     my $error = $self->send_receipt(
110       'manual'    => $options{'manual'},
111     );
112     warn "can't send payment receipt/statement: $error" if $error;
113   }
114
115   '';
116
117 }
118
119 =item delete
120
121 Delete this record from the database.
122
123 =cut
124
125 # the delete method can be inherited from FS::Record
126
127 =item replace OLD_RECORD
128
129 Replaces the OLD_RECORD with this one in the database.  If there is an error,
130 returns the error, otherwise returns false.
131
132 =cut
133
134 # the replace method can be inherited from FS::Record
135
136 =item check
137
138 Checks all fields to make sure this is a valid payment application.  If there
139 is an error, returns the error, otherwise returns false.  Called by the insert
140 and replace methods.
141
142 =cut
143
144 # the check method should currently be supplied - FS::Record contains some
145 # data checking routines
146
147 sub check {
148   my $self = shift;
149
150   my $error = 
151     $self->ut_numbern('billpaypkgnum')
152     || $self->ut_foreign_key('billpaynum', 'cust_bill_pay', 'billpaynum' )
153     || $self->ut_foreign_key('billpkgnum', 'cust_bill_pkg', 'billpkgnum' )
154     || $self->ut_foreign_keyn('pkgnum', 'cust_pkg', 'pkgnum')
155     || $self->ut_foreign_keyn('billpkgtaxlocationnum',
156                               'cust_bill_pkg_tax_location',
157                               'billpkgtaxlocationnum')
158     || $self->ut_foreign_keyn('billpkgtaxratelocationnum',
159                               'cust_bill_pkg_tax_rate_location',
160                               'billpkgtaxratelocationnum')
161     || $self->ut_money('amount')
162     || $self->ut_enum('setuprecur', [ 'setup', 'recur' ] )
163     || $self->ut_numbern('sdate')
164     || $self->ut_numbern('edate')
165   ;
166   return $error if $error;
167
168   $self->SUPER::check;
169 }
170
171 =item cust_bill_pay
172
173 Returns the FS::cust_bill_pay object (payment application to the overall
174 invoice).
175
176 =cut
177
178 sub cust_bill_pay {
179   my $self = shift;
180   qsearchs('cust_bill_pay', { 'billpaynum' => $self->billpaynum } );
181 }
182
183 =item cust_bill_pkg
184
185 Returns the FS::cust_bill_pkg object (line item to which payment is applied).
186
187 =cut
188
189 sub cust_bill_pkg {
190   my $self = shift;
191   qsearchs('cust_bill_pkg', { 'billpkgnum' => $self->billpkgnum } );
192 }
193
194 =item send_receipt
195
196 Sends a payment receipt for the associated payment, against this specific
197 invoice and packages.  If there is an error, returns the error, otherwise
198 returns false.
199
200 =cut
201
202 sub send_receipt {
203   my $self = shift;
204   my $opt = ref($_[0]) ? shift : { @_ };
205   $self->cust_bill_pay->send_receipt(
206     'cust_pkg' => scalar($self->cust_bill_pkg->cust_pkg),
207     %$opt,
208   );
209 }
210
211
212 =back
213
214 =head1 BUGS
215
216 B<setuprecur> field is a kludge to compensate for cust_bill_pkg having separate
217 setup and recur fields.  It should be removed once that's fixed.
218
219 =head1 SEE ALSO
220
221 L<FS::Record>, schema.html from the base documentation.
222
223 =cut
224
225 1;
226