4 use vars qw( @ISA $conf $invoice_template $money_char );
5 use vars qw( $invoice_lines @buf ); #yuck
8 use FS::Record qw( qsearch qsearchs );
10 use FS::cust_bill_pkg;
14 use FS::cust_credit_bill;
16 @ISA = qw( FS::Record );
18 #ask FS::UID to run this stuff for us later
19 $FS::UID::callback{'FS::cust_bill'} = sub {
23 $money_char = $conf->config('money_char') || '$';
25 my @invoice_template = $conf->config('invoice_template')
26 or die "cannot load config file invoice_template";
28 foreach ( grep /invoice_lines\(\d+\)/, @invoice_template ) { #kludgy
29 /invoice_lines\((\d+)\)/;
32 die "no invoice_lines() functions in template?" unless $invoice_lines;
33 $invoice_template = new Text::Template (
35 SOURCE => [ map "$_\n", @invoice_template ],
36 ) or die "can't create new Text::Template object: $Text::Template::ERROR";
37 $invoice_template->compile()
38 or die "can't compile template: $Text::Template::ERROR";
43 FS::cust_bill - Object methods for cust_bill records
49 $record = new FS::cust_bill \%hash;
50 $record = new FS::cust_bill { 'column' => 'value' };
52 $error = $record->insert;
54 $error = $new_record->replace($old_record);
56 $error = $record->delete;
58 $error = $record->check;
60 ( $total_previous_balance, @previous_cust_bill ) = $record->previous;
62 @cust_bill_pkg_objects = $cust_bill->cust_bill_pkg;
64 ( $total_previous_credits, @previous_cust_credit ) = $record->cust_credit;
66 @cust_pay_objects = $cust_bill->cust_pay;
68 $tax_amount = $record->tax;
70 @lines = $cust_bill->print_text;
71 @lines = $cust_bill->print_text $time;
75 An FS::cust_bill object represents an invoice; a declaration that a customer
76 owes you money. The specific charges are itemized as B<cust_bill_pkg> records
77 (see L<FS::cust_bill_pkg>). FS::cust_bill inherits from FS::Record. The
78 following fields are currently supported:
82 =item invnum - primary key (assigned automatically for new invoices)
84 =item custnum - customer (see L<FS::cust_main>)
86 =item _date - specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
87 L<Time::Local> and L<Date::Parse> for conversion functions.
89 =item charged - amount of this invoice
91 =item printed - how many times this invoice has been printed automatically
92 (see L<FS::cust_main/"collect">).
102 Creates a new invoice. To add the invoice to the database, see L<"insert">.
103 Invoices are normally created by calling the bill method of a customer object
104 (see L<FS::cust_main>).
108 sub table { 'cust_bill'; }
112 Adds this invoice to the database ("Posts" the invoice). If there is an error,
113 returns the error, otherwise returns false.
117 Currently unimplemented. I don't remove invoices because there would then be
118 no record you ever posted this invoice (which is bad, no?)
123 return "Can't remove invoice!"
126 =item replace OLD_RECORD
128 Replaces the OLD_RECORD with this one in the database. If there is an error,
129 returns the error, otherwise returns false.
131 Only printed may be changed. printed is normally updated by calling the
132 collect method of a customer object (see L<FS::cust_main>).
137 my( $new, $old ) = ( shift, shift );
138 return "Can't change custnum!" unless $old->custnum == $new->custnum;
139 #return "Can't change _date!" unless $old->_date eq $new->_date;
140 return "Can't change _date!" unless $old->_date == $new->_date;
141 return "Can't change charged!" unless $old->charged == $new->charged;
143 $new->SUPER::replace($old);
148 Checks all fields to make sure this is a valid invoice. If there is an error,
149 returns the error, otherwise returns false. Called by the insert and replace
158 $self->ut_numbern('invnum')
159 || $self->ut_number('custnum')
160 || $self->ut_numbern('_date')
161 || $self->ut_money('charged')
162 || $self->ut_numbern('printed')
164 return $error if $error;
166 return "Unknown customer"
167 unless qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
169 $self->_date(time) unless $self->_date;
171 $self->printed(0) if $self->printed eq '';
178 Returns a list consisting of the total previous balance for this customer,
179 followed by the previous outstanding invoices (as FS::cust_bill objects also).
186 my @cust_bill = sort { $a->_date <=> $b->_date }
187 grep { $_->owed != 0 && $_->_date < $self->_date }
188 qsearch( 'cust_bill', { 'custnum' => $self->custnum } )
190 foreach ( @cust_bill ) { $total += $_->owed; }
196 Returns the line items (see L<FS::cust_bill_pkg>) for this invoice.
202 qsearch( 'cust_bill_pkg', { 'invnum' => $self->invnum } );
207 Depreciated. See the cust_credited method.
209 #Returns a list consisting of the total previous credited (see
210 #L<FS::cust_credit>) and unapplied for this customer, followed by the previous
211 #outstanding credits (FS::cust_credit objects).
217 croak "FS::cust_bill->cust_credit depreciated; see ".
218 "FS::cust_bill->cust_credit_bill";
221 #my @cust_credit = sort { $a->_date <=> $b->_date }
222 # grep { $_->credited != 0 && $_->_date < $self->_date }
223 # qsearch('cust_credit', { 'custnum' => $self->custnum } )
225 #foreach (@cust_credit) { $total += $_->credited; }
226 #$total, @cust_credit;
231 Depreciated. See the cust_bill_pay method.
233 #Returns all payments (see L<FS::cust_pay>) for this invoice.
239 croak "FS::cust_bill->cust_pay depreciated; see FS::cust_bill->cust_bill_pay";
241 #sort { $a->_date <=> $b->_date }
242 # qsearch( 'cust_pay', { 'invnum' => $self->invnum } )
248 Returns all payment applications (see L<FS::cust_bill_pay>) for this invoice.
254 sort { $a->_date <=> $b->_date }
255 qsearch( 'cust_bill_pay', { 'invnum' => $self->invnum } );
260 Returns all applied credits (see L<FS::cust_credit_bill>) for this invoice.
266 sort { $a->_date <=> $b->_date }
267 qsearch( 'cust_credit_bill', { 'invnum' => $self->invnum } )
273 Returns the tax amount (see L<FS::cust_bill_pkg>) for this invoice.
280 my @taxlines = qsearch( 'cust_bill_pkg', { 'invnum' => $self->invnum ,
282 foreach (@taxlines) { $total += $_->setup; }
288 Returns the amount owed (still outstanding) on this invoice, which is charged
289 minus all payment applications (see L<FS::cust_bill_pay>) and credit
290 applications (see L<FS::cust_credit_bill>).
296 my $balance = $self->charged;
297 $balance -= $_->amount foreach ( $self->cust_bill_pay );
298 $balance -= $_->amount foreach ( $self->cust_credited );
299 $balance = sprintf( "%.2f", $balance);
300 $balance =~ s/^\-0\.00$/0.00/; #yay ieee fp
304 =item print_text [TIME];
306 Returns an text invoice, as a list of lines.
308 TIME an optional value used to control the printing of overdue messages. The
309 default is now. It isn't the date of the invoice; that's the `_date' field.
310 It is specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
311 L<Time::Local> and L<Date::Parse> for conversion functions.
317 my( $self, $today ) = ( shift, shift );
319 # my $invnum = $self->invnum;
320 my $cust_main = qsearchs('cust_main', { 'custnum', $self->custnum } );
321 $cust_main->payname( $cust_main->first. ' '. $cust_main->getfield('last') )
322 unless $cust_main->payname;
324 my( $pr_total, @pr_cust_bill ) = $self->previous; #previous balance
325 # my( $cr_total, @cr_cust_credit ) = $self->cust_credit; #credits
326 #my $balance_due = $self->owed + $pr_total - $cr_total;
327 my $balance_due = $self->owed + $pr_total;
330 #my($description,$amount);
334 foreach ( @pr_cust_bill ) {
336 "Previous Balance, Invoice #". $_->invnum.
337 " (". time2str("%x",$_->_date). ")",
338 $money_char. sprintf("%10.2f",$_->owed)
342 push @buf,['','-----------'];
343 push @buf,[ 'Total Previous Balance',
344 $money_char. sprintf("%10.2f",$pr_total ) ];
349 foreach ( $self->cust_bill_pkg ) {
353 my($cust_pkg)=qsearchs('cust_pkg', { 'pkgnum', $_->pkgnum } );
354 my($part_pkg)=qsearchs('part_pkg',{'pkgpart'=>$cust_pkg->pkgpart});
355 my($pkg)=$part_pkg->pkg;
357 if ( $_->setup != 0 ) {
358 push @buf, [ "$pkg Setup", $money_char. sprintf("%10.2f",$_->setup) ];
360 map { [ " ". $_->[0]. ": ". $_->[1], '' ] } $cust_pkg->labels;
363 if ( $_->recur != 0 ) {
365 "$pkg (" . time2str("%x",$_->sdate) . " - " .
366 time2str("%x",$_->edate) . ")",
367 $money_char. sprintf("%10.2f",$_->recur)
370 map { [ " ". $_->[0]. ": ". $_->[1], '' ] } $cust_pkg->labels;
374 push @buf,["Tax", $money_char. sprintf("%10.2f",$_->setup) ]
379 push @buf,['','-----------'];
380 push @buf,['Total New Charges',
381 $money_char. sprintf("%10.2f",$self->charged) ];
384 push @buf,['','-----------'];
385 push @buf,['Total Charges',
386 $money_char. sprintf("%10.2f",$self->charged + $pr_total) ];
390 foreach ( $self->cust_credited ) {
392 #something more elaborate if $_->amount ne $_->cust_credit->credited ?
394 my $reason = substr($_->cust_credit->reason,0,32);
395 $reason .= '...' if length($reason) < length($_->cust_credit->reason);
396 $reason = " ($reason) " if $reason;
398 "Credit #". $_->crednum. " (". time2str("%x",$_->cust_credit->_date) .")".
400 $money_char. sprintf("%10.2f",$_->amount)
403 #foreach ( @cr_cust_credit ) {
405 # "Credit #". $_->crednum. " (" . time2str("%x",$_->_date) .")",
406 # $money_char. sprintf("%10.2f",$_->credited)
410 #get & print payments
411 foreach ( $self->cust_bill_pay ) {
413 #something more elaborate if $_->amount ne ->cust_pay->paid ?
416 "Payment received ". time2str("%x",$_->cust_pay->_date ),
417 $money_char. sprintf("%10.2f",$_->amount )
422 push @buf,['','-----------'];
423 push @buf,['Balance Due', $money_char.
424 sprintf("%10.2f", $balance_due ) ];
426 #setup template variables
428 package FS::cust_bill::_template; #!
429 use vars qw( $invnum $date $page $total_pages @address $overdue @buf );
431 $invnum = $self->invnum;
432 $date = $self->_date;
436 int( scalar(@FS::cust_bill::buf) / $FS::cust_bill::invoice_lines );
438 if scalar(@FS::cust_bill::buf) % $FS::cust_bill::invoice_lines;
441 #format address (variable for the template)
443 @address = ( '', '', '', '', '', '' );
444 package FS::cust_bill; #!
445 $FS::cust_bill::_template::address[$l++] =
447 ( ( $cust_main->payby eq 'BILL' ) && $cust_main->payinfo
448 ? " (P.O. #". $cust_main->payinfo. ")"
452 $FS::cust_bill::_template::address[$l++] = $cust_main->company
453 if $cust_main->company;
454 $FS::cust_bill::_template::address[$l++] = $cust_main->address1;
455 $FS::cust_bill::_template::address[$l++] = $cust_main->address2
456 if $cust_main->address2;
457 $FS::cust_bill::_template::address[$l++] =
458 $cust_main->city. ", ". $cust_main->state. " ". $cust_main->zip;
459 $FS::cust_bill::_template::address[$l++] = $cust_main->country
460 unless $cust_main->country eq 'US';
462 #overdue? (variable for the template)
463 $FS::cust_bill::_template::overdue = (
465 && $today > $self->_date
466 # && $self->printed > 1
467 && $self->printed > 0
470 #and subroutine for the template
472 sub FS::cust_bill::_template::invoice_lines {
475 scalar(@buf) ? shift @buf : [ '', '' ];
480 $FS::cust_bill::_template::page = 1;
484 push @collect, split("\n",
485 $invoice_template->fill_in( PACKAGE => 'FS::cust_bill::_template' )
487 $FS::cust_bill::_template::page++;
490 map "$_\n", @collect;
498 $Id: cust_bill.pm,v 1.14 2001-12-21 21:40:24 ivan Exp $
504 print_text formatting (and some logic :/) is in source, but needs to be
505 slurped in from a file. Also number of lines ($=).
507 missing print_ps for a nice postscript copy (maybe HylaFAX-cover-page-style
508 or something similar so the look can be completely customized?)
512 L<FS::Record>, L<FS::cust_main>, L<FS::cust_bill_pay>, L<FS:;cust_pay>,
513 L<FS::cust_bill_pkg>, L<FS::cust_bill_credit>, schema.html from the base