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($_). '}' }
275 foreach ($csv->fields) {
276 $result .= ' & ' if $column > 1;
277 if ($column > 6) { # KLUDGE ALERT!
278 $result .= '\multicolumn{1}{l}{\small{'.
279 &$escape_function($_). '}}';
281 $result .= '\small{'. &$escape_function($_). '}';
287 if $format eq 'latex';
289 map { ( $_->format eq 'C'
290 ? &{$format_sub}( $_->detail )
291 : &{$escape_function}( $_->detail )
294 qsearch ({ 'table' => 'cust_bill_pkg_detail',
295 'hashref' => { 'pkgnum' => $self->pkgnum,
296 'invnum' => $self->invnum,
298 'order_by' => 'ORDER BY detailnum',
300 #qsearch ( 'cust_bill_pkg_detail', { 'lineitemnum' => $self->lineitemnum });
305 Returns a description for this line item. For typical line items, this is the
306 I<pkg> field of the corresponding B<FS::part_pkg> object (see L<FS::part_pkg>).
307 For one-shot line items and named taxes, it is the I<itemdesc> field of this
308 line item, and for generic taxes, simply returns "Tax".
315 if ( $self->pkgnum > 0 ) {
316 $self->part_pkg->pkg;
318 $self->itemdesc || 'Tax';
324 Returns the amount owed (still outstanding) on this line item's setup fee,
325 which is the amount of the line item minus all payment applications (see
326 L<FS::cust_bill_pay_pkg> and credit applications (see
327 L<FS::cust_credit_bill_pkg>).
333 $self->owed('setup', @_);
338 Returns the amount owed (still outstanding) on this line item's recurring fee,
339 which is the amount of the line item minus all payment applications (see
340 L<FS::cust_bill_pay_pkg> and credit applications (see
341 L<FS::cust_credit_bill_pkg>).
347 $self->owed('recur', @_);
350 # modeled after cust_bill::owed...
352 my( $self, $field ) = @_;
353 my $balance = $self->$field();
354 $balance -= $_->amount foreach ( $self->cust_bill_pay_pkg($field) );
355 $balance -= $_->amount foreach ( $self->cust_credit_bill_pkg($field) );
356 $balance = sprintf( '%.2f', $balance );
357 $balance =~ s/^\-0\.00$/0.00/; #yay ieee fp
361 sub cust_bill_pay_pkg {
362 my( $self, $field ) = @_;
363 qsearch( 'cust_bill_pay_pkg', { 'billpkgnum' => $self->billpkgnum,
364 'setuprecur' => $field,
369 sub cust_credit_bill_pkg {
370 my( $self, $field ) = @_;
371 qsearch( 'cust_credit_bill_pkg', { 'billpkgnum' => $self->billpkgnum,
372 'setuprecur' => $field,
379 Returns the number of billing units (for tax purposes) represented by this,
386 $self->part_pkg->calc_units($self->cust_pkg);
394 my( $self, $value ) = @_;
395 if ( defined($value) ) {
396 $self->setfield('quantity', $value);
398 $self->getfield('quantity') || 1;
406 my( $self, $value ) = @_;
407 if ( defined($value) ) {
408 $self->setfield('unitsetup', $value);
410 $self->getfield('unitsetup') eq ''
411 ? $self->getfield('setup')
412 : $self->getfield('unitsetup');
420 my( $self, $value ) = @_;
421 if ( defined($value) ) {
422 $self->setfield('unitrecur', $value);
424 $self->getfield('unitrecur') eq ''
425 ? $self->getfield('recur')
426 : $self->getfield('unitrecur');
433 setup and recur shouldn't be separate fields. There should be one "amount"
434 field and a flag to tell you if it is a setup/one-time fee or a recurring fee.
436 A line item with both should really be two separate records (preserving
437 sdate and edate for setup fees for recurring packages - that information may
438 be valuable later). Invoice generation (cust_main::bill), invoice printing
439 (cust_bill), tax reports (report_tax.cgi) and line item reports
440 (cust_bill_pkg.cgi) would need to be updated.
442 owed_setup and owed_recur could then be repaced by just owed, and
443 cust_bill::open_cust_bill_pkg and
444 cust_bill_ApplicationCommon::apply_to_lineitems could be simplified.
448 L<FS::Record>, L<FS::cust_bill>, L<FS::cust_pkg>, L<FS::cust_main>, schema.html
449 from the base documentation.