/******************************************************************************
* Product: Adempiere ERP & CRM Smart Business Solution *
* Copyright (C) 1999-2006 ComPiere, Inc. All Rights Reserved. *
* 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 *
* ComPiere, Inc., 2620 Augustine Dr. #245, Santa Clara, CA 95054, USA *
* or via info@compiere.org or http://www.compiere.org/license.html *
*****************************************************************************/
package org.compiere.process;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.logging.Level;
import org.adempiere.exceptions.NoVendorForProductException;
import org.apache.commons.collections.keyvalue.MultiKey;
import org.compiere.model.MBPartner;
import org.compiere.model.MCharge;
import org.compiere.model.MConversionType;
import org.compiere.model.MOrder;
import org.compiere.model.MOrderLine;
import org.compiere.model.MProcessPara;
import org.compiere.model.MProduct;
import org.compiere.model.MProductPO;
import org.compiere.model.MRequisition;
import org.compiere.model.MRequisitionLine;
import org.compiere.model.POResultSet;
import org.compiere.model.Query;
import org.compiere.util.AdempiereUserError;
import org.compiere.util.DB;
import org.compiere.util.Msg;
/**
* Create PO from Requisition
*
*
* @author Jorg Janke
* @version $Id: RequisitionPOCreate.java,v 1.2 2006/07/30 00:51:01 jjanke Exp $
*
* @author Teo Sarca, www.arhipac.ro
*
BF [ 2609760 ] RequisitionPOCreate not using DateRequired
*
BF [ 2605888 ] CreatePOfromRequisition creates more PO than needed
*
BF [ 2811718 ] Create PO from Requisition without any parameter terminate in NPE
* https://sourceforge.net/p/adempiere/bugs/1954/
*
FR [ 2844074 ] Requisition PO Create - more selection fields
* https://sourceforge.net/p/adempiere/feature-requests/796/
*/
@org.adempiere.base.annotation.Process
public class RequisitionPOCreate extends SvrProcess
{
/** Org */
private int p_AD_Org_ID = 0;
/** Warehouse */
private int p_M_Warehouse_ID = 0;
/** Doc Date From */
private Timestamp p_DateDoc_From;
/** Doc Date To */
private Timestamp p_DateDoc_To;
/** Doc Date From */
private Timestamp p_DateRequired_From;
/** Doc Date To */
private Timestamp p_DateRequired_To;
/** Priority */
private String p_PriorityRule = null;
/** User */
private int p_AD_User_ID = 0;
/** Product */
private int p_M_Product_ID = 0;
/** Product Category */
private int p_M_Product_Category_ID = 0;
/** BPartner Group */
private int p_C_BP_Group_ID = 0;
/** Requisition */
private int p_M_Requisition_ID = 0;
/** Consolidate */
private boolean p_ConsolidateDocument = false;
/** Order */
private MOrder m_order = null;
/** Order Line */
private MOrderLine m_orderLine = null;
/** Orders Cache : (C_BPartner_ID, DateRequired, M_PriceList_ID) -> MOrder */
private HashMap m_cacheOrders = new HashMap();
/**
* Prepare - e.g., get Parameters.
*/
protected void prepare()
{
ProcessInfoParameter[] para = getParameter();
for (int i = 0; i < para.length; i++)
{
String name = para[i].getParameterName();
if (para[i].getParameter() == null && para[i].getParameter_To() == null)
;
else if (name.equals("AD_Org_ID"))
p_AD_Org_ID = para[i].getParameterAsInt();
else if (name.equals("M_Warehouse_ID"))
p_M_Warehouse_ID = para[i].getParameterAsInt();
else if (name.equals("DateDoc"))
{
p_DateDoc_From = (Timestamp)para[i].getParameter();
p_DateDoc_To = (Timestamp)para[i].getParameter_To();
}
else if (name.equals("DateRequired"))
{
p_DateRequired_From = (Timestamp)para[i].getParameter();
p_DateRequired_To = (Timestamp)para[i].getParameter_To();
}
else if (name.equals("PriorityRule"))
p_PriorityRule = (String)para[i].getParameter();
else if (name.equals("AD_User_ID"))
p_AD_User_ID = para[i].getParameterAsInt();
else if (name.equals("M_Product_ID"))
p_M_Product_ID = para[i].getParameterAsInt();
else if (name.equals("M_Product_Category_ID"))
p_M_Product_Category_ID = para[i].getParameterAsInt();
else if (name.equals("C_BP_Group_ID"))
p_C_BP_Group_ID = para[i].getParameterAsInt();
else if (name.equals("M_Requisition_ID"))
p_M_Requisition_ID = para[i].getParameterAsInt();
else if (name.equals("ConsolidateDocument"))
p_ConsolidateDocument = "Y".equals(para[i].getParameter());
else
MProcessPara.validateUnknownParameter(getProcessInfo().getAD_Process_ID(), para[i]);
}
} // prepare
/**
* Process
* @return info
* @throws Exception
*/
protected String doIt() throws Exception
{
// Specific
if (p_M_Requisition_ID != 0)
{
if (log.isLoggable(Level.INFO)) log.info("M_Requisition_ID=" + p_M_Requisition_ID);
MRequisition req = new MRequisition(getCtx(), p_M_Requisition_ID, get_TrxName());
if (!MRequisition.DOCSTATUS_Completed.equals(req.getDocStatus()))
{
throw new AdempiereUserError("@DocStatus@ = " + req.getDocStatus());
}
MRequisitionLine[] lines = req.getLines();
for (int i = 0; i < lines.length; i++)
{
if (lines[i].getC_OrderLine_ID() == 0)
{
process (lines[i]);
}
}
closeOrder();
return "";
} // single Requisition
//
if (log.isLoggable(Level.INFO)) log.info("AD_Org_ID=" + p_AD_Org_ID
+ ", M_Warehouse_ID=" + p_M_Warehouse_ID
+ ", DateDoc=" + p_DateDoc_From + "/" + p_DateDoc_To
+ ", DateRequired=" + p_DateRequired_From + "/" + p_DateRequired_To
+ ", PriorityRule=" + p_PriorityRule
+ ", AD_User_ID=" + p_AD_User_ID
+ ", M_Product_ID=" + p_M_Product_ID
+ ", ConsolidateDocument" + p_ConsolidateDocument);
ArrayList