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