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)
60 =item quantity - If not set, defaults to 1
62 =item unitsetup - If not set, defaults to setup
64 =item unitrecur - If not set, defaults to recur
68 sdate and edate are specified as UNIX timestamps; see L<perlfunc/"time">. Also
69 see L<Time::Local> and L<Date::Parse> for conversion functions.
77 Creates a new line item. To add the line item to the database, see
78 L<"insert">. Line items are normally created by calling the bill method of a
79 customer object (see L<FS::cust_main>).
83 sub table { 'cust_bill_pkg'; }
87 Adds this line item to the database. If there is an error, returns the error,
88 otherwise returns false.
95 local $SIG{HUP} = 'IGNORE';
96 local $SIG{INT} = 'IGNORE';
97 local $SIG{QUIT} = 'IGNORE';
98 local $SIG{TERM} = 'IGNORE';
99 local $SIG{TSTP} = 'IGNORE';
100 local $SIG{PIPE} = 'IGNORE';
102 my $oldAutoCommit = $FS::UID::AutoCommit;
103 local $FS::UID::AutoCommit = 0;
106 my $error = $self->SUPER::insert;
108 $dbh->rollback if $oldAutoCommit;
112 unless ( defined dbdef->table('cust_bill_pkg_detail') && $self->get('details') ) {
113 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
117 foreach my $detail ( @{$self->get('details')} ) {
118 my $cust_bill_pkg_detail = new FS::cust_bill_pkg_detail {
119 'pkgnum' => $self->pkgnum,
120 'invnum' => $self->invnum,
121 'format' => (ref($detail) ? $detail->[0] : '' ),
122 'detail' => (ref($detail) ? $detail->[1] : $detail ),
124 $error = $cust_bill_pkg_detail->insert;
126 $dbh->rollback if $oldAutoCommit;
131 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
138 Currently unimplemented. I don't remove line items because there would then be
139 no record the items ever existed (which is bad, no?)
144 return "Can't delete cust_bill_pkg records!";
147 =item replace OLD_RECORD
149 Currently unimplemented. This would be even more of an accounting nightmare
150 than deleteing the items. Just don't do it.
155 return "Can't modify cust_bill_pkg records!";
160 Checks all fields to make sure this is a valid line item. If there is an
161 error, returns the error, otherwise returns false. Called by the insert
170 $self->ut_numbern('billpkgnum')
171 || $self->ut_snumber('pkgnum')
172 || $self->ut_number('invnum')
173 || $self->ut_money('setup')
174 || $self->ut_money('recur')
175 || $self->ut_numbern('sdate')
176 || $self->ut_numbern('edate')
177 || $self->ut_textn('itemdesc')
179 return $error if $error;
181 #if ( $self->pkgnum != 0 ) { #allow unchecked pkgnum 0 for tax! (add to part_pkg?)
182 if ( $self->pkgnum > 0 ) { #allow -1 for non-pkg line items and 0 for tax (add to part_pkg?)
183 return "Unknown pkgnum ". $self->pkgnum
184 unless qsearchs( 'cust_pkg', { 'pkgnum' => $self->pkgnum } );
187 return "Unknown invnum"
188 unless qsearchs( 'cust_bill' ,{ 'invnum' => $self->invnum } );
195 Returns the package (see L<FS::cust_pkg>) for this invoice line item.
201 qsearchs( 'cust_pkg', { 'pkgnum' => $self->pkgnum } );
206 Returns the package definition for this invoice line item.
212 if ( $self->pkgpart_override ) {
213 qsearchs('part_pkg', { 'pkgpart' => $self->pkgpart_override } );
215 $self->cust_pkg->part_pkg;
221 Returns the invoice (see L<FS::cust_bill>) for this invoice line item.
227 qsearchs( 'cust_bill', { 'invnum' => $self->invnum } );
230 =item details [ OPTION => VALUE ... ]
232 Returns an array of detail information for the invoice line item.
234 Currently available options are: I<format> I<escape_function>
236 If I<format> is set to html or latex then the array members are improved
237 for tabular appearance in those environments if possible.
239 If I<escape_function> is set then the array members are processed by this
240 function before being returned.
245 my ( $self, %opt ) = @_;
246 my $format = $opt{format} || '';
247 my $escape_function = $opt{escape_function} || sub { shift };
248 return () unless defined dbdef->table('cust_bill_pkg_detail');
250 eval "use Text::CSV_XS;";
252 my $csv = new Text::CSV_XS;
254 my $format_sub = sub { my $detail = shift;
255 $csv->parse($detail) or return "can't parse $detail";
256 join(' - ', map { &$escape_function($_) }
261 $format_sub = sub { my $detail = shift;
262 $csv->parse($detail) or return "can't parse $detail";
263 join('</TD><TD>', map { &$escape_function($_) }
267 if $format eq 'html';
269 $format_sub = sub { my $detail = shift;
270 $csv->parse($detail) or return "can't parse $detail";
271 join(' & ', map { '\small{'. &$escape_function($_). '}' }
274 if $format eq 'latex';
276 map { ( $_->format eq 'C'
277 ? &{$format_sub}( $_->detail )
278 : &{$escape_function}( $_->detail )
281 qsearch ({ 'table' => 'cust_bill_pkg_detail',
282 'hashref' => { 'pkgnum' => $self->pkgnum,
283 'invnum' => $self->invnum,
285 'order_by' => 'ORDER BY detailnum',
287 #qsearch ( 'cust_bill_pkg_detail', { 'lineitemnum' => $self->lineitemnum });
292 Returns a description for this line item. For typical line items, this is the
293 I<pkg> field of the corresponding B<FS::part_pkg> object (see L<FS::part_pkg>).
294 For one-shot line items and named taxes, it is the I<itemdesc> field of this
295 line item, and for generic taxes, simply returns "Tax".
302 if ( $self->pkgnum > 0 ) {
303 $self->part_pkg->pkg;
305 $self->itemdesc || 'Tax';
311 Returns the amount owed (still outstanding) on this line item's setup fee,
312 which is the amount of the line item minus all payment applications (see
313 L<FS::cust_bill_pay_pkg> and credit applications (see
314 L<FS::cust_credit_bill_pkg>).
320 $self->owed('setup', @_);
325 Returns the amount owed (still outstanding) on this line item's recurring fee,
326 which is the amount of the line item minus all payment applications (see
327 L<FS::cust_bill_pay_pkg> and credit applications (see
328 L<FS::cust_credit_bill_pkg>).
334 $self->owed('recur', @_);
337 # modeled after cust_bill::owed...
339 my( $self, $field ) = @_;
340 my $balance = $self->$field();
341 $balance -= $_->amount foreach ( $self->cust_bill_pay_pkg($field) );
342 $balance -= $_->amount foreach ( $self->cust_credit_bill_pkg($field) );
343 $balance = sprintf( '%.2f', $balance );
344 $balance =~ s/^\-0\.00$/0.00/; #yay ieee fp
348 sub cust_bill_pay_pkg {
349 my( $self, $field ) = @_;
350 qsearch( 'cust_bill_pay_pkg', { 'billpkgnum' => $self->billpkgnum,
351 'setuprecur' => $field,
356 sub cust_credit_bill_pkg {
357 my( $self, $field ) = @_;
358 qsearch( 'cust_credit_bill_pkg', { 'billpkgnum' => $self->billpkgnum,
359 'setuprecur' => $field,
366 Returns the number of billing units (for tax purposes) represented by this,
373 $self->part_pkg->calc_units($self->cust_pkg);
381 my( $self, $value ) = @_;
382 if ( defined($value) ) {
383 $self->setfield('quantity', $value);
385 $self->getfield('quantity') || 1;
393 my( $self, $value ) = @_;
394 if ( defined($value) ) {
395 $self->setfield('unitsetup', $value);
397 $self->getfield('unitsetup') eq ''
398 ? $self->getfield('setup')
399 : $self->getfield('unitsetup');
407 my( $self, $value ) = @_;
408 if ( defined($value) ) {
409 $self->setfield('unitrecur', $value);
411 $self->getfield('unitrecur') eq ''
412 ? $self->getfield('recur')
413 : $self->getfield('unitrecur');
420 setup and recur shouldn't be separate fields. There should be one "amount"
421 field and a flag to tell you if it is a setup/one-time fee or a recurring fee.
423 A line item with both should really be two separate records (preserving
424 sdate and edate for setup fees for recurring packages - that information may
425 be valuable later). Invoice generation (cust_main::bill), invoice printing
426 (cust_bill), tax reports (report_tax.cgi) and line item reports
427 (cust_bill_pkg.cgi) would need to be updated.
429 owed_setup and owed_recur could then be repaced by just owed, and
430 cust_bill::open_cust_bill_pkg and
431 cust_bill_ApplicationCommon::apply_to_lineitems could be simplified.
435 L<FS::Record>, L<FS::cust_bill>, L<FS::cust_pkg>, L<FS::cust_main>, schema.html
436 from the base documentation.