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;
15 @ISA = qw( FS::Record );
17 #ask FS::UID to run this stuff for us later
18 $FS::UID::callback{'FS::cust_bill'} = sub {
22 $money_char = $conf->config('money_char') || '$';
24 my @invoice_template = $conf->config('invoice_template')
25 or die "cannot load config file invoice_template";
27 foreach ( grep /invoice_lines\(\d+\)/, @invoice_template ) { #kludgy
28 /invoice_lines\((\d+)\)/;
31 die "no invoice_lines() functions in template?" unless $invoice_lines;
32 $invoice_template = new Text::Template (
34 SOURCE => [ map "$_\n", @invoice_template ],
35 ) or die "can't create new Text::Template object: $Text::Template::ERROR";
36 $invoice_template->compile()
37 or die "can't compile template: $Text::Template::ERROR";
42 FS::cust_bill - Object methods for cust_bill records
48 $record = new FS::cust_bill \%hash;
49 $record = new FS::cust_bill { 'column' => 'value' };
51 $error = $record->insert;
53 $error = $new_record->replace($old_record);
55 $error = $record->delete;
57 $error = $record->check;
59 ( $total_previous_balance, @previous_cust_bill ) = $record->previous;
61 @cust_bill_pkg_objects = $cust_bill->cust_bill_pkg;
63 ( $total_previous_credits, @previous_cust_credit ) = $record->cust_credit;
65 @cust_pay_objects = $cust_bill->cust_pay;
67 $tax_amount = $record->tax;
69 @lines = $cust_bill->print_text;
70 @lines = $cust_bill->print_text $time;
74 An FS::cust_bill object represents an invoice; a declaration that a customer
75 owes you money. The specific charges are itemized as B<cust_bill_pkg> records
76 (see L<FS::cust_bill_pkg>). FS::cust_bill inherits from FS::Record. The
77 following fields are currently supported:
81 =item invnum - primary key (assigned automatically for new invoices)
83 =item custnum - customer (see L<FS::cust_main>)
85 =item _date - specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
86 L<Time::Local> and L<Date::Parse> for conversion functions.
88 =item charged - amount of this invoice
90 =item printed - how many times this invoice has been printed automatically
91 (see L<FS::cust_main/"collect">).
101 Creates a new invoice. To add the invoice to the database, see L<"insert">.
102 Invoices are normally created by calling the bill method of a customer object
103 (see L<FS::cust_main>).
107 sub table { 'cust_bill'; }
111 Adds this invoice to the database ("Posts" the invoice). If there is an error,
112 returns the error, otherwise returns false.
116 Currently unimplemented. I don't remove invoices because there would then be
117 no record you ever posted this invoice (which is bad, no?)
122 return "Can't remove invoice!"
125 =item replace OLD_RECORD
127 Replaces the OLD_RECORD with this one in the database. If there is an error,
128 returns the error, otherwise returns false.
130 Only printed may be changed. printed is normally updated by calling the
131 collect method of a customer object (see L<FS::cust_main>).
136 my( $new, $old ) = ( shift, shift );
137 return "Can't change custnum!" unless $old->custnum == $new->custnum;
138 #return "Can't change _date!" unless $old->_date eq $new->_date;
139 return "Can't change _date!" unless $old->_date == $new->_date;
140 return "Can't change charged!" unless $old->charged == $new->charged;
142 $new->SUPER::replace($old);
147 Checks all fields to make sure this is a valid invoice. If there is an error,
148 returns the error, otherwise returns false. Called by the insert and replace
157 $self->ut_numbern('invnum')
158 || $self->ut_number('custnum')
159 || $self->ut_numbern('_date')
160 || $self->ut_money('charged')
161 || $self->ut_numbern('printed')
163 return $error if $error;
165 return "Unknown customer"
166 unless qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
168 $self->_date(time) unless $self->_date;
170 $self->printed(0) if $self->printed eq '';
177 Returns a list consisting of the total previous balance for this customer,
178 followed by the previous outstanding invoices (as FS::cust_bill objects also).
185 my @cust_bill = sort { $a->_date <=> $b->_date }
186 grep { $_->owed != 0 && $_->_date < $self->_date }
187 qsearch( 'cust_bill', { 'custnum' => $self->custnum } )
189 foreach ( @cust_bill ) { $total += $_->owed; }
195 Returns the line items (see L<FS::cust_bill_pkg>) for this invoice.
201 qsearch( 'cust_bill_pkg', { 'invnum' => $self->invnum } );
206 Returns a list consisting of the total previous credited (see
207 L<FS::cust_credit>) for this customer, followed by the previous outstanding
208 credits (FS::cust_credit objects).
215 my @cust_credit = sort { $a->_date <=> $b->_date }
216 grep { $_->credited != 0 && $_->_date < $self->_date }
217 qsearch('cust_credit', { 'custnum' => $self->custnum } )
219 foreach (@cust_credit) { $total += $_->credited; }
220 $total, @cust_credit;
225 Returns all payments (see L<FS::cust_pay>) for this invoice.
231 sort { $a->_date <=> $b->_date }
232 qsearch( 'cust_pay', { 'invnum' => $self->invnum } )
238 Returns the tax amount (see L<FS::cust_bill_pkg>) for this invoice.
245 my @taxlines = qsearch( 'cust_bill_pkg', { 'invnum' => $self->invnum ,
247 foreach (@taxlines) { $total += $_->setup; }
253 Returns the amount owed (still outstanding) on this invoice, which is charged
254 minus all payments (see L<FS::cust_pay>).
260 my $balance = $self->charged;
261 $balance -= $_->paid foreach ( $self->cust_pay );
265 =item print_text [TIME];
267 Returns an text invoice, as a list of lines.
269 TIME an optional value used to control the printing of overdue messages. The
270 default is now. It isn't the date of the invoice; that's the `_date' field.
271 It is specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
272 L<Time::Local> and L<Date::Parse> for conversion functions.
278 my( $self, $today ) = ( shift, shift );
280 # my $invnum = $self->invnum;
281 my $cust_main = qsearchs('cust_main', { 'custnum', $self->custnum } );
282 $cust_main->payname( $cust_main->first. ' '. $cust_main->getfield('last') )
283 unless $cust_main->payname;
285 my( $pr_total, @pr_cust_bill ) = $self->previous; #previous balance
286 my( $cr_total, @cr_cust_credit ) = $self->cust_credit; #credits
287 my $balance_due = $self->owed + $pr_total - $cr_total;
292 #my($description,$amount);
296 foreach ( @pr_cust_bill ) {
298 "Previous Balance, Invoice #". $_->invnum.
299 " (". time2str("%x",$_->_date). ")",
300 $money_char. sprintf("%10.2f",$_->owed)
304 push @buf,['','-----------'];
305 push @buf,[ 'Total Previous Balance',
306 $money_char. sprintf("%10.2f",$pr_total ) ];
311 foreach ( $self->cust_bill_pkg ) {
315 my($cust_pkg)=qsearchs('cust_pkg', { 'pkgnum', $_->pkgnum } );
316 my($part_pkg)=qsearchs('part_pkg',{'pkgpart'=>$cust_pkg->pkgpart});
317 my($pkg)=$part_pkg->pkg;
319 if ( $_->setup != 0 ) {
320 push @buf, [ "$pkg Setup", $money_char. sprintf("%10.2f",$_->setup) ];
322 map { [ " ". $_->[0]. ": ". $_->[1], '' ] } $cust_pkg->labels;
325 if ( $_->recur != 0 ) {
327 "$pkg (" . time2str("%x",$_->sdate) . " - " .
328 time2str("%x",$_->edate) . ")",
329 $money_char. sprintf("%10.2f",$_->recur)
332 map { [ " ". $_->[0]. ": ". $_->[1], '' ] } $cust_pkg->labels;
336 push @buf,["Tax", $money_char. sprintf("%10.2f",$_->setup) ]
341 push @buf,['','-----------'];
342 push @buf,['Total New Charges',
343 $money_char. sprintf("%10.2f",$self->charged) ];
346 push @buf,['','-----------'];
347 push @buf,['Total Charges',
348 $money_char. sprintf("%10.2f",$self->charged + $pr_total) ];
352 foreach ( @cr_cust_credit ) {
354 "Credit #". $_->crednum. " (" . time2str("%x",$_->_date) .")",
355 $money_char. sprintf("%10.2f",$_->credited)
359 #get & print payments
360 foreach ( $self->cust_pay ) {
362 "Payment received ". time2str("%x",$_->_date ),
363 $money_char. sprintf("%10.2f",$_->paid )
368 push @buf,['','-----------'];
369 push @buf,['Balance Due', $money_char.
370 sprintf("%10.2f",$self->owed + $pr_total - $cr_total ) ];
372 #setup template variables
374 package FS::cust_bill::_template; #!
375 use vars qw( $invnum $date $page $total_pages @address $overdue @buf );
377 $invnum = $self->invnum;
378 $date = $self->_date;
382 int( scalar(@FS::cust_bill::buf) / $FS::cust_bill::invoice_lines );
384 if scalar(@FS::cust_bill::buf) % $FS::cust_bill::invoice_lines;
387 #format address (variable for the template)
389 @address = ( '', '', '', '', '', '' );
390 package FS::cust_bill; #!
391 $FS::cust_bill::_template::address[$l++] =
393 ( ( $cust_main->payby eq 'BILL' ) && $cust_main->payinfo
394 ? " (P.O. #". $cust_main->payinfo. ")"
398 $FS::cust_bill::_template::address[$l++] = $cust_main->company
399 if $cust_main->company;
400 $FS::cust_bill::_template::address[$l++] = $cust_main->address1;
401 $FS::cust_bill::_template::address[$l++] = $cust_main->address2
402 if $cust_main->address2;
403 $FS::cust_bill::_template::address[$l++] =
404 $cust_main->city. ", ". $cust_main->state. " ". $cust_main->zip;
405 $FS::cust_bill::_template::address[$l++] = $cust_main->country
406 unless $cust_main->country eq 'US';
408 #overdue? (variable for the template)
409 $FS::cust_bill::_template::overdue = (
411 && $today > $self->_date
412 # && $self->printed > 1
413 && $self->printed > 0
416 #and subroutine for the template
418 sub FS::cust_bill::_template::invoice_lines {
421 scalar(@buf) ? shift @buf : [ '', '' ];
426 $FS::cust_bill::_template::page = 1;
430 push @collect, split("\n",
431 $invoice_template->fill_in( PACKAGE => 'FS::cust_bill::_template' )
433 $FS::cust_bill::_template::page++;
436 map "$_\n", @collect;
444 $Id: cust_bill.pm,v 1.8 2001-08-03 20:34:28 jeff Exp $
450 print_text formatting (and some logic :/) is in source, but needs to be
451 slurped in from a file. Also number of lines ($=).
453 missing print_ps for a nice postscript copy (maybe HylaFAX-cover-page-style
454 or something similar so the look can be completely customized?)
458 L<FS::Record>, L<FS::cust_main>, L<FS::cust_pay>, L<FS::cust_bill_pkg>,
459 L<FS::cust_credit>, schema.html from the base documentation.