1 16 package com.opensourcestrategies.financials.manufacturing; 17 18 import java.util.HashMap ; 19 import java.util.Iterator ; 20 import java.util.List ; 21 import java.util.Map ; 22 import java.math.BigDecimal ; 23 24 import org.ofbiz.base.util.Debug; 25 import org.ofbiz.base.util.UtilMisc; 26 import org.ofbiz.base.util.UtilNumber; 27 import org.ofbiz.entity.GenericDelegator; 28 import org.ofbiz.entity.GenericEntityException; 29 import org.ofbiz.entity.GenericValue; 30 import org.ofbiz.entity.condition.EntityCondition; 31 import org.ofbiz.entity.condition.EntityConditionList; 32 import org.ofbiz.entity.condition.EntityExpr; 33 import org.ofbiz.entity.condition.EntityOperator; 34 import org.ofbiz.entity.util.EntityUtil; 35 import org.ofbiz.service.DispatchContext; 36 import org.ofbiz.service.GenericServiceException; 37 import org.ofbiz.service.LocalDispatcher; 38 import org.ofbiz.service.ServiceUtil; 39 40 44 45 public class ManufacturingServices { 46 47 public static final String module = ManufacturingServices.class.getName(); 48 49 private static BigDecimal ZERO = new BigDecimal ("0"); 50 private static BigDecimal ONE = new BigDecimal ("1"); 51 private static int decimals = -1; 52 private static int rounding = -1; 53 static { 54 decimals = UtilNumber.getBigDecimalScale("order.decimals"); 55 rounding = UtilNumber.getBigDecimalRoundingMode("order.rounding"); 56 ZERO.setScale(decimals); 58 ONE.setScale(decimals); 59 } 60 61 public static Map createWorkEffortCosts(DispatchContext ctx, Map context) { 62 Map result = new HashMap (); 63 GenericDelegator delegator = ctx.getDelegator(); 64 LocalDispatcher dispatcher = ctx.getDispatcher(); 65 GenericValue userLogin = (GenericValue) context.get("userLogin"); 66 String workEffortId = (String )context.get("workEffortId"); 67 String workEffortTemplateId = null; 68 try { 69 GenericValue workEffort = delegator.findByPrimaryKey("WorkEffort", UtilMisc.toMap("workEffortId", workEffortId)); 70 if (workEffort == null) { 71 return ServiceUtil.returnError("Cannot find work effort [" + workEffortId + "]"); 72 } 73 GenericValue workEffortTemplate = EntityUtil.getFirst(EntityUtil.filterByDate(delegator.findByAnd("WorkEffortAssoc", 75 UtilMisc.toMap("workEffortIdTo", workEffortId, 76 "workEffortAssocTypeId", "WORK_EFF_TEMPLATE")))); 77 if (workEffortTemplate == null) { 78 workEffortTemplateId = workEffortId; 79 } else { 80 workEffortTemplateId = workEffortTemplate.getString("workEffortIdFrom"); 81 } 82 List workEffortCostCalcs = EntityUtil.filterByDate(delegator.findByAnd("WorkEffortCostCalc", 84 UtilMisc.toMap("workEffortId", workEffortTemplateId))); 85 Iterator workEffortCostCalcsIt = workEffortCostCalcs.iterator(); 86 while (workEffortCostCalcsIt.hasNext()) { 87 GenericValue workEffortCostCalc = (GenericValue)workEffortCostCalcsIt.next(); 88 GenericValue costComponentCalc = workEffortCostCalc.getRelatedOne("CostComponentCalc"); 89 GenericValue customMethod = costComponentCalc.getRelatedOne("CustomMethod"); 90 if (customMethod == null) { 91 BigDecimal fixedCost = costComponentCalc.getBigDecimal("fixedCost"); 92 if (fixedCost == null) { 93 fixedCost = ZERO; 94 } 95 BigDecimal variableCost = costComponentCalc.getBigDecimal("variableCost"); 96 if (variableCost == null) { 97 variableCost = ZERO; 98 } 99 Long perMilliSecond = costComponentCalc.getLong("perMilliSecond"); 100 if (perMilliSecond == null) { 101 perMilliSecond = new Long (1); 102 } 103 Double actualMilliSeconds = workEffort.getDouble("actualMilliSeconds"); 104 if (actualMilliSeconds == null) { 105 actualMilliSeconds = new Double (0); 106 } 107 Double actualSetupMillis = workEffort.getDouble("actualSetupMillis"); 108 if (actualSetupMillis == null) { 109 actualSetupMillis = new Double (0); 110 } 111 Double totalTime = new Double ((actualMilliSeconds.doubleValue() + actualSetupMillis.doubleValue()) / perMilliSecond.intValue()); BigDecimal totalCost = fixedCost.add(variableCost.multiply(new BigDecimal (totalTime.doubleValue()))).setScale(decimals, rounding); 113 Map inputMap = UtilMisc.toMap("userLogin", userLogin, "workEffortId", workEffortId, "cost", new Double (totalCost.doubleValue())); 115 inputMap.put("costComponentTypeId", "ACTUAL_" + workEffortCostCalc.getString("costComponentTypeId")); 116 inputMap.put("costUomId", costComponentCalc.getString("currencyUomId")); 117 inputMap.put("costComponentCalcId", costComponentCalc.getString("costComponentCalcId")); 118 Map outputMap = dispatcher.runSync("createCostComponent", inputMap); 119 120 } else { 121 Map inputMap = UtilMisc.toMap("userLogin", userLogin, "workEffort", workEffort); 123 inputMap.put("workEffortCostCalc", workEffortCostCalc); 124 inputMap.put("costComponentCalc", costComponentCalc); 125 Map outputMap = dispatcher.runSync(customMethod.getString("customMethodName"), inputMap); 126 } 127 } 128 BigDecimal totalMaterialsCost = ZERO; 130 String prevCurrencyUomId = null; 131 List allInventoryAssigned = delegator.findByAnd("WorkEffortAndInventoryAssign", UtilMisc.toMap("workEffortId", workEffortId), UtilMisc.toList("currencyUomId")); 132 Iterator allInventoryAssignedIt = allInventoryAssigned.iterator(); 133 while (allInventoryAssignedIt.hasNext()) { 134 GenericValue inventoryAssigned = (GenericValue)allInventoryAssignedIt.next(); 135 String currencyUomId = inventoryAssigned.getString("currencyUomId"); 136 if (prevCurrencyUomId != null && !currencyUomId.equals(prevCurrencyUomId)) { 137 if (ZERO.compareTo(totalMaterialsCost) != 0) { 138 Map inputMap = UtilMisc.toMap("userLogin", userLogin, "workEffortId", workEffortId, "cost", new Double (totalMaterialsCost.doubleValue())); 139 inputMap.put("costComponentTypeId", "ACTUAL_MAT_COST"); 140 inputMap.put("costUomId", prevCurrencyUomId); 141 Map outputMap = dispatcher.runSync("createCostComponent", inputMap); 142 } 143 totalMaterialsCost = ZERO; 144 } 145 prevCurrencyUomId = currencyUomId; 146 BigDecimal unitCost = inventoryAssigned.getBigDecimal("unitCost"); 147 BigDecimal quantity = inventoryAssigned.getBigDecimal("quantity"); 148 totalMaterialsCost = totalMaterialsCost.add(unitCost.multiply(quantity)).setScale(decimals, rounding); 149 } 150 if (ZERO.compareTo(totalMaterialsCost) != 0) { 151 Map inputMap = UtilMisc.toMap("userLogin", userLogin, "workEffortId", workEffortId, "cost", new Double (totalMaterialsCost.doubleValue())); 152 inputMap.put("costComponentTypeId", "ACTUAL_MAT_COST"); 153 inputMap.put("costUomId", prevCurrencyUomId); 154 Map outputMap = dispatcher.runSync("createCostComponent", inputMap); 155 } 156 } catch(Exception exc) { 157 return ServiceUtil.returnError("Cannot create costs for work effort [" + workEffortId + "]: " + exc.getMessage()); 158 } 159 return ServiceUtil.returnSuccess(); 160 } 161 } 162 | Popular Tags |