KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > product > inventory > InventoryWorker


1 /*
2  * Copyright 2001-2006 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations
14  * under the License.
15  */

16
17 package org.ofbiz.product.inventory;
18
19 import java.util.Collection JavaDoc;
20 import java.util.Iterator JavaDoc;
21 import java.util.List JavaDoc;
22 import java.util.Map JavaDoc;
23
24 import javolution.util.FastMap;
25
26 import org.ofbiz.base.util.Debug;
27 import org.ofbiz.base.util.UtilMisc;
28 import org.ofbiz.base.util.UtilValidate;
29 import org.ofbiz.entity.GenericDelegator;
30 import org.ofbiz.entity.GenericEntityException;
31 import org.ofbiz.entity.GenericValue;
32 import org.ofbiz.entity.condition.EntityConditionList;
33 import org.ofbiz.entity.condition.EntityExpr;
34 import org.ofbiz.entity.condition.EntityOperator;
35
36 public class InventoryWorker {
37     
38     public final static String JavaDoc module = InventoryWorker.class.getName();
39
40     /**
41      * Finds all outstanding Purchase orders for a productId. The orders and the items cannot be completed, cancelled, or rejected
42      * @param productId
43      * @param delegator
44      * @return
45      */

46     public static List JavaDoc getOutstandingPurchaseOrders(String JavaDoc productId, GenericDelegator delegator) {
47         try {
48             List JavaDoc purchaseOrderConditions = UtilMisc.toList(new EntityExpr("orderStatusId", EntityOperator.NOT_EQUAL, "ORDER_COMPLETED"),
49                     new EntityExpr("orderStatusId", EntityOperator.NOT_EQUAL, "ORDER_CANCELLED"),
50                     new EntityExpr("orderStatusId", EntityOperator.NOT_EQUAL, "ORDER_REJECTED"),
51                     new EntityExpr("itemStatusId", EntityOperator.NOT_EQUAL, "ITEM_COMPLETED"),
52                     new EntityExpr("itemStatusId", EntityOperator.NOT_EQUAL, "ITEM_CANCELLED"),
53                     new EntityExpr("itemStatusId", EntityOperator.NOT_EQUAL, "ITEM_REJECTED"));
54             purchaseOrderConditions.add(new EntityExpr("orderTypeId", EntityOperator.EQUALS, "PURCHASE_ORDER"));
55             purchaseOrderConditions.add(new EntityExpr("productId", EntityOperator.EQUALS, productId));
56             List JavaDoc purchaseOrders = delegator.findByCondition("OrderHeaderAndItems", new EntityConditionList(purchaseOrderConditions, EntityOperator.AND),
57                     null, UtilMisc.toList("estimatedDeliveryDate DESC", "orderDate"));
58             return purchaseOrders;
59         } catch (GenericEntityException ex) {
60             Debug.logError("Unable to find outstanding purchase orders for product [" + productId + "] due to " + ex.getMessage() + " - returning null", module);
61             return null;
62         }
63     }
64     
65     /**
66      * Finds the net outstanding ordered quantity for a productId, netting quantity on outstanding purchase orders against cancelQuantity
67      * @param productId
68      * @param delegator
69      * @return
70      */

71     public static double getOutstandingPurchasedQuantity(String JavaDoc productId, GenericDelegator delegator) {
72         double qty = 0.0;
73         List JavaDoc purchaseOrders = getOutstandingPurchaseOrders(productId, delegator);
74         if (UtilValidate.isEmpty(purchaseOrders)) {
75             return qty;
76         } else {
77             for (Iterator JavaDoc pOi = purchaseOrders.iterator(); pOi.hasNext();) {
78                 GenericValue nextOrder = (GenericValue) pOi.next();
79                 if (nextOrder.get("quantity") != null) {
80                     double itemQuantity = nextOrder.getDouble("quantity").doubleValue();
81                     double cancelQuantity = 0.0;
82                     if (nextOrder.get("cancelQuantity") != null) {
83                         cancelQuantity = nextOrder.getDouble("cancelQuantity").doubleValue();
84                     }
85                     itemQuantity -= cancelQuantity;
86                     if (itemQuantity >= 0.0) {
87                         qty += itemQuantity;
88                     }
89                 }
90             }
91         }
92
93         return qty;
94     }
95
96     /**
97      * Gets the quanitty of each product in the order that is outstanding across all orders of the given input type.
98      * Uses the OrderItemQuantityReportGroupByProduct view entity.
99      *
100      * @param productIds Collection of disticnt productIds in an order. Use OrderReadHelper.getOrderProductIds()
101      * @param orderTypeId Either "SALES_ORDER" or "PURCHASE_ORDER"
102      * @param delegator The delegator to use
103      * @return Map of productIds to quantities outstanding.
104      */

105     public static Map JavaDoc getOutstandingProductQuantities(Collection JavaDoc productIds, String JavaDoc orderTypeId, GenericDelegator delegator) {
106         List JavaDoc fieldsToSelect = UtilMisc.toList("productId", "quantityOpen");
107         List JavaDoc condList = UtilMisc.toList(
108                 new EntityExpr("orderTypeId", EntityOperator.EQUALS, orderTypeId),
109                 new EntityExpr("orderStatusId", EntityOperator.NOT_EQUAL, "ORDER_COMPLETED"),
110                 new EntityExpr("orderStatusId", EntityOperator.NOT_EQUAL, "ORDER_REJECTED"),
111                 new EntityExpr("orderStatusId", EntityOperator.NOT_EQUAL, "ORDER_CANCELLED")
112                 );
113         if (productIds.size() > 0) {
114             condList.add(new EntityExpr("productId", EntityOperator.IN, productIds));
115         }
116         condList.add(new EntityExpr("orderItemStatusId", EntityOperator.NOT_EQUAL, "ITEM_COMPLETED"));
117         condList.add(new EntityExpr("orderItemStatusId", EntityOperator.NOT_EQUAL, "ITEM_REJECTED"));
118         condList.add(new EntityExpr("orderItemStatusId", EntityOperator.NOT_EQUAL, "ITEM_CANCELLED"));
119         EntityConditionList conditions = new EntityConditionList(condList, EntityOperator.AND);
120
121         Map JavaDoc results = FastMap.newInstance();
122         try {
123             List JavaDoc orderedProducts = delegator.findByCondition("OrderItemQuantityReportGroupByProduct", conditions, fieldsToSelect, null);
124             for (Iterator JavaDoc iter = orderedProducts.iterator(); iter.hasNext(); ) {
125                 GenericValue value = (GenericValue) iter.next();
126                 results.put(value.getString("productId"), value.getDouble("quantityOpen"));
127             }
128         } catch (GenericEntityException e) {
129             Debug.logError(e, module);
130         }
131         return results;
132     }
133
134     /** As above, but for sales orders */
135     public static Map JavaDoc getOutstandingProductQuantitiesForSalesOrders(Collection JavaDoc productIds, GenericDelegator delegator) {
136         return getOutstandingProductQuantities(productIds, "SALES_ORDER", delegator);
137     }
138
139     /** As above, but for purhcase orders */
140     public static Map JavaDoc getOutstandingProductQuantitiesForPurchaseOrders(Collection JavaDoc productIds, GenericDelegator delegator) {
141         return getOutstandingProductQuantities(productIds, "PURCHASE_ORDER", delegator);
142     }
143 }
144
Popular Tags