KickJava   Java API By Example, From Geeks To Geeks.

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


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.util.*;
17 import java.sql.*;
18 import java.math.*;
19 import java.io.Serializable JavaDoc;
20 import org.compiere.util.*;
21
22
23 /**
24  * Invoice Line Model
25  *
26  * @author Jorg Janke
27  * @version $Id: MInvoiceLine.java,v 1.12 2003/11/06 07:08:06 jjanke Exp $
28  */

29 public class MInvoiceLine extends X_C_InvoiceLine
30 {
31     /**
32      * Invoice Line Constructor
33      * @param ctx context
34      * @param C_InvoiceLine_ID invoice line or 0
35      */

36     public MInvoiceLine (Properties ctx, int C_InvoiceLine_ID)
37     {
38         super (ctx, C_InvoiceLine_ID);
39         if (C_InvoiceLine_ID == 0)
40         {
41             setIsDescription(false);
42             setIsPrinted (true);
43             setLineNetAmt (Env.ZERO);
44             setChargeAmt (Env.ZERO);
45             setPriceActual (Env.ZERO);
46             setQtyInvoiced (Env.ZERO);
47             setPriceLimit (Env.ZERO);
48             setPriceList (Env.ZERO);
49         }
50     } // MInvoiceLine
51

52     /**
53      * Invoice Line Constructor
54      * @param ctx context
55      * @param C_InvoiceLine_ID invoice line ot 0
56      * @param C_Invoice_ID invoice
57      */

58     public MInvoiceLine (MInvoice invoice)
59     {
60         this (invoice.getCtx(), 0);
61         setC_Invoice_ID (invoice.getC_Invoice_ID());
62         setInvoice(invoice);
63     } // MInvoiceLine
64

65
66     /**
67      * Load Constructor
68      * @param ctx context
69      * @param rs result set record
70      */

71     public MInvoiceLine (Properties ctx, ResultSet rs)
72     {
73         super (ctx, rs);
74     } // MInvoiceLine
75

76     private int m_M_PriceList_ID = 0;
77     private Timestamp m_DateInvoiced = null;
78     private int m_C_BPartner_Location_ID = 0;
79     private boolean m_IsSOTrx = true;
80     private boolean m_priceSet = false;
81
82     /** Cached Name of the line */
83     private String JavaDoc m_name;
84
85     /**
86      * Set Defaults from Order.
87      * Does not set Parent !!
88      * @param invoice invoice
89      */

90     public void setInvoice (MInvoice invoice)
91     {
92         setClientOrg(invoice.getAD_Client_ID(), invoice.getAD_Org_ID());
93         m_M_PriceList_ID = invoice.getM_PriceList_ID();
94         m_DateInvoiced = invoice.getDateInvoiced();
95         m_C_BPartner_Location_ID = invoice.getC_BPartner_Location_ID();
96         m_IsSOTrx = invoice.isSOTrx();
97     } // setOrder
98

99     /**
100      * Set Price for Product and PriceList
101      */

102     public void setPrice()
103     {
104         if (getM_Product_ID() == 0)
105             return;
106         //
107
Log.trace(Log.l4_Data, "MOrderLine.setPrice - M_PriceList_ID=" + m_M_PriceList_ID);
108         MProductPricing pp = new MProductPricing (getM_Product_ID());
109         pp.setM_PriceList_ID(m_M_PriceList_ID);
110         setPriceActual (pp.getPriceStd());
111         setPriceList (pp.getPriceList());
112         setPriceLimit (pp.getPriceLimit());
113         if (getC_UOM_ID() == 0)
114             setC_UOM_ID(pp.getC_UOM_ID());
115         //
116
m_priceSet = true;
117     } // setPrice
118

119     /**
120      * Set Tax
121      */

122     public void setTax()
123     {
124         int M_Warehouse_ID = Env.getContextAsInt(getCtx(), "#M_Warehouse_ID");
125         int ii = Tax.get(getCtx(), getM_Product_ID(), getC_Charge_ID() , m_DateInvoiced, m_DateInvoiced,
126             getAD_Org_ID(), M_Warehouse_ID,
127             m_C_BPartner_Location_ID, // should be bill to
128
m_C_BPartner_Location_ID, m_IsSOTrx);
129         if (ii != 0)
130             setC_Tax_ID (ii);
131         else
132             Log.error("MOrderLine.setTax - No Tax found");
133     } // setTax
134

135     /**
136      * Set Defaults if not set
137      */

138     private void setDefaults()
139     {
140         // Set Price
141
if (!m_priceSet && getPriceActual().intValue() == 0)
142             setPrice();
143
144         // Set Tax
145
if (getC_Tax_ID() == 0)
146             setTax();
147
148         // Get Line No
149
if (getLine() == 0)
150         {
151             String JavaDoc sql = "SELECT COALESCE(MAX(Line),0)+10 FROM C_InvoiceLine WHERE C_Invoice_ID=?";
152             int ii = DB.getSQLValue (sql, getC_Invoice_ID());
153             setLine (ii);
154         }
155         //
156
if (getC_UOM_ID() == 0)
157             setC_UOM_ID (Env.getContextAsInt(getCtx(), "#C_UOM_ID"));
158
159         // Calculations
160
setLineNetAmt(getPriceActual().multiply(getQtyInvoiced()));
161     } // setDefaults
162

163     /**
164      * Save Line
165      * @return true if saved
166      */

167     public boolean save ()
168     {
169         Log.trace (Log.l4_Data, "MInvoiceLine.save");
170         setDefaults();
171         return super.save ();
172     }
173
174     public String JavaDoc toString ()
175     {
176         StringBuffer JavaDoc sb = new StringBuffer JavaDoc ("MInvoiceLine[")
177             .append(getID())
178             .append ("]");
179         return sb.toString ();
180     }
181
182     /**
183      * Get Name
184      * @return name
185      */

186     public String JavaDoc getName ()
187     {
188         if (m_name == null)
189         {
190             String JavaDoc sql = "SELECT COALESCE (p.Name, c.Name) "
191                 + "FROM C_InvoiceLine il"
192                 + " LEFT OUTER JOIN M_Product p ON (il.M_Product_ID=p.M_Product_ID)"
193                 + " LEFT OUTER JOIN C_Charge C ON (il.C_Charge_ID=c.C_Charge_ID) "
194                 + "WHERE C_InvoiceLine_ID=?";
195             PreparedStatement pstmt = null;
196             try
197             {
198                 pstmt = DB.prepareStatement(sql);
199                 pstmt.setInt(1, getC_InvoiceLine_ID());
200                 ResultSet rs = pstmt.executeQuery();
201                 if (rs.next())
202                     m_name = rs.getString(1);
203                 rs.close();
204                 pstmt.close();
205                 pstmt = null;
206                 if (m_name == null)
207                     m_name = "??";
208             }
209             catch (Exception JavaDoc e)
210             {
211                 log.error("getName", e);
212             }
213             finally
214             {
215                 try
216                 {
217                     if (pstmt != null)
218                         pstmt.close ();
219                 }
220                 catch (Exception JavaDoc e)
221                 {}
222                 pstmt = null;
223             }
224         }
225         return m_name;
226     } // getName
227

228     /**
229      * Set Temporary (cached) Name
230      * @param tempName Cached Name
231      */

232     public void setName (String JavaDoc tempName)
233     {
234         m_name = tempName;
235     } // setName
236

237 } // MInvoiceLine
238
Popular Tags