KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > opensourcestrategies > financials > manufacturing > ManufacturingServices


1 /*
2  * Copyright (c) 2006 - 2007 Open Source Strategies, Inc.
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the Honest Public License.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * Honest Public License for more details.
11  *
12  * You should have received a copy of the Honest Public License
13  * along with this program; if not, write to Funambol,
14  * 643 Bair Island Road, Suite 305 - Redwood City, CA 94063, USA
15  */

16 package com.opensourcestrategies.financials.manufacturing;
17
18 import java.util.HashMap JavaDoc;
19 import java.util.Iterator JavaDoc;
20 import java.util.List JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.math.BigDecimal JavaDoc;
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 /**
41  * ManufacturingServices - Services to manage production run costs
42  *
43  */

44
45 public class ManufacturingServices {
46
47     public static final String JavaDoc module = ManufacturingServices.class.getName();
48     
49     private static BigDecimal JavaDoc ZERO = new BigDecimal JavaDoc("0");
50     private static BigDecimal JavaDoc ONE = new BigDecimal JavaDoc("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         // set zero to the proper scale
57
ZERO.setScale(decimals);
58         ONE.setScale(decimals);
59     }
60
61     public static Map JavaDoc createWorkEffortCosts(DispatchContext ctx, Map JavaDoc context) {
62         Map JavaDoc result = new HashMap JavaDoc();
63         GenericDelegator delegator = ctx.getDelegator();
64         LocalDispatcher dispatcher = ctx.getDispatcher();
65         GenericValue userLogin = (GenericValue) context.get("userLogin");
66         String JavaDoc workEffortId = (String JavaDoc)context.get("workEffortId");
67         String JavaDoc 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             // Get the template (aka routing task) of the work effort
74
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             // Get all the valid CostComponentCalc entries
83
List JavaDoc workEffortCostCalcs = EntityUtil.filterByDate(delegator.findByAnd("WorkEffortCostCalc",
84                                                           UtilMisc.toMap("workEffortId", workEffortTemplateId)));
85             Iterator JavaDoc 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 JavaDoc fixedCost = costComponentCalc.getBigDecimal("fixedCost");
92                     if (fixedCost == null) {
93                         fixedCost = ZERO;
94                     }
95                     BigDecimal JavaDoc variableCost = costComponentCalc.getBigDecimal("variableCost");
96                     if (variableCost == null) {
97                         variableCost = ZERO;
98                     }
99                     Long JavaDoc perMilliSecond = costComponentCalc.getLong("perMilliSecond");
100                     if (perMilliSecond == null) {
101                         perMilliSecond = new Long JavaDoc(1);
102                     }
103                     Double JavaDoc actualMilliSeconds = workEffort.getDouble("actualMilliSeconds");
104                     if (actualMilliSeconds == null) {
105                         actualMilliSeconds = new Double JavaDoc(0);
106                     }
107                     Double JavaDoc actualSetupMillis = workEffort.getDouble("actualSetupMillis");
108                     if (actualSetupMillis == null) {
109                         actualSetupMillis = new Double JavaDoc(0);
110                     }
111                     Double JavaDoc totalTime = new Double JavaDoc((actualMilliSeconds.doubleValue() + actualSetupMillis.doubleValue()) / perMilliSecond.intValue()); // TODO: should we use BigDecimals here?
112
BigDecimal JavaDoc totalCost = fixedCost.add(variableCost.multiply(new BigDecimal JavaDoc(totalTime.doubleValue()))).setScale(decimals, rounding);
113                     // store the cost in the cost component entity
114
Map JavaDoc inputMap = UtilMisc.toMap("userLogin", userLogin, "workEffortId", workEffortId, "cost", new Double JavaDoc(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 JavaDoc outputMap = dispatcher.runSync("createCostComponent", inputMap);
119
120                 } else {
121                     // invoke the custom method
122
Map JavaDoc inputMap = UtilMisc.toMap("userLogin", userLogin, "workEffort", workEffort);
123                     inputMap.put("workEffortCostCalc", workEffortCostCalc);
124                     inputMap.put("costComponentCalc", costComponentCalc);
125                     Map JavaDoc outputMap = dispatcher.runSync(customMethod.getString("customMethodName"), inputMap);
126                 }
127             }
128             // The costs for the materials consumed by the work effort are also created
129
BigDecimal JavaDoc totalMaterialsCost = ZERO;
130             String JavaDoc prevCurrencyUomId = null;
131             List JavaDoc allInventoryAssigned = delegator.findByAnd("WorkEffortAndInventoryAssign", UtilMisc.toMap("workEffortId", workEffortId), UtilMisc.toList("currencyUomId"));
132             Iterator JavaDoc allInventoryAssignedIt = allInventoryAssigned.iterator();
133             while (allInventoryAssignedIt.hasNext()) {
134                 GenericValue inventoryAssigned = (GenericValue)allInventoryAssignedIt.next();
135                 String JavaDoc currencyUomId = inventoryAssigned.getString("currencyUomId");
136                 if (prevCurrencyUomId != null && !currencyUomId.equals(prevCurrencyUomId)) {
137                     if (ZERO.compareTo(totalMaterialsCost) != 0) {
138                         Map JavaDoc inputMap = UtilMisc.toMap("userLogin", userLogin, "workEffortId", workEffortId, "cost", new Double JavaDoc(totalMaterialsCost.doubleValue()));
139                         inputMap.put("costComponentTypeId", "ACTUAL_MAT_COST");
140                         inputMap.put("costUomId", prevCurrencyUomId);
141                         Map JavaDoc outputMap = dispatcher.runSync("createCostComponent", inputMap);
142                     }
143                     totalMaterialsCost = ZERO;
144                 }
145                 prevCurrencyUomId = currencyUomId;
146                 BigDecimal JavaDoc unitCost = inventoryAssigned.getBigDecimal("unitCost");
147                 BigDecimal JavaDoc quantity = inventoryAssigned.getBigDecimal("quantity");
148                 totalMaterialsCost = totalMaterialsCost.add(unitCost.multiply(quantity)).setScale(decimals, rounding);
149             }
150             if (ZERO.compareTo(totalMaterialsCost) != 0) {
151                 Map JavaDoc inputMap = UtilMisc.toMap("userLogin", userLogin, "workEffortId", workEffortId, "cost", new Double JavaDoc(totalMaterialsCost.doubleValue()));
152                 inputMap.put("costComponentTypeId", "ACTUAL_MAT_COST");
153                 inputMap.put("costUomId", prevCurrencyUomId);
154                 Map JavaDoc outputMap = dispatcher.runSync("createCostComponent", inputMap);
155             }
156         } catch(Exception JavaDoc exc) {
157             return ServiceUtil.returnError("Cannot create costs for work effort [" + workEffortId + "]: " + exc.getMessage());
158         }
159         return ServiceUtil.returnSuccess();
160     }
161 }
162
Popular Tags