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