KickJava   Java API By Example, From Geeks To Geeks.

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


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 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.util.*;
17 import java.sql.*;
18 import java.math.*;
19 import java.io.Serializable JavaDoc;
20
21 import org.compiere.util.*;
22
23 /**
24  * Order Line Model.
25  * <code>
26  * MOrderLine ol = new MOrderLine(m_order);
27             ol.setM_Product_ID(wbl.getM_Product_ID());
28             ol.setQtyOrdered(wbl.getQuantity());
29             ol.setPrice();
30             ol.setPriceActual(wbl.getPrice());
31             ol.setTax();
32             ol.save();
33
34  * </code>
35  * @author Jorg Janke
36  * @version $Id: MOrderLine.java,v 1.14 2003/10/11 05:20:32 jjanke Exp $
37  */

38 public class MOrderLine extends X_C_OrderLine
39 {
40     /**
41      * Default Constructor
42      * @param ctx context
43      * @param C_OrderLine_ID order line to load
44      */

45     public MOrderLine (Properties ctx, int C_OrderLine_ID)
46     {
47         super (ctx, C_OrderLine_ID);
48     } // MOrderLine
49

50     /**
51      * Parent Constructor
52      * @param order parent order
53      */

54     public MOrderLine(MOrder order)
55     {
56         super (order.getCtx(), 0);
57         setC_Order_ID (order.getC_Order_ID()); // parent
58
setOrder(order);
59         //
60
setC_Tax_ID (0);
61         setLine (0);
62         setC_UOM_ID (0);
63         //
64
setDirectShip (false);
65         setIsDescription(false);
66         //
67
setPriceList (Env.ZERO);
68         setPriceActual (Env.ZERO);
69         setPriceLimit (Env.ZERO);
70         setLineNetAmt (Env.ZERO);
71         //
72
setQtyOrdered (Env.ZERO);
73         setQtyDelivered (Env.ZERO);
74         setQtyReserved (Env.ZERO);
75         setQtyInvoiced (Env.ZERO);
76         //
77
setFreightAmt (Env.ZERO);
78         setChargeAmt (Env.ZERO);
79     } // MOrderLine
80

81     /**
82      * Load Constructor
83      * @param ctx context
84      * @param rs result set record
85      */

86     public MOrderLine (Properties ctx, ResultSet rs)
87     {
88         super (ctx, rs);
89     } // MOrderLine
90

91     private int m_M_PriceList_ID = 0;
92     private boolean m_IsSOTrx = true;
93     private boolean m_priceSet = false;
94
95     /**
96      * Set Defaults from Order.
97      * Does not set Parent !!
98      * @param order order
99      */

100     public void setOrder (MOrder order)
101     {
102         setClientOrg(order.getAD_Client_ID(), order.getAD_Org_ID());
103         setC_BPartner_ID(order.getC_BPartner_ID());
104         setC_BPartner_Location_ID(order.getC_BPartner_Location_ID());
105         setM_Warehouse_ID(order.getM_Warehouse_ID());
106         setDateOrdered(order.getDateOrdered());
107         setDatePromised(order.getDatePromised());
108         m_M_PriceList_ID = order.getM_PriceList_ID();
109         m_IsSOTrx = order.isSOTrx();
110         setC_Currency_ID(order.getC_Currency_ID());
111     } // setOrder
112

113     /**
114      * Set Price for Product and PriceList
115      */

116     public void setPrice()
117     {
118         if (getM_Product_ID() == 0)
119             return;
120         //
121
Log.trace(Log.l4_Data, "MOrderLine.setPrice - M_PriceList_ID=" + m_M_PriceList_ID);
122         MProductPricing pp = new MProductPricing (getM_Product_ID());
123         pp.setM_PriceList_ID(m_M_PriceList_ID);
124         setPriceActual (pp.getPriceStd());
125         setPriceList (pp.getPriceList());
126         setPriceLimit (pp.getPriceLimit());
127         // Calculate Discount
128
setDiscount(pp.getDiscount());
129         // Set UOM
130
setC_UOM_ID(pp.getC_UOM_ID());
131         //
132
m_priceSet = true;
133     } // setPrice
134

135     /**
136      * Set Tax
137      */

138     public void setTax()
139     {
140         int ii = Tax.get(getCtx(), getM_Product_ID(), getC_Charge_ID(), getDateOrdered(), getDateOrdered(),
141             getAD_Org_ID(), getM_Warehouse_ID(),
142             getC_BPartner_Location_ID(), // should be bill to
143
getC_BPartner_Location_ID(), m_IsSOTrx);
144         if (ii != 0)
145             setC_Tax_ID (ii);
146         else
147             log.error("MOrderLine.setTax - No Tax found");
148     } // setTax
149

150     /**
151      * Set Defaults if not set
152      */

153     private void setDefaults()
154     {
155         // Get Defaults from Parent
156
if (getC_BPartner_ID() == 0 || getC_BPartner_Location_ID() == 0
157             || getM_Warehouse_ID() == 0)
158         {
159             MOrder o = new MOrder (getCtx(), getC_Order_ID());
160             setOrder (o);
161         }
162
163         // Set Price
164
if (!m_priceSet && Env.ZERO.compareTo(getPriceActual()) == 0)
165             setPrice();
166
167         // Set Tax
168
if (getC_Tax_ID() == 0)
169             setTax();
170
171         // Get Line No
172
if (getLine() == 0)
173         {
174             String JavaDoc sql = "SELECT COALESCE(MAX(Line),0)+10 FROM C_OrderLine WHERE C_Order_ID=?";
175             int ii = DB.getSQLValue (sql, getC_Order_ID());
176             setLine (ii);
177         }
178         //
179
if (getC_UOM_ID() == 0)
180             setC_UOM_ID (Env.getContextAsInt(getCtx(), "#C_UOM_ID"));
181
182         // Calculations
183
setLineNetAmt(getPriceActual().multiply(getQtyOrdered()));
184         setDiscount();
185     } // setDefaults
186

187     /**
188      * Save
189      * @return true if saved
190      */

191     public boolean save ()
192     {
193         Log.trace (Log.l4_Data, "MOrderLine.save");
194         setDefaults();
195         return super.save ();
196     } // save
197

198     /**
199      * String Representation
200      * @return info
201      */

202     public String JavaDoc toString ()
203     {
204         StringBuffer JavaDoc sb = new StringBuffer JavaDoc ("MOrderLine[")
205             .append(getID())
206             .append ("]");
207         return sb.toString ();
208     }
209
210     /**
211      * Get Name
212      * @return get the name of the line (from Product)
213      */

214     public String JavaDoc getName()
215     {
216         String JavaDoc retValue = "";
217         int M_Product_ID = getM_Product_ID();
218         if (M_Product_ID == 0)
219             return retValue;
220         //
221
String JavaDoc sql = "SELECT Name FROM M_Product WHERE M_Product_ID=?";
222         PreparedStatement pstmt = null;
223         try
224         {
225             pstmt = DB.prepareStatement(sql);
226             pstmt.setInt(1, M_Product_ID);
227             ResultSet rs = pstmt.executeQuery();
228             if (rs.next())
229                 retValue = rs.getString(1);
230             rs.close();
231             pstmt.close();
232             pstmt = null;
233         }
234         catch (Exception JavaDoc e)
235         {
236             log.error("getName", e);
237         }
238         finally
239         {
240             try
241             {
242                 if (pstmt != null)
243                     pstmt.close ();
244             }
245             catch (Exception JavaDoc e)
246             {}
247             pstmt = null;
248         }
249         return retValue;
250     } // getName
251

252     /**
253      * Set Discount
254      */

255     public void setDiscount ()
256     {
257         BigDecimal list = getPriceList();
258         // No List Price
259
if (Env.ZERO.compareTo(list) == 0)
260             return;
261         BigDecimal discount = list.subtract(getPriceActual())
262             .multiply(new BigDecimal(100))
263             .divide(list, 2, BigDecimal.ROUND_HALF_UP);
264         setDiscount(discount);
265     } // setDiscount
266

267     /**
268      * Set UOM.
269      * make access public
270      * @param C_UOM_ID uom
271      */

272     public void setC_UOM_ID (int C_UOM_ID)
273     {
274         super.setC_UOM_ID (C_UOM_ID);
275     } // setC_UOM_ID
276

277 } // MOrderLine
278
Popular Tags