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 an array of detail information for the invoice line item.
203 return () unless defined dbdef->table('cust_bill_pkg_detail');
205 qsearch ( 'cust_bill_pkg_detail', { 'pkgnum' => $self->pkgnum,
206 'invnum' => $self->invnum, } );
207 #qsearch ( 'cust_bill_pkg_detail', { 'lineitemnum' => $self->lineitemnum });
212 Returns a description for this line item. For typical line items, this is the
213 I<pkg> field of the corresponding B<FS::part_pkg> object (see L<FS::part_pkg>).
214 For one-shot line items and named taxes, it is the I<itemdesc> field of this
215 line item, and for generic taxes, simply returns "Tax".
222 if ( $self->pkgnum > 0 ) {
223 $self->cust_pkg->part_pkg->pkg;
225 $self->itemdesc || 'Tax';
231 Returns the amount owed (still outstanding) on this line item's setup fee,
232 which is the amount of the line item minus all payment applications (see
233 L<FS::cust_bill_pay_pkg> and credit applications (see
234 L<FS::cust_credit_bill_pkg>).
240 $self->owed('setup', @_);
245 Returns the amount owed (still outstanding) on this line item's recurring fee,
246 which is the amount of the line item minus all payment applications (see
247 L<FS::cust_bill_pay_pkg> and credit applications (see
248 L<FS::cust_credit_bill_pkg>).
254 $self->owed('recur', @_);
257 # modeled after cust_bill::owed...
259 my( $self, $field ) = @_;
260 my $balance = $self->$field();
261 $balance -= $_->amount foreach ( $self->cust_bill_pay_pkg($field) );
262 $balance -= $_->amount foreach ( $self->cust_credit_bill_pkg($field) );
263 $balance = sprintf( '%.2f', $balance );
264 $balance =~ s/^\-0\.00$/0.00/; #yay ieee fp
268 sub cust_bill_pay_pkg {
269 my( $self, $field ) = @_;
270 qsearch( 'cust_bill_pay_pkg', { 'billpkgnum' => $self->billpkgnum,
271 'setuprecur' => $field,
276 sub cust_credit_bill_pkg {
277 my( $self, $field ) = @_;
278 qsearch( 'cust_credit_bill_pkg', { 'billpkgnum' => $self->billpkgnum,
279 'setuprecur' => $field,
288 setup and recur shouldn't be separate fields. There should be one "amount"
289 field and a flag to tell you if it is a setup/one-time fee or a recurring fee.
291 A line item with both should really be two separate records (preserving
292 sdate and edate for setup fees for recurring packages - that information may
293 be valuable later). Invoice generation (cust_main::bill), invoice printing
294 (cust_bill), tax reports (report_tax.cgi) and line item reports
295 (cust_bill_pkg.cgi) would need to be updated.
297 owed_setup and owed_recur could then be repaced by just owed, and
298 cust_bill::open_cust_bill_pkg and
299 cust_bill_ApplicationCommon::apply_to_lineitems could be simplified.
303 L<FS::Record>, L<FS::cust_bill>, L<FS::cust_pkg>, L<FS::cust_main>, schema.html
304 from the base documentation.