X-Git-Url: http://git.freeside.biz/gitweb/?p=freeside.git;a=blobdiff_plain;f=FS%2FFS%2Fcust_bill.pm;h=5118f020e061c200eab96503c3ebe7edaf142f3c;hp=1c5ecf8d3744b2cc00655fd0ae0cbd614ce87c75;hb=17a8b72b78ba455b58d53731fe557a471e0f2947;hpb=7fe9f776655a9a7fc3b93b4f0e06c8b8193834b6 diff --git a/FS/FS/cust_bill.pm b/FS/FS/cust_bill.pm index 1c5ecf8d3..5118f020e 100644 --- a/FS/FS/cust_bill.pm +++ b/FS/FS/cust_bill.pm @@ -215,6 +215,7 @@ FS::cust_bill_pkg_void). sub void { my $self = shift; my $reason = scalar(@_) ? shift : ''; + my $reprocess_cdrs = scalar(@_) ? shift : ''; local $SIG{HUP} = 'IGNORE'; local $SIG{INT} = 'IGNORE'; @@ -238,7 +239,7 @@ sub void { } foreach my $cust_bill_pkg ( $self->cust_bill_pkg ) { - my $error = $cust_bill_pkg->void($reason); + my $error = $cust_bill_pkg->void($reason, $reprocess_cdrs); if ( $error ) { $dbh->rollback if $oldAutoCommit; return $error; @@ -448,16 +449,20 @@ followed by the previous outstanding invoices (as FS::cust_bill objects also). sub previous { my $self = shift; - my $total = 0; - my @cust_bill = sort { $a->_date <=> $b->_date } - 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; + # simple memoize; we use this a lot + if (!$self->get('previous')) { + my $total = 0; + my @cust_bill = sort { $a->_date <=> $b->_date } + grep { $_->owed != 0 } + qsearch( 'cust_bill', { 'custnum' => $self->custnum, + #'_date' => { op=>'<', value=>$self->_date }, + 'invnum' => { op=>'<', value=>$self->invnum }, + } ) + ; + foreach ( @cust_bill ) { $total += $_->owed; } + $self->set('previous', [$total, @cust_bill]); + } + return @{ $self->get('previous') }; } =item enable_previous @@ -482,7 +487,13 @@ Returns the line items (see L) for this invoice. sub cust_bill_pkg { my $self = shift; qsearch( - { 'table' => 'cust_bill_pkg', + { + 'select' => 'cust_bill_pkg.*, pkg_category.categoryname', + 'table' => 'cust_bill_pkg', + 'addl_from' => ' LEFT JOIN cust_pkg USING ( pkgnum ) '. + ' LEFT JOIN part_pkg USING ( pkgpart ) '. + ' LEFT JOIN pkg_class USING ( classnum ) '. + ' LEFT JOIN pkg_category USING ( categorynum ) ', 'hashref' => { 'invnum' => $self->invnum }, 'order_by' => 'ORDER BY billpkgnum', } @@ -909,6 +920,7 @@ sub hide { =item apply_payments_and_credits [ OPTION => VALUE ... ] Applies unapplied payments and credits to this invoice. +Payments with the no_auto_apply flag set will not be applied. A hash of optional arguments may be passed. Currently "manual" is supported. If true, a payment receipt is sent instead of a statement when @@ -935,7 +947,9 @@ sub apply_payments_and_credits { $self->select_for_update; #mutex - my @payments = grep { $_->unapplied > 0 } $self->cust_main->cust_pay; + my @payments = grep { $_->unapplied > 0 } + grep { !$_->no_auto_apply } + $self->cust_main->cust_pay; my @credits = grep { $_->credited > 0 } $self->cust_main->cust_credit; if ( $conf->exists('pkg-balances') ) { @@ -1092,10 +1106,7 @@ sub email { # this is where we set the From: address $from ||= $self->_agent_invoice_from || #XXX should go away - $conf->config('invoice_from_name', $self->cust_main->agentnum ) ? - $conf->config('invoice_from_name', $self->cust_main->agentnum ) . ' <' . - $conf->config('invoice_from', $self->cust_main->agentnum ) . '>' : - $conf->config('invoice_from', $self->cust_main->agentnum ); + $conf->invoice_from_full( $self->cust_main->agentnum ); my @invoicing_list = $self->cust_main->invoicing_list_emailonly; @@ -1124,6 +1135,9 @@ sub queueable_email { my $self = qsearchs('cust_bill', { 'invnum' => $opt{invnum} } ) or die "invalid invoice number: " . $opt{invnum}; + $self->set('mode', $opt{mode}) + if $opt{mode}; + my %args = map {$_ => $opt{$_}} grep { $opt{$_} } qw( from notice_name no_coupon template ); @@ -1152,6 +1166,11 @@ sub email_subject { eval qq("$subject"); } +sub pdf_filename { + my $self = shift; + 'Invoice-'. $self->invnum. '.pdf'; +} + =item lpr_data HASHREF Returns the postscript or plaintext for this invoice as an arrayref. @@ -1263,6 +1282,10 @@ sub batch_invoice { batchnum => $bill_batch->batchnum, invnum => $self->invnum, }); + if ( $self->mode ) { + $opt->{mode} ||= $self->mode; + $opt->{mode} = $opt->{mode}->modenum if ref $opt->{mode}; + } return $cust_bill_batch->insert($opt); } @@ -1314,7 +1337,7 @@ sub ftp_invoice { =item spool_invoice [ TEMPLATENAME ] -Spools this invoice data (see L) +Spools this invoice data (see L) TEMPLATENAME is unused? @@ -1910,7 +1933,19 @@ sub print_csv { if ( lc($opt{'format'}) eq 'billco' ) { my $lineseq = 0; - foreach my $item ( $self->_items_pkg ) { + my %items_opt = ( format => 'template', + escape_function => sub { shift } ); + # I don't know what characters billco actually tolerates in spool entries. + # Text::CSV will take care of delimiters, though. + + my @items = ( $self->_items_pkg(%items_opt), + $self->_items_fee(%items_opt) ); + foreach my $item (@items) { + + my $description = $item->{'description'}; + if ( $item->{'_is_discount'} and exists($item->{ext_description}[0]) ) { + $description .= ': ' . $item->{ext_description}[0]; + } $csv->combine( '', # 1 | N/A-Leave Empty CHAR 2 @@ -1918,7 +1953,7 @@ sub print_csv { $tracctnum, # 3 | Account Number CHAR 15 $self->invnum, # 4 | Invoice Number CHAR 15 $lineseq++, # 5 | Line Sequence (sort order) NUM 6 - $item->{'description'}, # 6 | Transaction Detail CHAR 100 + $description, # 6 | Transaction Detail CHAR 100 $item->{'amount'}, # 7 | Amount NUM* 9 '', # 8 | Line Format Control** CHAR 2 '', # 9 | Grouping Code CHAR 2 @@ -2203,7 +2238,7 @@ sub _items_extra_usage_sections { my %classnums = (); my %lines = (); - my $maxlength = $conf->config('cust_bill-latex_lineitem_maxlength') || 50; + my $maxlength = $conf->config('cust_bill-latex_lineitem_maxlength') || 40; my %usage_class = map { $_->classnum => $_ } qsearch( 'usage_class', {} ); foreach my $cust_bill_pkg ( $self->cust_bill_pkg ) { @@ -2443,7 +2478,7 @@ sub _items_svc_phone_sections { my %classnums = (); my %lines = (); - my $maxlength = $conf->config('cust_bill-latex_lineitem_maxlength') || 50; + my $maxlength = $conf->config('cust_bill-latex_lineitem_maxlength') || 40; my %usage_class = map { $_->classnum => $_ } qsearch( 'usage_class', {} ); $usage_class{''} ||= new FS::usage_class { 'classname' => '', 'weight' => 0 }; @@ -2679,10 +2714,12 @@ sub _items_usage_class_summary { my %opt = @_; my $escape = $opt{escape} || sub { $_[0] }; + my $money_char = $opt{money_char}; my $invnum = $self->invnum; my @classes = qsearch({ 'table' => 'usage_class', - 'select' => 'classnum, classname, SUM(amount) AS amount', + 'select' => 'classnum, classname, SUM(amount) AS amount,'. + ' COUNT(*) AS calls, SUM(duration) AS duration', 'addl_from' => ' LEFT JOIN cust_bill_pkg_detail USING (classnum)' . ' LEFT JOIN cust_bill_pkg USING (billpkgnum)', 'extra_sql' => " WHERE cust_bill_pkg.invnum = $invnum". @@ -2693,17 +2730,21 @@ sub _items_usage_class_summary { my @l; my $section = { description => &{$escape}($self->mt('Usage Summary')), - no_subtotal => 1, usage_section => 1, + subtotal => 0, }; foreach my $class (@classes) { + $section->{subtotal} += $class->get('amount'); push @l, { 'description' => &{$escape}($class->classname), - 'amount' => sprintf('%.2f', $class->amount), + 'amount' => $money_char.sprintf('%.2f', $class->get('amount')), + 'quantity' => $class->get('calls'), + 'duration' => $class->get('duration'), 'usage_classnum' => $class->classnum, 'section' => $section, }; } + $section->{subtotal} = $money_char.sprintf('%.2f', $section->{subtotal}); return @l; } @@ -2742,7 +2783,7 @@ sub _items_previous { sub _items_credits { my( $self, %opt ) = @_; - my $trim_len = $opt{'trim_len'} || 60; + my $trim_len = $opt{'trim_len'} || 40; my @b; #credits @@ -2839,6 +2880,89 @@ sub _items_payments { } +sub _items_total { + my $self = shift; + my $conf = $self->conf; + + my @items; + my ($pr_total) = $self->previous; + my ($previous_charges_desc, $new_charges_desc, $new_charges_amount); + + if ( $conf->exists('previous_balance-exclude_from_total') ) { + # if enabled, specifically add a line for the previous balance total + $previous_charges_desc = $self->mt( + $conf->config('previous_balance-text') || 'Previous Balance' + ); + + # then return separate lines for previous balance and total new charges + if ( $pr_total ) { + push @items, + { total_item => $previous_charges_desc, + total_amount => sprintf('%.2f',$pr_total) + }; + } + } + + if ( $conf->exists('previous_balance-exclude_from_total') + or !$self->enable_previous ) { + # show new charges only + + $new_charges_desc = $self->mt( + $conf->config('previous_balance-text-total_new_charges') + || 'Total New Charges' + ); + + $new_charges_amount = $self->charged; + + } else { + # show new charges + previous invoice total + + $new_charges_desc = $self->mt('Total Charges'); + if ( $self->enable_previous ) { + $new_charges_amount = sprintf('%.2f', $self->charged + $pr_total); + } else { + $new_charges_amount = sprintf('%.2f', $self->charged); + } + + } + if ( $conf->exists('invoice_show_prior_due_date') && !$conf->exists('invoice_omit_due_date') ) { + # then the due date should be shown with Total New Charges, + # and should NOT be shown with the Balance Due message. + if ( $self->due_date ) { + $new_charges_desc .= $self->invoice_pay_by_msg; + } elsif ( $self->terms ) { + # phrases like "due on receipt" should be localized + $new_charges_desc .= ' - ' . $self->mt($self->terms); + } + } + + push @items, + { total_item => $new_charges_desc, + total_amount => $new_charges_amount, + }; + + @items; +} + + + +=item has_call_details + +Returns true if this invoice has call details. + +=cut + +sub has_call_details { + my $self = shift; + $self->scalar_sql(" + SELECT 1 FROM cust_bill_pkg_detail + LEFT JOIN cust_bill_pkg USING (billpkgnum) + WHERE cust_bill_pkg_detail.format = 'C' + AND cust_bill_pkg.invnum = ? + LIMIT 1 + ", $self->invnum); +} + =item call_details [ OPTION => VALUE ... ] Returns an array of CSV strings representing the call details for this invoice @@ -2936,6 +3060,9 @@ sub process_re_X { } +# this is called from search/cust_bill.html and given all its search +# parameters, so it needs to perform the same search. + sub re_X { # spool_invoice ftp_invoice fax_invoice print_invoice my($method, $job, %param ) = @_; @@ -2945,22 +3072,15 @@ sub re_X { } #some false laziness w/search/cust_bill.html - my $distinct = ''; - my $orderby = 'ORDER BY cust_bill._date'; - - my $extra_sql = ' WHERE '. FS::cust_bill->search_sql_where(\%param); - - my $addl_from = 'LEFT JOIN cust_main USING ( custnum )'; - - my @cust_bill = qsearch( { - #'select' => "cust_bill.*", - 'table' => 'cust_bill', - 'addl_from' => $addl_from, - 'hashref' => {}, - 'extra_sql' => $extra_sql, - 'order_by' => $orderby, - 'debug' => 1, - } ); + $param{'order_by'} = 'cust_bill._date'; + + my $query = FS::cust_bill->search(\%param); + delete $query->{'count_query'}; + delete $query->{'count_addl'}; + + $query->{debug} = 1; # was in here before, is obviously useful + + my @cust_bill = qsearch( $query ); $method .= '_invoice' unless $method eq 'email' || $method eq 'print'; @@ -3084,7 +3204,7 @@ The delete method. =head1 SEE ALSO L, L, L, L, -L, L, schema.html from the base +L, L, schema.html from the base documentation. =cut