fun generateReqsFromCancelledPOItems( dctx: DispatchContext, context: Map<String?, Any?> ): Map<String?, Any?>? { val delegator: Delegator = dctx.getDelegator() val dispatcher: LocalDispatcher = dctx.getDispatcher() val userLogin: GenericValue? = context["userLogin"] as GenericValue? val locale: Locale? = context["locale"] as Locale? val orderId = context["orderId"] as String? val facilityId = context["facilityId"] as String? try { val orderHeader: GenericValue = EntityQuery.use(delegator).from("OrderHeader").where("orderId", orderId).queryOne() if (UtilValidate.isEmpty(orderHeader)) { val errorMessage: String = UtilProperties.getMessage( resource_error, "OrderErrorOrderIdNotFound", UtilMisc.toMap("orderId", orderId), Locale ) Debug.logError(errorMessage, module) return ServiceUtil.returnError(errorMessage) } if ("PURCHASE_ORDER" != orderHeader.getString("orderTypeId")) { val errorMessage: String = UtilProperties.getMessage( resource_error, "ProductErrorOrderNotPurchaseOrder", UtilMisc.toMap("orderId", orderId), Locale ) Debug.logError(errorMessage, module) return ServiceUtil.returnError(errorMessage) } val productRequirementQuantities: MutableMap<String, Any> = HashMap() val orderItems: List<GenericValue> = orderHeader.getRelated("OrderItem", null, null, false) for (orderItem in orderItems) { if ("PRODUCT_ORDER_ITEM" != orderItem.getString("orderItemTypeId")) continue var orderItemCancelQuantity: BigDecimal = BigDecimal.ZERO if (!UtilValidate.isEmpty(orderItem.get("cancelQuantity"))) {orderItemCancelQuantity = orderItem.getBigDecimal("cancelQuantity")} if (orderItemCancelQuantity.compareTo(BigDecimal.ZERO) <= 0) continue val productId: String = orderItem.getString("productId") if (productRequirementQuantities.containsKey(productId)) { orderItemCancelQuantity = orderItemCancelQuantity.add(productRequirementQuantities[productId] as BigDecimal?) } productRequirementQuantities[productId] = orderItemCancelQuantity } for (productId in productRequirementQuantities.keys) { val requiredQuantity: BigDecimal? = productRequirementQuantities[productId] as BigDecimal? val createRequirementResult: Map<String?, Any?> = dispatcher.runSync( "createRequirement", UtilMisc.< String, Object > toMap<String, Any>( "requirementTypeId", "PRODUCT_REQUIREMENT", "facilityId", facilityId, "productId", productId, "quantity", requiredQuantity, "userLogin", userLogin ) ) if (ServiceUtil.isError(createRequirementResult)) return createRequirementResult } } catch (e: GenericEntityException) { Debug.logError(e, module) return ServiceUtil.returnError(e.getMessage()) } catch (se: GenericServiceException) { Debug.logError(se, module) return ServiceUtil.returnError(se.getMessage()) } return ServiceUtil.returnSuccess() }
Generates a product requirement for the total cancelled quantity over all order items for each product