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 type - can be set to U for usage; more later
52 =item setup - setup fee
54 =item recur - recurring fee
56 =item sdate - starting date of recurring fee
58 =item edate - ending date of recurring fee
60 =item itemdesc - Line item description (overrides normal package description)
62 =item section - Invoice section (overrides normal package section)
64 =duplicate - Indicates this item appears elsewhere on the invoice
65 (and should not be retaxed or reincluded in totals)
67 =post_total - A hint that this item should appear after invoice totals
72 my ( $self, $value ) = @_;
73 if ( defined($value) ) {
74 $self->setfield('section', $value);
76 $self->getfield('section') || $self->part_pkg->categoryname;
80 =item quantity - If not set, defaults to 1
82 =item unitsetup - If not set, defaults to setup
84 =item unitrecur - If not set, defaults to recur
88 sdate and edate are specified as UNIX timestamps; see L<perlfunc/"time">. Also
89 see L<Time::Local> and L<Date::Parse> for conversion functions.
97 Creates a new line item. To add the line item to the database, see
98 L<"insert">. Line items are normally created by calling the bill method of a
99 customer object (see L<FS::cust_main>).
103 sub table { 'cust_bill_pkg'; }
107 Adds this line item to the database. If there is an error, returns the error,
108 otherwise returns false.
115 local $SIG{HUP} = 'IGNORE';
116 local $SIG{INT} = 'IGNORE';
117 local $SIG{QUIT} = 'IGNORE';
118 local $SIG{TERM} = 'IGNORE';
119 local $SIG{TSTP} = 'IGNORE';
120 local $SIG{PIPE} = 'IGNORE';
122 my $oldAutoCommit = $FS::UID::AutoCommit;
123 local $FS::UID::AutoCommit = 0;
126 my $error = $self->SUPER::insert;
128 $dbh->rollback if $oldAutoCommit;
132 unless ( defined dbdef->table('cust_bill_pkg_detail') && $self->get('details') ) {
133 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
137 foreach my $detail ( @{$self->get('details')} ) {
138 my $cust_bill_pkg_detail = new FS::cust_bill_pkg_detail {
139 'billpkgnum' => $self->billpkgnum,
140 'format' => (ref($detail) ? $detail->[0] : '' ),
141 'detail' => (ref($detail) ? $detail->[1] : $detail ),
142 'amount' => (ref($detail) ? $detail->[2] : '' ),
143 'classnum' => (ref($detail) ? $detail->[3] : '' ),
145 $error = $cust_bill_pkg_detail->insert;
147 $dbh->rollback if $oldAutoCommit;
152 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
159 Currently unimplemented. I don't remove line items because there would then be
160 no record the items ever existed (which is bad, no?)
165 return "Can't delete cust_bill_pkg records!";
168 =item replace OLD_RECORD
170 Currently unimplemented. This would be even more of an accounting nightmare
171 than deleteing the items. Just don't do it.
176 return "Can't modify cust_bill_pkg records!";
181 Checks all fields to make sure this is a valid line item. If there is an
182 error, returns the error, otherwise returns false. Called by the insert
191 $self->ut_numbern('billpkgnum')
192 || $self->ut_snumber('pkgnum')
193 || $self->ut_number('invnum')
194 || $self->ut_money('setup')
195 || $self->ut_money('recur')
196 || $self->ut_numbern('sdate')
197 || $self->ut_numbern('edate')
198 || $self->ut_textn('itemdesc')
199 || $self->ut_textn('section')
200 || $self->ut_enum('duplicate', [ '', 'Y' ])
201 || $self->ut_enum('post_total', [ '', 'Y' ])
202 || $self->ut_enum('type', [ '', 'U' ]) #only usage for now
204 return $error if $error;
206 #if ( $self->pkgnum != 0 ) { #allow unchecked pkgnum 0 for tax! (add to part_pkg?)
207 if ( $self->pkgnum > 0 ) { #allow -1 for non-pkg line items and 0 for tax (add to part_pkg?)
208 return "Unknown pkgnum ". $self->pkgnum
209 unless qsearchs( 'cust_pkg', { 'pkgnum' => $self->pkgnum } );
212 return "Unknown invnum"
213 unless qsearchs( 'cust_bill' ,{ 'invnum' => $self->invnum } );
220 Returns the package (see L<FS::cust_pkg>) for this invoice line item.
226 qsearchs( 'cust_pkg', { 'pkgnum' => $self->pkgnum } );
231 Returns the package definition for this invoice line item.
237 if ( $self->pkgpart_override ) {
238 qsearchs('part_pkg', { 'pkgpart' => $self->pkgpart_override } );
240 $self->cust_pkg->part_pkg;
246 Returns the invoice (see L<FS::cust_bill>) for this invoice line item.
252 qsearchs( 'cust_bill', { 'invnum' => $self->invnum } );
255 =item details [ OPTION => VALUE ... ]
257 Returns an array of detail information for the invoice line item.
259 Currently available options are: I<format> I<escape_function>
261 If I<format> is set to html or latex then the array members are improved
262 for tabular appearance in those environments if possible.
264 If I<escape_function> is set then the array members are processed by this
265 function before being returned.
270 my ( $self, %opt ) = @_;
271 my $format = $opt{format} || '';
272 my $escape_function = $opt{escape_function} || sub { shift };
273 return () unless defined dbdef->table('cust_bill_pkg_detail');
275 eval "use Text::CSV_XS;";
277 my $csv = new Text::CSV_XS;
279 my $format_sub = sub { my $detail = shift;
280 $csv->parse($detail) or return "can't parse $detail";
281 join(' - ', map { &$escape_function($_) }
286 $format_sub = sub { my $detail = shift;
287 $csv->parse($detail) or return "can't parse $detail";
288 join('</TD><TD>', map { &$escape_function($_) }
292 if $format eq 'html';
294 $format_sub = sub { my $detail = shift;
295 $csv->parse($detail) or return "can't parse $detail";
296 #join(' & ', map { '\small{'. &$escape_function($_). '}' }
300 foreach ($csv->fields) {
301 $result .= ' & ' if $column > 1;
302 if ($column > 6) { # KLUDGE ALERT!
303 $result .= '\multicolumn{1}{l}{\small{'.
304 &$escape_function($_). '}}';
306 $result .= '\small{'. &$escape_function($_). '}';
312 if $format eq 'latex';
314 $format_sub = $opt{format_function} if $opt{format_function};
316 map { ( $_->format eq 'C'
317 ? &{$format_sub}( $_->detail )
318 : &{$escape_function}( $_->detail )
321 qsearch ({ 'table' => 'cust_bill_pkg_detail',
322 'hashref' => { 'billpkgnum' => $self->billpkgnum },
323 'order_by' => 'ORDER BY detailnum',
325 #qsearch ( 'cust_bill_pkg_detail', { 'lineitemnum' => $self->lineitemnum });
330 Returns a description for this line item. For typical line items, this is the
331 I<pkg> field of the corresponding B<FS::part_pkg> object (see L<FS::part_pkg>).
332 For one-shot line items and named taxes, it is the I<itemdesc> field of this
333 line item, and for generic taxes, simply returns "Tax".
340 if ( $self->pkgnum > 0 ) {
341 $self->itemdesc || $self->part_pkg->pkg;
343 $self->itemdesc || 'Tax';
349 Returns the amount owed (still outstanding) on this line item's setup fee,
350 which is the amount of the line item minus all payment applications (see
351 L<FS::cust_bill_pay_pkg> and credit applications (see
352 L<FS::cust_credit_bill_pkg>).
358 $self->owed('setup', @_);
363 Returns the amount owed (still outstanding) on this line item's recurring fee,
364 which is the amount of the line item minus all payment applications (see
365 L<FS::cust_bill_pay_pkg> and credit applications (see
366 L<FS::cust_credit_bill_pkg>).
372 $self->owed('recur', @_);
375 # modeled after cust_bill::owed...
377 my( $self, $field ) = @_;
378 my $balance = $self->duplicate ? 0 : $self->$field();
379 $balance -= $_->amount foreach ( $self->cust_bill_pay_pkg($field) );
380 $balance -= $_->amount foreach ( $self->cust_credit_bill_pkg($field) );
381 $balance = sprintf( '%.2f', $balance );
382 $balance =~ s/^\-0\.00$/0.00/; #yay ieee fp
386 sub cust_bill_pay_pkg {
387 my( $self, $field ) = @_;
388 qsearch( 'cust_bill_pay_pkg', { 'billpkgnum' => $self->billpkgnum,
389 'setuprecur' => $field,
394 sub cust_credit_bill_pkg {
395 my( $self, $field ) = @_;
396 qsearch( 'cust_credit_bill_pkg', { 'billpkgnum' => $self->billpkgnum,
397 'setuprecur' => $field,
404 Returns the number of billing units (for tax purposes) represented by this,
411 $self->pkgnum ? $self->part_pkg->calc_units($self->cust_pkg) : 0; # 1?
419 my( $self, $value ) = @_;
420 if ( defined($value) ) {
421 $self->setfield('quantity', $value);
423 $self->getfield('quantity') || 1;
431 my( $self, $value ) = @_;
432 if ( defined($value) ) {
433 $self->setfield('unitsetup', $value);
435 $self->getfield('unitsetup') eq ''
436 ? $self->getfield('setup')
437 : $self->getfield('unitsetup');
445 my( $self, $value ) = @_;
446 if ( defined($value) ) {
447 $self->setfield('unitrecur', $value);
449 $self->getfield('unitrecur') eq ''
450 ? $self->getfield('recur')
451 : $self->getfield('unitrecur');
456 Returns true if this line item represents a cdr line item in its own section.
460 # lame, but works for now
463 $self->pkgnum && $self->section ne $self->part_pkg->categoryname;
468 Returns the amount of the charge associated with usage class CLASSNUM if
469 CLASSNUM is defined. Otherwise returns the total charge associated with
475 my( $self, $classnum ) = @_;
479 if ( $self->get('details') ) {
483 grep { ref($_) && ( defined($classnum) ? $_->[3] eq $classnum : 1 ) }
484 @{ $self->get('details') };
488 my $hashref = { 'billpkgnum' => $self->billpkgnum };
489 $hashref->{ 'classnum' } = $classnum if defined($classnum);
490 @values = map { $_->amount } qsearch('cust_bill_pkg_detail', $hashref);
494 foreach ( @values ) {
502 Returns a list of usage classnums associated with this invoice line's
510 if ( $self->get('details') ) {
513 foreach my $detail ( grep { ref($_) } @{$self->get('details')} ) {
514 $seen{ $detail->[3] } = 1;
521 qsearch({ table => 'cust_bill_pkg_detail',
522 hashref => { billpkgnum => $self->billpkgnum },
523 select => 'DISTINCT classnum',
534 setup and recur shouldn't be separate fields. There should be one "amount"
535 field and a flag to tell you if it is a setup/one-time fee or a recurring fee.
537 A line item with both should really be two separate records (preserving
538 sdate and edate for setup fees for recurring packages - that information may
539 be valuable later). Invoice generation (cust_main::bill), invoice printing
540 (cust_bill), tax reports (report_tax.cgi) and line item reports
541 (cust_bill_pkg.cgi) would need to be updated.
543 owed_setup and owed_recur could then be repaced by just owed, and
544 cust_bill::open_cust_bill_pkg and
545 cust_bill_ApplicationCommon::apply_to_lineitems could be simplified.
549 L<FS::Record>, L<FS::cust_bill>, L<FS::cust_pkg>, L<FS::cust_main>, schema.html
550 from the base documentation.