KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > model > MStorage


1 /******************************************************************************
2  * The contents of this file are subject to the Compiere License Version 1.1
3  * ("License"); You may not use this file except in compliance with the License
4  * You may obtain a copy of the License at http://www.compiere.org/license.html
5  * Software distributed under the License is distributed on an "AS IS" basis,
6  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
7  * the specific language governing rights and limitations under the License.
8  * The Original Code is Compiere ERP & CRM Smart Business Solution
9  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
10  * Portions created by Jorg Janke are Copyright (C) 1999-2003 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.model;
15
16 import java.sql.*;
17 import java.util.*;
18 import java.math.*;
19
20 import org.compiere.util.*;
21
22 /**
23  * Inventory Storage Model
24  *
25  * @author Jorg Janke
26  * @version $Id: MStorage.java,v 1.2 2003/10/04 03:51:51 jjanke Exp $
27  */

28 public class MStorage extends X_M_Storage
29 {
30     /**
31      * Load Constructor
32      * @param ctx context
33      * @param rs result set
34      */

35     public MStorage (Properties ctx, ResultSet rs)
36     {
37         super (ctx, rs);
38     } // MStorage
39

40     /** Log */
41     private static Logger s_log = Logger.getCLogger (MStorage.class);
42
43     /**
44      * String Representation
45      * @return info
46      */

47     public String JavaDoc toString()
48     {
49         StringBuffer JavaDoc sb = new StringBuffer JavaDoc("MStorage[")
50             .append("M_Locator_ID=").append(getM_Locator_ID()).append(", M_Product_ID=").append(getM_Product_ID()).append(", M_AttributeSetInstance_ID=").append(getM_AttributeSetInstance_ID())
51             .append("-OnHand=").append(getQtyOnHand())
52             .append(",Reserved=").append(getQtyReserved())
53             .append(",Ordered=").append(getQtyOrdered())
54             .append("]");
55         return sb.toString();
56     } // toString
57

58     /*************************************************************************/
59
60     /**
61      * Get Storage Info
62      * @param ctx context
63      * @param M_Locator_ID locator
64      * @param M_Product_ID product
65      * @param M_AttributeSetInstance_ID instance
66      * @return existing or null
67      */

68     public static MStorage get (Properties ctx, int M_Locator_ID, int M_Product_ID, int M_AttributeSetInstance_ID)
69     {
70         MStorage retValue = null;
71         String JavaDoc sql = "SELECT * FROM M_Storage WHERE M_Locator_ID=? AND M_Product_ID=? AND M_AttributeSetInstance_ID=?";
72         PreparedStatement pstmt = null;
73         try
74         {
75             pstmt = DB.prepareStatement (sql);
76             pstmt.setInt (1, M_Locator_ID);
77             pstmt.setInt (2, M_Product_ID);
78             pstmt.setInt (3, M_AttributeSetInstance_ID);
79             ResultSet rs = pstmt.executeQuery ();
80             if (rs.next ())
81                 retValue = new MStorage (ctx, rs);
82             rs.close ();
83             pstmt.close ();
84             pstmt = null;
85         }
86         catch (SQLException ex)
87         {
88             s_log.error ("get", ex);
89         }
90         try
91         {
92             if (pstmt != null)
93                 pstmt.close ();
94         }
95         catch (SQLException ex1)
96         {
97         }
98         pstmt = null;
99         if (retValue == null)
100             s_log.warn("get - failed - M_Locator_ID=" + M_Locator_ID + ", M_Product_ID=" + M_Product_ID + ", M_AttributeSetInstance_ID=" + M_AttributeSetInstance_ID);
101         else
102             s_log.debug("get - M_Locator_ID=" + M_Locator_ID + ", M_Product_ID=" + M_Product_ID + ", M_AttributeSetInstance_ID=" + M_AttributeSetInstance_ID);
103         return retValue;
104     } // get
105

106     /**
107      * Create or Get Storage Info
108      * @param ctx context
109      * @param M_Locator_ID locator
110      * @param M_Product_ID product
111      * @param M_AttributeSetInstance_ID instance
112      * @return existing/new or null
113      */

114     public static MStorage create (Properties ctx, int M_Locator_ID, int M_Product_ID, int M_AttributeSetInstance_ID)
115     {
116         MStorage retValue = get(ctx, M_Locator_ID, M_Product_ID, M_AttributeSetInstance_ID);
117         if (retValue != null)
118             return retValue;
119         // Insert row
120
String JavaDoc sql = "INSERT INTO M_STORAGE "
121             + "(M_Locator_ID,M_Product_ID,M_AttributeSetInstance_ID,"
122             + "AD_Client_ID,AD_Org_ID, IsActive,Created,CreatedBy,Updated,UpdatedBy, "
123             + "QtyOnHand,QtyReserved,QtyOrdered, DateLastInventory) "
124             + "SELECT M_Locator_ID, " + M_Product_ID + ", " + M_AttributeSetInstance_ID + ","
125             + "AD_Client_ID,AD_Org_ID, 'Y', SysDate,0,SysDate,0,"
126             + "0,0,0, null "
127             + "FROM M_Locator "
128             + "WHERE M_Locator_ID=" + M_Locator_ID;
129         int no = DB.executeUpdate(sql, false);
130         if (no != 1)
131             s_log.warn("create - failed - M_Locator_ID=" + M_Locator_ID + ", M_Product_ID=" + M_Product_ID + ", M_AttributeSetInstance_ID=" + M_AttributeSetInstance_ID);
132         retValue = get(ctx, M_Locator_ID, M_Product_ID, M_AttributeSetInstance_ID);
133         return retValue;
134     } // create
135

136     /**
137      * Update Storage Info add
138      * @param ctx context
139      * @param M_Locator_ID locator
140      * @param M_Product_ID product
141      * @param M_AttributeSetInstance_ID instance
142      * @param diffQtyOnHand add on hand
143      * @param diffQtyReserved add reserved
144      * @param diffQtyOrdered add order
145      * @return true if updated
146      */

147     public static boolean add (Properties ctx, int M_Locator_ID, int M_Product_ID, int M_AttributeSetInstance_ID,
148         BigDecimal diffQtyOnHand, BigDecimal diffQtyReserved, BigDecimal diffQtyOrdered)
149     {
150         MStorage storage = create (ctx, M_Locator_ID, M_Product_ID, M_AttributeSetInstance_ID);
151         if (storage == null)
152             return false;
153         boolean changed = false;
154         StringBuffer JavaDoc diffText = new StringBuffer JavaDoc("add (");
155         if (diffQtyOnHand != null && !diffQtyOnHand.equals(Env.ZERO))
156         {
157             storage.setQtyOnHand (storage.getQtyOnHand().add (diffQtyOnHand));
158             diffText.append("OnHand=").append(diffQtyOnHand);
159             changed = true;
160         }
161         if (diffQtyReserved != null && !diffQtyReserved.equals(Env.ZERO))
162         {
163             storage.setQtyReserved (storage.getQtyReserved().add (diffQtyReserved));
164             diffText.append("Reserved=").append(diffQtyReserved);
165             changed = true;
166         }
167         if (diffQtyOrdered != null && !diffQtyOrdered.equals(Env.ZERO))
168         {
169             storage.setQtyOrdered (storage.getQtyOrdered().add (diffQtyOrdered));
170             diffText.append("Ordered=").append(diffQtyOrdered);
171             changed = true;
172         }
173         if (changed)
174         {
175             diffText.append(") -> ").append(storage.toString());
176             s_log.debug(diffText);
177             return storage.save ();
178         }
179         return true;
180     } // add
181

182     /*************************************************************************/
183
184     /**
185      * Get Location with highest OnHand Qty
186      * @param M_Warehouse_ID warehouse
187      * @param M_Product_ID product
188      * @return id
189      */

190     public static int getM_Location_ID (int M_Warehouse_ID, int M_Product_ID)
191     {
192         int M_Location_ID = 0;
193         String JavaDoc sql = "SELECT M_Locator_ID "
194             + "FROM M_Storage s INNER JOIN M_Locator l ON (s.M_Locator_ID=l.M_Locator_ID) "
195             + "WHERE l.M_Warehouse_ID=?"
196             + " AND s.M_Product_ID=?"
197             + " AND l.IsActive='Y' "
198             + "ORDER BY s.QtyOnHand DESC";
199         PreparedStatement pstmt = null;
200         try
201         {
202             pstmt = DB.prepareStatement(sql);
203             pstmt.setInt(1, M_Warehouse_ID);
204             pstmt.setInt(2, M_Product_ID);
205             ResultSet rs = pstmt.executeQuery();
206             if (rs.next())
207                 M_Location_ID = rs.getInt(1);
208             rs.close();
209             pstmt.close();
210             pstmt = null;
211         }
212         catch (SQLException ex)
213         {
214             s_log.error("getM_Location_ID", ex);
215         }
216         try
217         {
218             if (pstmt != null)
219                 pstmt.close();
220         }
221         catch (SQLException ex1)
222         {
223         }
224         pstmt = null;
225         return M_Location_ID;
226     } // getM_Location_ID
227

228 } // MStorage
229
Popular Tags