private void useExistingCBAFromTransaction ( final BigDecimal accountCBA , final List < Tag > invoicesTags , final EntitySqlDaoWrapperFactory entitySqlDaoWrapperFactory , final InternalCallContext context ) throws InvoiceApiException , EntityPersistenceException { if ( accountCBA . compareTo ( BigDecimal . ZERO ) <= 0 ) { return ; } // PERF: Computing the invoice balance is difficult to do in the DB, so we effectively need to retrieve all invoices on the account and filter the unpaid ones in memory. // This should be infrequent though because of the account CBA check above. final List < InvoiceModelDao > allInvoices = invoiceDaoHelper . getAllInvoicesByAccountFromTransaction ( false , invoicesTags , entitySqlDaoWrapperFactory , context ) ; final List < InvoiceModelDao > unpaidInvoices = invoiceDaoHelper . getUnpaidInvoicesByAccountFromTransaction ( allInvoices , null ) ; // We order the same os BillingStateCalculator-- should really share the comparator final List < InvoiceModelDao > orderedUnpaidInvoices = Ordering . from ( new Comparator < InvoiceModelDao > ( ) { @ Override public int compare ( final InvoiceModelDao i1 , final InvoiceModelDao i2 ) { return i1 . getInvoiceDate ( ) . compareTo ( i2 . getInvoiceDate ( ) ) ; } } ) . immutableSortedCopy ( unpaidInvoices ) ; BigDecimal remainingAccountCBA = accountCBA ; for ( final InvoiceModelDao unpaidInvoice : orderedUnpaidInvoices ) { remainingAccountCBA = computeCBAComplexityAndCreateCBAItem ( remainingAccountCBA , unpaidInvoice , entitySqlDaoWrapperFactory , context ) ; if ( remainingAccountCBA . compareTo ( BigDecimal . ZERO ) <= 0 ) { break ; } } }
Distribute account CBA across all COMMITTED unpaid invoices