Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.aoi.enh.allocation.process;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.Timestamp;
- import java.util.logging.Level;
- import org.aoi.mrp.model.X_kst_ImportSetting;
- import org.compiere.model.MProduct;
- import org.compiere.model.Query;
- import org.compiere.process.ProcessInfoParameter;
- import org.compiere.process.SvrProcess;
- import org.compiere.util.DB;
- import org.compiere.util.Env;
- import org.eevolution.model.X_PP_Cost_Collector;
- import org.surya.production.model.MPPCostCollector;
- import org.surya.production.model.MPPOrder;
- public class WMSOrderIssueIntegration extends SvrProcess {
- /** Client to be imported to */
- private int m_AD_Client_ID = 0;
- /** Organization to be imported to */
- private int m_AD_Org_ID = 0;
- /** RecevingDate */
- private Timestamp p_DateFrom;
- private Timestamp p_DateTo;
- /** Document Type */
- private int p_C_DocType_ID = 0;
- /** S_Resource_ID */
- private int p_S_Resoure_ID = 0;
- //private String testingId = " and material_movement_per_size_id =''dc808950-0b3f-11ec-adf4-57d7385f36a2'' ";
- private String testingId = " and from_locator_erp_id = ''1000001'' ";
- @Override
- 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 = para[i].getParameterAsInt();
- else if (name.equals("AD_Org_ID"))
- m_AD_Org_ID = para[i].getParameterAsInt();
- else if (name.equals("MovementDate")){
- p_DateFrom = para[i].getParameterAsTimestamp();
- p_DateTo = para[i].getParameter_ToAsTimestamp();
- }
- else if (name.equals("C_DocType_ID"))
- p_C_DocType_ID = para[i].getParameterAsInt();
- else if (name.equals("S_Resource_ID"))
- p_S_Resoure_ID = para[i].getParameterAsInt();
- else
- log.log(Level.SEVERE, "Unknown Parameter: " + name);
- }
- }
- @Override
- protected String doIt() throws Exception {
- String msg = "Cost Collector Created = ";
- String sqlSetting = " Name = 'WMS'";
- X_kst_ImportSetting impSett = new Query(Env.getCtx(),X_kst_ImportSetting.Table_Name,sqlSetting,null)
- .setOrderBy("kst_importsetting_id desc")
- .setOnlyActiveRecords(true)
- .first();
- String ip = impSett.getkst_IP();
- String dbname = impSett.getkst_DatabaseName();
- String port = impSett.getkst_Port();
- String user = impSett.getkst_UserName();
- String password = impSett.getkst_Password();
- //RunningRefreshMaterializedAtWms(impSett);
- StringBuffer sql = new StringBuffer();
- sql.append("select issue.material_movement_id, issue.material_movement_line_id, issue.material_movement_per_size_id, ")
- .append("issue.from_locator_erp_id, issue.to_locator_erp_id, issue.po_buyer, issue.item_id, issue.c_order_id, issue.uom, issue.size, issue.qty_per_size, ")
- .append("issue.status, issue.movement_date, issue.warehouse_id, issue.is_integrate, issue.integration_date, coalesce(issue.item_id_source, item_id) as item_id_source ")
- .append("from dblink( ")
- .append("'").append("dbname=").append(dbname).append(" port=").append(port).append(" host=").append(ip).append(" user=").append(user).append(" password=").append(password).append("', ")
- .append("'select * from material_out_issue_mv where is_integrate=false and integration_date is null and status_mo=true "+testingId+"') issue (")
- .append("material_movement_id char(36), ")
- .append("material_movement_line_id char(36), ")
- .append("material_movement_per_size_id char(36), ")
- .append("from_locator_erp_id integer, ")
- .append("to_locator_erp_id integer, ")
- .append("po_buyer varchar(255), ")
- .append("item_id varchar(255), ")
- .append("c_order_id varchar(255), ")
- .append("uom varchar(255), ")
- .append("size varchar(255), ")
- .append("qty_per_size numeric, ")
- .append("status varchar(255), ")
- .append("movement_date timestamp without time zone, ")
- .append("warehouse_id numeric, ")
- .append("is_integrate boolean, ")
- .append("integration_date timestamp without time zone, ")
- .append("item_id_source varchar(255), ")
- .append("status_mo boolean) ")
- /*.append("where trunc(issue.movement_date) between ? and ? ")
- .append("and not exists (select 1 from PP_Cost_Collector where Description=issue.material_movement_per_size_id and DocStatus='CO') ")*/
- .append("where trunc(issue.movement_date)>='2020-04-07'::timestamp and not exists (select 1 from PP_Cost_Collector where Description=issue.material_movement_per_size_id and DocStatus='CO') ")
- .append("order by 1 asc, 2 asc, 3 asc ");
- PreparedStatement pstmt = null;
- ResultSet rs = null;
- try {
- pstmt = DB.prepareStatement(sql.toString(), get_TrxName());
- /*pstmt.setTimestamp(1, p_DateFrom);
- pstmt.setTimestamp(2, p_DateTo);*/
- rs = pstmt.executeQuery();
- while (rs.next()){
- /* Find Manufacturing Order */
- StringBuffer sqlMO = new StringBuffer();
- sqlMO.append("select ppo.PP_Order_ID ")
- .append("from C_Order co join C_OrderLine col on (co.C_Order_ID=col.C_Order_ID and co.DocStatus in ('CO', 'RE', 'VO')) ")
- .append("join M_Product mp on (col.M_Product_ID=mp.M_Product_ID) ")
- .append("join PP_Order ppo on (ppo.C_OrderLine_ID=col.C_OrderLine_ID and ppo.M_Product_ID=col.M_Product_ID and ppo.DocStatus='CO') ")
- .append("where co.AD_Client_ID=? ")
- .append("and co.AD_Org_ID=? ")
- .append("and co.IsSOTrx='Y' ")
- .append("and co.POReference=? ")
- .append("and mp.Value like '%-").append(rs.getString("size")).append("' ")
- .append("order by ppo.PP_Order_ID desc");
- PreparedStatement pstmt3 = null;
- ResultSet rs3 = null;
- int PP_Order_ID = 0;
- try{
- pstmt3 = DB.prepareStatement(sqlMO.toString(), get_TrxName());
- pstmt3.setInt(1, m_AD_Client_ID);
- pstmt3.setInt(2, m_AD_Org_ID);
- pstmt3.setString(3, rs.getString("po_buyer"));
- rs3 = pstmt3.executeQuery();
- if (rs3.next()){
- PP_Order_ID = rs3.getInt("PP_Order_ID");
- } else {
- //rollback();
- //return "Error on Integration - No Manufacturing Order - " + rs.getString("po_number") + rs.getString("item_code");
- continue;
- }
- } catch (Exception e){
- //rollback();
- //return "Error on Integration - No Manufacturing Order - " + e.toString();
- continue;
- } finally {
- DB.close(rs3, pstmt3);
- pstmt3 = null;
- rs3 = null;
- }
- /*
- int PP_Order_ID = DB.getSQLValue(get_TrxName(), sqlMO.toString(), m_AD_Client_ID, m_AD_Org_ID, rs.getString("po_buyer"));
- if (PP_Order_ID<0){
- rollback();
- return "Error on Integration - No Manufacturing Order - " + rs.getString("material_movement_per_size_id");
- }
- */
- if (PP_Order_ID==0)
- continue;
- MPPOrder order = new MPPOrder(getCtx(), PP_Order_ID, get_TrxName());
- MPPCostCollector ppc = new MPPCostCollector(order);
- ppc.setAD_Org_ID(m_AD_Org_ID);
- ppc.setC_DocType_ID(p_C_DocType_ID);
- ppc.setC_DocTypeTarget_ID(p_C_DocType_ID);
- ppc.setCostCollectorType(X_PP_Cost_Collector.COSTCOLLECTORTYPE_ComponentIssue);
- //MProduct product = new MProduct(getCtx(), rs.getInt("item_id"), get_TrxName());
- MProduct product = new MProduct(getCtx(), rs.getInt("item_id_source"), get_TrxName());
- ppc.setM_Product_ID(product.getM_Product_ID());
- ppc.setC_UOM_ID(product.getC_UOM_ID());
- ppc.setMovementDate(rs.getTimestamp("movement_date"));
- ppc.setDateAcct(rs.getTimestamp("movement_date"));
- ppc.setM_Warehouse_ID(rs.getInt("warehouse_id"));
- ppc.setM_Locator_ID(rs.getInt("from_locator_erp_id"));
- ppc.setMovementQty(rs.getBigDecimal("qty_per_size"));
- ppc.setQtyReject(Env.ZERO);
- ppc.setScrappedQty(Env.ZERO);
- ppc.setS_Resource_ID(p_S_Resoure_ID);
- ppc.setDurationReal(Env.ZERO);
- ppc.setSetupTimeReal(Env.ZERO);
- ppc.setIsBatchTime(false);
- ppc.setDescription(rs.getString("material_movement_per_size_id"));
- ppc.set_ValueOfColumn("IsWMS", "Y");
- ppc.setDocStatus(X_PP_Cost_Collector.DOCSTATUS_Drafted);
- ppc.setDocAction(X_PP_Cost_Collector.DOCACTION_Complete);
- /* Find PP_Order_BOMLine */
- StringBuffer sqlBL = new StringBuffer();
- sqlBL.append("select bl.PP_Order_BOMLine_ID ")
- .append("from PP_Order ppo join PP_Order_BOMLine bl on (ppo.PP_Order_ID=bl.PP_Order_ID) ")
- .append("where ppo.C_OrderLine_ID=? ")
- .append("and bl.M_Product_ID=? ")
- .append("and ppo.DocStatus='CO'");
- int PP_Order_BOMLine_ID = DB.getSQLValue(get_TrxName(), sqlBL.toString(), order.getC_OrderLine_ID(), product.getM_Product_ID());
- if (PP_Order_BOMLine_ID<0){
- PP_Order_BOMLine_ID = DB.getSQLValue(get_TrxName(), sqlBL.toString(), order.getC_OrderLine_ID(), rs.getInt("item_id"));
- if (PP_Order_BOMLine_ID<0){
- //rollback();
- //return "Error on Integration - No Order BOM Line - " + rs.getString("material_movement_per_size_id");
- continue;
- }
- MProduct productSub = new MProduct(getCtx(), rs.getInt("item_id"), get_TrxName());
- ppc.set_ValueOfColumn("product_substitute", productSub.getValue());
- }
- ppc.setPP_Order_BOMLine_ID(PP_Order_BOMLine_ID);
- ppc.saveEx(get_TrxName());
- if (!ppc.completeIt().equals(X_PP_Cost_Collector.DOCSTATUS_Completed)){
- //rollback();
- //return "Error on Integration - Cannot Complete Cost Collector - " + rs.getString("material_movement_per_size_id");
- continue;
- } else {
- ppc.saveEx(get_TrxName());
- /* Update Flag */
- String wms_id =ppc.getDescription();
- DB.executeUpdate(" select * "
- + " FROM dblink('"
- + "dbname=" + dbname +" port=" + port +" host=" + ip +" user=" + user +" password=" + password +"' "
- + ",'update material_movement_per_sizes set is_integrate = true, integration_date = now() "
- + "where id = ''" + wms_id +"''' ) tt(updated text);", get_TrxName());
- System.out.println("WMS #"+wms_id+" Update IsIntegration...");
- }
- msg = msg + "\n " + ppc.getDocumentNo();
- }
- } catch (Exception e){
- log.warning("Error on Integration - " + e.toString());
- //rollback();
- //return "Error on Integration - " + e.toString();
- } finally {
- DB.close(rs, pstmt);
- pstmt = null;
- rs = null;
- }
- return msg;
- }
- private void RunningRefreshMaterializedAtWms(X_kst_ImportSetting impSett){
- String ip = impSett.getkst_IP();
- String dbname = impSett.getkst_DatabaseName();
- String port = impSett.getkst_Port();
- String user = impSett.getkst_UserName();
- String password = impSett.getkst_Password();
- DB.executeUpdate(" select * "
- + " FROM dblink('"
- + "dbname=" + dbname +" port=" + port +" host=" + ip +" user=" + user +" password=" + password +"' "
- + ",'refresh materialized view material_out_issue_mv') "
- + "tt(updated text);", get_TrxName());
- }
- }
Add Comment
Please, Sign In to add comment