1 14 package org.compiere.model; 15 16 import java.util.*; 17 import java.sql.*; 18 19 import org.compiere.util.*; 20 21 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 setPostingType (POSTINGTYPE_Actual); 38 setProcessed (false); 39 setProcessing ("N"); 40 setTotalCr (Env.ZERO); 41 setTotalDr (Env.ZERO); 42 } 43 } 45 public MJournalBatch (Properties ctx, ResultSet rs) 46 { 47 super (ctx, rs); 48 } 50 55 public void setClientOrg (int AD_Client_ID, int AD_Org_ID) 56 { 57 super.setClientOrg(AD_Client_ID, AD_Org_ID); 58 } 60 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 } 79 83 public void setDocumentNo (String DocumentNo) 84 { 85 if (DocumentNo == null || DocumentNo.length() == 0) 86 return; 87 super.setDocumentNo(DocumentNo); 88 } 90 91 95 public boolean save() 96 { 97 if (getDocumentNo() == null) 98 { 99 String DocumentNo = DB.getDocumentNo (getAD_Client_ID(), "N", "GL_JournalBatch"); 100 setDocumentNo (DocumentNo); 101 } 102 return super.save(); 103 } 105 109 public MJournal[] getJournals() 110 { 111 ArrayList list = new ArrayList(); 112 String 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 MJournal[] retValue = new MJournal[list.size()]; 140 list.toArray(retValue); 141 return retValue; 142 } 144 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 } 180 181 182 183 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 ("From Journal Batch not found GL_JournalBatch_ID=" + GL_JournalBatch_ID); 195 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 if (!to.save()) 203 throw new IllegalStateException ("Could not create Journal Batch"); 204 205 if (to.copyDetailsFrom(from) == 0) 206 throw new IllegalStateException ("Could not create Journal Batch Details"); 207 208 return to; 209 } 211 }
| Popular Tags
|