1 package FS::cust_bill_pkg;
5 use FS::Record qw( qsearch qsearchs dbdef dbh );
6 use FS::cust_main_Mixin;
9 use FS::cust_bill_pkg_detail;
10 use FS::cust_bill_pay_pkg;
11 use FS::cust_credit_bill_pkg;
13 @ISA = qw( FS::cust_main_Mixin FS::Record );
17 FS::cust_bill_pkg - Object methods for cust_bill_pkg records
21 use FS::cust_bill_pkg;
23 $record = new FS::cust_bill_pkg \%hash;
24 $record = new FS::cust_bill_pkg { 'column' => 'value' };
26 $error = $record->insert;
28 $error = $new_record->replace($old_record);
30 $error = $record->delete;
32 $error = $record->check;
36 An FS::cust_bill_pkg object represents an invoice line item.
37 FS::cust_bill_pkg inherits from FS::Record. The following fields are currently
42 =item billpkgnum - primary key
44 =item invnum - invoice (see L<FS::cust_bill>)
46 =item pkgnum - package (see L<FS::cust_pkg>) or 0 for the special virtual sales tax package, or -1 for the virtual line item (itemdesc is used for the line)
48 =item setup - setup fee
50 =item recur - recurring fee
52 =item sdate - starting date of recurring fee
54 =item edate - ending date of recurring fee
56 =item itemdesc - Line item description (currentlty used only when pkgnum is 0 or -1)
60 sdate and edate are specified as UNIX timestamps; see L<perlfunc/"time">. Also
61 see L<Time::Local> and L<Date::Parse> for conversion functions.
69 Creates a new line item. To add the line item to the database, see
70 L<"insert">. Line items are normally created by calling the bill method of a
71 customer object (see L<FS::cust_main>).
75 sub table { 'cust_bill_pkg'; }
79 Adds this line item to the database. If there is an error, returns the error,
80 otherwise returns false.
87 local $SIG{HUP} = 'IGNORE';
88 local $SIG{INT} = 'IGNORE';
89 local $SIG{QUIT} = 'IGNORE';
90 local $SIG{TERM} = 'IGNORE';
91 local $SIG{TSTP} = 'IGNORE';
92 local $SIG{PIPE} = 'IGNORE';
94 my $oldAutoCommit = $FS::UID::AutoCommit;
95 local $FS::UID::AutoCommit = 0;
98 my $error = $self->SUPER::insert;
100 $dbh->rollback if $oldAutoCommit;
104 unless ( defined dbdef->table('cust_bill_pkg_detail') && $self->get('details') ) {
105 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
109 foreach my $detail ( @{$self->get('details')} ) {
110 my $cust_bill_pkg_detail = new FS::cust_bill_pkg_detail {
111 'pkgnum' => $self->pkgnum,
112 'invnum' => $self->invnum,
115 $error = $cust_bill_pkg_detail->insert;
117 $dbh->rollback if $oldAutoCommit;
122 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
129 Currently unimplemented. I don't remove line items because there would then be
130 no record the items ever existed (which is bad, no?)
135 return "Can't delete cust_bill_pkg records!";
138 =item replace OLD_RECORD
140 Currently unimplemented. This would be even more of an accounting nightmare
141 than deleteing the items. Just don't do it.
146 return "Can't modify cust_bill_pkg records!";
151 Checks all fields to make sure this is a valid line item. If there is an
152 error, returns the error, otherwise returns false. Called by the insert
161 $self->ut_numbern('billpkgnum')
162 || $self->ut_snumber('pkgnum')
163 || $self->ut_number('invnum')
164 || $self->ut_money('setup')
165 || $self->ut_money('recur')
166 || $self->ut_numbern('sdate')
167 || $self->ut_numbern('edate')
168 || $self->ut_textn('itemdesc')
170 return $error if $error;
172 #if ( $self->pkgnum != 0 ) { #allow unchecked pkgnum 0 for tax! (add to part_pkg?)
173 if ( $self->pkgnum > 0 ) { #allow -1 for non-pkg line items and 0 for tax (add to part_pkg?)
174 return "Unknown pkgnum ". $self->pkgnum
175 unless qsearchs( 'cust_pkg', { 'pkgnum' => $self->pkgnum } );
178 return "Unknown invnum"
179 unless qsearchs( 'cust_bill' ,{ 'invnum' => $self->invnum } );
186 Returns the package (see L<FS::cust_pkg>) for this invoice line item.
192 qsearchs( 'cust_pkg', { 'pkgnum' => $self->pkgnum } );
197 Returns the invoice (see L<FS::cust_bill>) for this invoice line item.
203 qsearchs( 'cust_bill', { 'invnum' => $self->invnum } );
208 Returns an array of detail information for the invoice line item.
214 return () unless defined dbdef->table('cust_bill_pkg_detail');
216 qsearch ( 'cust_bill_pkg_detail', { 'pkgnum' => $self->pkgnum,
217 'invnum' => $self->invnum, } );
218 #qsearch ( 'cust_bill_pkg_detail', { 'lineitemnum' => $self->lineitemnum });
223 Returns a description for this line item. For typical line items, this is the
224 I<pkg> field of the corresponding B<FS::part_pkg> object (see L<FS::part_pkg>).
225 For one-shot line items and named taxes, it is the I<itemdesc> field of this
226 line item, and for generic taxes, simply returns "Tax".
233 if ( $self->pkgnum > 0 ) {
234 $self->cust_pkg->part_pkg->pkg;
236 $self->itemdesc || 'Tax';
242 Returns the amount owed (still outstanding) on this line item's setup fee,
243 which is the amount of the line item minus all payment applications (see
244 L<FS::cust_bill_pay_pkg> and credit applications (see
245 L<FS::cust_credit_bill_pkg>).
251 $self->owed('setup', @_);
256 Returns the amount owed (still outstanding) on this line item's recurring fee,
257 which is the amount of the line item minus all payment applications (see
258 L<FS::cust_bill_pay_pkg> and credit applications (see
259 L<FS::cust_credit_bill_pkg>).
265 $self->owed('recur', @_);
268 # modeled after cust_bill::owed...
270 my( $self, $field ) = @_;
271 my $balance = $self->$field();
272 $balance -= $_->amount foreach ( $self->cust_bill_pay_pkg($field) );
273 $balance -= $_->amount foreach ( $self->cust_credit_bill_pkg($field) );
274 $balance = sprintf( '%.2f', $balance );
275 $balance =~ s/^\-0\.00$/0.00/; #yay ieee fp
279 sub cust_bill_pay_pkg {
280 my( $self, $field ) = @_;
281 qsearch( 'cust_bill_pay_pkg', { 'billpkgnum' => $self->billpkgnum,
282 'setuprecur' => $field,
287 sub cust_credit_bill_pkg {
288 my( $self, $field ) = @_;
289 qsearch( 'cust_credit_bill_pkg', { 'billpkgnum' => $self->billpkgnum,
290 'setuprecur' => $field,
299 setup and recur shouldn't be separate fields. There should be one "amount"
300 field and a flag to tell you if it is a setup/one-time fee or a recurring fee.
302 A line item with both should really be two separate records (preserving
303 sdate and edate for setup fees for recurring packages - that information may
304 be valuable later). Invoice generation (cust_main::bill), invoice printing
305 (cust_bill), tax reports (report_tax.cgi) and line item reports
306 (cust_bill_pkg.cgi) would need to be updated.
308 owed_setup and owed_recur could then be repaced by just owed, and
309 cust_bill::open_cust_bill_pkg and
310 cust_bill_ApplicationCommon::apply_to_lineitems could be simplified.
314 L<FS::Record>, L<FS::cust_bill>, L<FS::cust_pkg>, L<FS::cust_main>, schema.html
315 from the base documentation.