use of org.apache.ofbiz.manufacturing.bom.BOMTree in project ofbiz-framework by apache.
the class ProductionRunServices method createProductionRunsForOrder.
public static Map<String, Object> createProductionRunsForOrder(DispatchContext dctx, Map<String, ? extends Object> context) {
Map<String, Object> result = new HashMap<String, Object>();
Delegator delegator = dctx.getDelegator();
LocalDispatcher dispatcher = dctx.getDispatcher();
GenericValue userLogin = (GenericValue) context.get("userLogin");
String orderId = (String) context.get("orderId");
String shipmentId = (String) context.get("shipmentId");
String orderItemSeqId = (String) context.get("orderItemSeqId");
String shipGroupSeqId = (String) context.get("shipGroupSeqId");
BigDecimal quantity = (BigDecimal) context.get("quantity");
String fromDateStr = (String) context.get("fromDate");
Locale locale = (Locale) context.get("locale");
Date fromDate = null;
if (UtilValidate.isNotEmpty(fromDateStr)) {
try {
fromDate = Timestamp.valueOf(fromDateStr);
} catch (Exception e) {
}
}
if (fromDate == null) {
fromDate = new Date();
}
List<GenericValue> orderItems = null;
if (orderItemSeqId != null) {
try {
GenericValue orderItem = null;
if (UtilValidate.isNotEmpty(shipGroupSeqId)) {
orderItem = EntityQuery.use(delegator).from("OrderItemShipGroupAssoc").where("orderId", orderId, "orderItemSeqId", orderItemSeqId, "shipGroupSeqId", shipGroupSeqId).queryOne();
} else {
orderItem = EntityQuery.use(delegator).from("OrderItem").where("orderId", orderId, "orderItemSeqId", orderItemSeqId).queryOne();
}
if (orderItem == null) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceOrder, "OrderErrorOrderItemNotFound", UtilMisc.toMap("orderId", orderId, "orderItemSeqId", ""), locale));
}
if (quantity != null) {
orderItem.set("quantity", quantity);
}
orderItems = UtilMisc.toList(orderItem);
} catch (GenericEntityException gee) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceOrder, "OrderProblemsReadingOrderItemInformation", UtilMisc.toMap("errorString", gee.getMessage()), locale));
}
} else {
try {
orderItems = EntityQuery.use(delegator).from("OrderItem").where("orderId", orderId).queryList();
if (orderItems == null) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceOrder, "OrderErrorOrderItemNotFound", UtilMisc.toMap("orderId", orderId, "orderItemSeqId", ""), locale));
}
} catch (GenericEntityException gee) {
return ServiceUtil.returnError(UtilProperties.getMessage(resourceOrder, "OrderProblemsReadingOrderItemInformation", UtilMisc.toMap("errorString", gee.getMessage()), locale));
}
}
List<String> productionRuns = new LinkedList<String>();
for (int i = 0; i < orderItems.size(); i++) {
GenericValue orderItemOrShipGroupAssoc = orderItems.get(i);
String productId = null;
BigDecimal amount = null;
GenericValue orderItem = null;
if ("OrderItemShipGroupAssoc".equals(orderItemOrShipGroupAssoc.getEntityName())) {
try {
orderItem = orderItemOrShipGroupAssoc.getRelatedOne("OrderItem", false);
} catch (GenericEntityException gee) {
Debug.logInfo("Unable to find order item for " + orderItemOrShipGroupAssoc, module);
}
} else {
orderItem = orderItemOrShipGroupAssoc;
}
if (orderItem == null || orderItem.get("productId") == null) {
continue;
} else {
productId = orderItem.getString("productId");
}
if (orderItem.get("selectedAmount") != null) {
amount = orderItem.getBigDecimal("selectedAmount");
}
if (amount == null) {
amount = BigDecimal.ZERO;
}
if (orderItemOrShipGroupAssoc.get("quantity") != null) {
quantity = orderItemOrShipGroupAssoc.getBigDecimal("quantity");
} else {
continue;
}
try {
List<GenericValue> existingProductionRuns = null;
if (UtilValidate.isNotEmpty(shipGroupSeqId)) {
existingProductionRuns = EntityQuery.use(delegator).from("WorkAndOrderItemFulfillment").where(EntityCondition.makeCondition("orderId", EntityOperator.EQUALS, orderItemOrShipGroupAssoc.get("orderId")), EntityCondition.makeCondition("orderItemSeqId", EntityOperator.EQUALS, orderItemOrShipGroupAssoc.get("orderItemSeqId")), EntityCondition.makeCondition("shipGroupSeqId", EntityOperator.EQUALS, orderItemOrShipGroupAssoc.get("shipGroupSeqId")), EntityCondition.makeCondition("currentStatusId", EntityOperator.NOT_EQUAL, "PRUN_CANCELLED")).cache().queryList();
} else {
existingProductionRuns = EntityQuery.use(delegator).from("WorkAndOrderItemFulfillment").where(EntityCondition.makeCondition("orderId", EntityOperator.EQUALS, orderItemOrShipGroupAssoc.get("orderId")), EntityCondition.makeCondition("orderItemSeqId", EntityOperator.EQUALS, orderItemOrShipGroupAssoc.get("orderItemSeqId")), EntityCondition.makeCondition("currentStatusId", EntityOperator.NOT_EQUAL, "PRUN_CANCELLED")).cache().queryList();
}
if (UtilValidate.isNotEmpty(existingProductionRuns)) {
Debug.logWarning("Production Run for order item [" + orderItemOrShipGroupAssoc.getString("orderId") + "/" + orderItemOrShipGroupAssoc.getString("orderItemSeqId") + "] and ship group [" + shipGroupSeqId + "] already exists.", module);
continue;
}
} catch (GenericEntityException gee) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ManufacturinWorkOrderItemFulfillmentError", UtilMisc.toMap("errorString", gee.getMessage()), locale));
}
try {
List<BOMNode> components = new LinkedList<BOMNode>();
BOMTree tree = new BOMTree(productId, "MANUF_COMPONENT", fromDate, BOMTree.EXPLOSION_MANUFACTURING, delegator, dispatcher, userLogin);
tree.setRootQuantity(quantity);
tree.setRootAmount(amount);
tree.print(components);
productionRuns.add(tree.createManufacturingOrders(null, fromDate, null, null, null, orderId, orderItem.getString("orderItemSeqId"), shipGroupSeqId, shipmentId, userLogin));
} catch (GenericEntityException gee) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ManufacturingBomErrorCreatingBillOfMaterialsTree", UtilMisc.toMap("errorString", gee.getMessage()), locale));
}
}
result.put("productionRuns", productionRuns);
return result;
}
use of org.apache.ofbiz.manufacturing.bom.BOMTree in project ofbiz-framework by apache.
the class ProductionRunServices method createProductionRunsForProductBom.
public static Map<String, Object> createProductionRunsForProductBom(DispatchContext dctx, Map<String, ? extends Object> context) {
Map<String, Object> result = new HashMap<String, Object>();
Delegator delegator = dctx.getDelegator();
LocalDispatcher dispatcher = dctx.getDispatcher();
GenericValue userLogin = (GenericValue) context.get("userLogin");
Locale locale = (Locale) context.get("locale");
String productId = (String) context.get("productId");
Timestamp startDate = (Timestamp) context.get("startDate");
BigDecimal quantity = (BigDecimal) context.get("quantity");
String facilityId = (String) context.get("facilityId");
String workEffortName = (String) context.get("workEffortName");
String description = (String) context.get("description");
String routingId = (String) context.get("routingId");
String workEffortId = null;
if (quantity == null) {
quantity = BigDecimal.ONE;
}
try {
List<BOMNode> components = new LinkedList<BOMNode>();
BOMTree tree = new BOMTree(productId, "MANUF_COMPONENT", startDate, BOMTree.EXPLOSION_MANUFACTURING, delegator, dispatcher, userLogin);
tree.setRootQuantity(quantity);
tree.setRootAmount(BigDecimal.ZERO);
tree.print(components);
workEffortId = tree.createManufacturingOrders(facilityId, startDate, workEffortName, description, routingId, null, null, null, null, userLogin);
} catch (GenericEntityException gee) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ManufacturingBomErrorCreatingBillOfMaterialsTree", UtilMisc.toMap("errorString", gee.getMessage()), locale));
}
if (workEffortId == null) {
return ServiceUtil.returnError(UtilProperties.getMessage(resource, "ManufacturingProductionRunIsNotRequiredForProductId", UtilMisc.toMap("productId", productId, "startDate", startDate), locale));
}
List<String> productionRuns = new LinkedList<String>();
result.put("productionRuns", productionRuns);
result.put("productionRunId", workEffortId);
return result;
}
use of org.apache.ofbiz.manufacturing.bom.BOMTree in project ofbiz-framework by apache.
the class ProposedOrder method calculateStartDate.
/**
* calculate the ProposedOrder requirementStartDate and update the requirementStartDate property.
* <ul>
* <li>For the build product,
* <ul>
* <li>read the routing associated to the product,</li>
* <li>read the routingTask associated to the routing</li>
* <li> step by step calculate from the endDate the startDate</li>
* </ul>
* </li>
* <li>For the bought product, the first ProductFacility.daysToShip is used to calculated the startDate</li>
* </ul>
* @return
* <ul>
* <li>if ProposedOrder.isBuild a Map with all the routingTaskId as keys and estimatedStartDate as value.</li>
* <li>else null.</li>
* </ul>
*/
public Map<String, Object> calculateStartDate(int daysToShip, GenericValue routing, Delegator delegator, LocalDispatcher dispatcher, GenericValue userLogin) {
Map<String, Object> result = null;
Timestamp endDate = (Timestamp) requiredByDate.clone();
Timestamp startDate = endDate;
long timeToShip = daysToShip * 8 * 60 * 60 * 1000;
if (isBuilt) {
List<GenericValue> listRoutingTaskAssoc = null;
if (routing == null) {
try {
Map<String, Object> routingInMap = UtilMisc.<String, Object>toMap("productId", product.getString("productId"), "ignoreDefaultRouting", "Y", "userLogin", userLogin);
Map<String, Object> routingOutMap = dispatcher.runSync("getProductRouting", routingInMap);
if (ServiceUtil.isError(routingOutMap)) {
String errorMessage = ServiceUtil.getErrorMessage(routingOutMap);
Debug.logError(errorMessage, module);
}
routing = (GenericValue) routingOutMap.get("routing");
listRoutingTaskAssoc = UtilGenerics.checkList(routingOutMap.get("tasks"));
if (routing == null) {
// try to find a routing linked to the virtual product
BOMTree tree = null;
List<BOMNode> components = new LinkedList<BOMNode>();
try {
tree = new BOMTree(product.getString("productId"), "MANUF_COMPONENT", requiredByDate, BOMTree.EXPLOSION_SINGLE_LEVEL, delegator, dispatcher, userLogin);
tree.setRootQuantity(quantity);
tree.print(components, true);
if (components.size() > 0)
components.remove(0);
} catch (Exception exc) {
Debug.logWarning(exc.getMessage(), module);
tree = null;
}
if (tree != null && tree.getRoot() != null && tree.getRoot().getProduct() != null) {
routingInMap = UtilMisc.toMap("productId", tree.getRoot().getProduct().getString("productId"), "userLogin", userLogin);
routingOutMap = dispatcher.runSync("getProductRouting", routingInMap);
if (ServiceUtil.isError(routingOutMap)) {
String errorMessage = ServiceUtil.getErrorMessage(routingOutMap);
Debug.logError(errorMessage, module);
}
routing = (GenericValue) routingOutMap.get("routing");
}
}
} catch (GenericServiceException gse) {
Debug.logWarning(gse.getMessage(), module);
}
}
if (routing != null) {
result = new HashMap<String, Object>();
// Looks for all the routingTask (ordered by inversed (begin from the end) sequence number)
if (listRoutingTaskAssoc == null) {
try {
Map<String, Object> routingTasksInMap = UtilMisc.<String, Object>toMap("workEffortId", routing.getString("workEffortId"), "userLogin", userLogin);
Map<String, Object> routingTasksOutMap = dispatcher.runSync("getRoutingTaskAssocs", routingTasksInMap);
if (ServiceUtil.isError(routingTasksOutMap)) {
String errorMessage = ServiceUtil.getErrorMessage(routingTasksOutMap);
Debug.logError(errorMessage, module);
}
listRoutingTaskAssoc = UtilGenerics.checkList(routingTasksOutMap.get("routingTaskAssocs"));
} catch (GenericServiceException gse) {
Debug.logWarning(gse.getMessage(), module);
}
}
}
if (listRoutingTaskAssoc != null) {
for (int i = 1; i <= listRoutingTaskAssoc.size(); i++) {
GenericValue routingTaskAssoc = listRoutingTaskAssoc.get(listRoutingTaskAssoc.size() - i);
if (EntityUtil.isValueActive(routingTaskAssoc, endDate)) {
GenericValue routingTask = null;
try {
routingTask = routingTaskAssoc.getRelatedOne("ToWorkEffort", true);
} catch (GenericEntityException e) {
Debug.logError(e.getMessage(), module);
}
// Calculate the estimatedStartDate
long totalTime = ProductionRun.getEstimatedTaskTime(routingTask, quantity, dispatcher);
if (i == listRoutingTaskAssoc.size()) {
// add the daysToShip at the end of the routing
totalTime += timeToShip;
}
startDate = TechDataServices.addBackward(TechDataServices.getTechDataCalendar(routingTask), endDate, totalTime);
// record the routingTask with the startDate associated
result.put(routingTask.getString("workEffortId"), startDate);
endDate = startDate;
}
}
} else {
// routing is null
Debug.logError("No routing found for product = " + product.getString("productId"), module);
}
} else {
// TODO: REVIEW this code
try {
GenericValue techDataCalendar = product.getDelegator().findOne("TechDataCalendar", UtilMisc.toMap("calendarId", "SUPPLIER"), true);
startDate = TechDataServices.addBackward(techDataCalendar, endDate, timeToShip);
} catch (GenericEntityException e) {
Debug.logError(e, "Error : reading SUPPLIER TechDataCalendar: " + e.getMessage(), module);
}
}
requirementStartDate = startDate;
return result;
}
use of org.apache.ofbiz.manufacturing.bom.BOMTree in project ofbiz-framework by apache.
the class ProposedOrder method create.
/**
* create a ProposedOrder in the Requirement Entity calling the createRequirement service.
* @param ctx The DispatchContext used to call service to create the Requirement Entity record.
* @return String the requirementId
*/
public String create(DispatchContext ctx, GenericValue userLogin) {
if ("WIP".equals(product.getString("productTypeId"))) {
// No requirements for Work In Process products
return null;
}
LocalDispatcher dispatcher = ctx.getDispatcher();
Delegator delegator = ctx.getDelegator();
Map<String, Object> parameters = UtilMisc.<String, Object>toMap("userLogin", userLogin);
if (isBuilt) {
try {
List<BOMNode> bom = new LinkedList<BOMNode>();
BOMTree tree = new BOMTree(productId, "MANUF_COMPONENT", null, BOMTree.EXPLOSION_MANUFACTURING, delegator, dispatcher, userLogin);
tree.setRootQuantity(quantity);
tree.print(bom);
requirementStartDate = tree.getRoot().getStartDate(manufacturingFacilityId, requiredByDate, true);
} catch (Exception e) {
Debug.logError(e, "Error : computing the requirement start date. " + e.getMessage(), module);
}
}
parameters.put("productId", productId);
parameters.put("statusId", "REQ_PROPOSED");
parameters.put("facilityId", (isBuilt ? manufacturingFacilityId : facilityId));
parameters.put("requiredByDate", requiredByDate);
parameters.put("requirementStartDate", requirementStartDate);
parameters.put("quantity", quantity);
parameters.put("requirementTypeId", (isBuilt ? "INTERNAL_REQUIREMENT" : "PRODUCT_REQUIREMENT"));
if (mrpName != null) {
parameters.put("description", "MRP_" + mrpName);
} else {
parameters.put("description", "Automatically generated by MRP");
}
try {
Map<String, Object> result = dispatcher.runSync("createRequirement", parameters);
if (ServiceUtil.isError(result)) {
String errorMessage = ServiceUtil.getErrorMessage(result);
Debug.logError(errorMessage, module);
return null;
}
return (String) result.get("requirementId");
} catch (GenericServiceException e) {
Debug.logError(e, "Error : createRequirement with parameters = " + parameters + "--" + e.getMessage(), module);
return null;
}
}
Aggregations