X-Git-Url: http://git.freeside.biz/gitweb/?a=blobdiff_plain;f=FS%2FFS%2Fcust_bill.pm;h=fc6a7ddbe309a00ff518ccd73ab9f2d7e611d680;hb=3d0a1bb06b895c5be6e3f0517d355442a6b1e125;hp=15543a9ea2c57b60fbc9bb28c453de3c6c8d995f;hpb=a9579d4fc556a54ad76b863cdd19abba79f3fc5f;p=freeside.git diff --git a/FS/FS/cust_bill.pm b/FS/FS/cust_bill.pm index 15543a9ea..fc6a7ddbe 100644 --- a/FS/FS/cust_bill.pm +++ b/FS/FS/cust_bill.pm @@ -1,24 +1,20 @@ package FS::cust_bill; +use base qw( FS::Template_Mixin FS::cust_main_Mixin FS::Record ); use strict; -use vars qw( @ISA $DEBUG $me $conf - $money_char $date_format $rdate_format $date_format_long ); -use vars qw( $invoice_lines @buf ); #yuck +use vars qw( $DEBUG $me $date_format ); + # but NOT $conf use Fcntl qw(:flock); #for spool_csv use Cwd; -use List::Util qw(min max); +use List::Util qw(min max sum); use Date::Format; -use Text::Template 1.20; use File::Temp 0.14; -use String::ShellQuote; use HTML::Entities; -use Locale::Country; use Storable qw( freeze thaw ); use GD::Barcode; use FS::UID qw( datasrc ); -use FS::Misc qw( send_email send_fax generate_ps generate_pdf do_print ); +use FS::Misc qw( send_email send_fax do_print ); use FS::Record qw( qsearch qsearchs dbh ); -use FS::cust_main_Mixin; use FS::cust_main; use FS::cust_statement; use FS::cust_bill_pkg; @@ -41,19 +37,17 @@ use FS::bill_batch; use FS::cust_bill_batch; use FS::cust_bill_pay_pkg; use FS::cust_credit_bill_pkg; - -@ISA = qw( FS::cust_main_Mixin FS::Record ); +use FS::discount_plan; +use FS::cust_bill_void; +use FS::L10N; $DEBUG = 0; $me = '[FS::cust_bill]'; #ask FS::UID to run this stuff for us later FS::UID->install_callback( sub { - $conf = new FS::Conf; - $money_char = $conf->config('money_char') || '$'; + my $conf = new FS::Conf; #global $date_format = $conf->config('date_format') || '%x'; #/YY - $rdate_format = $conf->config('date_format') || '%m/%d/%Y'; #/YYYY - $date_format_long = $conf->config('date_format_long') || '%b %o, %Y'; } ); =head1 NAME @@ -116,9 +110,11 @@ Customer info at invoice generation time =over 4 -=item previous_balance +=item billing_balance - the customer's balance at the time the invoice was +generated (not including charges on this invoice) -=item billing_balance +=item previous_balance - the billing_balance of this customer's previous +invoice plus the charges on that invoice =back @@ -140,6 +136,8 @@ Specific use cases =item agent_invid - legacy invoice number +=item promised_date - customer promised payment date, for collection + =back =head1 METHODS @@ -155,6 +153,7 @@ Invoices are normally created by calling the bill method of a customer object =cut sub table { 'cust_bill'; } +sub notice_name { 'Invoice'; } sub cust_linked { $_[0]->cust_main_custnum; } sub cust_unlinked_msg { @@ -207,10 +206,63 @@ sub insert { } +=item void + +Voids this invoice: deletes the invoice and adds a record of the voided invoice +to the FS::cust_bill_void table (and related tables starting from +FS::cust_bill_pkg_void). + +=cut + +sub void { + my $self = shift; + my $reason = scalar(@_) ? shift : ''; + + local $SIG{HUP} = 'IGNORE'; + local $SIG{INT} = 'IGNORE'; + local $SIG{QUIT} = 'IGNORE'; + local $SIG{TERM} = 'IGNORE'; + local $SIG{TSTP} = 'IGNORE'; + local $SIG{PIPE} = 'IGNORE'; + + my $oldAutoCommit = $FS::UID::AutoCommit; + local $FS::UID::AutoCommit = 0; + my $dbh = dbh; + + my $cust_bill_void = new FS::cust_bill_void ( { + map { $_ => $self->get($_) } $self->fields + } ); + $cust_bill_void->reason($reason); + my $error = $cust_bill_void->insert; + if ( $error ) { + $dbh->rollback if $oldAutoCommit; + return $error; + } + + foreach my $cust_bill_pkg ( $self->cust_bill_pkg ) { + my $error = $cust_bill_pkg->void($reason); + if ( $error ) { + $dbh->rollback if $oldAutoCommit; + return $error; + } + } + + $error = $self->delete; + if ( $error ) { + $dbh->rollback if $oldAutoCommit; + return $error; + } + + $dbh->commit or die $dbh->errstr if $oldAutoCommit; + + ''; + +} + =item delete This method now works but you probably shouldn't use it. Instead, apply a -credit against the invoice. +credit against the invoice, or use the new void method. Using this method to delete invoices outright is really, really bad. There would be no record you ever posted this invoice, and there are no check to @@ -240,12 +292,10 @@ sub delete { cust_event cust_credit_bill cust_bill_pay - cust_bill_pay - cust_credit_bill cust_pay_batch cust_bill_pay_batch - cust_bill_pkg cust_bill_batch + cust_bill_pkg )) { foreach my $linked ( $self->$table() ) { @@ -364,6 +414,7 @@ cust_bill-default_agent_invid is set and it has a value, invnum otherwise. sub display_invnum { my $self = shift; + my $conf = $self->conf; if ( $conf->exists('cust_bill-default_agent_invid') && $self->agent_invid ){ return $self->agent_invid; } else { @@ -382,13 +433,29 @@ sub previous { my $self = shift; my $total = 0; my @cust_bill = sort { $a->_date <=> $b->_date } - grep { $_->owed != 0 && $_->_date < $self->_date } - qsearch( 'cust_bill', { 'custnum' => $self->custnum } ) + grep { $_->owed != 0 } + qsearch( 'cust_bill', { 'custnum' => $self->custnum, + #'_date' => { op=>'<', value=>$self->_date }, + 'invnum' => { op=>'<', value=>$self->invnum }, + } ) ; foreach ( @cust_bill ) { $total += $_->owed; } $total, @cust_bill; } +=item enable_previous + +Whether to show the 'Previous Charges' section when printing this invoice. +The negation of the 'disable_previous_balance' config setting. + +=cut + +sub enable_previous { + my $self = shift; + my $agentnum = $self->cust_main->agentnum; + !$self->conf->exists('disable_previous_balance', $agentnum); +} + =item cust_bill_pkg Returns the line items (see L) for this invoice. @@ -745,6 +812,18 @@ sub cust_bill_batch { qsearch('cust_bill_batch', { 'invnum' => $self->invnum }); } +=item discount_plans + +Returns all discount plans (L) for this invoice, as a +hash keyed by term length. + +=cut + +sub discount_plans { + my $self = shift; + FS::discount_plan->all($self); +} + =item tax Returns the tax amount (see L) for this invoice. @@ -793,6 +872,23 @@ sub owed_pkgnum { $balance; } +=item hide + +Returns true if this invoice should be hidden. See the +selfservice-hide_invoices-taxclass configuraiton setting. + +=cut + +sub hide { + my $self = shift; + my $conf = $self->conf; + my $hide_taxclass = $conf->config('selfservice-hide_invoices-taxclass') + or return ''; + my @cust_bill_pkg = $self->cust_bill_pkg; + my @part_pkg = grep $_, map $_->part_pkg, @cust_bill_pkg; + ! grep { $_->taxclass ne $hide_taxclass } @part_pkg; +} + =item apply_payments_and_credits [ OPTION => VALUE ... ] Applies unapplied payments and credits to this invoice. @@ -807,6 +903,7 @@ If there is an error, returns the error, otherwise returns false. sub apply_payments_and_credits { my( $self, %options ) = @_; + my $conf = $self->conf; local $SIG{HUP} = 'IGNORE'; local $SIG{INT} = 'IGNORE'; @@ -955,6 +1052,7 @@ sub generate_email { my $self = shift; my %args = @_; + my $conf = $self->conf; my $me = '[FS::cust_bill::generate_email]'; @@ -989,7 +1087,7 @@ sub generate_email { my $alternative = build MIME::Entity 'Type' => 'multipart/alternative', - 'Encoding' => '7bit', + #'Encoding' => '7bit', 'Disposition' => 'inline' ; @@ -1017,47 +1115,60 @@ sub generate_email { $alternative->attach( 'Type' => 'text/plain', - #'Encoding' => 'quoted-printable', - 'Encoding' => '7bit', + 'Encoding' => 'quoted-printable', + #'Encoding' => '7bit', 'Data' => $data, 'Disposition' => 'inline', ); - $args{'from'} =~ /\@([\w\.\-]+)/; - my $from = $1 || 'example.com'; - my $content_id = join('.', rand()*(2**32), $$, time). "\@$from"; - - my $logo; - my $agentnum = $cust_main->agentnum; - if ( defined($args{'template'}) && length($args{'template'}) - && $conf->exists( 'logo_'. $args{'template'}. '.png', $agentnum ) - ) - { - $logo = 'logo_'. $args{'template'}. '.png'; + + my $htmldata; + my $image = ''; + my $barcode = ''; + if ( $conf->exists('invoice_email_pdf') + and scalar($conf->config('invoice_email_pdf_note')) ) { + + $htmldata = join('
', $conf->config('invoice_email_pdf_note') ); + } else { - $logo = "logo.png"; - } - my $image_data = $conf->config_binary( $logo, $agentnum); - - my $image = build MIME::Entity - 'Type' => 'image/png', - 'Encoding' => 'base64', - 'Data' => $image_data, - 'Filename' => 'logo.png', - 'Content-ID' => "<$content_id>", - ; + + $args{'from'} =~ /\@([\w\.\-]+)/; + my $from = $1 || 'example.com'; + my $content_id = join('.', rand()*(2**32), $$, time). "\@$from"; + + my $logo; + my $agentnum = $cust_main->agentnum; + if ( defined($args{'template'}) && length($args{'template'}) + && $conf->exists( 'logo_'. $args{'template'}. '.png', $agentnum ) + ) + { + $logo = 'logo_'. $args{'template'}. '.png'; + } else { + $logo = "logo.png"; + } + my $image_data = $conf->config_binary( $logo, $agentnum); + + $image = build MIME::Entity + 'Type' => 'image/png', + 'Encoding' => 'base64', + 'Data' => $image_data, + 'Filename' => 'logo.png', + 'Content-ID' => "<$content_id>", + ; - my $barcode; - if($conf->exists('invoice-barcode')){ - my $barcode_content_id = join('.', rand()*(2**32), $$, time). "\@$from"; - $barcode = build MIME::Entity - 'Type' => 'image/png', - 'Encoding' => 'base64', - 'Data' => $self->invoice_barcode(0), - 'Filename' => 'barcode.png', - 'Content-ID' => "<$barcode_content_id>", - ; - $opt{'barcode_cid'} = $barcode_content_id; + if ($conf->exists('invoice-barcode')) { + my $barcode_content_id = join('.', rand()*(2**32), $$, time). "\@$from"; + $barcode = build MIME::Entity + 'Type' => 'image/png', + 'Encoding' => 'base64', + 'Data' => $self->invoice_barcode(0), + 'Filename' => 'barcode.png', + 'Content-ID' => "<$barcode_content_id>", + ; + $opt{'barcode_cid'} = $barcode_content_id; + } + + $htmldata = $self->print_html({ 'cid'=>$content_id, %opt }); } $alternative->attach( @@ -1070,7 +1181,7 @@ sub generate_email { ' ', ' ', ' ', - $self->print_html({ 'cid'=>$content_id, %opt }), + $htmldata, ' ', '', ], @@ -1078,6 +1189,7 @@ sub generate_email { #'Filename' => 'invoice.pdf', ); + my @otherparts = (); if ( $cust_main->email_csv_cdr ) { @@ -1116,7 +1228,7 @@ sub generate_email { $related->add_part($alternative); - $related->add_part($image); + $related->add_part($image) if $image; my $pdf = build MIME::Entity $self->mimebuild_pdf(\%opt); @@ -1132,11 +1244,10 @@ sub generate_email { # image/png $return{'content-type'} = 'multipart/related'; - if($conf->exists('invoice-barcode')){ - $return{'mimeparts'} = [ $alternative, $image, $barcode, @otherparts ]; - } - else { - $return{'mimeparts'} = [ $alternative, $image, @otherparts ]; + if ($conf->exists('invoice-barcode') && $barcode) { + $return{'mimeparts'} = [ $alternative, $image, $barcode, @otherparts ]; + } else { + $return{'mimeparts'} = [ $alternative, $image, @otherparts ]; } $return{'type'} = 'multipart/alternative'; #Content-Type of first part... #$return{'disposition'} = 'inline'; @@ -1221,6 +1332,8 @@ invoice and all older invoices is greater than the specified amount. I, if specified, overrides "Invoice" as the name of the sent document (templates from 10/2009 or newer required) +I, if specified, is passed to + =cut sub queueable_send { @@ -1240,10 +1353,12 @@ sub queueable_send { sub send { my $self = shift; + my $conf = $self->conf; my( $template, $invoice_from, $notice_name ); my $agentnums = ''; my $balance_over = 0; + my $lpr = ''; if ( ref($_[0]) ) { my $opt = shift; @@ -1254,6 +1369,7 @@ sub send { $invoice_from = $opt->{'invoice_from'}; $balance_over = $opt->{'balance_over'} if $opt->{'balance_over'}; $notice_name = $opt->{'notice_name'}; + $lpr = $opt->{'lpr'} } else { $template = scalar(@_) ? shift : ''; if ( scalar(@_) && $_[0] ) { @@ -1263,14 +1379,16 @@ sub send { $balance_over = shift if scalar(@_) && $_[0] !~ /^\s*$/; } + my $cust_main = $self->cust_main; + return 'N/A' unless ! $agentnums - or grep { $_ == $self->cust_main->agentnum } @$agentnums; + or grep { $_ == $cust_main->agentnum } @$agentnums; return '' - unless $self->cust_main->total_owed_date($self->_date) > $balance_over; + unless $cust_main->total_owed_date($self->_date) > $balance_over; $invoice_from ||= $self->_agent_invoice_from || #XXX should go away - $conf->config('invoice_from', $self->cust_main->agentnum ); + $conf->config('invoice_from', $cust_main->agentnum ); my %opt = ( 'template' => $template, @@ -1278,16 +1396,19 @@ sub send { 'notice_name' => ( $notice_name || 'Invoice' ), ); - my @invoicing_list = $self->cust_main->invoicing_list; + my @invoicing_list = $cust_main->invoicing_list; #$self->email_invoice(\%opt) $self->email(\%opt) - if grep { $_ !~ /^(POST|FAX)$/ } @invoicing_list or !@invoicing_list; + if ( grep { $_ !~ /^(POST|FAX)$/ } @invoicing_list or !@invoicing_list ) + && ! $self->invoice_noemail; + $opt{'lpr'} = $lpr; #$self->print_invoice(\%opt) $self->print(\%opt) if grep { $_ eq 'POST' } @invoicing_list; #postal + #this has never been used post-$ORIGINAL_ISP afaik $self->fax_invoice(\%opt) if grep { $_ eq 'FAX' } @invoicing_list; #fax @@ -1329,6 +1450,8 @@ sub queueable_email { #sub email_invoice { sub email { my $self = shift; + return if $self->hide; + my $conf = $self->conf; my( $template, $invoice_from, $notice_name, $no_coupon ); if ( ref($_[0]) ) { @@ -1378,6 +1501,7 @@ sub email { sub email_subject { my $self = shift; + my $conf = $self->conf; #my $template = scalar(@_) ? shift : ''; #per-template? @@ -1409,6 +1533,7 @@ I, if specified, overrides "Invoice" as the name of the sent docume sub lpr_data { my $self = shift; + my $conf = $self->conf; my( $template, $notice_name ); if ( ref($_[0]) ) { my $opt = shift; @@ -1444,14 +1569,19 @@ I, if specified, overrides "Invoice" as the name of the sent docume #sub print_invoice { sub print { my $self = shift; - my( $template, $notice_name ); + return if $self->hide; + my $conf = $self->conf; + + my( $template, $notice_name, $lpr ); if ( ref($_[0]) ) { my $opt = shift; $template = $opt->{'template'} || ''; $notice_name = $opt->{'notice_name'} || 'Invoice'; + $lpr = $opt->{'lpr'} } else { $template = scalar(@_) ? shift : ''; $notice_name = 'Invoice'; + $lpr = ''; } my %opt = ( @@ -1464,7 +1594,11 @@ sub print { $self->batch_invoice(\%opt); } else { - do_print $self->lpr_data(\%opt); + do_print( + $self->lpr_data(\%opt), + 'agentnum' => $self->cust_main->agentnum, + 'lpr' => $lpr, + ); } } @@ -1483,6 +1617,9 @@ I, if specified, overrides "Invoice" as the name of the sent docume sub fax_invoice { my $self = shift; + return if $self->hide; + my $conf = $self->conf; + my( $template, $notice_name ); if ( ref($_[0]) ) { my $opt = shift; @@ -1520,14 +1657,37 @@ isn't an open batch, one will be created. sub batch_invoice { my ($self, $opt) = @_; - my $batch = FS::bill_batch->get_open_batch; + my $bill_batch = $self->get_open_bill_batch; my $cust_bill_batch = FS::cust_bill_batch->new({ - batchnum => $batch->batchnum, + batchnum => $bill_batch->batchnum, invnum => $self->invnum, }); return $cust_bill_batch->insert($opt); } +=item get_open_batch + +Returns the currently open batch as an FS::bill_batch object, creating a new +one if necessary. (A per-agent batch if invoice_print_pdf-spoolagent is +enabled) + +=cut + +sub get_open_bill_batch { + my $self = shift; + my $conf = $self->conf; + my $hashref = { status => 'O' }; + $hashref->{'agentnum'} = $conf->exists('invoice_print_pdf-spoolagent') + ? $self->cust_main->agentnum + : ''; + my $batch = qsearchs('bill_batch', $hashref); + return $batch if $batch; + $batch = FS::bill_batch->new($hashref); + my $error = $batch->insert; + die $error if $error; + return $batch; +} + =item ftp_invoice [ TEMPLATENAME ] Sends this invoice data via FTP. @@ -1538,6 +1698,7 @@ TEMPLATENAME is unused? sub ftp_invoice { my $self = shift; + my $conf = $self->conf; my $template = scalar(@_) ? shift : ''; $self->send_csv( @@ -1560,6 +1721,7 @@ TEMPLATENAME is unused? sub spool_invoice { my $self = shift; + my $conf = $self->conf; my $template = scalar(@_) ? shift : ''; $self->spool_csv( @@ -1662,13 +1824,24 @@ Options are: =over 4 -=item format - 'default' or 'billco' +=item format - any of FS::Misc::::Invoicing::spool_formats + +=item dest - if set (to POST, EMAIL or FAX), only sends spools invoices if the +customer has the corresponding invoice destinations set (see +L). -=item dest - if set (to POST, EMAIL or FAX), only sends spools invoices if the customer has the corresponding invoice destinations set (see L). +=item agent_spools - if set to a true value, will spool to per-agent files +rather than a single global file -=item agent_spools - if set to a true value, will spool to per-agent files rather than a single global file +=item upload_targetnum - if set to a target (see L), will +append to that spool. L will then send the spool file to +that destination. -=item balanceover - if set, only spools the invoice if the total amount owed on this invoice and all older invoices is greater than the specified amount. +=item balanceover - if set, only spools the invoice if the total amount owed on +this invoice and all older invoices is greater than the specified amount. + +=item time - the "current time". Controls the printing of past due messages +in the ICS format. =back @@ -1677,6 +1850,7 @@ Options are: sub spool_csv { my($self, %opt) = @_; + my $time = $opt{'time'} || time; my $cust_main = $self->cust_main; if ( $opt{'dest'} ) { @@ -1694,15 +1868,27 @@ sub spool_csv { my $spooldir = "/usr/local/etc/freeside/export.". datasrc. "/cust_bill"; mkdir $spooldir, 0700 unless -d $spooldir; - my $tracctnum = $self->invnum. time2str('-%Y%m%d%H%M%S', time); + my $tracctnum = $self->invnum. time2str('-%Y%m%d%H%M%S', $time); + + my $file; + if ( $opt{'agent_spools'} ) { + $file = 'agentnum'.$cust_main->agentnum; + } else { + $file = 'spool'; + } + + if ( $opt{'upload_targetnum'} ) { + $spooldir .= '/target'.$opt{'upload_targetnum'}; + mkdir $spooldir, 0700 unless -d $spooldir; + } # otherwise it just goes into export.xxx/cust_bill + + if ( lc($opt{'format'}) eq 'billco' ) { + $file .= '-header'; + } - my $file = - "$spooldir/". - ( $opt{'agent_spools'} ? 'agentnum'.$cust_main->agentnum : 'spool' ). - ( lc($opt{'format'}) eq 'billco' ? '-header' : '' ) . - '.csv'; + $file = "$spooldir/$file.csv"; - my ( $header, $detail ) = $self->print_csv(%opt, 'tracctnum' => $tracctnum ); + my ( $header, $detail ) = $self->print_csv(%opt, 'tracctnum' => $tracctnum); open(CSV, ">>$file") or die "can't open $file: $!"; flock(CSV, LOCK_EX); @@ -1715,17 +1901,14 @@ sub spool_csv { flock(CSV, LOCK_UN); close CSV; - $file = - "$spooldir/". - ( $opt{'agent_spools'} ? 'agentnum'.$cust_main->agentnum : 'spool' ). - '-detail.csv'; + $file =~ s/-header.csv$/-detail.csv/; open(CSV,">>$file") or die "can't open $file: $!"; flock(CSV, LOCK_EX); seek(CSV, 0, 2); } - print CSV $detail; + print CSV $detail if defined($detail); flock(CSV, LOCK_UN); close CSV; @@ -1740,7 +1923,7 @@ Returns CSV data for this invoice. Options are: -format - 'default' or 'billco' +format - 'default', 'billco', 'oneline', 'bridgestone' Returns a list consisting of two scalars. The first is a single line of CSV header information for this invoice. The second is one or more lines of CSV @@ -1749,7 +1932,8 @@ detail information for this invoice. If I is not specified or "default", the fields of the CSV file are as follows: -record_type, invnum, custnum, _date, charged, first, last, company, address1, address2, city, state, zip, country, pkg, setup, recur, sdate, edate +record_type, invnum, custnum, _date, charged, first, last, company, address1, +address2, city, state, zip, country, pkg, setup, recur, sdate, edate =over 4 @@ -1854,6 +2038,26 @@ If I is "billco", the fields of the detail CSV file are as follows: 9 | Grouping Code | GROUP | CHAR | 2 10 | User Defined | ACCT CODE | CHAR | 15 +If format is 'oneline', there is no detail file. Each invoice has a +header line only, with the fields: + +Agent number, agent name, customer number, first name, last name, address +line 1, address line 2, city, state, zip, invoice date, invoice number, +amount charged, amount due, previous balance, due date. + +and then, for each line item, three columns containing the package number, +description, and amount. + +If format is 'bridgestone', there is no detail file. Each invoice has a +header line with the following fields in a fixed-width format: + +Customer number (in display format), date, name (first last), company, +address 1, address 2, city, state, zip. + +This is a mailing list format, and has no per-invoice fields. To avoid +sending redundant notices, the spooling event should have a "once" or +"once_percust_every" condition. + =cut sub print_csv { @@ -1865,8 +2069,11 @@ sub print_csv { my $cust_main = $self->cust_main; my $csv = Text::CSV_XS->new({'always_quote'=>1}); + my $format = lc($opt{'format'}); - if ( lc($opt{'format'}) eq 'billco' ) { + my $time = $opt{'time'} || time; + + if ( $format eq 'billco' ) { my $taxtotal = 0; $taxtotal += $_->{'amount'} foreach $self->_items_tax; @@ -1919,7 +2126,184 @@ sub print_csv { '0', # 29 | Other Taxes & Fees*** NUM* 9 ); - } else { + } elsif ( $format eq 'oneline' ) { #name + + my ($previous_balance) = $self->previous; + $previous_balance = sprintf('%.2f', $previous_balance); + my $totaldue = sprintf('%.2f', $self->owed + $previous_balance); + my @items = map { + $_->{pkgnum}, + $_->{description}, + $_->{amount} + } + $self->_items_pkg, #_items_nontax? no sections or anything + # with this format + $self->_items_tax; + + $csv->combine( + $cust_main->agentnum, + $cust_main->agent->agent, + $self->custnum, + $cust_main->first, + $cust_main->last, + $cust_main->company, + $cust_main->address1, + $cust_main->address2, + $cust_main->city, + $cust_main->state, + $cust_main->zip, + + # invoice fields + time2str("%x", $self->_date), + $self->invnum, + $self->charged, + $totaldue, + $previous_balance, + $self->due_date2str("%x"), + + @items, + ); + + } elsif ( $format eq 'bridgestone' ) { + + # bypass the CSV stuff and just return this + my $longdate = time2str('%B %d, %Y', $time); #current time, right? + my $zip = $cust_main->zip; + $zip =~ s/\D//; + my $prefix = $self->conf->config('bridgestone-prefix', $cust_main->agentnum) + || ''; + return ( + sprintf( + "%-5s%-15s%-20s%-30s%-30s%-30s%-30s%-20s%-2s%-9s\n", + $prefix, + $cust_main->display_custnum, + $longdate, + uc(substr($cust_main->contact_firstlast,0,30)), + uc(substr($cust_main->company ,0,30)), + uc(substr($cust_main->address1 ,0,30)), + uc(substr($cust_main->address2 ,0,30)), + uc(substr($cust_main->city ,0,20)), + uc($cust_main->state), + $zip + ), + '' #detail + ); + + } elsif ( $format eq 'ics' ) { + + my $bill = $cust_main->bill_location; + my $zip = $bill->zip; + my $zip4 = ''; + + $zip =~ s/\D//; + if ( $zip =~ /^(\d{5})(\d{4})$/ ) { + $zip = $1; + $zip4 = $2; + } + + # minor false laziness with print_generic + my ($previous_balance) = $self->previous; + my $balance_due = $self->owed + $previous_balance; + my $payment_total = sum(0, map { $_->{'amount'} } $self->_items_payments); + my $credit_total = sum(0, map { $_->{'amount'} } $self->_items_credits); + + my $past_due = ''; + if ( $self->due_date and $time >= $self->due_date ) { + $past_due = sprintf('Past due:$%0.2f Due Immediately', $balance_due); + } + + # again, bypass CSV + my $header = sprintf( + '%-10s%-30s%-48s%-2s%-50s%-30s%-30s%-25s%-2s%-5s%-4s%-8s%-8s%-10s%-10s%-10s%-10s%-10s%-10s%-480s%-35s', + $cust_main->display_custnum, #BID + uc($cust_main->first), #FNAME + uc($cust_main->last), #LNAME + '00', #BATCH, should this ever be anything else? + uc($cust_main->company), #COMP + uc($bill->address1), #STREET1 + uc($bill->address2), #STREET2 + uc($bill->city), #CITY + uc($bill->state), #STATE + $zip, + $zip4, + time2str('%Y%m%d', $self->_date), #BILL_DATE + $self->due_date2str('%Y%m%d'), #DUE_DATE, + ( map {sprintf('%0.2f', $_)} + $balance_due, #AMNT_DUE + $previous_balance, #PREV_BAL + $payment_total, #PYMT_RCVD + $credit_total, #CREDITS + $previous_balance, #BEG_BAL--is this correct? + $self->charged, #NEW_CHRG + ), + 'img01', #MRKT_MSG? + $past_due, #PAST_MSG + ); + + my @details; + my %svc_class = ('' => ''); # maybe cache this more persistently? + + foreach my $cust_bill_pkg ( $self->cust_bill_pkg ) { + + my $show_pkgnum = $cust_bill_pkg->pkgnum || ''; + my $cust_pkg = $cust_bill_pkg->cust_pkg if $show_pkgnum; + + if ( $cust_pkg ) { + + my @dates = ( $self->_date, undef ); + if ( my $prev = $cust_bill_pkg->previous_cust_bill_pkg ) { + $dates[1] = $prev->sdate; #questionable + } + + # generate an 01 detail for each service + my @svcs = $cust_pkg->h_cust_svc(@dates, 'I'); + foreach my $cust_svc ( @svcs ) { + $show_pkgnum = ''; # hide it if we're showing svcnums + + my $svcpart = $cust_svc->svcpart; + if (!exists($svc_class{$svcpart})) { + my $classnum = $cust_svc->part_svc->classnum; + my $part_svc_class = FS::part_svc_class->by_key($classnum) + if $classnum; + $svc_class{$svcpart} = $part_svc_class ? + $part_svc_class->classname : + ''; + } + + my @h_label = $cust_svc->label(@dates, 'I'); + push @details, sprintf('01%-9s%-20s%-47s', + $cust_svc->svcnum, + $svc_class{$svcpart}, + $h_label[1], + ); + } #foreach $cust_svc + } #if $cust_pkg + + my $desc = $cust_bill_pkg->desc; # itemdesc or part_pkg.pkg + if ($cust_bill_pkg->recur > 0) { + $desc .= ' '.time2str('%d-%b-%Y', $cust_bill_pkg->sdate).' to '. + time2str('%d-%b-%Y', $cust_bill_pkg->edate - 86400); + } + push @details, sprintf('02%-6s%-60s%-10s', + $show_pkgnum, + $desc, + sprintf('%0.2f', $cust_bill_pkg->setup + $cust_bill_pkg->recur), + ); + } #foreach $cust_bill_pkg + + # Tag this row so that we know whether this is one page (1), two pages + # (2), # or "big" (B). The tag will be stripped off before uploading. + if ( scalar(@details) < 12 ) { + push @details, '1'; + } elsif ( scalar(@details) < 58 ) { + push @details, '2'; + } else { + push @details, 'B'; + } + + return join('', $header, @details, "\n"); + + } else { # default $csv->combine( 'cust_bill', @@ -1958,6 +2342,10 @@ sub print_csv { } + } elsif ( lc($opt{'format'}) eq 'oneline' ) { + + #do nothing + } else { foreach my $cust_bill_pkg ( $self->cust_bill_pkg ) { @@ -2069,6 +2457,7 @@ sub realtime_lec { sub realtime_bop { my( $self, $method ) = (shift,shift); + my $conf = $self->conf; my %opt = @_; my $cust_main = $self->cust_main; @@ -2135,141 +2524,6 @@ sub _agent_invoice_from { $self->cust_main->agent_invoice_from; } -=item print_text HASHREF | [ TIME [ , TEMPLATE [ , OPTION => VALUE ... ] ] ] - -Returns an text invoice, as a list of lines. - -Options can be passed as a hashref (recommended) or as a list of time, template -and then any key/value pairs for any other options. - -I