fix 'Can't call method "setup" on an undefined value' error when using into rates...
[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_foreign_keyn('pkgnum', 'cust_pkg', 'pkgnum')
153     || $self->ut_foreign_keyn('billpkgtaxlocationnum',
154                               'cust_bill_pkg_tax_location',
155                               'billpkgtaxlocationnum')
156     || $self->ut_foreign_keyn('billpkgtaxratelocationnum',
157                               'cust_bill_pkg_tax_rate_location',
158                               'billpkgtaxratelocationnum')
159     || $self->ut_money('amount')
160     || $self->ut_enum('setuprecur', [ 'setup', 'recur' ] )
161     || $self->ut_numbern('sdate')
162     || $self->ut_numbern('edate')
163   ;
164   return $error if $error;
165
166   $self->SUPER::check;
167 }
168
169 =item cust_bill_pay
170
171 Returns the FS::cust_bill_pay object (payment application to the overall
172 invoice).
173
174 =cut
175
176 sub cust_bill_pay {
177   my $self = shift;
178   qsearchs('cust_bill_pay', { 'billpaynum' => $self->billpaynum } );
179 }
180
181 =item cust_bill_pkg
182
183 Returns the FS::cust_bill_pkg object (line item to which payment is applied).
184
185 =cut
186
187 sub cust_bill_pkg {
188   my $self = shift;
189   qsearchs('cust_bill_pkg', { 'billpkgnum' => $self->billpkgnum } );
190 }
191
192 =item send_receipt
193
194 Sends a payment receipt for the associated payment, against this specific
195 invoice and packages.  If there is an error, returns the error, otherwise
196 returns false.
197
198 =cut
199
200 sub send_receipt {
201   my $self = shift;
202   my $opt = ref($_[0]) ? shift : { @_ };
203   $self->cust_bill_pay->send_receipt(
204     'cust_pkg' => scalar($self->cust_bill_pkg->cust_pkg),
205     %$opt,
206   );
207 }
208
209
210 =back
211
212 =head1 BUGS
213
214 B<setuprecur> field is a kludge to compensate for cust_bill_pkg having separate
215 setup and recur fields.  It should be removed once that's fixed.
216
217 =head1 SEE ALSO
218
219 L<FS::Record>, schema.html from the base documentation.
220
221 =cut
222
223 1;
224