Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/******************************************************************************
* Product: Adempiere ERP & CRM Smart Business Solution *
* 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. *
* For the text or an alternative of this public license, you may reach us *
* Copyright (C) 2003-2013 E.R.P. Consultores y Asociados, C.A. *
* All Rights Reserved. *
* Contributor(s): Yamel Senih www.erpya.com *
*****************************************************************************/
package org.spin.form;
import java.math.BigDecimal;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.text.DecimalFormat;
import java.util.Arrays;
import java.util.Optional;
import java.util.Vector;
import java.util.logging.Level;
import org.adempiere.exceptions.AdempiereException;
import org.adempiere.exceptions.DocTypeNotFoundException;
import org.compiere.minigrid.IMiniTable;
import org.compiere.model.MDocType;
import org.compiere.model.MLocator;
import org.compiere.model.MOrderLine;
import org.compiere.model.MProduct;
import org.compiere.model.MRefList;
import org.compiere.model.MRole;
import org.compiere.model.MStorage;
import org.compiere.model.MUOM;
import org.compiere.model.MWarehouse;
import org.compiere.model.X_C_Order;
import org.compiere.util.CLogger;
import org.compiere.util.DB;
import org.compiere.util.DisplayType;
import org.compiere.util.Env;
import org.compiere.util.KeyNamePair;
import org.compiere.util.Msg;
import org.compiere.util.Util;
import org.compiere.util.ValueNamePair;
import org.eevolution.model.I_DD_Order;
import org.eevolution.model.MDDOrderLine;
import org.eevolution.model.MWMInOutBound;
import org.eevolution.model.MWMInOutBoundLine;
/**
* @author Yamel Senih 2011-06-24, 12:57
* @author Carlos Parada, [email protected]
*
FR [ 1 ] Add Support to generate load order with non stocked product
* @see https://github.com/erpcya/FTA/issues/1
*
*/
public class OutBoundOrder {
/** Logger */
public static CLogger log = CLogger.getCLogger(OutBoundOrder.class);
public final int SELECT = 0;
public final int ORDER = 2;
/** Lines */
public final int OL_WAREHOUSE = 1;
public final int ORDER_LINE = 2;
public final int OL_PRODUCT = 3;
public final int OL_UOM = 4;
public final int OL_QTY_ON_HAND = 5;
public final int OL_QTY = 6;
public final int OL_WEIGHT = 7;
public final int OL_VOLUME = 8;
public final int OL_SEQNO = 9;
public final int OL_QTY_ORDERED = 10;
public final int OL_UOM_CONVERSION = 11;
public final int OL_QTY_RESERVERD = 12;
public final int OL_QTY_INVOICED = 13;
public final int OL_QTY_DELIVERED = 14;
public final int OL_QTY_IN_TRANSIT = 15;
public final int OL_DELIVERY_RULE = 16;
/** Warehouse and Product */
public final int SW_PRODUCT = 0;
public final int SW_UOM = 1;
public final int SW_WAREHOUSE = 2;
public final int SW_QTY_ON_HAND = 3;
public final int SW_QTY_IN_TRANSIT = 4;
public final int SW_QTY_SET = 5;
public final int SW_QTY_AVAILABLE = 6;
/** Buffer */
public Vector m_BufferSelect = null;
public StringBuffer m_Symmary = new StringBuffer();
public StringBuffer m_QueryAdd = new StringBuffer();
/** Client */
protected int clientId = 0;
/** Organization */
protected int orgId = 0;
/** Warehouse */
protected int salesRegionId = 0;
/** Sales Rep */
protected int salesRepId = 0;
/** Warehouse */
protected int warehouseId = 0;
/** Operation Type */
protected String movementType = null;
/** Document Action */
protected String documentAction = null;
/** Document Type */
protected int docTypeId = 0;
/** Document Type Target*/
protected int docTypeTargetId = 0;
/** Delivery Rule */
protected String deliveryRule = null;
/** Delivery Via Rule */
protected String deliveryViaRule = null;
/** Document Date */
protected Timestamp documentDate = null;
/** Shipment Date */
protected Timestamp shipmentDate = null;
/** Shipper */
protected int shipperId = 0;
/** Load Capacity */
protected BigDecimal payloadCapacity = Env.ZERO;
/** Volume Capacity */
protected BigDecimal volumeCapacity = Env.ZERO;
/** Weight Unit Measure */
protected int uOMWeightId = 0;
/** Volume Unit Measure */
protected int uOMVolumeId = 0;
/** Weight Precision */
protected int weightPrecision = 0;
/** Volume Precision */
protected int volumePrecision = 0;
/** Rows Selected */
protected int rowsSelected = 0;
/** UOM Weight Symbol */
protected String uOMWeightSymbol = null;
/** UOM Volume Symbol */
protected String uOMVolumeSymbol = null;
/** Total Weight */
protected BigDecimal totalWeight = Env.ZERO;
/** Total Volume */
protected BigDecimal totalVolume = Env.ZERO;
/** Max Sequence */
protected int maxSeqNo = 0;
/** Load Order */
protected MWMInOutBound outBoundOrder = null;
/** Outbound Locator Id */
protected Integer locatorId = null;
/**
* Get Order data from parameters
* @return Vector>
*/
protected Vector> getOrderData(IMiniTable orderTable) {
// Load Validation Flag
ResultSet rs = null;
PreparedStatement pstmt = null;
//
Vector> data = new Vector>();
StringBuffer sql = null;
if (movementType.equals(I_DD_Order.Table_Name)) {
//Query for Material Movement
sql = new StringBuffer("SELECT " +
"wr.Name Warehouse, ord.DD_Order_ID, ord.DocumentNo, " + // 1..3
"ord.DateOrdered, ord.DatePromised, reg.Name, cit.Name, sr.Name SalesRep, " + // 4..8
"cp.Name Partner, bploc.Name, " + // 9..10
"loc.Address1, loc.Address2, loc.Address3, loc.Address4, ord.C_BPartner_Location_ID, ord.Weight, ord.Volume " + // 11..17
"FROM DD_Order ord " +
"INNER JOIN DD_OrderLine lord ON(lord.DD_Order_ID = ord.DD_Order_ID) " +
"INNER JOIN M_Product pr ON(pr.M_Product_ID = lord.M_Product_ID) " +
"INNER JOIN C_BPartner cp ON(cp.C_BPartner_ID = ord.C_BPartner_ID) " +
"INNER JOIN AD_User sr ON(sr.AD_User_ID = ord.SalesRep_ID) " +
"INNER JOIN M_Locator wloc ON(wloc.M_Locator_ID = lord.M_Locator_ID) " +
"INNER JOIN M_Warehouse wr ON(wr.M_Warehouse_ID = wloc.M_Warehouse_ID) " +
"INNER JOIN C_BPartner_Location bploc ON(bploc.C_BPartner_Location_ID = ord.C_BPartner_Location_ID) " +
"INNER JOIN C_Location loc ON(loc.C_Location_ID = bploc.C_Location_ID) " +
"LEFT JOIN C_Region reg ON(reg.C_Region_ID = loc.C_Region_ID) " +
"LEFT JOIN C_City cit ON(cit.C_City_ID = loc.C_City_ID) " +
"LEFT JOIN (SELECT lord.DD_OrderLine_ID, " +
" (COALESCE(lord.QtyOrdered, 0) - " +
" SUM(" +
" CASE WHEN (c.IsDelivered = 'N' AND lc.DD_Order_ID IS NOT NULL AND c.DocStatus = 'CO') " +
" THEN COALESCE(lc.MovementQty, 0) " +
" ELSE 0 " +
" END" +
" )" +
" ) QtyAvailable " +
" FROM DD_OrderLine lord " +
" LEFT JOIN WM_InOutBoundLine lc ON(lc.DD_OrderLine_ID = lord.DD_OrderLine_ID) " +
" LEFT JOIN WM_InOutBound c ON(c.WM_InOutBound_ID = lc.WM_InOutBound_ID) " +
" WHERE lord.M_Product_ID IS NOT NULL " +
" GROUP BY lord.DD_Order_ID, lord.DD_OrderLine_ID, lord.QtyOrdered " +
" ORDER BY lord.DD_OrderLine_ID ASC) qafl " +
" ON(qafl.DD_OrderLine_ID = lord.DD_OrderLine_ID) " +
"WHERE wr.IsActive = 'Y' " +
"AND ord.DocStatus = 'CO' " +
"AND COALESCE(qafl.QtyAvailable, 0) > 0 " +
"AND ord.AD_Client_ID=? ");
if (orgId > 0)
sql.append("AND lord.AD_Org_ID=? ");
if (warehouseId > 0)
sql.append("AND wr.M_Warehouse_ID=? ");
if (salesRegionId > 0)
sql.append("AND bploc.C_SalesRegion_ID=? ");
if (salesRepId > 0)
sql.append("AND ord.SalesRep_ID=? ");
if (docTypeId > 0)
sql.append("AND ord.C_DocType_ID=? ");
// Group By
sql.append("GROUP BY wr.Name, ord.DD_Order_ID, ord.DocumentNo, ord.DateOrdered, " +
"ord.DatePromised, ord.Weight, ord.Volume, sr.Name, cp.Name, bploc.Name, " +
"reg.Name, cit.Name, loc.Address1, loc.Address2, loc.Address3, loc.Address4, ord.C_BPartner_Location_ID ");
// Having
sql.append("HAVING (SUM(COALESCE(lord.QtyOrdered, 0)) - SUM(COALESCE(lord.QtyInTransit, 0)) - SUM(COALESCE(lord.QtyDelivered, 0))) > 0 ");
// Order By
sql.append("ORDER BY ord.DD_Order_ID ASC");
// role security
} else {//Query for Sales Order
sql = new StringBuffer("SELECT " +
"wr.Name Warehouse, ord.C_Order_ID, ord.DocumentNo, " + // 1..3
"ord.DateOrdered, ord.DatePromised, reg.Name, cit.Name, sr.Name SalesRep, " + // 4..8
"cp.Name Partner, bploc.Name, " + // 9..10
"loc.Address1, loc.Address2, loc.Address3, loc.Address4, ord.C_BPartner_Location_ID, ord.Weight, ord.Volume " + // 11..17
"FROM C_Order ord " +
"INNER JOIN C_OrderLine lord ON(lord.C_Order_ID = ord.C_Order_ID) " +
"INNER JOIN M_Product pr ON(pr.M_Product_ID = lord.M_Product_ID) " +
"INNER JOIN C_BPartner cp ON(cp.C_BPartner_ID = ord.C_BPartner_ID) " +
"INNER JOIN M_Warehouse wr ON(wr.M_Warehouse_ID = ord.M_Warehouse_ID) " +
"INNER JOIN C_BPartner_Location bploc ON(bploc.C_BPartner_Location_ID = ord.C_BPartner_Location_ID) " +
"INNER JOIN C_Location loc ON(loc.C_Location_ID = bploc.C_Location_ID) " +
"LEFT JOIN AD_User sr ON(sr.AD_User_ID = ord.SalesRep_ID) " +
"LEFT JOIN C_Region reg ON(reg.C_Region_ID = loc.C_Region_ID) " +
"LEFT JOIN C_City cit ON(cit.C_City_ID = loc.C_City_ID) " +
"LEFT JOIN (SELECT lord.C_OrderLine_ID, " +
" (COALESCE(lord.QtyOrdered, 0) - " +
" SUM(" +
" CASE WHEN (c.IsDelivered = 'N' AND lc.C_Order_ID IS NOT NULL AND c.DocStatus = 'CO') " +
" THEN COALESCE(lc.MovementQty, 0) " +
" ELSE 0 " +
" END" +
" )" +
" ) QtyAvailable " +
" FROM C_OrderLine lord " +
" LEFT JOIN WM_InOutBoundLine lc ON(lc.C_OrderLine_ID = lord.C_OrderLine_ID) " +
" LEFT JOIN WM_InOutBound c ON(c.WM_InOutBound_ID = lc.WM_InOutBound_ID) " +
" WHERE lord.M_Product_ID IS NOT NULL " +
" GROUP BY lord.C_Order_ID, lord.C_OrderLine_ID, lord.QtyOrdered " +
" ORDER BY lord.C_OrderLine_ID ASC) qafl " +
" ON(qafl.C_OrderLine_ID = lord.C_OrderLine_ID) " +
"WHERE ord.IsSOTrx = 'Y' " +
"AND wr.IsActive = 'Y' " +
"AND ord.DocStatus = 'CO' " +
"AND COALESCE(qafl.QtyAvailable, 0) > 0 " +
"AND ord.AD_Client_ID=? ");
if (orgId > 0)
sql.append("AND lord.AD_Org_ID=? ");
if (warehouseId > 0)
sql.append("AND lord.M_Warehouse_ID=? ");
if (salesRegionId > 0)
sql.append("AND bploc.C_SalesRegion_ID=? ");
if (salesRepId > 0)
sql.append("AND ord.SalesRep_ID=? ");
if (docTypeId > 0 )
sql.append("AND ord.C_DocType_ID=? ");
// Group By
sql.append("GROUP BY wr.Name, ord.C_Order_ID, ord.DocumentNo, ord.DateOrdered, " +
"ord.DatePromised, ord.Weight, ord.Volume, sr.Name, cp.Name, bploc.Name, " +
"reg.Name, cit.Name, loc.Address1, loc.Address2, loc.Address3, loc.Address4, ord.C_BPartner_Location_ID ");
// Having
sql.append("HAVING (SUM(COALESCE(lord.QtyOrdered, 0)) - SUM(COALESCE(lord.QtyDelivered, 0))) > 0 ");
// Order By
sql.append("ORDER BY ord.C_Order_ID ASC");
// role security
}
//
log.fine("LoadOrderSQL=" + sql.toString());
//
try {
//
int param = 1;
int column = 1;
pstmt = DB.prepareStatement(sql.toString(), null);
pstmt.setInt(param++, Env.getAD_Client_ID(Env.getCtx()));
if (orgId > 0)
pstmt.setInt(param++, orgId);
if (warehouseId > 0)
pstmt.setInt(param++, warehouseId);
if (salesRegionId > 0)
pstmt.setInt(param++, salesRegionId);
if (salesRepId > 0)
pstmt.setInt(param++, salesRepId);
if (docTypeId > 0)
pstmt.setInt(param++, docTypeId);
log.fine("AD_Org_ID=" + orgId);
log.fine("M_Warehouse_ID=" + warehouseId);
log.fine("SalesRep_ID=" + salesRepId);
log.fine("C_DocType_ID=" + docTypeId);
rs = pstmt.executeQuery();
while (rs.next()) {
column = 1;
Vector