Advertisement
tiko_pb

wmsIntegrasion.java

Sep 11th, 2019
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 17.40 KB | None | 0 0
  1. package org.aoi.enh.allocation.process;
  2.  
  3. import java.math.BigDecimal;
  4. import java.math.RoundingMode;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.Timestamp;
  8. import java.util.logging.Level;
  9.  
  10. import org.aoi.enh.model.MkstFSAllocation;
  11. import org.aoi.enh.model.MkstFSAllocationDetail;
  12. import org.aoi.mrp.model.X_kst_ImportSetting;
  13. import org.aoi.mrp.model.X_kst_PO_Allocation;
  14. import org.aoi.mrp.model.X_kst_PO_AllocationLine;
  15. import org.compiere.model.MOrderLine;
  16. import org.compiere.model.MProduct;
  17. import org.compiere.model.MUOM;
  18. import org.compiere.model.Query;
  19. import org.compiere.process.ProcessInfoParameter;
  20. import org.compiere.process.SvrProcess;
  21. import org.compiere.util.DB;
  22. import org.compiere.util.Env;
  23.  
  24. public class WMSFreeStockAllocateIntegration extends SvrProcess {
  25.     /** Client to be imported to        */
  26.     private int             m_AD_Client_ID = 0;
  27.     /** Organization to be imported to      */
  28.     private int             m_AD_Org_ID = 0;
  29.     /** RecevingDate        */
  30.     private Timestamp       p_DateFrom;
  31.     private Timestamp       p_DateTo;
  32.     /** Document Type       */
  33.     private int             p_C_DocType_ID = 0;
  34.     /** Document Type   Return  */
  35.     private int             p_C_DocType_ID_Return = 0; 
  36.  
  37.    
  38.     @Override
  39.     protected void prepare() {
  40.         ProcessInfoParameter[] para = getParameter();
  41.         for (int i = 0; i < para.length; i++)
  42.         {
  43.             String name = para[i].getParameterName();
  44.             if (name.equals("AD_Client_ID"))
  45.                 m_AD_Client_ID = para[i].getParameterAsInt();
  46.             else if (name.equals("AD_Org_ID"))
  47.                 m_AD_Org_ID = para[i].getParameterAsInt();
  48.             else
  49.                 log.log(Level.SEVERE, "Unknown Parameter: " + name);
  50.         }      
  51.     }
  52.  
  53.     @Override
  54.     protected String doIt() throws Exception {
  55.         String msg ="";
  56.         String msg2 ="";
  57.         String sqlSetting = " Name = 'WMS'";
  58.         X_kst_ImportSetting impSett = new Query(Env.getCtx(),X_kst_ImportSetting.Table_Name,sqlSetting,null)
  59.                                         .setOrderBy("kst_importsetting_id desc")
  60.                                         .setOnlyActiveRecords(true)
  61.                                         .first();
  62.        
  63.         String ip = impSett.getkst_IP();
  64.         String dbname = impSett.getkst_DatabaseName();
  65.         String port = impSett.getkst_Port();
  66.         String user = impSett.getkst_UserName();
  67.         String password = impSett.getkst_Password();
  68.         int UpdateFlag = 0;
  69.         //MUTASI
  70. //      String sql = "SELECT * FROM("
  71. //              + " SELECT A.*,"
  72. //              + " CASE WHEN B.kst_FS_AllocationDetail_UU IS NULL AND C.kst_PO_AllocationLine_UU IS NULL THEN 0 ELSE 1 END as result FROM wms_move_mutasi A "
  73. //              + " LEFT JOIN kst_FS_AllocationDetail B ON A.id = B.kst_idwms"
  74. //              + " LEFT JOIN kst_PO_AllocationLine C ON A.id = C.kst_idwms"
  75. //              + " ) Z"
  76. //              + " WHERE result = 0 ORDER BY created_at FETCH FIRST 500 ROWS ONLY";
  77.         String sql = "SELECT * FROM wms_move_mutasi2 ORDER BY created_at FETCH FIRST 500 ROWS ONLY";
  78.         PreparedStatement pstmt = null;
  79.         ResultSet rs = null;
  80.         try{
  81.             pstmt = DB.prepareStatement(sql.toString(), get_TrxName());
  82.             rs = pstmt.executeQuery();
  83.             while (rs.next()){
  84.                 MProduct prd = new Query(getCtx(), MProduct.Table_Name, " value = ?  ", get_TrxName())
  85.                     .setClient_ID()
  86.                     .setOnlyActiveRecords(true)
  87.                     .setParameters(rs.getString("item_code"))
  88.                     .first();
  89.                 MUOM uom = new Query(getCtx(), MUOM.Table_Name, " uomsymbol = ? ", get_TrxName())
  90.                     .setClient_ID()
  91.                     .setOnlyActiveRecords(true)
  92.                     .setParameters(rs.getString("uom"))
  93.                     .first();
  94.                 int State = 0;
  95.                 if(prd == null && uom == null ){
  96.                     msg = "Product or UOM is not Found";
  97.                 }else{
  98.                     if(rs.getString("is_stock_allocation").equals("f")){ // Free Stock
  99.                         MkstFSAllocation fs = null;
  100.                         String ordID = rs.getString("c_order_id");
  101.                         String prtId = rs.getNString("c_bpartner_id");
  102.                         if(ordID.equals("FREE STOCK") && prtId.equals(("FREE STOCK"))){
  103.                             fs = new Query(getCtx(), MkstFSAllocation.Table_Name, " m_product_id = ? AND  m_warehouse_id = ?  AND type_stock = ? AND c_bpartner_id IS NULL", get_TrxName())
  104.                             .setClient_ID()
  105.                             .setOnlyActiveRecords(true)
  106.                             .setParameters(prd.get_ID(),rs.getInt("warehouse_id"),rs.getString("type_stock_erp_code"))
  107.                             .first();
  108.                         }else if(ordID.equals("FREE STOCK") ){
  109.                             fs = new Query(getCtx(), MkstFSAllocation.Table_Name, " m_product_id = ? AND  m_warehouse_id = ?  AND type_stock = ? AND c_bpartner_ID = ? ", get_TrxName())
  110.                             .setClient_ID()
  111.                             .setOnlyActiveRecords(true)
  112.                             .setParameters(prd.get_ID(),rs.getInt("warehouse_id"),rs.getString("type_stock_erp_code"),Integer.parseInt(rs.getString("c_bpartner_id")))
  113.                             .first();                      
  114.                         }else{
  115.                             fs = new Query(getCtx(), MkstFSAllocation.Table_Name, " m_product_id = ? AND  m_warehouse_id = ?  AND type_stock = ? AND c_bpartner_ID = ? ", get_TrxName())
  116.                             .setClient_ID()
  117.                             .setOnlyActiveRecords(true)
  118.                             .setParameters(prd.get_ID(),rs.getInt("warehouse_id"),rs.getString("type_stock_erp_code"),Integer.parseInt(rs.getString("c_bpartner_id")))
  119.                             .first();
  120.                         }
  121.                         if(fs != null){ // Free Stock Exist
  122.                             if(rs.getString("po_buyer_allocation") != null){
  123.                                 MkstFSAllocationDetail line = null;
  124.                                 line  = new MkstFSAllocationDetail(getCtx(), 0, get_TrxName());
  125.                                
  126.                                 line.setAD_Org_ID(fs.getAD_Org_ID());
  127.                                 line.set_ValueOfColumn("AD_Client_ID", fs.getAD_Client_ID());
  128.                                 line.set_ValueOfColumn("Description", "ID "+rs.getString("id")+" | "+rs.getString("note")+" | "+rs.getString("source")+rs.getString("style_po_buyer_allocation")+" "+rs.getString("article_po_buyer_allocation"));
  129.                                 line.set_ValueOfColumn("kst_lcdate",  rs.getTimestamp("lc_date_po_buyer_allocation"));
  130.                                 line.setkst_FS_Allocation_ID(fs.get_ID());
  131.                                 line.setPOReference(rs.getString("po_buyer_allocation"));
  132.                                 line.set_ValueOfColumn("kst_idwms", rs.getString("id"));
  133.                                 line.setM_Product_ID(prd.get_ID());
  134.                                 line.setqtyallocated(rs.getBigDecimal("qty_allocation"));
  135.                                 if(!line.save()){
  136.                                     return "gagal save Existing FreeStock Line";
  137.                                 }else{
  138.                                     msg += rs.getString("id")+" , ";
  139.                                     State = 1;
  140.                                 }
  141.                                
  142.                             }else{
  143.                                 MkstFSAllocationDetail line = null;
  144.                                 if(rs.getBigDecimal("stock_new").compareTo(Env.ZERO) > 0){
  145.                                    
  146.                                     line  = new MkstFSAllocationDetail(getCtx(), 0, get_TrxName());
  147.                                     line.setM_Product_ID(prd.get_ID());
  148.                                     line.setAD_Org_ID(fs.getAD_Org_ID());
  149.                                     line.set_ValueOfColumn("AD_Client_ID", fs.getAD_Client_ID());
  150.                                     line.set_ValueOfColumn("Description", "ID "+rs.getString("id")+" | "+rs.getString("note")+" | "+rs.getString("source"));
  151.                                     //line_old.set_ValueOfColumn("C_UOM_ID", fsdata.get_ValueAsInt("C_UOM_ID"));
  152.                                     line.set_ValueOfColumn("kst_lcdate", rs.getTimestamp("lc_date"));
  153.                                     line.setkst_FS_Allocation_ID(fs.get_ID());
  154.                                     line.setPOReference(rs.getString("po_buyer"));
  155.                                     line.setqtyallocated(rs.getBigDecimal("stock_new").negate());
  156.                                     line.set_ValueOfColumn("kst_idwms", rs.getString("id"));
  157.                                     line.setM_Product_ID(prd.get_ID());
  158.                                     if(!line.save()){
  159.                                         return "gagal save";
  160.                                     }else{
  161.                                         msg += rs.getString("id")+" , ";
  162.                                         State = 1;
  163.                                     }
  164.                                 }
  165.                                 MkstFSAllocationDetail line_old = null;
  166.                                 if(rs.getBigDecimal("stock_old").compareTo(Env.ZERO) > 0){
  167.                                    
  168.                                     line_old  = new MkstFSAllocationDetail(getCtx(), 0, get_TrxName());
  169.                                     line_old.setM_Product_ID(prd.get_ID());
  170.                                     line_old.setAD_Org_ID(fs.getAD_Org_ID());
  171.                                     line_old.set_ValueOfColumn("AD_Client_ID", fs.getAD_Client_ID());
  172.                                     line_old.set_ValueOfColumn("Description", "ID "+rs.getString("id")+" | "+rs.getString("note")+" | "+rs.getString("source"));
  173.                                     //line_old.set_ValueOfColumn("C_UOM_ID", fsdata.get_ValueAsInt("C_UOM_ID"));
  174.                                     line_old.set_ValueOfColumn("kst_lcdate", rs.getTimestamp("lc_date"));
  175.                                     line_old.setkst_FS_Allocation_ID(fs.get_ID());
  176.                                     line_old.setPOReference(rs.getString("po_buyer"));
  177.                                     line_old.setqtyallocated(rs.getBigDecimal("stock_old"));
  178.                                     line_old.set_ValueOfColumn("kst_idwms", rs.getString("id"));
  179.                                     line_old.setM_Product_ID(prd.get_ID());
  180.                                     //line_old.save();
  181.                                     if(!line_old.save()){
  182.                                         line.delete(true);
  183.                                         State = 0;
  184.                                         return "gagal save";
  185.                                     }else{
  186.                                         msg += rs.getString("id")+" , ";
  187.                                         State = 1;
  188.                                     }
  189.                                
  190.                                 }
  191.                             }
  192.                            
  193.                         }else{ // New Free Stock
  194.                             fs = new MkstFSAllocation(getCtx(), 0, get_TrxName());
  195.                             fs.set_ValueOfColumn("AD_Client_ID", m_AD_Client_ID);
  196.                             fs.setAD_Org_ID(m_AD_Org_ID);
  197.                             fs.setM_Product_ID(prd.get_ID());
  198.                             fs.set_ValueOfColumn("M_Warehouse_ID", rs.getInt("warehouse_id"));
  199.                             fs.set_ValueOfColumn("M_Product_Category_ID", prd.getM_Product_Category_ID());
  200.                             BigDecimal NewQty = rs.getBigDecimal("stock_new").setScale(4, RoundingMode.UP);
  201.                             String sqlavail = "select kst_convertedqty("+NewQty+", "+prd.getM_Product_ID()+", "+uom.get_ID()+",  "+prd.getC_UOM_ID()+" ) ";
  202.                             BigDecimal qtyAvail = DB.getSQLValueBD(get_TrxName(), sqlavail);
  203.                             fs.setQty(NewQty);
  204.                             fs.setQtyAvailable(qtyAvail);
  205.                             fs.setqtyallocated(Env.ZERO);
  206.                            
  207.                             if(!prtID.equals("FREE STOCK")){
  208.                                 fs.set_ValueOfColumn("C_BPartner_ID", Integer.parseInt(rs.getString("c_bpartner_id")));
  209.                             }
  210.                             fs.set_ValueOfColumn("description", "Opening Balance New Integrate FreeStock ID "+rs.getString("id"));
  211.                             fs.set_ValueOfColumn("C_UOM_ID", uom.get_ID());
  212.                             fs.set_ValueOfColumn("type_stock", rs.getString("type_stock_erp_code"));
  213.                             if(!fs.save()){
  214.                                 fs.delete(true);
  215.                                 return "gagal Save";
  216.                             }else{
  217.                                 MkstFSAllocationDetail line = null;
  218.                                 line  = new MkstFSAllocationDetail(getCtx(), 0, get_TrxName());
  219.                                 line.setM_Product_ID(prd.get_ID());
  220.                                 line.setAD_Org_ID(fs.getAD_Org_ID());
  221.                                 line.set_ValueOfColumn("AD_Client_ID", fs.getAD_Client_ID());
  222.                                 line.set_ValueOfColumn("Description", "Opening Balance ID "+rs.getString("id")+" | "+rs.getString("note")+" | "+rs.getString("source"));
  223.                                 //line_old.set_ValueOfColumn("C_UOM_ID", fsdata.get_ValueAsInt("C_UOM_ID"));
  224.                                 line.set_ValueOfColumn("kst_lcdate", rs.getTimestamp("lc_date"));
  225.                                 line.setkst_FS_Allocation_ID(fs.get_ID());
  226.                                 line.setPOReference(rs.getString("po_buyer"));
  227.                                 line.setqtyallocated(Env.ZERO);
  228.                                 line.set_ValueOfColumn("kst_idwms", rs.getString("id"));
  229.                                 if(!line.save()){
  230.                                     return "gagal save";
  231.                                 }else{
  232.                                     msg += rs.getString("id")+" , ";
  233.                                     State = 1;
  234.                                 }
  235.                             }
  236.                         }
  237.                        
  238.                     }else{ // PO Allocation
  239.                         X_kst_PO_Allocation poal = new Query(getCtx(), X_kst_PO_Allocation.Table_Name, " m_product_id = ? AND c_order_id = ? AND m_warehouse_id = ? AND type_stock = ? ", get_TrxName())
  240.                                 .setClient_ID()
  241.                                 .setOnlyActiveRecords(true)
  242.                                 .setParameters(prd.get_ID(),rs.getInt("c_order_id"),rs.getInt("warehouse_id"),rs.getString("type_stock_erp_code"))
  243.                                 .first();
  244.                         if(poal != null){ // PO Allocation Exist
  245.                            
  246.                             if(rs.getString("po_buyer_allocation") != null){
  247.                                 X_kst_PO_AllocationLine line = null;
  248.                                 line  = new X_kst_PO_AllocationLine(getCtx(), 0, get_TrxName());
  249.                                
  250.                                 line.setAD_Org_ID(poal.getAD_Org_ID());
  251.                                 line.set_ValueOfColumn("AD_Client_ID", poal.getAD_Client_ID());
  252.                                 line.set_ValueOfColumn("Description", "ID "+rs.getString("id")+" | "+rs.getString("note")+" | "+rs.getString("source")+rs.getString("style_po_buyer_allocation")+" "+rs.getString("article_po_buyer_allocation"));
  253.                                 line.set_ValueOfColumn("kst_lcdate",  rs.getTimestamp("lc_date_po_buyer_allocation"));
  254.                                 line.setkst_po_allocation_ID(poal.get_ID());
  255.                                 line.setPOReference(rs.getString("po_buyer_allocation"));
  256.                                 line.set_ValueOfColumn("kst_idwms", rs.getString("id"));
  257.                                 line.setqtyallocated(rs.getBigDecimal("qty_allocation"));
  258.                                 if(!line.save()){
  259.                                     return "gagal save Existing PO Alloc Line";
  260.                                 }else{
  261.                                     msg += rs.getString("id")+" , ";
  262.                                     State = 1;
  263.                                 }
  264.                                
  265.                             }else{
  266.                                 X_kst_PO_AllocationLine line = null;
  267.                                 if(rs.getBigDecimal("stock_new").compareTo(Env.ZERO) > 0){
  268.                                    
  269.                                     line  = new X_kst_PO_AllocationLine(getCtx(), 0, get_TrxName());
  270.                                    
  271.                                     line.setAD_Org_ID(poal.getAD_Org_ID());
  272.                                     line.set_ValueOfColumn("AD_Client_ID", poal.getAD_Client_ID());
  273.                                     line.set_ValueOfColumn("Description", "ID "+rs.getString("id")+" | "+rs.getString("note")+" | "+rs.getString("source"));
  274.                                     line.set_ValueOfColumn("kst_lcdate",  rs.getTimestamp("lc_date"));
  275.                                     line.setkst_po_allocation_ID(poal.get_ID());
  276.                                     line.setPOReference(rs.getString("po_buyer"));
  277.                                     line.set_ValueOfColumn("kst_idwms", rs.getString("id"));
  278.                                     line.setqtyallocated(rs.getBigDecimal("stock_new").negate());
  279.                                     if(!line.save()){
  280.                                         return "gagal save Existing PO Alloc Line";
  281.                                     }else{
  282.                                         msg += rs.getString("id")+" , ";
  283.                                         State = 1;
  284.                                     }
  285.                                 }
  286.                                 X_kst_PO_AllocationLine line_old = null;
  287.                                 if(rs.getBigDecimal("stock_old").compareTo(Env.ZERO) > 0){
  288.                                    
  289.                                     line_old  = new X_kst_PO_AllocationLine(getCtx(), 0, get_TrxName());
  290.                                     line_old.setAD_Org_ID(poal.getAD_Org_ID());
  291.                                     line_old.set_ValueOfColumn("AD_Client_ID", poal.getAD_Client_ID());
  292.                                     line_old.set_ValueOfColumn("Description", "ID "+rs.getString("id")+" | "+rs.getString("note")+" | "+rs.getString("source"));
  293.                                     //line_old.set_ValueOfColumn("C_UOM_ID", fsdata.get_ValueAsInt("C_UOM_ID"));
  294.                                     line_old.set_ValueOfColumn("kst_lcdate", rs.getTimestamp("lc_date"));
  295.                                     line_old.setkst_po_allocation_ID(poal.get_ID());
  296.                                     line_old.setPOReference(rs.getString("po_buyer"));
  297.                                     line_old.setqtyallocated(rs.getBigDecimal("stock_old"));
  298.                                     line_old.set_ValueOfColumn("kst_idwms", rs.getString("id"));
  299.                                     if(!line_old.save()){
  300.                                         line.delete(true);
  301.                                         State = 0;
  302.                                         return "gagal save";
  303.                                     }else{
  304.                                         msg += rs.getString("id")+" , ";
  305.                                         State = 1;
  306.                                     }
  307.                                 }  
  308.                             }
  309.                         }else{ // New PO Allocation
  310.                             MOrderLine po_supp_line = new Query(getCtx(), MOrderLine.Table_Name, " C_Order_ID = ? AND M_Product_ID = ? ", get_TrxName())
  311.                                     .setClient_ID()
  312.                                     .setOnlyActiveRecords(true)
  313.                                     .setParameters(rs.getInt("c_order_id"),prd.get_ID())
  314.                                     .first();
  315.                             if(po_supp_line == null)    {
  316.                                 msg = "Some datas is not found";
  317.                             }else{
  318.                                 BigDecimal NewQty = rs.getBigDecimal("stock_new").setScale(4, RoundingMode.UP);
  319.                                 String sqlavail = "select kst_convertedqty("+NewQty+", "+prd.getM_Product_ID()+", "+uom.get_ID()+",  "+prd.getC_UOM_ID()+" ) ";
  320.                                 BigDecimal qtyAvail = DB.getSQLValueBD(get_TrxName(), sqlavail);
  321.                                 poal = new X_kst_PO_Allocation(getCtx(), 0, get_TrxName());
  322.                                 poal.set_ValueOfColumn("AD_Client_ID", m_AD_Client_ID);
  323.                                 poal.setAD_Org_ID(m_AD_Org_ID);
  324.                                 poal.setC_Order_ID(rs.getInt("c_order_id"));
  325.                                 poal.setC_OrderLine_ID(po_supp_line.get_ID());
  326.                                 poal.setM_Product_ID(prd.getM_Product_ID());
  327.                                 poal.setQty(NewQty);
  328.                                 poal.setQtyAvailable(qtyAvail);
  329.                                 poal.set_ValueOfColumn("C_UOM_ID", uom.get_ID());
  330.                                 poal.set_ValueOfColumn("M_Product_Category_ID", prd.getM_Product_Category_ID());
  331.                                 poal.setqtyallocated(Env.ZERO);
  332.                                 poal.set_ValueOfColumn("Description", "Opening Balance ID "+rs.getString("id")+" | "+rs.getString("note")+" | "+rs.getString("source"));
  333.                                 poal.set_ValueOfColumn("M_Warehouse_ID", rs.getInt("warehouse_id"));
  334.                                 poal.set_ValueOfColumn("type_stock", rs.getString("type_stock_erp_code"));
  335.                                 if(!poal.save()){
  336.                                     poal.delete(true);
  337.                                     return "gagal Save PO Alloc Header";
  338.                                 }else{
  339.                                     X_kst_PO_AllocationLine line = null;
  340.                                     line  = new X_kst_PO_AllocationLine(getCtx(), 0, get_TrxName());
  341.                                    
  342.                                     line.setAD_Org_ID(poal.getAD_Org_ID());
  343.                                     line.set_ValueOfColumn("AD_Client_ID", poal.getAD_Client_ID());
  344.                                     line.set_ValueOfColumn("Description", "Opening Balance ID "+rs.getString("id")+" | "+rs.getString("note")+" | "+rs.getString("source"));
  345.                                     line.set_ValueOfColumn("kst_lcdate",  rs.getTimestamp("lc_date"));
  346.                                     line.setkst_po_allocation_ID(poal.get_ID());
  347.                                     line.setPOReference(rs.getString("po_buyer"));
  348.                                     line.set_ValueOfColumn("kst_idwms", rs.getString("id"));
  349.                                     line.setqtyallocated(Env.ZERO);
  350.                                    
  351.                                     if(!line.save()){
  352.                                         return "gagal save Existing PO Alloc Line";
  353.                                     }else{
  354.                                         msg += rs.getString("id")+" , ";
  355.                                         State = 1;
  356.                                     }
  357.                                 }
  358.                             }
  359.                         }
  360.                     }
  361.                 }
  362.                 if(State >= 1){
  363.                     String wms_id = rs.getString("id");
  364.                     DB.executeUpdate(" select * "
  365.                     + " FROM dblink('"
  366.                     + "dbname=" + dbname +" port=" + port +" host=" + ip +" user=" + user +"  password=" + password +"' "
  367.                     + ",'Update history_material_stocks set is_integrate = true, integration_date = now()  "
  368.                     + "WHERE id =''" + wms_id  +"''' ) tt(updated text);");
  369.                     System.out.println("WMS #"+wms_id+" Updated IsIntegration...");
  370.                 }
  371.             }
  372.         }catch (Exception e){
  373.             msg = e.toString();
  374.             return msg;
  375.         }finally{
  376.             DB.close(rs, pstmt);
  377.             pstmt = null;
  378.             rs = null;
  379.         }
  380.         //msg = " \n " + msg2;
  381.         return msg;
  382.     }
  383.  
  384. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement