package ibase.webitm.ejb.dis;

import java.math.BigDecimal;
import java.rmi.RemoteException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import org.w3c.dom.Document;

import ibase.system.config.ConnDriver;
import ibase.utility.BaseLogger;
import ibase.utility.UserInfoBean;
import ibase.webitm.ejb.ActionHandlerEJB;
import ibase.webitm.ejb.ITMDBAccessEJB;
import ibase.webitm.utility.ITMException;

public class ReqForQuotationSubmit extends ActionHandlerEJB 
{
	public String confirm(String tranID,String xtraParams, String forcedFlag) throws RemoteException,ITMException
	{

		String retString = "";
		Connection conn = null;
		boolean isConn= false;
		try
		{
			BaseLogger.log("3", getUserInfo(), null,  "tranID:::::"+tranID);
			BaseLogger.log("3", getUserInfo(), null,  "xtraParams:::::"+xtraParams);
			BaseLogger.log("3", getUserInfo(), null,  "forcedFlag:::::"+forcedFlag);

			retString = this.confirm(tranID, xtraParams, forcedFlag, conn, isConn);
			BaseLogger.log("3", getUserInfo(), null,  "retString:::::"+retString);
		}
		catch(Exception e)
		{
			BaseLogger.log("3", getUserInfo(), null,  "Exception in [ReqForQuotationSubmit] submit " + e.getMessage());
			throw new ITMException(e);
		}
		return retString;
	}
	public String confirm( String tranId, String xtraParams, String forcedFlag, Connection conn, boolean connStatus ) throws RemoteException,ITMException
	{ 
		String retString = " ",status="";
		String sql = "";
		int count=0,upd=0;
		String refSer = "";
		String confirmed = "";
		ResultSet rs = null;
		PreparedStatement pstmt = null;
		ibase.utility.E12GenericUtility genericUtility= null;
		ITMDBAccessEJB itmDBAccess = null;
		String ptr="",rateQuot="";
		BigDecimal ratePtrVal = null;
		BigDecimal rateQuotVal = null;
		try
		{
			
			UserInfoBean userInfo = getUserInfo();
			
			BaseLogger.log( "3", null, null, "ManageSQLSubmit userInfo" + userInfo);

			BaseLogger.log("3", getUserInfo(), null,  " [ReqForQuotationSubmit] submit tranId:: " + tranId);
			BaseLogger.log("3", getUserInfo(), null,  " [ReqForQuotationSubmit] submit xtraParams:: " + xtraParams);

			genericUtility = new  ibase.utility.E12GenericUtility();
			itmDBAccess = new ITMDBAccessEJB();
			ConnDriver connDriver = new ConnDriver();
			//Changes and Commented By Bhushan on 09-06-2016 :START
			//conn = connDriver.getConnectDB("DriverITM");
			conn = getConnection();
			
			
			String ptrAndQuotRateSql = "SELECT PTR, RATE_QUOT FROM SALES_QUOT_REQ_DET WHERE TRAN_ID = ?";
			PreparedStatement ptrAndQuotRatePs = conn.prepareStatement(ptrAndQuotRateSql);
			ptrAndQuotRatePs.setString(1, tranId);
			ResultSet ptrAndQuotRateRs = ptrAndQuotRatePs.executeQuery();
			while(ptrAndQuotRateRs.next())
			{
				ptr = ptrAndQuotRateRs.getString("PTR");
				rateQuot = ptrAndQuotRateRs.getString("RATE_QUOT");

			}
			
			
			if(ptr != null && ptr.trim().length() > 0)
			{
			    ratePtrVal = new BigDecimal(ptr);

			}
			if(rateQuot != null && rateQuot.trim().length() > 0)
			{
			    rateQuotVal = new BigDecimal(rateQuot);
			}
			String loginCode = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"loginCode");
		     BaseLogger.log("3", null, null,"loginCode>>>>:"+loginCode); 

		    //Added condition if ptr and special rate is same direct create the sales quotation
			
			 if (rateQuotVal.compareTo(ratePtrVal) == 0) { 
		     BaseLogger.log("3", null, null,"In PTR and Special rate equal condition"); 
		     ReqForQuotationAprv reqForQuotationAprv = new ReqForQuotationAprv();
		     String domStringForTranId = "<Root><ref_id>"+tranId+"</ref_id><emp_code>"+loginCode+"</emp_code></Root>";
			 String result =  reqForQuotationAprv.updateWFStatus(domStringForTranId, conn, xtraParams);
			 BaseLogger.log("3", null, null, "ReqForQuotationAprv result>>>>> "+result);
			 
			 Document dom = genericUtility.parseString(result);

			 String detail = checkNull(genericUtility.getColumnValue("Detail", dom));

			 if ("Success".equalsIgnoreCase(detail)) {
				 BaseLogger.log("3", null, null, "ReqForQuotation transaction submitted");
				 retString = (new ITMDBAccessEJB()).getErrorString("", "VTSUBMTSUC", "", "", userInfo.getTransDB());
			 } else {
				 BaseLogger.log("3", null, null, "ReqForQuotation transaction submition failed");

					retString = (new ITMDBAccessEJB()).getErrorString("", "VTSUBMTREJ", "", "", userInfo.getTransDB());
			 }
			 BaseLogger.log("3", null, null, "ReqForQuotation retString::"+retString);


			 }
			 
			 else {
				 retString = (new ITMDBAccessEJB()).getErrorString("", "VTSUBMTSUC", "", "", userInfo.getTransDB());

			 }
			 
	}
	catch( Exception e )
	{
		try
		{
			conn.rollback();
			retString = e.getMessage();
			e.printStackTrace();
		}
		catch (Exception e1)
		{
			BaseLogger.log("3", getUserInfo(), null,  "Exception : "+e);e.printStackTrace();
		}
		throw new ITMException(e);
	}
	finally
	{
		System.out.println("retString1"+retString);
		if(retString != null && retString.indexOf("submitted") > -1)
		{
			try {
				conn.commit();
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		else if(retString != null && retString.indexOf("VTCONF8") > -1)
		{
			System.out.println("retString2"+retString);
			retString = itmDBAccess.getErrorString("","VTALSUB1","","",conn);
		}
		else
		{
			System.out.println("retString3"+retString);
			retString = itmDBAccess.getErrorString("","VTERRSUB","","",conn);
		}
		try{
			
		if(rs != null)
		{
			rs.close();rs = null;
		}
		if(pstmt != null)
		{
			pstmt.close();pstmt = null;
		}
		if(conn != null)
		{
			conn.close();
			conn = null;
		}
		}
		catch(Exception e)
		{BaseLogger.log("3", getUserInfo(), null,  "Exception : "+e);e.printStackTrace();}
		
	}
		return retString;
	}
	
	
	private String checkNull( String inputVal )
	{
		if ( inputVal == null )
		{
			inputVal = "";
		}
		return inputVal;
	}	

}
