1 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 28 public class MStorage extends X_M_Storage 29 { 30 35 public MStorage (Properties ctx, ResultSet rs) 36 { 37 super (ctx, rs); 38 } 40 41 private static Logger s_log = Logger.getCLogger (MStorage.class); 42 43 47 public String toString() 48 { 49 StringBuffer sb = new StringBuffer ("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 } 58 59 60 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 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 } 106 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 String 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 } 136 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 diffText = new StringBuffer ("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 } 182 183 184 190 public static int getM_Location_ID (int M_Warehouse_ID, int M_Product_ID) 191 { 192 int M_Location_ID = 0; 193 String 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 } 228 } | Popular Tags |