1 16 17 package org.ofbiz.product.inventory; 18 19 import java.util.Collection ; 20 import java.util.Iterator ; 21 import java.util.List ; 22 import java.util.Map ; 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 module = InventoryWorker.class.getName(); 39 40 46 public static List getOutstandingPurchaseOrders(String productId, GenericDelegator delegator) { 47 try { 48 List 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 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 71 public static double getOutstandingPurchasedQuantity(String productId, GenericDelegator delegator) { 72 double qty = 0.0; 73 List purchaseOrders = getOutstandingPurchaseOrders(productId, delegator); 74 if (UtilValidate.isEmpty(purchaseOrders)) { 75 return qty; 76 } else { 77 for (Iterator 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 105 public static Map getOutstandingProductQuantities(Collection productIds, String orderTypeId, GenericDelegator delegator) { 106 List fieldsToSelect = UtilMisc.toList("productId", "quantityOpen"); 107 List 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 results = FastMap.newInstance(); 122 try { 123 List orderedProducts = delegator.findByCondition("OrderItemQuantityReportGroupByProduct", conditions, fieldsToSelect, null); 124 for (Iterator 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 135 public static Map getOutstandingProductQuantitiesForSalesOrders(Collection productIds, GenericDelegator delegator) { 136 return getOutstandingProductQuantities(productIds, "SALES_ORDER", delegator); 137 } 138 139 140 public static Map getOutstandingProductQuantitiesForPurchaseOrders(Collection productIds, GenericDelegator delegator) { 141 return getOutstandingProductQuantities(productIds, "PURCHASE_ORDER", delegator); 142 } 143 } 144 | Popular Tags |