/**
*	Window Name : w_journal_year_end
*	Button name : Closing
*	Action		: Get Closing Details Value
*/
package ibase.webitm.ejb.fin.adv;

import ibase.webitm.utility.GenericUtility;
import java.rmi.RemoteException;
import java.sql.*;
import org.w3c.dom.*;
import javax.ejb.*;

import ibase.webitm.utility.ITMException;
import ibase.webitm.ejb.*;
import ibase.system.config.*;
import java.util.StringTokenizer;
import javax.ejb.Stateless; // added for ejb3

@Stateless // added for ejb3

public class JournalYearEndAct extends ActionHandlerEJB implements JournalYearEndActLocal,JournalYearEndActRemote //SessionBean
{
	/*public void ejbCreate() throws RemoteException, CreateException 
	{
	}

	public void ejbRemove()
	{
	}

	public void ejbActivate() 
	{
	}

	public void ejbPassivate() 
	{
	}*/

    public String actionHandler() throws RemoteException,ITMException
	{
		return "";
	}

	public String actionHandler(String actionType, String xmlString, String objContext, String xtraParams) throws RemoteException,ITMException
	{
		Document dom = null;
		String  resString = null;
		try
		{
			if (actionType.equalsIgnoreCase("Closing"))
			{
				if(xmlString != null && xmlString.trim().length()!=0)
				{
					GenericUtility genericUtility = GenericUtility.getInstance();
					System.out.println("XML String :"+xmlString);
					dom = genericUtility.parseString(xmlString); 
				}
				resString = actionClose(dom,objContext,xtraParams);
			}
		}
	   	catch(Exception e)
		{
			System.out.println("Exception :JournalYearEnd :actionHandler(String xmlString):" + e.getMessage() + ":");
			e.printStackTrace();
			throw new ITMException(e);
		}
		System.out.println("returning from JournalYearEnd actionHandler"+resString);
	    return (resString);
	}

	private String actionClose(Document dom, String objContext, String xtraParams) throws RemoteException , ITMException
	{
		String tranId = "";
		String siteCodeFor = "";
		String reMarks = "";
		String acctCodeFrom = "";
		String acctCodeTo = "";
		String prdCodeFrom = "";
		String prdCodeTo = "";
		String siteCode = "";
		String errCode = "";
		String errString = "";
		String detCnt = "0";
		double amtDr = 0;
		double amtCr = 0;
		String accDescr = null;
		StringBuffer valueXmlString = new StringBuffer("<?xml version=\"1.0\"?>\r\n<Root>\r\n");
		String sql = "";
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		ITMDBAccessEJB itmDBAccessEJB = new ITMDBAccessEJB();
		ConnDriver connDriver = new ConnDriver();
		GenericUtility genericUtility = GenericUtility.getInstance();
		try
		{
			//detCnt = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"detCnt"); //Will be Uncommented later - Jiten
			if (detCnt.equals("0"))
			{
				conn = connDriver.getConnectDB("DriverITM");
				tranId = genericUtility.getColumnValue("tran_id",dom);
				siteCodeFor = genericUtility.getColumnValue("site_code__for",dom);
				reMarks = genericUtility.getColumnValue("remarks",dom);
				if (siteCodeFor == null)
				{
					errCode = "VTITEM3";
					System.out.println("errCode: "+errCode);
					if (errCode != null  && errCode.trim().length() > 0)
					{
						System.out.println("errCode: "+errCode);
						errString = itmDBAccessEJB.getErrorString("", errCode, "", "", conn);
					}
					System.out.println("errString: "+errString);
					return errString; 
				}
				//Get Closing Detail Value
				acctCodeFrom = genericUtility.getColumnValue("acct_code__fr",dom);
				acctCodeTo = genericUtility.getColumnValue("acct_code__to",dom);	
				prdCodeFrom = genericUtility.getColumnValue("prd_code__fr",dom);
				prdCodeTo = genericUtility.getColumnValue("prd_code__to",dom);
				siteCode   = genericUtility.getColumnValue("site_code__close",dom);
				System.out.println("siteCode :"+siteCode); 
				if(siteCode != null)				
				{
					StringTokenizer st = new StringTokenizer(siteCode,",");
					StringBuffer finalStr = new StringBuffer("");
					while (st.hasMoreTokens()) 
					{
						finalStr = finalStr.append(st.nextToken()).append("'").append(",").append("'");
					}
					siteCode = "";
					siteCode = finalStr.substring(0,finalStr.length()-3);
					sql = "SELECT A.ACCT_CODE, SUM(CASE WHEN A.DR_AMT__BASE IS NULL THEN 0 ELSE A.DR_AMT__BASE END) AMT_DR,"  
								  +"SUM(CASE WHEN A.CR_AMT__BASE IS NULL THEN 0 ELSE A.CR_AMT__BASE END) AMT_CR, A.CCTR_CODE, B.DESCR FROM ACCTBAL A, "
								  +"ACCOUNTS B, ACCTSGRP C, ACCTGRP D "
								  +"WHERE A.ACCT_CODE = B.ACCT_CODE "
								  +"AND B.SGROUP_CODE = C.SGROUP_CODE "
								  +"AND C.GROUP_CODE = D.GROUP_CODE "
								  +"AND D.ACCT_CAT IN ('I','E') "
								  +"AND A.SITE_CODE IN ('"+ siteCode +"') "
								  +"AND A.PRD_CODE >= '"+ prdCodeFrom + "' "
								  +"AND A.PRD_CODE <=  '"+ prdCodeTo +"' "
								  +"AND A.ACCT_CODE >= '"+ acctCodeFrom +"' "
								  +"AND A.ACCT_CODE <= '"+ acctCodeTo +"' "
								  +"GROUP BY A.PRD_CODE, A.ACCT_CODE, A.CCTR_CODE, B.DESCR "
								  +"HAVING SUM(CASE WHEN A.DR_AMT__BASE IS NULL THEN 0 ELSE A.DR_AMT__BASE END) <> SUM(CASE WHEN A.CR_AMT__BASE " +"IS NULL THEN 0 ELSE A.CR_AMT__BASE END)";
					
					System.out.println("sql ::"+sql);
					stmt = conn.createStatement();
					rs = stmt.executeQuery(sql);
					while (rs.next())
					{
						valueXmlString.append("<Detail>\r\n");
						valueXmlString.append("<acct_code isSrvCallOnChg='0'>").append("<![CDATA[").append(rs.getString(1)).append("]]>").append("</acct_code>\r\n");
						valueXmlString.append("<cctr_code isSrvCallOnChg='0'>").append("<![CDATA[").append(rs.getString(4)).append("]]>").append("</cctr_code>\r\n");
						valueXmlString.append("<remarks isSrvCallOnChg='0'>").append("<![CDATA[").append(reMarks).append("]]>").append("</remarks>\r\n");
						amtDr = Double.parseDouble(rs.getString(2));
						amtCr = Double.parseDouble(rs.getString(3));
						accDescr = rs.getString(5);
						if (amtDr >= amtCr)
						{
							valueXmlString.append("<drcr_flag isSrvCallOnChg='0'>").append("<![CDATA[").append("C").append("]]>").append("</drcr_flag>\r\n");
							valueXmlString.append("<amount isSrvCallOnChg='0'>").append("<![CDATA[").append(amtDr - amtCr).append("]]>").append("</amount>\r\n");
						}
						else
						{
							valueXmlString.append("<drcr_flag isSrvCallOnChg='0'>").append("<![CDATA[").append("D").append("]]>").append("</drcr_flag>\r\n");
							valueXmlString.append("<amount isSrvCallOnChg='0'>").append("<![CDATA[").append(amtCr - amtDr).append("]]>").append("</amount>\r\n");
						}
						valueXmlString.append("<accounts_descr isSrvCallOnChg='0'>").append("<![CDATA[").append(accDescr).append("]]>").append("</accounts_descr>\r\n");
						valueXmlString.append("</Detail>\r\n");
					}
					stmt.close();
					valueXmlString.append("</Root>\r\n");			
				}//end if
				else
				{
					errCode = "VMNLSITE";
				}
			}
			else
			{
				errCode = "VTDETCNT";
			}
			System.out.println("errCode :"+errCode);
			if (!errCode.equals("") && errCode.trim().length()>0)
			{
				System.out.println("errCode :"+errCode);
				errString = itmDBAccessEJB.getErrorString("",errCode,"","",conn);
				//conn.close();
				//conn = null;
				return errString;
			}
		}
		catch(SQLException e)
		{
			System.out.println("Exception : JournalYearEnd : actionHandler :(Document dom)" +e.getMessage());
			throw new ITMException(e);
		}
		catch(Exception e)
		{
			System.out.println("Exception : JournalYearEnd : actionHandler :(Document dom)" +e.getMessage());
			throw new ITMException(e);
		}
		finally
		{
			try
			{
				System.out.println("Closing Connection.....");
				conn.close();
				conn = null;
			}catch(SQLException se){}
		}
		return valueXmlString.toString();
	}
}