package ibase.webitm.ejb.mfg;

import java.util.ArrayList;
//import ibase.webitm.utility.ITMException;
import ibase.webitm.utility.GenericUtility;
//import java.sql.Timestamp;
import java.text.SimpleDateFormat;

class ADPElement
{
	private String itemCode;
	private String siteCode;
	private String routeCode;
	private String suppSour;
	private String bomCode;
	private String itemSchedule;
	private String itemStru;
	private String siteCodeSupp;
	private String siteCodeCust;
	private String siteCodePlan;
	private String orderOpt;
	private String batchQtyType;
	private String masterSch;
	private String unit;
    private String itemDescr;
    private String aprCode;
    private String packCode;
    private String empCodePur;
    private String suppCodePref;
    private String acctCodePh;
    private String cctrCodePh;
    private String acctCodeWp;
    private String cctrCodeWp;
    private String itemSer;
    private String mfgType;
        
	private double minQty = 0;
	private double reoQty = 0;
	private double demand = 0;
	private double supply = 0;
	private double planDemand = 0;
	private double planSupply = 0;
	// 12/04/10 manoharan MF90BHU001
	private double stockQty = 0;
	private double othSupply = 0;
	// end 12/04/10 manoharan MF90BHU001
	private double minOrderQty = 0;
	private double integralQty = 0;
	private double maxQty = 0;
	private double minimumQty = 0;
	private double reorderQty = 0;
	private double yieldPerc = 100;
	private double minPlanPerc = 0;
	private double batchQty = 0;
    private double purcRate = 0;

	private int days = 0;
	
	private int purLeadTime = 0;
	private int qcLeadTime = 0;
	private int mfgLeadTime = 0;
	private int maxStkDay = 0;
	private int minStkDay = 0;
	
	private ArrayList timeMRPArr; 
	transient GenericUtility genericUtility = null;
	// 22/06/10 manoharan mfg lead time based on batch size
	private String mfgLeadBasis = "F";
	private double batchSizeLead = 0;
	// end 22/06/10 manoharan
	
	//sabyasachi 17-03-2011
	private String saleOrder;
	private String lineNo;
	//end sabyasachi 17-03-2011
	
	public ADPElement()
	{
		//System.out.println("New ADPElement Created");
		this.timeMRPArr = new ArrayList();
		this.saleOrder = "          ";
		this.lineNo = "   ";
	}

	public void setItemCode(String itemCode)
	{
		//itemCode = itemCode.trim() + "          ";
		//itemCode = itemCode.substring(0,9);	
		this.itemCode = itemCode;
	}
	public String getItemCode()
	{
		return this.itemCode;
	}
	public void setSiteCode(String siteCode)
	{
		//siteCode = siteCode.trim() + "     ";
		//siteCode = siteCode.substring(0,4);	
		this.siteCode = siteCode;
	}
	public String getSiteCode()
	{
		return this.siteCode;
	}
        public void setUnit(String uom)
	{
		this.unit = uom;
	}
	public String getUnit()
	{
		return this.unit;
	}	
        public void setItemDescr(String descr)
	{
		this.itemDescr = descr;
	}
	public String getItemDescr()
	{
		return this.itemDescr;
	}	
        public void setItemSer(String code)
	{
		this.itemSer = code;
	}
	public String getItemSer()
	{
		return this.itemSer;
	}	
        public void setAprCode(String code)
	{
		this.aprCode = code;
	}
	public String getAprCode()
	{
		return this.aprCode;
	}	
        public void setPackCode(String code)
	{
		this.packCode = code;
	}
	public String getPackCode()
	{
		return this.packCode;
	}	
        public void setEmpCodePur(String code)
	{
		this.empCodePur = code;
	}
	public String getEmpCodePur()
	{
		return this.empCodePur;
	}	
    public void setSuppCodePref(String code)
	{
		this.suppCodePref = code;
	}
	public String getSuppCodePref()
	{
		return this.suppCodePref;
	}	
        public void setAcctCodePh(String code)
	{
		this.acctCodePh = code;
	}
	public String getAcctCodePh()
	{
		return this.acctCodePh;
	}	
        public void setCctrCodePh(String code)
	{
		this.cctrCodePh = code;
	}
	public String getCctrCodePh()
	{
		return this.cctrCodePh;
	}	
        public void setAcctCodeWp(String code)
	{
		this.acctCodeWp = code;
	}
	public String getAcctCodeWp()
	{
		return this.acctCodeWp;
	}	
        public void setCctrCodeWp(String code)
	{
		this.cctrCodeWp = code;
	}
	public String getCctrCodeWp()
	{
		return this.cctrCodeWp;
	}	
        public void setMfgType(String code)
	{
		this.mfgType = code;
	}
	public String getMfgType()
	{
		return this.mfgType;
	}	
        public void setPurcRate(double rate)
	{
		this.purcRate = rate;
	}
	public double getPurcRate()
	{
		return this.purcRate;
	}	
    //changed method arguments by sabyasachi 29-03-2011
	public void setDemand(java.sql.Timestamp dueDate, double demand, String saleOrder, String lineNo)
	//public void setDemand(java.sql.Timestamp dueDate, double demand)
	{
		this.demand += demand;
		this.saleOrder = saleOrder;
		this.lineNo = lineNo;
		//updateTimeMrp(dueDate, demand, 0, 0, 0,saleOrder,lineNo);
		updateTimeMrp(dueDate, demand, 0, 0, 0);
	}
	//changed method arguments by sabyasachi 29-03-2011 end
	public double getDemand()
	{
		return this.demand;
	}
	public void setSupply(java.sql.Timestamp dueDate, double supply)
	{
		this.supply += supply;
		//updateTimeMrp(dueDate, 0, supply, 0, 0,"          ","   ");
		updateTimeMrp(dueDate, 0, supply, 0, 0);
	}
	public double getSupply()
	{
		return this.supply;
	}
	// 12/04/10 manoharan MF90BHU001
	public void setStockQty(java.sql.Timestamp dueDate, double stockQty)
	{
		this.stockQty += stockQty;
		//updateTimeMrp(dueDate, 0,0, stockQty,0,"          ","   ");
		updateTimeMrp(dueDate, 0,0, stockQty,0);
	}
	public double getStockQty()
	{
		return this.stockQty;
	}
	public void setOthSupply(java.sql.Timestamp dueDate, double othSupply)
	{
		this.othSupply += othSupply;
		//updateTimeMrp(dueDate, 0,0, 0, othSupply,"          ","   ");
		updateTimeMrp(dueDate, 0,0, 0, othSupply);
	}
	public double getOthSupply()
	{
		return this.othSupply;
	}
	// end 12/04/10 manoharan MF90BHU001
	
	public void setPlanSupply(java.sql.Timestamp dueDate, double supply)
	{
		this.planSupply += supply;
		//updateTimeMrp(dueDate, 0, supply, 0, 0,"          ","   ");
		updateTimeMrp(dueDate, 0, supply, 0, 0);
	}
	public double getPlanSupply()
	{
		return this.planSupply;
	}
	//added by dharmendra on 28th-dec	
	
	public void setSuppSour(String suppSour)
	{
		this.suppSour = suppSour;
	}
	public String getSuppSour()
	{
		return this.suppSour;
	}
	public void setBomCode(String bomCode)
	{
		this.bomCode = bomCode;
	}
	public String getBomCode()
	{
		return this.bomCode;
	}
	public void setRouteCode(String routeCode)
	{
		this.routeCode = routeCode;
	}
	public String getRouteCode()
	{
		return this.routeCode;
	}

	public void setMinQty(double minQty)
	{
		this.minQty = minQty;
	}
	public double getMinQty()
	{
		return this.minQty;
	}
	public void setMinStkDay(int minStkDay)
	{
		this.minStkDay = minStkDay;
	}
	public int getMinStkDay()
	{
		return this.minStkDay;
	}
	public void setReoQty(double reoQty)
	{
		this.reoQty = reoQty;
	}
	public double getReoQty()
	{
		return this.reoQty;
	}
	public void setPurLeadTime(int purLeadTime)
	{
		this.purLeadTime = purLeadTime;
	}
	public int getPurLeadTime()
	{
		return this.purLeadTime;
	}
	public void setQcLeadTime(int qcLeadTime)
	{
		this.qcLeadTime = qcLeadTime;
	}
	public int getQcLeadTime()
	{
		return this.qcLeadTime;
	}
	public void setIntegralQty(double integralQty)
	{
		this.integralQty = integralQty;
	}
	public double getIntegralQty()
	{
		return this.integralQty;
	}
	public void setItemStru(String itemStru)
	{
		this.itemStru = itemStru;
	}
	public String getItemStru()
	{
		return this.itemStru;
	}
	public void setSiteCodeSupp(String siteCodeSupp)
	{
		this.siteCodeSupp = siteCodeSupp;
	}
	public String getSiteCodeSupp()
	{
		return this.siteCodeSupp;
	}
	public void setSiteCodePlan(String siteCodePlan)
	{
		this.siteCodePlan = siteCodePlan;
	}
	public String getSiteCodePlan()
	{
		return this.siteCodePlan;
	}
	public void setSiteCodeCust(String siteCode)
	{
		this.siteCodeCust = siteCode;
	}
	public String getSiteCodeCust()
	{
		return this.siteCodeCust;
	}
	public void setBatchQtyType(String batchQtyType)
	{
		this.batchQtyType = batchQtyType;
	}
	public String getBatchQtyType()
	{
		return this.batchQtyType;
	}
	public void setMinOrderQty(double minOrderQty)
	{
		this.minOrderQty = minOrderQty;
	}
	public double getMinOrderQty()
	{
		return this.minOrderQty;
	}
	public void setMaxStkDay(int maxStkDay)
	{
		this.maxStkDay = maxStkDay;
	}
	public int getMaxStkDay()
	{
		return this.maxStkDay;
	}
	public void setMaxQty(double maxQty)
	{
		this.maxQty = maxQty;
	}
	public double getMaxQty()
	{
		return this.maxQty;
	}
	public void setOrderOpt(String orderOpt)
	{
		this.orderOpt = orderOpt;
	}
	public String getOrderOpt()
	{
		return this.orderOpt;
	}
	public void setYieldPerc(double yieldPerc)
	{
		this.yieldPerc = yieldPerc;
	}
	public double getYieldPerc()
	{
		return this.yieldPerc;
	}
	public void setMasterSch(String masterSch)
	{
		this.masterSch = masterSch;
	}
	public String getMasterSch()
	{
		return this.masterSch;
	}
	public void setMfgLeadTime(int mfgLeadTime)
	{
		this.mfgLeadTime = mfgLeadTime;
	}
	public int getMfgLeadTime()
	{
		return this.mfgLeadTime;
	}
	public void setMinPlanPerc(double minPlanPerc)
	{
		this.minPlanPerc = minPlanPerc;
	}
	public double getMinPlanPerc()
	{
		return this.minPlanPerc;
	}
	public void setBatchQty(double qty)
	{
		this.batchQty = qty;
	}
	public double getBatchQty()
	{
		return this.batchQty;
	}
	public String toString()
	{
		return siteCode+":"+itemCode;
	}
	public ArrayList getTimeMrpList()
	{
		return this.timeMRPArr;
	}
	
	public void setIndependentDemand(java.sql.Timestamp dueDate, double demand)
	{
		//this.independentDemand += demand;
		updateIndependentDemand(dueDate, demand);
	}
	/*public double getIndependentDemand()
	{
		return this.independentDemand;
	}*/
	// 22/06/10 manoharan mfg lead time based on batch size
	public void setMfgLeadBasis(String mfgLeadBasis)
	{
		this.mfgLeadBasis = mfgLeadBasis;
	}
	public String getMfgLeadBasis()
	{
		return this.mfgLeadBasis;
	}
	public void setBatchSizeLead(double batchSizeLead)
	{
		this.batchSizeLead = batchSizeLead;
	}
	public double getBatchSizeLead()
	{
		return this.batchSizeLead;
	}
	// end 22/06/10 manoharan
	//added by sabyasachi 17-03-11
	public void setSaleOrder(String saleOrder)
	{
		this.saleOrder = saleOrder;
	}
	public void setLineNo(String lineNo)
	{
		this.lineNo = lineNo;
	}
	public String getSaleOrder()
	{
		return this.saleOrder;
	}
	public String getLineNo()
	{
		return this.lineNo;
	}
	//end added by sabyasachi 17-03-11
	

	//private void updateTimeMrp(java.sql.Timestamp dueDate, double demand, double supply, double stockQty, double othSupply, String saleOrder, String lineNo)
	private void updateTimeMrp(java.sql.Timestamp dueDate, double demand, double supply, double stockQty, double othSupply)
	{
		String errString = "";
		TimeMRP timeMRP = null;
		genericUtility = GenericUtility.getInstance();	
		int timeEle = -1, ctr;
		SimpleDateFormat sdt=new SimpleDateFormat("yyyy-MM-dd");
		dueDate = java.sql.Timestamp.valueOf(sdt.format(dueDate) + " 00:00:00.000");
		
		try
		{
			if (timeMRPArr.size() > 0)
			{
				for (ctr = 0; ctr < timeMRPArr.size(); ctr++)
				{
					timeMRP = (TimeMRP)timeMRPArr.get(ctr);
					//if (timeMRP.getDueDate() != null && timeMRP.getSaleOrder().equals(saleOrder) && timeMRP.getLineNo().equals(lineNo))
					if (timeMRP.getDueDate() != null)
					{
						if (timeMRP.getDueDate().equals(dueDate))
						{
							timeEle = ctr;
							break;	
						}
					}					
				}
			}
			if (timeEle == -1)
			{
				timeMRP = new TimeMRP();
				timeMRP.setDueDate(dueDate);
				//added by sabyasachi 29.03.2011
				//timeMRP.setDemand(demand,saleOrder,lineNo);
				timeMRP.setDemand(demand);
				//end added by sabyasachi 29.03.2011
				timeMRP.setSupply(supply);
				// 12/04/10 manoharan MF90BHU001
				timeMRP.setStockQty(stockQty);
				timeMRP.setOthSupply(othSupply);				
				// end 12/04/10 manoharan MF90BHU001	
				this.timeMRPArr.add(timeMRP);
			}
			else
			{
				timeMRP = (TimeMRP)this.timeMRPArr.get(timeEle);
				//added by sabyasachi 29.03.2011
				//timeMRP.setDemand(demand,saleOrder,lineNo);
				timeMRP.setDemand(demand);
				//end added by sabyasachi 29.03.2011	
				timeMRP.setSupply(supply);
				// 12/04/10 manoharan MF90BHU001
				timeMRP.setStockQty(stockQty);
				timeMRP.setOthSupply(othSupply);
				// end 12/04/10 manoharan MF90BHU001								
				//timeMRP.setSaleOrder(saleOrder);
				//timeMRP.setLineNo(lineNo);							
				this.timeMRPArr.set(timeEle,timeMRP);
			}
			updateRunningDemandSupply(dueDate);			
		}
		catch(Exception e)
		{
			System.out.println("Exception :adpeLEMENT : updateTimeMrp(Date, supply, demand):...." + e.getMessage() + ":");
			errString = genericUtility.createErrorString(e);
			e.printStackTrace();
			errString = e.getMessage();
		}		
	}
	private void updateRunningDemandSupply(java.sql.Timestamp dueDate)
	{
		int index = 0;
		double runningDemand = 0;
		double runningSupply = 0;
		double runningPlanSupply = 0;
		double planSupply = 0;
		String errString = null;
		TimeMRP timeMRP = null;
		try
		{
			for (int ctr = 0; ctr < timeMRPArr.size(); ctr++)
			{
				timeMRP = (TimeMRP)timeMRPArr.get(ctr);
				if (timeMRP.getDueDate().equals(dueDate))
				{		 
					index = ctr;
					break;
				}
				runningDemand = runningDemand + timeMRP.getDemand();
				runningSupply = runningSupply + timeMRP.getSupply();							
									// 10-04-2006 manoharan
				runningPlanSupply = runningPlanSupply + timeMRP.getPlanSupply();							
									// end 10-04-2006 manoharan
			}
			
			for (int ctr = index; ctr < timeMRPArr.size(); ctr++)
			{
				timeMRP = (TimeMRP)timeMRPArr.get(ctr);
				runningDemand = runningDemand + timeMRP.getDemand();
				//runningSupply = runningSupply + timeMRP.getSupply();			
                runningSupply = runningSupply + timeMRP.getSupply();
				timeMRP.setRunningDemand(runningDemand);
				timeMRP.setRunningSupply(runningSupply);
                // 10-04-2006 manoharan
                planSupply = timeMRP.getPlanSupply();
                runningPlanSupply = runningPlanSupply + planSupply;
				timeMRP.setRunningPlanSupply(runningPlanSupply);
                // end 10-04-2006 manoharan
				timeMRPArr.set(ctr, timeMRP);
			}
		}
		catch(Exception e)
		{
			System.out.println("Exception :RunMRPPrcEJB : updateRunningDemandSupply(Timestamp Date):...." + e.getMessage() + ":");
			errString = genericUtility.createErrorString(e);
			e.printStackTrace();
			errString = e.getMessage();
		}
	}
	private void updateIndependentDemand(java.sql.Timestamp dueDate, double demand)
	{
		String errString = "";
		TimeMRP timeMRP = null;
		genericUtility = GenericUtility.getInstance();	
		int timeEle = -1, ctr;
		SimpleDateFormat sdt=new SimpleDateFormat("yyyy-MM-dd");
		dueDate = java.sql.Timestamp.valueOf(sdt.format(dueDate) + " 00:00:00.000");
		try
		{
			if (timeMRPArr.size() > 0)
			{
				for (ctr = 0; ctr < timeMRPArr.size(); ctr++)
				{
					timeMRP = (TimeMRP)timeMRPArr.get(ctr);
					if (timeMRP.getDueDate() != null)
					{
						if (timeMRP.getDueDate().equals(dueDate))
						{
							timeEle = ctr;
							break;	
						}
					}
				}
			}
			if (timeEle == -1)
			{
				timeMRP = new TimeMRP();
				timeMRP.setDueDate(dueDate);
				timeMRP.setIndependentDemand(demand);
				this.timeMRPArr.add(timeMRP);
			}
			else
			{
				timeMRP = (TimeMRP)this.timeMRPArr.get(timeEle);
				timeMRP.setIndependentDemand(demand);
				this.timeMRPArr.set(timeEle,timeMRP);
			}
		}
		catch(Exception e)
		{
			System.out.println("Exception :adpeLEMENT : updateTimeMrp(Date, supply, demand):...." + e.getMessage() + ":");
			errString = genericUtility.createErrorString(e);
			e.printStackTrace();
			errString = e.getMessage();
		}		
	}
	
	
}