From f81eebbe27a6acc0ae4284fa04b5525a41ae4570 Mon Sep 17 00:00:00 2001 From: Mitch Jackson Date: Thu, 9 Aug 2018 14:55:44 -0400 Subject: [PATCH] RT# 78547 bill_and_collect() small optimization --- FS/FS/cust_main.pm | 10 ++++++++++ FS/FS/cust_main/Billing.pm | 8 +++----- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/FS/FS/cust_main.pm b/FS/FS/cust_main.pm index 3bffa3a59..ea524dae4 100644 --- a/FS/FS/cust_main.pm +++ b/FS/FS/cust_main.pm @@ -5414,6 +5414,16 @@ sub process_bill_and_collect { $cust_main->bill_and_collect( %$param ); } +=item pending_invoice_count + +Return number of cust_bill with pending=Y for this customer + +=cut + +sub pending_invoice_count { + FS::cust_bill->count( 'custnum = '.shift->custnum."AND pending = 'Y'" ); +} + #starting to take quite a while for big dbs # (JRNL: journaled so it only happens once per database) # - seq scan of h_cust_main (yuck), but not going to index paycvv, so diff --git a/FS/FS/cust_main/Billing.pm b/FS/FS/cust_main/Billing.pm index 9cf9b56c6..71d5c9b81 100644 --- a/FS/FS/cust_main/Billing.pm +++ b/FS/FS/cust_main/Billing.pm @@ -1,6 +1,7 @@ package FS::cust_main::Billing; use strict; +use feature 'state'; use vars qw( $conf $DEBUG $me ); use Carp; use Data::Dumper; @@ -170,11 +171,8 @@ sub bill_and_collect { # In a batch tax environment, do not run collection if any pending # invoices were created. Collection will run after the next tax batch. - my $tax = FS::TaxEngine->new; - if ( $tax->info->{batch} and - qsearch('cust_bill', { custnum => $self->custnum, pending => 'Y' }) - ) - { + state $is_batch_tax = FS::TaxEngine->new->info->{batch} ? 1 : 0; + if ( $is_batch_tax && $self->pending_invoice_count ) { warn "skipped collection for custnum ".$self->custnum. " due to pending invoices\n" if $DEBUG; } elsif ( $conf->exists('cancelled_cust-noevents') -- 2.11.0