package ibase.webitm.ejb.mfg;

import java.util.ArrayList;
//import ibase.webitm.utility.ITMException;
//import ibase.webitm.utility.GenericUtility;
import java.sql.*;

public class InsertPlanTable
{
	PreparedStatement pstmtPlanHdr = null;
	PreparedStatement pstmtPlanDet = null;
	String resultString = "";		
	
	String siteCode = "";
	String siteCodeSupp = "";
	String itemCode = "";
	String itemSer = "";
	String itemStru = "";
	String masterSch = "";
	String bomCode = "";
	String suppSour = "";
	String suppCodePref = "";
	String orderOpt = "";
	String dueDateStr = "";
	String batchQtyType = "";
	String empCodePur = "";
	String packCode = "";
	String unit = "";
	String acctCodePh = "";
	String acctCodeWp = "";
	String cctrCodePh = "";
	String aprCode = "";
	String cctrCodeWp = "";
	
	double demand = 0;
	double supply = 0;
	double planDemand = 0;
	double planSupply = 0;
	double runningDemand = 0;
	double runningSupply = 0;
	double runningPlanSupply = 0;
	double independentDemand = 0;
	double minOrderQty = 0;
	double maxOrderQty = 0;
	double integralQty = 0;
	double maxQty = 0;
	double maxStkQty = 0;
	double minQty = 0;
	double reorderQty = 0;
	double yieldPerc = 100;
	double minPlanPerc = 0;
	double batchQty = 0;
	double purcRate = 0;
	
	int purLeadTime = 0;
	int qcLeadTime = 0;
	int mfgLeadTime = 0;
	int minStkDay = 0;
	int maxStkDay = 0;
	
	boolean flag = true;
	
	java.sql.Timestamp dueDate = null;
	
	ADPElement adpElement = null;
	
	public String InsertPlanHdrDet(ArrayList adpeList, Connection conn)
	{
		try
		{
			String sqlPlanHdr = "INSERT INTO PLAN_HDR(SITE_CODE, ITEM_CODE, ITEM_SER, SITE_CODE__SUPP, "
						 	  + "SUPP_SOUR, BOM_CODE, MIN_QTY, MIN_ORDER_QTY, MIN_STK_DAY, MAX_QTY, "
						 	  + "MAX_STK_DAY, ITEM_STRU, REO_QTY, PUR_LEAD_TIME, QC_LEAD_TIME, "
						 	  + "MFG_LEAD_TIME, INTEGRAL_QTY, BATCH_QTY, BATCH_QTY_TYPE, MASTER_SCH, ORDER_OPT, "
						 	  + "YIELD_PERC, MIN_PLAN_PERC, UNIT, APR_CODE, PACK_CODE, EMP_CODE__PUR, "
						 	  + "SUPP_CODE__PREF, PURC_RATE, ACCT_CODE__PH, CCTR_CODE__PH, ACCT_CODE__WP, CCTR_CODE__WP) "
						 	  + "VALUES( ? , ? , ? , ? , ? , ? ,"
					  		  + " ? , ? , ? , ? , ? , ? ,"
					  		  + " ? , ? , ? , ? , ? , ? ,"
					  		  + " ? , ? , ? , ? , ? , ? ,"
					  		  + " ? , ? , ? , ? , ? , ? ,"
					  		  + " ? , ? , ? ) ";
			pstmtPlanHdr = conn.prepareStatement(sqlPlanHdr);
		
			String sqlPlanDtl = "INSERT INTO PLAN_DTL(SITE_CODE, ITEM_CODE, DEMAND, SUPPLY, PLAN_SUPP, "
						 	  + "RUNNING_DEMAND, RUNNING_SUPP, RUNNING_PLAN_SUPP, IND_DEMAND, DUE_DATE) "
						 	  + "VALUES( ? , ? , ? , ? , ? , ? ,"
					  		  + " ? , ? , ? , ? ) ";
			pstmtPlanDet = conn.prepareStatement(sqlPlanDtl);

			for(int adpeCount=0; adpeCount<adpeList.size(); adpeCount++)
			{
				flag = true;
				adpElement = (ADPElement)adpeList.get(adpeCount);
				
				siteCode = adpElement.getSiteCode();
				itemCode = adpElement.getItemCode();
				itemSer = adpElement.getItemSer();
				siteCodeSupp = adpElement.getSiteCodeSupp();
				suppSour = adpElement.getSuppSour();
				bomCode =  adpElement.getBomCode();
				minQty = adpElement.getMinQty();
				minOrderQty =  adpElement.getMinOrderQty();
				minStkDay =  adpElement.getMinStkDay();
				maxQty = adpElement.getMaxQty();
				maxStkDay =  adpElement.getMaxStkDay();
				itemStru = adpElement.getItemStru();
				reorderQty =  adpElement.getReoQty();
				purLeadTime = adpElement.getPurLeadTime();
				qcLeadTime = adpElement.getQcLeadTime();
				mfgLeadTime = adpElement.getMfgLeadTime();
				integralQty =  adpElement.getIntegralQty();
				batchQty =  adpElement.getBatchQty();
				batchQtyType = adpElement.getBatchQtyType();
				masterSch = adpElement.getMasterSch();
				orderOpt = adpElement.getOrderOpt();
				yieldPerc =  adpElement.getYieldPerc();
				minPlanPerc =  adpElement.getMinPlanPerc();
				unit =  adpElement.getUnit();
				aprCode = adpElement.getAprCode();
				packCode = adpElement.getPackCode();
				empCodePur = adpElement.getEmpCodePur();
				suppCodePref = adpElement.getSuppCodePref();
				purcRate = adpElement.getPurcRate();
				acctCodePh = adpElement.getAcctCodePh();
				cctrCodePh = adpElement.getCctrCodePh();
				acctCodeWp = adpElement.getAcctCodeWp();
				cctrCodeWp = adpElement.getCctrCodeWp();
				
				ArrayList timeMRParr = adpElement.getTimeMrpList();
				
				for (int timeCtr = 0; timeCtr < timeMRParr.size() ; timeCtr++)
				{
					TimeMRP timeMrp = (TimeMRP)timeMRParr.get(timeCtr);
					
					demand = timeMrp.getDemand();
					supply = timeMrp.getSupply();
					planSupply = timeMrp.getPlanSupply();
					runningDemand = timeMrp.getRunningDemand();
					runningSupply = timeMrp.getRunningSupply();
					runningPlanSupply = timeMrp.getRunningPlanSupply();
					independentDemand = timeMrp.getIndependentDemand();
					dueDate = timeMrp.getDueDate();
					
					pstmtPlanDet.setString(1,siteCode);
					pstmtPlanDet.setString(2,itemCode);
					pstmtPlanDet.setDouble(3,demand);
					pstmtPlanDet.setDouble(4,supply);
					pstmtPlanDet.setDouble(5,planSupply);
					pstmtPlanDet.setDouble(6,runningDemand);
					pstmtPlanDet.setDouble(7,runningSupply);
					pstmtPlanDet.setDouble(8,runningPlanSupply);
					pstmtPlanDet.setDouble(9,independentDemand);
					pstmtPlanDet.setTimestamp(10,dueDate);
					
					pstmtPlanDet.addBatch();
				}
				pstmtPlanHdr.setString(1,siteCode);
				pstmtPlanHdr.setString(2,itemCode);
				pstmtPlanHdr.setString(3,itemSer);
				pstmtPlanHdr.setString(4,siteCodeSupp);
				pstmtPlanHdr.setString(5,suppSour);
				pstmtPlanHdr.setString(6,bomCode);
				pstmtPlanHdr.setDouble(7,minQty);
				pstmtPlanHdr.setDouble(8,minOrderQty);
				pstmtPlanHdr.setInt(9,minStkDay);
				pstmtPlanHdr.setDouble(10,maxQty);
				pstmtPlanHdr.setInt(11,maxStkDay);
				pstmtPlanHdr.setString(12,itemStru);
				pstmtPlanHdr.setDouble(13,reorderQty);
				pstmtPlanHdr.setInt(14,purLeadTime);
				pstmtPlanHdr.setInt(15,qcLeadTime);
				pstmtPlanHdr.setInt(16,mfgLeadTime);
				pstmtPlanHdr.setDouble(17,integralQty);
				pstmtPlanHdr.setDouble(18,batchQty);
				pstmtPlanHdr.setString(19,batchQtyType);
				pstmtPlanHdr.setString(20,masterSch);
				pstmtPlanHdr.setString(21,orderOpt);
				pstmtPlanHdr.setDouble(22,yieldPerc);
				pstmtPlanHdr.setDouble(23,minPlanPerc);
				pstmtPlanHdr.setString(24,unit);
				pstmtPlanHdr.setString(25,aprCode);
				pstmtPlanHdr.setString(26,packCode);
				pstmtPlanHdr.setString(27,empCodePur);
				pstmtPlanHdr.setString(28,suppCodePref);
				pstmtPlanHdr.setDouble(29,purcRate);
				pstmtPlanHdr.setString(30,acctCodePh);
				pstmtPlanHdr.setString(31,cctrCodePh);
				pstmtPlanHdr.setString(32,acctCodeWp);
				pstmtPlanHdr.setString(33,cctrCodeWp);
			
				pstmtPlanHdr.addBatch();
			}
			pstmtPlanHdr.executeBatch();	
			pstmtPlanHdr.clearParameters();
			pstmtPlanHdr.close();	

			pstmtPlanDet.executeBatch();	
			pstmtPlanDet.clearParameters();
			pstmtPlanDet.close();	

		}
		catch (SQLException se)
		{
			System.out.println("SQLException :RunMRPPrcEJB :InsertPlanTable():" + se.getMessage());
			se.printStackTrace();
			resultString = se.getMessage();
		//	throw new ITMException(e);
		}
		catch (Exception e)
		{
			System.out.println("Exception :RunMRPPrcEJB :InsertPlanTable():" + e.getMessage());
			e.printStackTrace();
			resultString = e.getMessage();
		//	throw new ITMException(e);
		}
		return resultString;
	}
}