KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > compiere > acct > Doc_MatchPO


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-2001 Jorg Janke, parts
11  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
12  * Contributor(s): ______________________________________.
13  *****************************************************************************/

14 package org.compiere.acct;
15
16 import java.math.*;
17 import java.util.*;
18 import java.sql.*;
19
20 import org.compiere.util.*;
21 import org.compiere.model.*;
22
23 /**
24  * Post MatchPO Documents.
25  * <pre>
26  * Table: C_MatchPO (473)
27  * Document Types: MXP
28  * </pre>
29  * @author Jorg Janke
30  * @version $Id: Doc_MatchPO.java,v 1.9 2003/03/19 06:47:32 jjanke Exp $
31  */

32 public class Doc_MatchPO extends Doc
33 {
34     /**
35      * Constructor
36      * @param AD_Client_ID client
37      */

38     protected Doc_MatchPO(int AD_Client_ID)
39     {
40         super(AD_Client_ID);
41     } // Doc_MatchPO
42

43     private int m_C_OrderLine_ID = 0;
44     private int m_M_InOutLine_ID = 0;
45     private int m_C_InvoiceLine_ID = 0;
46     private ProductInfo m_pi;
47
48     /**
49      * Return TableName of Document
50      * @return M_MatchPO
51      */

52     public String JavaDoc getTableName()
53     {
54         return "M_MatchPO";
55     } // getTableName
56

57     /**
58      * Get Table ID
59      * @return 473
60      */

61     public int getAD_Table_ID()
62     {
63         return 473;
64     } // getAD_Table_ID
65

66     /**
67      * Load Specific Document Details
68      * @param rs result set
69      * @return true if loadDocumentType was set
70      */

71     protected boolean loadDocumentDetails (ResultSet rs)
72     {
73         p_vo.DocumentType = DocVO.DOCTYPE_MatMatchPO;
74         try
75         {
76             p_vo.DateDoc = rs.getTimestamp("DateTrx");
77             p_vo.M_Product_ID = rs.getInt("M_Product_ID");
78             p_vo.Qty = rs.getBigDecimal("Qty");
79             m_C_OrderLine_ID = rs.getInt("C_OrderLine_ID");
80             m_M_InOutLine_ID = rs.getInt("M_InOutLine_ID");
81             m_C_InvoiceLine_ID = rs.getInt("C_InvoiceLine_ID");
82             //
83
m_pi = new ProductInfo (p_vo.M_Product_ID);
84             m_pi.setQty(p_vo.Qty);
85         }
86         catch (SQLException e)
87         {
88             log.error("loadDocumentDetails", e);
89         }
90         p_vo.C_Currency_ID = Doc.NO_CURRENCY;
91         return false;
92     } // loadDocumentDetails
93

94     /*************************************************************************/
95
96     /**
97      * Get Source Currency Balance - subtracts line and tax amounts from total - no rounding
98      * @return Zero - always balanced
99      */

100     public BigDecimal getBalance()
101     {
102         return Env.ZERO;
103     } // getBalance
104

105     /**
106      * Create Facts (the accounting logic) for
107      * MXP.
108      * <pre>
109      * Product PPV <difference>
110      * PPV_Offset <difference>
111      * </pre>
112      * @param as accounting schema
113      * @return Fact
114      */

115     public Fact createFact (AcctSchema as)
116     {
117         // create Fact Header
118
Fact fact = new Fact(this, as, Fact.POST_Actual);
119         // Nothing to do if no Product
120
if (p_vo.M_Product_ID == 0)
121             return fact;
122         // No posting if not matched to Shipment
123
if (m_M_InOutLine_ID == 0)
124             return fact;
125         //
126
p_vo.C_Currency_ID = as.getC_Currency_ID();
127         BigDecimal difference = loadInfo(as); // PPV difference
128
if (difference == null)
129         {
130             p_vo.Error = "No PPV Difference";
131             log.error("createFact - " + p_vo.Error);
132             return null;
133         }
134
135         // Product PPV
136
FactLine cr = fact.createLine(null,
137             m_pi.getAccount(ProductInfo.ACCTTYPE_P_PPV, as),
138             as.getC_Currency_ID(), difference);
139         // PPV Offset
140
FactLine dr = fact.createLine(null,
141             getAccount(Doc.ACCTTYPE_PPVOffset, as),
142             as.getC_Currency_ID(), difference.negate());
143
144         return fact;
145     } // createFact
146

147     /**
148      * Load Info and return difference.
149      * Difference between Receipt (cost) and Purchase Order amount
150      * @param as accounting schema
151      * @return difference
152      */

153     private BigDecimal loadInfo(AcctSchema as)
154     {
155         BigDecimal difference = null;
156         // get PO Amount (probably no Acct_Fact)
157
String JavaDoc sql = "SELECT PriceActual from C_OrderLine WHERE C_OrderLine_ID=?";
158         try
159         {
160             PreparedStatement pstmt = DB.prepareStatement(sql);
161             pstmt.setInt(1, m_C_OrderLine_ID);
162             ResultSet rs = pstmt.executeQuery();
163             if (rs.next())
164             {
165                 difference = rs.getBigDecimal(1);
166             }
167             rs.close();
168             pstmt.close();
169         }
170         catch (SQLException e)
171         {
172             log.error("loadInfo", e);
173         }
174         if (difference == null)
175             return null;
176
177         // subtract Standard Cost (should get data from Receipt account entry) ??
178
// is "more" correct if Average Costing - but issue if std price has changed in the meantime
179
difference.subtract(m_pi.getProductItemCost(as, AcctSchema.COSTING_STANDARD));
180
181         return difference.multiply(p_vo.Qty);
182     } // loadInfo
183

184     /**
185      * Update Product Info.
186      * - Costing (CostStandardPOQty, CostStandardPOAmt)
187      * @param C_AcctSchema_ID accounting schema
188      */

189     private void updateProductInfo (int C_AcctSchema_ID)
190     {
191         log.debug("updateProductInfo - M_MatchPO_ID=" + p_vo.Record_ID);
192
193         // update Product Costing
194
// requires existence of currency conversion !!
195
StringBuffer JavaDoc sql = new StringBuffer JavaDoc (
196             "UPDATE M_Product_Costing pc "
197             + "SET (CostStandardPOQty,CostStandardPOAmt) = "
198             + "(SELECT CostStandardPOQty + m.Qty,"
199             + " CostStandardPOAmt + C_Currency_Convert(ol.PriceActual,ol.C_Currency_ID,a.C_Currency_ID,ol.DateOrdered,null,ol.AD_Client_ID,ol.AD_Org_ID)*m.Qty "
200             + "FROM M_MatchPO m, C_OrderLine ol, C_AcctSchema a "
201             + "WHERE m.C_OrderLine_ID=ol.C_OrderLine_ID"
202             + " AND pc.M_Product_ID=ol.M_Product_ID"
203             + " AND pc.C_AcctSchema_ID=a.C_AcctSchema_ID"
204             + "AND m.M_MatchPO_ID=").append(p_vo.Record_ID).append(") ")
205             .append("WHERE pc.C_AcctSchema_ID=").append(C_AcctSchema_ID)
206             .append(" AND pc.M_Product_ID=").append(p_vo.M_Product_ID);
207         int no = DB.executeUpdate(sql.toString());
208         log.debug("M_Product_Costing - Updated=" + no);
209     } // updateProductInfo
210

211 } // Doc_MatchPO
212
Popular Tags