/*
Name :- BaseInfo Pvt Ltd.
Modification:-
		Reason						Date[Like 05052007 all modified code should contain this so that search easier]

1-		The tran_ser P-IBC% also consider in where clause (07052007)

2-

3-


	Window Name : w_misc_pay
	Button Name : Voucher , All Rec
    Action      : Adding Records  for voucher and AllRecord by
	              selecting data from MISC_PAYABLES
*/
package ibase.webitm.ejb.fin.adv;

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.webitm.utility.GenericUtility;
import ibase.system.config.*;
import java.text.SimpleDateFormat;
import javax.ejb.Stateless; // added for ejb3

@Stateless // added for ejb3

public class MiscPayAct extends ActionHandlerEJB implements MiscPayActLocal,MiscPayActRemote //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("Voucher"))
			{
				if(xmlString != null && xmlString.trim().length()!=0)
				{
					GenericUtility genericUtility = GenericUtility.getInstance();
					System.out.println("XML String :"+xmlString);
					dom = genericUtility.parseString(xmlString);
				}
				resString = actionVoucher(dom,objContext,xtraParams);
			}
			if (actionType.equalsIgnoreCase("AllRec"))
			{
				if(xmlString != null && xmlString.trim().length()!=0)
				{
					GenericUtility genericUtility = GenericUtility.getInstance();
					System.out.println("XML String :"+xmlString);
					dom = genericUtility.parseString(xmlString);
				}
				resString = actionAllRecords(dom,objContext,xtraParams);
			}

		}
	   	catch(Exception e)
		{
			System.out.println("Exception :MiscPay :actionHandler(String xmlString):" + e.getMessage() + ":");
			e.printStackTrace();
			throw new ITMException(e);
		}
		System.out.println("returning from actionVoucher actionHandler"+resString);
	    return (resString);
	}

	public String actionHandlerTransform(String actionType, String xmlString, String xmlString1, String objContext, String xtraParams, String selDataStr) throws RemoteException,ITMException
	{
		System.out.println("actionHandlerTransform is calling.............");
		Document dom = null;
		Document dom1 = null;
		Document selDataDom = null;

		String  retString = null;
		try
		{
			if(xmlString != null && xmlString.trim().length()!=0)
			{
				dom = GenericUtility.getInstance().parseString(xmlString);
			}
			if(xmlString1 != null && xmlString1.trim().length()!=0)
			{
				dom1 = GenericUtility.getInstance().parseString(xmlString1);
			}
			if(selDataStr != null && selDataStr.length() > 0)
			{
				selDataDom = GenericUtility.getInstance().parseString(selDataStr);
			}

			System.out.println("!!!!actionType:"+actionType+":");
			System.out.println("!!!!xmlString :"+xmlString+"\n xmlString1 :"+xmlString1);
			System.out.println("!!!!!selDataStr :"+selDataStr);
			if (actionType.equalsIgnoreCase("Voucher"))
			{
				retString = voucherTransform(dom, dom1, objContext, xtraParams, selDataDom);
			}
		}
	   	catch(Exception e)
		{
			System.out.println("Exception :actionVoucher :actionHandlerTransform(String xmlString):" + e.getMessage() + ":");
			e.printStackTrace();
			throw new ITMException(e);
		}
		System.out.println("returning String from actionVoucher : actionHandlerTransform"+retString);
	    return retString;
	}

	private String actionVoucher(Document dom, String objContext, String xtraParams) throws RemoteException , ITMException
	{
		//System.out.println("\n***********!!!!!!!!In Action voucher................");
		//System.out.println("\n  in actionVoucher............");
		String siteCode = "";
		String currCode = "";
		String sundryCode = "";
		String sundryType = "";
		StringBuffer valueXmlString = new StringBuffer("<?xml version=\"1.0\"?>\r\n<Root>\r\n");
		String sql1 = "";
		String sql2 = "";
		String sql3 = "";
		String sql4 = "";
		String errCode = "";
		String str1 = "";
		String str2 = "";
		String grpCode = "";
		String errString = "";

		String tranID="";

		char sundryType1;
		int rowCnt = 0;
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		ResultSet rs1 = null;
		ResultSet rs2 = null;
		String paymentType= null;
		//Aviprash Start 26/10/05
		java.sql.Date vouchDate = null, tranDate = null, billDate = null;
		String vouchDate1 = "", tranDate1 = "", billDate1 = "";
		//End
		GenericUtility genericUtility = GenericUtility.getInstance();
		ConnDriver connDriver = new ConnDriver();
		ITMDBAccessEJB itmDBAccessEJB = new ITMDBAccessEJB();
		try
		{
			conn = connDriver.getConnectDB("DriverITM");
			paymentType=  genericUtility.getColumnValue("pay_type",dom);
			tranID  =  genericUtility.getColumnValue("tran_id",dom);
			//System.out.println("!!!!!!!!The transaction id is==="+tranID);
			//System.out.println("!!!!!!!!The pay_type is s==="+paymentType);

			//added below condition by mukesh 26sept for req id - FI89ALL024
			if("P".equalsIgnoreCase(paymentType))
     	    {
	    		siteCode = genericUtility.getColumnValue("site_code__bill",dom);
			}
			else
			{
			  siteCode = genericUtility.getColumnValue("site_code",dom);
			}
			currCode = genericUtility.getColumnValue("curr_code",dom);
			sundryCode = genericUtility.getColumnValue("sundry_code",dom);
			sundryType = genericUtility.getColumnValue("sundry_type",dom);
			sundryType1 = sundryType.charAt(0);

			sql1="SELECT COUNT(*) AS ROWCNT FROM MISC_PAYABLES WHERE SITE_CODE = '"+ siteCode +"'" + " AND CURR_CODE = '"+currCode +"'" + " AND SUNDRY_TYPE = '"+ sundryType +"'"+"AND SUNDRY_CODE = '"+ sundryCode +"'"+" AND TOT_AMT - ADJ_AMT <> 0";

			System.out.println("sql1 :="+sql1);
			stmt = conn.createStatement();
			rs = stmt.executeQuery(sql1);
			if (rs.next())
			{
				rowCnt = rs.getInt("ROWCNT");
			}
			if (rowCnt == 0)
			{
				errCode = "VTSTOCK1";
				System.out.println("errCode :"+errCode);
				errString = itmDBAccessEJB.getErrorString("", errCode, "", "", conn);
				conn.close();
				conn = null;
				return errString;
			}
			System.out.println("sundryType check in switch case "+sundryType1);
			switch (sundryType1)
			{
				case 'E': //Employee
					str1 = "AND MISC_PAYABLES.SUNDRY_CODE  = '" + sundryCode +"'";
					break;
				case 'C': //Customer
					sql2 = "SELECT GROUP_CODE FROM CUSTOMER WHERE CUST_CODE = '"+ sundryCode +"'";
					rs1 = conn.createStatement().executeQuery(sql2);
					if (rs1.next())
					{
						grpCode = rs1.getString(1);
					}
					str1 = "AND MISC_PAYABLES.SUNDRY_CODE IN (SELECT CUST_CODE FROM CUSTOMER WHERE GROUP_CODE = '"+ grpCode +"')";
					break;
				case 'R': //Strategic Customer
					sql2 = "SELECT GROUP_CODE FROM CUSTOMER WHERE CUST_CODE = (SELECT CUST_CODE FROM STRG_CUSTOMER WHERE SC_CODE = '"+ sundryCode +"')";
					rs1 = conn.createStatement().executeQuery(sql2);
					if (rs1.next())
					{
						grpCode = rs1.getString(1);
					}
					str1 = "AND MISC_PAYABLES.SUNDRY_CODE IN (SELECT CUST_CODE FROM CUSTOMER WHERE GROUP_CODE = '" + grpCode +"')";
					break;
  				case 'S': //Supplier
					sql2 = " SELECT GROUP_CODE FROM SUPPLIER WHERE SUPP_CODE ='"+ sundryCode +"'";
					rs1 = conn.createStatement().executeQuery(sql2);
					if (rs1.next())
					{
						grpCode = rs1.getString(1);
					}
					str1 = "AND MISC_PAYABLES.SUNDRY_CODE IN (SELECT SUPP_CODE FROM SUPPLIER WHERE GROUP_CODE ='"+ grpCode +"')";
					break;
				case 'B': //Bank
					sql2 = "SELECT GROUP_CODE FROM BANK WHERE BANK_CODE ='"+ sundryCode +"'";
					rs1 = conn.createStatement().executeQuery(sql2);
					if (rs1.next())
					{
						grpCode = rs1.getString(1);
					}
					str1 = "AND MISC_PAYABLES.SUNDRY_CODE IN (SELECT BANK_CODE FROM BANK WHERE GROUP_CODE ='"+ grpCode +"')";
					break;
				case 'P': //Sales Person
					sql2 = "SELECT GROUP_CODE FROM SALES_PERS WHERE SALES_PERS ='"+ sundryCode +"'";
					rs1 = conn.createStatement().executeQuery(sql2);
					if (rs1.next())
					{
						grpCode = rs1.getString(1);
					}
					str1 = "AND MISC_PAYABLES.SUNDRY_CODE IN (SELECT SALES_PERS FROM SALES_PERS WHERE GROUP_CODE ='"+ grpCode +"')";
					break;
				case 'T': //Transporter
					sql2 = "SELECT GROUP_CODE FROM TRANSPORTER WHERE TRAN_CODE ='"+ sundryCode +"'";
					rs1 = conn.createStatement().executeQuery(sql2);
					if (rs1.next())
					{
						grpCode = rs1.getString(1);
					}
					str1 = "AND MISC_PAYABLES.SUNDRY_CODE IN (SELECT TRAN_CODE FROM TRANSPORTER WHERE GROUP_CODE ='"+ grpCode +"')";
					break;
				case 'X': //Tax Authority
					break;
				case 'O':
			}
			sql3 = "SELECT MISC_PAYABLES.TRAN_ID, "
					+"MISC_PAYABLES.TRAN_SER, "
					+"MISC_PAYABLES.TRAN_DATE, "
					+"MISC_PAYABLES.BILL_NO, "
					+"MISC_PAYABLES.BILL_DATE, "
					+"MISC_PAYABLES.CURR_CODE, "
					+"MISC_PAYABLES.SUNDRY_CODE, "
					+"MISC_PAYABLES.TOT_AMT, "
					+"MISC_PAYABLES.ADJ_AMT, "
					+"MISC_PAYABLES.SUNDRY_TYPE, "
					+"MISC_PAYABLES.REF_NO, "
					+"MISC_PAYABLES.REF_DATE "
					+"FROM MISC_PAYABLES "
					+"WHERE (MISC_PAYABLES.TRAN_SER IN ('M-VOUC', 'VOUCH') OR TRAN_SER LIKE 'P-IBC%') "//(07052007)
					+"AND MISC_PAYABLES.CURR_CODE = '"+currCode+"' "
					+"AND MISC_PAYABLES.SITE_CODE = '"+siteCode+"' "
					+"AND MISC_PAYABLES.SUNDRY_TYPE = '"+sundryType+"' "
					+"AND MISC_PAYABLES.TOT_AMT - MISC_PAYABLES.ADJ_AMT <> 0 ";
			System.out.println("str1:="+str1);
			System.out.println("sql3:="+sql3);
			sql4 = sql3.concat(str1);
			System.out.println("sql4:="+sql4);
			SimpleDateFormat sdf = new SimpleDateFormat(genericUtility.getApplDateFormat());
			rs2 = conn.createStatement().executeQuery(sql4);
			while (rs2.next())
			{
				double peindingQty = rs2.getDouble(8) - rs2.getDouble(9);
				valueXmlString.append("<Detail>\r\n");
				valueXmlString.append("<tran_id>").append("<![CDATA[").append(rs2.getString(1) == null?"":rs2.getString(1).trim()).append("]]>").append("</tran_id>\r\n");
				valueXmlString.append("<ref_no>").append("<![CDATA[").append(rs2.getString(11) == null?"":rs2.getString(11).trim()).append("]]>").append("</ref_no>\r\n");
				valueXmlString.append("<tran_ser>").append("<![CDATA[").append(rs2.getString(2) == null?"":rs2.getString(2).trim()).append("]]>").append("</tran_ser>\r\n");
				valueXmlString.append("<tran_date>").append("<![CDATA[").append(rs2.getDate(12) == null?"":sdf.format(rs2.getDate(12))).append("]]>").append("</tran_date>\r\n");
				valueXmlString.append("<sundry_type>").append("<![CDATA[").append(rs2.getString(10) == null?"":rs2.getString(10).trim()).append("]]>").append("</sundry_type>\r\n");
				valueXmlString.append("<sundry_code>").append("<![CDATA[").append(rs2.getString(7) == null?"":rs2.getString(7).trim()).append("]]>").append("</sundry_code>\r\n");
				valueXmlString.append("<tot_amt>").append("<![CDATA[").append(rs2.getString(8) == null?"":rs2.getString(8).trim()).append("]]>").append("</tot_amt>\r\n");
				valueXmlString.append("<pending_amt>").append("<![CDATA[").append(peindingQty).append("]]>").append("</pending_amt>\r\n");
				//valueXmlString.append("<adj_amt>").append("<![CDATA[").append(rs2.getString(9) == null?"":rs2.getString(9).trim()).append("]]>").append("</adj_amt>\r\n");

				valueXmlString.append("<tran_id__payable>").append("<![CDATA[").append(rs2.getString(1).trim()).append("]]>").append("</tran_id__payable>\r\n");
			    //valueXmlString.append("<bill_no>").append("<![CDATA[").append(rs2.getString(4) == null?"":rs2.getString(4).trim()).append("]]>").append("</bill_no>\r\n");
				//commented by mukesh to avoid garbage value 26 sept 08
				if(rs2.getString(4)==null)
				{
				   valueXmlString.append("<bill_no/>");
				}
				else
				{
				   valueXmlString.append("<bill_no>").append("<![CDATA[").append(rs2.getString(4).trim()).append("]]>").append("</bill_no>\r\n");
				}
				valueXmlString.append("<bill_date>").append("<![CDATA[").append(rs2.getDate(5) == null?"":sdf.format(rs2.getDate(5))).append("]]>").append("</bill_date>\r\n");
				valueXmlString.append("<curr_code>").append("<![CDATA[").append(rs2.getString(6) == null?"":rs2.getString(6).trim()).append("]]>").append("</curr_code>\r\n");
				valueXmlString.append("<vouch_no>").append("<![CDATA[").append( rs2.getString(11) == null?"":rs2.getString(11).trim() ).append("]]>").append("</vouch_no>\r\n");
				valueXmlString.append("<vouch_date>").append("<![CDATA[").append(rs2.getDate(12) == null?"":sdf.format(rs2.getDate(12))).append("]]>").append("</vouch_date>\r\n");
				valueXmlString.append("</Detail>\r\n");
				//End
			}
			stmt.close();
			valueXmlString.append("</Root>\r\n");
		}
		catch(SQLException e)
		{
			System.out.println("Exception : MiscPay : actionVoucher " +e.getMessage());
			throw new ITMException(e);
		}
		catch(Exception e)
		{
			System.out.println("Exception : MiscPay : actionVoucher :(Document dom)" +e.getMessage());
			throw new ITMException(e);
		}
		finally
		{
			try{
			conn.close();
			}catch(Exception e){}
		}
		System.out.println("valueXmlString.toString() "+valueXmlString.toString());
		return valueXmlString.toString();
	}

	private String actionAllRecords(Document dom, String objContext, String xtraParams) throws RemoteException , ITMException
	{

	    System.out.println("\n######!!!!!!!! In actionAllRecords..............");
		String tranId = "";
		String sql = "";
		StringBuffer valueXmlString = new StringBuffer("<?xml version=\"1.0\"?>\r\n<Root>\r\n");
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		String errCode = "" ,errString = "";
		String detCnt = "0";
		ConnDriver connDriver = new ConnDriver();
		GenericUtility genericUtility = GenericUtility.getInstance();
		ITMDBAccessEJB itmDBAccessEJB = new ITMDBAccessEJB();
		try
		{
			//detCnt = genericUtility.getValueFromXTRA_PARAMS(xtraParams,"detCnt"); //Will be Uncommented later - Jiten
			if (detCnt.equals("0"))
			{
				conn = connDriver.getConnectDB("DriverITM");
				tranId = genericUtility.getColumnValue("vouch_no",dom) + "%";
				System.out.println("tranId: ="+tranId);
				sql = "SELECT REF_NO FROM MISC_PAYABLES WHERE REF_NO LIKE '"+ tranId +"'"+" AND CASE WHEN TOT_AMT IS NULL THEN 0 ELSE TOT_AMT END - CASE WHEN ADJ_AMT IS NULL THEN 0 ELSE ADJ_AMT END <> 0 AND CASE WHEN HOLD_AMT IS NULL THEN 0 ELSE HOLD_AMT END = 0";
				stmt = conn.createStatement();
				rs = stmt.executeQuery(sql);
				while (rs.next())
				{
					valueXmlString.append("<Detail>\r\n");
					valueXmlString.append("<vouch_no>").append("<![CDATA[").append( rs.getString(1) == null?"":rs.getString(1).trim() ).append("]]>").append("</vouch_no>\r\n");
					valueXmlString.append("</Detail>\r\n");
				}
				stmt.close();
				valueXmlString.append("</Root>\r\n");
			}
			else
			{
				errCode = "VTDETCNT";
			}
			if (!errCode.equals(""))
			{
				errString = itmDBAccessEJB.getErrorString("",errCode,"","",conn);
				conn.close();
				conn = null;
				return errString;
			}
		}
		catch(SQLException e)
		{
			System.out.println("Exception : MiscPay : actionAllRecords " +e.getMessage());
			throw new ITMException(e);
		}
		catch(Exception e)
		{
			System.out.println("Exception : MiscPay : actionHandler :(Document dom)" +e.getMessage());
			throw new ITMException(e);
		}
		finally
		{
			try
			{
				System.out.println("Closing Connection........");
				conn.close();
				conn = null;
			}catch(SQLException se){}
		}
		System.out.println("valueXmlString.toString() :"+valueXmlString.toString());
		return valueXmlString.toString();
	}

	private String voucherTransform(Document dom,Document dom1, String objContext, String xtraParams, Document selDataDom)throws ITMException
	{
		//System.out.println(" \n @@@@@@@@@ ######!!!!!!!!  voucherTransform is calling.............");
		//System.out.println("\n voucherTransform is calling.............");
		StringBuffer valueXmlString = new StringBuffer("<?xml version=\"1.0\"?>\r\n<Root>\r\n");
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		Node currDetail = null;
		String sundryType = "", sundryCode = "", refNo = "", currCode = "", acctCode ="";
		String sql = "", cctrCode = "", transSer = "", taxChap = "", taxClass = "", taxEnv = "";
		java.sql.Date refDate = null;
		double exchRate = 0, netAmt = 0, payAmt = 0;
		ConnDriver connDriver = new ConnDriver();
		ITMDBAccessEJB itmDBAccessEJB = new ITMDBAccessEJB();
		String tranIdHrd = "", payMode = "", bankCode = "", tranDate = "", payableAt = "", stanCode = "";
		double hdrExchRate = 0, chngRate = 0, totalPaid = 0;
		String taxClassHdr = "", taxChapHdr  = "", taxEnvHdr = "", tranId ="";
		//GenericUtility genericUtility = GenericUtility.getInstance();
		//System.out.println("\n In voucherTransform The dom is ==="+dom);
		//System.out.println("\n IN voucherTransform the dom1 is=="+dom1);
		//System.out.println("\n In voucher Transform , the selDom is =="+selDataDom);
		try
		{
			conn = connDriver.getConnectDB("DriverITM");
			stmt = conn.createStatement();
			NodeList detailList = selDataDom.getElementsByTagName("Detail");
			int noOfDetails = detailList.getLength();

			for(int ctr = 0; ctr < noOfDetails; ctr++)
			{
				currDetail = detailList.item(ctr);
				tranId = GenericUtility.getInstance().getColumnValueFromNode("tran_id", currDetail);
				System.out.println("tranId :"+tranId);
				sql = "SELECT SUNDRY_TYPE, "
					 +"SUNDRY_CODE, "
					 +"REF_NO, "
					 +"REF_DATE, "
					 +"CURR_CODE, "
					 +"EXCH_RATE, "
					 +"ACCT_CODE, "
					 +"CCTR_CODE, "
					 +"TOT_AMT - ADJ_AMT, "
					 +"TOT_AMT - ADJ_AMT, "
					 +"TRAN_SER  "
					 +"FROM MISC_PAYABLES "
					 +"WHERE TRAN_ID = '"+tranId+"' ";
				System.out.println("sql :"+sql);
			//	PreparedStatement pstmt = conn.prepareStatement(sql);
			//	pstmt.setString(1, tranId);
				rs = stmt.executeQuery(sql);
				if (rs.next())
				{
					sundryType = rs.getString(1);
					sundryCode = rs.getString(2);
					refNo = rs.getString(3);
					refDate = rs.getDate(4);
					currCode = rs.getString(5);
					exchRate = rs.getDouble(6);
					acctCode = rs.getString(7);
					cctrCode = rs.getString(8);
					netAmt = rs.getDouble(9);
					payAmt = rs.getDouble(10);
					transSer = rs.getString(11);
				}
				//System.out.println("sundryType :"+sundryType+" sundryCode :"+sundryCode+" refNo :"+refNo+" refDate :"+refDate+" currCode :"+currCode+" exchRate :"+exchRate+" acctCode :"+acctCode+" netAmt :"+netAmt+" payAmt :"+payAmt+" transSer :"+transSer);
				hdrExchRate = Double.parseDouble(GenericUtility.getInstance().getColumnValue("exch_rate", dom1));
				tranIdHrd = GenericUtility.getInstance().getColumnValue("tran_id", dom1);
				//System.out.println("hdrExchRate  :"+hdrExchRate+" tranIdHrd :"+tranIdHrd);
				//System.out.println("@@@@@@@@##The trainId Hrd from voucherTransform is ==:"+tranIdHrd);
				SimpleDateFormat sdf = new SimpleDateFormat(GenericUtility.getInstance().getApplDateFormat());
				valueXmlString.append("<Detail>");
				valueXmlString.append("<tran_id isSrvCallOnChg='0'>").append("<![CDATA[").append(tranIdHrd).append("]]>").append("</tran_id>\r\n");
				valueXmlString.append("<tran_ser isSrvCallOnChg='0'>").append("<![CDATA[").append(transSer).append("]]>").append("</tran_ser>\r\n");
				valueXmlString.append("<vouch_no isSrvCallOnChg='1'>").append("<![CDATA[").append(refNo == null?"":refNo.trim()).append("]]>").append("</vouch_no>\r\n");
				valueXmlString.append("<vouch_date isSrvCallOnChg='0'>").append("<![CDATA[").append(refDate == null?"":sdf.format(refDate)).append("]]>").append("</vouch_date>\r\n");
				valueXmlString.append("<sundry_type isSrvCallOnChg='0'>").append("<![CDATA[").append(sundryType).append("]]>").append("</sundry_type>\r\n");
				valueXmlString.append("<sundry_code isSrvCallOnChg='0'>").append("<![CDATA[").append(sundryCode).append("]]>").append("</sundry_code>\r\n");
				valueXmlString.append("<vouch_date isSrvCallOnChg='0'>").append("<![CDATA[").append(refDate == null?"":sdf.format(refDate)).append("]]>").append("</vouch_date>\r\n");
				valueXmlString.append("<curr_code isSrvCallOnChg='0'>").append("<![CDATA[").append(currCode).append("]]>").append("</curr_code>\r\n");
				valueXmlString.append("<exch_rate__vouch isSrvCallOnChg='0'>").append("<![CDATA[").append(exchRate).append("]]>").append("</exch_rate__vouch>\r\n");
				valueXmlString.append("<acct_code__ap isSrvCallOnChg='0'>").append("<![CDATA[").append(acctCode).append("]]>").append("</acct_code__ap>\r\n");
				//valueXmlString.append("<accounts_descr isSrvCallOnChg='0'>").append("<![CDATA[").append(tranIdHrd).append("]]>").append("</accounts_descr>\r\n");
				valueXmlString.append("<cctr_code__ap isSrvCallOnChg='0'>").append("<![CDATA[").append(cctrCode).append("]]>").append("</cctr_code__ap>\r\n");
				valueXmlString.append("<net_amt isSrvCallOnChg='0'>").append("<![CDATA[").append(netAmt).append("]]>").append("</net_amt>\r\n");
				valueXmlString.append("<net_pay_amt isSrvCallOnChg='0'>").append("<![CDATA[").append(netAmt).append("]]>").append("</net_pay_amt>\r\n");
				valueXmlString.append("<pay_amt isSrvCallOnChg='0'>").append("<![CDATA[").append(payAmt).append("]]>").append("</pay_amt>\r\n");
				/*The total Paid column not appear on Eclipse but its appear on PB(ITM) Following is the calculation for total Paid Aviprash 13/02/06 */
				//totalPaid = totalPaid + payAmt;
				//valueXmlString.append("<cumulative_total isSrvCallOnChg='0'>").append("<![CDATA[").append(totalPaid).append("]]>").append("</cumulative_total>\r\n");

				double currDiffrence = ((payAmt * exchRate) - (payAmt * hdrExchRate));
				System.out.println("currDiffrence :"+currDiffrence);
				valueXmlString.append("<diff_amt__exch isSrvCallOnChg='0'>").append("<![CDATA[").append(currDiffrence).append("]]>").append("</diff_amt__exch>\r\n");
				payMode = GenericUtility.getInstance().getColumnValue("pay_mode", dom1);
				if (payMode.equals("D") || payMode.equals("P") || payMode.equals("T"))
				{
					payAmt = Double.parseDouble(GenericUtility.getInstance().getColumnValue("pay_amt", dom));
					bankCode = GenericUtility.getInstance().getColumnValue("bank_code", dom1);
					tranDate = GenericUtility.getInstance().getColumnValue("tran_date", dom1);
					System.out.println("payAmt  :"+payAmt+" \n bankCode :"+bankCode+" \ntranDate:"+tranDate);
					chngRate = bankChargesCalc(bankCode, payMode, tranDate, payAmt);
					valueXmlString.append("<bank_charges isSrvCallOnChg='0'>").append("<![CDATA[").append(chngRate).append("]]>").append("</bank_charges>\r\n");
					if (payMode.equals("D") || payMode.equals("P"))
					{
						payableAt = payableAt(sundryType, sundryCode, bankCode);
						valueXmlString.append("<drawn_at isSrvCallOnChg='0'>").append("<![CDATA[").append(payableAt).append("]]>").append("</drawn_at>\r\n");
					}
					stanCode = stanDrawn(sundryType, sundryCode);
					valueXmlString.append("<stan_code isSrvCallOnChg='0'>").append("<![CDATA[").append(stanCode).append("]]>").append("</stan_code>\r\n");
				}
				taxClassHdr = GenericUtility.getInstance().getColumnValue("tax_class", dom1);
				taxChapHdr = GenericUtility.getInstance().getColumnValue("tax_chap", dom1);
				taxEnvHdr = GenericUtility.getInstance().getColumnValue("tax_env", dom1);
				String itemCode = "", itemSer = "M-PAY", code = "", siteCode = "", stationFr = "", stationTo  = "";
				if((taxClassHdr == null || taxClassHdr.trim().length() == 0) && (taxChapHdr == null || taxChapHdr.trim().length() == 0) && (taxEnvHdr == null || taxEnvHdr.trim().length() == 0))
				{
					if (taxChap == null || taxChap.trim().length() == 0)
					{
						if (taxChapHdr == null || taxChapHdr.trim().length() == 0)
						{
							taxChap = itmDBAccessEJB.getTaxChapter(itemCode, itemSer, 'S', code, siteCode, conn);
							System.out.println("taxChap :"+taxChap);
							valueXmlString.append("<tax_chap isSrvCallOnChg='0'>").append("<![CDATA[").append(taxChap).append("]]>").append("</tax_chap>\r\n");
						}
						else
						{
							valueXmlString.append("<tax_chap isSrvCallOnChg='0'>").append("<![CDATA[").append(taxChapHdr).append("]]>").append("</tax_chap>\r\n");
						}
					}

					if (taxClass == null || taxClass.trim().length() == 0)
					{
						if (taxClassHdr == null || taxClassHdr.trim().length() == 0)
						{
							taxClass = itmDBAccessEJB.getTaxClass('S', code, itemCode, siteCode, conn);
							System.out.println("taxClass :"+taxClass);
							valueXmlString.append("<tax_class isSrvCallOnChg='0'>").append("<![CDATA[").append(taxClass).append("]]>").append("</tax_class>\r\n");
						}
						else
						{
							valueXmlString.append("<tax_class isSrvCallOnChg='0'>").append("<![CDATA[").append(taxClassHdr).append("]]>").append("</tax_class>\r\n");
						}
					}

					if (taxEnv == null || taxEnv.trim().length() == 0)
					{
						if (taxEnvHdr == null || taxEnvHdr.trim().length() == 0)
						{
							taxChap = GenericUtility.getInstance().getColumnValue("tax_chap", dom);
							taxClass = GenericUtility.getInstance().getColumnValue("tax_class", dom);
							stationFr = GenericUtility.getInstance().getColumnValue("stan_code", dom);
							sql = "SELECT STAN_CODE "
								  +"FROM SITE WHERE SITE_CODE = '"+siteCode+"' ";
							rs = stmt.executeQuery(sql);
							if (rs.next())
							{
								stationTo = rs.getString(1);
							}
							taxEnv = itmDBAccessEJB.getTaxEnv(stationFr, stationTo, taxChap, taxClass, siteCode, conn);
							System.out.println("taxEnv :"+taxEnv);
							valueXmlString.append("<tax_env isSrvCallOnChg='0'>").append("<![CDATA[").append(taxEnv).append("]]>").append("</tax_env>\r\n");
						}
						else
						{
							valueXmlString.append("<tax_env isSrvCallOnChg='0'>").append("<![CDATA[").append(taxEnvHdr).append("]]>").append("</tax_env>\r\n");
						}
					}
				}
				else
				{
					if (taxChap == null || taxChap.trim().length() == 0)
					{
						valueXmlString.append("<tax_chap isSrvCallOnChg='0'>").append("<![CDATA[").append(taxChapHdr).append("]]>").append("</tax_chap>\r\n");
					}
					if (taxClass == null || taxClass.trim().length() == 0)
					{
						valueXmlString.append("<tax_class isSrvCallOnChg='0'>").append("<![CDATA[").append(taxClassHdr).append("]]>").append("</tax_class>\r\n");
					}
					if (taxEnv == null || taxEnv.trim().length() == 0)
					{
						valueXmlString.append("<tax_env isSrvCallOnChg='0'>").append("<![CDATA[").append(taxEnvHdr).append("]]>").append("</tax_env>\r\n");
					}
				}
				valueXmlString.append("</Detail>");
			}// for end
			valueXmlString.append("</Root>");
		}// end try
		catch(ITMException itme)
		{
			throw itme;
		}
		catch(Exception e)
		{
			throw new ITMException(e);
		}
		finally
		{
			try
			{
				System.out.println("Closing Connection.....");
				conn.close();
				conn = null;
			}catch(Exception e){}
		}
		System.out.println("valueXmlString from voucherTransFer():"+valueXmlString.toString());
		return valueXmlString.toString();
	}

	private String payableAt(String sundryType, String sundryCode, String bankCode)throws ITMException
	{
		Connection conn = null;
		Statement stmt = null;
		String sql = "", payableAt = "", stanCode = "", bankRef = "";
		ConnDriver connDriver = new ConnDriver();
		ResultSet rs = null;
		try
		{
			conn = connDriver.getConnectDB("DriverITM");
			stmt = conn.createStatement();
			char sundryType1 = sundryType.charAt(0);
			switch (sundryType1)
			{
			case 'E':
				sql = "SELECT DD_PAYABLE_BANK, STAN_CODE FROM EMPLOYEE WHERE EMP_CODE = '"+sundryCode+"'";
				break;
			case 'S':
				sql = "SELECT BANK_REF, STAN_CODE FROM SUPPLIER WHERE SUPP_CODE = '"+sundryCode+"'";
				break;
			case 'C':
				sql = "SELECT BANK_NAME, STAN_CODE FROM CUSTOMER WHERE CUST_CODE = '"+sundryCode+"'";
				break;
			case 'P':
			case 'T':
				sql = "SELECT PAYABLE_REF, STAN_CODE FROM TRANSPORTER WHERE TRAN_CODE = '"+sundryCode+"'";
				break;
			case 'X':
				sql = "SELECT BANK_REF, STAN_CODE FROM TAX_AUTHORITY WHERE TAUTH_CODE = '"+sundryCode+"'";
			}// switch end
			System.out.println("sql :"+sql);
			if (sql != null)
			{
				rs = stmt.executeQuery(sql);
				if (rs.next())
				{
					payableAt = rs.getString(1);
					stanCode =  rs.getString(2);
				}
				stmt.close();
			}
			System.out.println("payableAt :"+payableAt);
			if (payableAt == null || payableAt.trim().length() > 0)
			{
				stmt = conn.createStatement();
				sql ="SELECT DRAWN_AT, BANK_REF FROM BANK_STAN_SERV WHERE BANK_CODE = '"+bankCode+"' "
					+"AND STAN_CODE = '"+stanCode+"'";
				System.out.println("sql :"+sql);
				rs = stmt.executeQuery(sql);
				if (rs.next())
				{
					payableAt = rs.getString(1);
					System.out.println("payableAt  :"+payableAt);
					bankRef =  rs.getString(2);
					System.out.println("bankRef :"+bankRef);
				}
				if (bankRef != null)
				{
					payableAt = (payableAt == null?"":payableAt.trim()+", ")+bankRef.trim();
					System.out.println("payableAt  :"+payableAt);
				}
			}
		}// try end
		catch (SQLException sqx)
		{
			System.out.println("The sqlException occure in MiscPayActEJB :"+sqx);
			throw new ITMException(sqx);
		}
		catch (Exception e)
		{
			System.out.println("The Exception occure in MiscPayActEJB :"+e);
			throw new ITMException(e);
		}
		finally
		{
			try
			{
				System.out.println("Closing Connection.....");
				conn.close();
				conn = null;
			}
			catch (Exception e){}
		}
		return payableAt;
	}

	private String stanDrawn(String sundryType, String sundryCode)throws ITMException
	{
		Connection conn = null;
		Statement stmt = null;
		String sql = "", empCode = "", stanCode = "";
		ConnDriver connDriver = new ConnDriver();
		ResultSet rs = null;
		try
		{
			conn = connDriver.getConnectDB("DriverITM");
			stmt = conn.createStatement();
			char sundryType1 = sundryType.charAt(0);
			switch (sundryType1)
			{
			case 'E':
				sql = "SELECT STAN_CODE FROM EMPLOYEE WHERE EMP_CODE = '"+sundryCode+"'";
				break;
			case 'S':
				sql = "SELECT STAN_CODE FROM SUPPLIER WHERE SUPP_CODE = '"+sundryCode+"'";
				break;
			case 'C':
				sql = "SELECT STAN_CODE FROM CUSTOMER WHERE CUST_CODE = '"+sundryCode+"'";
				break;
			case 'P':
				sql = "SELECT EMP_CODE FROM SALES_PERS WHERE SALES_PERS = '"+sundryCode+"'";
				rs = stmt.executeQuery(sql);
				if (rs.next())
				{
					empCode = rs.getString(1);
				}
				if (empCode != null || empCode.trim().length() > 0)
				{
					sql ="SELECT STAN_CODE FROM EMPLOYEE WHERE EMP_CODE = '"+empCode+"'";
				}
				stmt.close();
				break;
			case 'T':
				sql ="SELECT STAN_CODE FROM TRANSPORTER WHERE TRAN_CODE = '"+sundryCode+"'";
				break;
			case 'X':
				sql ="SELECT STAN_CODE FROM TAX_AUTHORITY WHERE TAUTH_CODE = '"+sundryCode+"'";
			}// switch end
			System.out.println("sql :"+sql);
			if (sql != null)
			{
				stmt = conn.createStatement();
				rs = stmt.executeQuery(sql);
				if (rs.next())
				{
					stanCode = rs.getString(1);
				}
				stmt.close();
			}
			System.out.println("stanCode :"+stanCode);
		}// try end
		catch (SQLException sqx)
		{
			System.out.println("The sqlException occure in MiscPayActEJB :"+sqx);
			throw new ITMException(sqx);
		}
		catch (Exception e)
		{
			System.out.println("The Exception occure in MiscPayActEJB :"+e);
			throw new ITMException(e);
		}
		finally
		{
			try
			{
				System.out.println("Closing Connection.....");
				conn.close();
				conn = null;
			}
			catch (Exception e){}
		}
		return stanCode;
	}

	private double bankChargesCalc(String bankCode,String payMode, String tranDate, double payAmt)throws ITMException
	{
		System.out.println("bankCode :"+bankCode+" \n payMode :"+payMode+" \n tranDate :"+tranDate+" \n payAmt :"+payAmt);
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		String sql = "", cumulative = "", round = "", changeType = "";
		double bankCharges = 0, roundTo = 0, minAmt = 0, maxAmt = 0, charges = 0;
		ConnDriver connDriver = new ConnDriver();
		ITMDBAccessEJB itmDBAccessEJB = new ITMDBAccessEJB();
		GenericUtility genericUtility = GenericUtility.getInstance();
		try
		{
			conn = connDriver.getConnectDB("DriverITM");
			//stmt = conn.createStatement();

			sql = "SELECT BANK_CHARGES, "
				 +"CUMULATIVE, "
				 +"ROUND, "
				 +"ROUND_TO, "
				 +"CHARGE_TYPE "
				 +"FROM BANK_CHARGES "
				 +"WHERE BANK_CODE = ? "
				 +"AND PAY_MODE = ? "
				 +"AND EFF_DATE <= ? "
				 +"AND EXP_DATE >= ? "
				 +"AND MIN_AMT <= ? "
				 +"AND MAX_AMT >= ? ";
//			java.sql.Date tranDate1 = java.sql.Date.valueOf(genericUtility.getValidDateString(tranDate,genericUtility.getApplDateFormat(),genericUtility.getDBDateFormat()));
//			System.out.println("tranDate1 :"+tranDate1);
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, bankCode);
			pstmt.setString(2, payMode);
			pstmt.setTimestamp(3, java.sql.Timestamp.valueOf(genericUtility.getValidDateTimeString(tranDate,genericUtility.getApplDateFormat(),genericUtility.getDBDateFormat())));
			pstmt.setTimestamp(4, java.sql.Timestamp.valueOf(genericUtility.getValidDateTimeString(tranDate,genericUtility.getApplDateFormat(),genericUtility.getDBDateFormat())));
//			pstmt.setDate(4, java.sql.Timestamp.valueOf(tranDate));
			pstmt.setDouble(5, payAmt);
			pstmt.setDouble(6, payAmt);
			rs = pstmt.executeQuery();
			if (rs.next())
			{
				bankCharges = rs.getDouble(1);
				cumulative = rs.getString(2);
				round = rs.getString(3);
				roundTo = rs.getDouble(4);
				changeType = rs.getString(5);
			}
			System.out.println("bankCharges :"+bankCharges+" cumulative :"+cumulative+" round :"+round+" roundTo:"+roundTo+" changeType :"+changeType);
			if (changeType.equals("P"))
			{
				bankCharges = (payAmt * (bankCharges / 100));
			}
			System.out.println("bankCharges :"+bankCharges);
			if (cumulative == null || cumulative.trim().length() == 0 || cumulative.equals("N"))
			{
				bankCharges = 0;
				sql = "SELECT MIN_AMT, "
		  			 +"MAX_AMT, "
					 +"CHARGE_TYPE, "
					 +"BANK_CHARGES "
					 +"FROM BANK_CHARGES "
					 +"WHERE BANK_CODE = ? "
					 +"AND PAY_MODE = ? "
					 +"AND EFF_DATE <= ? "
					 +"AND EXP_DATE >= ? "
					 +"AND (MIN_AMT < ? OR ( MIN_AMT < ? AND MAX_AMT > ?)) "
					 +"AND CUMULATIVE = 'Y' "
					 +"ORDER BY MIN_AMT ";
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1, bankCode);
				pstmt.setString(2, payMode);
				pstmt.setTimestamp(3, java.sql.Timestamp.valueOf(genericUtility.getValidDateTimeString(tranDate,genericUtility.getApplDateFormat(),genericUtility.getDBDateFormat())));
				pstmt.setTimestamp(4, java.sql.Timestamp.valueOf(genericUtility.getValidDateTimeString(tranDate,genericUtility.getApplDateFormat(),genericUtility.getDBDateFormat())));
				pstmt.setDouble(5, payAmt);
				pstmt.setDouble(6, payAmt);
				pstmt.setDouble(7, payAmt);
				rs = pstmt.executeQuery();
				while (rs.next())
				{
					minAmt = rs.getDouble(1);
					maxAmt = rs.getDouble(2);
					changeType = rs.getString(3);
					charges = rs.getDouble(4);
					if (charges != 0)
					{
						if (changeType.equals("F"))
						{
							bankCharges = bankCharges + charges;
						}
						else if (changeType.equals("P"))
						{
							if (payAmt > maxAmt)
							{
								bankCharges	= (bankCharges + (charges * ((maxAmt - minAmt) / 100)));
							}
							else
							{
								bankCharges	= (bankCharges + (charges * ((payAmt - minAmt) / 100)));
							}
						}
						else
						{
							return 0;
						}
					}//end if
				}//end while
			}
			if (bankCharges == 0)
			{
				return 0;
			}
			else
			{
				char round1 = round.charAt(0);
				bankCharges = itmDBAccessEJB.getRndamt(bankCharges, round1, roundTo);
			}
		}// end try
		catch (SQLException sqx)
		{
			System.out.println("The sqlException occure in MiscPayActEJB :"+sqx);
			throw new ITMException(sqx);
		}
		catch (Exception e)
		{
			System.out.println("The Exception occure in MiscPayActEJB :"+e);
			throw new ITMException(e);
		}
		finally
		{
			try
			{
				System.out.println("Closing Connection.....");
				conn.close();
				conn = null;
			}
			catch (Exception e){}
		}
		return bankCharges;
	}
}