4 use vars qw( @ISA $conf $money_char );
5 use vars qw( $lpr $invoice_from $smtpmachine );
6 use vars qw( $xaction $E_NoErr );
7 use vars qw( $bop_processor $bop_login $bop_password $bop_action @bop_options );
8 use vars qw( $ach_processor $ach_login $ach_password $ach_action @ach_options );
9 use vars qw( $invoice_lines @buf ); #yuck
10 use vars qw( $quiet );
12 use Mail::Internet 1.44;
15 use FS::UID qw( datasrc );
16 use FS::Record qw( qsearch qsearchs );
18 use FS::cust_bill_pkg;
22 use FS::cust_credit_bill;
23 use FS::cust_pay_batch;
24 use FS::cust_bill_event;
26 @ISA = qw( FS::Record );
28 #ask FS::UID to run this stuff for us later
29 $FS::UID::callback{'FS::cust_bill'} = sub {
33 $money_char = $conf->config('money_char') || '$';
35 $lpr = $conf->config('lpr');
36 $invoice_from = $conf->config('invoice_from');
37 $smtpmachine = $conf->config('smtpmachine');
39 ( $bop_processor,$bop_login, $bop_password, $bop_action ) = ( '', '', '', '');
41 ( $ach_processor,$ach_login, $ach_password, $ach_action ) = ( '', '', '', '');
44 if ( $conf->exists('business-onlinepayment') ) {
50 ) = $conf->config('business-onlinepayment');
51 $bop_action ||= 'normal authorization';
52 ( $ach_processor, $ach_login, $ach_password, $ach_action, @ach_options ) =
53 ( $bop_processor, $bop_login, $bop_password, $bop_action, @bop_options );
54 eval "use Business::OnlinePayment";
57 if ( $conf->exists('business-onlinepayment-ach') ) {
63 ) = $conf->config('business-onlinepayment-ach');
64 $ach_action ||= 'normal authorization';
65 eval "use Business::OnlinePayment";
72 FS::cust_bill - Object methods for cust_bill records
78 $record = new FS::cust_bill \%hash;
79 $record = new FS::cust_bill { 'column' => 'value' };
81 $error = $record->insert;
83 $error = $new_record->replace($old_record);
85 $error = $record->delete;
87 $error = $record->check;
89 ( $total_previous_balance, @previous_cust_bill ) = $record->previous;
91 @cust_bill_pkg_objects = $cust_bill->cust_bill_pkg;
93 ( $total_previous_credits, @previous_cust_credit ) = $record->cust_credit;
95 @cust_pay_objects = $cust_bill->cust_pay;
97 $tax_amount = $record->tax;
99 @lines = $cust_bill->print_text;
100 @lines = $cust_bill->print_text $time;
104 An FS::cust_bill object represents an invoice; a declaration that a customer
105 owes you money. The specific charges are itemized as B<cust_bill_pkg> records
106 (see L<FS::cust_bill_pkg>). FS::cust_bill inherits from FS::Record. The
107 following fields are currently supported:
111 =item invnum - primary key (assigned automatically for new invoices)
113 =item custnum - customer (see L<FS::cust_main>)
115 =item _date - specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
116 L<Time::Local> and L<Date::Parse> for conversion functions.
118 =item charged - amount of this invoice
120 =item printed - deprecated
122 =item closed - books closed flag, empty or `Y'
132 Creates a new invoice. To add the invoice to the database, see L<"insert">.
133 Invoices are normally created by calling the bill method of a customer object
134 (see L<FS::cust_main>).
138 sub table { 'cust_bill'; }
142 Adds this invoice to the database ("Posts" the invoice). If there is an error,
143 returns the error, otherwise returns false.
147 Currently unimplemented. I don't remove invoices because there would then be
148 no record you ever posted this invoice (which is bad, no?)
154 return "Can't delete closed invoice" if $self->closed =~ /^Y/i;
155 $self->SUPER::delete(@_);
158 =item replace OLD_RECORD
160 Replaces the OLD_RECORD with this one in the database. If there is an error,
161 returns the error, otherwise returns false.
163 Only printed may be changed. printed is normally updated by calling the
164 collect method of a customer object (see L<FS::cust_main>).
169 my( $new, $old ) = ( shift, shift );
170 return "Can't change custnum!" unless $old->custnum == $new->custnum;
171 #return "Can't change _date!" unless $old->_date eq $new->_date;
172 return "Can't change _date!" unless $old->_date == $new->_date;
173 return "Can't change charged!" unless $old->charged == $new->charged;
175 $new->SUPER::replace($old);
180 Checks all fields to make sure this is a valid invoice. If there is an error,
181 returns the error, otherwise returns false. Called by the insert and replace
190 $self->ut_numbern('invnum')
191 || $self->ut_number('custnum')
192 || $self->ut_numbern('_date')
193 || $self->ut_money('charged')
194 || $self->ut_numbern('printed')
195 || $self->ut_enum('closed', [ '', 'Y' ])
197 return $error if $error;
199 return "Unknown customer"
200 unless qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
202 $self->_date(time) unless $self->_date;
204 $self->printed(0) if $self->printed eq '';
211 Returns a list consisting of the total previous balance for this customer,
212 followed by the previous outstanding invoices (as FS::cust_bill objects also).
219 my @cust_bill = sort { $a->_date <=> $b->_date }
220 grep { $_->owed != 0 && $_->_date < $self->_date }
221 qsearch( 'cust_bill', { 'custnum' => $self->custnum } )
223 foreach ( @cust_bill ) { $total += $_->owed; }
229 Returns the line items (see L<FS::cust_bill_pkg>) for this invoice.
235 qsearch( 'cust_bill_pkg', { 'invnum' => $self->invnum } );
238 =item cust_bill_event
240 Returns the completed invoice events (see L<FS::cust_bill_event>) for this
245 sub cust_bill_event {
247 qsearch( 'cust_bill_event', { 'invnum' => $self->invnum } );
253 Returns the customer (see L<FS::cust_main>) for this invoice.
259 qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
264 Depreciated. See the cust_credited method.
266 #Returns a list consisting of the total previous credited (see
267 #L<FS::cust_credit>) and unapplied for this customer, followed by the previous
268 #outstanding credits (FS::cust_credit objects).
274 croak "FS::cust_bill->cust_credit depreciated; see ".
275 "FS::cust_bill->cust_credit_bill";
278 #my @cust_credit = sort { $a->_date <=> $b->_date }
279 # grep { $_->credited != 0 && $_->_date < $self->_date }
280 # qsearch('cust_credit', { 'custnum' => $self->custnum } )
282 #foreach (@cust_credit) { $total += $_->credited; }
283 #$total, @cust_credit;
288 Depreciated. See the cust_bill_pay method.
290 #Returns all payments (see L<FS::cust_pay>) for this invoice.
296 croak "FS::cust_bill->cust_pay depreciated; see FS::cust_bill->cust_bill_pay";
298 #sort { $a->_date <=> $b->_date }
299 # qsearch( 'cust_pay', { 'invnum' => $self->invnum } )
305 Returns all payment applications (see L<FS::cust_bill_pay>) for this invoice.
311 sort { $a->_date <=> $b->_date }
312 qsearch( 'cust_bill_pay', { 'invnum' => $self->invnum } );
317 Returns all applied credits (see L<FS::cust_credit_bill>) for this invoice.
323 sort { $a->_date <=> $b->_date }
324 qsearch( 'cust_credit_bill', { 'invnum' => $self->invnum } )
330 Returns the tax amount (see L<FS::cust_bill_pkg>) for this invoice.
337 my @taxlines = qsearch( 'cust_bill_pkg', { 'invnum' => $self->invnum ,
339 foreach (@taxlines) { $total += $_->setup; }
345 Returns the amount owed (still outstanding) on this invoice, which is charged
346 minus all payment applications (see L<FS::cust_bill_pay>) and credit
347 applications (see L<FS::cust_credit_bill>).
353 my $balance = $self->charged;
354 $balance -= $_->amount foreach ( $self->cust_bill_pay );
355 $balance -= $_->amount foreach ( $self->cust_credited );
356 $balance = sprintf( "%.2f", $balance);
357 $balance =~ s/^\-0\.00$/0.00/; #yay ieee fp
363 Sends this invoice to the destinations configured for this customer: send
364 emails or print. See L<FS::cust_main_invoice>.
369 my($self,$template) = @_;
370 my @print_text = $self->print_text('', $template);
371 my @invoicing_list = $self->cust_main->invoicing_list;
373 if ( grep { $_ ne 'POST' } @invoicing_list or !@invoicing_list ) { #email
375 #better to notify this person than silence
376 @invoicing_list = ($invoice_from) unless @invoicing_list;
378 #false laziness w/FS::cust_pay::delete & fs_signup_server && ::realtime_card
379 #$ENV{SMTPHOSTS} = $smtpmachine;
380 $ENV{MAILADDRESS} = $invoice_from;
381 my $header = new Mail::Header ( [
382 "From: $invoice_from",
383 "To: ". join(', ', grep { $_ ne 'POST' } @invoicing_list ),
384 "Sender: $invoice_from",
385 "Reply-To: $invoice_from",
386 "Date: ". time2str("%a, %d %b %Y %X %z", time),
389 my $message = new Mail::Internet (
391 'Body' => [ @print_text ], #( date)
394 $message->smtpsend( Host => $smtpmachine )
395 or $message->smtpsend( Host => $smtpmachine, Debug => 1 )
396 or return "(customer # ". $self->custnum. ") can't send invoice email".
397 " to ". join(', ', grep { $_ ne 'POST' } @invoicing_list ).
398 " via server $smtpmachine with SMTP: $!";
402 if ( grep { $_ eq 'POST' } @invoicing_list ) { #postal
404 or return "Can't open pipe to $lpr: $!";
405 print LPR @print_text;
407 or return $! ? "Error closing $lpr: $!"
408 : "Exit status $? from $lpr";
415 =item send_csv OPTIONS
417 Sends invoice as a CSV data-file to a remote host with the specified protocol.
421 protocol - currently only "ftp"
427 The file will be named "N-YYYYMMDDHHMMSS.csv" where N is the invoice number
428 and YYMMDDHHMMSS is a timestamp.
430 The fields of the CSV file is as follows:
432 record_type, invnum, custnum, _date, charged, first, last, company, address1, address2, city, state, zip, country, pkg, setup, recur, sdate, edate
436 =item record type - B<record_type> is either C<cust_bill> or C<cust_bill_pkg>
438 If B<record_type> is C<cust_bill>, this is a primary invoice record. The
439 last five fields (B<pkg> through B<edate>) are irrelevant, and all other
440 fields are filled in.
442 If B<record_type> is C<cust_bill_pkg>, this is a line item record. Only the
443 first two fields (B<record_type> and B<invnum>) and the last five fields
444 (B<pkg> through B<edate>) are filled in.
446 =item invnum - invoice number
448 =item custnum - customer number
450 =item _date - invoice date
452 =item charged - total invoice amount
454 =item first - customer first name
456 =item last - customer first name
458 =item company - company name
460 =item address1 - address line 1
462 =item address2 - address line 1
472 =item pkg - line item description
474 =item setup - line item setup fee (one or both of B<setup> and B<recur> will be defined)
476 =item recur - line item recurring fee (one or both of B<setup> and B<recur> will be defined)
478 =item sdate - start date for recurring fee
480 =item edate - end date for recurring fee
487 my($self, %opt) = @_;
489 #part one: create file
491 my $spooldir = "/usr/local/etc/freeside/export.". datasrc. "/cust_bill";
492 mkdir $spooldir, 0700 unless -d $spooldir;
494 my $file = $spooldir. '/'. $self->invnum. time2str('-%Y%m%d%H%M%S.csv', time);
496 open(CSV, ">$file") or die "can't open $file: $!";
498 eval "use Text::CSV_XS";
501 my $csv = Text::CSV_XS->new({'always_quote'=>1});
503 my $cust_main = $self->cust_main;
509 time2str("%x", $self->_date),
510 sprintf("%.2f", $self->charged),
511 ( map { $cust_main->getfield($_) }
512 qw( first last company address1 address2 city state zip country ) ),
514 ) or die "can't create csv";
515 print CSV $csv->string. "\n";
517 #new charges (false laziness w/print_text)
518 foreach my $cust_bill_pkg ( $self->cust_bill_pkg ) {
520 my($pkg, $setup, $recur, $sdate, $edate);
521 if ( $cust_bill_pkg->pkgnum ) {
523 ($pkg, $setup, $recur, $sdate, $edate) = (
524 $cust_bill_pkg->cust_pkg->part_pkg->pkg,
525 ( $cust_bill_pkg->setup != 0
526 ? sprintf("%.2f", $cust_bill_pkg->setup )
528 ( $cust_bill_pkg->recur != 0
529 ? sprintf("%.2f", $cust_bill_pkg->recur )
531 time2str("%x", $cust_bill_pkg->sdate),
532 time2str("%x", $cust_bill_pkg->edate),
536 next unless $cust_bill_pkg->setup != 0;
537 my $itemdesc = defined $cust_bill_pkg->dbdef_table->column('itemdesc')
538 ? ( $cust_bill_pkg->itemdesc || 'Tax' )
540 ($pkg, $setup, $recur, $sdate, $edate) =
541 ( $itemdesc, sprintf("%10.2f",$cust_bill_pkg->setup), '', '', '' );
547 ( map { '' } (1..11) ),
548 ($pkg, $setup, $recur, $sdate, $edate)
549 ) or die "can't create csv";
550 print CSV $csv->string. "\n";
554 close CSV or die "can't close CSV: $!";
559 if ( $opt{protocol} eq 'ftp' ) {
560 eval "use Net::FTP;";
562 $net = Net::FTP->new($opt{server}) or die @$;
564 die "unknown protocol: $opt{protocol}";
567 $net->login( $opt{username}, $opt{password} )
568 or die "can't FTP to $opt{username}\@$opt{server}: login error: $@";
570 $net->binary or die "can't set binary mode";
572 $net->cwd($opt{dir}) or die "can't cwd to $opt{dir}";
574 $net->put($file) or die "can't put $file: $!";
584 Pays this invoice with a compliemntary payment. If there is an error,
585 returns the error, otherwise returns false.
591 my $cust_pay = new FS::cust_pay ( {
592 'invnum' => $self->invnum,
593 'paid' => $self->owed,
596 'payinfo' => $self->cust_main->payinfo,
604 Attempts to pay this invoice with a credit card payment via a
605 Business::OnlinePayment realtime gateway. See
606 http://search.cpan.org/search?mode=module&query=Business%3A%3AOnlinePayment
607 for supported processors.
626 Attempts to pay this invoice with an electronic check (ACH) payment via a
627 Business::OnlinePayment realtime gateway. See
628 http://search.cpan.org/search?mode=module&query=Business%3A%3AOnlinePayment
629 for supported processors.
648 Attempts to pay this invoice with phone bill (LEC) payment via a
649 Business::OnlinePayment realtime gateway. See
650 http://search.cpan.org/search?mode=module&query=Business%3A%3AOnlinePayment
651 for supported processors.
669 my( $self, $method, $processor, $login, $password, $action, $options ) = @_;
670 my $cust_main = $self->cust_main;
671 my $amount = $self->owed;
673 my $address = $cust_main->address1;
674 $address .= ", ". $cust_main->address2 if $cust_main->address2;
676 my($payname, $payfirst, $paylast);
677 if ( $cust_main->payname && $method ne 'ECHECK' ) {
678 $payname = $cust_main->payname;
679 $payname =~ /^\s*([\w \,\.\-\']*)?\s+([\w\,\.\-\']+)\s*$/
681 #$dbh->rollback if $oldAutoCommit;
682 return "Illegal payname $payname";
684 ($payfirst, $paylast) = ($1, $2);
686 $payfirst = $cust_main->getfield('first');
687 $paylast = $cust_main->getfield('last');
688 $payname = "$payfirst $paylast";
691 my @invoicing_list = grep { $_ ne 'POST' } $cust_main->invoicing_list;
692 if ( $conf->exists('emailinvoiceauto')
693 || ( $conf->exists('emailinvoiceonly') && ! @invoicing_list ) ) {
694 push @invoicing_list, $cust_main->all_emails;
696 my $email = $invoicing_list[0];
698 my( $action1, $action2 ) = split(/\s*\,\s*/, $action );
700 my $description = 'Internet Services';
701 if ( $conf->exists('business-onlinepayment-description') ) {
702 my $dtempl = $conf->config('business-onlinepayment-description');
704 my $agent_obj = $cust_main->agent
705 or die "can't retreive agent for $cust_main (agentnum ".
706 $cust_main->agentnum. ")";
707 my $agent = $agent_obj->agent;
708 my $pkgs = join(', ',
709 map { $_->cust_pkg->part_pkg->pkg }
710 grep { $_->pkgnum } $self->cust_bill_pkg
712 $description = eval qq("$dtempl");
717 if ( $method eq 'CC' ) {
718 $content{card_number} = $cust_main->payinfo;
719 $cust_main->paydate =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/;
720 $content{expiration} = "$2/$1";
721 } elsif ( $method eq 'ECHECK' ) {
722 my($account_number,$routing_code) = $cust_main->payinfo;
723 ( $content{account_number}, $content{routing_code} ) =
724 split('@', $cust_main->payinfo);
725 $content{bank_name} = $cust_main->payname;
726 } elsif ( $method eq 'LEC' ) {
727 $content{phone} = $cust_main->payinfo;
731 new Business::OnlinePayment( $processor, @$options );
732 $transaction->content(
735 'password' => $password,
736 'action' => $action1,
737 'description' => $description,
739 'invoice_number' => $self->invnum,
740 'customer_id' => $self->custnum,
741 'last_name' => $paylast,
742 'first_name' => $payfirst,
744 'address' => $address,
745 'city' => $cust_main->city,
746 'state' => $cust_main->state,
747 'zip' => $cust_main->zip,
748 'country' => $cust_main->country,
749 'referer' => 'http://cleanwhisker.420.am/',
751 'phone' => $cust_main->daytime || $cust_main->night,
754 $transaction->submit();
756 if ( $transaction->is_success() && $action2 ) {
757 my $auth = $transaction->authorization;
758 my $ordernum = $transaction->can('order_number')
759 ? $transaction->order_number
762 #warn "********* $auth ***********\n";
763 #warn "********* $ordernum ***********\n";
765 new Business::OnlinePayment( $processor, @$options );
772 password => $password,
773 order_number => $ordernum,
775 authorization => $auth,
776 description => $description,
779 foreach my $field (qw( authorization_source_code returned_ACI transaction_identifier validation_code
780 transaction_sequence_num local_transaction_date
781 local_transaction_time AVS_result_code )) {
782 $capture{$field} = $transaction->$field() if $transaction->can($field);
785 $capture->content( %capture );
789 unless ( $capture->is_success ) {
790 my $e = "Authorization sucessful but capture failed, invnum #".
791 $self->invnum. ': '. $capture->result_code.
792 ": ". $capture->error_message;
799 if ( $transaction->is_success() ) {
807 my $cust_pay = new FS::cust_pay ( {
808 'invnum' => $self->invnum,
811 'payby' => method2payby{$method},
812 'payinfo' => $cust_main->payinfo,
813 'paybatch' => "$processor:". $transaction->authorization,
815 my $error = $cust_pay->insert;
817 # gah, even with transactions.
818 my $e = 'WARNING: Card/ACH debited but database not updated - '.
819 'error applying payment, invnum #' . $self->invnum.
820 " ($processor): $error";
826 #} elsif ( $options{'report_badcard'} ) {
829 my $perror = "$processor error, invnum #". $self->invnum. ': '.
830 $transaction->result_code. ": ". $transaction->error_message;
832 if ( !$quiet && $conf->exists('emaildecline')
833 && grep { $_ ne 'POST' } $cust_main->invoicing_list
835 my @templ = $conf->config('declinetemplate');
836 my $template = new Text::Template (
838 SOURCE => [ map "$_\n", @templ ],
839 ) or return "($perror) can't create template: $Text::Template::ERROR";
841 or return "($perror) can't compile template: $Text::Template::ERROR";
843 my $templ_hash = { error => $transaction->error_message };
845 #false laziness w/FS::cust_pay::delete & fs_signup_server && ::send
846 $ENV{MAILADDRESS} = $invoice_from;
847 my $header = new Mail::Header ( [
848 "From: $invoice_from",
849 "To: ". join(', ', grep { $_ ne 'POST' } $cust_main->invoicing_list ),
850 "Sender: $invoice_from",
851 "Reply-To: $invoice_from",
852 "Date: ". time2str("%a, %d %b %Y %X %z", time),
853 "Subject: Your payment could not be processed",
855 my $message = new Mail::Internet (
857 'Body' => [ $template->fill_in(HASH => $templ_hash) ],
860 $message->smtpsend( Host => $smtpmachine )
861 or $message->smtpsend( Host => $smtpmachine, Debug => 1 )
862 or return "($perror) (customer # ". $self->custnum.
863 ") can't send card decline email to ".
864 join(', ', grep { $_ ne 'POST' } $cust_main->invoicing_list ).
865 " via server $smtpmachine with SMTP: $!";
875 Adds a payment for this invoice to the pending credit card batch (see
876 L<FS::cust_pay_batch>).
882 my $cust_main = $self->cust_main;
884 my $cust_pay_batch = new FS::cust_pay_batch ( {
885 'invnum' => $self->getfield('invnum'),
886 'custnum' => $cust_main->getfield('custnum'),
887 'last' => $cust_main->getfield('last'),
888 'first' => $cust_main->getfield('first'),
889 'address1' => $cust_main->getfield('address1'),
890 'address2' => $cust_main->getfield('address2'),
891 'city' => $cust_main->getfield('city'),
892 'state' => $cust_main->getfield('state'),
893 'zip' => $cust_main->getfield('zip'),
894 'country' => $cust_main->getfield('country'),
896 'cardnum' => $cust_main->getfield('payinfo'),
897 'exp' => $cust_main->getfield('paydate'),
898 'payname' => $cust_main->getfield('payname'),
899 'amount' => $self->owed,
901 my $error = $cust_pay_batch->insert;
902 die $error if $error;
907 =item print_text [TIME];
909 Returns an text invoice, as a list of lines.
911 TIME an optional value used to control the printing of overdue messages. The
912 default is now. It isn't the date of the invoice; that's the `_date' field.
913 It is specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
914 L<Time::Local> and L<Date::Parse> for conversion functions.
920 my( $self, $today, $template ) = @_;
922 # my $invnum = $self->invnum;
923 my $cust_main = qsearchs('cust_main', { 'custnum', $self->custnum } );
924 $cust_main->payname( $cust_main->first. ' '. $cust_main->getfield('last') )
925 unless $cust_main->payname && $cust_main->payby ne 'CHEK';
927 my( $pr_total, @pr_cust_bill ) = $self->previous; #previous balance
928 # my( $cr_total, @cr_cust_credit ) = $self->cust_credit; #credits
929 #my $balance_due = $self->owed + $pr_total - $cr_total;
930 my $balance_due = $self->owed + $pr_total;
933 #my($description,$amount);
937 foreach ( @pr_cust_bill ) {
939 "Previous Balance, Invoice #". $_->invnum.
940 " (". time2str("%x",$_->_date). ")",
941 $money_char. sprintf("%10.2f",$_->owed)
945 push @buf,['','-----------'];
946 push @buf,[ 'Total Previous Balance',
947 $money_char. sprintf("%10.2f",$pr_total ) ];
952 foreach ( ( grep { $_->pkgnum } $self->cust_bill_pkg ), #packages first
953 ( grep { ! $_->pkgnum } $self->cust_bill_pkg ), #then taxes
958 my($cust_pkg)=qsearchs('cust_pkg', { 'pkgnum', $_->pkgnum } );
959 my($part_pkg)=qsearchs('part_pkg',{'pkgpart'=>$cust_pkg->pkgpart});
960 my($pkg)=$part_pkg->pkg;
962 if ( $_->setup != 0 ) {
963 push @buf, [ "$pkg Setup", $money_char. sprintf("%10.2f",$_->setup) ];
965 map { [ " ". $_->[0]. ": ". $_->[1], '' ] } $cust_pkg->labels;
968 if ( $_->recur != 0 ) {
970 "$pkg (" . time2str("%x",$_->sdate) . " - " .
971 time2str("%x",$_->edate) . ")",
972 $money_char. sprintf("%10.2f",$_->recur)
975 map { [ " ". $_->[0]. ": ". $_->[1], '' ] } $cust_pkg->labels;
979 my $itemdesc = defined $_->dbdef_table->column('itemdesc')
980 ? ( $_->itemdesc || 'Tax' )
982 push @buf,[$itemdesc, $money_char. sprintf("%10.2f",$_->setup) ]
987 push @buf,['','-----------'];
988 push @buf,['Total New Charges',
989 $money_char. sprintf("%10.2f",$self->charged) ];
992 push @buf,['','-----------'];
993 push @buf,['Total Charges',
994 $money_char. sprintf("%10.2f",$self->charged + $pr_total) ];
998 foreach ( $self->cust_credited ) {
1000 #something more elaborate if $_->amount ne $_->cust_credit->credited ?
1002 my $reason = substr($_->cust_credit->reason,0,32);
1003 $reason .= '...' if length($reason) < length($_->cust_credit->reason);
1004 $reason = " ($reason) " if $reason;
1006 "Credit #". $_->crednum. " (". time2str("%x",$_->cust_credit->_date) .")".
1008 $money_char. sprintf("%10.2f",$_->amount)
1011 #foreach ( @cr_cust_credit ) {
1013 # "Credit #". $_->crednum. " (" . time2str("%x",$_->_date) .")",
1014 # $money_char. sprintf("%10.2f",$_->credited)
1018 #get & print payments
1019 foreach ( $self->cust_bill_pay ) {
1021 #something more elaborate if $_->amount ne ->cust_pay->paid ?
1024 "Payment received ". time2str("%x",$_->cust_pay->_date ),
1025 $money_char. sprintf("%10.2f",$_->amount )
1030 push @buf,['','-----------'];
1031 push @buf,['Balance Due', $money_char.
1032 sprintf("%10.2f", $balance_due ) ];
1034 #create the template
1035 my $templatefile = 'invoice_template';
1036 $templatefile .= "_$template" if $template;
1037 my @invoice_template = $conf->config($templatefile)
1038 or die "cannot load config file $templatefile";
1041 foreach ( grep /invoice_lines\(\d*\)/, @invoice_template ) { #kludgy
1042 /invoice_lines\((\d*)\)/;
1043 $invoice_lines += $1 || scalar(@buf);
1046 die "no invoice_lines() functions in template?" unless $wasfunc;
1047 my $invoice_template = new Text::Template (
1049 SOURCE => [ map "$_\n", @invoice_template ],
1050 ) or die "can't create new Text::Template object: $Text::Template::ERROR";
1051 $invoice_template->compile()
1052 or die "can't compile template: $Text::Template::ERROR";
1054 #setup template variables
1055 package FS::cust_bill::_template; #!
1056 use vars qw( $invnum $date $page $total_pages @address $overdue @buf $agent );
1058 $invnum = $self->invnum;
1059 $date = $self->_date;
1061 $agent = $self->cust_main->agent->agent;
1063 if ( $FS::cust_bill::invoice_lines ) {
1065 int( scalar(@FS::cust_bill::buf) / $FS::cust_bill::invoice_lines );
1067 if scalar(@FS::cust_bill::buf) % $FS::cust_bill::invoice_lines;
1072 #format address (variable for the template)
1074 @address = ( '', '', '', '', '', '' );
1075 package FS::cust_bill; #!
1076 $FS::cust_bill::_template::address[$l++] =
1077 $cust_main->payname.
1078 ( ( $cust_main->payby eq 'BILL' ) && $cust_main->payinfo
1079 ? " (P.O. #". $cust_main->payinfo. ")"
1083 $FS::cust_bill::_template::address[$l++] = $cust_main->company
1084 if $cust_main->company;
1085 $FS::cust_bill::_template::address[$l++] = $cust_main->address1;
1086 $FS::cust_bill::_template::address[$l++] = $cust_main->address2
1087 if $cust_main->address2;
1088 $FS::cust_bill::_template::address[$l++] =
1089 $cust_main->city. ", ". $cust_main->state. " ". $cust_main->zip;
1090 $FS::cust_bill::_template::address[$l++] = $cust_main->country
1091 unless $cust_main->country eq 'US';
1093 # #overdue? (variable for the template)
1094 # $FS::cust_bill::_template::overdue = (
1096 # && $today > $self->_date
1097 ## && $self->printed > 1
1098 # && $self->printed > 0
1101 #and subroutine for the template
1102 sub FS::cust_bill::_template::invoice_lines {
1103 my $lines = shift || scalar(@buf);
1105 scalar(@buf) ? shift @buf : [ '', '' ];
1111 $FS::cust_bill::_template::page = 1;
1115 push @collect, split("\n",
1116 $invoice_template->fill_in( PACKAGE => 'FS::cust_bill::_template' )
1118 $FS::cust_bill::_template::page++;
1121 map "$_\n", @collect;
1129 $Id: cust_bill.pm,v 1.60 2002-12-28 09:16:49 ivan Exp $
1135 print_text formatting (and some logic :/) is in source, but needs to be
1136 slurped in from a file. Also number of lines ($=).
1138 missing print_ps for a nice postscript copy (maybe HylaFAX-cover-page-style
1139 or something similar so the look can be completely customized?)
1143 L<FS::Record>, L<FS::cust_main>, L<FS::cust_bill_pay>, L<FS::cust_pay>,
1144 L<FS::cust_bill_pkg>, L<FS::cust_bill_credit>, schema.html from the base