Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /******************************************************************************
- * Copyright (C) 2009 Low Heng Sin *
- * Copyright (C) 2009 Idalica Corporation *
- * This program is free software; you can redistribute it and/or modify it *
- * under the terms version 2 of the GNU General Public License as published *
- * by the Free Software Foundation. This program is distributed in the hope *
- * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied *
- * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
- * See the GNU General Public License for more details. *
- * You should have received a copy of the GNU General Public License along *
- * with this program; if not, write to the Free Software Foundation, Inc., *
- * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *
- *****************************************************************************/
- package org.kosta.aoi.form;
- import java.math.BigDecimal;
- import java.sql.PreparedStatement;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Timestamp;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Vector;
- import java.util.logging.Level;
- import org.adempiere.exceptions.AdempiereException;
- import org.compiere.apps.IStatusBar;
- import org.compiere.grid.CreateFrom;
- import org.compiere.minigrid.IMiniTable;
- import org.compiere.model.GridTab;
- import org.compiere.model.MOrder;
- import org.compiere.model.MOrderLine;
- import org.compiere.model.MOrg;
- import org.compiere.model.MProduct;
- import org.compiere.model.MProductPO;
- import org.compiere.model.MProductPrice;
- import org.compiere.model.MRequisition;
- import org.compiere.model.MRequisitionLine;
- import org.compiere.model.MUOM;
- import org.compiere.model.MUOMConversion;
- import org.compiere.model.Query;
- import org.compiere.util.CacheMgt;
- import org.compiere.util.DB;
- import org.compiere.util.Env;
- import org.compiere.util.KeyNamePair;
- import org.compiere.util.Msg;
- import org.kosta.aoi.model.MKSTOrderDetail;
- import org.surya.production.model.X_kst_product_category;
- /**
- * Create PO Orders Transactions from Requisition
- *
- * @author Zuhri Utama (zuhriutama@gmail.com)
- */
- public abstract class CreateFromOrder extends CreateFrom
- {
- protected static final int DOCTYPE_PO_INDIRECT = 1000047; // Purchase Order Indirect Material
- protected static final int DOCTYPE_PO_INDIRECT_2 = 1000127; // Purchase Order Indirect Material - AOI2
- protected static final int DOCTYPE_PO_DIRECT_ADIDAS = 1000048; // Purchase Order Direct Material for ADIDAS
- protected static final int DOCTYPE_PO_DIRECT_PUMA = 1000067; // Purchase Order Direct Material for PUMA
- protected static final int DOCTYPE_PO_ASSET = 1000065; // Purchase Order for Asset
- protected static final int DOCTYPE_PO_DEVELOPMENT = 1000114; // Purchase Order for Development Sample
- protected static final int DOCTYPE_PR_DIRECT_DEV = 1000166; // Requisition for Development
- protected static final int DOCTYPE_PR_DIRECT = 1000060; // Purchase Requisition Direct Material
- protected static final int DOCTYPE_PR_INDIRECT = 1000061; // Purchase Requisition Indirect Material
- protected static final int DOCTYPE_PR_ASSET = 1000063; // Purchase Requisition for Asset
- protected static final int DOCTYPE_PO_INDIRECT_KEMILAU = 1000131;// Purchase Order Indirect Material Kemilau
- private MOrg m_org = null;
- protected MOrder m_order = null;
- private int AD_Client_ID = 0;
- protected int AD_Org_ID = 0; // added by @ZuhriUtama
- private int C_Order_ID = 0;
- private int M_Warehouse_ID = 0; // add by AH@kosta
- /** Loaded Requisition */
- private MRequisition m_requisition = null;
- /**
- * Protected Constructor
- * @param mTab MTab
- */
- public CreateFromOrder(GridTab mTab)
- {
- super(mTab);
- if (log.isLoggable(Level.INFO)) log.info(mTab.toString());
- } // CreateFromOrder
- /**
- * Dynamic Init
- * @return true if initialized
- */
- public boolean dynInit() throws Exception
- {
- log.config("");
- setTitle(Msg.getElement(Env.getCtx(), "C_Order_ID", false) + " .. " + Msg.translate(Env.getCtx(), "CreateFrom"));
- AD_Client_ID = ((Integer) getGridTab().getValue("AD_Client_ID")).intValue();
- AD_Org_ID = ((Integer) getGridTab().getValue("AD_Org_ID")).intValue(); // added by @ZuhriUtama
- C_Order_ID = ((Integer) getGridTab().getValue("C_Order_ID")).intValue();
- M_Warehouse_ID = ((Integer) getGridTab().getValue("M_Warehouse_ID")).intValue(); // add by AH@kosta
- m_order = new MOrder(Env.getCtx(), C_Order_ID, null);
- return true;
- } // dynInit
- /**
- * Load Requisition Field.
- */
- protected ArrayList<KeyNamePair> loadOrganizationData() {
- ArrayList<KeyNamePair> list = new ArrayList<KeyNamePair>();
- String sqlStmt = "SELECT o.AD_Org_ID, o.Name from AD_Org o WHERE o.AD_Client_ID=?";
- PreparedStatement pstmt = null;
- ResultSet rs = null;
- try {
- pstmt = DB.prepareStatement(sqlStmt, null);
- pstmt.setInt(1, AD_Client_ID);
- rs = pstmt.executeQuery();
- while (rs.next()) {
- list.add(new KeyNamePair(rs.getInt(1), rs.getString(2)));
- }
- } catch (SQLException e) {
- log.log(Level.SEVERE, sqlStmt.toString(), e);
- } finally{
- DB.close(rs, pstmt);
- rs = null;
- pstmt = null;
- }
- return list;
- }
- /**
- * Load Product Category Field.
- */
- protected ArrayList<KeyNamePair> loadProductCategoryData() {
- ArrayList<KeyNamePair> list = new ArrayList<KeyNamePair>();
- String sqlStmt = "SELECT o.M_Product_Category_ID, o.Name from M_Product_Category o WHERE o.AD_Client_ID=?";
- PreparedStatement pstmt = null;
- ResultSet rs = null;
- try {
- pstmt = DB.prepareStatement(sqlStmt, null);
- pstmt.setInt(1, AD_Client_ID);
- rs = pstmt.executeQuery();
- while (rs.next()) {
- list.add(new KeyNamePair(rs.getInt(1), rs.getString(2)));
- }
- } catch (SQLException e) {
- log.log(Level.SEVERE, sqlStmt.toString(), e);
- } finally{
- DB.close(rs, pstmt);
- rs = null;
- pstmt = null;
- }
- return list;
- }
- protected ArrayList<KeyNamePair> loadDocTypeData() {
- ArrayList<KeyNamePair> list = new ArrayList<KeyNamePair>();
- String sqlStmt = "SELECT o.C_DocType_ID, o.Name from C_DocType o WHERE o.AD_Client_ID=? "
- + " AND DocBaseType = 'POR'";
- if(m_order.getC_DocTypeTarget_ID()==DOCTYPE_PO_DEVELOPMENT){
- sqlStmt += " AND o.C_DocType_ID="+DOCTYPE_PR_DIRECT_DEV;
- }
- PreparedStatement pstmt = null;
- ResultSet rs = null;
- try {
- pstmt = DB.prepareStatement(sqlStmt, null);
- pstmt.setInt(1, AD_Client_ID);
- rs = pstmt.executeQuery();
- while (rs.next()) {
- list.add(new KeyNamePair(rs.getInt(1), rs.getString(2)));
- }
- } catch (SQLException e) {
- log.log(Level.SEVERE, sqlStmt.toString(), e);
- } finally{
- DB.close(rs, pstmt);
- rs = null;
- pstmt = null;
- }
- return list;
- }
- /**
- * Load Requisition Field.
- * @param M_Product_Category_ID
- * @param AD_Org_ID
- */
- protected ArrayList<KeyNamePair> loadRequisitionByOrg(int AD_Org_ID) {
- ArrayList<KeyNamePair> list = new ArrayList<KeyNamePair>();
- String sqlStmt = "SELECT r.M_Requisition_ID, r.DocumentNo from M_Requisition r "
- + "WHERE r.DocStatus in ('CO', 'CL') "
- + "AND EXISTS(SELECT 1 FROM M_RequisitionLine rl "
- + "WHERE rl.M_Requisition_ID=r.M_Requisition_ID "
- + "AND rl.AD_Org_ID=? AND rl.isActive = 'Y'"
- + "AND rl.C_BPartner_ID=? "
- + "AND (rl.Qty-"
- + "(SELECT COALESCE(SUM(dt.Qty),0) "
- + "FROM kst_orderdetail dt "
- + "JOIN c_orderline ol on dt.c_orderline_id=ol.c_Orderline_id "
- + "JOIN c_order o on ol.c_order_id=o.c_Order_id "
- + "where o.DocStatus IN ('DR','IP','CO','CL') AND dt.m_requisitionline_id=rl.m_requisitionline_id)) > 0) "
- + " AND r.M_Warehouse_ID = ? "; // #908
- if(m_order.getC_DocTypeTarget_ID()==DOCTYPE_PO_DIRECT_PUMA || m_order.getC_DocTypeTarget_ID()==DOCTYPE_PO_DIRECT_ADIDAS){
- sqlStmt += "AND r.c_doctype_id="+DOCTYPE_PR_DIRECT;
- }else if(m_order.getC_DocTypeTarget_ID()==DOCTYPE_PO_INDIRECT || m_order.getC_DocTypeTarget_ID()==DOCTYPE_PO_INDIRECT_2){
- sqlStmt += "AND r.c_doctype_id="+DOCTYPE_PR_INDIRECT;
- }else if(m_order.getC_DocTypeTarget_ID()==DOCTYPE_PO_ASSET){
- sqlStmt += "AND r.c_doctype_id="+DOCTYPE_PR_ASSET;
- }else if(m_order.getC_DocTypeTarget_ID()==DOCTYPE_PO_DEVELOPMENT){
- sqlStmt += "AND r.c_doctype_id="+DOCTYPE_PR_DIRECT_DEV;
- }
- sqlStmt += " ORDER BY r.DocumentNo DESC ";
- PreparedStatement pstmt = null;
- ResultSet rs = null;
- try {
- pstmt = DB.prepareStatement(sqlStmt, null);
- pstmt.setInt(1, AD_Org_ID);
- pstmt.setInt(2, m_order.getC_BPartner_ID());
- pstmt.setInt(3, M_Warehouse_ID);
- rs = pstmt.executeQuery();
- while (rs.next()) {
- list.add(new KeyNamePair(rs.getInt(1), rs.getString(2)));
- }
- } catch (SQLException e) {
- log.log(Level.SEVERE, sqlStmt.toString(), e);
- } finally{
- DB.close(rs, pstmt);
- rs = null;
- pstmt = null;
- }
- return list;
- }
- 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)
- {
- Vector<Vector<Object>> data = new Vector<Vector<Object>>();
- StringBuilder sqlStmt = new StringBuilder();
- //added by Syahnan@Kosta-Consulting 20160224
- sqlStmt.append("SELECT a.M_RequisitionLine_ID, a.line, a.qty , a.M_Product_ID, a.product, a.C_UOM_ID, a.value "
- + ", a.kst_Division , a.DocumentNo , a.Description , a.C_Charge_ID, a.name, a.datepromised ");
- sqlStmt.append("FROM ( ");
- sqlStmt.append("SELECT rl.M_RequisitionLine_ID, r.documentno as line, "
- + "(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 "
- + ", p.M_Product_ID, p.value||'_'||p.Name as product, uom.C_UOM_ID, COALESCE(uom.UOMSymbol,uom.Name) as value ");
- sqlStmt.append(", rl.kst_Division "); // Division
- sqlStmt.append(", o.DocumentNo "); // Sales Order Document No
- if(m_order.getC_DocTypeTarget_ID()==DOCTYPE_PO_INDIRECT || m_order.getC_DocTypeTarget_ID()==DOCTYPE_PO_ASSET
- ||m_order.getC_DocTypeTarget_ID()==DOCTYPE_PO_INDIRECT_2){
- sqlStmt.append(", rl.kst_Description as Description "); // KST Description
- }
- else sqlStmt.append(", case when rl.Description IS NOT NULL THEN rl.Description else rl.poreference end as Description"); // Description -->when case fajar tambahan
- sqlStmt.append(", c.C_Charge_ID, c.name, rl.datepromised "); // Charge
- sqlStmt.append("FROM M_RequisitionLine rl ");
- sqlStmt.append("INNER JOIN M_Requisition r ON r.M_Requisition_ID=rl.M_Requisition_ID ");
- sqlStmt.append("LEFT JOIN M_Product p ON p.M_Product_ID = rl.M_Product_ID ");
- sqlStmt.append("LEFT JOIN C_Charge c ON rl.C_Charge_ID = c.C_Charge_ID ");
- sqlStmt.append("LEFT JOIN C_OrderLine ol ON rl.C_OrderLine_ID=ol.C_OrderLine_ID ");
- sqlStmt.append("LEFT JOIN C_Order o ON ol.C_Order_ID=o.C_Order_ID ");
- sqlStmt.append("LEFT JOIN C_DocType dt ON dt.C_DocType_ID= r.C_DocType_ID ");
- //if (Env.isBaseLanguage(Env.getCtx(), "C_UOM"))
- //{
- sqlStmt.append("LEFT OUTER JOIN C_UOM uom ON (uom.C_UOM_ID=rl.C_UOM_ID) ");
- //}
- //else
- //{
- // sqlStmt.append("LEFT OUTER JOIN C_UOM_Trl uom ON (uom.C_UOM_ID=100 AND uom.AD_Language='");
- // sqlStmt.append(Env.getAD_Language(Env.getCtx())).append("') ");
- //}
- //sqlStmt.append("WHERE rl.AD_Org_ID=? AND rl.C_BPartner_ID=? AND r.DocStatus IN ('CO','CL') ");
- sqlStmt.append("WHERE rl.AD_Org_ID=? AND rl.C_BPartner_ID=? AND r.DocStatus IN ('CO') ");
- /* Added by Roynaldo - 20151020
- * Include only Active Req Line
- */
- sqlStmt.append("AND rl.IsActive='Y' ");
- sqlStmt.append("AND rl.qty <> 0 ");
- /* End Added by Roynaldo - 20151020 */
- if(M_Product_Category_ID>0)
- sqlStmt.append("AND p.M_Product_Category_ID=? ");
- if(M_Requisition_ID>0)
- sqlStmt.append("AND rl.M_Requisition_ID=? ");
- else
- {
- sqlStmt.append("AND rl.M_Requisition_ID IN (");
- ArrayList<KeyNamePair> reqList = loadRequisitionByOrg(AD_Org_ID);
- int x=0;
- for(KeyNamePair req: reqList)
- {
- if(++x ==reqList.size())
- sqlStmt.append(req.getID()+") ");
- else
- sqlStmt.append(req.getID()+",");
- }
- }
- if(upc!=null && !upc.equals(""))
- sqlStmt.append("AND p.upc=? ");
- if(value!=null && !value.equals(""))
- sqlStmt.append("AND p.value= ? ");
- if(jobOrder!=null && !jobOrder.equals(""))
- sqlStmt.append("AND rl.Description LIKE ? ");
- if(jobOrderAutoPR!=null && !jobOrderAutoPR.equals(""))
- sqlStmt.append("AND rl.kst_JobOrder LIKE ? ");
- if(datePromised!= null && !datePromised.equals("")){
- sqlStmt.append(" AND rl.datepromised = ? ");
- }
- if(C_DocType_ID > 0){
- sqlStmt.append(" AND dt.C_DocType_ID = ? ");
- }
- // filter for only QtyRemaining of Requisition>0
- // remark by Syahnan@Kosta-Consulting
- //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 ");
- if(m_order.getC_DocTypeTarget_ID()==DOCTYPE_PO_DIRECT_PUMA || m_order.getC_DocTypeTarget_ID()==DOCTYPE_PO_DIRECT_ADIDAS){
- sqlStmt.append("AND r.c_doctype_id="+DOCTYPE_PR_DIRECT);
- }else if(m_order.getC_DocTypeTarget_ID()==DOCTYPE_PO_INDIRECT || m_order.getC_DocTypeTarget_ID()==DOCTYPE_PO_INDIRECT_2){
- sqlStmt.append("AND r.c_doctype_id="+DOCTYPE_PR_INDIRECT);
- }else if(m_order.getC_DocTypeTarget_ID()==DOCTYPE_PO_ASSET){
- sqlStmt.append("AND r.c_doctype_id="+DOCTYPE_PR_ASSET);
- }else if(m_order.getC_DocTypeTarget_ID()==DOCTYPE_PO_DEVELOPMENT){
- sqlStmt.append("AND r.c_doctype_id="+DOCTYPE_PR_DIRECT_DEV);
- }
- // add by Syahnan@Kosta-Consulting
- sqlStmt.append(" ) a ");
- sqlStmt.append("GROUP BY a.M_RequisitionLine_ID, a.line, a.qty , a.M_Product_ID, a.product, a.C_UOM_ID, a.value "
- + " , a.kst_Division , a.DocumentNo , a.Description , a.C_Charge_ID, a.name, a.datepromised ");
- sqlStmt.append(" HAVING a.qty > 0");
- sqlStmt.append(" ORDER BY a.Line, a.product");
- PreparedStatement pstmt = null;
- ResultSet rs = null;
- try
- {
- pstmt = DB.prepareStatement(sqlStmt.toString(), null);
- int index = 1;
- pstmt.setInt(index++, AD_Org_ID);
- pstmt.setInt(index++, m_order.getC_BPartner_ID());
- if(M_Product_Category_ID>0){
- pstmt.setInt(index++, M_Product_Category_ID);
- }
- if(M_Requisition_ID>0){
- pstmt.setInt(index++, M_Requisition_ID);
- }
- if(upc!=null && !upc.equals("")){
- pstmt.setString(index++, upc);
- }
- if(value!=null && !value.equals("")){
- pstmt.setString(index++, value);
- }
- if(jobOrder!=null && !jobOrder.equals("")){
- pstmt.setString(index++, jobOrder);
- }
- if(jobOrderAutoPR!=null && !jobOrderAutoPR.equals("")){
- pstmt.setString(index++, jobOrderAutoPR);
- }
- if(datePromised!= null && !datePromised.equals("")){
- pstmt.setTimestamp(index++, datePromised);
- }
- if(C_DocType_ID > 0){
- pstmt.setInt(index++, C_DocType_ID);
- }
- rs = pstmt.executeQuery();
- while (rs.next())
- {
- Vector<Object> line = new Vector<Object>(6);
- line.add(new Boolean(false)); // 0-Selection
- line.add(rs.getBigDecimal(3)); // 1-Qty
- KeyNamePair pp = new KeyNamePair(rs.getInt(6), rs.getString(7));
- line.add(pp); // 2-UOM
- pp = new KeyNamePair(rs.getInt(4), rs.getString(5));
- line.add(pp); // 3-Product
- pp = new KeyNamePair(rs.getInt(11), rs.getString(12));
- line.add(pp); // 4-Charge
- pp = new KeyNamePair(rs.getInt(1), rs.getString(2));
- line.add(pp); //5-Requisition
- line.add(rs.getString(8)); // 6-Division
- line.add(rs.getString(9)); // 7-SO No
- line.add(rs.getString(10)); // 8-Description
- line.add(rs.getString("DatePromised")); // 9-date promised
- data.add(line);
- }
- }
- catch (Exception ex)
- {
- log.log(Level.SEVERE, sqlStmt.toString(), ex);
- }
- finally
- {
- DB.close(rs, pstmt);
- rs = null; pstmt = null;
- }
- return data;
- }
- /**
- * List number of rows selected
- */
- public void info(IMiniTable miniTable, IStatusBar statusBar)
- {
- } // infoInvoice
- protected void configureMiniTable (IMiniTable miniTable)
- {
- miniTable.setColumnClass(0, Boolean.class, false); // Selection
- miniTable.setColumnClass(1, BigDecimal.class, false); // Qty
- miniTable.setColumnClass(2, String.class, true); // UOM
- miniTable.setColumnClass(3, String.class, true); // Product
- miniTable.setColumnClass(4, String.class, true); // Charge
- miniTable.setColumnClass(5, String.class, true); // Requisition
- miniTable.setColumnClass(6, String.class, true); // division
- miniTable.setColumnClass(7, String.class, true); // SO
- miniTable.setColumnClass(8, String.class, true); // Description
- miniTable.setColumnClass(9, String.class, true); // Date Promised
- // Table UI
- miniTable.autoSize();
- }
- /**
- * Save - Create Invoice Lines
- * @return true if saved
- */
- public boolean save(IMiniTable miniTable, String trxName)
- {
- // Get Shipment
- int C_Order_ID = ((Integer) getGridTab().getValue("C_Order_ID")).intValue();
- MOrder order = new MOrder(Env.getCtx(), C_Order_ID, trxName);
- if (log.isLoggable(Level.CONFIG)) log.config(order.toString());
- // Lines
- for (int i = 0; i < miniTable.getRowCount(); i++)
- {
- if (((Boolean)miniTable.getValueAt(i, 0)).booleanValue()) {
- // variable values
- BigDecimal QtyEntered = (BigDecimal) miniTable.getValueAt(i, 1); // Qty
- KeyNamePair pp = (KeyNamePair) miniTable.getValueAt(i, 2); // UOM
- int C_UOM_ID = pp.getKey();
- pp = (KeyNamePair) miniTable.getValueAt(i, 3); // Product
- int M_Product_ID = pp.getKey();
- pp = (KeyNamePair) miniTable.getValueAt(i, 4); // Charge
- int C_Charge_ID = pp.getKey();
- int M_RequisitionLine_ID = 0;
- pp = (KeyNamePair) miniTable.getValueAt(i, 5); // Requisition
- // If we have Requisition
- if (pp != null)
- M_RequisitionLine_ID = pp.getKey();
- // Precision of Qty UOM
- int precision = 2;
- if (M_Product_ID != 0)
- {
- MProduct product = MProduct.get(Env.getCtx(), M_Product_ID);
- precision = product.getUOMPrecision();
- }
- QtyEntered = QtyEntered.setScale(precision, BigDecimal.ROUND_HALF_DOWN);
- //
- if (log.isLoggable(Level.FINE)) log.fine("Line QtyEntered=" + QtyEntered
- + ", Product=" + M_Product_ID
- + ", RequisitionLine=" + M_RequisitionLine_ID);
- String desc = (String)miniTable.getValueAt(i, 8); // kst_Description
- // Check if already exist order line with product
- MRequisitionLine rLine = new MRequisitionLine(Env.getCtx(), M_RequisitionLine_ID, trxName);
- MOrderLine iol = getOrderLine(order, M_Product_ID, desc, (Timestamp)rLine.get_Value("DatePromised"));
- if(iol==null){
- iol = new MOrderLine (order);
- if(M_Product_ID>0)
- iol.setM_Product_ID(M_Product_ID, C_UOM_ID); // Line UOM
- if(C_Charge_ID>0)
- iol.setC_Charge_ID(C_Charge_ID);
- iol.setDatePromised((Timestamp)rLine.get_Value("DatePromised"));
- iol.setQty(QtyEntered); // Movement/Entered
- iol.set_ValueOfColumn("kst_Description", desc);
- iol.set_ValueOfColumn("poreference",rLine.get_Value("poreference"));
- iol.set_ValueOfColumn("kst_joborder",rLine.get_Value("kst_joborder"));
- }else{
- iol.setQty(QtyEntered.add(iol.getQtyEntered())); // Movement/Entered
- }
- // Dimension AH@20190123
- iol.setC_Activity_ID(rLine.get_ValueAsInt("C_Activity_ID"));
- iol.setC_Campaign_ID(rLine.get_ValueAsInt("C_Campaign_ID"));
- /*
- * #1714 - Set Product Price PO. added by @ZuhriUtama
- */
- // set price for All DocType use Last PO Price except PO Development
- if(M_Product_ID>0 && !order.getC_DocTypeTarget().getName().equalsIgnoreCase("PO-DEVELOPMENT"))
- /* Comment by Sidik to All DocType use Last PO Price
- */
- {
- MProductPO[] productPOs = MProductPO.getOfProduct(Env.getCtx(), M_Product_ID, null);
- MProductPO curPO = null;
- for(MProductPO productPO : productPOs){
- if(productPO.getC_BPartner_ID()==order.getC_BPartner_ID() && productPO.getC_Currency_ID() == order.getC_Currency_ID())
- curPO = productPO;
- }
- if(productPOs.length==0)
- throw new AdempiereException("No Vendor Price Found for Product "+iol.getM_Product().getValue());
- if(curPO==null){
- // curPO = productPOs[0]; // change by AH@kosta
- iol.setHeaderInfo(order);
- iol.setPrice();
- // develop if product purchasing is not exsis
- //mas uta tolong cek a
- MProductPO productPurchasing = new MProductPO(Env.getCtx(), 0, null);
- productPurchasing.setAD_Org_ID(order.getAD_Org_ID());
- productPurchasing.setM_Product_ID(M_Product_ID);
- productPurchasing.setC_BPartner_ID(order.getC_BPartner_ID());
- BigDecimal zeroPrice = new BigDecimal("1");
- productPurchasing.setPriceLastPO(zeroPrice);
- productPurchasing.setPriceList(zeroPrice);
- productPurchasing.setC_Currency_ID(order.getC_Currency_ID());
- productPurchasing.setC_UOM_ID(iol.getM_Product().getC_UOM_ID());
- productPurchasing.setVendorProductNo(iol.getM_Product().getValue());
- productPurchasing.setIsCurrentVendor(false);
- productPurchasing.saveEx();
- //throw new AdempiereException("No Vendor Price Found for Product "+iol.getM_Product().getValue());
- }
- else {
- //iol.setPriceActual(curPO.getPriceList());
- //iol.setPriceList(curPO.getPriceList());
- if(curPO.getPriceLastPO().compareTo(BigDecimal.ZERO)==0)
- {
- iol.setPriceActual(curPO.getPriceList());
- iol.setPriceList(curPO.getPriceList());
- }
- else
- {
- iol.setPriceActual(curPO.getPriceLastPO()); // changed by Surya Alison #1605
- iol.setPriceList(curPO.getPriceLastPO()); //changed by Surya Alison #1605
- }
- }
- if(iol.getC_UOM_ID()!=iol.getM_Product().getC_UOM_ID()){
- //BigDecimal priceEntered = MUOMConversion.convertProductTo(Env.getCtx(), M_Product_ID, iol.getC_UOM_ID(), curPO.getPriceList());
- //mas uta tolong cek
- if(curPO.getPriceLastPO().compareTo(BigDecimal.ZERO)!=0)
- {
- BigDecimal priceEntered = MUOMConversion.convertProductTo(Env.getCtx(), M_Product_ID, iol.getC_UOM_ID(), curPO.getPriceLastPO()); // changed by Surya Alison #1605
- if (priceEntered==null)
- {
- log.info("");
- Env.reset(false);
- CacheMgt.get().reset();
- MUOM uomFrom = new MUOM(Env.getCtx(), C_UOM_ID, trxName);
- MUOM uomTo = new MUOM(Env.getCtx(), iol.getM_Product().getC_UOM_ID(), trxName);
- throw new AdempiereException("Product " + iol.getM_Product().getName() +" "+ iol.getM_Product().getValue()+
- " ini tidak memiliki conversion dari " + uomFrom.getName() + " ke " + uomTo.getName());
- }
- iol.setPriceEntered(priceEntered);
- }
- else
- {
- BigDecimal priceEntered = MUOMConversion.convertProductTo(Env.getCtx(), M_Product_ID, iol.getC_UOM_ID(), curPO.getPriceList()); // changed by Surya Alison #1605
- if (priceEntered==null)
- {
- log.info("");
- Env.reset(false);
- CacheMgt.get().reset();
- MUOM uomFrom = new MUOM(Env.getCtx(), C_UOM_ID, trxName);
- MUOM uomTo = new MUOM(Env.getCtx(), iol.getM_Product().getC_UOM_ID(), trxName);
- throw new AdempiereException("Product " + iol.getM_Product().getName() +" "+ iol.getM_Product().getValue()+
- " ini tidak memiliki conversion dari " + uomFrom.getName() + " ke " + uomTo.getName());
- }
- iol.setPriceEntered(priceEntered);
- }
- }
- else{
- // Conversion UOM Product
- if ( curPO != null){
- if(curPO.getPriceLastPO().compareTo(BigDecimal.ZERO)==0)
- iol.setPriceEntered(curPO.getPriceList());
- else
- iol.setPriceEntered(curPO.getPriceLastPO());
- }
- }
- }
- else
- {
- // set price by product price @ZuhriUtama
- iol.setHeaderInfo(order);
- //Prepare Plugin by Sidik
- // 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 ";
- // if(order.get_ValueAsString("kst_Season").equals("") || order.get_ValueAsString("kst_Season").equals(null)){
- // sqlAmt += "where kst_season is null";
- // }else{
- // sqlAmt += "where kst_season = '"+order.get_ValueAsString("kst_Season")+"'";
- // }
- // sqlAmt += ") tmp LEFT JOIN m_productprice mpp ON tmp.M_PriceList_Version_ID = mpp.M_PriceList_Version_ID"
- // + " where M_product_ID = "+rLine.getM_Product().getM_Product_ID();
- // BigDecimal actual = DB.getSQLValueBD(trxName, sqlAmt);
- //if(actual == 0){
- // iol.setPrice();
- // }else{
- // iol.setPrice(actual);
- // }
- iol.setPrice();
- iol.setC_UOM_ID(C_UOM_ID);
- iol.setQtyOrdered(rLine.getQty());
- //added by Surya Alison
- /*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)
- .setParameters(iol.getM_Product_ID(),iol.getC_BPartner_ID(),iol.getC_Currency_ID(),C_UOM_ID)
- .first();
- if (curPO != null)
- {
- if(curPO.getPriceLastPO().signum()!=0)
- {
- iol.setPriceEntered(curPO.getPriceLastPO());
- }
- }
- */
- //added by Surya Alison -- Set price base on Season.
- // if(order.get_Value("kst_season")!=null && !order.isSOTrx())
- // {
- // String season = order.get_Value("kst_season").toString();
- // int M_PriceList_ID = order.getM_PriceList_ID();
- // MPriceListVersion mplVersion = new Query(Env.getCtx(),MPriceListVersion.Table_Name,"m_pricelist_id = ? AND kst_season = ?",trxName)
- // .setParameters(M_PriceList_ID,season).first();
- // if(mplVersion!=null)
- // {
- // MProductPrice prodPrice = new Query (Env.getCtx(),MProductPrice.Table_Name,"m_pricelist_version_id = ? AND m_product_id = ?",trxName)
- // .setParameters(mplVersion.getM_PriceList_Version_ID(),iol.getM_Product_ID()).first();
- // if (prodPrice == null)
- // throw new AdempiereException("Tidak ada Product Price untuk Season " + season +" di product "+ iol.getM_Product().getValue());
- // BigDecimal priceLimit = prodPrice.getPriceLimit();
- // BigDecimal priceList = prodPrice.getPriceList();
- // BigDecimal priceStd = prodPrice.getPriceStd();
- //
- // iol.setPriceActual(priceStd);
- // iol.setPriceList(priceList);
- // iol.setPriceLimit(priceLimit);
- // if(iol.getQtyEntered().compareTo(iol.getQtyOrdered())==0)
- // iol.setPriceEntered(priceStd);
- // else
- // {
- // iol.setPriceEntered(priceStd.multiply(iol.getQtyEntered()
- // .divide(priceStd, 12, BigDecimal.ROUND_HALF_UP)));
- // }
- // }
- // }
- if(iol.getC_UOM_ID()!=iol.getM_Product().getC_UOM_ID()){
- BigDecimal priceEntered = MUOMConversion.convertProductFrom(Env.getCtx(), M_Product_ID, C_UOM_ID, iol.getPriceEntered());
- if (priceEntered==null)
- {
- log.info("");
- Env.reset(false);
- CacheMgt.get().reset();
- MUOM uomFrom = new MUOM(Env.getCtx(), C_UOM_ID, trxName);
- MUOM uomTo = new MUOM(Env.getCtx(), iol.getM_Product().getC_UOM_ID(), trxName);
- throw new AdempiereException("Product " + iol.getM_Product().getName() +" "+ iol.getM_Product().getValue()+
- " ini tidak memiliki conversion dari " + uomFrom.getName() + " ke " + uomTo.getName());
- }
- iol.setPriceEntered(priceEntered);
- BigDecimal priceActual = MUOMConversion.convertProductTo(Env.getCtx(), M_Product_ID, C_UOM_ID, iol.getPriceEntered());
- iol.setPriceActual(priceActual);
- iol.setPriceList(priceActual);
- }
- //iol.setC_UOM_ID(C_UOM_ID);
- /*
- if(C_UOM_ID!=iol.getM_Product().getC_UOM_ID()){
- BigDecimal qty = MUOMConversion.convertProductFrom(Env.getCtx(), M_Product_ID, C_UOM_ID, QtyEntered);
- iol.setQtyOrdered(qty);
- iol.setC_UOM_ID(C_UOM_ID);
- }
- */
- //iol.setPrice(rLine.getPriceActual());
- }
- // * end #1714
- //add by Sidik - Exceptional PO to force FOC
- int PO_CM = 1000169;
- int DocType = order.getC_DocTypeTarget_ID();
- if(DocType == PO_CM){
- iol.set_ValueOfColumn("isFOCkst", true);
- iol.setPrice(Env.ZERO);
- iol.setPriceActual(Env.ZERO);
- iol.setPriceList(Env.ZERO);
- }
- iol.saveEx();
- // Create Order Detail
- MKSTOrderDetail detail = new MKSTOrderDetail(iol);
- detail.setM_RequisitionLine_ID(M_RequisitionLine_ID);
- BigDecimal QtyConverted = QtyEntered;
- if (C_UOM_ID != iol.getM_Product().getC_UOM_ID())
- 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);
- if (QtyConverted==null)
- {
- log.info("");
- Env.reset(false);
- CacheMgt.get().reset();
- MUOM uomFrom = new MUOM(Env.getCtx(), C_UOM_ID, trxName);
- MUOM uomTo = new MUOM(Env.getCtx(), iol.getM_Product().getC_UOM_ID(), trxName);
- throw new AdempiereException("Product " + iol.getM_Product().getName() +" "+ iol.getM_Product().getValue()+
- " ini tidak memiliki conversion dari " + uomFrom.getName() + " ke " + uomTo.getName());
- }
- detail.setQty(QtyConverted);
- detail.setC_UOM_ID(iol.getM_Product().getC_UOM_ID());
- detail.saveEx();
- } // if selected
- } // for all rows
- /**
- * Update Header
- */
- if (p_order != null && p_order.getC_Order_ID() != 0)
- {
- order.setC_Order_ID (p_order.getC_Order_ID());
- order.setAD_OrgTrx_ID(p_order.getAD_OrgTrx_ID());
- order.setC_Project_ID(p_order.getC_Project_ID());
- order.setC_Campaign_ID(p_order.getC_Campaign_ID());
- order.setC_Activity_ID(p_order.getC_Activity_ID());
- order.setUser1_ID(p_order.getUser1_ID());
- order.setUser2_ID(p_order.getUser2_ID());
- if ( p_order.isDropShip() )
- {
- order.setM_Warehouse_ID( p_order.getM_Warehouse_ID() );
- order.setIsDropShip(p_order.isDropShip());
- order.setDropShip_BPartner_ID(p_order.getDropShip_BPartner_ID());
- order.setDropShip_Location_ID(p_order.getDropShip_Location_ID());
- order.setDropShip_User_ID(p_order.getDropShip_User_ID());
- }
- }
- order.saveEx();
- return true;
- } // saveOrder
- //edited by Surya Alison, ignore Desc pada penggabungan orderline
- private MOrderLine getOrderLine(MOrder order, int m_product_id, String desc, Timestamp datePromised) {
- String where = "C_Order_ID=? AND M_Product_ID=?";
- List<Object> para = new ArrayList<Object>();
- para.add(order.get_ID());
- para.add(m_product_id);
- MProduct product = new MProduct(order.getCtx(),m_product_id,order.get_TrxName());
- X_kst_product_category productCategoryMapping = new Query(order.getCtx(),"kst_product_category","M_Product_Category_ID = ?",order.get_TrxName())
- .setParameters(product.getM_Product_Category_ID()).first();
- if(productCategoryMapping==null)
- return null;
- if(productCategoryMapping.get_ValueAsBoolean("isPerPOBuyer"))
- return null;
- if(order.getC_DocTypeTarget_ID()!=DOCTYPE_PO_INDIRECT && order.getC_DocTypeTarget_ID()!=DOCTYPE_PO_INDIRECT_2 && order.getC_DocTypeTarget_ID()!= DOCTYPE_PO_ASSET){
- where += " AND DatePromised=?";
- para.add(datePromised);
- }
- else
- {
- if(desc!=null && !desc.equals("")){
- where += " AND kst_Description=?";
- para.add(desc);
- }
- }
- /*if(desc!=null && !desc.equals("")){
- where += " AND kst_Description=?";
- para.add(desc);
- }*/
- MOrderLine line = new Query(order.getCtx(), MOrderLine.Table_Name, where, order.get_TrxName())
- .setParameters(para)
- .first();
- return line;
- }
- protected Vector<String> getOISColumnNames()
- {
- // Header Info
- Vector<String> columnNames = new Vector<String>(5);
- columnNames.add(Msg.getMsg(Env.getCtx(), "Select"));
- columnNames.add(Msg.translate(Env.getCtx(), "Quantity"));
- columnNames.add(Msg.translate(Env.getCtx(), "C_UOM_ID"));
- columnNames.add(Msg.translate(Env.getCtx(), "M_Product_ID"));
- columnNames.add(Msg.translate(Env.getCtx(), "C_Charge_ID"));
- columnNames.add(Msg.getElement(Env.getCtx(), "M_RequisitionLine_ID"));
- columnNames.add(Msg.getElement(Env.getCtx(), "kst_Division"));
- columnNames.add(Msg.getElement(Env.getCtx(), "C_Order_ID"));
- columnNames.add(Msg.getElement(Env.getCtx(), "kst_Description"));
- columnNames.add(Msg.getElement(Env.getCtx(), "DatePromised"));
- return columnNames;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement