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 is a candidate for summarizing and duplicating at print time
66 =post_total - A hint that this item should appear after invoice totals
71 my ( $self, $value ) = @_;
72 if ( defined($value) ) {
73 $self->setfield('section', $value);
75 $self->getfield('section') || $self->part_pkg->categoryname;
79 sub duplicate_section {
81 $self->duplicate ? $self->part_pkg->categoryname : '';
84 =item quantity - If not set, defaults to 1
86 =item unitsetup - If not set, defaults to setup
88 =item unitrecur - If not set, defaults to recur
92 sdate and edate are specified as UNIX timestamps; see L<perlfunc/"time">. Also
93 see L<Time::Local> and L<Date::Parse> for conversion functions.
101 Creates a new line item. To add the line item to the database, see
102 L<"insert">. Line items are normally created by calling the bill method of a
103 customer object (see L<FS::cust_main>).
107 sub table { 'cust_bill_pkg'; }
111 Adds this line item to the database. If there is an error, returns the error,
112 otherwise returns false.
119 local $SIG{HUP} = 'IGNORE';
120 local $SIG{INT} = 'IGNORE';
121 local $SIG{QUIT} = 'IGNORE';
122 local $SIG{TERM} = 'IGNORE';
123 local $SIG{TSTP} = 'IGNORE';
124 local $SIG{PIPE} = 'IGNORE';
126 my $oldAutoCommit = $FS::UID::AutoCommit;
127 local $FS::UID::AutoCommit = 0;
130 my $error = $self->SUPER::insert;
132 $dbh->rollback if $oldAutoCommit;
136 unless ( defined dbdef->table('cust_bill_pkg_detail') && $self->get('details') ) {
137 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
141 foreach my $detail ( @{$self->get('details')} ) {
142 my $cust_bill_pkg_detail = new FS::cust_bill_pkg_detail {
143 'billpkgnum' => $self->billpkgnum,
144 'format' => (ref($detail) ? $detail->[0] : '' ),
145 'detail' => (ref($detail) ? $detail->[1] : $detail ),
146 'amount' => (ref($detail) ? $detail->[2] : '' ),
147 'classnum' => (ref($detail) ? $detail->[3] : '' ),
149 $error = $cust_bill_pkg_detail->insert;
151 $dbh->rollback if $oldAutoCommit;
156 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
163 Currently unimplemented. I don't remove line items because there would then be
164 no record the items ever existed (which is bad, no?)
169 return "Can't delete cust_bill_pkg records!";
172 =item replace OLD_RECORD
174 Currently unimplemented. This would be even more of an accounting nightmare
175 than deleteing the items. Just don't do it.
180 return "Can't modify cust_bill_pkg records!";
185 Checks all fields to make sure this is a valid line item. If there is an
186 error, returns the error, otherwise returns false. Called by the insert
195 $self->ut_numbern('billpkgnum')
196 || $self->ut_snumber('pkgnum')
197 || $self->ut_number('invnum')
198 || $self->ut_money('setup')
199 || $self->ut_money('recur')
200 || $self->ut_numbern('sdate')
201 || $self->ut_numbern('edate')
202 || $self->ut_textn('itemdesc')
203 || $self->ut_textn('section')
204 || $self->ut_enum('duplicate', [ '', 'Y' ])
205 || $self->ut_enum('post_total', [ '', 'Y' ])
206 || $self->ut_enum('type', [ '', 'U' ]) #only usage for now
208 return $error if $error;
210 #if ( $self->pkgnum != 0 ) { #allow unchecked pkgnum 0 for tax! (add to part_pkg?)
211 if ( $self->pkgnum > 0 ) { #allow -1 for non-pkg line items and 0 for tax (add to part_pkg?)
212 return "Unknown pkgnum ". $self->pkgnum
213 unless qsearchs( 'cust_pkg', { 'pkgnum' => $self->pkgnum } );
216 return "Unknown invnum"
217 unless qsearchs( 'cust_bill' ,{ 'invnum' => $self->invnum } );
224 Returns the package (see L<FS::cust_pkg>) for this invoice line item.
230 qsearchs( 'cust_pkg', { 'pkgnum' => $self->pkgnum } );
235 Returns the package definition for this invoice line item.
241 if ( $self->pkgpart_override ) {
242 qsearchs('part_pkg', { 'pkgpart' => $self->pkgpart_override } );
244 $self->cust_pkg->part_pkg;
250 Returns the invoice (see L<FS::cust_bill>) for this invoice line item.
256 qsearchs( 'cust_bill', { 'invnum' => $self->invnum } );
259 =item details [ OPTION => VALUE ... ]
261 Returns an array of detail information for the invoice line item.
263 Currently available options are: I<format> I<escape_function>
265 If I<format> is set to html or latex then the array members are improved
266 for tabular appearance in those environments if possible.
268 If I<escape_function> is set then the array members are processed by this
269 function before being returned.
274 my ( $self, %opt ) = @_;
275 my $format = $opt{format} || '';
276 my $escape_function = $opt{escape_function} || sub { shift };
277 return () unless defined dbdef->table('cust_bill_pkg_detail');
279 eval "use Text::CSV_XS;";
281 my $csv = new Text::CSV_XS;
283 my $format_sub = sub { my $detail = shift;
284 $csv->parse($detail) or return "can't parse $detail";
285 join(' - ', map { &$escape_function($_) }
290 $format_sub = sub { my $detail = shift;
291 $csv->parse($detail) or return "can't parse $detail";
292 join('</TD><TD>', map { &$escape_function($_) }
296 if $format eq 'html';
298 $format_sub = sub { my $detail = shift;
299 $csv->parse($detail) or return "can't parse $detail";
300 #join(' & ', map { '\small{'. &$escape_function($_). '}' }
304 foreach ($csv->fields) {
305 $result .= ' & ' if $column > 1;
306 if ($column > 6) { # KLUDGE ALERT!
307 $result .= '\multicolumn{1}{l}{\small{'.
308 &$escape_function($_). '}}';
310 $result .= '\small{'. &$escape_function($_). '}';
316 if $format eq 'latex';
318 $format_sub = $opt{format_function} if $opt{format_function};
320 map { ( $_->format eq 'C'
321 ? &{$format_sub}( $_->detail )
322 : &{$escape_function}( $_->detail )
325 qsearch ({ 'table' => 'cust_bill_pkg_detail',
326 'hashref' => { 'billpkgnum' => $self->billpkgnum },
327 'order_by' => 'ORDER BY detailnum',
329 #qsearch ( 'cust_bill_pkg_detail', { 'lineitemnum' => $self->lineitemnum });
334 Returns a description for this line item. For typical line items, this is the
335 I<pkg> field of the corresponding B<FS::part_pkg> object (see L<FS::part_pkg>).
336 For one-shot line items and named taxes, it is the I<itemdesc> field of this
337 line item, and for generic taxes, simply returns "Tax".
344 if ( $self->pkgnum > 0 ) {
345 $self->itemdesc || $self->part_pkg->pkg;
347 $self->itemdesc || 'Tax';
353 Returns the amount owed (still outstanding) on this line item's setup fee,
354 which is the amount of the line item minus all payment applications (see
355 L<FS::cust_bill_pay_pkg> and credit applications (see
356 L<FS::cust_credit_bill_pkg>).
362 $self->owed('setup', @_);
367 Returns the amount owed (still outstanding) on this line item's recurring fee,
368 which is the amount of the line item minus all payment applications (see
369 L<FS::cust_bill_pay_pkg> and credit applications (see
370 L<FS::cust_credit_bill_pkg>).
376 $self->owed('recur', @_);
379 # modeled after cust_bill::owed...
381 my( $self, $field ) = @_;
382 my $balance = $self->$field();
383 $balance -= $_->amount foreach ( $self->cust_bill_pay_pkg($field) );
384 $balance -= $_->amount foreach ( $self->cust_credit_bill_pkg($field) );
385 $balance = sprintf( '%.2f', $balance );
386 $balance =~ s/^\-0\.00$/0.00/; #yay ieee fp
390 sub cust_bill_pay_pkg {
391 my( $self, $field ) = @_;
392 qsearch( 'cust_bill_pay_pkg', { 'billpkgnum' => $self->billpkgnum,
393 'setuprecur' => $field,
398 sub cust_credit_bill_pkg {
399 my( $self, $field ) = @_;
400 qsearch( 'cust_credit_bill_pkg', { 'billpkgnum' => $self->billpkgnum,
401 'setuprecur' => $field,
408 Returns the number of billing units (for tax purposes) represented by this,
415 $self->pkgnum ? $self->part_pkg->calc_units($self->cust_pkg) : 0; # 1?
423 my( $self, $value ) = @_;
424 if ( defined($value) ) {
425 $self->setfield('quantity', $value);
427 $self->getfield('quantity') || 1;
435 my( $self, $value ) = @_;
436 if ( defined($value) ) {
437 $self->setfield('unitsetup', $value);
439 $self->getfield('unitsetup') eq ''
440 ? $self->getfield('setup')
441 : $self->getfield('unitsetup');
449 my( $self, $value ) = @_;
450 if ( defined($value) ) {
451 $self->setfield('unitrecur', $value);
453 $self->getfield('unitrecur') eq ''
454 ? $self->getfield('recur')
455 : $self->getfield('unitrecur');
460 Returns the amount of the charge associated with usage class CLASSNUM if
461 CLASSNUM is defined. Otherwise returns the total charge associated with
467 my( $self, $classnum ) = @_;
471 if ( $self->get('details') ) {
475 grep { ref($_) && ( defined($classnum) ? $_->[3] eq $classnum : 1 ) }
476 @{ $self->get('details') };
480 my $hashref = { 'billpkgnum' => $self->billpkgnum };
481 $hashref->{ 'classnum' } = $classnum if defined($classnum);
482 @values = map { $_->amount } qsearch('cust_bill_pkg_detail', $hashref);
486 foreach ( @values ) {
494 Returns a list of usage classnums associated with this invoice line's
502 if ( $self->get('details') ) {
505 foreach my $detail ( grep { ref($_) } @{$self->get('details')} ) {
506 $seen{ $detail->[3] } = 1;
513 qsearch({ table => 'cust_bill_pkg_detail',
514 hashref => { billpkgnum => $self->billpkgnum },
515 select => 'DISTINCT classnum',
526 setup and recur shouldn't be separate fields. There should be one "amount"
527 field and a flag to tell you if it is a setup/one-time fee or a recurring fee.
529 A line item with both should really be two separate records (preserving
530 sdate and edate for setup fees for recurring packages - that information may
531 be valuable later). Invoice generation (cust_main::bill), invoice printing
532 (cust_bill), tax reports (report_tax.cgi) and line item reports
533 (cust_bill_pkg.cgi) would need to be updated.
535 owed_setup and owed_recur could then be repaced by just owed, and
536 cust_bill::open_cust_bill_pkg and
537 cust_bill_ApplicationCommon::apply_to_lineitems could be simplified.
541 L<FS::Record>, L<FS::cust_bill>, L<FS::cust_pkg>, L<FS::cust_main>, schema.html
542 from the base documentation.