KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openbravo > erpCommon > ad_process > ImportTax


1 /*
2  ******************************************************************************
3  * The contents of this file are subject to the Compiere License Version 1.1
4  * ("License"); You may not use this file except in compliance with the License
5  * You may obtain a copy of the License at http://www.compiere.org/license.html
6  * Software distributed under the License is distributed on an "AS IS" basis,
7  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
8  * the specific language governing rights and limitations under the License.
9  * The Original Code is Compiere ERP & CRM Business Solution
10  * The Initial Developer of the Original Code is Jorg Janke and ComPiere, Inc.
11  * Portions created by Jorg Janke are Copyright (C) 1999-2001 Jorg Janke, parts
12  * created by ComPiere are Copyright (C) ComPiere, Inc.; All Rights Reserved.
13  * Contributor(s): Openbravo SL
14  * Contributions are Copyright (C) 2001-2006 Openbravo S.L.
15  ******************************************************************************
16 */

17 package org.openbravo.erpCommon.ad_process;
18
19 import org.openbravo.erpCommon.utility.*;
20 import org.openbravo.base.secureApp.VariablesSecureApp;
21 import javax.servlet.*;
22 import org.apache.log4j.Logger ;
23
24 // imports for transactions
25
import org.openbravo.database.ConnectionProvider;
26 import java.sql.Connection JavaDoc;
27
28
29 public class ImportTax extends ImportProcess {
30   static Logger log4j = Logger.getLogger(ImportBPartner.class);
31
32   private String JavaDoc m_AD_Process_ID = "";
33   private String JavaDoc m_Record_ID = "";
34   private boolean m_deleteOldImported;
35
36   public ImportTax(ConnectionProvider conn, String JavaDoc AD_Process_ID, String JavaDoc recordId, boolean deleteOld) {
37     super(conn);
38     m_AD_Process_ID = AD_Process_ID;
39     m_Record_ID = recordId;
40     m_deleteOldImported = deleteOld;
41   }
42
43   protected String JavaDoc getAD_Process_ID() {
44     return m_AD_Process_ID;
45   }
46
47   protected String JavaDoc getRecord_ID() {
48     return m_Record_ID;
49   }
50
51   protected void createInstanceParams(VariablesSecureApp vars) throws ServletException {
52     if (log4j.isDebugEnabled()) log4j.debug("Creating parameters");
53   }
54
55   protected boolean doIt(VariablesSecureApp vars) throws ServletException {
56     int no = 0;
57     ConnectionProvider conn = null;
58     Connection JavaDoc con = null;
59         try {
60       conn = getConnection();
61             con = conn.getTransactionConnection();
62             if(m_deleteOldImported) {
63                 no = ImportTaxData.deleteOld(con, conn, getAD_Client_ID());
64                 if (log4j.isDebugEnabled()) log4j.debug("Delete Old Imported = " + no);
65             }
66             // Set Client, Org, IaActive, Created/Updated
67
no = ImportTaxData.updateRecords(con, conn, getAD_Client_ID());
68             if (log4j.isDebugEnabled()) log4j.debug("Reset = " + no);
69
70       /* This cannot be done like this, because it can be referred by data inside the file, it has to be done line per line
71             // Tax Category ID
72             no = ImportTaxData.updateTaxCategoryId(con, conn, getAD_Client_ID());
73             if (log4j.isDebugEnabled()) log4j.debug("Tax Category Id = " + no);
74       
75       // BP Tax Category ID
76       no = ImportTaxData.updateBPTaxCategoryId(con, conn, getAD_Client_ID());
77             if (log4j.isDebugEnabled()) log4j.debug("BP Tax Category Id = " + no);
78       
79       // Tax ID
80       no = ImportTaxData.updateTaxId(con, conn, getAD_Client_ID());
81             if (log4j.isDebugEnabled()) log4j.debug("BP Tax Category Id = " + no);*/

82       
83       // Set Country From
84
no = ImportTaxData.updateCountryFromId(con, conn, getAD_Client_ID());
85             if (log4j.isDebugEnabled()) log4j.debug("Set Country From =" + no);
86             
87             no = ImportTaxData.updateCountryFromError(con, conn, getAD_Client_ID());
88             if (log4j.isDebugEnabled()) log4j.debug("Invalid Country From =" + no);
89       
90       // Set Country To
91
no = ImportTaxData.updateCountryToId(con, conn, getAD_Client_ID());
92             if (log4j.isDebugEnabled()) log4j.debug("Set Country to =" + no);
93             
94             no = ImportTaxData.updateCountryToError(con, conn, getAD_Client_ID());
95             if (log4j.isDebugEnabled()) log4j.debug("Invalid Country to =" + no);
96       
97       // Set Region From
98
no = ImportTaxData.updateRegionFromId(con, conn, getAD_Client_ID());
99             if (log4j.isDebugEnabled()) log4j.debug("Set Region From =" + no);
100             
101             no = ImportTaxData.updateRegionFromError(con, conn, getAD_Client_ID());
102             if (log4j.isDebugEnabled()) log4j.debug("Invalid Region From =" + no);
103       
104       // Set Region From
105
no = ImportTaxData.updateRegionToId(con, conn, getAD_Client_ID());
106             if (log4j.isDebugEnabled()) log4j.debug("Set Region To =" + no);
107             
108             no = ImportTaxData.updateRegionToError(con, conn, getAD_Client_ID());
109             if (log4j.isDebugEnabled()) log4j.debug("Invalid Region To =" + no);
110       
111       //
112

113             conn.releaseCommitConnection(con);
114         } catch (Exception JavaDoc se) {
115       try {
116         conn.releaseRollbackConnection(con);
117       } catch (Exception JavaDoc ignored) {}
118             se.printStackTrace();
119             addLog(Utility.messageBD(conn, "ProcessRunError", vars.getLanguage()));
120             return false;
121         }
122
123     // till here, the edition of the I_ImportBPartner table
124
// now, the insertion from I_ImportBPartner table in C_BPartner...
125

126     int noTaxInsert = 0;
127     int noTaxUpdate = 0;
128     int noTCInsert = 0;
129     int noTCUpdate = 0;
130     int noBPTCInsert = 0;
131     int noBPTCUpdate = 0;
132
133         try {
134             // Go through Records
135
ImportTaxData[] data = ImportTaxData.select(conn, getAD_Client_ID());
136             if (log4j.isDebugEnabled()) log4j.debug("Going through " + data.length + " records");
137             for(int i=0;i<data.length;i++){
138                 String JavaDoc I_Tax_ID = data[i].iTaxId;
139                 String JavaDoc C_TaxCategory_ID = data[i].cTaxcategoryId;
140         String JavaDoc C_BPTaxCategory_ID = data[i].cBpTaxcategoryId;
141         String JavaDoc C_Tax_ID = data[i].cTaxId;
142         String JavaDoc ParentTax_ID = data[i].parentTaxId;
143                 boolean newTaxCategory = C_TaxCategory_ID == "";
144         boolean newBPTaxCategory = C_BPTaxCategory_ID == "";
145         boolean newTax = C_Tax_ID == "";
146                 con = conn.getTransactionConnection();
147
148                 // create/update TaxCategory
149
if (!data[i].tcName.equals("") || !newTaxCategory){ //Inserting taxes this shouldn't be null, but it can be if inserting only BP Tax Cat
150
if (newTaxCategory) C_TaxCategory_ID = ImportTaxData.selectTaxCategoryId(conn,data[i].tcName,getAD_Client_ID());
151           newTaxCategory = ((C_TaxCategory_ID == "") || (C_TaxCategory_ID==null));
152           if (log4j.isDebugEnabled()) log4j.debug("TCId: "+C_TaxCategory_ID);
153           if (newTaxCategory) { // Insert new TaxCategory
154
C_TaxCategory_ID = SequenceIdData.getSequence(conn, "C_TaxCategory", vars.getClient());
155             try {
156               no = ImportTaxData.insertTaxCategory(con, conn, C_TaxCategory_ID, I_Tax_ID);
157               if (log4j.isDebugEnabled()) log4j.debug("Insert TaxCategory = " + no);
158               noTCInsert++;
159             } catch (ServletException ex) {
160               String JavaDoc err = "Insert TaxCategory - " + ex.toString();
161               if (log4j.isDebugEnabled()) log4j.debug(err);
162               no = ImportTaxData.taxError(conn, err, I_Tax_ID);
163               try {
164                 conn.releaseRollbackConnection(con);
165               } catch (Exception JavaDoc ignored) {}
166               continue;
167             }
168           } else { // Update existing TaxCategory
169
try {
170               no = ImportTaxData.updateTaxCategory(con, conn, I_Tax_ID, C_TaxCategory_ID);
171               if (log4j.isDebugEnabled()) log4j.debug("Update TaxCategory = " + no);
172               noTCUpdate++;
173             } catch (ServletException ex) {
174               String JavaDoc err = "Update TaxCategory - " + ex.toString();
175               if (log4j.isDebugEnabled()) log4j.debug(err);
176               no = ImportTaxData.taxError(conn, err, I_Tax_ID);
177               try {
178                 conn.releaseRollbackConnection(con);
179               } catch (Exception JavaDoc ignored) {}
180               continue;
181             }
182           }
183         }
184         
185         // create/update BPTaxCategory
186
if (!data[i].bptcName.equals("") || !newBPTaxCategory){
187           if (newBPTaxCategory) C_BPTaxCategory_ID = ImportTaxData.selectBPTaxCategoryId(conn, data[i].bptcName, getAD_Client_ID());
188           newBPTaxCategory = ((C_BPTaxCategory_ID == "") || (C_BPTaxCategory_ID==null));
189           if (newTaxCategory) { // Insert new BPTaxCategory
190
C_BPTaxCategory_ID = SequenceIdData.getSequence(conn, "C_BP_TaxCategory", vars.getClient());
191             try {
192               no = ImportTaxData.insertBPTaxCategory(con, conn, C_BPTaxCategory_ID, I_Tax_ID);
193               if (log4j.isDebugEnabled()) log4j.debug("Insert BPTaxCategory = " + no);
194               noBPTCInsert++;
195             } catch (ServletException ex) {
196               String JavaDoc err = "Insert BPTaxCategory - " + ex.toString();
197               if (log4j.isDebugEnabled()) log4j.debug(err);
198               no = ImportTaxData.taxError(conn, err, I_Tax_ID);
199               try {
200                 conn.releaseRollbackConnection(con);
201               } catch (Exception JavaDoc ignored) {}
202               continue;
203             }
204           } else { // Update existing BPTaxCategory
205
try {
206               no = ImportTaxData.updateBPTaxCategory(con, conn, I_Tax_ID, C_BPTaxCategory_ID);
207               if (log4j.isDebugEnabled()) log4j.debug("Update BPTaxCategory = " + no);
208               noBPTCUpdate++;
209             } catch (ServletException ex) {
210               String JavaDoc err = "Update BPTaxCategory - " + ex.toString();
211               if (log4j.isDebugEnabled()) log4j.debug(err);
212               no = ImportTaxData.taxError(conn, err, I_Tax_ID);
213               try {
214                 conn.releaseRollbackConnection(con);
215               } catch (Exception JavaDoc ignored) {}
216               continue;
217             }
218           }
219         }
220         
221         if (!data[i].tName.equals("") || !newTax) { //Update Create Tax
222
if (log4j.isDebugEnabled()) log4j.debug("Tax:"+data[i].tName);
223            
224            if (!data[i].parentName.equals("")) { //Parent tax
225
ParentTax_ID = ImportTaxData.selectTaxId(conn, data[i].parentName, getAD_Client_ID()); //Check if parent tax is created, if not create with default values
226
if ((ParentTax_ID == "") || (ParentTax_ID==null)) { //Insert parent tax
227
try {
228                  ParentTax_ID = SequenceIdData.getSequence(conn, "C_Tax", vars.getClient());
229                  if (log4j.isDebugEnabled()) log4j.debug("Insert Parent Tax = " + no);
230                  no = ImportTaxData.insertDefaultTax(con,conn, ParentTax_ID, C_TaxCategory_ID,I_Tax_ID);
231                  noTaxInsert++;
232                } catch(ServletException ex) {
233                  String JavaDoc err = "Insert Parent Tax - " + ex.toString();
234                  if (log4j.isDebugEnabled()) log4j.debug(err);
235                  no = ImportTaxData.taxError(conn, err, I_Tax_ID);
236                  try {
237                    conn.releaseRollbackConnection(con);
238                  } catch (Exception JavaDoc ignored) {}
239                  continue;
240                }
241              }
242            }
243            
244            if (newTax) C_Tax_ID = ImportTaxData.selectTaxId(conn, data[i].tName, getAD_Client_ID());
245            newTax = ((C_Tax_ID == "")||(C_Tax_ID==null));
246            if (log4j.isDebugEnabled()) log4j.debug("Tax:"+data[i].tName+" - new:"+newTax);
247            if (newTax) { // Insert new Tax
248
C_Tax_ID = SequenceIdData.getSequence(conn, "C_Tax", vars.getClient());
249             try {
250               no = ImportTaxData.insertTax(con, conn, C_Tax_ID, C_TaxCategory_ID, C_BPTaxCategory_ID, ParentTax_ID, I_Tax_ID);
251               if (log4j.isDebugEnabled()) log4j.debug("Insert Tax = " + no);
252               noTaxInsert++;
253             } catch (ServletException ex) {
254               String JavaDoc err = "Insert Tax - " + ex.toString();
255               if (log4j.isDebugEnabled()) log4j.debug(err);
256               no = ImportTaxData.taxError(conn, err, I_Tax_ID);
257               try {
258                 conn.releaseRollbackConnection(con);
259               } catch (Exception JavaDoc ignored) {}
260               continue;
261             }
262           } else { // Update existing Tax
263
try {
264               no = ImportTaxData.updateTax(con, conn, I_Tax_ID, C_TaxCategory_ID, C_BPTaxCategory_ID, ParentTax_ID, C_Tax_ID);
265               if (log4j.isDebugEnabled()) log4j.debug("Update Tax = " + no);
266               noTaxUpdate++;
267             } catch (ServletException ex) {
268               String JavaDoc err = "Update Tax - " + ex.toString();
269               if (log4j.isDebugEnabled()) log4j.debug(err);
270               no = ImportTaxData.taxError(conn, err, I_Tax_ID);
271               try {
272                 conn.releaseRollbackConnection(con);
273               } catch (Exception JavaDoc ignored) {}
274               continue;
275             }
276           }
277           
278           // Coutry-Region
279
if ((!data[i].cCountryId.equals("")||!data[i].cRegionId.equals("")||!data[i].toCountryId.equals("0")||!data[i].toRegionId.equals("0"))
280               &&ImportTaxData.existsLocation(con, conn, I_Tax_ID, C_Tax_ID).equals("0")) {
281             if (ImportTaxData.hasLocation(con, conn, C_Tax_ID).equals("0")) { //Update Location in c_tax
282
try {
283                 no = ImportTaxData.updateTaxRegion(con, conn, I_Tax_ID, C_Tax_ID);
284                 if (log4j.isDebugEnabled()) log4j.debug("Update Tax Location (in c_tax) = " + no);
285               } catch (ServletException ex) {
286                 String JavaDoc err = "Update Tax Location (in c_tax)- " + ex.toString();
287                 if (log4j.isDebugEnabled()) log4j.debug(err);
288                 no = ImportTaxData.taxError(conn, err, I_Tax_ID);
289                 try {
290                   conn.releaseRollbackConnection(con);
291                 } catch (Exception JavaDoc ignored) {}
292                 continue;
293               }
294             } else{ //create Location in c_tax_zone
295
try {
296                 String JavaDoc C_Tax_Zone_ID = SequenceIdData.getSequence(conn, "C_Tax_Zone", vars.getClient());
297                 no = ImportTaxData.insertTaxZone(con, conn, C_Tax_Zone_ID, C_Tax_ID, I_Tax_ID);
298                 if (log4j.isDebugEnabled()) log4j.debug("Insert tax Location (in c_tax_zone) = " + no);
299               } catch (ServletException ex) {
300                 String JavaDoc err = "Insert tax Location (in c_tax_zone)- " + ex.toString();
301                 if (log4j.isDebugEnabled()) log4j.debug(err);
302                 no = ImportTaxData.taxError(conn, err, I_Tax_ID);
303                 try {
304                   conn.releaseRollbackConnection(con);
305                 } catch (Exception JavaDoc ignored) {}
306                 continue;
307               }
308             }
309           }
310         } //tax
311

312         // Update I_Tax
313
try {
314                     no = ImportTaxData.setImported(con, conn, C_TaxCategory_ID, I_Tax_ID);
315                     conn.releaseCommitConnection(con);
316                 } catch (Exception JavaDoc ex) {
317                     if (log4j.isDebugEnabled()) log4j.debug("Update Imported - " + ex.toString());
318                     noTCInsert--;
319                     no = ImportTaxData.updateSetImportedError(conn, I_Tax_ID);
320           try {
321             conn.releaseRollbackConnection(con);
322           } catch (Exception JavaDoc ignored) {}
323                     continue;
324                 }
325             }
326
327             // Set Error to indicator to not imported
328
no=ImportTaxData.updateNotImported(conn, getAD_Client_ID());
329         } catch (Exception JavaDoc se) {
330             se.printStackTrace();
331             addLog(Utility.messageBD(conn, "ProcessRunError", vars.getLanguage()));
332             return false;
333         }
334     addLog(Utility.messageBD(conn, "Errors", vars.getLanguage()) + ": " + no + "; ");
335     addLog("Tax inserted: " + noTaxInsert + "; ");
336     addLog("Tax updated: " + noTaxUpdate);
337     addLog("Tax Cat inserted: " + noTCInsert);
338     addLog("Tax Cat updated: " + noTCUpdate + "; ");
339     addLog("BP Tax Cat inserted: " + noBPTCInsert);
340     addLog("BP Tax Cat updated: " + noBPTCUpdate + "; ");
341     return true;
342
343   } // doIt
344
}
345
Free Books   Free Magazines  
Popular Tags