Search in sources :

Example 1 with ContractsGrantsInvoiceDetail

use of org.kuali.kfs.module.ar.businessobject.ContractsGrantsInvoiceDetail in project cu-kfs by CU-CommunityApps.

the class ContractsGrantsInvoiceDocumentServiceImpl method adjustObjectCodeAmountsIfChanged.

/**
 * If any of the current expenditures for the cost categories on the Contracts & Grants Invoice Document have
 * changed, recalculate the Object Code amounts.
 *
 * @param contractsGrantsInvoiceDocument document containing cost categories to review
 * @return true if expenditure value changed, false otherwise
 */
protected boolean adjustObjectCodeAmountsIfChanged(ContractsGrantsInvoiceDocument contractsGrantsInvoiceDocument) {
    boolean isExpenditureValueChanged = false;
    // put the invoiceDetailAccountObjectCode into a map based on category
    List<InvoiceDetailAccountObjectCode> invoiceDetailAccountObjectCodes = contractsGrantsInvoiceDocument.getInvoiceDetailAccountObjectCodes();
    Map<String, List<InvoiceDetailAccountObjectCode>> invoiceDetailAccountObjectCodeMap = new HashMap<>();
    for (InvoiceDetailAccountObjectCode invoiceDetailAccountObjectCode : invoiceDetailAccountObjectCodes) {
        String categoryCode = invoiceDetailAccountObjectCode.getCategoryCode();
        List<InvoiceDetailAccountObjectCode> invoiceDetailAccountObjectCodeList = invoiceDetailAccountObjectCodeMap.get(categoryCode);
        // if new category, create new list to put into map
        if (invoiceDetailAccountObjectCodeList == null) {
            List<InvoiceDetailAccountObjectCode> newInvoiceDetailAccountObjectCodeList = new ArrayList<>();
            newInvoiceDetailAccountObjectCodeList.add(invoiceDetailAccountObjectCode);
            invoiceDetailAccountObjectCodeMap.put(categoryCode, newInvoiceDetailAccountObjectCodeList);
        } else {
            // else, if list is found, add it to existing list
            invoiceDetailAccountObjectCodeMap.get(categoryCode).add(invoiceDetailAccountObjectCode);
        }
    }
    // invoiceDetailObjectCode and update account details
    for (ContractsGrantsInvoiceDetail invoiceDetail : contractsGrantsInvoiceDocument.getInvoiceDetails()) {
        KualiDecimal total = getSumOfExpendituresOfCategory(invoiceDetailAccountObjectCodeMap.get(invoiceDetail.getCategoryCode()));
        // To set expenditures to zero if its blank - to avoid exceptions.
        if (ObjectUtils.isNull(invoiceDetail.getInvoiceAmount())) {
            invoiceDetail.setInvoiceAmount(KualiDecimal.ZERO);
        }
        if (invoiceDetail.getInvoiceAmount().compareTo(total) != 0) {
            recalculateObjectCodeByCategory(contractsGrantsInvoiceDocument, invoiceDetail, total, invoiceDetailAccountObjectCodeMap.get(invoiceDetail.getCategoryCode()));
            isExpenditureValueChanged = true;
        }
    }
    return isExpenditureValueChanged;
}
Also used : ContractsGrantsInvoiceDetail(org.kuali.kfs.module.ar.businessobject.ContractsGrantsInvoiceDetail) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) AbstractKualiDecimal(org.kuali.kfs.core.api.util.type.AbstractKualiDecimal) KualiDecimal(org.kuali.kfs.core.api.util.type.KualiDecimal) ArrayList(java.util.ArrayList) List(java.util.List) InvoiceDetailAccountObjectCode(org.kuali.kfs.module.ar.businessobject.InvoiceDetailAccountObjectCode)

Example 2 with ContractsGrantsInvoiceDetail

use of org.kuali.kfs.module.ar.businessobject.ContractsGrantsInvoiceDetail in project cu-kfs by CU-CommunityApps.

the class ContractsGrantsInvoiceDocumentServiceImpl method buildTotalIndirectCostInvoiceDetailFieldsMap.

private Map<String, Object> buildTotalIndirectCostInvoiceDetailFieldsMap(ContractsGrantsInvoiceDocument document) {
    Map<String, Object> totalIndirectCostInvoiceDetailFieldsMap = new HashMap<>();
    ContractsGrantsInvoiceDetail totalInDirectCostInvoiceDetail = document.getTotalIndirectCostInvoiceDetail();
    if (ObjectUtils.isNotNull(totalInDirectCostInvoiceDetail)) {
        final String prefix = ArPropertyConstants.IN_DIRECT_COST_INVOICE_DETAIL + ".";
        totalIndirectCostInvoiceDetailFieldsMap.putAll(buildInvoiceDetailFieldMap(prefix, totalInDirectCostInvoiceDetail));
    }
    return totalIndirectCostInvoiceDetailFieldsMap;
}
Also used : ContractsGrantsInvoiceDetail(org.kuali.kfs.module.ar.businessobject.ContractsGrantsInvoiceDetail) HashMap(java.util.HashMap)

Example 3 with ContractsGrantsInvoiceDetail

use of org.kuali.kfs.module.ar.businessobject.ContractsGrantsInvoiceDetail in project cu-kfs by CU-CommunityApps.

the class ContractsGrantsInvoiceDocumentServiceImpl method buildDirectCostInvoiceDetailFieldsMap.

private Map<String, Object> buildDirectCostInvoiceDetailFieldsMap(ContractsGrantsInvoiceDocument document) {
    Map<String, Object> directCostInvoiceDetailFieldsMap = new HashMap<>();
    if (CollectionUtils.isNotEmpty(document.getDirectCostInvoiceDetails())) {
        ContractsGrantsInvoiceDetail firstInvoiceDetail = document.getDirectCostInvoiceDetails().get(0);
        for (int i = 0; i < document.getDirectCostInvoiceDetails().size(); i++) {
            final String prefix = ArPropertyConstants.INVOICE_DETAIL + "[" + i + "].";
            directCostInvoiceDetailFieldsMap.put(prefix + ArPropertyConstants.CATEGORY, document.getDirectCostInvoiceDetails().get(i).getCostCategory().getCategoryName());
            directCostInvoiceDetailFieldsMap.putAll(buildInvoiceDetailFieldMap(prefix, document.getDirectCostInvoiceDetails().get(i)));
            directCostInvoiceDetailFieldsMap.put(prefix + ArPropertyConstants.AMOUNT_REMAINING_TO_BILL, firstInvoiceDetail.getAmountRemainingToBill());
        }
    }
    return directCostInvoiceDetailFieldsMap;
}
Also used : ContractsGrantsInvoiceDetail(org.kuali.kfs.module.ar.businessobject.ContractsGrantsInvoiceDetail) HashMap(java.util.HashMap)

Example 4 with ContractsGrantsInvoiceDetail

use of org.kuali.kfs.module.ar.businessobject.ContractsGrantsInvoiceDetail in project cu-kfs by CU-CommunityApps.

the class ContractsGrantsInvoiceDocumentServiceImpl method buildTotalDirectCostInvoiceDetailFieldsMap.

private Map<String, Object> buildTotalDirectCostInvoiceDetailFieldsMap(ContractsGrantsInvoiceDocument document) {
    Map<String, Object> totalDirectCostInvoiceDetailFieldsMap = new HashMap<>();
    ContractsGrantsInvoiceDetail totalDirectCostInvoiceDetail = document.getTotalDirectCostInvoiceDetail();
    if (ObjectUtils.isNotNull(totalDirectCostInvoiceDetail)) {
        final String prefix = ArPropertyConstants.DIRECT_COST_INVOICE_DETAIL + ".";
        totalDirectCostInvoiceDetailFieldsMap.putAll(buildInvoiceDetailFieldMap(prefix, totalDirectCostInvoiceDetail));
    }
    return totalDirectCostInvoiceDetailFieldsMap;
}
Also used : ContractsGrantsInvoiceDetail(org.kuali.kfs.module.ar.businessobject.ContractsGrantsInvoiceDetail) HashMap(java.util.HashMap)

Example 5 with ContractsGrantsInvoiceDetail

use of org.kuali.kfs.module.ar.businessobject.ContractsGrantsInvoiceDetail in project cu-kfs by CU-CommunityApps.

the class ContractsGrantsInvoiceDocumentServiceImpl method prorateBill.

@Override
public void prorateBill(ContractsGrantsInvoiceDocument contractsGrantsInvoiceDocument) {
    // Amount to be billed on this invoice
    KualiDecimal totalCost = new KualiDecimal(0);
    // must iterate through the invoice details because the user might have manually changed the value
    for (ContractsGrantsInvoiceDetail invD : contractsGrantsInvoiceDocument.getInvoiceDetails()) {
        totalCost = totalCost.add(invD.getInvoiceAmount());
    }
    // Total Billed so far
    KualiDecimal billedTotalCost = contractsGrantsInvoiceDocument.getInvoiceGeneralDetail().getTotalPreviouslyBilled();
    // AwardTotal
    KualiDecimal accountAwardTotal = contractsGrantsInvoiceDocument.getInvoiceGeneralDetail().getAwardTotal();
    if (accountAwardTotal.subtract(billedTotalCost).isGreaterEqual(new KualiDecimal(0))) {
        KualiDecimal amountEligibleForBilling = accountAwardTotal.subtract(billedTotalCost);
        if (totalCost.isGreaterThan(amountEligibleForBilling)) {
            // use BigDecimal because percentage should not have only a scale of 2, we need more for accuracy
            BigDecimal percentage = amountEligibleForBilling.bigDecimalValue().divide(totalCost.bigDecimalValue(), 10, RoundingMode.HALF_DOWN);
            // use to check if rounding has left a few cents off
            KualiDecimal amountToBill = new KualiDecimal(0);
            ContractsGrantsInvoiceDetail largestCostCategory = null;
            BigDecimal largestAmount = BigDecimal.ZERO;
            for (ContractsGrantsInvoiceDetail invD : contractsGrantsInvoiceDocument.getInvoiceDetails()) {
                BigDecimal newValue = invD.getInvoiceAmount().bigDecimalValue().multiply(percentage);
                KualiDecimal newKualiDecimalValue = new KualiDecimal(newValue.setScale(2, RoundingMode.DOWN));
                invD.setInvoiceAmount(newKualiDecimalValue);
                amountToBill = amountToBill.add(newKualiDecimalValue);
                if (newValue.compareTo(largestAmount) > 0) {
                    largestAmount = newKualiDecimalValue.bigDecimalValue();
                    largestCostCategory = invD;
                }
            }
            if (!amountToBill.equals(amountEligibleForBilling)) {
                KualiDecimal remaining = amountEligibleForBilling.subtract(amountToBill);
                if (ObjectUtils.isNull(largestCostCategory) && CollectionUtils.isNotEmpty(contractsGrantsInvoiceDocument.getInvoiceDetails())) {
                    largestCostCategory = contractsGrantsInvoiceDocument.getInvoiceDetails().get(0);
                }
                if (ObjectUtils.isNotNull(largestCostCategory)) {
                    largestCostCategory.setInvoiceAmount(largestCostCategory.getInvoiceAmount().add(remaining));
                }
            }
            recalculateTotalAmountBilledToDate(contractsGrantsInvoiceDocument);
        }
    }
}
Also used : ContractsGrantsInvoiceDetail(org.kuali.kfs.module.ar.businessobject.ContractsGrantsInvoiceDetail) AbstractKualiDecimal(org.kuali.kfs.core.api.util.type.AbstractKualiDecimal) KualiDecimal(org.kuali.kfs.core.api.util.type.KualiDecimal) BigDecimal(java.math.BigDecimal)

Aggregations

ContractsGrantsInvoiceDetail (org.kuali.kfs.module.ar.businessobject.ContractsGrantsInvoiceDetail)13 KualiDecimal (org.kuali.kfs.core.api.util.type.KualiDecimal)8 HashMap (java.util.HashMap)7 InvoiceDetailAccountObjectCode (org.kuali.kfs.module.ar.businessobject.InvoiceDetailAccountObjectCode)5 List (java.util.List)4 AbstractKualiDecimal (org.kuali.kfs.core.api.util.type.AbstractKualiDecimal)4 AwardExtendedAttribute (edu.cornell.kfs.module.cg.businessobject.AwardExtendedAttribute)3 BigDecimal (java.math.BigDecimal)3 ArrayList (java.util.ArrayList)3 InvoiceAccountDetail (org.kuali.kfs.module.ar.businessobject.InvoiceAccountDetail)3 Award (org.kuali.kfs.module.cg.businessobject.Award)3 Date (java.sql.Date)2 ContractsAndGrantsBillingAward (org.kuali.kfs.integration.cg.ContractsAndGrantsBillingAward)2 InvoiceAddressDetail (org.kuali.kfs.module.ar.businessobject.InvoiceAddressDetail)2 InvoiceGeneralDetail (org.kuali.kfs.module.ar.businessobject.InvoiceGeneralDetail)2 InvoiceMilestone (org.kuali.kfs.module.ar.businessobject.InvoiceMilestone)2 ContractsGrantsInvoiceDocument (org.kuali.kfs.module.ar.document.ContractsGrantsInvoiceDocument)2 CuArParameterKeyConstants (edu.cornell.kfs.module.ar.CuArParameterKeyConstants)1 CuArPropertyConstants (edu.cornell.kfs.module.ar.CuArPropertyConstants)1 CustomerExtendedAttribute (edu.cornell.kfs.module.ar.businessobject.CustomerExtendedAttribute)1