001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.manufacturing.mrp;
019:
020: import java.util.Map;
021:
022: import org.ofbiz.base.util.Debug;
023: import org.ofbiz.base.util.UtilMisc;
024: import org.ofbiz.base.util.UtilValidate;
025: import org.ofbiz.entity.GenericDelegator;
026: import org.ofbiz.entity.GenericEntityException;
027: import org.ofbiz.entity.GenericValue;
028: import org.ofbiz.service.DispatchContext;
029: import org.ofbiz.service.ServiceUtil;
030:
031: /**
032: * InventoryEventPlannedServices - InventoryEventPlanned related Services
033: *
034: */
035: public class InventoryEventPlannedServices {
036:
037: public static final String module = InventoryEventPlannedServices.class
038: .getName();
039:
040: /**
041: *
042: * Create an InventoryEventPlanned.
043: * Make an update if a record exist with same key, (adding the eventQuantity to the exiting record)
044: *
045: * @param ctx
046: * @param context: a map containing the parameters used to create an InventoryEventPlanned (see the servcie definition)
047: * @return result: a map with service status
048: */
049: public static Map createInventoryEventPlanned(DispatchContext ctx,
050: Map context) {
051: GenericDelegator delegator = ctx.getDelegator();
052: Map parameters = UtilMisc.toMap("productId", context
053: .get("productId"), "eventDate", context
054: .get("eventDate"), "inventoryEventPlanTypeId", context
055: .get("inventoryEventPlanTypeId"));
056: Double quantity = (Double) context.get("eventQuantity");
057: GenericValue inventoryEventPlanned = null;
058: try {
059: createOrUpdateInventoryEventPlanned(parameters, quantity,
060: (String) context.get("facilityId"),
061: (String) context.get("eventName"), false, delegator);
062: } catch (GenericEntityException e) {
063: Debug.logError(e,
064: "Error : delegator.findByPrimaryKey(\"InventoryEventPlanned\", parameters =)"
065: + parameters, module);
066: return ServiceUtil
067: .returnError("Problem, on database access, for more detail look at the log");
068: }
069: return ServiceUtil.returnSuccess();
070: }
071:
072: public static void createOrUpdateInventoryEventPlanned(
073: Map inventoryEventPlannedKeyMap, Double newQuantity,
074: String facilityId, String eventName, boolean isLate,
075: GenericDelegator delegator) throws GenericEntityException {
076: GenericValue inventoryEventPlanned = null;
077: inventoryEventPlanned = delegator.findByPrimaryKey(
078: "InventoryEventPlanned", inventoryEventPlannedKeyMap);
079: if (inventoryEventPlanned == null) {
080: inventoryEventPlanned = delegator.makeValue(
081: "InventoryEventPlanned",
082: inventoryEventPlannedKeyMap);
083: inventoryEventPlanned.put("eventQuantity", newQuantity);
084: inventoryEventPlanned.put("eventName", eventName);
085: inventoryEventPlanned.put("facilityId", facilityId);
086: inventoryEventPlanned.put("isLate", (isLate ? "Y" : "N"));
087: inventoryEventPlanned.create();
088: } else {
089: double qties = newQuantity.doubleValue()
090: + ((Double) inventoryEventPlanned
091: .get("eventQuantity")).doubleValue();
092: inventoryEventPlanned.put("eventQuantity",
093: new Double(qties));
094: if (!UtilValidate.isEmpty(eventName)) {
095: String existingEventName = inventoryEventPlanned
096: .getString("eventName");
097: inventoryEventPlanned.put("eventName", (UtilValidate
098: .isEmpty(existingEventName) ? eventName
099: : existingEventName + ", " + eventName));
100: }
101: if (isLate) {
102: inventoryEventPlanned.put("isLate", "Y");
103: }
104: inventoryEventPlanned.store();
105: }
106: }
107: }
|