KickJava   Java API By Example, From Geeks To Geeks.

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


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.businessUtility.*;
20 import org.openbravo.erpCommon.reference.*;
21 import org.openbravo.erpCommon.ad_actionButton.*;
22 import org.openbravo.erpCommon.ad_callouts.SEOrderBPartnerData;
23 import org.openbravo.erpCommon.utility.*;
24 import org.openbravo.exception.*;
25 import org.openbravo.base.secureApp.VariablesSecureApp;
26 import java.sql.*;
27 import java.math.BigDecimal JavaDoc;
28 import javax.servlet.*;
29 import java.io.IOException JavaDoc;
30 import org.apache.log4j.Logger ;
31
32 // imports for transactions
33
import org.openbravo.database.ConnectionProvider;
34 import java.sql.Connection JavaDoc;
35
36
37 public class ImportOrder extends ImportProcess {
38   static Logger log4j = Logger.getLogger(ImportOrder.class);
39
40   static int total = 0;
41
42   private String JavaDoc m_AD_Process_ID = "";
43   private String JavaDoc m_Record_ID = "";
44   private boolean m_deleteOldImported;
45
46   public ImportOrder(ConnectionProvider conn, String JavaDoc AD_Process_ID, String JavaDoc recordId, boolean deleteOld) {
47     super(conn);
48     m_AD_Process_ID = AD_Process_ID;
49     m_Record_ID = recordId;
50     m_deleteOldImported = deleteOld;
51   }
52
53   protected String JavaDoc getAD_Process_ID() {
54     return m_AD_Process_ID;
55   }
56
57   protected String JavaDoc getRecord_ID() {
58     return m_Record_ID;
59   }
60
61   protected void createInstanceParams(VariablesSecureApp vars) throws ServletException {
62     if (log4j.isDebugEnabled()) log4j.debug("Creating parameters");
63   }
64
65   protected boolean doIt(VariablesSecureApp vars) throws ServletException {
66     ConnectionProvider conn = null;
67     Connection JavaDoc con = null;
68     try {
69       int no = 0;
70       conn = getConnection();
71       con = conn.getTransactionConnection();
72       if(m_deleteOldImported) {
73         no = ImportOrderData.deleteOld(con, conn, getAD_Client_ID());
74         if (log4j.isDebugEnabled()) log4j.debug("Delete Old Imported = " + no);
75       }
76       // Set Client, Org, IsActive, Created/Updated
77
no = ImportOrderData.updateRecords(con, conn, getAD_Client_ID());
78       if (log4j.isDebugEnabled()) log4j.debug("ImportOrder updated = " + no);
79       no = ImportOrderData.updateInvalidVendor(con, conn, getAD_Org_ID(), getAD_Client_ID());
80       if (log4j.isDebugEnabled()) log4j.debug("Invalid vendor errors = " + no);
81       // Invalid organization
82
no = ImportOrderData.updateRecordsError(con, conn, getAD_Client_ID());
83       if (log4j.isDebugEnabled()) log4j.debug("Invalid organization errors = " + no);
84       // Document Type - PO - SO
85
no = ImportOrderData.updateDocTypePO(con, conn, getAD_Client_ID());
86       if (log4j.isDebugEnabled()) log4j.debug("Updated DocTypePO = " + no);
87       no = ImportOrderData.updateDocTypeSO(con, conn, getAD_Client_ID());
88       if (log4j.isDebugEnabled()) log4j.debug("Updated DocTypeSO = " + no);
89       no = ImportOrderData.updateDocType(con, conn, getAD_Client_ID());
90       if (log4j.isDebugEnabled()) log4j.debug("Updated DocType = " + no);
91       no = ImportOrderData.updateDocTypeError(con, conn, getAD_Client_ID());
92       if (log4j.isDebugEnabled()) log4j.debug("Invalid DocType errors = " + no);
93       // DocType Default
94
no = ImportOrderData.updateDocTypePODefault(con, conn, getAD_Client_ID());
95       if (log4j.isDebugEnabled()) log4j.debug("Updated DocTypePO default = " + no);
96       no = ImportOrderData.updateDocTypeSODefault(con, conn, getAD_Client_ID());
97       if (log4j.isDebugEnabled()) log4j.debug("Updated DocTypeSO default = " + no);
98       no = ImportOrderData.updateDocTypeDefault(con, conn, getAD_Client_ID());
99       if (log4j.isDebugEnabled()) log4j.debug("Updated DocType default = " + no);
100       no = ImportOrderData.updateDocTypeDefaultError(con, conn, getAD_Client_ID());
101       if (log4j.isDebugEnabled()) log4j.debug("Invalid DocType default errors = " + no);
102       // Set IsSOTrx
103
no = ImportOrderData.updateIsSOTrxY(con, conn, getAD_Client_ID());
104       if (log4j.isDebugEnabled()) log4j.debug("Updated IsSOTrx=Y = " + no);
105       no = ImportOrderData.updateIsSOTrxN(con, conn, getAD_Client_ID());
106       if (log4j.isDebugEnabled()) log4j.debug("Updated IsSOTrx=N = " + no);
107       // Price List
108
no = ImportOrderData.updatePriceListCurrencyDefault(con, conn, getAD_Client_ID());
109       if (log4j.isDebugEnabled()) log4j.debug("Updated PriceList by currency (default) = " + no);
110       no = ImportOrderData.updatePriceListNullCurrencyDefault(con, conn, getAD_Client_ID());
111       if (log4j.isDebugEnabled()) log4j.debug("Updated PriceList with null currency (default) = " + no);
112       no = ImportOrderData.updatePriceListCurrency(con, conn, getAD_Client_ID());
113       if (log4j.isDebugEnabled()) log4j.debug("Updated PriceList by currency = " + no);
114       no = ImportOrderData.updatePriceListNullCurrency(con, conn, getAD_Client_ID());
115       if (log4j.isDebugEnabled()) log4j.debug("Updated PriceList with null currency = " + no);
116       no = ImportOrderData.updatePriceListError(con, conn, getAD_Client_ID());
117       if (log4j.isDebugEnabled()) log4j.debug("Invalid PriceList errors = " + no);
118       // Payment Term
119
no = ImportOrderData.updatePaymentTerm(con, conn, getAD_Client_ID());
120       if (log4j.isDebugEnabled()) log4j.debug("Updated PaymentTerm = " + no);
121       no = ImportOrderData.updatePaymentTermDefault(con, conn, getAD_Client_ID());
122       if (log4j.isDebugEnabled()) log4j.debug("Updated PaymentTerm default = " + no);
123       no = ImportOrderData.updatePaymentTermError(con, conn, getAD_Client_ID());
124       if (log4j.isDebugEnabled()) log4j.debug("Invalid PaymentTerm errors = " + no);
125       // Warehouse
126
no = ImportOrderData.updateWarehouse(con, conn, getAD_Client_ID());
127       if (log4j.isDebugEnabled()) log4j.debug("Updated Warehouse = " + no);
128       no = ImportOrderData.updateWarehouseOther(con, conn, getAD_Client_ID());
129       if (log4j.isDebugEnabled()) log4j.debug("Updated Warehouse (other) = " + no);
130       no = ImportOrderData.updateWarehouseError(con, conn, getAD_Client_ID());
131       if (log4j.isDebugEnabled()) log4j.debug("Invalid Warehouse errors = " + no);
132       // BusinessPartner
133
// import depending on the external value
134
no = ImportOrderData.updateBPartnerFromValue(con, conn, getAD_Client_ID());
135       if (log4j.isDebugEnabled()) log4j.debug("Updated BPartner from value = " + no);
136       no = ImportOrderData.updateBPartnerFromEmail(con, conn, getAD_Client_ID());
137       if (log4j.isDebugEnabled()) log4j.debug("Updated BPartner from email = " + no);
138       no = ImportOrderData.updateBPartnerFromContact(con, conn, getAD_Client_ID());
139       if (log4j.isDebugEnabled()) log4j.debug("Updated BPartner from contact = " + no);
140       no = ImportOrderData.updateBPartnerFromName(con, conn, getAD_Client_ID());
141       if (log4j.isDebugEnabled()) log4j.debug("Updated BPartner from name = " + no);
142       no = ImportOrderData.updateBPartnerFromUPC(con, conn, getAD_Client_ID());
143       if (log4j.isDebugEnabled()) log4j.debug("Updated BPartner from upc = " + no);
144       no = ImportOrderData.updateBPartnerDefault(con, conn, getAD_Client_ID());
145       if (log4j.isDebugEnabled()) log4j.debug("Updated BPartner (default) = " + no);
146       // update PriceList and PaymentTerm according to BPartner
147
no = ImportOrderData.updatePriceListFromBPartner(con, conn, getAD_Client_ID());
148       if (log4j.isDebugEnabled()) log4j.debug("Updated PriceList from BPartner = " + no);
149       no = ImportOrderData.updatePOPriceListFromBPartner(con, conn, getAD_Client_ID());
150       if (log4j.isDebugEnabled()) log4j.debug("Updated POPriceList from BPartner = " + no);
151       no = ImportOrderData.updatePaymentTermFromBPartner(con, conn, getAD_Client_ID());
152       if (log4j.isDebugEnabled()) log4j.debug("Updated PaymentTerm from BPartner = " + no);
153       no = ImportOrderData.updatePOPaymentTermFromBPartner(con, conn, getAD_Client_ID());
154       if (log4j.isDebugEnabled()) log4j.debug("Updated POPaymentTerm from BPartner = " + no);
155       // Location
156
no = ImportOrderData.updateLocationByUPC(con, conn, getAD_Client_ID());
157       if (log4j.isDebugEnabled()) log4j.debug("Updated Location by UPC = " + no);
158       no = ImportOrderData.updateBilltoByUPC(con, conn, getAD_Client_ID());
159       if (log4j.isDebugEnabled()) log4j.debug("Updated bill location by UPC = " + no);
160       no = ImportOrderData.updateLocationExisting(con, conn, getAD_Client_ID());
161       if (log4j.isDebugEnabled()) log4j.debug("Updated Location (existing) = " + no);
162       no = ImportOrderData.updateBillLocation(con, conn, getAD_Client_ID());
163       if (log4j.isDebugEnabled()) log4j.debug("Updated BillLocation = " + no);
164       no = ImportOrderData.updateLocation(con, conn, getAD_Client_ID());
165       if (log4j.isDebugEnabled()) log4j.debug("Updated Location = " + no);
166       no = ImportOrderData.updateLocationError(con, conn, getAD_Client_ID());
167       if (log4j.isDebugEnabled()) log4j.debug("Invalid Location errors = " + no);
168       // Country
169
no = ImportOrderData.updateCountryDefault(con, conn, getAD_Client_ID());
170       if (log4j.isDebugEnabled()) log4j.debug("Updated Country (default) = " + no);
171       no = ImportOrderData.updateCountry(con, conn, getAD_Client_ID());
172       if (log4j.isDebugEnabled()) log4j.debug("Updated Country = " + no);
173       no = ImportOrderData.updateCountryError(con, conn, getAD_Client_ID());
174       if (log4j.isDebugEnabled()) log4j.debug("Invalid Country errors = " + no);
175       // Set Region
176
no = ImportOrderData.updateRegionDefault(con, conn, getAD_Client_ID());
177       if (log4j.isDebugEnabled()) log4j.debug("Updated Region (default) = " + no);
178       no = ImportOrderData.updateRegion(con, conn, getAD_Client_ID());
179       if (log4j.isDebugEnabled()) log4j.debug("Updated Region = " + no);
180       no = ImportOrderData.updateRegionError(con, conn, getAD_Client_ID());
181       if (log4j.isDebugEnabled()) log4j.debug("Invalid Region errors = " + no);
182       // Product
183
no = ImportOrderData.updateProductFromValue(con, conn, getAD_Client_ID());
184       if (log4j.isDebugEnabled()) log4j.debug("Updated Product from value = " + no);
185       no = ImportOrderData.updateProductFromUpc(con, conn, getAD_Client_ID());
186       if (log4j.isDebugEnabled()) log4j.debug("Updated Product from UPC = " + no);
187       no = ImportOrderData.updateProductFromSku(con, conn, getAD_Client_ID());
188       if (log4j.isDebugEnabled()) log4j.debug("Updated Product from SKU = " + no);
189       no = ImportOrderData.updateProductError(con, conn, getAD_Client_ID());
190       if (log4j.isDebugEnabled()) log4j.debug("Invalid Product errors = " + no);
191       // Tax
192
no = ImportOrderData.updateTax(con, conn, getAD_Client_ID());
193       if (log4j.isDebugEnabled()) log4j.debug("Updated Tax = " + no);
194       no = ImportOrderData.updateTaxError(con, conn, getAD_Client_ID());
195       if (log4j.isDebugEnabled()) log4j.debug("Invalid Tax errors = " + no);
196       conn.releaseCommitConnection(con);
197   
198       // till here, the edition of the I_Order table
199
// now, the insertion from I_Order table in C_Order
200
// New BPartner
201
ImportOrderData[] data = ImportOrderData.selectWithoutBP(conn, getAD_Client_ID());
202       if (log4j.isDebugEnabled()) log4j.debug("Going through " + data.length + " records");
203       for (int i=0; i<data.length; i++) {
204         con = conn.getTransactionConnection();
205         if (data[i].bpartnervalue==null || data[i].bpartnervalue=="") {
206           if (data[i].email != null && !data[i].email.equals(""))
207             data[i].bpartnervalue = data[i].email;
208           else if (data[i].name != null && !data[i].name.equals(""))
209             data[i].bpartnervalue = data[i].name;
210           else
211             continue;
212         }
213         if (data[i].name == null || data[i].name.equals("")) {
214           if (data[i].contactname != null && !data[i].contactname.equals(""))
215             data[i].name = data[i].contactname;
216           else
217             data[i].name = data[i].bpartnervalue;
218         }
219         BPartnerData bp = new BPartnerData();
220         bp.cBpartnerId = SequenceIdData.getSequence(conn, "C_BPartner", vars.getClient());
221         bp.adClientId = data[i].adClientId;
222         bp.adOrgId = data[i].adOrgId;
223         bp.value = data[i].bpartnervalue;
224         bp.name = data[i].name;
225         bp.insert(con, conn);
226         data[i].cBpartnerId = bp.cBpartnerId;
227         LocationData loc = new LocationData();
228         loc.cLocationId = SequenceIdData.getSequence(conn, "C_Location", vars.getClient());
229         loc.adClientId = data[i].adClientId;
230         loc.adOrgId = data[i].adOrgId;
231         loc.address1 = data[i].address1;
232         loc.address2 = data[i].address2;
233         loc.city = data[i].city;
234         loc.postal = data[i].postal;
235         if (data[i].cRegionId != null)
236           loc.cRegionId = data[i].cRegionId;
237         loc.cCountryId = data[i].cCountryId;
238         loc.insert(con, conn);
239         data[i].cLocationId = loc.cLocationId;
240         BPartnerLocationData bpl = new BPartnerLocationData();
241         bpl.cBpartnerLocationId = SequenceIdData.getSequence(conn, "C_BPartner_Location", vars.getClient());
242         bpl.adClientId = data[i].adClientId;
243         bpl.adOrgId = data[i].adOrgId;
244         bpl.cBpartnerId = data[i].cBpartnerId;
245         bpl.cLocationId = data[i].cLocationId;
246         bpl.insert(con, conn);
247         data[i].cBpartnerLocationId = bpl.cBpartnerLocationId;
248         data[i].billtoId = bpl.cBpartnerLocationId;
249         if (data[i].contactname != null || data[i].email != null || data[i].phone != null) {
250           UserData user = new UserData();
251           user.adUserId = SequenceIdData.getSequence(conn, "AD_User", vars.getClient());
252           user.adClientId = data[i].adClientId;
253           user.adOrgId = data[i].adOrgId;
254           user.cBpartnerId = data[i].cBpartnerId;
255           if (data[i].contactname != null && !data[i].contactname.equals(""))
256             user.name = data[i].contactname;
257           else
258             user.name = data[i].name;
259           user.email = data[i].email;
260           user.phone = data[i].phone;
261           user.insert(con, conn);
262           data[i].adUserId = user.adUserId;
263           data[i].updatedby = user.adUserId;
264         }
265         data[i].update(con, conn);
266         conn.releaseCommitConnection(con);
267       }
268       con = conn.getTransactionConnection();
269       no = ImportOrderData.updateBPartnerError(con, conn, getAD_Client_ID());
270       if (log4j.isDebugEnabled()) log4j.debug("Invalid BPartner errors = " + no);
271       conn.releaseCommitConnection(con);
272       // New Order
273
int noInsert = 0;
274       int noInsertLine = 0;
275       data = ImportOrderData.selectNotImported(conn, getAD_Client_ID());
276       if (log4j.isDebugEnabled()) log4j.debug("Going through " + data.length + " records");
277       String JavaDoc C_BPartner_ID = "";
278       String JavaDoc BillTo_ID = "";
279       String JavaDoc C_BPartner_Location_ID = "";
280       COrderData corder = null;
281       String JavaDoc order_documentno="";
282       String JavaDoc corder_corderid="";
283       String JavaDoc corder_mpricelistid="";
284       String JavaDoc corder_ccurrencyid="";
285       String JavaDoc corder_cbpartnerid="";
286       String JavaDoc corder_cbpartnerlocationid="";
287       String JavaDoc strPostMessage = "";
288       int lineNo = 0;
289       for (int i=0; i<data.length; i++) {
290         con = conn.getTransactionConnection();
291         if (!order_documentno.equals(data[i].documentno)) {
292   // if (!C_BPartner_ID.equals(data[i].cBpartnerId) || !BillTo_ID.equals(data[i].billtoId) || !C_BPartner_Location_ID.equals(data[i].cBpartnerLocationId)) {
293
order_documentno = data[i].documentno;
294           C_BPartner_ID = data[i].cBpartnerId;
295           BillTo_ID = data[i].billtoId;
296           C_BPartner_Location_ID = data[i].cBpartnerLocationId;
297           //Looking for same order yet inserted
298
ImportOrderData[] orderInserted = ImportOrderData.selectOrderInserted(conn, getAD_Client_ID(), data[i].documentno, data[i].dateordered);
299           if (orderInserted!=null && orderInserted.length==0) {
300             corder = new COrderData();
301             corder.cOrderId = SequenceIdData.getSequence(conn, "C_Order", vars.getClient());
302             if (log4j.isDebugEnabled()) log4j.debug("Creating new order with id = " + corder.cOrderId);
303             corder.adClientId = data[i].adClientId;
304             corder.adOrgId = data[i].adOrgId;
305             corder.cDoctypetargetId = data[i].cDoctypeId;
306             corder.cDoctypeId = "0";
307             if (log4j.isDebugEnabled()) log4j.debug("data[i].cBpartnerLocationId: " + data[i].cBpartnerLocationId);
308             corder.cBpartnerLocationId = data[i].cBpartnerLocationId;
309             if (log4j.isDebugEnabled()) log4j.debug("data[i].description: " + data[i].description);
310             corder.description = data[i].description;
311             if (data[i].description != null && !data[i].description.equals("")) {
312               String JavaDoc location_name = ImportOrderData.selectLocationName(con, conn, data[i].description);
313               if (log4j.isDebugEnabled()) log4j.debug("location_name: " + location_name);
314               if (location_name != null && !location_name.equals("")) {
315                 corder.description += " " + location_name;
316                 if (log4j.isDebugEnabled()) log4j.debug("corder.description: " + corder.description);
317               }
318             }
319             corder.issotrx = data[i].issotrx;
320             if (data[i].documentno != null && !data[i].documentno.equals("")) {
321               corder.documentno = data[i].documentno;
322             } else {
323               String JavaDoc docTargetType = ImportOrderData.cDoctypeTarget(con, conn, vars.getUser(), vars.getOrg());
324               corder.documentno = Utility.getDocumentNo(conn, vars, "", "C_Order", docTargetType, docTargetType, false, true);
325             }
326             corder.docstatus = "DR";
327             corder.docaction = "CO";
328             corder.processing = "N";
329             corder.cBpartnerId= data[i].cBpartnerId;
330             corder.billtoId = data[i].billtoId;
331             corder.cBpartnerLocationId = data[i].cBpartnerLocationId;
332             if (!data[i].adUserId.equals("") && Integer.parseInt(data[i].adUserId) != 0)
333               corder.adUserId = data[i].adUserId;
334             else
335               corder.adUserId = vars.getUser();
336             corder.cPaymenttermId = data[i].cPaymenttermId;
337             corder.mPricelistId = data[i].mPricelistId;
338             corder.mWarehouseId = data[i].mWarehouseId;
339             if (!data[i].mShipperId.equals("") && Integer.parseInt(data[i].mShipperId) != 0)
340               corder.mShipperId = data[i].mShipperId;
341             if (!data[i].salesrepId.equals("") && Integer.parseInt(data[i].salesrepId) != 0)
342               corder.salesrepId = data[i].salesrepId;
343             if (!data[i].adOrgtrxId.equals("") && Integer.parseInt(data[i].adOrgtrxId) != 0)
344               corder.adOrgtrxId = data[i].adOrgtrxId;
345             if (!data[i].cActivityId.equals("") && Integer.parseInt(data[i].cActivityId) != 0)
346               corder.cActivityId = data[i].cActivityId;
347             if (!data[i].cCampaignId.equals("") && Integer.parseInt(data[i].cCampaignId) != 0)
348               corder.cCampaignId = data[i].cCampaignId;
349             if (!data[i].cProjectId.equals("") && Integer.parseInt(data[i].cProjectId) != 0)
350               corder.cProjectId = data[i].cProjectId;
351             if (data[i].dateordered != null && !data[i].dateordered.equals(""))
352               corder.dateordered = data[i].dateordered;
353             if (data[i].datepromised != null && !data[i].datepromised.equals(""))
354               corder.datepromised = data[i].datepromised;
355             if (log4j.isDebugEnabled()) log4j.debug("getting bp values as default");
356             SEOrderBPartnerData[] data1 = SEOrderBPartnerData.select(conn, data[i].cBpartnerId);
357             String JavaDoc tmpCurrency = COrderData.selectCurrency(conn, vars.getUser(), data[i].cBpartnerId);
358             corder.isdiscountprinted = "N";
359             if (log4j.isDebugEnabled()) log4j.debug("stablishing default values");
360             corder.cCurrencyId = (tmpCurrency == null || tmpCurrency.equals(""))?"102":tmpCurrency; // euro as default
361
corder.paymentrule = data1[0].paymentrule.equals("")?"":data1[0].paymentrule;
362             if (data1[0].paymentrule != null && !data1[0].paymentrule.equals("")) {
363               corder.paymentrule = data1[0].paymentrule;
364             } else {
365               String JavaDoc defaultPaymentRule = ImportOrderData.defaultValue(con, conn, "C_Order", "PaymentRule");
366               corder.paymentrule = (defaultPaymentRule.equals(""))?"P":defaultPaymentRule; // P = on credit
367
}
368             if (log4j.isDebugEnabled()) log4j.debug("corder.paymentrule = " + corder.paymentrule);
369             if (data1[0].invoicerule != null && !data1[0].invoicerule.equals("")) {
370               corder.invoicerule = data1[0].invoicerule;
371             } else {
372               String JavaDoc defaultInvoiceRule = ImportOrderData.defaultValue(con, conn, "C_Order", "InvoiceRule");
373               corder.invoicerule = (defaultInvoiceRule.equals(""))?"I":defaultInvoiceRule; // I = immediate
374
}
375             if (log4j.isDebugEnabled()) log4j.debug("corder.invoicerule = " + corder.invoicerule);
376             if (data1[0].deliveryrule != null && !data1[0].deliveryrule.equals("")) {
377               corder.deliveryrule = data1[0].deliveryrule;
378             } else {
379               String JavaDoc defaultDeliveryRule = ImportOrderData.defaultValue(con, conn, "C_Order", "DeliveryRule");
380               corder.deliveryrule = (defaultDeliveryRule.equals(""))?"A":defaultDeliveryRule; // A = availability
381
}
382             if (log4j.isDebugEnabled()) log4j.debug("corder.deliveryrule = " + corder.deliveryrule);
383             if (data1[0].deliveryviarule != null && !data1[0].deliveryviarule.equals("")) {
384               corder.deliveryviarule = data1[0].deliveryviarule;
385             } else {
386               String JavaDoc defaultDeliveryViaRule = ImportOrderData.defaultValue(con, conn, "C_Order", "DeliveryViaRule");
387               corder.deliveryviarule = (defaultDeliveryViaRule.equals(""))?"S":defaultDeliveryViaRule; // S = shipper
388
}
389             if (log4j.isDebugEnabled()) log4j.debug("corder.deliveryviarule = " + corder.deliveryviarule);
390             if (data1[0].freightcostrule != null && !data1[0].freightcostrule.equals("")) {
391               corder.freightcostrule = data1[0].freightcostrule;
392             } else {
393               String JavaDoc defaultFreightcostRule = ImportOrderData.defaultValue(con, conn, "C_Order", "FreightCostRule");
394               corder.freightcostrule = (defaultFreightcostRule.equals(""))?"I":defaultFreightcostRule; // I = included
395
}
396             if (log4j.isDebugEnabled()) log4j.debug("corder.freightcostrule = " + corder.freightcostrule);
397             if (data1[0].cPaymenttermId != null && !data1[0].cPaymenttermId.equals("")) {
398               corder.cPaymenttermId = data1[0].cPaymenttermId;
399             }
400             if (log4j.isDebugEnabled()) log4j.debug("corder.cPaymenttermId = " + corder.cPaymenttermId);
401             if (data1[0].paymentrule != null && !data1[0].paymentrule.equals("")) {
402               corder.paymentrule = data1[0].paymentrule;
403             } else {
404               String JavaDoc defaultPaymentRule = ImportOrderData.defaultValue(con, conn, "C_Order", "PaymentRule");
405               corder.paymentrule = (defaultPaymentRule.equals(""))?"P":defaultPaymentRule; // P = on credit
406
}
407             if (log4j.isDebugEnabled()) log4j.debug("corder.paymentrule = " + corder.paymentrule);
408             if (data1[0].salesrepId != null && !data1[0].salesrepId.equals("")) {
409               String JavaDoc salesrep = ImportOrderData.selectSalesRep(con, conn, data[i].cBpartnerId);
410               corder.salesrepId = salesrep;
411             }
412             if (log4j.isDebugEnabled()) log4j.debug("corder.salesrepId = " + corder.salesrepId);
413             String JavaDoc user = ImportOrderData.defaultUser(con, conn, data[i].cBpartnerId);
414             if (user != null && !user.equals("")) {
415               corder.adUserId = user;
416             }
417             if (log4j.isDebugEnabled()) log4j.debug("other default values");
418             corder.priorityrule = "5"; // medium
419
corder_corderid = corder.cOrderId;
420             corder_mpricelistid = corder.mPricelistId;
421             corder_ccurrencyid = corder.cCurrencyId;
422             corder_cbpartnerid = corder.cBpartnerId;
423             corder_cbpartnerlocationid = corder.cBpartnerLocationId;
424             corder.insert(con, conn);
425             noInsert++;
426             lineNo = 10;
427           }
428           else{
429             corder_corderid = orderInserted[0].cOrderId;
430             C_BPartner_ID = orderInserted[0].cBpartnerId;
431             BillTo_ID = orderInserted[0].billtoId;
432             C_BPartner_Location_ID = orderInserted[0].cBpartnerLocationId;
433             order_documentno=orderInserted[0].documentno;
434             corder_corderid=orderInserted[0].cOrderId;
435             corder_mpricelistid=orderInserted[0].mPricelistId;
436             corder_ccurrencyid=orderInserted[0].cCurrencyId;
437             corder_cbpartnerid=orderInserted[0].cBpartnerId;
438             corder_cbpartnerlocationid=orderInserted[0].cBpartnerLocationId;
439             lineNo = Integer.parseInt(orderInserted[0].linedescription);
440           }
441         }
442         
443         data[i].cOrderId = corder_corderid;
444         // New OrderLine
445
COrderLineData line = new COrderLineData();
446         line.cOrderlineId = SequenceIdData.getSequence(conn, "C_OrderLine", vars.getClient());
447         line.adClientId = data[i].adClientId;
448         line.adOrgId = data[i].adOrgId;
449         line.adUserId = vars.getUser();
450         line.cOrderId = corder_corderid;
451         line.line = Integer.toString(lineNo);
452         if (log4j.isDebugEnabled()) log4j.debug("reading order line number: " + line.line);
453         line.description = data[i].linedescription;
454         line.cBpartnerId = corder_cbpartnerid;
455         line.cBpartnerLocationId = corder_cbpartnerlocationid;
456         lineNo += 10;
457         if (data[i].mProductId != null && !data[i].mProductId.equals("") && Integer.parseInt(data[i].mProductId) != 0)
458           line.mProductId = data[i].mProductId;
459         line.qtyordered = data[i].qtyordered;
460         // set price
461
if (line.mProductId != null && !line.mProductId.equals("")) {
462           ProductPriceData[] pprice = ProductPriceData.selectPL(conn, line.mProductId, corder_mpricelistid);
463           if (pprice.length > 0) {
464             line.pricestd = pprice[0].pricestd;
465             line.priceactual = pprice[0].pricestd;
466             line.pricelist = (pprice[0].pricelist == null && pprice[0].pricelist.equals("")) ? "0" : pprice[0].pricelist;
467             line.pricelimit = (pprice[0].pricelimit == null && pprice[0].pricelimit.equals("")) ? "0" : pprice[0].pricelimit;
468             line.discount = ((Double.parseDouble(pprice[0].pricelist)==Double.parseDouble("0"))?"0":(new BigDecimal JavaDoc((Double.parseDouble(pprice[0].pricelist) - Double.parseDouble(line.priceactual)) / Double.parseDouble(pprice[0].pricelist) * 100.0)).toString());
469             line.cUomId = pprice[0].cUomId;
470           } else {
471             if (log4j.isDebugEnabled()) log4j.debug("Could not establish prices");
472           }
473         } // set price
474
if (data[i].priceactual != null && !data[i].priceactual.equals("") && Double.parseDouble(data[i].priceactual) != Double.parseDouble("0"))
475             line.priceactual = data[i].priceactual;
476           if (data[i].cTaxId != null && !data[i].cTaxId.equals("") && Integer.parseInt(data[i].cTaxId) != 0)
477             line.cTaxId = data[i].cTaxId;
478           else {
479             try {
480               line.cTaxId = Tax.get(conn, data[i].mProductId, DateTimeData.today(conn), data[i].adOrgId, data[i].mWarehouseId.equals("")?vars.getWarehouse():data[i].mWarehouseId, ExpenseSOrderData.cBPartnerLocationId(conn, data[i].cBpartnerId), ExpenseSOrderData.cBPartnerLocationId(conn, data[i].cBpartnerId), data[i].cProjectId, true);
481             } catch (IOException JavaDoc ioe) {
482               if (log4j.isDebugEnabled()) log4j.debug("IOException");
483             }
484           }
485           if (line.cTaxId == null || line.cTaxId.equals(""))
486             line.cTaxId = ProductPriceData.selectCTaxId(conn, vars.getClient());
487           data[i].cTaxId = line.cTaxId;
488           if (data[i].freightamt != null && !data[i].freightamt.equals(""))
489             line.freightamt = data[i].freightamt;
490           line.dateordered = data[i].dateordered;
491           line.mWarehouseId = (data[i].mWarehouseId == null || data[i].mWarehouseId.equals("")) ? vars.getWarehouse() : data[i].mWarehouseId;
492           if (line.cUomId == null || line.cUomId.equals("")) line.cUomId = ProductPriceData.selectCUomIdByProduct(conn, line.mProductId);
493           if (line.cUomId == null || line.cUomId.equals("")) line.cUomId = ProductPriceData.selectCUomIdDefault(conn);
494           line.cCurrencyId = (data[i].cCurrencyId == null || data[i].cCurrencyId.equals("")) ? corder_ccurrencyid : data[i].cCurrencyId;
495           line.insert(con, conn);
496           data[i].cOrderlineId = line.cOrderlineId;
497           data[i].iIsimported = "Y";
498           data[i].processed = "Y";
499           data[i].update(con, conn);
500           String JavaDoc [] arrayPayment = {data[i].paymentrule1, data[i].paymentrule2};
501           String JavaDoc [] arrayAmount = {data[i].paymentamount1, data[i].paymentamount2};
502           for (int k = 0; k<arrayPayment.length; k++) {
503             if (!arrayPayment[k].equals("")) {
504               CDebtpaymentData cdebtpayment = new CDebtpaymentData();
505               cdebtpayment.cDebtPaymentId = SequenceIdData.getSequence(conn, "C_Debt_Payment", vars.getClient());
506               cdebtpayment.adClientId = data[i].adClientId;
507               cdebtpayment.adOrgId = data[i].adOrgId;
508               cdebtpayment.createdby = getAD_User_ID();
509               cdebtpayment.updatedby = getAD_User_ID();
510               cdebtpayment.cBpartnerId = corder_cbpartnerid;
511               cdebtpayment.cCurrencyId = corder_ccurrencyid;
512               /*cdebtpayment.cBankaccountId = ;
513               cdebtpayment.cCashbookId = ;*/

514               cdebtpayment.paymentrule = arrayPayment[k];
515               cdebtpayment.amount = arrayAmount[k];
516               cdebtpayment.ispaid = "N";
517               cdebtpayment.dateplanned = data[i].dateordered;
518               cdebtpayment.ismanual = "N";
519               cdebtpayment.isvalid = "Y";
520               cdebtpayment.changesettlementcancel = "N";
521               cdebtpayment.cancelProcessed = "N";
522               cdebtpayment.generateProcessed = "N";
523               cdebtpayment.glitemamt = "0";
524               cdebtpayment.isdirectposting = "N";
525               cdebtpayment.status = "DE";
526               cdebtpayment.statusInitial = "DE";
527               cdebtpayment.cOrderId = data[i].cOrderId;
528               cdebtpayment.insert(con, conn);
529             }
530           }
531         
532           try {
533             if (data[i].performPost.equals("Y")) {
534               if (i != data.length - 1) {
535                 if (!order_documentno.equals(data[i+1].documentno)) strPostMessage += cOrderPost(con, conn, vars, data[i].cOrderId, order_documentno);
536               } else {
537                 strPostMessage += cOrderPost(con, conn, vars, data[i].cOrderId, order_documentno);
538               }
539             }
540           } catch (IOException JavaDoc e) {
541             e.printStackTrace();
542             log4j.debug("Post error");
543             ImportOrderData.updatePostError(con, conn, strPostMessage, data[i].orderReferenceno, getAD_Client_ID());
544           }
545           noInsertLine++;
546           conn.releaseCommitConnection(con);
547       }
548       con = conn.getTransactionConnection();
549       no = ImportOrderData.updateNotImported(con, conn, getAD_Client_ID());
550       addLog(Utility.messageBD(conn, "Errors", vars.getLanguage()) + ": " + no + "\\n");
551       addLog("Pedidos importados: " + noInsert + "\\n");
552       addLog("LĂ­neas importadas: " + noInsertLine);
553       addLog("Procesados: " + strPostMessage);
554       conn.releaseCommitConnection(con);
555     } catch (NoConnectionAvailableException ex) {
556       throw new ServletException("@CODE=NoConnectionAvailable");
557     } catch (SQLException ex2) {
558       try {
559         conn.releaseRollbackConnection(con);
560       } catch (Exception JavaDoc ignored) {}
561       throw new ServletException("@CODE=" + Integer.toString(ex2.getErrorCode()) + "@" + ex2.getMessage());
562     } catch (Exception JavaDoc ex3) {
563       try {
564         conn.releaseRollbackConnection(con);
565       } catch (Exception JavaDoc ignored) {}
566       throw new ServletException("@CODE=@" + ex3.getMessage());
567     }
568     return true;
569   }
570
571   String JavaDoc cOrderPost(Connection JavaDoc con, ConnectionProvider conn, VariablesSecureApp vars, String JavaDoc strcOrderId, String JavaDoc order_documentno)
572       throws IOException JavaDoc, ServletException {
573     String JavaDoc pinstance = SequenceIdData.getSequence(conn, "AD_PInstance", vars.getClient());
574     //System.out.println("*************************"+pinstance);
575

576     PInstanceProcessData.insertPInstance(conn, pinstance, "104", strcOrderId, "N", vars.getUser(), vars.getClient(), vars.getOrg());
577     //PInstanceProcessData.insertPInstanceParam(this, pinstance, "1", "Selection", "Y", vars.getClient(), vars.getOrg(), vars.getUser());
578
ImportOrderData.cOrderPost0(con, conn, pinstance);
579     
580     PInstanceProcessData[] pinstanceData = PInstanceProcessData.select(conn, pinstance);
581     String JavaDoc messageResult="";
582     if (pinstanceData!=null && pinstanceData.length>0) {
583       if (!pinstanceData[0].errormsg.equals("")) {
584         String JavaDoc message = pinstanceData[0].errormsg;
585         //System.out.println("*****************1"+message);
586
if (message.startsWith("@") && message.endsWith("@")) {
587           message = message.substring(1, message.length()-1);
588           if (message.indexOf("@")==-1) messageResult = Utility.messageBD(conn, message, vars.getLanguage());
589           else messageResult = Utility.parseTranslation(conn, vars, vars.getLanguage(), "@" + message + "@");
590         } else {
591           messageResult = Utility.parseTranslation(conn, vars, vars.getLanguage(), message);
592         }
593       } else if (!pinstanceData[0].pMsg.equals("")) {
594         //String message = pinstanceData[0].pMsg;
595
//messageResult = Utility.parseTranslation(this, vars, vars.getLanguage(), message);
596
total += 1;
597         messageResult = Utility.messageBD(conn, "Created", vars.getLanguage()) + ": " + Integer.toString(total);
598       } else if (pinstanceData[0].result.equals("1")) {
599         //messageResult = Utility.messageBD(this, "Success", vars.getLanguage());
600
total += 1;
601         messageResult = Utility.messageBD(conn, "Created", vars.getLanguage() + " :" + String.valueOf(total));
602       } else {
603         messageResult = Utility.messageBD(conn, "Error", vars.getLanguage());
604         ImportOrderData.updatePostError(con, conn, messageResult, order_documentno, getAD_Client_ID());
605         
606       }
607     }
608     return messageResult;
609   }
610 }
611
Popular Tags