Advertisement
tiko_pb

CreateFromOrder.Java

Aug 30th, 2019
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 33.75 KB | None | 0 0
  1. /******************************************************************************
  2.  * Copyright (C) 2009 Low Heng Sin                                            *
  3.  * Copyright (C) 2009 Idalica Corporation                                     *
  4.  * This program is free software; you can redistribute it and/or modify it    *
  5.  * under the terms version 2 of the GNU General Public License as published   *
  6.  * by the Free Software Foundation. This program is distributed in the hope   *
  7.  * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied *
  8.  * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.           *
  9.  * See the GNU General Public License for more details.                       *
  10.  * You should have received a copy of the GNU General Public License along    *
  11.  * with this program; if not, write to the Free Software Foundation, Inc.,    *
  12.  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.                     *
  13.  *****************************************************************************/
  14.  
  15. package org.kosta.aoi.form;
  16.  
  17. import java.math.BigDecimal;
  18. import java.sql.PreparedStatement;
  19. import java.sql.ResultSet;
  20. import java.sql.SQLException;
  21. import java.sql.Timestamp;
  22. import java.util.ArrayList;
  23. import java.util.List;
  24. import java.util.Vector;
  25. import java.util.logging.Level;
  26.  
  27. import org.adempiere.exceptions.AdempiereException;
  28. import org.compiere.apps.IStatusBar;
  29. import org.compiere.grid.CreateFrom;
  30. import org.compiere.minigrid.IMiniTable;
  31. import org.compiere.model.GridTab;
  32. import org.compiere.model.MOrder;
  33. import org.compiere.model.MOrderLine;
  34. import org.compiere.model.MOrg;
  35. import org.compiere.model.MProduct;
  36. import org.compiere.model.MProductPO;
  37. import org.compiere.model.MProductPrice;
  38. import org.compiere.model.MRequisition;
  39. import org.compiere.model.MRequisitionLine;
  40. import org.compiere.model.MUOM;
  41. import org.compiere.model.MUOMConversion;
  42. import org.compiere.model.Query;
  43. import org.compiere.util.CacheMgt;
  44. import org.compiere.util.DB;
  45. import org.compiere.util.Env;
  46. import org.compiere.util.KeyNamePair;
  47. import org.compiere.util.Msg;
  48. import org.kosta.aoi.model.MKSTOrderDetail;
  49. import org.surya.production.model.X_kst_product_category;
  50.  
  51. /**
  52.  *  Create PO Orders Transactions from Requisition
  53.  *
  54.  *  @author Zuhri Utama (zuhriutama@gmail.com)
  55.  */
  56. public abstract class CreateFromOrder extends CreateFrom
  57. {
  58.     protected static final int DOCTYPE_PO_INDIRECT = 1000047; // Purchase Order Indirect Material
  59.     protected static final int DOCTYPE_PO_INDIRECT_2 = 1000127; // Purchase Order Indirect Material - AOI2
  60.     protected static final int DOCTYPE_PO_DIRECT_ADIDAS = 1000048; // Purchase Order Direct Material for ADIDAS
  61.     protected static final int DOCTYPE_PO_DIRECT_PUMA = 1000067; // Purchase Order Direct Material for PUMA
  62.     protected static final int DOCTYPE_PO_ASSET = 1000065; // Purchase Order for Asset
  63.     protected static final int DOCTYPE_PO_DEVELOPMENT = 1000114; // Purchase Order for Development Sample
  64.     protected static final int DOCTYPE_PR_DIRECT_DEV = 1000166; // Requisition for Development
  65.     protected static final int DOCTYPE_PR_DIRECT = 1000060; // Purchase Requisition Direct Material
  66.     protected static final int DOCTYPE_PR_INDIRECT = 1000061; // Purchase Requisition Indirect Material
  67.     protected static final int DOCTYPE_PR_ASSET = 1000063; // Purchase Requisition for Asset
  68.     protected static final int DOCTYPE_PO_INDIRECT_KEMILAU = 1000131;// Purchase Order Indirect Material Kemilau
  69.     private MOrg m_org = null;
  70.     protected MOrder m_order = null;
  71.    
  72.     private int AD_Client_ID = 0;
  73.     protected int AD_Org_ID = 0; // added by @ZuhriUtama
  74.    
  75.     private int C_Order_ID = 0;
  76.     private int M_Warehouse_ID = 0; // add by AH@kosta
  77.    
  78.     /**  Loaded Requisition         */
  79.     private MRequisition m_requisition = null;
  80.    
  81.     /**
  82.      *  Protected Constructor
  83.      *  @param mTab MTab
  84.      */
  85.     public CreateFromOrder(GridTab mTab)
  86.     {
  87.         super(mTab);
  88.         if (log.isLoggable(Level.INFO)) log.info(mTab.toString());
  89.     }   //  CreateFromOrder
  90.  
  91.     /**
  92.      *  Dynamic Init
  93.      *  @return true if initialized
  94.      */
  95.     public boolean dynInit() throws Exception
  96.     {
  97.         log.config("");
  98.         setTitle(Msg.getElement(Env.getCtx(), "C_Order_ID", false) + " .. " + Msg.translate(Env.getCtx(), "CreateFrom"));
  99.        
  100.         AD_Client_ID = ((Integer) getGridTab().getValue("AD_Client_ID")).intValue();
  101.         AD_Org_ID = ((Integer) getGridTab().getValue("AD_Org_ID")).intValue(); // added by @ZuhriUtama
  102.         C_Order_ID = ((Integer) getGridTab().getValue("C_Order_ID")).intValue();
  103.         M_Warehouse_ID = ((Integer) getGridTab().getValue("M_Warehouse_ID")).intValue(); // add by AH@kosta
  104.         m_order = new MOrder(Env.getCtx(), C_Order_ID, null);
  105.         return true;
  106.     }   //  dynInit
  107.    
  108.     /**
  109.      *  Load Requisition Field.
  110.      */
  111.     protected ArrayList<KeyNamePair> loadOrganizationData() {
  112.         ArrayList<KeyNamePair> list = new ArrayList<KeyNamePair>();
  113.  
  114.         String sqlStmt = "SELECT o.AD_Org_ID, o.Name from AD_Org o WHERE o.AD_Client_ID=?";
  115.  
  116.         PreparedStatement pstmt = null;
  117.         ResultSet rs = null;
  118.         try {
  119.             pstmt = DB.prepareStatement(sqlStmt, null);
  120.             pstmt.setInt(1, AD_Client_ID);
  121.             rs = pstmt.executeQuery();
  122.             while (rs.next()) {
  123.                 list.add(new KeyNamePair(rs.getInt(1), rs.getString(2)));
  124.             }
  125.         } catch (SQLException e) {
  126.             log.log(Level.SEVERE, sqlStmt.toString(), e);
  127.         } finally{
  128.             DB.close(rs, pstmt);
  129.             rs = null;
  130.             pstmt = null;
  131.         }
  132.  
  133.         return list;
  134.     }
  135.    
  136.     /**
  137.      *  Load Product Category Field.
  138.      */
  139.     protected ArrayList<KeyNamePair> loadProductCategoryData() {
  140.         ArrayList<KeyNamePair> list = new ArrayList<KeyNamePair>();
  141.  
  142.         String sqlStmt = "SELECT o.M_Product_Category_ID, o.Name from M_Product_Category o WHERE o.AD_Client_ID=?";
  143.  
  144.         PreparedStatement pstmt = null;
  145.         ResultSet rs = null;
  146.         try {
  147.             pstmt = DB.prepareStatement(sqlStmt, null);
  148.             pstmt.setInt(1, AD_Client_ID);
  149.             rs = pstmt.executeQuery();
  150.             while (rs.next()) {
  151.                 list.add(new KeyNamePair(rs.getInt(1), rs.getString(2)));
  152.             }
  153.         } catch (SQLException e) {
  154.             log.log(Level.SEVERE, sqlStmt.toString(), e);
  155.         } finally{
  156.             DB.close(rs, pstmt);
  157.             rs = null;
  158.             pstmt = null;
  159.         }
  160.  
  161.         return list;
  162.     }
  163.    
  164.     protected ArrayList<KeyNamePair> loadDocTypeData() {
  165.         ArrayList<KeyNamePair> list = new ArrayList<KeyNamePair>();
  166.  
  167.         String sqlStmt = "SELECT o.C_DocType_ID, o.Name from C_DocType o WHERE o.AD_Client_ID=? "
  168.                 + " AND DocBaseType = 'POR'";
  169.        
  170.         if(m_order.getC_DocTypeTarget_ID()==DOCTYPE_PO_DEVELOPMENT){
  171.             sqlStmt += " AND o.C_DocType_ID="+DOCTYPE_PR_DIRECT_DEV;
  172.         }
  173.  
  174.         PreparedStatement pstmt = null;
  175.         ResultSet rs = null;
  176.         try {
  177.             pstmt = DB.prepareStatement(sqlStmt, null);
  178.             pstmt.setInt(1, AD_Client_ID);
  179.             rs = pstmt.executeQuery();
  180.             while (rs.next()) {
  181.                 list.add(new KeyNamePair(rs.getInt(1), rs.getString(2)));
  182.             }
  183.         } catch (SQLException e) {
  184.             log.log(Level.SEVERE, sqlStmt.toString(), e);
  185.         } finally{
  186.             DB.close(rs, pstmt);
  187.             rs = null;
  188.             pstmt = null;
  189.         }
  190.  
  191.         return list;
  192.     }
  193.    
  194.     /**
  195.      *  Load Requisition Field.
  196.      * @param M_Product_Category_ID
  197.      * @param AD_Org_ID
  198.      */
  199.     protected ArrayList<KeyNamePair> loadRequisitionByOrg(int AD_Org_ID) {
  200.         ArrayList<KeyNamePair> list = new ArrayList<KeyNamePair>();
  201.  
  202.         String sqlStmt = "SELECT r.M_Requisition_ID, r.DocumentNo from M_Requisition r "
  203.             + "WHERE r.DocStatus in ('CO', 'CL') "
  204.             + "AND EXISTS(SELECT 1 FROM M_RequisitionLine rl "
  205.             + "WHERE rl.M_Requisition_ID=r.M_Requisition_ID "
  206.             + "AND rl.AD_Org_ID=? AND rl.isActive = 'Y'"
  207.             + "AND rl.C_BPartner_ID=? "
  208.             + "AND (rl.Qty-"
  209.             + "(SELECT COALESCE(SUM(dt.Qty),0) "
  210.             + "FROM kst_orderdetail dt "
  211.             + "JOIN c_orderline ol on dt.c_orderline_id=ol.c_Orderline_id "
  212.             + "JOIN c_order o on ol.c_order_id=o.c_Order_id "
  213.             + "where o.DocStatus IN ('DR','IP','CO','CL') AND dt.m_requisitionline_id=rl.m_requisitionline_id)) > 0) "
  214.             + " AND r.M_Warehouse_ID = ? "; // #908
  215.            
  216.         if(m_order.getC_DocTypeTarget_ID()==DOCTYPE_PO_DIRECT_PUMA || m_order.getC_DocTypeTarget_ID()==DOCTYPE_PO_DIRECT_ADIDAS){
  217.             sqlStmt += "AND r.c_doctype_id="+DOCTYPE_PR_DIRECT;
  218.         }else if(m_order.getC_DocTypeTarget_ID()==DOCTYPE_PO_INDIRECT || m_order.getC_DocTypeTarget_ID()==DOCTYPE_PO_INDIRECT_2){
  219.             sqlStmt += "AND r.c_doctype_id="+DOCTYPE_PR_INDIRECT;  
  220.         }else if(m_order.getC_DocTypeTarget_ID()==DOCTYPE_PO_ASSET){
  221.             sqlStmt += "AND r.c_doctype_id="+DOCTYPE_PR_ASSET; 
  222.         }else if(m_order.getC_DocTypeTarget_ID()==DOCTYPE_PO_DEVELOPMENT){
  223.             sqlStmt += "AND r.c_doctype_id="+DOCTYPE_PR_DIRECT_DEV;
  224.         }
  225.         sqlStmt += " ORDER BY r.DocumentNo DESC ";
  226.        
  227.         PreparedStatement pstmt = null;
  228.         ResultSet rs = null;
  229.         try {
  230.             pstmt = DB.prepareStatement(sqlStmt, null);
  231.             pstmt.setInt(1, AD_Org_ID);
  232.             pstmt.setInt(2, m_order.getC_BPartner_ID());
  233.             pstmt.setInt(3, M_Warehouse_ID);
  234.             rs = pstmt.executeQuery();
  235.             while (rs.next()) {
  236.                 list.add(new KeyNamePair(rs.getInt(1), rs.getString(2)));
  237.             }
  238.         } catch (SQLException e) {
  239.             log.log(Level.SEVERE, sqlStmt.toString(), e);
  240.         } finally{
  241.             DB.close(rs, pstmt);
  242.             rs = null;
  243.             pstmt = null;
  244.         }
  245.  
  246.         return list;
  247.     }
  248.    
  249.     protected Vector<Vector<Object>> getRequisitionData(int AD_Org_ID, int M_Product_Category_ID, int M_Requisition_ID, String upc, String value, String jobOrder, Timestamp datePromised, int C_DocType_ID, String jobOrderAutoPR)
  250.     {          
  251.         Vector<Vector<Object>> data = new Vector<Vector<Object>>();
  252.         StringBuilder sqlStmt = new StringBuilder();
  253.        
  254.         //added by Syahnan@Kosta-Consulting 20160224
  255.         sqlStmt.append("SELECT a.M_RequisitionLine_ID, a.line, a.qty , a.M_Product_ID, a.product, a.C_UOM_ID, a.value "
  256.                 + ", a.kst_Division , a.DocumentNo , a.Description , a.C_Charge_ID, a.name, a.datepromised ");
  257.         sqlStmt.append("FROM ( ");
  258.         sqlStmt.append("SELECT rl.M_RequisitionLine_ID, r.documentno as line, "
  259.                 + "(rl.Qty-(SELECT COALESCE(SUM(dt.Qty),0) FROM kst_orderdetail dt JOIN C_OrderLine col ON dt.C_OrderLine_ID=col.C_OrderLine_ID JOIN C_Order co ON col.C_Order_ID=co.C_Order_ID where co.DocStatus IN ('DR','IP','CO','CL') AND dt.m_requisitionline_id=rl.m_requisitionline_id))*(rl.QtyEntered/rl.Qty) as qty "
  260.                 + ", p.M_Product_ID, p.value||'_'||p.Name as product, uom.C_UOM_ID, COALESCE(uom.UOMSymbol,uom.Name) as value ");
  261.         sqlStmt.append(", rl.kst_Division "); // Division
  262.         sqlStmt.append(", o.DocumentNo "); // Sales Order Document No
  263.         if(m_order.getC_DocTypeTarget_ID()==DOCTYPE_PO_INDIRECT || m_order.getC_DocTypeTarget_ID()==DOCTYPE_PO_ASSET
  264.                 ||m_order.getC_DocTypeTarget_ID()==DOCTYPE_PO_INDIRECT_2){
  265.             sqlStmt.append(", rl.kst_Description as Description "); // KST Description
  266.         }
  267.         else sqlStmt.append(", case when rl.Description IS NOT NULL THEN rl.Description else rl.poreference end as Description"); // Description -->when case fajar tambahan
  268.         sqlStmt.append(", c.C_Charge_ID, c.name, rl.datepromised "); // Charge
  269.         sqlStmt.append("FROM M_RequisitionLine rl ");
  270.         sqlStmt.append("INNER JOIN M_Requisition r ON r.M_Requisition_ID=rl.M_Requisition_ID ");
  271.         sqlStmt.append("LEFT JOIN M_Product p ON p.M_Product_ID = rl.M_Product_ID ");
  272.         sqlStmt.append("LEFT JOIN C_Charge c ON rl.C_Charge_ID = c.C_Charge_ID ");
  273.         sqlStmt.append("LEFT JOIN C_OrderLine ol ON rl.C_OrderLine_ID=ol.C_OrderLine_ID ");
  274.         sqlStmt.append("LEFT JOIN C_Order o ON ol.C_Order_ID=o.C_Order_ID ");
  275.         sqlStmt.append("LEFT JOIN C_DocType dt ON dt.C_DocType_ID= r.C_DocType_ID ");
  276.         //if (Env.isBaseLanguage(Env.getCtx(), "C_UOM"))
  277.         //{
  278.             sqlStmt.append("LEFT OUTER JOIN C_UOM uom ON (uom.C_UOM_ID=rl.C_UOM_ID) ");
  279.         //}
  280.         //else
  281.         //{
  282.         //    sqlStmt.append("LEFT OUTER JOIN C_UOM_Trl uom ON (uom.C_UOM_ID=100 AND uom.AD_Language='");
  283.         //    sqlStmt.append(Env.getAD_Language(Env.getCtx())).append("') ");
  284.         //}
  285.         //sqlStmt.append("WHERE rl.AD_Org_ID=? AND rl.C_BPartner_ID=? AND r.DocStatus IN ('CO','CL') ");
  286.         sqlStmt.append("WHERE rl.AD_Org_ID=? AND rl.C_BPartner_ID=? AND r.DocStatus IN ('CO') ");
  287.         /* Added by Roynaldo - 20151020
  288.          * Include only Active Req Line
  289.          */
  290.        
  291.         sqlStmt.append("AND rl.IsActive='Y' ");
  292.         sqlStmt.append("AND rl.qty <> 0 ");
  293.         /* End Added by Roynaldo - 20151020 */
  294.         if(M_Product_Category_ID>0)
  295.             sqlStmt.append("AND p.M_Product_Category_ID=? ");
  296.         if(M_Requisition_ID>0)
  297.             sqlStmt.append("AND rl.M_Requisition_ID=? ");
  298.         else
  299.         {
  300.             sqlStmt.append("AND rl.M_Requisition_ID IN (");
  301.             ArrayList<KeyNamePair> reqList = loadRequisitionByOrg(AD_Org_ID);
  302.             int x=0;
  303.             for(KeyNamePair req: reqList)
  304.             {
  305.                 if(++x ==reqList.size())
  306.                     sqlStmt.append(req.getID()+") ");
  307.                 else
  308.                     sqlStmt.append(req.getID()+",");
  309.             }
  310.         }
  311.        
  312.         if(upc!=null && !upc.equals(""))
  313.             sqlStmt.append("AND p.upc=? ");
  314.         if(value!=null && !value.equals(""))
  315.             sqlStmt.append("AND p.value= ? ");
  316.         if(jobOrder!=null && !jobOrder.equals(""))
  317.             sqlStmt.append("AND rl.Description LIKE ? ");
  318.         if(jobOrderAutoPR!=null && !jobOrderAutoPR.equals(""))
  319.             sqlStmt.append("AND rl.kst_JobOrder LIKE ? ");
  320.         if(datePromised!= null && !datePromised.equals("")){
  321.             sqlStmt.append(" AND rl.datepromised = ? ");
  322.         }
  323.         if(C_DocType_ID > 0){
  324.             sqlStmt.append(" AND dt.C_DocType_ID = ? ");
  325.         }
  326.        
  327.         // filter for only QtyRemaining of Requisition>0
  328.         // remark by Syahnan@Kosta-Consulting
  329.         //sqlStmt.append("AND (rl.Qty-(SELECT COALESCE(SUM(dt.Qty),0) FROM kst_orderdetail dt JOIN C_OrderLine col ON dt.C_OrderLine_ID=col.C_OrderLine_ID JOIN C_Order co ON col.C_Order_ID=co.C_Order_ID where co.DocStatus IN ('DR','IP','CO','CL') AND dt.m_requisitionline_id=rl.m_requisitionline_id))>0 ");
  330.  
  331.         if(m_order.getC_DocTypeTarget_ID()==DOCTYPE_PO_DIRECT_PUMA || m_order.getC_DocTypeTarget_ID()==DOCTYPE_PO_DIRECT_ADIDAS){
  332.             sqlStmt.append("AND r.c_doctype_id="+DOCTYPE_PR_DIRECT);   
  333.         }else if(m_order.getC_DocTypeTarget_ID()==DOCTYPE_PO_INDIRECT || m_order.getC_DocTypeTarget_ID()==DOCTYPE_PO_INDIRECT_2){
  334.             sqlStmt.append("AND r.c_doctype_id="+DOCTYPE_PR_INDIRECT);
  335.         }else if(m_order.getC_DocTypeTarget_ID()==DOCTYPE_PO_ASSET){
  336.             sqlStmt.append("AND r.c_doctype_id="+DOCTYPE_PR_ASSET);
  337.         }else if(m_order.getC_DocTypeTarget_ID()==DOCTYPE_PO_DEVELOPMENT){
  338.             sqlStmt.append("AND r.c_doctype_id="+DOCTYPE_PR_DIRECT_DEV);   
  339.         }
  340.         // add by Syahnan@Kosta-Consulting
  341.         sqlStmt.append(" ) a ");
  342.         sqlStmt.append("GROUP BY a.M_RequisitionLine_ID, a.line, a.qty , a.M_Product_ID, a.product, a.C_UOM_ID, a.value "
  343.                             + " , a.kst_Division , a.DocumentNo , a.Description , a.C_Charge_ID, a.name, a.datepromised ");
  344.         sqlStmt.append(" HAVING a.qty > 0");
  345.         sqlStmt.append(" ORDER BY a.Line, a.product");
  346.         PreparedStatement pstmt = null;
  347.         ResultSet rs = null;
  348.         try
  349.         {
  350.             pstmt = DB.prepareStatement(sqlStmt.toString(), null);
  351.             int index = 1;
  352.             pstmt.setInt(index++, AD_Org_ID);
  353.             pstmt.setInt(index++, m_order.getC_BPartner_ID());
  354.             if(M_Product_Category_ID>0){
  355.                 pstmt.setInt(index++, M_Product_Category_ID);
  356.             }
  357.             if(M_Requisition_ID>0){
  358.                 pstmt.setInt(index++, M_Requisition_ID);
  359.             }
  360.             if(upc!=null && !upc.equals("")){
  361.                 pstmt.setString(index++, upc);
  362.             }
  363.             if(value!=null && !value.equals("")){
  364.                 pstmt.setString(index++, value);
  365.             }
  366.             if(jobOrder!=null && !jobOrder.equals("")){
  367.                 pstmt.setString(index++, jobOrder);
  368.             }
  369.             if(jobOrderAutoPR!=null && !jobOrderAutoPR.equals("")){
  370.                 pstmt.setString(index++, jobOrderAutoPR);
  371.             }
  372.             if(datePromised!= null && !datePromised.equals("")){
  373.                 pstmt.setTimestamp(index++, datePromised);
  374.             }
  375.             if(C_DocType_ID > 0){
  376.                 pstmt.setInt(index++, C_DocType_ID);
  377.             }
  378.            
  379.             rs = pstmt.executeQuery();
  380.                    
  381.             while (rs.next())
  382.             {
  383.                 Vector<Object> line = new Vector<Object>(6);
  384.                 line.add(new Boolean(false));   // 0-Selection
  385.                 line.add(rs.getBigDecimal(3));  // 1-Qty
  386.                 KeyNamePair pp = new KeyNamePair(rs.getInt(6), rs.getString(7));
  387.                 line.add(pp); // 2-UOM
  388.                 pp = new KeyNamePair(rs.getInt(4), rs.getString(5));
  389.                 line.add(pp); // 3-Product
  390.                 pp = new KeyNamePair(rs.getInt(11), rs.getString(12));
  391.                 line.add(pp); // 4-Charge
  392.                 pp = new KeyNamePair(rs.getInt(1), rs.getString(2));
  393.                 line.add(pp);   //5-Requisition
  394.                 line.add(rs.getString(8)); // 6-Division
  395.                 line.add(rs.getString(9)); // 7-SO No
  396.                 line.add(rs.getString(10)); // 8-Description
  397.                 line.add(rs.getString("DatePromised")); // 9-date promised
  398.                 data.add(line);
  399.             }
  400.         }
  401.         catch (Exception ex)
  402.         {
  403.             log.log(Level.SEVERE, sqlStmt.toString(), ex);
  404.         }
  405.         finally
  406.         {
  407.             DB.close(rs, pstmt);
  408.             rs = null; pstmt = null;
  409.         }
  410.        
  411.         return data;
  412.     }
  413.    
  414.     /**
  415.      *  List number of rows selected
  416.      */
  417.     public void info(IMiniTable miniTable, IStatusBar statusBar)
  418.     {
  419.  
  420.     }   //  infoInvoice
  421.  
  422.     protected void configureMiniTable (IMiniTable miniTable)
  423.     {
  424.         miniTable.setColumnClass(0, Boolean.class, false);     //  Selection
  425.         miniTable.setColumnClass(1, BigDecimal.class, false);  //  Qty
  426.         miniTable.setColumnClass(2, String.class, true);       //  UOM
  427.         miniTable.setColumnClass(3, String.class, true);       //  Product
  428.         miniTable.setColumnClass(4, String.class, true);       //  Charge
  429.         miniTable.setColumnClass(5, String.class, true);       //  Requisition
  430.         miniTable.setColumnClass(6, String.class, true);       //  division
  431.         miniTable.setColumnClass(7, String.class, true);       //  SO
  432.         miniTable.setColumnClass(8, String.class, true);       //  Description
  433.         miniTable.setColumnClass(9, String.class, true);       //  Date Promised
  434.        
  435.         //  Table UI
  436.         miniTable.autoSize();
  437.          
  438.     }
  439.  
  440.     /**
  441.      *  Save - Create Invoice Lines
  442.      *  @return true if saved
  443.      */
  444.     public boolean save(IMiniTable miniTable, String trxName)
  445.     {
  446.        
  447.         // Get Shipment
  448.         int C_Order_ID = ((Integer) getGridTab().getValue("C_Order_ID")).intValue();
  449.         MOrder order = new MOrder(Env.getCtx(), C_Order_ID, trxName);
  450.         if (log.isLoggable(Level.CONFIG)) log.config(order.toString());
  451.  
  452.         // Lines
  453.         for (int i = 0; i < miniTable.getRowCount(); i++)
  454.         {
  455.             if (((Boolean)miniTable.getValueAt(i, 0)).booleanValue()) {
  456.                 // variable values
  457.                 BigDecimal QtyEntered = (BigDecimal) miniTable.getValueAt(i, 1); // Qty
  458.                 KeyNamePair pp = (KeyNamePair) miniTable.getValueAt(i, 2); // UOM
  459.                 int C_UOM_ID = pp.getKey();
  460.                 pp = (KeyNamePair) miniTable.getValueAt(i, 3); // Product
  461.                 int M_Product_ID = pp.getKey();
  462.                 pp = (KeyNamePair) miniTable.getValueAt(i, 4); // Charge
  463.                 int C_Charge_ID = pp.getKey();
  464.                 int M_RequisitionLine_ID = 0;
  465.                 pp = (KeyNamePair) miniTable.getValueAt(i, 5); // Requisition
  466.                 // If we have Requisition
  467.                 if (pp != null)
  468.                     M_RequisitionLine_ID = pp.getKey();
  469.                 //  Precision of Qty UOM
  470.                 int precision = 2;
  471.                 if (M_Product_ID != 0)
  472.                 {
  473.                     MProduct product = MProduct.get(Env.getCtx(), M_Product_ID);
  474.                     precision = product.getUOMPrecision();
  475.                 }
  476.                 QtyEntered = QtyEntered.setScale(precision, BigDecimal.ROUND_HALF_DOWN);
  477.                 //
  478.                 if (log.isLoggable(Level.FINE)) log.fine("Line QtyEntered=" + QtyEntered
  479.                         + ", Product=" + M_Product_ID
  480.                         + ", RequisitionLine=" + M_RequisitionLine_ID);
  481.                 String desc = (String)miniTable.getValueAt(i, 8); // kst_Description
  482.                
  483.                 //  Check if already exist order line with product
  484.                 MRequisitionLine rLine = new MRequisitionLine(Env.getCtx(), M_RequisitionLine_ID, trxName);
  485.                 MOrderLine iol = getOrderLine(order, M_Product_ID, desc, (Timestamp)rLine.get_Value("DatePromised"));
  486.                
  487.                
  488.                 if(iol==null){
  489.                     iol = new MOrderLine (order);
  490.                     if(M_Product_ID>0)
  491.                         iol.setM_Product_ID(M_Product_ID, C_UOM_ID);    //  Line UOM
  492.                     if(C_Charge_ID>0)
  493.                         iol.setC_Charge_ID(C_Charge_ID);
  494.                     iol.setDatePromised((Timestamp)rLine.get_Value("DatePromised"));
  495.                     iol.setQty(QtyEntered); //  Movement/Entered
  496.                     iol.set_ValueOfColumn("kst_Description", desc);
  497.                     iol.set_ValueOfColumn("poreference",rLine.get_Value("poreference"));
  498.                     iol.set_ValueOfColumn("kst_joborder",rLine.get_Value("kst_joborder"));
  499.                 }else{
  500.                     iol.setQty(QtyEntered.add(iol.getQtyEntered()));    //  Movement/Entered
  501.                 }
  502.                 // Dimension AH@20190123
  503.                 iol.setC_Activity_ID(rLine.get_ValueAsInt("C_Activity_ID"));
  504.                 iol.setC_Campaign_ID(rLine.get_ValueAsInt("C_Campaign_ID"));
  505.                
  506.                 /*
  507.                  * #1714 - Set Product Price PO. added by @ZuhriUtama
  508.                  */
  509.                 // set price for All DocType use Last PO Price except PO Development
  510.                 if(M_Product_ID>0 && !order.getC_DocTypeTarget().getName().equalsIgnoreCase("PO-DEVELOPMENT"))
  511.                 /* Comment by Sidik to All DocType use Last PO Price
  512.                 */
  513.                 {
  514.                     MProductPO[] productPOs = MProductPO.getOfProduct(Env.getCtx(), M_Product_ID, null);
  515.                     MProductPO curPO = null;
  516.                     for(MProductPO productPO : productPOs){
  517.                         if(productPO.getC_BPartner_ID()==order.getC_BPartner_ID() && productPO.getC_Currency_ID() == order.getC_Currency_ID())
  518.                             curPO = productPO;
  519.                     }
  520.                     if(productPOs.length==0)
  521.                         throw new AdempiereException("No Vendor Price Found for Product "+iol.getM_Product().getValue());
  522.                    
  523.                     if(curPO==null){
  524.                         // curPO = productPOs[0]; // change by AH@kosta
  525.                         iol.setHeaderInfo(order);
  526.                         iol.setPrice();
  527.                         // develop if product purchasing is not exsis
  528.                        
  529.                             //mas uta tolong cek a
  530.                         MProductPO productPurchasing = new MProductPO(Env.getCtx(), 0, null);
  531.                         productPurchasing.setAD_Org_ID(order.getAD_Org_ID());
  532.                         productPurchasing.setM_Product_ID(M_Product_ID);
  533.                         productPurchasing.setC_BPartner_ID(order.getC_BPartner_ID());
  534.                         BigDecimal zeroPrice = new BigDecimal("1");
  535.                         productPurchasing.setPriceLastPO(zeroPrice);
  536.                         productPurchasing.setPriceList(zeroPrice);
  537.                         productPurchasing.setC_Currency_ID(order.getC_Currency_ID());
  538.                         productPurchasing.setC_UOM_ID(iol.getM_Product().getC_UOM_ID());
  539.                         productPurchasing.setVendorProductNo(iol.getM_Product().getValue());
  540.                         productPurchasing.setIsCurrentVendor(false);
  541.                         productPurchasing.saveEx();
  542.                        
  543.                         //throw new AdempiereException("No Vendor Price Found for Product "+iol.getM_Product().getValue());
  544.                     }
  545.                     else {
  546.                         //iol.setPriceActual(curPO.getPriceList());
  547.                         //iol.setPriceList(curPO.getPriceList());  
  548.                         if(curPO.getPriceLastPO().compareTo(BigDecimal.ZERO)==0)
  549.                         {
  550.                             iol.setPriceActual(curPO.getPriceList());
  551.                             iol.setPriceList(curPO.getPriceList());
  552.                         }
  553.                         else
  554.                         {
  555.                             iol.setPriceActual(curPO.getPriceLastPO()); // changed by Surya Alison #1605
  556.                             iol.setPriceList(curPO.getPriceLastPO());   //changed by Surya Alison #1605
  557.                         }
  558.                        
  559.                     }                  
  560.                    
  561.                     if(iol.getC_UOM_ID()!=iol.getM_Product().getC_UOM_ID()){
  562.                         //BigDecimal priceEntered = MUOMConversion.convertProductTo(Env.getCtx(), M_Product_ID, iol.getC_UOM_ID(), curPO.getPriceList());
  563.                     //mas uta tolong cek
  564.                         if(curPO.getPriceLastPO().compareTo(BigDecimal.ZERO)!=0)
  565.                         {
  566.                             BigDecimal priceEntered = MUOMConversion.convertProductTo(Env.getCtx(), M_Product_ID, iol.getC_UOM_ID(), curPO.getPriceLastPO()); // changed by Surya Alison #1605
  567.                             if (priceEntered==null)
  568.                             {
  569.                                 log.info("");
  570.                                 Env.reset(false);
  571.                                 CacheMgt.get().reset();
  572.                                 MUOM uomFrom = new MUOM(Env.getCtx(), C_UOM_ID, trxName);
  573.                                 MUOM uomTo = new MUOM(Env.getCtx(), iol.getM_Product().getC_UOM_ID(), trxName);
  574.                                 throw new AdempiereException("Product " + iol.getM_Product().getName() +" "+ iol.getM_Product().getValue()+
  575.                                         " ini tidak memiliki conversion dari " + uomFrom.getName() + " ke " + uomTo.getName());
  576.                             }
  577.                             iol.setPriceEntered(priceEntered);
  578.                         }
  579.                         else
  580.                         {
  581.                             BigDecimal priceEntered = MUOMConversion.convertProductTo(Env.getCtx(), M_Product_ID, iol.getC_UOM_ID(), curPO.getPriceList()); // changed by Surya Alison #1605
  582.                             if (priceEntered==null)
  583.                             {
  584.                                 log.info("");
  585.                                 Env.reset(false);
  586.                                 CacheMgt.get().reset();
  587.                                 MUOM uomFrom = new MUOM(Env.getCtx(), C_UOM_ID, trxName);
  588.                                 MUOM uomTo = new MUOM(Env.getCtx(), iol.getM_Product().getC_UOM_ID(), trxName);
  589.                                 throw new AdempiereException("Product " + iol.getM_Product().getName() +" "+ iol.getM_Product().getValue()+
  590.                                         " ini tidak memiliki conversion dari " + uomFrom.getName() + " ke " + uomTo.getName());
  591.                             }
  592.                             iol.setPriceEntered(priceEntered);
  593.                         }
  594.                     }
  595.                     else{
  596.                         // Conversion UOM Product
  597.                         if ( curPO != null){
  598.                             if(curPO.getPriceLastPO().compareTo(BigDecimal.ZERO)==0)
  599.                                 iol.setPriceEntered(curPO.getPriceList());
  600.                             else
  601.                                 iol.setPriceEntered(curPO.getPriceLastPO());
  602.                         }
  603.                        
  604.                     }
  605.                 }
  606.                 else
  607.                 {
  608.                     // set price by product price @ZuhriUtama
  609.                     iol.setHeaderInfo(order);
  610.                     //Prepare Plugin by Sidik
  611. //                  String sqlAmt = "SELECT pricestd FROM(select * from (SELECT * FROM M_PriceList where M_PriceList_ID = "+order.getM_PriceList_ID()+" ) mp left join M_PriceList_Version mpv ON mpv.M_PriceList_ID = mp.M_PriceList_ID  ";
  612. //                  if(order.get_ValueAsString("kst_Season").equals("") || order.get_ValueAsString("kst_Season").equals(null)){
  613. //                      sqlAmt += "where kst_season is null";
  614. //                  }else{
  615. //                      sqlAmt += "where kst_season = '"+order.get_ValueAsString("kst_Season")+"'";
  616. //                  }
  617. //                  sqlAmt += ") tmp LEFT JOIN m_productprice mpp ON tmp.M_PriceList_Version_ID = mpp.M_PriceList_Version_ID"
  618. //                          + " where M_product_ID = "+rLine.getM_Product().getM_Product_ID();
  619.                    
  620. //                  BigDecimal actual = DB.getSQLValueBD(trxName, sqlAmt);
  621.                     //if(actual == 0){
  622. //                      iol.setPrice();
  623. //                  }else{
  624. //                      iol.setPrice(actual);
  625. //                  }
  626.                    
  627.                    
  628.                     iol.setPrice();
  629.                     iol.setC_UOM_ID(C_UOM_ID);
  630.                     iol.setQtyOrdered(rLine.getQty());
  631.                     //added by Surya Alison
  632.                     /*MProductPO curPO = new Query(Env.getCtx(), MProductPO.Table_Name, " M_Product_ID = ? AND C_BPartner_ID = ? AND C_currency_id = ? AND C_UOM_ID = ?", trxName)
  633.                     .setParameters(iol.getM_Product_ID(),iol.getC_BPartner_ID(),iol.getC_Currency_ID(),C_UOM_ID)
  634.                     .first();
  635.                    
  636.                     if (curPO != null)
  637.                     {
  638.                         if(curPO.getPriceLastPO().signum()!=0)
  639.                         {
  640.                             iol.setPriceEntered(curPO.getPriceLastPO());
  641.                         }
  642.                        
  643.                     }
  644.                     */
  645.                    
  646.                    
  647.                     //added by Surya Alison -- Set price base on Season.
  648. //                  if(order.get_Value("kst_season")!=null && !order.isSOTrx())
  649. //                  {
  650. //                      String season = order.get_Value("kst_season").toString();
  651. //                      int M_PriceList_ID = order.getM_PriceList_ID();
  652. //                      MPriceListVersion mplVersion = new Query(Env.getCtx(),MPriceListVersion.Table_Name,"m_pricelist_id = ? AND kst_season = ?",trxName)
  653. //                      .setParameters(M_PriceList_ID,season).first();
  654. //                      if(mplVersion!=null)
  655. //                      {
  656. //                          MProductPrice prodPrice = new Query (Env.getCtx(),MProductPrice.Table_Name,"m_pricelist_version_id = ? AND m_product_id = ?",trxName)
  657. //                          .setParameters(mplVersion.getM_PriceList_Version_ID(),iol.getM_Product_ID()).first();
  658. //                          if (prodPrice == null)
  659. //                              throw new AdempiereException("Tidak ada Product Price untuk Season " + season +" di product "+ iol.getM_Product().getValue());
  660. //                          BigDecimal priceLimit = prodPrice.getPriceLimit();
  661. //                          BigDecimal priceList = prodPrice.getPriceList();
  662. //                          BigDecimal priceStd = prodPrice.getPriceStd();
  663. //                     
  664. //                          iol.setPriceActual(priceStd);
  665. //                          iol.setPriceList(priceList);
  666. //                          iol.setPriceLimit(priceLimit);
  667. //                          if(iol.getQtyEntered().compareTo(iol.getQtyOrdered())==0)
  668. //                              iol.setPriceEntered(priceStd);
  669. //                          else
  670. //                          {
  671. //                              iol.setPriceEntered(priceStd.multiply(iol.getQtyEntered()
  672. //                                  .divide(priceStd, 12, BigDecimal.ROUND_HALF_UP)));
  673. //                          }
  674. //                      }  
  675. //                  }
  676.                    
  677.                    
  678.                     if(iol.getC_UOM_ID()!=iol.getM_Product().getC_UOM_ID()){
  679.                         BigDecimal priceEntered = MUOMConversion.convertProductFrom(Env.getCtx(), M_Product_ID, C_UOM_ID, iol.getPriceEntered());
  680.                         if (priceEntered==null)
  681.                         {
  682.                             log.info("");
  683.                             Env.reset(false);
  684.                             CacheMgt.get().reset();
  685.                             MUOM uomFrom = new MUOM(Env.getCtx(), C_UOM_ID, trxName);
  686.                             MUOM uomTo = new MUOM(Env.getCtx(), iol.getM_Product().getC_UOM_ID(), trxName);
  687.                             throw new AdempiereException("Product " + iol.getM_Product().getName() +" "+ iol.getM_Product().getValue()+
  688.                                     " ini tidak memiliki conversion dari " + uomFrom.getName() + " ke " + uomTo.getName());
  689.                         }
  690.                         iol.setPriceEntered(priceEntered);
  691.                         BigDecimal priceActual = MUOMConversion.convertProductTo(Env.getCtx(), M_Product_ID, C_UOM_ID, iol.getPriceEntered());
  692.                         iol.setPriceActual(priceActual);
  693.                         iol.setPriceList(priceActual);
  694.                     }
  695.                    
  696.                     //iol.setC_UOM_ID(C_UOM_ID);
  697.                     /*
  698.                     if(C_UOM_ID!=iol.getM_Product().getC_UOM_ID()){
  699.                         BigDecimal qty = MUOMConversion.convertProductFrom(Env.getCtx(), M_Product_ID, C_UOM_ID, QtyEntered);
  700.                         iol.setQtyOrdered(qty);
  701.                         iol.setC_UOM_ID(C_UOM_ID);
  702.                     }
  703.                     */
  704.                     //iol.setPrice(rLine.getPriceActual());
  705.                 }
  706.                 // * end #1714
  707.                
  708.                 //add by Sidik - Exceptional PO to force FOC
  709.                 int PO_CM = 1000169;
  710.                 int DocType = order.getC_DocTypeTarget_ID();
  711.                 if(DocType == PO_CM){
  712.                     iol.set_ValueOfColumn("isFOCkst", true);
  713.                     iol.setPrice(Env.ZERO);
  714.                     iol.setPriceActual(Env.ZERO);
  715.                     iol.setPriceList(Env.ZERO);
  716.                 }
  717.                 iol.saveEx();
  718.  
  719.                 // Create Order Detail
  720.                 MKSTOrderDetail detail = new MKSTOrderDetail(iol);
  721.                 detail.setM_RequisitionLine_ID(M_RequisitionLine_ID);
  722.                 BigDecimal QtyConverted = QtyEntered;
  723.                 if (C_UOM_ID != iol.getM_Product().getC_UOM_ID())
  724.                     QtyConverted = MUOMConversion.convertProductFrom(iol.getCtx(), M_Product_ID, C_UOM_ID, QtyEntered); //MUOMConversion.convert(iol.getM_Product().getC_UOM_ID(), C_UOM_ID, QtyEntered, true);
  725.                 if (QtyConverted==null)
  726.                 {
  727.                     log.info("");
  728.                     Env.reset(false);
  729.                     CacheMgt.get().reset();
  730.                     MUOM uomFrom = new MUOM(Env.getCtx(), C_UOM_ID, trxName);
  731.                     MUOM uomTo = new MUOM(Env.getCtx(), iol.getM_Product().getC_UOM_ID(), trxName);
  732.                     throw new AdempiereException("Product " + iol.getM_Product().getName() +" "+ iol.getM_Product().getValue()+
  733.                             " ini tidak memiliki conversion dari " + uomFrom.getName() + " ke " + uomTo.getName());
  734.                 }
  735.                 detail.setQty(QtyConverted);
  736.                 detail.setC_UOM_ID(iol.getM_Product().getC_UOM_ID());
  737.                 detail.saveEx();
  738.             }   //   if selected
  739.         }   //  for all rows
  740.  
  741.         /**
  742.          *  Update Header
  743.          */
  744.         if (p_order != null && p_order.getC_Order_ID() != 0)
  745.         {
  746.             order.setC_Order_ID (p_order.getC_Order_ID());
  747.             order.setAD_OrgTrx_ID(p_order.getAD_OrgTrx_ID());
  748.             order.setC_Project_ID(p_order.getC_Project_ID());
  749.             order.setC_Campaign_ID(p_order.getC_Campaign_ID());
  750.             order.setC_Activity_ID(p_order.getC_Activity_ID());
  751.             order.setUser1_ID(p_order.getUser1_ID());
  752.             order.setUser2_ID(p_order.getUser2_ID());
  753.  
  754.             if ( p_order.isDropShip() )
  755.             {
  756.                 order.setM_Warehouse_ID( p_order.getM_Warehouse_ID() );
  757.                 order.setIsDropShip(p_order.isDropShip());
  758.                 order.setDropShip_BPartner_ID(p_order.getDropShip_BPartner_ID());
  759.                 order.setDropShip_Location_ID(p_order.getDropShip_Location_ID());
  760.                 order.setDropShip_User_ID(p_order.getDropShip_User_ID());
  761.             }
  762.         }
  763.         order.saveEx();
  764.         return true;       
  765.  
  766.     }   //  saveOrder
  767.  
  768.     //edited by Surya Alison, ignore Desc pada penggabungan orderline
  769.     private MOrderLine getOrderLine(MOrder order, int m_product_id, String desc, Timestamp datePromised) {
  770.         String where = "C_Order_ID=? AND M_Product_ID=?";
  771.         List<Object> para = new ArrayList<Object>();
  772.         para.add(order.get_ID());
  773.         para.add(m_product_id);
  774.        
  775.         MProduct product = new MProduct(order.getCtx(),m_product_id,order.get_TrxName());
  776.         X_kst_product_category productCategoryMapping = new Query(order.getCtx(),"kst_product_category","M_Product_Category_ID = ?",order.get_TrxName())
  777.                                     .setParameters(product.getM_Product_Category_ID()).first();
  778.        
  779.         if(productCategoryMapping==null)
  780.             return null;
  781.        
  782.         if(productCategoryMapping.get_ValueAsBoolean("isPerPOBuyer"))
  783.                 return null;                   
  784.        
  785.         if(order.getC_DocTypeTarget_ID()!=DOCTYPE_PO_INDIRECT && order.getC_DocTypeTarget_ID()!=DOCTYPE_PO_INDIRECT_2 && order.getC_DocTypeTarget_ID()!= DOCTYPE_PO_ASSET){
  786.             where += " AND DatePromised=?";
  787.             para.add(datePromised);
  788.         }
  789.         else
  790.         {
  791.             if(desc!=null && !desc.equals("")){
  792.                  where += " AND kst_Description=?";
  793.                  para.add(desc);
  794.             }
  795.         }
  796.         /*if(desc!=null && !desc.equals("")){
  797.              where += " AND kst_Description=?";
  798.              para.add(desc);
  799.         }*/
  800.        
  801.         MOrderLine line = new Query(order.getCtx(), MOrderLine.Table_Name, where, order.get_TrxName())
  802.                             .setParameters(para)
  803.                             .first();
  804.        
  805.         return line;
  806.     }
  807.  
  808.     protected Vector<String> getOISColumnNames()
  809.     {
  810.         //  Header Info
  811.         Vector<String> columnNames = new Vector<String>(5);
  812.         columnNames.add(Msg.getMsg(Env.getCtx(), "Select"));
  813.         columnNames.add(Msg.translate(Env.getCtx(), "Quantity"));
  814.         columnNames.add(Msg.translate(Env.getCtx(), "C_UOM_ID"));
  815.         columnNames.add(Msg.translate(Env.getCtx(), "M_Product_ID"));
  816.         columnNames.add(Msg.translate(Env.getCtx(), "C_Charge_ID"));
  817.         columnNames.add(Msg.getElement(Env.getCtx(), "M_RequisitionLine_ID"));
  818.         columnNames.add(Msg.getElement(Env.getCtx(), "kst_Division"));
  819.         columnNames.add(Msg.getElement(Env.getCtx(), "C_Order_ID"));
  820.         columnNames.add(Msg.getElement(Env.getCtx(), "kst_Description"));
  821.         columnNames.add(Msg.getElement(Env.getCtx(), "DatePromised"));
  822.        
  823.        
  824.         return columnNames;
  825.     }
  826. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement