package ibase.webitm.ejb.dis;
import ibase.system.config.ConnDriver;
import ibase.webitm.ejb.ITMDBAccessEJB;
import ibase.webitm.ejb.ProcessEJB;
import ibase.webitm.utility.GenericUtility;
import ibase.webitm.utility.ITMException;

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

import javax.ejb.Stateless;

import org.w3c.dom.Document;

@Stateless // added for ejb3 


public class CustSeriesPrcEjb extends ProcessEJB  implements CustSeriesPrcEjbLocal ,CustSeriesPrcEjbRemote
{
	GenericUtility genericUtility = GenericUtility.getInstance();

	public String process(String xmlString, String xmlString2, String windowName, String xtraParams) throws RemoteException,ITMException
	{
		String retStr = "";
		Document detailDom = null;
		Document headerDom = null;

		GenericUtility genericUtility = GenericUtility.getInstance();

		try
		{	
			if(xmlString != null && xmlString.trim().length()!=0)
			{
				headerDom = genericUtility.parseString(xmlString); 
				System.out.println("headerDom" + headerDom);
			}
			if(xmlString2 != null && xmlString2.trim().length()!=0)
			{
				detailDom = genericUtility.parseString(xmlString2); 
				System.out.println("detailDom" + detailDom);
			}

			retStr = process(headerDom, detailDom, windowName, xtraParams);

		}
		catch (Exception e)
		{
			System.out.println("Exception :ConsolidationPrcEJB :process(String xmlString, String xmlString2, String windowName, String xtraParams):" + e.getMessage() + ":");
			e.printStackTrace();
			System.out.println("exception :"+e.getMessage());
			retStr = e.getMessage();
		}
		return retStr;

	}
	//END

	public String process(Document headerDom, Document detailDom, String windowName, String xtraParams) throws RemoteException,ITMException
	{

		//variable declaration 
		String sql = ""; 		
		String errString = "";
		String custCode = "";
		String crTerm = "";
		double crLimit = 0.0;
		String nwProdCrTerm = "";
		double businessLimit = 0.0;
		double discPerc = 0.0;
		double commPerc = 0.0;
		double commPerc1 = 0.0;
		double commPerc2 = 0.0;
		double creditPrd = 0.0;
		int ignoreDays = 0;
		double ignoreCredit = 0.0;
		//end of varible declaration
		Connection conn = null;
		PreparedStatement pstmt = null, pstmt1 = null;
		ResultSet rs = null;

		int count = 0;
		boolean isError = false;
		ITMDBAccessEJB itmDBAccessEJB = new ITMDBAccessEJB();

		try
		{
			System.out.println("*********inside try block*******");
			ConnDriver connDriver = new ConnDriver();
			//get connection
			conn = connDriver.getConnectDB("DriverITM");
			conn.setAutoCommit(false);
			connDriver = null;
			//get data from header DOM

			custCode = genericUtility.getColumnValue("cust_code", headerDom)==null?"null":genericUtility.getColumnValue("cust_code", headerDom);
			System.out.println("custCode--------"+custCode);
			crTerm = genericUtility.getColumnValue("cr_term", headerDom)==null?"X":genericUtility.getColumnValue("cr_term", headerDom);
			System.out.println("crTerm--------"+crTerm);
			crLimit = Double.valueOf(genericUtility.getColumnValue("credit_lmt", headerDom)==null?"0.0":genericUtility.getColumnValue("credit_lmt", headerDom)) ;
			System.out.println("crLimit--------"+crLimit);
			nwProdCrTerm = genericUtility.getColumnValue("cr_term__np", headerDom)==null?"X":genericUtility.getColumnValue("cr_term__np", headerDom) ;
			System.out.println("nwProdCrTerm--------"+nwProdCrTerm);
			businessLimit = Double.valueOf(genericUtility.getColumnValue("busi_limit", headerDom)==null?"0.0":genericUtility.getColumnValue("busi_limit", headerDom)) ;
			System.out.println("businessLimit--------"+businessLimit);
			discPerc = Double.valueOf(genericUtility.getColumnValue("disc_perc", headerDom)==null?"0.0":genericUtility.getColumnValue("disc_perc", headerDom)) ;
			System.out.println("discPerc--------"+discPerc);
			commPerc = Double.valueOf(genericUtility.getColumnValue("comm_perc", headerDom)==null?"0.0":genericUtility.getColumnValue("comm_perc", headerDom)) ;
			System.out.println("commPerc--------"+commPerc);
			commPerc1 = Double.valueOf(genericUtility.getColumnValue("comm_perc__1", headerDom)==null?"0.0":genericUtility.getColumnValue("comm_perc__1", headerDom));
			System.out.println("commPerc1--------"+commPerc1);
			commPerc2 = Double.valueOf(genericUtility.getColumnValue("comm_perc__2", headerDom)==null?"0.0":genericUtility.getColumnValue("comm_perc__2", headerDom)) ;//
			System.out.println("commPerc2--------"+commPerc2);
			ignoreDays = Integer.valueOf(genericUtility.getColumnValue("ignore_days", headerDom) ==null?"0":genericUtility.getColumnValue("ignore_days", headerDom));
			System.out.println("ignoreDays--------"+ignoreDays);
			ignoreCredit = Double.valueOf(genericUtility.getColumnValue("ignore_credit", headerDom)==null?"0.0":genericUtility.getColumnValue("ignore_credit", headerDom)) ;
			System.out.println("ignoreCredit--------"+ignoreCredit);
			creditPrd = Double.valueOf(genericUtility.getColumnValue("credit_prd", headerDom)==null?"0.0":genericUtility.getColumnValue("credit_prd", headerDom)) ;
			System.out.println("credit period-------"+creditPrd);
			//IF CUSTOMER CODE IS NOT NULL THEN IT SHOULD BE EXIST IN CUSTOMER TABLE  
			if(custCode!=null)
			{
				sql = "SELECT COUNT(*) FROM CUSTOMER WHERE CUST_CODE = ?";
				pstmt1 = conn.prepareStatement(sql);
				pstmt1.setString(1, custCode );
				rs = pstmt1.executeQuery();
				while( rs.next() )
				{
				   System.out.println("count1----"+rs.getInt(1));
					count = rs.getInt(1);
				}
				System.out.println("count----"+count);
				rs.close();
				rs = null;
				pstmt1.close();
				pstmt1 = null;
				if(count == 0)
				{
					System.out.println("customer code in master ");
					errString = itmDBAccessEJB.getErrorString("","VMCUST1","","",conn);
					return errString;
				}
				else
				{
				sql = "SELECT COUNT(*)  FROM CUSTOMER_SERIES WHERE CUST_CODE = ?";
				pstmt1 = conn.prepareStatement(sql);
				pstmt1.setString(1, custCode );
				rs = pstmt1.executeQuery();
				while( rs.next() )
				{
					count = rs.getInt(1);
				}
				rs.close();
				rs = null;
				pstmt1.close();
				pstmt1 = null;
				if(count == 0)
				{
					System.out.println("customer code in Customer Series master ");
					errString = itmDBAccessEJB.getErrorString("","VTINVCUSCO","","",conn);
					return errString;
				}
				}

				
			}
			 if(!crTerm.equals("X"))
			{
				sql = "SELECT COUNT(*) FROM CRTERM WHERE CR_TERM = ?";
				pstmt1 = conn.prepareStatement(sql);
				pstmt1.setString(1, crTerm );
				rs = pstmt1.executeQuery();
				while( rs.next() )
				{
					count = rs.getInt(1);
				}
				rs.close();
				rs = null;
				pstmt1.close();
				pstmt1 = null;
				if(count == 0)
				{
					System.out.println("CREDIT TERM IN MASTER ");
					errString = itmDBAccessEJB.getErrorString("","VMCRTER1","","",conn);
					return errString;
				}

				
			}
			if(!nwProdCrTerm.equals("X"))
			{
				sql = "SELECT COUNT(*)  FROM CRTERM WHERE CR_TERM = ?";
				pstmt1 = conn.prepareStatement(sql);
				pstmt1.setString(1, nwProdCrTerm );
				rs = pstmt1.executeQuery();
				while( rs.next() )
				{
					count = rs.getInt(1);
				}
				rs.close();
				rs = null;
				pstmt1.close();
				pstmt1 = null;
				if(count == 0)
				{
					System.out.println("CREDIT TERM IN MASTER ");
					errString = itmDBAccessEJB.getErrorString("","VMCRTER1","","",conn);
					return errString;
				}

				
			}
			//update customer _series table against customer code (cust_code)
			
			sql="update customer_series set cr_term =  ( case when '"+crTerm+"' = 'X' then cr_term else '"+crTerm+"' end)" +
			", credit_lMT = ( case when "+crLimit+" = 0.0 then credit_lmt else "+crLimit+" end)" +
			",cr_term__np = ( case when '"+nwProdCrTerm+"' = 'X' then cr_term__np else '"+nwProdCrTerm+"' end)"+
			",busi_limit = ( case when "+businessLimit+" = 0.0 then busi_limit else "+businessLimit+" end)"+
			",disc_perc = ( case when "+discPerc+" = 0.0 then disc_perc else "+discPerc+" end) "+
			",comm_perc = ( case when "+commPerc+" = 0.0 then comm_perc else "+commPerc+" end) "+
			",comm_perc__1 = ( case when "+commPerc1+" = 0.0 then comm_perc__1 else "+commPerc1+" end) "+
			",comm_perc__2 = ( case when "+commPerc2+" = 0.0 then comm_perc__2 else "+commPerc2+" end) "+
			",ignore_days = ( case when "+ignoreDays+" = 0 then ignore_days else "+ignoreDays+" end) "+
			",ignore_credit = ( case when "+ignoreCredit+" = 0.0 then ignore_credit else "+ignoreCredit+" end)"+
			",credit_prd = ( case when "+creditPrd+" = 0.0 then credit_prd else "+creditPrd+" end)"+
			" where cust_code =?";
			System.out.println("sql-------"+sql);
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, custCode);
			count = pstmt.executeUpdate();
			pstmt.close();
			if(count > 0)
			{
				System.out.println("update row "+count);
				errString = itmDBAccessEJB.getErrorString("","VTPRCOMPLT","","",conn);
				//return errString;
			}
			
			
			

		}		
		catch(Exception se)
		{
			isError = true;
			System.out.println("SQLException :" + se);
			se.printStackTrace();
			System.out.println("error ****"+se.getMessage());
			errString = se.getMessage();
			try
			{
				conn.rollback();
			}
			catch(SQLException e1)
			{
				se = e1;
			}
			return errString;
		}

		finally
		{
			try
			{
				if( !isError  )
				{
					conn.commit(); 
				}

				if(conn != null)
				{	
					if(rs != null)
					{
						rs.close();
						rs = null;
					}
					if(pstmt != null)
					{
						pstmt.close();
						pstmt = null;
					}
					conn.close();
					conn = null;
				}
			}
			catch(Exception e)
			{
				errString = e.getMessage();
				e.printStackTrace();
				System.out.println("error =>"+e.getMessage());
				return errString ;
			}
		}
		return errString;
	}

	@Override
	public void remove() {
		// TODO Auto-generated method stub
		
	}
	
}

