tko_pb

WMSOrderIssueIntegration.java

Sep 9th, 2021 (edited)
489
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.83 KB | None | 0 0
  1. package org.aoi.enh.allocation.process;
  2.  
  3. import java.sql.PreparedStatement;
  4. import java.sql.ResultSet;
  5. import java.sql.Timestamp;
  6. import java.util.logging.Level;
  7.  
  8. import org.aoi.mrp.model.X_kst_ImportSetting;
  9. import org.compiere.model.MProduct;
  10. import org.compiere.model.Query;
  11. import org.compiere.process.ProcessInfoParameter;
  12. import org.compiere.process.SvrProcess;
  13. import org.compiere.util.DB;
  14. import org.compiere.util.Env;
  15. import org.eevolution.model.X_PP_Cost_Collector;
  16. import org.surya.production.model.MPPCostCollector;
  17. import org.surya.production.model.MPPOrder;
  18.  
  19. public class WMSOrderIssueIntegration extends SvrProcess {
  20.  
  21.     /** Client to be imported to        */
  22.     private int             m_AD_Client_ID = 0;
  23.     /** Organization to be imported to      */
  24.     private int             m_AD_Org_ID = 0;
  25.     /** RecevingDate        */
  26.     private Timestamp       p_DateFrom;
  27.     private Timestamp       p_DateTo;
  28.     /**     Document Type   */
  29.     private int p_C_DocType_ID = 0;
  30.     /** S_Resource_ID       */
  31.     private int p_S_Resoure_ID = 0;
  32.    
  33.     //private String testingId = " and material_movement_per_size_id =''dc808950-0b3f-11ec-adf4-57d7385f36a2'' ";
  34.     private String testingId = " and from_locator_erp_id = ''1000001'' ";
  35.    
  36.     @Override
  37.     protected void prepare() {
  38.         ProcessInfoParameter[] para = getParameter();
  39.         for (int i = 0; i < para.length; i++)
  40.         {
  41.             String name = para[i].getParameterName();
  42.             if (name.equals("AD_Client_ID"))
  43.                 m_AD_Client_ID = para[i].getParameterAsInt();
  44.             else if (name.equals("AD_Org_ID"))
  45.                 m_AD_Org_ID = para[i].getParameterAsInt();
  46.             else if (name.equals("MovementDate")){
  47.                 p_DateFrom = para[i].getParameterAsTimestamp();
  48.                 p_DateTo = para[i].getParameter_ToAsTimestamp();               
  49.             }
  50.             else if (name.equals("C_DocType_ID"))
  51.                 p_C_DocType_ID = para[i].getParameterAsInt();
  52.             else if (name.equals("S_Resource_ID"))
  53.                 p_S_Resoure_ID = para[i].getParameterAsInt();
  54.             else
  55.                 log.log(Level.SEVERE, "Unknown Parameter: " + name);
  56.         }
  57.     }
  58.  
  59.     @Override
  60.     protected String doIt() throws Exception {
  61.         String msg = "Cost Collector Created = ";
  62.        
  63.         String sqlSetting = " Name = 'WMS'";
  64.         X_kst_ImportSetting impSett = new Query(Env.getCtx(),X_kst_ImportSetting.Table_Name,sqlSetting,null)
  65.                                         .setOrderBy("kst_importsetting_id desc")
  66.                                         .setOnlyActiveRecords(true)
  67.                                         .first();
  68.        
  69.         String ip = impSett.getkst_IP();
  70.         String dbname = impSett.getkst_DatabaseName();
  71.         String port = impSett.getkst_Port();
  72.         String user = impSett.getkst_UserName();
  73.         String password = impSett.getkst_Password();
  74.        
  75.         //RunningRefreshMaterializedAtWms(impSett);
  76.        
  77.         StringBuffer sql = new StringBuffer();
  78.         sql.append("select issue.material_movement_id, issue.material_movement_line_id, issue.material_movement_per_size_id,  ")
  79.             .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, ")
  80.             .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 ")
  81.             .append("from dblink( ")
  82.             .append("'").append("dbname=").append(dbname).append(" port=").append(port).append(" host=").append(ip).append(" user=").append(user).append(" password=").append(password).append("', ")
  83.             .append("'select * from material_out_issue_mv where is_integrate=false and integration_date is null and status_mo=true "+testingId+"') issue (")
  84.             .append("material_movement_id char(36), ")
  85.             .append("material_movement_line_id char(36), ")
  86.             .append("material_movement_per_size_id char(36), ")
  87.             .append("from_locator_erp_id integer, ")
  88.             .append("to_locator_erp_id integer, ")
  89.             .append("po_buyer varchar(255), ")
  90.             .append("item_id varchar(255), ")
  91.             .append("c_order_id varchar(255), ")
  92.             .append("uom varchar(255), ")
  93.             .append("size varchar(255), ")
  94.             .append("qty_per_size numeric, ")
  95.             .append("status varchar(255), ")
  96.             .append("movement_date timestamp without time zone, ")
  97.             .append("warehouse_id numeric, ")
  98.             .append("is_integrate boolean, ")
  99.             .append("integration_date timestamp without time zone, ")
  100.             .append("item_id_source varchar(255), ")
  101.             .append("status_mo boolean) ")
  102.             /*.append("where trunc(issue.movement_date) between ? and ? ")
  103.             .append("and not exists (select 1 from PP_Cost_Collector where Description=issue.material_movement_per_size_id and DocStatus='CO') ")*/
  104.             .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') ")
  105.             .append("order by 1 asc, 2 asc, 3 asc ");
  106.    
  107.         PreparedStatement pstmt = null;
  108.         ResultSet rs = null;
  109.         try {
  110.             pstmt = DB.prepareStatement(sql.toString(), get_TrxName());
  111.             /*pstmt.setTimestamp(1, p_DateFrom);
  112.             pstmt.setTimestamp(2, p_DateTo);*/
  113.             rs = pstmt.executeQuery();
  114.             while (rs.next()){
  115.                 /* Find Manufacturing Order */
  116.                 StringBuffer sqlMO = new StringBuffer();
  117.                 sqlMO.append("select  ppo.PP_Order_ID ")
  118.                     .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')) ")
  119.                     .append("join M_Product mp on (col.M_Product_ID=mp.M_Product_ID) ")
  120.                     .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') ")
  121.                     .append("where co.AD_Client_ID=? ")
  122.                     .append("and co.AD_Org_ID=? ")
  123.                     .append("and co.IsSOTrx='Y' ")
  124.                     .append("and co.POReference=? ")
  125.                     .append("and mp.Value like '%-").append(rs.getString("size")).append("' ")
  126.                     .append("order by ppo.PP_Order_ID desc");
  127.                
  128.                 PreparedStatement pstmt3 = null;
  129.                 ResultSet rs3 = null;
  130.                 int PP_Order_ID = 0;
  131.                 try{
  132.                     pstmt3 = DB.prepareStatement(sqlMO.toString(), get_TrxName());
  133.                     pstmt3.setInt(1, m_AD_Client_ID);
  134.                     pstmt3.setInt(2, m_AD_Org_ID);
  135.                     pstmt3.setString(3, rs.getString("po_buyer"));
  136.                     rs3 = pstmt3.executeQuery();
  137.                     if (rs3.next()){
  138.                         PP_Order_ID = rs3.getInt("PP_Order_ID");
  139.                     } else {
  140.                         //rollback();
  141.                         //return "Error on Integration - No Manufacturing Order - " + rs.getString("po_number") + rs.getString("item_code");
  142.                         continue;
  143.                     }
  144.                 } catch (Exception e){
  145.                     //rollback();
  146.                     //return "Error on Integration - No Manufacturing Order - " + e.toString();
  147.                     continue;
  148.                 } finally {
  149.                     DB.close(rs3, pstmt3);
  150.                     pstmt3 = null;
  151.                     rs3 = null;
  152.                 }
  153.                
  154.                 /*
  155.                 int PP_Order_ID = DB.getSQLValue(get_TrxName(), sqlMO.toString(), m_AD_Client_ID, m_AD_Org_ID, rs.getString("po_buyer"));
  156.                 if (PP_Order_ID<0){
  157.                     rollback();
  158.                     return "Error on Integration - No Manufacturing Order - " + rs.getString("material_movement_per_size_id");
  159.                 }
  160.                 */
  161.                
  162.                 if (PP_Order_ID==0)
  163.                     continue;
  164.                
  165.                 MPPOrder order = new MPPOrder(getCtx(), PP_Order_ID, get_TrxName());
  166.                
  167.                 MPPCostCollector ppc = new MPPCostCollector(order);
  168.                 ppc.setAD_Org_ID(m_AD_Org_ID);
  169.                 ppc.setC_DocType_ID(p_C_DocType_ID);
  170.                 ppc.setC_DocTypeTarget_ID(p_C_DocType_ID);
  171.                 ppc.setCostCollectorType(X_PP_Cost_Collector.COSTCOLLECTORTYPE_ComponentIssue);
  172.                 //MProduct product = new MProduct(getCtx(), rs.getInt("item_id"), get_TrxName());
  173.                 MProduct product = new MProduct(getCtx(), rs.getInt("item_id_source"), get_TrxName());
  174.                 ppc.setM_Product_ID(product.getM_Product_ID());
  175.                 ppc.setC_UOM_ID(product.getC_UOM_ID());            
  176.                 ppc.setMovementDate(rs.getTimestamp("movement_date"));
  177.                 ppc.setDateAcct(rs.getTimestamp("movement_date"));
  178.                 ppc.setM_Warehouse_ID(rs.getInt("warehouse_id"));
  179.                 ppc.setM_Locator_ID(rs.getInt("from_locator_erp_id"));
  180.                 ppc.setMovementQty(rs.getBigDecimal("qty_per_size"));
  181.                 ppc.setQtyReject(Env.ZERO);
  182.                 ppc.setScrappedQty(Env.ZERO);
  183.                 ppc.setS_Resource_ID(p_S_Resoure_ID);
  184.                 ppc.setDurationReal(Env.ZERO);
  185.                 ppc.setSetupTimeReal(Env.ZERO);
  186.                 ppc.setIsBatchTime(false);
  187.                 ppc.setDescription(rs.getString("material_movement_per_size_id"));
  188.                 ppc.set_ValueOfColumn("IsWMS", "Y");
  189.                 ppc.setDocStatus(X_PP_Cost_Collector.DOCSTATUS_Drafted);
  190.                 ppc.setDocAction(X_PP_Cost_Collector.DOCACTION_Complete);
  191.                
  192.                 /* Find PP_Order_BOMLine */
  193.                 StringBuffer sqlBL = new StringBuffer();
  194.                 sqlBL.append("select bl.PP_Order_BOMLine_ID ")
  195.                     .append("from PP_Order ppo join PP_Order_BOMLine bl on (ppo.PP_Order_ID=bl.PP_Order_ID) ")
  196.                     .append("where ppo.C_OrderLine_ID=? ")
  197.                     .append("and bl.M_Product_ID=? ")
  198.                     .append("and ppo.DocStatus='CO'");
  199.                 int PP_Order_BOMLine_ID = DB.getSQLValue(get_TrxName(), sqlBL.toString(), order.getC_OrderLine_ID(), product.getM_Product_ID());
  200.                 if (PP_Order_BOMLine_ID<0){
  201.                     PP_Order_BOMLine_ID = DB.getSQLValue(get_TrxName(), sqlBL.toString(), order.getC_OrderLine_ID(), rs.getInt("item_id"));
  202.                     if (PP_Order_BOMLine_ID<0){
  203.                         //rollback();
  204.                         //return "Error on Integration - No Order BOM Line - " + rs.getString("material_movement_per_size_id");
  205.                         continue;
  206.                     }
  207.                     MProduct productSub = new MProduct(getCtx(), rs.getInt("item_id"), get_TrxName());
  208.                     ppc.set_ValueOfColumn("product_substitute", productSub.getValue());
  209.                 }
  210.                 ppc.setPP_Order_BOMLine_ID(PP_Order_BOMLine_ID);
  211.                 ppc.saveEx(get_TrxName());
  212.                
  213.                 if (!ppc.completeIt().equals(X_PP_Cost_Collector.DOCSTATUS_Completed)){
  214.                     //rollback();
  215.                     //return "Error on Integration - Cannot Complete Cost Collector - " + rs.getString("material_movement_per_size_id");
  216.                     continue;
  217.                 } else {
  218.                     ppc.saveEx(get_TrxName());
  219.                     /* Update Flag */
  220.                     String wms_id =ppc.getDescription();
  221.                     DB.executeUpdate(" select * "
  222.                     + " FROM dblink('"
  223.                     + "dbname=" + dbname +" port=" + port +" host=" + ip +" user=" + user +"  password=" + password +"' "
  224.                     + ",'update material_movement_per_sizes set is_integrate = true, integration_date = now()  "
  225.                     + "where id = ''" + wms_id  +"''' ) tt(updated text);", get_TrxName());
  226.                     System.out.println("WMS #"+wms_id+" Update IsIntegration...");                     
  227.                 }
  228.                
  229.                 msg = msg + "\n " + ppc.getDocumentNo();
  230.             }
  231.         } catch (Exception e){
  232.             log.warning("Error on Integration - " + e.toString());
  233.             //rollback();
  234.             //return "Error on Integration - " + e.toString();
  235.         } finally {
  236.             DB.close(rs, pstmt);
  237.             pstmt = null;
  238.             rs = null;
  239.         }
  240.        
  241.         return msg;
  242.     }
  243.    
  244.     private void RunningRefreshMaterializedAtWms(X_kst_ImportSetting impSett){
  245.         String ip = impSett.getkst_IP();
  246.         String dbname = impSett.getkst_DatabaseName();
  247.         String port = impSett.getkst_Port();
  248.         String user = impSett.getkst_UserName();
  249.         String password = impSett.getkst_Password();
  250.        
  251.         DB.executeUpdate(" select * "
  252.         + " FROM dblink('"
  253.         + "dbname=" + dbname +" port=" + port +" host=" + ip +" user=" + user +"  password=" + password +"' "
  254.         + ",'refresh materialized view material_out_issue_mv')  "
  255.         + "tt(updated text);", get_TrxName());
  256.     }
  257.  
  258. }
  259.  
Add Comment
Please, Sign In to add comment