Search in sources :

Example 1 with PurchasingAccountsPayableDocumentBase

use of org.kuali.kfs.module.purap.document.PurchasingAccountsPayableDocumentBase in project cu-kfs by CU-CommunityApps.

the class PurchasingActionBase method doDistribution.

/**
 * Distribute accounting line(s) to the item(s). Does not distribute the accounting line(s) to an item if there are already
 * accounting lines associated with that item, if the item is a below-the-line item and has no unit cost, or if the item is
 * inactive. Distribute commodity code to the item(s). Does not distribute the commodity code to an item if the item is not
 * above the line item, is inactive or if the commodity code fails the validation (i.e. inactive commodity code or non existence
 * commodity code).
 *
 * @param mapping An ActionMapping
 * @param form An ActionForm
 * @param request The HttpServletRequest
 * @param response The HttpServletResponse
 * @return An ActionForward
 * @throws Exception
 */
public ActionForward doDistribution(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
    PurchasingFormBase purchasingForm = (PurchasingFormBase) form;
    boolean needToDistributeCommodityCode = false;
    if (StringUtils.isNotBlank(purchasingForm.getDistributePurchasingCommodityCode())) {
        // Do the logic for distributing purchasing commodity code to all the items.
        needToDistributeCommodityCode = true;
    }
    boolean needToDistributeAccount = false;
    List<PurApAccountingLine> distributionsourceAccountingLines = purchasingForm.getAccountDistributionsourceAccountingLines();
    if (distributionsourceAccountingLines.size() > 0) {
        needToDistributeAccount = true;
    }
    if (needToDistributeAccount || needToDistributeCommodityCode) {
        PurchasingAccountsPayableDocumentBase purApDocument = (PurchasingAccountsPayableDocumentBase) purchasingForm.getDocument();
        boolean institutionNeedsDistributeAccountValidation = SpringContext.getBean(ParameterService.class).getParameterValueAsBoolean(KfsParameterConstants.PURCHASING_DOCUMENT.class, PurapParameterConstants.VALIDATE_ACCOUNT_DISTRIBUTION_IND);
        boolean foundAccountDistributionError = false;
        boolean foundCommodityCodeDistributionError = false;
        boolean performedAccountDistribution = false;
        boolean performedCommodityCodeDistribution = false;
        // do check for account percents only if distribution method not equal to "P"
        if (!PurapConstants.AccountDistributionMethodCodes.PROPORTIONAL_CODE.equalsIgnoreCase(purApDocument.getAccountDistributionMethod())) {
            // the total percentage in the distribute account list does not equal 100 % then we should display error
            if (institutionNeedsDistributeAccountValidation && needToDistributeAccount && purchasingForm.getTotalPercentageOfAccountDistributionsourceAccountingLines().compareTo(new BigDecimal(100)) != 0) {
                GlobalVariables.getMessageMap().putError(PurapConstants.ACCOUNT_DISTRIBUTION_ERROR_KEY, PurapKeyConstants.ERROR_DISTRIBUTE_ACCOUNTS_NOT_100_PERCENT);
                foundAccountDistributionError = true;
            }
        }
        // there is a validation error in the accounts to distribute then we should display an error
        if (institutionNeedsDistributeAccountValidation && needToDistributeAccount && (validateDistributeAccounts(purchasingForm.getDocument(), distributionsourceAccountingLines) == false)) {
            foundAccountDistributionError = true;
        }
        for (PurApItem item : ((PurchasingAccountsPayableDocument) purchasingForm.getDocument()).getItems()) {
            boolean itemIsActive = true;
            if (item instanceof PurchaseOrderItem) {
                // if item is PO item... only validate active items
                itemIsActive = ((PurchaseOrderItem) item).isItemActiveIndicator();
            }
            if (needToDistributeCommodityCode) {
                // only the above the line items need the commodity code.
                if (item.getItemType().isLineItemIndicator() && StringUtils.isBlank(((PurchasingItemBase) item).getPurchasingCommodityCode()) && itemIsActive) {
                    // Ideally we should invoke rules to check whether the commodity code is valid (active, not restricted,
                    // not missing, etc), probably somewhere here or invoke the rule class from here.
                    boolean rulePassed = SpringContext.getBean(KualiRuleService.class).applyRules(new AttributedCommodityCodesForDistributionEvent("", purchasingForm.getDocument(), purchasingForm.getDistributePurchasingCommodityCode()));
                    if (rulePassed) {
                        ((PurchasingItemBase) item).setPurchasingCommodityCode(purchasingForm.getDistributePurchasingCommodityCode());
                        performedCommodityCodeDistribution = true;
                    } else {
                        foundCommodityCodeDistributionError = true;
                    }
                } else if (item.getItemType().isLineItemIndicator() && !StringUtils.isBlank(((PurchasingItemBase) item).getPurchasingCommodityCode()) && itemIsActive) {
                    // could not apply to line, as it wasn't blank
                    foundCommodityCodeDistributionError = true;
                }
            }
            if (needToDistributeAccount && !foundAccountDistributionError) {
                BigDecimal zero = new BigDecimal(0);
                // We should be distributing accounting lines to above the line items all the time;
                // but only to the below the line items when there is a unit cost.
                boolean unitCostNotZeroForBelowLineItems = item.getItemType().isLineItemIndicator() ? true : item.getItemUnitPrice() != null && zero.compareTo(item.getItemUnitPrice()) < 0;
                Document document = ((PurchasingFormBase) form).getDocument();
                Class clazz = document instanceof PurchaseOrderAmendmentDocument ? PurchaseOrderDocument.class : document.getClass();
                List<String> typesNotAllowingEdit = new ArrayList<String>(SpringContext.getBean(ParameterService.class).getParameterValuesAsString(clazz, PurapParameterConstants.PURAP_ITEM_TYPES_RESTRICTING_ACCOUNT_EDIT));
                boolean itemOnExcludeList = (typesNotAllowingEdit == null) ? false : typesNotAllowingEdit.contains(item.getItemTypeCode());
                if (item.getSourceAccountingLines().size() == 0 && unitCostNotZeroForBelowLineItems && !itemOnExcludeList && itemIsActive) {
                    for (PurApAccountingLine purApAccountingLine : distributionsourceAccountingLines) {
                        item.getSourceAccountingLines().add((PurApAccountingLine) ObjectUtils.deepCopy(purApAccountingLine));
                    }
                    performedAccountDistribution = true;
                }
            }
        }
        if ((needToDistributeCommodityCode && performedCommodityCodeDistribution && !foundCommodityCodeDistributionError) || (needToDistributeAccount && performedAccountDistribution && !foundAccountDistributionError)) {
            if (needToDistributeCommodityCode && !foundCommodityCodeDistributionError && performedCommodityCodeDistribution) {
                KNSGlobalVariables.getMessageList().add(PurapKeyConstants.PUR_COMMODITY_CODE_DISTRIBUTED);
                purchasingForm.setDistributePurchasingCommodityCode(null);
            }
            if (needToDistributeAccount && !foundAccountDistributionError && performedAccountDistribution) {
                KNSGlobalVariables.getMessageList().add(PurapKeyConstants.PURAP_GENERAL_ACCOUNTS_DISTRIBUTED);
                distributionsourceAccountingLines.clear();
            }
            purchasingForm.setHideDistributeAccounts(true);
        }
        if ((needToDistributeAccount && !performedAccountDistribution && foundAccountDistributionError)) {
            GlobalVariables.getMessageMap().putError(PurapConstants.ACCOUNT_DISTRIBUTION_ERROR_KEY, PurapKeyConstants.PURAP_GENERAL_NO_ITEMS_TO_DISTRIBUTE_TO, "account numbers");
        }
        if (needToDistributeCommodityCode && !performedCommodityCodeDistribution && foundCommodityCodeDistributionError) {
            GlobalVariables.getMessageMap().putError(PurapConstants.ITEM_PURCHASING_COMMODITY_CODE, PurapKeyConstants.PURAP_GENERAL_NO_ITEMS_TO_DISTRIBUTE_TO, "commodity codes");
        }
    } else {
        GlobalVariables.getMessageMap().putError(PurapConstants.ACCOUNT_DISTRIBUTION_ERROR_KEY, PurapKeyConstants.PURAP_GENERAL_NO_ACCOUNTS_TO_DISTRIBUTE);
    }
    return mapping.findForward(KFSConstants.MAPPING_BASIC);
}
Also used : PurchaseOrderAmendmentDocument(org.kuali.kfs.module.purap.document.PurchaseOrderAmendmentDocument) ParameterService(org.kuali.kfs.coreservice.framework.parameter.ParameterService) PurApItem(org.kuali.kfs.module.purap.businessobject.PurApItem) PurApAccountingLine(org.kuali.kfs.module.purap.businessobject.PurApAccountingLine) PurchasingAccountsPayableDocumentBase(org.kuali.kfs.module.purap.document.PurchasingAccountsPayableDocumentBase) KfsParameterConstants(org.kuali.kfs.sys.service.impl.KfsParameterConstants) ArrayList(java.util.ArrayList) PurApArrayList(org.kuali.kfs.module.purap.util.PurApArrayList) WorkflowDocument(org.kuali.rice.kew.api.WorkflowDocument) PurchaseOrderDocument(org.kuali.kfs.module.purap.document.PurchaseOrderDocument) RequisitionDocument(org.kuali.kfs.module.purap.document.RequisitionDocument) MaintenanceDocument(org.kuali.kfs.kns.document.MaintenanceDocument) PurchaseOrderAmendmentDocument(org.kuali.kfs.module.purap.document.PurchaseOrderAmendmentDocument) PurchasingDocument(org.kuali.kfs.module.purap.document.PurchasingDocument) Document(org.kuali.kfs.krad.document.Document) PurchasingAccountsPayableDocument(org.kuali.kfs.module.purap.document.PurchasingAccountsPayableDocument) BigDecimal(java.math.BigDecimal) PurchaseOrderItem(org.kuali.kfs.module.purap.businessobject.PurchaseOrderItem) KualiRuleService(org.kuali.kfs.krad.service.KualiRuleService) PurchasingItemBase(org.kuali.kfs.module.purap.businessobject.PurchasingItemBase) AttributedCommodityCodesForDistributionEvent(org.kuali.kfs.module.purap.document.validation.event.AttributedCommodityCodesForDistributionEvent) PurchasingAccountsPayableDocument(org.kuali.kfs.module.purap.document.PurchasingAccountsPayableDocument)

Example 2 with PurchasingAccountsPayableDocumentBase

use of org.kuali.kfs.module.purap.document.PurchasingAccountsPayableDocumentBase in project cu-kfs by CU-CommunityApps.

the class CuPurapAccountingServiceImpl method updateAccountAmounts.

/**
 * @see org.kuali.kfs.module.purap.service.PurapAccountingService#updateAccountAmounts(org.kuali.kfs.module.purap.document.PurchasingAccountsPayableDocument)
 */
@Override
public void updateAccountAmounts(PurchasingAccountsPayableDocument document) {
    PurchasingAccountsPayableDocumentBase purApDocument = (PurchasingAccountsPayableDocumentBase) document;
    String accountDistributionMethod = purApDocument.getAccountDistributionMethod();
    KualiRuleService kualiRuleService = SpringContext.getBean(KualiRuleService.class);
    WorkflowDocument workflowDocument = purApDocument.getDocumentHeader().getWorkflowDocument();
    Set<String> nodeNames = workflowDocument.getCurrentNodeNames();
    // don't update if past the AP review level
    if ((document instanceof PaymentRequestDocument) && purapService.isFullDocumentEntryCompleted(document)) {
        if (nodeNames.contains(PaymentRequestStatuses.NODE_PAYMENT_METHOD_REVIEW)) {
            // CU needs this update because the customization allows Treasury Manager to change unit/extended price and 'calculate'
            for (PurApItem item : document.getItems()) {
                updatePreqItemAccountAmountsOnly(item);
            }
        } else {
            convertMoneyToPercent((PaymentRequestDocument) document);
        }
        return;
    }
    document.fixItemReferences();
    // if distribution method is sequential and document is PREQ or VCM then...
    if (((document instanceof PaymentRequestDocument) || (document instanceof VendorCreditMemoDocument)) && PurapConstants.AccountDistributionMethodCodes.SEQUENTIAL_CODE.equalsIgnoreCase(accountDistributionMethod)) {
        if (document instanceof VendorCreditMemoDocument) {
            VendorCreditMemoDocument cmDocument = (VendorCreditMemoDocument) document;
            cmDocument.updateExtendedPriceOnItems();
            for (PurApItem item : document.getItems()) {
                for (PurApAccountingLine account : item.getSourceAccountingLines()) {
                    account.setAmount(KualiDecimal.ZERO);
                }
            }
        }
        // update the accounts amounts for PREQ and distribution method = sequential
        for (PurApItem item : document.getItems()) {
            updatePreqItemAccountAmounts(item);
        }
        return;
    }
    // if distribution method is proportional and document is PREQ or VCM then...
    if (((document instanceof PaymentRequestDocument) || (document instanceof VendorCreditMemoDocument)) && PurapConstants.AccountDistributionMethodCodes.PROPORTIONAL_CODE.equalsIgnoreCase(accountDistributionMethod)) {
        // update the accounts amounts for PREQ and distribution method = sequential
        if (document instanceof VendorCreditMemoDocument) {
            VendorCreditMemoDocument cmDocument = (VendorCreditMemoDocument) document;
            cmDocument.updateExtendedPriceOnItems();
            for (PurApItem item : document.getItems()) {
                for (PurApAccountingLine account : item.getSourceAccountingLines()) {
                    account.setAmount(KualiDecimal.ZERO);
                }
            }
        }
        for (PurApItem item : document.getItems()) {
            boolean rulePassed = true;
            // check any business rules
            rulePassed &= kualiRuleService.applyRules(new PurchasingAccountsPayableItemPreCalculateEvent(document, item));
            if (rulePassed) {
                updatePreqProportionalItemAccountAmounts(item);
            }
        }
        return;
    }
    // No recalculate if the account distribution method code is equal to "S" sequential ON REQ or POs..
    if (PurapConstants.AccountDistributionMethodCodes.SEQUENTIAL_CODE.equalsIgnoreCase(accountDistributionMethod)) {
        for (PurApItem item : document.getItems()) {
            boolean rulePassed = true;
            // check any business rules
            rulePassed &= kualiRuleService.applyRules(new PurchasingAccountsPayableItemPreCalculateEvent(document, item));
            return;
        }
    }
    // do recalculate only if the account distribution method code is not equal to "S" sequential method.
    if (!PurapConstants.AccountDistributionMethodCodes.SEQUENTIAL_CODE.equalsIgnoreCase(accountDistributionMethod)) {
        for (PurApItem item : document.getItems()) {
            boolean rulePassed = true;
            // check any business rules
            rulePassed &= kualiRuleService.applyRules(new PurchasingAccountsPayableItemPreCalculateEvent(document, item));
            if (rulePassed) {
                updateItemAccountAmounts(item);
            }
        }
    }
}
Also used : VendorCreditMemoDocument(org.kuali.kfs.module.purap.document.VendorCreditMemoDocument) KualiRuleService(org.kuali.kfs.krad.service.KualiRuleService) WorkflowDocument(org.kuali.rice.kew.api.WorkflowDocument) PurApItem(org.kuali.kfs.module.purap.businessobject.PurApItem) PurchasingAccountsPayableDocumentBase(org.kuali.kfs.module.purap.document.PurchasingAccountsPayableDocumentBase) PurApAccountingLine(org.kuali.kfs.module.purap.businessobject.PurApAccountingLine) PaymentRequestDocument(org.kuali.kfs.module.purap.document.PaymentRequestDocument) PurchasingAccountsPayableItemPreCalculateEvent(org.kuali.kfs.module.purap.document.validation.event.PurchasingAccountsPayableItemPreCalculateEvent)

Example 3 with PurchasingAccountsPayableDocumentBase

use of org.kuali.kfs.module.purap.document.PurchasingAccountsPayableDocumentBase in project cu-kfs by CU-CommunityApps.

the class CuRequisitionAssignToTradeInValidation method validate.

public boolean validate(AttributedDocumentEvent event) {
    // Initialize the valid and true, and get the requisition document.
    boolean foundTradeIn = false;
    boolean valid = true;
    PurchasingAccountsPayableDocumentBase purapDoc = (PurchasingAccountsPayableDocumentBase) event.getDocument();
    // First, get all the items from the requisition document.  For each of
    // the items, look for the ones that are assigned to a trade-in value.
    // For these trade-in items, validate that the trade-in line has a valid
    // description and amount.
    List<PurApItem> items = (List<PurApItem>) purapDoc.getItems();
    for (PurApItem item : items) {
        item.refreshReferenceObject(PurapPropertyConstants.ITEM_TYPE);
        if (item.getItemAssignedToTradeInIndicator()) {
            foundTradeIn = true;
            break;
        }
    }
    // Was a trade-in found for any of the above items?
    if (foundTradeIn) {
        // Get the trade-in item.
        PurApItem tradeInItem = purapDoc.getTradeInItem();
        if (tradeInItem != null) {
            if (StringUtils.isEmpty(tradeInItem.getItemDescription())) {
                String attributeLabel = dataDictionaryService.getDataDictionary().getBusinessObjectEntry(tradeInItem.getClass().getName()).getAttributeDefinition(PurapPropertyConstants.ITEM_DESCRIPTION).getLabel();
                tradeInItem.getItemLineNumber();
                GlobalVariables.getMessageMap().putError("document.item[" + getTradeInItemLineIndex(items) + "]." + PurapPropertyConstants.ITEM_DESCRIPTION, PurapKeyConstants.ERROR_ITEM_BELOW_THE_LINE, "The item description of " + tradeInItem.getItemType().getItemTypeDescription(), "empty");
                valid = false;
            } else if (ObjectUtils.isNull(tradeInItem.getItemUnitPrice())) {
                String attributeLabel = dataDictionaryService.getDataDictionary().getBusinessObjectEntry(tradeInItem.getClass().getName()).getAttributeDefinition(PurapPropertyConstants.ITEM_UNIT_PRICE).getLabel();
                GlobalVariables.getMessageMap().putError("document.item[" + getTradeInItemLineIndex(items) + "]." + PurapPropertyConstants.ITEM_UNIT_PRICE, PurapKeyConstants.ERROR_ITEM_BELOW_THE_LINE, tradeInItem.getItemType().getItemTypeDescription(), "zero");
                valid = false;
            }
        }
    }
    return valid;
}
Also used : PurApItem(org.kuali.kfs.module.purap.businessobject.PurApItem) PurchasingAccountsPayableDocumentBase(org.kuali.kfs.module.purap.document.PurchasingAccountsPayableDocumentBase) List(java.util.List)

Aggregations

PurApItem (org.kuali.kfs.module.purap.businessobject.PurApItem)3 PurchasingAccountsPayableDocumentBase (org.kuali.kfs.module.purap.document.PurchasingAccountsPayableDocumentBase)3 KualiRuleService (org.kuali.kfs.krad.service.KualiRuleService)2 PurApAccountingLine (org.kuali.kfs.module.purap.businessobject.PurApAccountingLine)2 WorkflowDocument (org.kuali.rice.kew.api.WorkflowDocument)2 BigDecimal (java.math.BigDecimal)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 ParameterService (org.kuali.kfs.coreservice.framework.parameter.ParameterService)1 MaintenanceDocument (org.kuali.kfs.kns.document.MaintenanceDocument)1 Document (org.kuali.kfs.krad.document.Document)1 PurchaseOrderItem (org.kuali.kfs.module.purap.businessobject.PurchaseOrderItem)1 PurchasingItemBase (org.kuali.kfs.module.purap.businessobject.PurchasingItemBase)1 PaymentRequestDocument (org.kuali.kfs.module.purap.document.PaymentRequestDocument)1 PurchaseOrderAmendmentDocument (org.kuali.kfs.module.purap.document.PurchaseOrderAmendmentDocument)1 PurchaseOrderDocument (org.kuali.kfs.module.purap.document.PurchaseOrderDocument)1 PurchasingAccountsPayableDocument (org.kuali.kfs.module.purap.document.PurchasingAccountsPayableDocument)1 PurchasingDocument (org.kuali.kfs.module.purap.document.PurchasingDocument)1 RequisitionDocument (org.kuali.kfs.module.purap.document.RequisitionDocument)1 VendorCreditMemoDocument (org.kuali.kfs.module.purap.document.VendorCreditMemoDocument)1