Search in sources :

Example 1 with CreditMemoItem

use of org.kuali.kfs.module.purap.businessobject.CreditMemoItem in project cu-kfs by CU-CommunityApps.

the class CuPendingTransactionServiceImpl method getCreditMemoEncumbrance.

/**
 * Re-encumber the Encumbrance on a PO based on values in a PREQ. This is used when a PREQ is cancelled. Note: This modifies the
 * encumbrance values on the PO and saves the PO
 *
 * @param cm Credit Memo document
 * @param po Purchase Order document modify encumbrances
 * @return List of accounting lines to use to create the pending general ledger entries
 */
protected List<SourceAccountingLine> getCreditMemoEncumbrance(VendorCreditMemoDocument cm, PurchaseOrderDocument po) {
    LOG.debug("getCreditMemoEncumbrance() started");
    if (ObjectUtils.isNull(po)) {
        return null;
    }
    LOG.debug("getCreditMemoEncumbrance() Receiving items back from vendor (cancelled CM)");
    Map encumbranceAccountMap = new HashMap();
    // Get each item one by one
    for (Iterator items = cm.getItems().iterator(); items.hasNext(); ) {
        CreditMemoItem cmItem = (CreditMemoItem) items.next();
        PurchaseOrderItem poItem = getPoItem(po, cmItem.getItemLineNumber(), cmItem.getItemType());
        // Amount to disencumber for this item
        KualiDecimal itemDisEncumber = null;
        // Amount to alter the invoicedAmt on the PO item
        KualiDecimal itemAlterInvoiceAmt = null;
        String logItmNbr = "Item # " + cmItem.getItemLineNumber();
        LOG.debug("getCreditMemoEncumbrance() " + logItmNbr);
        final KualiDecimal cmItemTotalAmount = (cmItem.getTotalAmount() == null) ? KualiDecimal.ZERO : cmItem.getTotalAmount();
        ;
        // If there isn't a PO item or the total amount is 0, we don't need encumbrances
        if ((poItem == null) || (cmItemTotalAmount == null) || (cmItemTotalAmount.doubleValue() == 0)) {
            LOG.debug("getCreditMemoEncumbrance() " + logItmNbr + " No encumbrances required");
        } else {
            LOG.debug("getCreditMemoEncumbrance() " + logItmNbr + " Calculate encumbrance GL entries");
            // Do we calculate the encumbrance amount based on quantity or amount?
            if (poItem.getItemType().isQuantityBasedGeneralLedgerIndicator()) {
                LOG.debug("getCreditMemoEncumbrance() " + logItmNbr + " Calculate encumbrance based on quantity");
                // Do encumbrance calculations based on quantity
                KualiDecimal cmQuantity = cmItem.getItemQuantity() == null ? ZERO : cmItem.getItemQuantity();
                KualiDecimal encumbranceQuantityChange = calculateQuantityChange(poItem, cmQuantity);
                LOG.debug("getCreditMemoEncumbrance() " + logItmNbr + " encumbranceQtyChange " + encumbranceQuantityChange + " outstandingEncumberedQty " + poItem.getItemOutstandingEncumberedQuantity() + " invoicedTotalQuantity " + poItem.getItemInvoicedTotalQuantity());
                itemDisEncumber = encumbranceQuantityChange.multiply(new KualiDecimal(poItem.getItemUnitPrice()));
                // add tax for encumbrance
                KualiDecimal itemTaxAmount = poItem.getItemTaxAmount() == null ? ZERO : poItem.getItemTaxAmount();
                KualiDecimal encumbranceTaxAmount = encumbranceQuantityChange.divide(poItem.getItemQuantity()).multiply(itemTaxAmount);
                itemDisEncumber = itemDisEncumber.add(encumbranceTaxAmount);
                itemAlterInvoiceAmt = cmItemTotalAmount;
                itemAlterInvoiceAmt = itemAlterInvoiceAmt.multiply(new KualiDecimal("-1"));
            } else {
                LOG.debug("getCreditMemoEncumbrance() " + logItmNbr + " Calculate encumbrance based on amount");
                // Do encumbrance calculations based on amount only
                // Decrease encumbrance
                itemDisEncumber = cmItemTotalAmount.multiply(new KualiDecimal("-1"));
                if (poItem.getItemOutstandingEncumberedAmount().add(itemDisEncumber).doubleValue() < 0) {
                    LOG.debug("getCreditMemoEncumbrance() Cancel overflow");
                    itemDisEncumber = poItem.getItemOutstandingEncumberedAmount();
                }
                itemAlterInvoiceAmt = itemDisEncumber;
            }
            // alter the encumbrance based on what was originally encumbered
            poItem.setItemOutstandingEncumberedAmount(poItem.getItemOutstandingEncumberedAmount().add(itemDisEncumber));
            // alter the invoiced amt based on what was actually credited on the credit memo
            poItem.setItemInvoicedTotalAmount(poItem.getItemInvoicedTotalAmount().subtract(itemAlterInvoiceAmt));
            if (poItem.getItemInvoicedTotalAmount().compareTo(ZERO) < 0) {
                poItem.setItemInvoicedTotalAmount(ZERO);
            }
            LOG.debug("getCreditMemoEncumbrance() " + logItmNbr + " Amount to disencumber: " + itemDisEncumber);
            // Sort accounts
            Collections.sort((List) poItem.getSourceAccountingLines());
            // make the list of accounts for the disencumbrance entry
            PurchaseOrderAccount lastAccount = null;
            KualiDecimal accountTotal = ZERO;
            // Collections.sort((List)poItem.getSourceAccountingLines());
            for (Iterator accountIter = poItem.getSourceAccountingLines().iterator(); accountIter.hasNext(); ) {
                PurchaseOrderAccount account = (PurchaseOrderAccount) accountIter.next();
                if (!account.isEmpty()) {
                    KualiDecimal encumbranceAmount = null;
                    SourceAccountingLine acctString = account.generateSourceAccountingLine();
                    // amount = item disencumber * account percent / 100
                    encumbranceAmount = itemDisEncumber.multiply(new KualiDecimal(account.getAccountLinePercent().toString())).divide(new KualiDecimal(100));
                    account.setItemAccountOutstandingEncumbranceAmount(account.getItemAccountOutstandingEncumbranceAmount().add(encumbranceAmount));
                    // For rounding check at the end
                    accountTotal = accountTotal.add(encumbranceAmount);
                    lastAccount = account;
                    LOG.debug("getCreditMemoEncumbrance() " + logItmNbr + " " + acctString + " = " + encumbranceAmount);
                    if (encumbranceAccountMap.get(acctString) == null) {
                        encumbranceAccountMap.put(acctString, encumbranceAmount);
                    } else {
                        KualiDecimal amt = (KualiDecimal) encumbranceAccountMap.get(acctString);
                        encumbranceAccountMap.put(acctString, amt.add(encumbranceAmount));
                    }
                }
            }
            // account for rounding by adjusting last account as needed
            if (lastAccount != null) {
                KualiDecimal difference = itemDisEncumber.subtract(accountTotal);
                LOG.debug("getCreditMemoEncumbrance() difference: " + logItmNbr + " " + difference);
                SourceAccountingLine acctString = lastAccount.generateSourceAccountingLine();
                KualiDecimal amount = (KualiDecimal) encumbranceAccountMap.get(acctString);
                if (amount == null) {
                    encumbranceAccountMap.put(acctString, difference);
                } else {
                    encumbranceAccountMap.put(acctString, amount.add(difference));
                }
                lastAccount.setItemAccountOutstandingEncumbranceAmount(lastAccount.getItemAccountOutstandingEncumbranceAmount().add(difference));
            }
        }
    }
    List<SourceAccountingLine> encumbranceAccounts = new ArrayList();
    for (Iterator iter = encumbranceAccountMap.keySet().iterator(); iter.hasNext(); ) {
        SourceAccountingLine acctString = (SourceAccountingLine) iter.next();
        KualiDecimal amount = (KualiDecimal) encumbranceAccountMap.get(acctString);
        if (amount.doubleValue() != 0) {
            acctString.setAmount(amount);
            encumbranceAccounts.add(acctString);
        }
    }
    return encumbranceAccounts;
}
Also used : PurchaseOrderItem(org.kuali.kfs.module.purap.businessobject.PurchaseOrderItem) HashMap(java.util.HashMap) CreditMemoItem(org.kuali.kfs.module.purap.businessobject.CreditMemoItem) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) KualiDecimal(org.kuali.rice.core.api.util.type.KualiDecimal) SourceAccountingLine(org.kuali.kfs.sys.businessobject.SourceAccountingLine) Map(java.util.Map) HashMap(java.util.HashMap) PurchaseOrderAccount(org.kuali.kfs.module.purap.businessobject.PurchaseOrderAccount)

Aggregations

ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 Iterator (java.util.Iterator)1 Map (java.util.Map)1 CreditMemoItem (org.kuali.kfs.module.purap.businessobject.CreditMemoItem)1 PurchaseOrderAccount (org.kuali.kfs.module.purap.businessobject.PurchaseOrderAccount)1 PurchaseOrderItem (org.kuali.kfs.module.purap.businessobject.PurchaseOrderItem)1 SourceAccountingLine (org.kuali.kfs.sys.businessobject.SourceAccountingLine)1 KualiDecimal (org.kuali.rice.core.api.util.type.KualiDecimal)1