KickJava   Java API By Example, From Geeks To Geeks.

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


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
20 import org.compiere.util.*;
21
22 /**
23  * Journal Model
24  *
25  * @author Jorg Janke
26  * @version $Id: MJournal.java,v 1.3 2003/08/25 02:31:57 jjanke Exp $
27  */

28 public class MJournal extends X_GL_Journal
29 {
30     public MJournal (Properties ctx, int GL_Journal_ID)
31     {
32         super (ctx, GL_Journal_ID);
33         if (GL_Journal_ID == 0)
34         {
35         // setGL_Journal_ID (0); // PK
36
// setC_AcctSchema_ID (0);
37
// setC_Currency_ID (0);
38
// setC_DocType_ID (0);
39
// setC_Period_ID (0);
40
//
41
setCurrencyRate (Env.ONE);
42             setCurrencyRateType (CURRENCYRATETYPE_Spot);
43             setDateAcct (new Timestamp(System.currentTimeMillis()));
44             setDateDoc (new Timestamp(System.currentTimeMillis()));
45         // setDescription (null);
46
setDocAction (DOCACTION_Process);
47             setDocStatus (DOCSTATUS_Drafted);
48         // setDocumentNo (null);
49
// setGL_Category_ID (0);
50
setPostingType (POSTINGTYPE_Actual);
51             setTotalCr (Env.ZERO);
52             setTotalDr (Env.ZERO);
53             setIsApproved (false);
54             setIsPrinted (false);
55             setPosted (false);
56             setProcessed(false);
57         }
58     } // MJournal
59

60     public MJournal (Properties ctx, ResultSet rs)
61     {
62         super (ctx, rs);
63     } // MJournal
64

65     /**
66      * Overwrite Client/Org if required
67      * @param AD_Client_ID client
68      * @param AD_Org_ID org
69      */

70     public void setClientOrg (int AD_Client_ID, int AD_Org_ID)
71     {
72         super.setClientOrg(AD_Client_ID, AD_Org_ID);
73     } // setClientOrg
74

75     /**
76      * Set Accounting Date.
77      * Set also Period if not set earlier
78      * @param DateAcct date
79      */

80     public void setDateAcct (Timestamp DateAcct)
81     {
82         super.setDateAcct(DateAcct);
83         if (DateAcct == null)
84             return;
85         if (getC_Period_ID() != 0)
86             return;
87         int C_Period_ID = MPeriod.getC_Period_ID(getCtx(), DateAcct);
88         if (C_Period_ID == 0)
89             log.warn("setDateAcct - Period not found");
90         else
91             setC_Period_ID(C_Period_ID);
92     } // setDateAcct
93

94     /**
95      * Set Document No
96      * @param DocumentNo optional DocumentNo
97      */

98     public void setDocumentNo (String JavaDoc DocumentNo)
99     {
100         if (DocumentNo == null || DocumentNo.length() == 0)
101             return;
102         super.setDocumentNo(DocumentNo);
103     } // setDocumentNo
104

105     public void setGL_JournalBatch_ID (int GL_JournalBatch_ID)
106     {
107         super.setGL_JournalBatch_ID(GL_JournalBatch_ID);
108     }
109     public void setC_AcctSchema_ID (int C_AcctSchema_ID)
110     {
111         super.setC_AcctSchema_ID (C_AcctSchema_ID);
112     }
113
114     /**
115      * Set Currency Info
116      * @param C_Currency_ID currenct
117      * @param CurrencyRateType type
118      * @param CurrencyRate rate
119      */

120     public void setCurrency (int C_Currency_ID, String JavaDoc CurrencyRateType, BigDecimal CurrencyRate)
121     {
122         if (C_Currency_ID != 0)
123             setC_Currency_ID(C_Currency_ID);
124         if (CurrencyRateType != null && CurrencyRateType.length() != 0)
125             setCurrencyRateType(CurrencyRateType);
126         if (CurrencyRate != null && CurrencyRate.compareTo(Env.ZERO) == 0)
127             setCurrencyRate(CurrencyRate);
128     } // setCurrency
129

130
131     /**
132      * Save - set DocumentNo if null
133      * @return true if saved
134      */

135     public boolean save()
136     {
137         if (getDocumentNo() == null)
138         {
139             String JavaDoc DocumentNo = DB.getDocumentNo (getAD_Client_ID (), getC_DocType_ID ());
140             setDocumentNo (DocumentNo);
141         }
142         return super.save();
143     } // save
144

145     /*************************************************************************/
146
147     /**
148      * Get Journal Lines
149      * @return Array of lines
150      */

151     public MJournalLine[] getLines()
152     {
153         ArrayList list = new ArrayList();
154         String JavaDoc sql = "SELECT * FROM GL_JournalLine WHERE GL_Journal_ID=? ORDER BY Line";
155         PreparedStatement pstmt = null;
156         try
157         {
158             pstmt = DB.prepareStatement(sql);
159             pstmt.setInt(1, getGL_Journal_ID());
160             ResultSet rs = pstmt.executeQuery();
161             while (rs.next())
162                 list.add(new MJournalLine (getCtx(), rs));
163             rs.close();
164             pstmt.close();
165             pstmt = null;
166         }
167         catch (SQLException ex)
168         {
169             log.error("getLines", ex);
170         }
171         try
172         {
173             if (pstmt != null)
174                 pstmt.close();
175         }
176         catch (SQLException ex1)
177         {
178         }
179         pstmt = null;
180         //
181
MJournalLine[] retValue = new MJournalLine[list.size()];
182         list.toArray(retValue);
183         return retValue;
184     } // getLines
185

186     /**
187      * Copy Lines from other Journal
188      * @param fromJournal Journal
189      * @return number of lines copied
190      */

191     public int copyLinesFrom (MJournal fromJournal)
192     {
193         if (isProcessed() || fromJournal == null)
194             return 0;
195         int count = 0;
196         MJournalLine[] fromLines = fromJournal.getLines();
197         for (int i = 0; i < fromLines.length; i++)
198         {
199             MJournalLine toLine = new MJournalLine (getCtx(), 0);
200             PO.copyValues(fromLines[i], toLine, getAD_Client_ID(), getAD_Org_ID());
201             toLine.setGL_Journal_ID(getGL_Journal_ID());
202             toLine.setDateAcct(getDateAcct());
203             toLine.setIsGenerated(true);
204             if (toLine.save())
205                 count++;
206         }
207         if (fromLines.length != count)
208             log.error("copyLinesFrom - Line difference - JournalLines=" + fromLines.length + " <> Saved=" + count);
209
210         return count;
211     } // copyLinesFrom
212

213 } // MJournal
214
Popular Tags