Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******************************************************************************
- * Product: Adempiere ERP & CRM Smart Business Solution *
- * Copyright (C) 1999-2006 ComPiere, Inc. All Rights Reserved. *
- * This program is free software; you can redistribute it and/or modify it *
- * under the terms version 2 of the GNU General Public License as published *
- * by the Free Software Foundation. This program is distributed in the hope *
- * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied *
- * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
- * See the GNU General Public License for more details. *
- * You should have received a copy of the GNU General Public License along *
- * with this program; if not, write to the Free Software Foundation, Inc., *
- * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *
- * For the text or an alternative of this public license, you may reach us *
- * ComPiere, Inc., 2620 Augustine Dr. #245, Santa Clara, CA 95054, USA *
- * or via info@compiere.org or http://www.compiere.org/license.html *
- *****************************************************************************/
- package org.compiere.process;
- import java.math.BigDecimal;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Timestamp;
- import java.util.logging.Level;
- import org.adempiere.model.ImportValidator;
- import org.adempiere.process.ImportProcess;
- import org.compiere.model.MProductPO;
- import org.compiere.model.ModelValidationEngine;
- import org.compiere.model.Query;
- import org.compiere.util.DB;
- import org.compiere.util.Env;
- import org.kosta.aoi.model.X_I_Product_PO;
- /**
- * Import Products from I_Product
- *
- * @author Jorg Janke
- * @version $Id: ImportProduct.java,v 1.3 2006/07/30 00:51:01 jjanke Exp $
- *
- * @author Carlos Ruiz, globalqss
- * <li>FR [ 2788278 ] Data Import Validator - migrate core processes
- * https://sourceforge.net/tracker/?func=detail&aid=2788278&group_id=176962&atid=879335
- */
- public class ImportProductPurchasing extends SvrProcess implements ImportProcess
- {
- /** Client to be imported to */
- private int m_AD_Client_ID = 0;
- /** Delete old Imported */
- private boolean m_deleteOldImported = false;
- /** Effective */
- private Timestamp m_DateValue = null;
- /** Pricelist to Update */
- private int p_M_PriceList_Version_ID = 0;
- private int AD_User_ID = 100; // SuperUser
- /**
- * Prepare - e.g., get Parameters.
- */
- protected void prepare()
- {
- ProcessInfoParameter[] para = getParameter();
- for (int i = 0; i < para.length; i++)
- {
- String name = para[i].getParameterName();
- if (name.equals("AD_Client_ID"))
- m_AD_Client_ID = ((BigDecimal)para[i].getParameter()).intValue();
- else if (name.equals("DeleteOldImported"))
- m_deleteOldImported = "Y".equals(para[i].getParameter());
- else if (name.equals("M_PriceList_Version_ID"))
- p_M_PriceList_Version_ID = para[i].getParameterAsInt();
- else
- log.log(Level.SEVERE, "Unknown Parameter: " + name);
- }
- if (m_DateValue == null)
- m_DateValue = new Timestamp (System.currentTimeMillis());
- } // prepare
- // Field to copy From Product if Import does not have value
- private String[] strFieldsToCopy = new String[] {
- "Value",
- "Name",
- "Description",
- "DocumentNote",
- "Help",
- "UPC",
- "SKU",
- "Classification",
- "ProductType",
- "Discontinued",
- "DiscontinuedBy",
- "DiscontinuedAt",
- "ImageURL",
- "DescriptionURL"
- };
- /**
- * Perform process.
- * @return Message
- * @throws Exception
- */
- protected String doIt() throws java.lang.Exception
- {
- StringBuilder sql = null;
- AD_User_ID = Env.getAD_User_ID(getCtx());
- int no = 0;
- String clientCheck = getWhereClause();
- // **** Prepare ****
- // Delete Old Imported
- if (m_deleteOldImported)
- {
- sql = new StringBuilder ("DELETE I_Product_PO ")
- .append("WHERE I_IsImported='Y'").append(clientCheck);
- no = DB.executeUpdate(sql.toString(), get_TrxName());
- if (log.isLoggable(Level.INFO)) log.info("Delete Old Imported =" + no);
- }
- // Set Client, Org, IaActive, Created/Updated, ProductType
- sql = new StringBuilder ("UPDATE I_Product_PO ")
- .append("SET AD_Client_ID = COALESCE (AD_Client_ID, ").append(m_AD_Client_ID).append("),")
- .append(" AD_Org_ID = COALESCE (AD_Org_ID, 0),")
- .append(" IsActive = COALESCE (IsActive, 'Y'),")
- .append(" Created = COALESCE (Created, SysDate),")
- .append(" CreatedBy = COALESCE (CreatedBy, 0),")
- .append(" Updated = COALESCE (Updated, SysDate),")
- .append(" UpdatedBy = COALESCE (UpdatedBy, 0),")
- //.append(" ProductType = COALESCE (ProductType, 'I'),")
- .append(" I_ErrorMsg = ' ',")
- .append(" I_IsImported = 'N' ")
- .append("WHERE I_IsImported<>'Y' OR I_IsImported IS NULL");
- no = DB.executeUpdate(sql.toString(), get_TrxName());
- if (log.isLoggable(Level.INFO)) log.info("Reset=" + no);
- ModelValidationEngine.get().fireImportValidate(this, null, null, ImportValidator.TIMING_BEFORE_VALIDATE);
- sql = new StringBuilder ("UPDATE I_Product_PO ")
- .append("SET I_IsImported='N', I_ErrorMsg=I_ErrorMsg||'ERR=Product, BP dan Promised Delivery Time harus diisi,' ")
- .append("WHERE product IS NULL OR C_Bpartnername IS NULL OR deliverytime_promised IS NULL")
- .append(" AND I_IsImported<>'Y'").append(clientCheck);
- no = DB.executeUpdate(sql.toString(), get_TrxName());
- if (no != 0)
- log.warning("Invalid Mandatory Field =" + no);
- // Value
- sql = new StringBuilder ("UPDATE I_Product_PO i ")
- .append("SET M_Product_ID=(SELECT M_Product_ID FROM M_Product p")
- .append(" WHERE i.product=p.Value AND i.AD_Client_ID=p.AD_Client_ID) ")
- .append("WHERE M_Product_ID IS NULL AND product IS NOT NULL")
- .append(" AND I_IsImported='N'").append(clientCheck);
- no = DB.executeUpdate(sql.toString(), get_TrxName());
- if (log.isLoggable(Level.INFO)) log.info("Product Existing Value=" + no);
- sql = new StringBuilder ("UPDATE I_Product_PO ")
- .append("SET I_IsImported='N', I_ErrorMsg=I_ErrorMsg||'ERR=Invalid Product,' ")
- .append("WHERE M_Product_ID IS NULL AND product IS NOT NULL")
- .append(" AND I_IsImported<>'Y'").append(clientCheck);
- no = DB.executeUpdate(sql.toString(), get_TrxName());
- if (no != 0)
- log.warning("Invalid Product =" + no);
- // set UOM
- sql = new StringBuilder ("UPDATE I_Product_PO i ")
- .append("SET C_UOM_ID=(SELECT C_UOM_ID FROM C_UOM p")
- .append(" WHERE i.uom=p.Name AND i.AD_Client_ID IN (0,p.AD_Client_ID)) ")
- .append("WHERE C_UOM_ID IS NULL AND uom IS NOT NULL")
- .append(" AND I_IsImported<>'Y'").append(clientCheck);
- no = DB.executeUpdate(sql.toString(), get_TrxName());
- if (log.isLoggable(Level.INFO)) log.info("UOM=" + no);
- //
- sql = new StringBuilder ("UPDATE I_Product_PO ")
- .append("SET I_IsImported='N', I_ErrorMsg=I_ErrorMsg||'ERR=Invalid UOM,' ")
- .append("WHERE C_UOM_ID IS NULL AND uom IS NOT NULL")
- .append(" AND I_IsImported<>'Y'").append(clientCheck);
- no = DB.executeUpdate(sql.toString(), get_TrxName());
- if (no != 0)
- log.warning("Invalid UOM=" + no);
- sql = new StringBuilder ("UPDATE I_Product_PO i ")
- .append("SET I_IsImported='N', I_ErrorMsg=I_ErrorMsg||'ERR=UOM tidak ada di dalam product,' ")
- .append("WHERE C_UOM_ID IS NOT NULL AND C_UOM_ID NOT IN ")
- .append("(SELECT DISTINCT C_UOM_ID FROM C_UOM_Conversion where M_PRODUCT_ID = i.M_Product_ID ")
- .append("UNION ")
- .append("SELECT DISTINCT C_UOM_TO_ID FROM C_UOM_Conversion where M_PRODUCT_ID = i.M_Product_ID ")
- .append("UNION ")
- .append("SELECT C_UOM_ID FROM M_PRODUCT WHERE M_PRODUCT_ID = i.M_Product_ID) ")
- .append(" AND I_IsImported<>'Y'").append(clientCheck);
- no = DB.executeUpdate(sql.toString(), get_TrxName());
- if (no != 0)
- log.warning("Invalid UOM=" + no);
- // Set BPartner
- sql = new StringBuilder ("UPDATE I_Product_PO i ")
- .append("SET C_BPartner_ID=(SELECT C_BPartner_ID FROM C_BPartner p")
- .append(" WHERE i.C_BPartnerName=p.value AND i.AD_Client_ID=p.AD_Client_ID) ")
- .append("WHERE C_BPartner_ID IS NULL AND C_BPartnerName IS NOT NULL ")
- .append(" AND I_IsImported<>'Y'").append(clientCheck);
- no = DB.executeUpdate(sql.toString(), get_TrxName());
- if (log.isLoggable(Level.INFO)) log.info("BPartner=" + no);
- sql = new StringBuilder ("UPDATE I_Product_PO ")
- .append("SET I_IsImported='N', I_ErrorMsg=I_ErrorMsg||'ERR=Invalid BPartner,' ")
- .append("WHERE C_BPartner_ID IS NULL AND C_BPartnerName IS NOT NULL")
- .append(" AND I_IsImported<>'Y'").append(clientCheck);
- no = DB.executeUpdate(sql.toString(), get_TrxName());
- if (no != 0)
- log.warning("Invalid BPartner=" + no);
- sql = new StringBuilder ("UPDATE I_Product_PO i ")
- .append("SET C_Currency_ID=(SELECT C_Currency_ID FROM C_Currency c")
- .append(" WHERE i.Currency=c.ISO_Code AND c.AD_Client_ID IN (0,i.AD_Client_ID)) ")
- .append("WHERE C_Currency_ID IS NULL AND Currency IS NOT NULL")
- .append(" AND I_IsImported<>'Y'").append(clientCheck);
- no = DB.executeUpdate(sql.toString(), get_TrxName());
- if (log.isLoggable(Level.INFO)) log.info("doIt- Set Currency=" + no);
- //
- sql = new StringBuilder ("UPDATE I_Product_PO ")
- .append("SET I_IsImported='N', I_ErrorMsg=I_ErrorMsg||'ERR=Currency,' ")
- .append("WHERE C_Currency_ID IS NULL AND Currency IS NOT NULL")
- .append(" AND I_IsImported<>'Y'").append(clientCheck);
- no = DB.executeUpdate(sql.toString(), get_TrxName());
- if (no != 0)
- log.warning("Invalid Currency=" + no);
- ModelValidationEngine.get().fireImportValidate(this, null, null, ImportValidator.TIMING_AFTER_VALIDATE);
- commitEx();
- // -------------------------------------------------------------------
- //int noInsert = 0;
- //int noUpdate = 0;
- int noInsertPO = 0;
- int noUpdatePO = 0;
- // Go through Records
- log.fine("start inserting/updating ...");
- sql = new StringBuilder ("SELECT * FROM I_Product_PO WHERE I_IsImported='N' AND I_ErrorMsg=' ' ")
- .append(clientCheck);
- PreparedStatement pstmt = null;
- ResultSet rs = null;
- PreparedStatement pstmt_insertProductPO = null;
- try
- {
- pstmt = DB.prepareStatement(sql.toString(), get_TrxName());
- rs = pstmt.executeQuery();
- while (rs.next())
- {
- X_I_Product_PO imp = new X_I_Product_PO(getCtx(), rs, get_TrxName());
- int M_Product_ID = imp.getM_Product_ID();
- int C_BPartner_ID = imp.getC_BPartner_ID();
- String where = "M_Product_ID = "+ M_Product_ID+" AND C_BPartner_ID = "+C_BPartner_ID;
- MProductPO mpo = new Query(getCtx(),MProductPO.Table_Name, where, get_TrxName()).first();
- boolean newProduct = mpo==null;
- if (log.isLoggable(Level.FINE)) log.fine("M_Product_ID=" + M_Product_ID
- + ", C_BPartner_ID=" + C_BPartner_ID);
- // Product
- if (newProduct) // Insert new Product
- {
- pstmt_insertProductPO = DB.prepareStatement
- ("INSERT INTO M_Product_PO (M_Product_ID,C_BPartner_ID, "
- + "AD_Client_ID,AD_Org_ID,IsActive,Created,CreatedBy,Updated,UpdatedBy,"
- + "IsCurrentVendor,C_UOM_ID,C_Currency_ID,UPC,"
- + "PriceList,PricePO,RoyaltyAmt,PriceEffective,"
- + "VendorProductNo,VendorCategory,Manufacturer,"
- + "Discontinued, DiscontinuedAt, Order_Min,Order_Pack,"
- + "CostPerOrder,DeliveryTime_Promised) "
- + "SELECT M_Product_ID,C_Bpartner_ID, "
- + "AD_Client_ID,AD_Org_ID,'Y',SysDate,CreatedBy,SysDate,UpdatedBy,"
- + "IsCurrentVendor,COALESCE(C_UOM_ID,(SELECT C_UOM_ID FROM M_PRODUCT WHERE M_Product_ID = ?))"
- + ",COALESCE(C_Currency_ID,(SELECT C_Currency_ID FROM C_BPartner WHERE C_BPartner_ID = ?))"
- + ",UPC,"
- + "PriceList,PricePO,RoyaltyAmt,PriceEffective,"
- + "COALESCE(VendorProductNo,'-'),VendorCategory,Manufacturer,"
- + "Discontinued,DiscontinuedAt, Order_Min,Order_Pack,"
- + "CostPerOrder,DeliveryTime_Promised "
- + "FROM I_Product_PO "
- + "WHERE M_Product_ID=? AND C_BPartner_ID = ?", get_TrxName());
- pstmt_insertProductPO.setInt(1, M_Product_ID);
- pstmt_insertProductPO.setInt(2, C_BPartner_ID);
- pstmt_insertProductPO.setInt(3, M_Product_ID);
- pstmt_insertProductPO.setInt(4, C_BPartner_ID);
- try
- {
- no = pstmt_insertProductPO.executeUpdate();
- if (log.isLoggable(Level.FINER)) log.finer("Insert Product_PO = " + no);
- noInsertPO++;
- imp.setProcessed(true);
- imp.setI_IsImported(true);
- imp.save();
- }
- catch (SQLException ex)
- {
- log.warning("Insert Product_PO - " + ex.toString());
- //noInsert--; // assume that product also did not exist
- rollback();
- StringBuilder sql0 = new StringBuilder ("UPDATE I_Product_PO i ")
- .append("SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||").append(DB.TO_STRING("Insert Product_PO: " + ex.toString()))
- .append("WHERE M_Product_ID=").append(M_Product_ID)
- .append(" AND C_BPartner_ID =").append(C_BPartner_ID);
- DB.executeUpdate(sql0.toString(), get_TrxName());
- continue;
- }
- }
- else // Update Product
- {
- //mpo.set_ValueOfColumn("IsCurrentVendor",imp.get_ValueAsBoolean("IsCurrentVendor"));
- if(imp.get_ValueAsInt("C_UOM_ID")>0)
- mpo.set_ValueOfColumn("C_UOM_ID",imp.get_ValueAsInt("C_UOM_ID"));
- if(imp.get_ValueAsInt("C_Currency_ID")>0)
- mpo.set_ValueOfColumn("C_Currency_ID",imp.get_ValueAsInt("C_Currency_ID"));
- //mpo.set_ValueOfColumn("UPC",imp.get_Value("UPC"));
- //mpo.set_ValueOfColumn("PriceList",imp.get_Value("PriceList"));
- //mpo.set_ValueOfColumn("PricePO",imp.get_Value("PricePO"));
- //mpo.set_ValueOfColumn("RoyaltyAmt",imp.get_Value("RoyaltyAmt"));
- //mpo.set_ValueOfColumn("PriceEffective",imp.get_Value("PriceEffective"));
- if(imp.get_Value("VendorProductNo")!=null)
- mpo.set_ValueOfColumn("VendorProductNo",imp.get_Value("VendorProductNo"));
- //mpo.set_ValueOfColumn("Manufacturer",imp.get_Value("Manufacturer"));
- //mpo.set_ValueOfColumn("Discontinued",imp.get_Value("Discontinued"));
- if(imp.get_Value("Order_Min")!=null)
- mpo.set_ValueOfColumn("Order_Min",imp.get_Value("Order_Min"));
- //mpo.set_ValueOfColumn("Order_Pack",imp.get_Value("Order_Pack"));
- //mpo.set_ValueOfColumn("CostPerOrder",imp.get_Value("CostPerOrder"));
- mpo.set_ValueOfColumn("DeliveryTime_Promised",imp.get_Value("DeliveryTime_Promised"));
- mpo.saveEx();
- imp.setProcessed(true);
- imp.setI_IsImported(true);
- imp.saveEx();
- noUpdatePO++;
- /*
- pstmt_insertProductPO = DB.prepareStatement
- ("UPDATE M_Product_PO SET (M_Product_ID,C_BPartner_ID, "
- + "AD_Client_ID,AD_Org_ID,IsActive,Created,CreatedBy,Updated,UpdatedBy,"
- + "IsCurrentVendor,C_UOM_ID,C_Currency_ID,UPC,"
- + "PriceList,PricePO,RoyaltyAmt,PriceEffective,"
- + "VendorProductNo,VendorCategory,Manufacturer,"
- + "Discontinued, DiscontinuedAt, Order_Min,Order_Pack,"
- + "CostPerOrder,DeliveryTime_Promised) = "
- + "SELECT M_Product_ID,C_Bpartner_ID, "
- + "AD_Client_ID,AD_Org_ID,IsActive,SysDate,CreatedBy,SysDate,UpdatedBy,"
- + "IsCurrentVendor,C_UOM_ID,C_Currency_ID,UPC," // get currentVendor from column. modified by @ZuhriUtama
- + "PriceList,PricePO,RoyaltyAmt,PriceEffective,"
- + "VendorProductNo,VendorCategory,Manufacturer,"
- + "Discontinued,DiscontinuedAt, Order_Min,Order_Pack,"
- + "CostPerOrder,DeliveryTime_Promised "
- + "FROM I_Product_PO "
- + "WHERE M_Product_ID=? AND C_BPartner_ID = ?", get_TrxName());
- pstmt_insertProductPO.setInt(1, M_Product_ID);
- pstmt_insertProductPO.setInt(2, C_BPartner_ID);
- try
- {
- no = pstmt_insertProductPO.executeUpdate();
- if (log.isLoggable(Level.FINER)) log.finer("Insert Product_PO = " + no);
- noUpdatePO++;
- imp.setProcessed(true);
- imp.setI_IsImported(true);
- imp.save();
- }
- catch (SQLException ex)
- {
- log.warning("Update Product_PO - " + ex.toString());
- noUpdatePO--; // assume that product also did not exist
- rollback();
- StringBuilder sql0 = new StringBuilder ("UPDATE I_Product_PO i ")
- .append("SET I_IsImported='E', I_ErrorMsg=I_ErrorMsg||").append(DB.TO_STRING("Insert Product_PO: " + ex.toString()))
- .append("WHERE M_Product_ID=").append(M_Product_ID)
- .append(" AND C_BPartner_ID =").append(C_BPartner_ID);
- DB.executeUpdate(sql0.toString(), get_TrxName());
- continue;
- }*/
- }
- commitEx();
- } // for all I_Product
- }
- catch (SQLException e)
- {
- }
- finally
- {
- DB.close(rs, pstmt);
- rs = null;pstmt = null;
- DB.close(pstmt_insertProductPO);
- pstmt_insertProductPO = null;
- }
- // Set Error to indicator to not imported
- sql = new StringBuilder ("UPDATE I_Product_PO ")
- .append("SET I_IsImported='N', Updated=SysDate ")
- .append("WHERE I_IsImported<>'Y'").append(clientCheck);
- no = DB.executeUpdate(sql.toString(), get_TrxName());
- addLog (0, null, new BigDecimal (no), "@Errors@");
- // addLog (0, null, new BigDecimal (noInsert), "@M_Product_ID@: @Inserted@");
- // addLog (0, null, new BigDecimal (noUpdate), "@M_Product_ID@: @Updated@");
- addLog (0, null, new BigDecimal (noInsertPO), "@M_Product_ID@ @Purchase@: @Inserted@");
- addLog (0, null, new BigDecimal (noUpdatePO), "@M_Product_ID@ @Purchase@: @Updated@");
- return "";
- } // doIt
- //
- // private void setProductName(X_I_Product imp) {
- // Properties ctx = getCtx();
- //
- // MProductCategory prodCat = new MProductCategory(ctx, imp.getM_Product_Category_ID(), null);
- // boolean isFG = prodCat.get_ValueAsBoolean("isFGkst");
- // boolean isWIP = prodCat.get_ValueAsBoolean("isWIPkst");
- //
- // String upc = imp.getUPC();
- //
- // String colorKey = "0";
- // String colorName = "";
- // if(imp.get_ValueAsInt("kst_ColorDetails_ID")>0){
- // X_kst_ColorDetails colorDetails = new X_kst_ColorDetails(ctx, imp.get_ValueAsInt("kst_ColorDetails_ID"), null);
- // colorKey = colorDetails.get_ValueAsString("value");
- // colorName = colorDetails.get_ValueAsString("name");
- // }
- //
- // String sourceSizeKey = "0";
- // String sourceSizeName = "";
- // if(imp.get_ValueAsInt("KST_SourceSize_ID")>0){
- // X_kst_SourceSize sourceSize = new X_kst_SourceSize(ctx, imp.get_ValueAsInt("KST_SourceSize_ID"), null);
- // sourceSizeKey = sourceSize.get_ValueAsString("value");
- // sourceSizeName = sourceSize.get_ValueAsString("name");
- // }
- //
- // String widthKey = "0";
- // String widthName = "";
- // if(imp.get_ValueAsInt("KST_Width_ID")>0){
- // X_kst_width width = new X_kst_width(ctx, imp.get_ValueAsInt("KST_Width_ID"), null);
- // widthKey = width.get_ValueAsString("value");
- // widthName = width.get_ValueAsString("name");
- // }
- //
- // String season = imp.get_ValueAsString("kst_Season");
- // String articleNo = imp.get_ValueAsString("kst_ArticleNo");
- //
- // String productCode = "";
- //
- // // kode FG kst_Season-UPC-kst_ArticleNo-kst_sourcesize
- // if(isFG)
- // productCode = season+"-"+upc+"-"+articleNo+"-"+sourceSizeKey;
- // else if(isWIP)
- // productCode = "WIP-"+season+"-"+upc+"-"+articleNo+"-"+sourceSizeKey;
- // // kode material UPC-Color Detail-Width
- // else
- // productCode = upc+"-"+colorKey+"-"+widthKey;
- //
- // //if(imp.getValue()==null || imp.getValue().equals(""))
- // imp.setValue(productCode);
- //
- // // set name
- // String name = imp.get_ValueAsString("kst_Name");
- // if(isFG || isWIP)
- // name += " " + articleNo + " " + sourceSizeName;
- // else
- // name += " " + colorName + " " + widthName;
- //
- // //if(imp.getName()==null || imp.getName().equals(""))
- // imp.setName(name);
- //
- // imp.saveEx();
- // }
- @Override
- public String getImportTableName() {
- return X_I_Product_PO.Table_Name;
- }
- @Override
- public String getWhereClause() {
- StringBuilder msgreturn = new StringBuilder(" AND AD_Client_ID=").append(m_AD_Client_ID)
- .append(" AND CreatedBy = ").append(AD_User_ID);
- return msgreturn.toString();
- }
- } // ImportProduct
Add Comment
Please, Sign In to add comment