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,
115 'format' => (ref($detail) ? $detail->[0] : '' ),
116 'detail' => (ref($detail) ? $detail->[1] : $detail ),
118 $error = $cust_bill_pkg_detail->insert;
120 $dbh->rollback if $oldAutoCommit;
125 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
132 Currently unimplemented. I don't remove line items because there would then be
133 no record the items ever existed (which is bad, no?)
138 return "Can't delete cust_bill_pkg records!";
141 =item replace OLD_RECORD
143 Currently unimplemented. This would be even more of an accounting nightmare
144 than deleteing the items. Just don't do it.
149 return "Can't modify cust_bill_pkg records!";
154 Checks all fields to make sure this is a valid line item. If there is an
155 error, returns the error, otherwise returns false. Called by the insert
164 $self->ut_numbern('billpkgnum')
165 || $self->ut_snumber('pkgnum')
166 || $self->ut_number('invnum')
167 || $self->ut_money('setup')
168 || $self->ut_money('recur')
169 || $self->ut_numbern('sdate')
170 || $self->ut_numbern('edate')
171 || $self->ut_textn('itemdesc')
173 return $error if $error;
175 #if ( $self->pkgnum != 0 ) { #allow unchecked pkgnum 0 for tax! (add to part_pkg?)
176 if ( $self->pkgnum > 0 ) { #allow -1 for non-pkg line items and 0 for tax (add to part_pkg?)
177 return "Unknown pkgnum ". $self->pkgnum
178 unless qsearchs( 'cust_pkg', { 'pkgnum' => $self->pkgnum } );
181 return "Unknown invnum"
182 unless qsearchs( 'cust_bill' ,{ 'invnum' => $self->invnum } );
189 Returns the package (see L<FS::cust_pkg>) for this invoice line item.
195 qsearchs( 'cust_pkg', { 'pkgnum' => $self->pkgnum } );
200 Returns the package definition for this invoice line item.
206 if ( $self->pkgpart_override ) {
207 qsearchs('part_pkg', { 'pkgpart' => $self->pkgpart_override } );
209 $self->cust_pkg->part_pkg;
215 Returns the invoice (see L<FS::cust_bill>) for this invoice line item.
221 qsearchs( 'cust_bill', { 'invnum' => $self->invnum } );
224 =item details [ OPTION => VALUE ... ]
226 Returns an array of detail information for the invoice line item.
228 Currently available options are: I<format> I<escape_function>
230 If I<format> is set to html or latex then the array members are improved
231 for tabular appearance in those environments if possible.
233 If I<escape_function> is set then the array members are processed by this
234 function before being returned.
239 my ( $self, %opt ) = @_;
240 my $format = $opt{format} || '';
241 my $escape_function = $opt{escape_function} || sub { shift };
242 return () unless defined dbdef->table('cust_bill_pkg_detail');
244 eval "use Text::CSV_XS;";
246 my $csv = new Text::CSV_XS;
248 my $format_sub = sub { my $detail = shift;
249 $csv->parse($detail) or return "can't parse $detail";
250 join(' - ', map { &$escape_function($_) }
255 $format_sub = sub { my $detail = shift;
256 $csv->parse($detail) or return "can't parse $detail";
257 join('</TD><TD>', map { &$escape_function($_) }
261 if $format eq 'html';
263 $format_sub = sub { my $detail = shift;
264 $csv->parse($detail) or return "can't parse $detail";
265 join(' & ', map { &$escape_function($_) } $csv->fields );
267 if $format eq 'latex';
269 map { ( $_->format eq 'C'
270 ? &{$format_sub}( $_->detail )
271 : &{$escape_function}( $_->detail )
274 qsearch ({ 'table' => 'cust_bill_pkg_detail',
275 'hashref' => { 'pkgnum' => $self->pkgnum,
276 'invnum' => $self->invnum,
278 'order_by' => 'ORDER BY detailnum',
280 #qsearch ( 'cust_bill_pkg_detail', { 'lineitemnum' => $self->lineitemnum });
285 Returns a description for this line item. For typical line items, this is the
286 I<pkg> field of the corresponding B<FS::part_pkg> object (see L<FS::part_pkg>).
287 For one-shot line items and named taxes, it is the I<itemdesc> field of this
288 line item, and for generic taxes, simply returns "Tax".
295 if ( $self->pkgnum > 0 ) {
296 $self->part_pkg->pkg;
298 $self->itemdesc || 'Tax';
304 Returns the amount owed (still outstanding) on this line item's setup fee,
305 which is the amount of the line item minus all payment applications (see
306 L<FS::cust_bill_pay_pkg> and credit applications (see
307 L<FS::cust_credit_bill_pkg>).
313 $self->owed('setup', @_);
318 Returns the amount owed (still outstanding) on this line item's recurring fee,
319 which is the amount of the line item minus all payment applications (see
320 L<FS::cust_bill_pay_pkg> and credit applications (see
321 L<FS::cust_credit_bill_pkg>).
327 $self->owed('recur', @_);
330 # modeled after cust_bill::owed...
332 my( $self, $field ) = @_;
333 my $balance = $self->$field();
334 $balance -= $_->amount foreach ( $self->cust_bill_pay_pkg($field) );
335 $balance -= $_->amount foreach ( $self->cust_credit_bill_pkg($field) );
336 $balance = sprintf( '%.2f', $balance );
337 $balance =~ s/^\-0\.00$/0.00/; #yay ieee fp
341 sub cust_bill_pay_pkg {
342 my( $self, $field ) = @_;
343 qsearch( 'cust_bill_pay_pkg', { 'billpkgnum' => $self->billpkgnum,
344 'setuprecur' => $field,
349 sub cust_credit_bill_pkg {
350 my( $self, $field ) = @_;
351 qsearch( 'cust_credit_bill_pkg', { 'billpkgnum' => $self->billpkgnum,
352 'setuprecur' => $field,
359 Returns the number of billing units (for tax purposes) represented by this,
366 $self->part_pkg->calc_units($self->cust_pkg);
373 setup and recur shouldn't be separate fields. There should be one "amount"
374 field and a flag to tell you if it is a setup/one-time fee or a recurring fee.
376 A line item with both should really be two separate records (preserving
377 sdate and edate for setup fees for recurring packages - that information may
378 be valuable later). Invoice generation (cust_main::bill), invoice printing
379 (cust_bill), tax reports (report_tax.cgi) and line item reports
380 (cust_bill_pkg.cgi) would need to be updated.
382 owed_setup and owed_recur could then be repaced by just owed, and
383 cust_bill::open_cust_bill_pkg and
384 cust_bill_ApplicationCommon::apply_to_lineitems could be simplified.
388 L<FS::Record>, L<FS::cust_bill>, L<FS::cust_pkg>, L<FS::cust_main>, schema.html
389 from the base documentation.