1 package FS::cust_bill_pkg;
5 use FS::Record qw( qsearch qsearchs dbdef dbh );
6 use FS::cust_main_Mixin;
10 use FS::cust_bill_pkg_detail;
11 use FS::cust_bill_pay_pkg;
12 use FS::cust_credit_bill_pkg;
14 @ISA = qw( FS::cust_main_Mixin FS::Record );
18 FS::cust_bill_pkg - Object methods for cust_bill_pkg records
22 use FS::cust_bill_pkg;
24 $record = new FS::cust_bill_pkg \%hash;
25 $record = new FS::cust_bill_pkg { 'column' => 'value' };
27 $error = $record->insert;
29 $error = $new_record->replace($old_record);
31 $error = $record->delete;
33 $error = $record->check;
37 An FS::cust_bill_pkg object represents an invoice line item.
38 FS::cust_bill_pkg inherits from FS::Record. The following fields are currently
43 =item billpkgnum - primary key
45 =item invnum - invoice (see L<FS::cust_bill>)
47 =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)
49 =item pkgpart_override - optional package definition (see L<FS::part_pkg>) override
50 =item setup - setup fee
52 =item recur - recurring fee
54 =item sdate - starting date of recurring fee
56 =item edate - ending date of recurring fee
58 =item itemdesc - Line item description (currentlty used only when pkgnum is 0 or -1)
62 sdate and edate are specified as UNIX timestamps; see L<perlfunc/"time">. Also
63 see L<Time::Local> and L<Date::Parse> for conversion functions.
71 Creates a new line item. To add the line item to the database, see
72 L<"insert">. Line items are normally created by calling the bill method of a
73 customer object (see L<FS::cust_main>).
77 sub table { 'cust_bill_pkg'; }
81 Adds this line item to the database. If there is an error, returns the error,
82 otherwise returns false.
89 local $SIG{HUP} = 'IGNORE';
90 local $SIG{INT} = 'IGNORE';
91 local $SIG{QUIT} = 'IGNORE';
92 local $SIG{TERM} = 'IGNORE';
93 local $SIG{TSTP} = 'IGNORE';
94 local $SIG{PIPE} = 'IGNORE';
96 my $oldAutoCommit = $FS::UID::AutoCommit;
97 local $FS::UID::AutoCommit = 0;
100 my $error = $self->SUPER::insert;
102 $dbh->rollback if $oldAutoCommit;
106 unless ( defined dbdef->table('cust_bill_pkg_detail') && $self->get('details') ) {
107 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
111 foreach my $detail ( @{$self->get('details')} ) {
112 my $cust_bill_pkg_detail = new FS::cust_bill_pkg_detail {
113 'pkgnum' => $self->pkgnum,
114 'invnum' => $self->invnum,
117 $error = $cust_bill_pkg_detail->insert;
119 $dbh->rollback if $oldAutoCommit;
124 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
131 Currently unimplemented. I don't remove line items because there would then be
132 no record the items ever existed (which is bad, no?)
137 return "Can't delete cust_bill_pkg records!";
140 =item replace OLD_RECORD
142 Currently unimplemented. This would be even more of an accounting nightmare
143 than deleteing the items. Just don't do it.
148 return "Can't modify cust_bill_pkg records!";
153 Checks all fields to make sure this is a valid line item. If there is an
154 error, returns the error, otherwise returns false. Called by the insert
163 $self->ut_numbern('billpkgnum')
164 || $self->ut_snumber('pkgnum')
165 || $self->ut_number('invnum')
166 || $self->ut_money('setup')
167 || $self->ut_money('recur')
168 || $self->ut_numbern('sdate')
169 || $self->ut_numbern('edate')
170 || $self->ut_textn('itemdesc')
172 return $error if $error;
174 #if ( $self->pkgnum != 0 ) { #allow unchecked pkgnum 0 for tax! (add to part_pkg?)
175 if ( $self->pkgnum > 0 ) { #allow -1 for non-pkg line items and 0 for tax (add to part_pkg?)
176 return "Unknown pkgnum ". $self->pkgnum
177 unless qsearchs( 'cust_pkg', { 'pkgnum' => $self->pkgnum } );
180 return "Unknown invnum"
181 unless qsearchs( 'cust_bill' ,{ 'invnum' => $self->invnum } );
188 Returns the package (see L<FS::cust_pkg>) for this invoice line item.
194 qsearchs( 'cust_pkg', { 'pkgnum' => $self->pkgnum } );
199 Returns the package definition for this invoice line item.
205 if ( $self->pkgpart_override ) {
206 qsearchs('part_pkg', { 'pkgpart' => $self->pkgpart_override } );
208 $self->cust_pkg->part_pkg;
214 Returns the invoice (see L<FS::cust_bill>) for this invoice line item.
220 qsearchs( 'cust_bill', { 'invnum' => $self->invnum } );
225 Returns an array of detail information for the invoice line item.
231 return () unless defined dbdef->table('cust_bill_pkg_detail');
233 qsearch ( 'cust_bill_pkg_detail', { 'pkgnum' => $self->pkgnum,
234 'invnum' => $self->invnum, } );
235 #qsearch ( 'cust_bill_pkg_detail', { 'lineitemnum' => $self->lineitemnum });
240 Returns a description for this line item. For typical line items, this is the
241 I<pkg> field of the corresponding B<FS::part_pkg> object (see L<FS::part_pkg>).
242 For one-shot line items and named taxes, it is the I<itemdesc> field of this
243 line item, and for generic taxes, simply returns "Tax".
250 if ( $self->pkgnum > 0 ) {
251 $self->part_pkg->pkg;
253 $self->itemdesc || 'Tax';
259 Returns the amount owed (still outstanding) on this line item's setup fee,
260 which is the amount of the line item minus all payment applications (see
261 L<FS::cust_bill_pay_pkg> and credit applications (see
262 L<FS::cust_credit_bill_pkg>).
268 $self->owed('setup', @_);
273 Returns the amount owed (still outstanding) on this line item's recurring fee,
274 which is the amount of the line item minus all payment applications (see
275 L<FS::cust_bill_pay_pkg> and credit applications (see
276 L<FS::cust_credit_bill_pkg>).
282 $self->owed('recur', @_);
285 # modeled after cust_bill::owed...
287 my( $self, $field ) = @_;
288 my $balance = $self->$field();
289 $balance -= $_->amount foreach ( $self->cust_bill_pay_pkg($field) );
290 $balance -= $_->amount foreach ( $self->cust_credit_bill_pkg($field) );
291 $balance = sprintf( '%.2f', $balance );
292 $balance =~ s/^\-0\.00$/0.00/; #yay ieee fp
296 sub cust_bill_pay_pkg {
297 my( $self, $field ) = @_;
298 qsearch( 'cust_bill_pay_pkg', { 'billpkgnum' => $self->billpkgnum,
299 'setuprecur' => $field,
304 sub cust_credit_bill_pkg {
305 my( $self, $field ) = @_;
306 qsearch( 'cust_credit_bill_pkg', { 'billpkgnum' => $self->billpkgnum,
307 'setuprecur' => $field,
316 setup and recur shouldn't be separate fields. There should be one "amount"
317 field and a flag to tell you if it is a setup/one-time fee or a recurring fee.
319 A line item with both should really be two separate records (preserving
320 sdate and edate for setup fees for recurring packages - that information may
321 be valuable later). Invoice generation (cust_main::bill), invoice printing
322 (cust_bill), tax reports (report_tax.cgi) and line item reports
323 (cust_bill_pkg.cgi) would need to be updated.
325 owed_setup and owed_recur could then be repaced by just owed, and
326 cust_bill::open_cust_bill_pkg and
327 cust_bill_ApplicationCommon::apply_to_lineitems could be simplified.
331 L<FS::Record>, L<FS::cust_bill>, L<FS::cust_pkg>, L<FS::cust_main>, schema.html
332 from the base documentation.