KickJava   Java API By Example, From Geeks To Geeks.

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


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

27 public class MJournalBatch extends X_GL_JournalBatch
28 {
29     public MJournalBatch (Properties ctx, int GL_JournalBatch_ID)
30     {
31         super (ctx, GL_JournalBatch_ID);
32         if (GL_JournalBatch_ID == 0)
33         {
34         // setGL_JournalBatch_ID (0); PK
35
// setDescription (null);
36
// setDocumentNo (null);
37
setPostingType (POSTINGTYPE_Actual);
38             setProcessed (false);
39             setProcessing ("N");
40             setTotalCr (Env.ZERO);
41             setTotalDr (Env.ZERO);
42         }
43     } // MJournalBatch
44

45     public MJournalBatch (Properties ctx, ResultSet rs)
46     {
47         super (ctx, rs);
48     } // MJournalBatch
49

50     /**
51      * Overwrite Client/Org if required
52      * @param AD_Client_ID client
53      * @param AD_Org_ID org
54      */

55     public void setClientOrg (int AD_Client_ID, int AD_Org_ID)
56     {
57         super.setClientOrg(AD_Client_ID, AD_Org_ID);
58     } // setClientOrg
59

60     /**
61      * Set Accounting Date.
62      * Set also Period if not set earlier
63      * @param DateAcct date
64      */

65     public void setDateAcct (Timestamp DateAcct)
66     {
67         super.setDateAcct(DateAcct);
68         if (DateAcct == null)
69             return;
70         if (getC_Period_ID() != 0)
71             return;
72         int C_Period_ID = MPeriod.getC_Period_ID(getCtx(), DateAcct);
73         if (C_Period_ID == 0)
74             log.warn("setDateAcct - Period not found");
75         else
76             setC_Period_ID(C_Period_ID);
77     } // setDateAcct
78

79     /**
80      * Set Document No
81      * @param DocumentNo optional DocumentNo
82      */

83     public void setDocumentNo (String JavaDoc DocumentNo)
84     {
85         if (DocumentNo == null || DocumentNo.length() == 0)
86             return;
87         super.setDocumentNo(DocumentNo);
88     } // setDocumentNo
89

90
91     /**
92      * Save - set DocumentNo if null
93      * @return true if saved
94      */

95     public boolean save()
96     {
97         if (getDocumentNo() == null)
98         {
99             String JavaDoc DocumentNo = DB.getDocumentNo (getAD_Client_ID(), "N", "GL_JournalBatch");
100             setDocumentNo (DocumentNo);
101         }
102         return super.save();
103     } // save
104

105     /**
106      * Get Journal Lines
107      * @return Array of lines
108      */

109     public MJournal[] getJournals()
110     {
111         ArrayList list = new ArrayList();
112         String JavaDoc sql = "SELECT * FROM GL_Journal WHERE GL_JournalBatch_ID=? ORDER BY DocumentNo";
113         PreparedStatement pstmt = null;
114         try
115         {
116             pstmt = DB.prepareStatement(sql);
117             pstmt.setInt(1, getGL_JournalBatch_ID());
118             ResultSet rs = pstmt.executeQuery();
119             while (rs.next())
120                 list.add(new MJournal (getCtx(), rs));
121             rs.close();
122             pstmt.close();
123             pstmt = null;
124         }
125         catch (SQLException ex)
126         {
127             log.error("getJournals", ex);
128         }
129         try
130         {
131             if (pstmt != null)
132                 pstmt.close();
133         }
134         catch (SQLException ex1)
135         {
136         }
137         pstmt = null;
138         //
139
MJournal[] retValue = new MJournal[list.size()];
140         list.toArray(retValue);
141         return retValue;
142     } // getJournals
143

144     /**
145      * Copy Journal/Lines from other Journal Batch
146      * @param jb Journal Batch
147      * @return number of journals + lines copied
148      */

149     public int copyDetailsFrom (MJournalBatch jb)
150     {
151         if (isProcessed() || jb == null)
152             return 0;
153         int count = 0;
154         int lineCount = 0;
155         MJournal[] fromJournals = jb.getJournals();
156         for (int i = 0; i < fromJournals.length; i++)
157         {
158             MJournal toJournal = new MJournal (getCtx(), 0);
159             PO.copyValues(fromJournals[i], toJournal, getAD_Client_ID(), getAD_Org_ID());
160             toJournal.setGL_JournalBatch_ID(getGL_JournalBatch_ID());
161             toJournal.setValueNoCheck ("DocumentNo", null);
162             toJournal.setDateAcct(getDateAcct());
163             toJournal.setDateDoc(getDateDoc());
164             toJournal.setIsApproved(false);
165             toJournal.setIsPrinted(false);
166             toJournal.setPosted(false);
167             toJournal.setProcessed(false);
168             if (toJournal.save())
169             {
170                 count++;
171                 lineCount += toJournal.copyLinesFrom(fromJournals[i]);
172             }
173         }
174         if (fromJournals.length != count)
175             log.error("copyDetailsFrom - Line difference - Journals=" + fromJournals.length + " <> Saved=" + count);
176
177         return count + lineCount;
178     } // copyLinesFrom
179

180
181     /*************************************************************************/
182
183     /**
184      * Create new Journal Batch by copying
185      * @param ctx context
186      * @param GL_JournalBatch_ID journal batch
187      * @param dateDoc date of the document date
188      * @return Journal Batch
189      */

190     public static MJournalBatch copyFrom (Properties ctx, int GL_JournalBatch_ID, Timestamp dateDoc)
191     {
192         MJournalBatch from = new MJournalBatch (ctx, GL_JournalBatch_ID);
193         if (from.getGL_JournalBatch_ID() == 0)
194             throw new IllegalArgumentException JavaDoc ("From Journal Batch not found GL_JournalBatch_ID=" + GL_JournalBatch_ID);
195         //
196
MJournalBatch to = new MJournalBatch (ctx, 0);
197         PO.copyValues(from, to, from.getAD_Client_ID(), from.getAD_Org_ID());
198         to.setValueNoCheck ("DocumentNo", null);
199         to.setDateAcct(dateDoc);
200         to.setDateDoc(dateDoc);
201         //
202
if (!to.save())
203             throw new IllegalStateException JavaDoc("Could not create Journal Batch");
204
205         if (to.copyDetailsFrom(from) == 0)
206             throw new IllegalStateException JavaDoc("Could not create Journal Batch Details");
207
208         return to;
209     } // copyFrom
210

211 } // MJournalBatch
212
Free Books   Free Magazines  
Popular Tags