1 package FS::cust_bill_pkg;
5 use FS::Record qw( qsearch qsearchs dbdef dbh );
6 use FS::cust_main_Mixin;
9 use FS::cust_bill_pkg_detail;
10 use FS::cust_bill_pay_pkg;
11 use FS::cust_credit_bill_pkg;
13 @ISA = qw( FS::cust_main_Mixin FS::Record );
17 FS::cust_bill_pkg - Object methods for cust_bill_pkg records
21 use FS::cust_bill_pkg;
23 $record = new FS::cust_bill_pkg \%hash;
24 $record = new FS::cust_bill_pkg { 'column' => 'value' };
26 $error = $record->insert;
28 $error = $new_record->replace($old_record);
30 $error = $record->delete;
32 $error = $record->check;
36 An FS::cust_bill_pkg object represents an invoice line item.
37 FS::cust_bill_pkg inherits from FS::Record. The following fields are currently
42 =item billpkgnum - primary key
44 =item invnum - invoice (see L<FS::cust_bill>)
46 =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)
48 =item setup - setup fee
50 =item recur - recurring fee
52 =item sdate - starting date of recurring fee
54 =item edate - ending date of recurring fee
56 =item itemdesc - Line item description (currentlty used only when pkgnum is 0 or -1)
58 =item quantity - If not set, defaults to 1
60 =item unitsetup - If not set, defaults to setup
62 =item unitrecur - If not set, defaults to recur
66 sdate and edate are specified as UNIX timestamps; see L<perlfunc/"time">. Also
67 see L<Time::Local> and L<Date::Parse> for conversion functions.
75 Creates a new line item. To add the line item to the database, see
76 L<"insert">. Line items are normally created by calling the bill method of a
77 customer object (see L<FS::cust_main>).
81 sub table { 'cust_bill_pkg'; }
85 Adds this line item to the database. If there is an error, returns the error,
86 otherwise returns false.
93 local $SIG{HUP} = 'IGNORE';
94 local $SIG{INT} = 'IGNORE';
95 local $SIG{QUIT} = 'IGNORE';
96 local $SIG{TERM} = 'IGNORE';
97 local $SIG{TSTP} = 'IGNORE';
98 local $SIG{PIPE} = 'IGNORE';
100 my $oldAutoCommit = $FS::UID::AutoCommit;
101 local $FS::UID::AutoCommit = 0;
104 my $error = $self->SUPER::insert;
106 $dbh->rollback if $oldAutoCommit;
110 unless ( defined dbdef->table('cust_bill_pkg_detail') && $self->get('details') ) {
111 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
115 foreach my $detail ( @{$self->get('details')} ) {
116 my $cust_bill_pkg_detail = new FS::cust_bill_pkg_detail {
117 'pkgnum' => $self->pkgnum,
118 'invnum' => $self->invnum,
119 'format' => (ref($detail) ? $detail->[0] : '' ),
120 'detail' => (ref($detail) ? $detail->[1] : $detail ),
122 $error = $cust_bill_pkg_detail->insert;
124 $dbh->rollback if $oldAutoCommit;
129 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
136 Currently unimplemented. I don't remove line items because there would then be
137 no record the items ever existed (which is bad, no?)
142 return "Can't delete cust_bill_pkg records!";
145 =item replace OLD_RECORD
147 Currently unimplemented. This would be even more of an accounting nightmare
148 than deleteing the items. Just don't do it.
153 return "Can't modify cust_bill_pkg records!";
158 Checks all fields to make sure this is a valid line item. If there is an
159 error, returns the error, otherwise returns false. Called by the insert
168 $self->ut_numbern('billpkgnum')
169 || $self->ut_snumber('pkgnum')
170 || $self->ut_number('invnum')
171 || $self->ut_money('setup')
172 || $self->ut_money('recur')
173 || $self->ut_numbern('sdate')
174 || $self->ut_numbern('edate')
175 || $self->ut_textn('itemdesc')
177 return $error if $error;
179 #if ( $self->pkgnum != 0 ) { #allow unchecked pkgnum 0 for tax! (add to part_pkg?)
180 if ( $self->pkgnum > 0 ) { #allow -1 for non-pkg line items and 0 for tax (add to part_pkg?)
181 return "Unknown pkgnum ". $self->pkgnum
182 unless qsearchs( 'cust_pkg', { 'pkgnum' => $self->pkgnum } );
185 return "Unknown invnum"
186 unless qsearchs( 'cust_bill' ,{ 'invnum' => $self->invnum } );
193 Returns the package (see L<FS::cust_pkg>) for this invoice line item.
199 qsearchs( 'cust_pkg', { 'pkgnum' => $self->pkgnum } );
204 Returns the invoice (see L<FS::cust_bill>) for this invoice line item.
210 qsearchs( 'cust_bill', { 'invnum' => $self->invnum } );
213 =item details [ OPTION => VALUE ... ]
215 Returns an array of detail information for the invoice line item.
217 Currently available options are: I<format> I<escape_function>
219 If I<format> is set to html or latex then the array members are improved
220 for tabular appearance in those environments if possible.
222 If I<escape_function> is set then the array members are processed by this
223 function before being returned.
228 my ( $self, %opt ) = @_;
229 my $format = $opt{format} || '';
230 my $escape_function = $opt{escape_function} || sub { shift };
231 return () unless defined dbdef->table('cust_bill_pkg_detail');
233 eval "use Text::CSV_XS;";
235 my $csv = new Text::CSV_XS;
237 my $format_sub = sub { my $detail = shift;
238 $csv->parse($detail) or return "can't parse $detail";
239 join(' - ', map { &$escape_function($_) }
244 $format_sub = sub { my $detail = shift;
245 $csv->parse($detail) or return "can't parse $detail";
246 join('</TD><TD>', map { &$escape_function($_) }
250 if $format eq 'html';
252 $format_sub = sub { my $detail = shift;
253 $csv->parse($detail) or return "can't parse $detail";
254 #join(' & ', map { '\small{'. &$escape_function($_). '}' }
258 foreach ($csv->fields) {
259 $result .= ' & ' if $column > 1;
260 if ($column > 6) { # KLUDGE ALERT!
261 $result .= '\multicolumn{1}{l}{\small{'.
262 &$escape_function($_). '}}';
264 $result .= '\small{'. &$escape_function($_). '}';
270 if $format eq 'latex';
272 map { ( $_->format eq 'C'
273 ? &{$format_sub}( $_->detail )
274 : &{$escape_function}( $_->detail )
277 qsearch ({ 'table' => 'cust_bill_pkg_detail',
278 'hashref' => { 'pkgnum' => $self->pkgnum,
279 'invnum' => $self->invnum,
281 'order_by' => 'ORDER BY detailnum',
283 #qsearch ( 'cust_bill_pkg_detail', { 'lineitemnum' => $self->lineitemnum });
288 Returns a description for this line item. For typical line items, this is the
289 I<pkg> field of the corresponding B<FS::part_pkg> object (see L<FS::part_pkg>).
290 For one-shot line items and named taxes, it is the I<itemdesc> field of this
291 line item, and for generic taxes, simply returns "Tax".
298 if ( $self->pkgnum > 0 ) {
299 $self->cust_pkg->part_pkg->pkg;
301 $self->itemdesc || 'Tax';
307 Returns the amount owed (still outstanding) on this line item's setup fee,
308 which is the amount of the line item minus all payment applications (see
309 L<FS::cust_bill_pay_pkg> and credit applications (see
310 L<FS::cust_credit_bill_pkg>).
316 $self->owed('setup', @_);
321 Returns the amount owed (still outstanding) on this line item's recurring fee,
322 which is the amount of the line item minus all payment applications (see
323 L<FS::cust_bill_pay_pkg> and credit applications (see
324 L<FS::cust_credit_bill_pkg>).
330 $self->owed('recur', @_);
333 # modeled after cust_bill::owed...
335 my( $self, $field ) = @_;
336 my $balance = $self->$field();
337 $balance -= $_->amount foreach ( $self->cust_bill_pay_pkg($field) );
338 $balance -= $_->amount foreach ( $self->cust_credit_bill_pkg($field) );
339 $balance = sprintf( '%.2f', $balance );
340 $balance =~ s/^\-0\.00$/0.00/; #yay ieee fp
344 sub cust_bill_pay_pkg {
345 my( $self, $field ) = @_;
346 qsearch( 'cust_bill_pay_pkg', { 'billpkgnum' => $self->billpkgnum,
347 'setuprecur' => $field,
352 sub cust_credit_bill_pkg {
353 my( $self, $field ) = @_;
354 qsearch( 'cust_credit_bill_pkg', { 'billpkgnum' => $self->billpkgnum,
355 'setuprecur' => $field,
365 my( $self, $value ) = @_;
366 if ( defined($value) ) {
367 $self->setfield('quantity', $value);
369 $self->getfield('quantity') || 1;
377 my( $self, $value ) = @_;
378 if ( defined($value) ) {
379 $self->setfield('unitsetup', $value);
381 $self->getfield('unitsetup') eq ''
382 ? $self->getfield('setup')
383 : $self->getfield('unitsetup');
391 my( $self, $value ) = @_;
392 if ( defined($value) ) {
393 $self->setfield('unitrecur', $value);
395 $self->getfield('unitrecur') eq ''
396 ? $self->getfield('recur')
397 : $self->getfield('unitrecur');
404 setup and recur shouldn't be separate fields. There should be one "amount"
405 field and a flag to tell you if it is a setup/one-time fee or a recurring fee.
407 A line item with both should really be two separate records (preserving
408 sdate and edate for setup fees for recurring packages - that information may
409 be valuable later). Invoice generation (cust_main::bill), invoice printing
410 (cust_bill), tax reports (report_tax.cgi) and line item reports
411 (cust_bill_pkg.cgi) would need to be updated.
413 owed_setup and owed_recur could then be repaced by just owed, and
414 cust_bill::open_cust_bill_pkg and
415 cust_bill_ApplicationCommon::apply_to_lineitems could be simplified.
419 L<FS::Record>, L<FS::cust_bill>, L<FS::cust_pkg>, L<FS::cust_main>, schema.html
420 from the base documentation.