ACL for hardware class config, RT#85057
[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
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 =
101     $conf->config('payment_receipt-trigger',
102                     $self->cust_bill_pay->cust_bill->cust_main->agentnum,
103                  )
104     || 'cust_pay';
105   if ( $trigger eq 'cust_bill_pay_pkg' ) {
106     my $error = $self->send_receipt(
107       'manual'    => $options{'manual'},
108     );
109     warn "can't send payment receipt/statement: $error" if $error;
110   }
111
112   '';
113
114 }
115
116 =item delete
117
118 Delete this record from the database.
119
120 =cut
121
122 # the delete method can be inherited from FS::Record
123
124 =item replace OLD_RECORD
125
126 Replaces the OLD_RECORD with this one in the database.  If there is an error,
127 returns the error, otherwise returns false.
128
129 =cut
130
131 # the replace method can be inherited from FS::Record
132
133 =item check
134
135 Checks all fields to make sure this is a valid payment application.  If there
136 is an error, returns the error, otherwise returns false.  Called by the insert
137 and replace methods.
138
139 =cut
140
141 # the check method should currently be supplied - FS::Record contains some
142 # data checking routines
143
144 sub check {
145   my $self = shift;
146
147   my $error = 
148     $self->ut_numbern('billpaypkgnum')
149     || $self->ut_foreign_key('billpaynum', 'cust_bill_pay', 'billpaynum' )
150     || $self->ut_foreign_key('billpkgnum', 'cust_bill_pkg', 'billpkgnum' )
151     || $self->ut_foreign_keyn('pkgnum', 'cust_pkg', 'pkgnum')
152     || $self->ut_foreign_keyn('billpkgtaxlocationnum',
153                               'cust_bill_pkg_tax_location',
154                               'billpkgtaxlocationnum')
155     || $self->ut_foreign_keyn('billpkgtaxratelocationnum',
156                               'cust_bill_pkg_tax_rate_location',
157                               'billpkgtaxratelocationnum')
158     || $self->ut_money('amount')
159     || $self->ut_enum('setuprecur', [ 'setup', 'recur' ] )
160     || $self->ut_numbern('sdate')
161     || $self->ut_numbern('edate')
162   ;
163   return $error if $error;
164
165   $self->SUPER::check;
166 }
167
168 =item cust_bill_pay
169
170 Returns the FS::cust_bill_pay object (payment application to the overall
171 invoice).
172
173 =item cust_bill_pkg
174
175 Returns the FS::cust_bill_pkg object (line item to which payment is applied).
176
177 =item send_receipt
178
179 Sends a payment receipt for the associated payment, against this specific
180 invoice and packages.  If there is an error, returns the error, otherwise
181 returns false.
182
183 =cut
184
185 sub send_receipt {
186   my $self = shift;
187   my $opt = ref($_[0]) ? shift : { @_ };
188   $self->cust_bill_pay->send_receipt(
189     'cust_pkg' => scalar($self->cust_bill_pkg->cust_pkg),
190     %$opt,
191   );
192 }
193
194
195 =back
196
197 =head1 BUGS
198
199 B<setuprecur> field is a kludge to compensate for cust_bill_pkg having separate
200 setup and recur fields.  It should be removed once that's fixed.
201
202 =head1 SEE ALSO
203
204 L<FS::Record>, schema.html from the base documentation.
205
206 =cut
207
208 1;
209