KickJava   Java API By Example, From Geeks To Geeks.

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


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 Invoice Documents.
25  * <pre>
26  * Table: GL_Journal (224)
27  * Document Types: GLJ
28  * </pre>
29  * @author Jorg Janke
30  * @version $Id: Doc_GLJournal.java,v 1.12 2003/08/25 02:35:10 jjanke Exp $
31  */

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

38     protected Doc_GLJournal(int AD_Client_ID)
39     {
40         super(AD_Client_ID);
41     }
42
43     private String JavaDoc m_PostingType = Fact.POST_Actual;
44
45     /**
46      * Return TableName of Document
47      * @return GL_Journal
48      */

49     public String JavaDoc getTableName()
50     {
51         return "GL_Journal";
52     } // getTableName
53

54     /**
55      * Get Table ID
56      * @return 224
57      */

58     public int getAD_Table_ID()
59     {
60         return 224;
61     } // getAD_Table_ID
62

63     /**
64      * Load Specific Document Details
65      * @param rs result set
66      * @return true if loadDocumentType was set
67      */

68     protected boolean loadDocumentDetails (ResultSet rs)
69     {
70         loadDocumentType(); // lines require doc type
71
try
72         {
73             m_PostingType = rs.getString ("PostingType");
74         }
75         catch (SQLException ex)
76         {
77             log.error("loadDocumentDetails - load PostingType", ex);
78         }
79         // Contained Objects
80
p_lines = loadLines();
81         log.debug("Lines=" + p_lines.length);
82         return true;
83     } // loadDocumentDetails
84

85
86     /**
87      * Load Invoice Line
88      * @return DocLine Array
89      */

90     private DocLine[] loadLines()
91     {
92         ArrayList list = new ArrayList();
93         String JavaDoc sql = "SELECT jl.GL_JournalLine_ID,jl.AD_Client_ID,jl.AD_Org_ID," // 1..3
94
+ " jl.Line,jl.Description," // 4..5
95
+ " jl.C_Currency_ID,jl.AmtSourceDr,jl.AmtSourceCr," // 6..8
96
+ " jl.CurrencyRateType,jl.CurrencyRate," // 9..10
97
+ " jl.DateAcct,jl.AmtAcctDr,jl.AmtAcctCr," // 11..13
98
+ " jl.C_UOM_ID,jl.Qty," // 14..15
99
+ " vc.C_AcctSchema_ID,vc.C_ValidCombination_ID," // 16..17
100
+ " vc.Account_ID,vc.M_Product_ID,vc.C_BPartner_ID," // 18..20
101
+ " vc.AD_OrgTrx_ID,vc.C_LocFrom_ID,vc.C_LocTo_ID,"
102             + " vc.C_SalesRegion_ID,vc.C_Project_ID,vc.C_Campaign_ID,"
103             + " vc.C_Activity_ID,vc.User1_ID,vc.User2_ID "
104             //
105
+ "FROM C_ValidCombination vc, GL_JournalLine jl "
106             + "WHERE vc.C_ValidCombination_ID=jl.C_ValidCombination_ID"
107             + " AND jl.GL_Journal_ID=?"
108             + " AND jl.IsActive='Y' AND vc.IsFullyQualified='Y' "
109             + "ORDER BY jl.Line";
110         try
111         {
112             PreparedStatement pstmt = DB.prepareStatement(sql);
113             pstmt.setInt(1, p_vo.Record_ID);
114             ResultSet rs = pstmt.executeQuery();
115             //
116
while (rs.next())
117             {
118                 int Line_ID = rs.getInt(1);
119                 DocLine docLine = new DocLine (p_vo.DocumentType, p_vo.Record_ID, Line_ID);
120                 docLine.loadAttributes (rs, p_vo);
121                 // -- Source Amounts
122
BigDecimal AmtSourceDr = rs.getBigDecimal(7);
123                 BigDecimal AmtSourceCr = rs.getBigDecimal(8);
124                 docLine.setAmount (AmtSourceDr, AmtSourceCr);
125                 // -- Converted Amounts
126
int C_AcctSchema_ID = rs.getInt(16);
127                 BigDecimal AmtAcctDr = rs.getBigDecimal(12);
128                 BigDecimal AmtAcctCr = rs.getBigDecimal(13);
129                 docLine.setConvertedAmt (C_AcctSchema_ID, AmtAcctDr, AmtAcctCr);
130                 // -- Account
131
int C_ValidCombination_ID = rs.getInt(17);
132                 Account acct = new Account(C_ValidCombination_ID);
133                 docLine.setAccount (acct);
134                 // -- Set Org from account (x-org)
135
docLine.setAD_Org_ID (acct.getAD_Org_ID());
136                 //
137
list.add(docLine);
138             }
139             //
140
rs.close();
141             pstmt.close();
142         }
143         catch (SQLException e)
144         {
145             log.error ("loadLines - SQL=" + sql, e);
146         }
147
148         // Return Array
149
int size = list.size();
150         DocLine[] dl = new DocLine[size];
151         list.toArray(dl);
152         return dl;
153     } // loadLines
154

155     /*************************************************************************/
156
157     /**
158      * Get Source Currency Balance - subtracts line and tax amounts from total - no rounding
159      * @return positive amount, if total invoice is bigger than lines
160      */

161     public BigDecimal getBalance()
162     {
163         BigDecimal retValue = Env.ZERO;
164         StringBuffer JavaDoc sb = new StringBuffer JavaDoc (" [");
165         // Lines
166
for (int i = 0; i < p_lines.length; i++)
167         {
168             retValue = retValue.add(p_lines[i].getAmount());
169             sb.append("+").append(p_lines[i].getAmount());
170         }
171         sb.append("]");
172         //
173
log.debug(toString() + " Balance=" + retValue + sb.toString());
174         return retValue;
175     } // getBalance
176

177     /**
178      * Create Facts (the accounting logic) for
179      * GLJ.
180      * (only for the accounting scheme, it was created)
181      * <pre>
182      * account DR CR
183      * </pre>
184      * @param as acct schema
185      * @return Fact
186      */

187     public Fact createFact (AcctSchema as)
188     {
189         // create Fact Header
190
Fact fact = new Fact (this, as, m_PostingType);
191
192         // GLJ
193
if (p_vo.DocumentType.equals(DocVO.DOCTYPE_GLJournal))
194         {
195             // account DR CR
196
for (int i = 0; i < p_lines.length; i++)
197             {
198                 if (p_lines[i].getC_AcctSchema_ID () == as.getC_AcctSchema_ID ())
199                 {
200                     FactLine line = fact.createLine (p_lines[i],
201                                     p_lines[i].getAccount (),
202                                     p_vo.C_Currency_ID,
203                                     p_lines[i].getAmtSourceDr (),
204                                     p_lines[i].getAmtSourceCr ());
205                 }
206             } // for all lines
207
}
208         else
209         {
210             p_vo.Error = "DocumentType unknown: " + p_vo.DocumentType;
211             log.error("createFact - " + p_vo.Error);
212             fact = null;
213         }
214         return fact;
215     } // createFact
216

217 } // Doc_GLJournal
218
Popular Tags