4 use vars qw( @ISA $conf $invoice_template $money_char );
5 use vars qw( $lpr $invoice_from $smtpmachine );
6 use vars qw( $processor );
7 use vars qw( $xaction $E_NoErr );
8 use vars qw( $bop_processor $bop_login $bop_password $bop_action @bop_options );
9 use vars qw( $invoice_lines @buf ); #yuck
14 use FS::Record qw( qsearch qsearchs );
16 use FS::cust_bill_pkg;
20 use FS::cust_credit_bill;
21 use FS::cust_pay_batch;
22 use FS::cust_bill_event;
24 @ISA = qw( FS::Record );
26 #ask FS::UID to run this stuff for us later
27 $FS::UID::callback{'FS::cust_bill'} = sub {
31 $money_char = $conf->config('money_char') || '$';
33 my @invoice_template = $conf->config('invoice_template')
34 or die "cannot load config file invoice_template";
36 foreach ( grep /invoice_lines\(\d+\)/, @invoice_template ) { #kludgy
37 /invoice_lines\((\d+)\)/;
40 die "no invoice_lines() functions in template?" unless $invoice_lines;
41 $invoice_template = new Text::Template (
43 SOURCE => [ map "$_\n", @invoice_template ],
44 ) or die "can't create new Text::Template object: $Text::Template::ERROR";
45 $invoice_template->compile()
46 or die "can't compile template: $Text::Template::ERROR";
48 $lpr = $conf->config('lpr');
49 $invoice_from = $conf->config('invoice_from');
50 $smtpmachine = $conf->config('smtpmachine');
52 if ( $conf->exists('cybercash3.2') ) {
54 #qw($MCKversion %Config InitConfig CCError CCDebug CCDebug2);
55 require CCMckDirectLib3_2;
57 require CCMckErrno3_2;
58 #qw(MCKGetErrorMessage $E_NoErr);
59 import CCMckErrno3_2 qw($E_NoErr);
62 ($merchant_conf,$xaction)= $conf->config('cybercash3.2');
63 my $status = &CCMckLib3_2::InitConfig($merchant_conf);
64 if ( $status != $E_NoErr ) {
65 warn "CCMckLib3_2::InitConfig error:\n";
66 foreach my $key (keys %CCMckLib3_2::Config) {
67 warn " $key => $CCMckLib3_2::Config{$key}\n"
69 my($errmsg) = &CCMckErrno3_2::MCKGetErrorMessage($status);
70 die "CCMckLib3_2::InitConfig fatal error: $errmsg\n";
72 $processor='cybercash3.2';
73 } elsif ( $conf->exists('business-onlinepayment') ) {
79 ) = $conf->config('business-onlinepayment');
80 $bop_action ||= 'normal authorization';
81 eval "use Business::OnlinePayment";
82 $processor="Business::OnlinePayment::$bop_processor";
89 FS::cust_bill - Object methods for cust_bill records
95 $record = new FS::cust_bill \%hash;
96 $record = new FS::cust_bill { 'column' => 'value' };
98 $error = $record->insert;
100 $error = $new_record->replace($old_record);
102 $error = $record->delete;
104 $error = $record->check;
106 ( $total_previous_balance, @previous_cust_bill ) = $record->previous;
108 @cust_bill_pkg_objects = $cust_bill->cust_bill_pkg;
110 ( $total_previous_credits, @previous_cust_credit ) = $record->cust_credit;
112 @cust_pay_objects = $cust_bill->cust_pay;
114 $tax_amount = $record->tax;
116 @lines = $cust_bill->print_text;
117 @lines = $cust_bill->print_text $time;
121 An FS::cust_bill object represents an invoice; a declaration that a customer
122 owes you money. The specific charges are itemized as B<cust_bill_pkg> records
123 (see L<FS::cust_bill_pkg>). FS::cust_bill inherits from FS::Record. The
124 following fields are currently supported:
128 =item invnum - primary key (assigned automatically for new invoices)
130 =item custnum - customer (see L<FS::cust_main>)
132 =item _date - specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
133 L<Time::Local> and L<Date::Parse> for conversion functions.
135 =item charged - amount of this invoice
137 =item printed - deprecated
139 =item closed - books closed flag, empty or `Y'
149 Creates a new invoice. To add the invoice to the database, see L<"insert">.
150 Invoices are normally created by calling the bill method of a customer object
151 (see L<FS::cust_main>).
155 sub table { 'cust_bill'; }
159 Adds this invoice to the database ("Posts" the invoice). If there is an error,
160 returns the error, otherwise returns false.
164 Currently unimplemented. I don't remove invoices because there would then be
165 no record you ever posted this invoice (which is bad, no?)
171 return "Can't delete closed invoice" if $self->closed =~ /^Y/i;
172 $self->SUPER::delete(@_);
175 =item replace OLD_RECORD
177 Replaces the OLD_RECORD with this one in the database. If there is an error,
178 returns the error, otherwise returns false.
180 Only printed may be changed. printed is normally updated by calling the
181 collect method of a customer object (see L<FS::cust_main>).
186 my( $new, $old ) = ( shift, shift );
187 return "Can't change custnum!" unless $old->custnum == $new->custnum;
188 #return "Can't change _date!" unless $old->_date eq $new->_date;
189 return "Can't change _date!" unless $old->_date == $new->_date;
190 return "Can't change charged!" unless $old->charged == $new->charged;
192 $new->SUPER::replace($old);
197 Checks all fields to make sure this is a valid invoice. If there is an error,
198 returns the error, otherwise returns false. Called by the insert and replace
207 $self->ut_numbern('invnum')
208 || $self->ut_number('custnum')
209 || $self->ut_numbern('_date')
210 || $self->ut_money('charged')
211 || $self->ut_numbern('printed')
212 || $self->ut_enum('closed', [ '', 'Y' ])
214 return $error if $error;
216 return "Unknown customer"
217 unless qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
219 $self->_date(time) unless $self->_date;
221 $self->printed(0) if $self->printed eq '';
228 Returns a list consisting of the total previous balance for this customer,
229 followed by the previous outstanding invoices (as FS::cust_bill objects also).
236 my @cust_bill = sort { $a->_date <=> $b->_date }
237 grep { $_->owed != 0 && $_->_date < $self->_date }
238 qsearch( 'cust_bill', { 'custnum' => $self->custnum } )
240 foreach ( @cust_bill ) { $total += $_->owed; }
246 Returns the line items (see L<FS::cust_bill_pkg>) for this invoice.
252 qsearch( 'cust_bill_pkg', { 'invnum' => $self->invnum } );
255 =item cust_bill_event
257 Returns the completed invoice events (see L<FS::cust_bill_event>) for this
262 sub cust_bill_event {
264 qsearch( 'cust_bill_event', { 'invnum' => $self->invnum } );
270 Returns the customer (see L<FS::cust_main>) for this invoice.
276 qsearchs( 'cust_main', { 'custnum' => $self->custnum } );
281 Depreciated. See the cust_credited method.
283 #Returns a list consisting of the total previous credited (see
284 #L<FS::cust_credit>) and unapplied for this customer, followed by the previous
285 #outstanding credits (FS::cust_credit objects).
291 croak "FS::cust_bill->cust_credit depreciated; see ".
292 "FS::cust_bill->cust_credit_bill";
295 #my @cust_credit = sort { $a->_date <=> $b->_date }
296 # grep { $_->credited != 0 && $_->_date < $self->_date }
297 # qsearch('cust_credit', { 'custnum' => $self->custnum } )
299 #foreach (@cust_credit) { $total += $_->credited; }
300 #$total, @cust_credit;
305 Depreciated. See the cust_bill_pay method.
307 #Returns all payments (see L<FS::cust_pay>) for this invoice.
313 croak "FS::cust_bill->cust_pay depreciated; see FS::cust_bill->cust_bill_pay";
315 #sort { $a->_date <=> $b->_date }
316 # qsearch( 'cust_pay', { 'invnum' => $self->invnum } )
322 Returns all payment applications (see L<FS::cust_bill_pay>) for this invoice.
328 sort { $a->_date <=> $b->_date }
329 qsearch( 'cust_bill_pay', { 'invnum' => $self->invnum } );
334 Returns all applied credits (see L<FS::cust_credit_bill>) for this invoice.
340 sort { $a->_date <=> $b->_date }
341 qsearch( 'cust_credit_bill', { 'invnum' => $self->invnum } )
347 Returns the tax amount (see L<FS::cust_bill_pkg>) for this invoice.
354 my @taxlines = qsearch( 'cust_bill_pkg', { 'invnum' => $self->invnum ,
356 foreach (@taxlines) { $total += $_->setup; }
362 Returns the amount owed (still outstanding) on this invoice, which is charged
363 minus all payment applications (see L<FS::cust_bill_pay>) and credit
364 applications (see L<FS::cust_credit_bill>).
370 my $balance = $self->charged;
371 $balance -= $_->amount foreach ( $self->cust_bill_pay );
372 $balance -= $_->amount foreach ( $self->cust_credited );
373 $balance = sprintf( "%.2f", $balance);
374 $balance =~ s/^\-0\.00$/0.00/; #yay ieee fp
380 Sends this invoice to the destinations configured for this customer: send
381 emails or print. See L<FS::cust_main_invoice>.
388 #my @print_text = $cust_bill->print_text; #( date )
389 my @invoicing_list = $self->cust_main->invoicing_list;
390 if ( grep { $_ ne 'POST' } @invoicing_list ) { #email invoice
391 #$ENV{SMTPHOSTS} = $smtpmachine;
392 $ENV{MAILADDRESS} = $invoice_from;
393 my $header = new Mail::Header ( [
394 "From: $invoice_from",
395 "To: ". join(', ', grep { $_ ne 'POST' } @invoicing_list ),
396 "Sender: $invoice_from",
397 "Reply-To: $invoice_from",
398 "Date: ". time2str("%a, %d %b %Y %X %z", time),
401 my $message = new Mail::Internet (
403 'Body' => [ $self->print_text ], #( date)
406 $message->smtpsend( Host => $smtpmachine )
407 or $message->smtpsend( Host => $smtpmachine, Debug => 1 )
408 or return "(customer # ". $self->custnum. ") can't send invoice email".
409 " to ". join(', ', grep { $_ ne 'POST' } @invoicing_list ).
410 " via server $smtpmachine with SMTP: $!";
412 #} elsif ( grep { $_ eq 'POST' } @invoicing_list ) {
413 } elsif ( ! @invoicing_list || grep { $_ eq 'POST' } @invoicing_list ) {
415 or return "Can't open pipe to $lpr: $!";
416 print LPR $self->print_text; #( date )
418 or return $! ? "Error closing $lpr: $!"
419 : "Exit status $? from $lpr";
428 Pays this invoice with a compliemntary payment. If there is an error,
429 returns the error, otherwise returns false.
435 my $cust_pay = new FS::cust_pay ( {
436 'invnum' => $self->invnum,
437 'paid' => $self->owed,
440 'payinfo' => $self->cust_main->payinfo,
448 Attempts to pay this invoice with a Business::OnlinePayment realtime gateway.
449 See http://search.cpan.org/search?mode=module&query=Business%3A%3AOnlinePayment
450 for supproted processors.
456 my $cust_main = $self->cust_main;
457 my $amount = $self->owed;
459 unless ( $processor =~ /^Business::OnlinePayment::(.*)$/ ) {
460 return "Real-time card processing not enabled (processor $processor)";
462 my $bop_processor = $1; #hmm?
464 my $address = $cust_main->address1;
465 $address .= ", ". $cust_main->address2 if $cust_main->address2;
468 #$cust_main->paydate =~ /^(\d+)\/\d*(\d{2})$/;
469 $cust_main->paydate =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/;
472 my($payname, $payfirst, $paylast);
473 if ( $cust_main->payname ) {
474 $payname = $cust_main->payname;
475 $payname =~ /^\s*([\w \,\.\-\']*)?\s+([\w\,\.\-\']+)$/
477 #$dbh->rollback if $oldAutoCommit;
478 return "Illegal payname $payname";
480 ($payfirst, $paylast) = ($1, $2);
482 $payfirst = $cust_main->getfield('first');
483 $paylast = $cust_main->getfield('first');
484 $payname = "$payfirst $paylast";
487 my @invoicing_list = grep { $_ ne 'POST' } $cust_main->invoicing_list;
488 if ( $conf->exists('emailinvoiceauto')
489 || ( $conf->exists('emailinvoiceonly') && ! @invoicing_list ) ) {
490 push @invoicing_list, $cust_main->default_invoicing_list;
492 my $email = $invoicing_list[0];
494 my( $action1, $action2 ) = split(/\s*\,\s*/, $bop_action );
497 new Business::OnlinePayment( $bop_processor, @bop_options );
498 $transaction->content(
500 'login' => $bop_login,
501 'password' => $bop_password,
502 'action' => $action1,
503 'description' => 'Internet Services',
505 'invoice_number' => $self->invnum,
506 'customer_id' => $self->custnum,
507 'last_name' => $paylast,
508 'first_name' => $payfirst,
510 'address' => $address,
511 'city' => $cust_main->city,
512 'state' => $cust_main->state,
513 'zip' => $cust_main->zip,
514 'country' => $cust_main->country,
515 'card_number' => $cust_main->payinfo,
516 'expiration' => $exp,
517 'referer' => 'http://cleanwhisker.420.am/',
520 $transaction->submit();
522 if ( $transaction->is_success() && $action2 ) {
523 my $auth = $transaction->authorization;
524 my $ordernum = $transaction->order_number;
525 #warn "********* $auth ***********\n";
526 #warn "********* $ordernum ***********\n";
528 new Business::OnlinePayment( $bop_processor, @bop_options );
533 password => $bop_password,
534 order_number => $ordernum,
536 authorization => $auth,
537 description => 'Internet Services',
542 unless ( $capture->is_success ) {
543 my $e = "Authorization sucessful but capture failed, invnum #".
544 $self->invnum. ': '. $capture->result_code.
545 ": ". $capture->error_message;
552 if ( $transaction->is_success() ) {
554 my $cust_pay = new FS::cust_pay ( {
555 'invnum' => $self->invnum,
559 'payinfo' => $cust_main->payinfo,
560 'paybatch' => "$processor:". $transaction->authorization,
562 my $error = $cust_pay->insert;
564 # gah, even with transactions.
565 my $e = 'WARNING: Card debited but database not updated - '.
566 'error applying payment, invnum #' . $self->invnum.
567 " ($processor): $error";
573 #} elsif ( $options{'report_badcard'} ) {
575 return "$processor error, invnum #". $self->invnum. ': '.
576 $transaction->result_code. ": ". $transaction->error_message;
581 =item realtime_card_cybercash
583 Attempts to pay this invoice with the CyberCash CashRegister realtime gateway.
587 sub realtime_card_cybercash {
589 my $cust_main = $self->cust_main;
590 my $amount = $self->owed;
592 return "CyberCash CashRegister real-time card processing not enabled!"
593 unless $processor eq 'cybercash3.2';
595 my $address = $cust_main->address1;
596 $address .= ", ". $cust_main->address2 if $cust_main->address2;
599 #$cust_main->paydate =~ /^(\d+)\/\d*(\d{2})$/;
600 $cust_main->paydate =~ /^\d{2}(\d{2})[\/\-](\d+)[\/\-]\d+$/;
605 my $paybatch = $self->invnum.
606 '-' . time2str("%y%m%d%H%M%S", time);
608 my $payname = $cust_main->payname ||
609 $cust_main->getfield('first').' '.$cust_main->getfield('last');
611 my $country = $cust_main->country eq 'US' ? 'USA' : $cust_main->country;
613 my @full_xaction = ( $xaction,
614 'Order-ID' => $paybatch,
615 'Amount' => "usd $amount",
616 'Card-Number' => $cust_main->getfield('payinfo'),
617 'Card-Name' => $payname,
618 'Card-Address' => $address,
619 'Card-City' => $cust_main->getfield('city'),
620 'Card-State' => $cust_main->getfield('state'),
621 'Card-Zip' => $cust_main->getfield('zip'),
622 'Card-Country' => $country,
627 %result = &CCMckDirectLib3_2::SendCC2_1Server(@full_xaction);
629 if ( $result{'MStatus'} eq 'success' ) { #cybercash smps v.2 or 3
630 my $cust_pay = new FS::cust_pay ( {
631 'invnum' => $self->invnum,
635 'payinfo' => $cust_main->payinfo,
636 'paybatch' => "$processor:$paybatch",
638 my $error = $cust_pay->insert;
640 # gah, even with transactions.
641 my $e = 'WARNING: Card debited but database not updated - '.
642 'error applying payment, invnum #' . $self->invnum.
643 " (CyberCash Order-ID $paybatch): $error";
649 # } elsif ( $result{'Mstatus'} ne 'failure-bad-money'
650 # || $options{'report_badcard'}
653 return 'Cybercash error, invnum #' .
654 $self->invnum. ':'. $result{'MErrMsg'};
661 Adds a payment for this invoice to the pending credit card batch (see
662 L<FS::cust_pay_batch>).
668 my $cust_main = $self->cust_main;
670 my $cust_pay_batch = new FS::cust_pay_batch ( {
671 'invnum' => $self->getfield('invnum'),
672 'custnum' => $cust_main->getfield('custnum'),
673 'last' => $cust_main->getfield('last'),
674 'first' => $cust_main->getfield('first'),
675 'address1' => $cust_main->getfield('address1'),
676 'address2' => $cust_main->getfield('address2'),
677 'city' => $cust_main->getfield('city'),
678 'state' => $cust_main->getfield('state'),
679 'zip' => $cust_main->getfield('zip'),
680 'country' => $cust_main->getfield('country'),
682 'cardnum' => $cust_main->getfield('payinfo'),
683 'exp' => $cust_main->getfield('paydate'),
684 'payname' => $cust_main->getfield('payname'),
685 'amount' => $self->owed,
687 $cust_pay_batch->insert;
691 =item print_text [TIME];
693 Returns an text invoice, as a list of lines.
695 TIME an optional value used to control the printing of overdue messages. The
696 default is now. It isn't the date of the invoice; that's the `_date' field.
697 It is specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
698 L<Time::Local> and L<Date::Parse> for conversion functions.
704 my( $self, $today ) = ( shift, shift );
706 # my $invnum = $self->invnum;
707 my $cust_main = qsearchs('cust_main', { 'custnum', $self->custnum } );
708 $cust_main->payname( $cust_main->first. ' '. $cust_main->getfield('last') )
709 unless $cust_main->payname;
711 my( $pr_total, @pr_cust_bill ) = $self->previous; #previous balance
712 # my( $cr_total, @cr_cust_credit ) = $self->cust_credit; #credits
713 #my $balance_due = $self->owed + $pr_total - $cr_total;
714 my $balance_due = $self->owed + $pr_total;
717 #my($description,$amount);
721 foreach ( @pr_cust_bill ) {
723 "Previous Balance, Invoice #". $_->invnum.
724 " (". time2str("%x",$_->_date). ")",
725 $money_char. sprintf("%10.2f",$_->owed)
729 push @buf,['','-----------'];
730 push @buf,[ 'Total Previous Balance',
731 $money_char. sprintf("%10.2f",$pr_total ) ];
736 foreach ( $self->cust_bill_pkg ) {
740 my($cust_pkg)=qsearchs('cust_pkg', { 'pkgnum', $_->pkgnum } );
741 my($part_pkg)=qsearchs('part_pkg',{'pkgpart'=>$cust_pkg->pkgpart});
742 my($pkg)=$part_pkg->pkg;
744 if ( $_->setup != 0 ) {
745 push @buf, [ "$pkg Setup", $money_char. sprintf("%10.2f",$_->setup) ];
747 map { [ " ". $_->[0]. ": ". $_->[1], '' ] } $cust_pkg->labels;
750 if ( $_->recur != 0 ) {
752 "$pkg (" . time2str("%x",$_->sdate) . " - " .
753 time2str("%x",$_->edate) . ")",
754 $money_char. sprintf("%10.2f",$_->recur)
757 map { [ " ". $_->[0]. ": ". $_->[1], '' ] } $cust_pkg->labels;
761 push @buf,["Tax", $money_char. sprintf("%10.2f",$_->setup) ]
766 push @buf,['','-----------'];
767 push @buf,['Total New Charges',
768 $money_char. sprintf("%10.2f",$self->charged) ];
771 push @buf,['','-----------'];
772 push @buf,['Total Charges',
773 $money_char. sprintf("%10.2f",$self->charged + $pr_total) ];
777 foreach ( $self->cust_credited ) {
779 #something more elaborate if $_->amount ne $_->cust_credit->credited ?
781 my $reason = substr($_->cust_credit->reason,0,32);
782 $reason .= '...' if length($reason) < length($_->cust_credit->reason);
783 $reason = " ($reason) " if $reason;
785 "Credit #". $_->crednum. " (". time2str("%x",$_->cust_credit->_date) .")".
787 $money_char. sprintf("%10.2f",$_->amount)
790 #foreach ( @cr_cust_credit ) {
792 # "Credit #". $_->crednum. " (" . time2str("%x",$_->_date) .")",
793 # $money_char. sprintf("%10.2f",$_->credited)
797 #get & print payments
798 foreach ( $self->cust_bill_pay ) {
800 #something more elaborate if $_->amount ne ->cust_pay->paid ?
803 "Payment received ". time2str("%x",$_->cust_pay->_date ),
804 $money_char. sprintf("%10.2f",$_->amount )
809 push @buf,['','-----------'];
810 push @buf,['Balance Due', $money_char.
811 sprintf("%10.2f", $balance_due ) ];
813 #setup template variables
815 package FS::cust_bill::_template; #!
816 use vars qw( $invnum $date $page $total_pages @address $overdue @buf );
818 $invnum = $self->invnum;
819 $date = $self->_date;
823 int( scalar(@FS::cust_bill::buf) / $FS::cust_bill::invoice_lines );
825 if scalar(@FS::cust_bill::buf) % $FS::cust_bill::invoice_lines;
828 #format address (variable for the template)
830 @address = ( '', '', '', '', '', '' );
831 package FS::cust_bill; #!
832 $FS::cust_bill::_template::address[$l++] =
834 ( ( $cust_main->payby eq 'BILL' ) && $cust_main->payinfo
835 ? " (P.O. #". $cust_main->payinfo. ")"
839 $FS::cust_bill::_template::address[$l++] = $cust_main->company
840 if $cust_main->company;
841 $FS::cust_bill::_template::address[$l++] = $cust_main->address1;
842 $FS::cust_bill::_template::address[$l++] = $cust_main->address2
843 if $cust_main->address2;
844 $FS::cust_bill::_template::address[$l++] =
845 $cust_main->city. ", ". $cust_main->state. " ". $cust_main->zip;
846 $FS::cust_bill::_template::address[$l++] = $cust_main->country
847 unless $cust_main->country eq 'US';
849 #overdue? (variable for the template)
850 $FS::cust_bill::_template::overdue = (
852 && $today > $self->_date
853 # && $self->printed > 1
854 && $self->printed > 0
857 #and subroutine for the template
859 sub FS::cust_bill::_template::invoice_lines {
862 scalar(@buf) ? shift @buf : [ '', '' ];
867 $FS::cust_bill::_template::page = 1;
871 push @collect, split("\n",
872 $invoice_template->fill_in( PACKAGE => 'FS::cust_bill::_template' )
874 $FS::cust_bill::_template::page++;
877 map "$_\n", @collect;
885 $Id: cust_bill.pm,v 1.22 2002-03-07 14:13:21 ivan Exp $
891 print_text formatting (and some logic :/) is in source, but needs to be
892 slurped in from a file. Also number of lines ($=).
894 missing print_ps for a nice postscript copy (maybe HylaFAX-cover-page-style
895 or something similar so the look can be completely customized?)
899 L<FS::Record>, L<FS::cust_main>, L<FS::cust_bill_pay>, L<FS::cust_pay>,
900 L<FS::cust_bill_pkg>, L<FS::cust_bill_credit>, schema.html from the base