Search in sources :

Example 1 with CuPurapService

use of edu.cornell.kfs.module.purap.document.service.CuPurapService in project cu-kfs by CU-CommunityApps.

the class CuRequisitionServiceImpl method checkAutomaticPurchaseOrderRules.

/**
 * Checks the rule for Automatic Purchase Order eligibility of the requisition and return a String containing the
 * reason why the requisition was not eligible to become an APO if it was not eligible, or return an empty String
 * if the requisition is eligible to become an APO
 *
 * @param requisition the requisition document to be checked for APO eligibility.
 * @return String containing the reason why the requisition was not eligible to become an APO if it was not
 *         eligible, or an empty String if the requisition is eligible to become an APO.
 */
@Override
protected String checkAutomaticPurchaseOrderRules(RequisitionDocument requisition) {
    String requisitionSource = requisition.getRequisitionSourceCode();
    KualiDecimal reqTotal = requisition.getTotalDollarAmount();
    KualiDecimal apoLimit = ((CuPurapService) purapService).getApoLimit(requisition);
    requisition.setOrganizationAutomaticPurchaseOrderLimit(apoLimit);
    if (LOG.isDebugEnabled()) {
        LOG.debug("isAPO() reqId = " + requisition.getPurapDocumentIdentifier() + "; apoLimit = " + apoLimit + "; reqTotal = " + reqTotal);
    }
    if (apoLimit == null) {
        return "APO limit is empty.";
    } else {
        if (reqTotal.compareTo(apoLimit) == 1) {
            return "Requisition total is greater than the APO limit.";
        }
    }
    if (reqTotal.compareTo(KualiDecimal.ZERO) <= 0) {
        return "Requisition total is not greater than zero.";
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("isAPO() vendor #" + requisition.getVendorHeaderGeneratedIdentifier() + "-" + requisition.getVendorDetailAssignedIdentifier());
    }
    if (requisition.getVendorHeaderGeneratedIdentifier() == null || requisition.getVendorDetailAssignedIdentifier() == null) {
        return "Vendor was not selected from the vendor database.";
    } else {
        VendorDetail vendorDetail = vendorService.getVendorDetail(requisition.getVendorHeaderGeneratedIdentifier(), requisition.getVendorDetailAssignedIdentifier());
        if (vendorDetail == null) {
            return "Error retrieving vendor from the database.";
        }
        if (StringUtils.isBlank(requisition.getVendorLine1Address()) || StringUtils.isBlank(requisition.getVendorCityName()) || StringUtils.isBlank(requisition.getVendorCountryCode())) {
            return "Requisition does not have all of the vendor address fields that are required for Purchase " + "Order.";
        }
        requisition.setVendorRestrictedIndicator(vendorDetail.getVendorRestrictedIndicator());
        if (requisition.getVendorRestrictedIndicator() != null && requisition.getVendorRestrictedIndicator()) {
            return "Selected vendor is marked as restricted.";
        }
        if (vendorDetail.isVendorDebarred()) {
            return "Selected vendor is marked as a debarred vendor";
        }
        requisition.setVendorDetail(vendorDetail);
        if (!PurapConstants.RequisitionSources.B2B.equals(requisitionSource) && ObjectUtils.isNull(requisition.getVendorContractGeneratedIdentifier())) {
            Person initiator = personService.getPerson(requisition.getDocumentHeader().getWorkflowDocument().getInitiatorPrincipalId());
            VendorContract b2bContract = vendorService.getVendorB2BContract(vendorDetail, initiator.getCampusCode());
            if (b2bContract != null) {
                return "Standard requisition with no contract selected but a B2B contract exists for the " + "selected vendor.";
            }
        }
        // if the vendor is selected through vendor contract is selected
        if (StringUtils.isNotBlank(requisition.getVendorContractName())) {
            // CU mod: KFSUPGRADE-926
            boolean routeToCM = parameterService.getParameterValueAsBoolean(RequisitionDocument.class, CUPurapParameterConstants.ROUTE_REQS_WITH_EXPIRED_CONTRACT_TO_CM, Boolean.FALSE);
            if (routeToCM && vendorService.isVendorContractExpired(requisition, requisition.getVendorContractGeneratedIdentifier(), vendorDetail)) {
                return "Contracted Vendor used where the contract end date is expired.";
            }
        }
    }
    // if vendor address isn't complete, no APO
    if (StringUtils.isBlank(requisition.getVendorLine1Address()) || StringUtils.isBlank(requisition.getVendorCityName()) || StringUtils.isBlank(requisition.getVendorCountryCode()) || !postalCodeValidationService.validateAddress(requisition.getVendorCountryCode(), requisition.getVendorStateCode(), requisition.getVendorPostalCode(), "", "")) {
        return "Requisition does not contain a complete vendor address";
    }
    // These are needed for commodity codes. They are put in here so that we don't have to loop through items too
    // many times.
    String purchaseOrderRequiresCommodityCode = parameterService.getParameterValueAsString(PurchaseOrderDocument.class, PurapRuleConstants.ITEMS_REQUIRE_COMMODITY_CODE_IND);
    boolean commodityCodeRequired = "Y".equals(purchaseOrderRequiresCommodityCode);
    for (Object anItem : requisition.getItems()) {
        RequisitionItem item = (RequisitionItem) anItem;
        if (item.isItemRestrictedIndicator()) {
            return "Requisition contains an item that is marked as restricted.";
        }
        // We only need to check the commodity codes if this is an above the line item.
        if (item.getItemType().isLineItemIndicator()) {
            List<VendorCommodityCode> vendorCommodityCodes = commodityCodeRequired ? requisition.getVendorDetail().getVendorCommodities() : null;
            String commodityCodesReason = checkAPORulesPerItemForCommodityCodes(item, vendorCommodityCodes, commodityCodeRequired);
            if (StringUtils.isNotBlank(commodityCodesReason)) {
                return commodityCodesReason;
            }
        }
        if (PurapConstants.ItemTypeCodes.ITEM_TYPE_ORDER_DISCOUNT_CODE.equals(item.getItemType().getItemTypeCode()) || PurapConstants.ItemTypeCodes.ITEM_TYPE_TRADE_IN_CODE.equals(item.getItemType().getItemTypeCode())) {
            if (item.getItemUnitPrice() != null && BigDecimal.ZERO.compareTo(item.getItemUnitPrice()) != 0) {
                // discount or trade-in item has unit price that is not empty or zero
                return "Requisition contains a " + item.getItemType().getItemTypeDescription() + " item, so it does not qualify as an APO.";
            }
        }
    // //Base code logic check not in CU mod
    // if (!PurapConstants.RequisitionSources.B2B.equals(requisitionSource)) {
    // for (PurApAccountingLine accountingLine : item.getSourceAccountingLines()) {
    // if (capitalAssetManagementModuleService.doesAccountingLineFailAutomaticPurchaseOrderRules(
    // accountingLine)) {
    // return "Requisition contains accounting line with capital object level";
    // }
    // }
    // }
    }
    if (StringUtils.isNotEmpty(requisition.getRecurringPaymentTypeCode())) {
        return "Payment type is marked as recurring.";
    }
    if (requisition.getPurchaseOrderTotalLimit() != null && KualiDecimal.ZERO.compareTo(requisition.getPurchaseOrderTotalLimit()) != 0) {
        LOG.debug("isAPO() po total limit is not null and not equal to zero; return false.");
        return "The 'PO not to exceed' amount has been entered.";
    }
    if (StringUtils.isNotEmpty(requisition.getAlternate1VendorName()) || StringUtils.isNotEmpty(requisition.getAlternate2VendorName()) || StringUtils.isNotEmpty(requisition.getAlternate3VendorName()) || StringUtils.isNotEmpty(requisition.getAlternate4VendorName()) || StringUtils.isNotEmpty(requisition.getAlternate5VendorName())) {
        LOG.debug("isAPO() alternate vendor name exists; return false.");
        return "Requisition contains additional suggested vendor names.";
    }
    if (requisition.isPostingYearNext() && !purapService.isTodayWithinApoAllowedRange()) {
        return "Requisition is set to encumber next fiscal year and approval is not within APO allowed date range.";
    }
    return "";
}
Also used : VendorDetail(org.kuali.kfs.vnd.businessobject.VendorDetail) CuPurapService(edu.cornell.kfs.module.purap.document.service.CuPurapService) RequisitionItem(org.kuali.kfs.module.purap.businessobject.RequisitionItem) VendorContract(org.kuali.kfs.vnd.businessobject.VendorContract) KualiDecimal(org.kuali.kfs.core.api.util.type.KualiDecimal) Person(org.kuali.kfs.kim.api.identity.Person) VendorCommodityCode(org.kuali.kfs.vnd.businessobject.VendorCommodityCode)

Aggregations

CuPurapService (edu.cornell.kfs.module.purap.document.service.CuPurapService)1 KualiDecimal (org.kuali.kfs.core.api.util.type.KualiDecimal)1 Person (org.kuali.kfs.kim.api.identity.Person)1 RequisitionItem (org.kuali.kfs.module.purap.businessobject.RequisitionItem)1 VendorCommodityCode (org.kuali.kfs.vnd.businessobject.VendorCommodityCode)1 VendorContract (org.kuali.kfs.vnd.businessobject.VendorContract)1 VendorDetail (org.kuali.kfs.vnd.businessobject.VendorDetail)1