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

1-	AcctCode should be go in drcr_pacct 	(07052007)

2-

3-					
*/
 
package ibase.webitm.ejb.fin;


import ibase.system.config.AppConnectParm;
import ibase.system.config.ConnDriver;
import ibase.webitm.ejb.ITMDBAccessEJB;
//import ibase.webitm.ejb.MasterStateful; // for ejb3
//import ibase.webitm.ejb.MasterStatefulHome;
import ibase.webitm.ejb.MasterStatefulLocal;
import ibase.webitm.ejb.MasterStatefulEJB;
import ibase.webitm.ejb.ProcessEJB;
import ibase.webitm.ejb.ValidatorEJB;
import ibase.utility.BaseException;
import ibase.utility.GenericUtility;
import ibase.webitm.utility.ITMException;

import java.io.*;
import java.rmi.RemoteException;
import java.sql.*;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.ejb.*;
import javax.naming.InitialContext;
import org.w3c.dom.*;
import javax.ejb.Stateless; // added for ejb3

import ibase.webitm.ejb.fin.FinCommon;
import ibase.webitm.ejb.sys.UtilMethods;
import ibase.webitm.ejb.ITMDBAccessEJB;
@Stateless // added for ejb3

public class PaymentReqInsert extends ProcessEJB implements PaymentReqInsertLocal ,PaymentReqInsertRemote //SessionBean 
{
	
	GenericUtility genericUtility = GenericUtility.getInstance();
	UtilMethods utilMethods = new UtilMethods();
	ITMDBAccessEJB itmDbAccessEJB = new ITMDBAccessEJB();
	
	File filePtr = new File("C:\\pb10\\log\\PaymentRequest.log");
	
	static
	{
		File mkd = new File("C:\\pb10\\log");
		if(!mkd.exists())
		{
			mkd.mkdirs(); 
			System.out.println("Directory Built................ "+mkd);
		}
	}

	/*public void create() throws RemoteException, CreateException {

	}

	public void remove() throws RemoteException {

	}

	public void ejbActivate() {

	}

	public void ejbPassivate() {

	}

	public void setSessionContext(SessionContext mSessionContext) {

	}*/

	public String process(String xmlString, String xmlString2,
			String windowName, String xtraParams) throws RemoteException,
			ITMException {
		String returnString = "";
		Document dom = null, dom1 = null;
		try {
			System.out.println("Process :: \n xmlString : " + xmlString
					+ "\n xmlString2 :" + xmlString2);
			if (xmlString != null) {
				dom = GenericUtility.getInstance().parseString(xmlString);
			}
			if (xmlString2 != null) {
				dom1 = GenericUtility.getInstance().parseString(xmlString2);
			}
			returnString = insertRecord(dom, dom1, windowName, xtraParams);
			System.out.println("Result String => " + returnString);

		} catch (ITMException e) {
			System.out.println("ITMException :" + e);
			e.printStackTrace();
			
			throw e;
		} catch (Exception e) {
			System.out.println("Exception :" + e);
			e.printStackTrace();
			throw new ITMException(e);
		}
		return returnString;
	}

	private String insertRecord(Document dom, Document dom1, String windowName, String xtraParams) throws ITMException
	{
		StringBuffer xmlBuff= new StringBuffer();
		int lineNo = 0;
		String tranIdPorcp="", tranDatePorcp = "", cctrCodeDr = "", siteCode = "";
		String suppCode = "", suppName = "", recDate = "";
		String purcOrder = "", transMode = "", tranName = "", lrNo = "", lrDate = "", dcNo = "", dcDate = "";
		String frtAmount = "", invoiceNo = "", invoiceDate = "", exciseRef = "", ordDate = "";
		String projCode = "", crTerm = "";
		String amountToPay = "", empCodeCust = "", empCodeDivHd = "", remarks = "";
		String custFName = "", custMName = "", custLName = "", divhdFName = "", divhdMName = "", divhdLName = "";
		String xmlString = "", tranIdPayReq = "";
		String retString = null;
		double quantity = 0, rate = 0;
		String errCode = "", errString = "", newMessage = "";
		Connection conn = null;
		ConnDriver connDriver = new ConnDriver();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = null;
				
		try
		{
			conn = connDriver.getConnectDB("DriverITM");
			connDriver = null;
			tranIdPorcp = genericUtility.getColumnValue("tran_id", dom);
			tranDatePorcp = genericUtility.getColumnValue("tran_date", dom);
			purcOrder = genericUtility.getColumnValue("purc_order", dom); 
			suppCode = genericUtility.getColumnValue("supp_code", dom); 
			suppName = genericUtility.getColumnValue("supp_name", dom); 
			dcNo = genericUtility.getColumnValue("dc_no", dom); 
			dcDate = genericUtility.getColumnValue("dc_date", dom);
			tranName = genericUtility.getColumnValue("tran_name", dom);
			recDate = genericUtility.getColumnValue("rec_date", dom);
			exciseRef = genericUtility.getColumnValue("excise_ref", dom);
			lrNo = genericUtility.getColumnValue("lr_no", dom);
			lrDate = genericUtility.getColumnValue("lr_date", dom);
			transMode = genericUtility.getColumnValue("trans_mode", dom);
			frtAmount = genericUtility.getColumnValue("frt_amt", dom);
			invoiceNo = genericUtility.getColumnValue("invoice_no", dom);
			invoiceDate = genericUtility.getColumnValue("invoice_date", dom);
			ordDate = genericUtility.getColumnValue("ord_date", dom);
			projCode = genericUtility.getColumnValue("proj_code", dom);
			crTerm = genericUtility.getColumnValue("cr_term", dom);
			siteCode = genericUtility.getColumnValue("site_code", dom);
			amountToPay = genericUtility.getColumnValue("amount_topay", dom);
			empCodeCust = genericUtility.getColumnValue("emp_code__cust", dom);
			empCodeDivHd = genericUtility.getColumnValue("emp_code__divhd", dom);
			remarks = genericUtility.getColumnValue("remarks", dom);
			custFName = genericUtility.getColumnValue("cust_fname", dom);
			custMName = genericUtility.getColumnValue("cust_mname", dom);
			custLName = genericUtility.getColumnValue("cust_lname", dom);
			divhdFName = genericUtility.getColumnValue("divhd_fname", dom);
			divhdMName = genericUtility.getColumnValue("divhd_mname", dom);
			divhdLName = genericUtility.getColumnValue("divhd_lname", dom);

			xmlBuff.append("<?xml version='1.0' encoding='ISO-8859-1'?>\n");
			xmlBuff.append("<DocumentRoot>");
			xmlBuff.append("<description>").append("Datawindow Root").append("</description>");
			xmlBuff.append("<group0>");
			xmlBuff.append("<description>").append("Group0 description").append("</description>");
			xmlBuff.append("<Header0>");
	
			xmlBuff.append("<objName><![CDATA[").append("payment_req").append("]]></objName>");
			xmlBuff.append("<pageContext><![CDATA[").append("1").append("]]></pageContext>");
			xmlBuff.append("<objContext><![CDATA[").append("1").append("]]></objContext>");
			xmlBuff.append("<editFlag><![CDATA[").append("A").append("]]></editFlag>");
			xmlBuff.append("<focusedColumn><![CDATA[").append("").append("]]></focusedColumn>");
			xmlBuff.append("<action><![CDATA[").append("SAVE").append("]]></action>");
			xmlBuff.append("<elementName><![CDATA[").append("").append("]]></elementName>");
			xmlBuff.append("<keyValue><![CDATA[").append("1").append("]]></keyValue>");
			xmlBuff.append("<taxKeyValue><![CDATA[").append("").append("]]></taxKeyValue>");
			xmlBuff.append("<saveLevel><![CDATA[").append("1").append("]]></saveLevel>");
			xmlBuff.append("<forcedSave><![CDATA[").append(true).append("]]></forcedSave>");
			xmlBuff.append("<taxInFocus><![CDATA[").append(false).append("]]></taxInFocus>");
	
			xmlBuff.append("<description>").append("Header0 members").append("</description>");
			xmlBuff.append("<Detail1 objContext =\"1\"").append(" objName=\"payment_req\" domID=\"1\" dbID=\"\">");
			xmlBuff.append("<attribute selected=\"N\" updateFlag=\"A\" status=\"N\" pkNames=\"\"/>");
			xmlBuff.append("<tran_id/>");
			xmlBuff.append("<tran_date><![CDATA[").append(genericUtility.getValidDateString(new Timestamp(System.currentTimeMillis()).toString(),genericUtility.getDBDateFormat(), genericUtility.getApplDateFormat())).append("]]></tran_date>");
			xmlBuff.append("<tran_id__porcp><![CDATA[").append(tranIdPorcp).append("]]></tran_id__porcp>");
			xmlBuff.append("<amount_topay><![CDATA[").append(amountToPay).append("]]></amount_topay>");
			xmlBuff.append("<remarks><![CDATA[").append(remarks==null?"":remarks).append("]]></remarks>");
			xmlBuff.append("<emp_code__cust><![CDATA[").append(empCodeCust).append("]]></emp_code__cust>");
			xmlBuff.append("<emp_code__divhd><![CDATA[").append(empCodeDivHd).append("]]></emp_code__divhd>");
			xmlBuff.append("<confirmed__cust/>");
			xmlBuff.append("<conf_date__cust/>");
			xmlBuff.append("<confirmed__divhd/>");
			xmlBuff.append("<conf_date__divhd/>");
			xmlBuff.append("<chg_user/>");
			xmlBuff.append("<chg_date/>");
			xmlBuff.append("<chg_term/>");
			xmlBuff.append("<tran_date__porcp><![CDATA[").append(tranDatePorcp).append("]]></tran_date__porcp>");
			xmlBuff.append("<supp_code><![CDATA[").append(suppCode).append("]]></supp_code>");
			xmlBuff.append("<supp_name><![CDATA[").append(suppName).append("]]></supp_name>");
			xmlBuff.append("<rec_date__porcp><![CDATA[").append(recDate==null?"":recDate).append("]]></rec_date__porcp>");
			xmlBuff.append("<purc_order><![CDATA[").append(purcOrder).append("]]></purc_order>");
			xmlBuff.append("<trans_mode><![CDATA[").append(transMode==null?"":transMode).append("]]></trans_mode>");
			xmlBuff.append("<tran_name><![CDATA[").append(tranName==null?"":tranName).append("]]></tran_name>");
			xmlBuff.append("<lr_no><![CDATA[").append(lrNo==null?"":lrNo).append("]]></lr_no>");
			xmlBuff.append("<lr_date><![CDATA[").append(lrDate==null?"":lrDate).append("]]></lr_date>");
			xmlBuff.append("<dc_no><![CDATA[").append(dcNo==null?"":dcNo).append("]]></dc_no>");
			xmlBuff.append("<dc_date><![CDATA[").append(dcDate==null?"":dcDate).append("]]></dc_date>");
			xmlBuff.append("<frt_amt><![CDATA[").append(frtAmount==null?"0":frtAmount).append("]]></frt_amt>");
			xmlBuff.append("<invoice_no><![CDATA[").append(invoiceNo==null?"":invoiceNo).append("]]></invoice_no>");
			xmlBuff.append("<invoice_date><![CDATA[").append(invoiceDate==null?"":invoiceDate).append("]]></invoice_date>");
			xmlBuff.append("<excise_ref><![CDATA[").append(exciseRef==null?"":exciseRef).append("]]></excise_ref>");
			xmlBuff.append("<ord_date><![CDATA[").append(ordDate==null?"":ordDate).append("]]></ord_date>");
			xmlBuff.append("<proj_code><![CDATA[").append(projCode==null?"":projCode).append("]]></proj_code>");
			xmlBuff.append("<cr_term><![CDATA[").append(crTerm==null?"":crTerm).append("]]></cr_term>");
			xmlBuff.append("<cust_fname><![CDATA[").append(custFName==null?"":custFName).append("]]></cust_fname>");
			xmlBuff.append("<cust_mname><![CDATA[").append(custMName==null?"":custMName).append("]]></cust_mname>");
			xmlBuff.append("<cust_lname><![CDATA[").append(custLName==null?"":custLName).append("]]></cust_lname>");
			xmlBuff.append("<divhd_fname><![CDATA[").append(divhdFName==null?"":divhdFName).append("]]></divhd_fname>");
			xmlBuff.append("<divhd_mname><![CDATA[").append(divhdMName==null?"":divhdMName).append("]]></divhd_mname>");
			xmlBuff.append("<divhd_lname><![CDATA[").append(divhdLName==null?"":divhdLName).append("]]></divhd_lname>");
			
			sql = " SELECT CCTR_CODE__DR FROM PORCPDET WHERE TRAN_ID = ? ";
			
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, tranIdPorcp);
			rs = pstmt.executeQuery();
			
			if (rs.next())
			{
					cctrCodeDr = rs.getString(1);
					System.out.println("cctrCodeDr :::: " + cctrCodeDr);
					xmlBuff.append("<cctr_code__dr><![CDATA[").append(cctrCodeDr).append("]]></cctr_code__dr>");
					xmlBuff.append("</Detail1>");
			}
			else
			{
					xmlBuff.append("</Detail1>");
			}

			
			sql = " SELECT A.LINE_NO, A.ITEM_CODE, B.DESCR, A.QUANTITY, A.UNIT, A.RATE, A.LOC_CODE, "
				+ " C.DLV_DATE FROM PORCPDET A, ITEM B, PORDDET C "
				+ " WHERE (A.ITEM_CODE = B.ITEM_CODE) AND (A.PURC_ORDER = C.PURC_ORDER) AND (C.LINE_NO = A.LINE_NO__ORD)"
				+ " AND ((A.TRAN_ID = ?))";
			
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, tranIdPorcp);
			rs = pstmt.executeQuery();
			
			while (rs.next())
			{
				lineNo = lineNo+1;
				quantity = rs.getDouble(4);
				rate = rs.getDouble(6);
				xmlBuff.append("<Detail2 objContext=\"2\" objName=\"payment_req\" domID=\"").append(lineNo).append("\" dbID=''>");
				xmlBuff.append("<attribute selected=\"N\" updateFlag=\"A\" status=\"N\" pkNames=\"\"/>");
				xmlBuff.append("<tran_id/>");
				xmlBuff.append("<line_no><![CDATA[").append(lineNo).append("]]></line_no>");
				xmlBuff.append("<line_no__porcp><![CDATA[").append(rs.getString(1)).append("]]></line_no__porcp>");
				xmlBuff.append("<item_code><![CDATA[").append(rs.getString(2)).append("]]></item_code>");
				xmlBuff.append("<item_descr><![CDATA[").append(rs.getString(3)).append("]]></item_descr>");
				xmlBuff.append("<quantity><![CDATA[").append(quantity).append("]]></quantity>");				
				xmlBuff.append("<unit><![CDATA[").append(rs.getString(5)).append("]]></unit>");
				xmlBuff.append("<rate><![CDATA[").append(rate).append("]]></rate>");
				xmlBuff.append("<loc_code><![CDATA[").append(rs.getString(7)).append("]]></loc_code>");
				xmlBuff.append("<dlv_date><![CDATA[").append(rs.getString(8)== null?"":genericUtility.getValidDateString(rs.getTimestamp(8).toString(),genericUtility.getDBDateFormat(), genericUtility.getApplDateFormat())).append("]]></dlv_date>");
				xmlBuff.append("<amount><![CDATA[").append(quantity*rate).append("]]></amount>");				
				xmlBuff.append("</Detail2>");
			}
			rs.close();
			pstmt.close();

			xmlBuff.append("</Header0>");
			xmlBuff.append("</group0>");
			xmlBuff.append("</DocumentRoot>");
			xmlString = xmlBuff.toString();	
			retString = saveData(siteCode,xmlString);
			
			if (retString.indexOf("Success") > -1)
			{
				tranIdPayReq = retString.substring(retString.indexOf("<TranID>") + 8, retString.indexOf("</TranID>"));
				sql = "UPDATE PORCP SET WORKFLOW_STATUS ='1' WHERE TRAN_ID = ? " ;
				pstmt=conn.prepareStatement(sql);
				pstmt.setString(1,tranIdPorcp);
				int update = pstmt.executeUpdate();
				if (update == 1)
				{
					errCode = "VTPRINS";
					errString = itmDbAccessEJB.getErrorString("", errCode, "", "", conn);
					newMessage = "PAYMENT REQUEST NO. ::: " + tranIdPayReq + " IS GENERATED";
					errString = updateMessage(errString, newMessage);
					return errString;
				}
			}
			else
			{
				errCode = "VTINSFL";
				errString = itmDbAccessEJB.getErrorString("", errCode, "", "", conn);
				return errString;
			}
			
		}
		catch (ITMException ie)
		{
			System.out.println(ie.getMessage());
			ie.printStackTrace();
		}
		catch (Exception e)
		{
			System.out.println(e.getMessage());
			e.printStackTrace();
		}
		finally
		{
			try
			{
				conn.close(); conn = null;
			}
			catch(Exception ex){}
		}
		return retString;
	}//end of function

	public String insertVoucher(String xmlDataAll)throws RemoteException, ITMException
	{
		Document dom = null;
		NodeList headerList = null;
		Node header = null;
		int ctr, rndTo = 0;
		Connection conn = null;
		ConnDriver connDriver = new ConnDriver();
		PreparedStatement pstmt = null, pstmt1 = null;
		ResultSet rs = null, rs1 = null;
		String sql = null, xmlString = "";
		

		String retString = "";
		String tranIdPorcp = "", pOType = "", convert = "", tranId = "";  //VARIABLE tranId ADDED BY ALKA 24/08/07
		String acctStr = "", acctCodeAp = "", cctrCodeAp = "", acctCodePr = "", cctrCodePr = "";
		String acctCodeCf = "", cctrCodeCf = "", acctCodeCfAp = "", cctrCodeCfAp = "";
		int cnt = 0, lineNo = 0;
		String payMode = "", suppCodePay = "", finEntity = "", bankCode = "", dueDate = "";
		String tranDatePorcp = "", suppCode = "", purcOrder = "", tranDate = "";
		String invoiceNo = "", invoiceDate = "", dcNo = "", dcDate = "";
		String projCode = "", crTerm = "", remarks = "", ledgPostConf = "";
		String rndOff = "", rndStr = "";//, autoConf = "", confirmed = "";
		String tranType= "", effDate= "", currCode= "", taxDate= "", tranSer= "", siteCode= "";
		String retOpt = "", postType = "", itemSer = "", invOnline = "", rcpRetId = "";
		double actAmount= 0, discount= 0, tax= 0, exchRate= 0;
		double totAmt = 0, netAmt = 0, netAmtBase = 0;
		String rcpLineNo = "", purcOrdRcp = "", purcOrdLine = "", rcpItem = "", rcpUnit = "";
		String acctCodeDr = "", cctrCodeDr = "", deptCode = "", acctCodeCr = "", cctrCodeCr = "", rateStr = ""; 
		double rcpQty = 0,	rcpRate = 0, rcpDiscount = 0, rcpTax = 0, rcpNet = 0, rcpQtyStd = 0;
		double rcpRateStd = 0, purcRateStd = 0, purcRate = 0, purcDisc = 0, sumRcpTax = 0, netAmtAdj = 0;
		String acctCodePh = "", cctrCodePh = "", taxVar = "", taxVar1 = "", taxVar2 = "", taxClass = "";
		String taxChap = "", taxEnv = "", rateClg = "", stdRate = "", acctCodeDiff = "", cctrCodeDiff = "";
		double rcpDiscAmt = 0, rcpAmount = 0, vouchQuantity = 0, vouchAmount = 0, sumVouchAmt = 0, sumNetAmt = 0;
		String refSer = "", refNo = "", currAdv = "", acctCodeAdv = "", cctrCodeAdv = "";
		String sType = "", sCode = "";
		double totAmtAdv = 0, advAmt = 0, advAdj = 0, exchAdv = 0, diffExch = 0, diffTot = 0, advTot = 0;

		
		StringBuffer xmlBuff = new StringBuffer();

		System.out.println("xmlDataAll ::: " + xmlDataAll);
		writeLog(filePtr,"xmlDataAll ::: " + xmlDataAll,true); //ADDED BY ALKA 24/08/07 FOR SOPs TO BE ADDED IN LOG FILE
		FinCommon finCommon = new FinCommon();
		ValidatorEJB validatorEJB = new ValidatorEJB();
		try
		{
			conn = connDriver.getConnectDB("DriverITM");
			connDriver = null;
			dom =  genericUtility.parseString(xmlDataAll);
			headerList = dom.getElementsByTagName("Detail1");			
			int noOfDetails = headerList.getLength();
			for (ctr = 0; ctr < noOfDetails; ctr ++)
			{
				header = headerList.item(ctr);
				tranId = genericUtility.getColumnValueFromNode("tran_id", header);  //ADDED BY ALKA 24/08/07 TO BE ADDED IN LOG FILE
				writeLog(filePtr,"[" + new Timestamp(System.currentTimeMillis()) + "] Payment Request No. ::::: " + tranId,true); //ADDED BY ALKA 24/08/07 FOR WRITING IN LOG FILE FOR EXCEPTION THROWN
				System.out.println("Payment Request No. ::::: " + tranId); //ADDED BY ALKA 24/08/07 FOR SOPs TO BE ADDED IN LOG FILE
				tranIdPorcp = genericUtility.getColumnValueFromNode("tran_id__porcp", header);
				System.out.println("tranIdPorcp ::::: " + tranIdPorcp);
				writeLog(filePtr,"tranIdPorcp ::::: " + tranIdPorcp,true); //ADDED BY ALKA 24/08/07 FOR SOPs TO BE ADDED IN LOG FILE
				purcOrder = genericUtility.getColumnValueFromNode("purc_order", header); 
				suppCode = genericUtility.getColumnValueFromNode("supp_code", header); 
				dcNo = genericUtility.getColumnValueFromNode("dc_no", header); 
				dcDate = genericUtility.getColumnValueFromNode("dc_date", header);
				projCode = genericUtility.getColumnValueFromNode("proj_code", header);
				if (projCode == null) projCode = "";
				crTerm = genericUtility.getColumnValueFromNode("cr_term", header);
				tranDatePorcp = genericUtility.getColumnValueFromNode("tran_date__porcp", header);
				invoiceDate = genericUtility.getColumnValueFromNode("invoice_date", header);
 
				rndOff = finCommon.getFinparams("999999", "VOUCH-RND", conn);
				if (rndOff != null && !rndOff.equalsIgnoreCase("NULLFOUND"))
				{
					rndStr = finCommon.getFinparams("999999", "VOUCH-RNDTO", conn);
					if (rndStr != null && !rndStr.equalsIgnoreCase("NULLFOUND"))
					{
						rndTo = Integer.parseInt(rndStr);
					}
				}
				else
				{
					rndOff = "";
				}
/*NOT TO BE ADDED AS UNCONFIRMED VOUCHER TO BE GENERATED
 * //				ADDED BY ALKA -- GNL CODE 09/06/07
				autoConf = finCommon.getFinparams("999999", "AUTO_CONF_VOUCH", conn);
				if (autoConf != null && autoConf.equalsIgnoreCase("Y"))
				{
					confirmed = "Y";
				}
				else
				{
					confirmed = "N";
				}
//				ENDED --------- ADDED BY ALKA -- GNL CODE 09/06/07
*/	
				sql = " SELECT TRAN_TYPE, EFF_DATE, AMOUNT, (CASE WHEN DISCOUNT IS NULL THEN 0 ELSE DISCOUNT END), "
					+ " TAX, CURR_CODE, EXCH_RATE, TAX_DATE, TRAN_SER, SITE_CODE, "
					+ " CASE WHEN RET_OPT IS NULL THEN ' ' ELSE RET_OPT END, POST_TYPE, REMARKS FROM PORCP "
					+ " WHERE TRAN_ID = ? AND CONFIRMED = 'Y' AND (VOUCH_CREATED IS NULL OR VOUCH_CREATED = 'N') ";
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1, tranIdPorcp);
				rs = pstmt.executeQuery();
				if (rs.next())
				{
					tranType = rs.getString(1) == null ? "": rs.getString(1);
					effDate = genericUtility.getValidDateString(rs.getTimestamp(2).toString(), genericUtility.getDBDateFormat(), genericUtility.getApplDateFormat());
					actAmount = rs.getDouble(3);
					discount = rs.getDouble(4);
//					tax = rs.getDouble(5);   //COMMENTED AS TAX NOT TO BE CONSIDERED.
					currCode  = rs.getString(6);
					exchRate = rs.getDouble(7);
					taxDate = genericUtility.getValidDateString(rs.getTimestamp(8).toString(), genericUtility.getDBDateFormat(), genericUtility.getApplDateFormat());
					tranSer = rs.getString(9);
					siteCode = rs.getString(10);
					retOpt = rs.getString(11);
					postType = rs.getString(12);
					remarks = rs.getString(13);
				}
				else
				{
					System.out.println("No records found for PORCP No. ::: " + tranIdPorcp);
					retString = "F";
					writeLog(filePtr,"No records found for PORCP No. ::: " + tranIdPorcp,true); //ADDED BY ALKA 22/08/07 FOR WRITING IN LOG FILE FOR EXCEPTION THROWN
					throw new Exception("No records found for PORCP No. ::: " + tranIdPorcp);
				}
				rs.close();
				pstmt.close();

				/* COMMENTED AS NOT INCLUDED IN GNL CODE
				 * if (tranSer.equalsIgnoreCase("P-RET") && retOpt.equals("R"))
				{
					System.out.println("Invalid Voucher Creation for Replacement for tranSer ::: " + tranSer);
					retString = "F";
					throw new Exception("Invalid Voucher Creation for Replacement for tranSer ::: " + tranSer);
				}*/
	
				sql = "	SELECT PORD_TYPE FROM PORDER WHERE PURC_ORDER = ? ";  //cr_terms, projCode
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1, purcOrder);
				rs = pstmt.executeQuery();
				if (rs.next())
				{
					pOType = rs.getString(1);
				}
				else
				{
					System.out.println("P. O. Type not found for purcOrder :::: " + purcOrder);
					retString = "F";
					writeLog(filePtr,"P. O. Type not found for purcOrder :::: " + purcOrder,true); //ADDED BY ALKA 22/08/07 FOR WRITING IN LOG FILE FOR EXCEPTION THROWN
					throw new Exception("P. O. Type not found for purcOrder :::: " + purcOrder);
				}
				rs.close();
				pstmt.close();
	
//				actAmount = actAmount - tax;  //COMMENTED AS TAX NOT TO BE CONSIDERED. 
		
				sql = "	SELECT ITEM_SER, ACCT_INV_ONLINE FROM PORCP WHERE TRAN_ID = ?";
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1, tranIdPorcp);
				rs = pstmt.executeQuery();
				if (rs.next())
				{
					itemSer = rs.getString(1);
					invOnline = rs.getString(2) == null ? "N" : rs.getString(2);
				}
				rs.close();
				pstmt.close();
	
//				MODIFIED AND PUT THE ELSE TO TAKE A DIFF. CREDIT ACCOUNT IN CASE THE INVENTORY ACCOUNTING
				if (invOnline.equalsIgnoreCase("Y"))
				{
					acctStr = finCommon.getAcctDetrTtype("",itemSer, "PORCP", pOType, conn);
					if (acctStr != null && acctStr.trim().length() > 0)
					{
						acctCodePr = acctStr.substring(0,acctStr.indexOf(","));
						cctrCodePr = acctStr.substring(acctStr.indexOf(",")+1);
					}
					
					sql = "SELECT ACCT_CODE__AP, CCTR_CODE__AP FROM SUPPLIER WHERE SUPP_CODE = ? " ;
					pstmt = conn.prepareStatement(sql);
					pstmt.setString(1, suppCode);
					rs = pstmt.executeQuery();
					if (rs.next())
					{
						acctCodeAp = rs.getString(1);
						cctrCodeAp = rs.getString(2);
					}
					rs.close();
					pstmt.close();
		
					if (acctCodeAp == null || acctCodeAp.trim().length() != 0)
					{
						acctStr = finCommon.getAcctDetrTtype("",itemSer, "POVOU", pOType, conn);
						if (acctStr != null && acctStr.trim().length() > 0)
						{
							acctCodeAp = acctStr.substring(0,acctStr.indexOf(","));
							cctrCodeAp = acctStr.substring(acctStr.indexOf(",")+1);
						}
					}
				}
				else
				{
					sql = " SELECT COUNT(DISTINCT ACCT_CODE__CR) FROM PORCPDET WHERE TRAN_ID = ? ";
					pstmt = conn.prepareStatement(sql);
					pstmt.setString(1, tranIdPorcp);
					rs = pstmt.executeQuery();
					if (rs.next())
					{
						cnt = rs.getInt(1);
					}
					rs.close();
					pstmt.close();
					if (cnt == 1)
					{
						sql =	" SELECT ACCT_CODE__CR, CCTR_CODE__CR FROM PORCPDET WHERE TRAN_ID = ? ";
						pstmt = conn.prepareStatement(sql);
						pstmt.setString(1, tranIdPorcp);
						rs = pstmt.executeQuery();
						if (rs.next())
						{
							acctCodeAp = rs.getString(1);
							cctrCodeAp = rs.getString(2);
						}
						rs.close();
						pstmt.close();
						if (acctCodeAp == null || acctCodeAp.trim().length() == 0)
						{
							cnt = 2;
						}
					}
					if (cnt > 1)
					{
						acctStr = finCommon.getAcctDetrTtype("",itemSer, "POVOU", pOType, conn);
						if (acctStr != null && acctStr.trim().length() > 0)
						{
							acctCodeAp = acctStr.substring(0,acctStr.indexOf(","));
							cctrCodeAp = acctStr.substring(acctStr.indexOf(",")+1);
						}
					}
					cnt = 0;
					
					if (acctCodeAp == null || acctCodeAp.trim().length() == 0)
					{
						System.out.println("Account code entered does not exists in account master. ~ for AP Account");
						retString = "F";
						writeLog(filePtr,"Account code entered does not exists in account master. ~ for AP Account",true); //ADDED BY ALKA 22/08/07 FOR WRITING IN LOG FILE FOR EXCEPTION THROWN
						throw new Exception("Account code entered does not exists in account master. ~ for AP Account");
					}
				}
				if (cctrCodeAp == null || cctrCodeAp.trim().length() == 0)
				{
					sql =	" SELECT CCTR_CODE__CR FROM PORCPDET WHERE TRAN_ID = ? ";
					pstmt = conn.prepareStatement(sql);
					pstmt.setString(1, tranIdPorcp);
					rs = pstmt.executeQuery();
					if (rs.next())
					{
						cctrCodeAp = rs.getString(1)== null ? " ":rs.getString(1);
					}
					rs.close();
					pstmt.close();
				}
				
				if (cctrCodeAp == null || cctrCodeAp.trim().length() == 0)
				{
					cctrCodeAp = " ";
				}
		
				sql =	" SELECT PAY_MODE, SUPP_CODE__PAY FROM SUPPLIER WHERE SUPP_CODE = ? ";
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1, suppCode);
				rs = pstmt.executeQuery();
				if (rs.next())
				{
					payMode = rs.getString(1)== null ? "":rs.getString(1);
					suppCodePay = rs.getString(2)== null ?suppCode:rs.getString(2);
				}
				rs.close();
				pstmt.close();
		
				sql =	" SELECT FIN_ENTITY, BANK_CODE FROM SITE WHERE SITE_CODE = ? ";
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1, siteCode);
				rs = pstmt.executeQuery();
				if (rs.next())
				{
					finEntity = rs.getString(1)== null ? "":rs.getString(1);
					bankCode = rs.getString(2)== null ? "":rs.getString(2);
				}
				rs.close(); rs = null;
				pstmt.close(); pstmt = null;
	
				sql =	" SELECT LEDG_POST_CONF FROM TRANSETUP WHERE TRAN_WINDOW = 'w_voucher' ";
				pstmt = conn.prepareStatement(sql);
				rs = pstmt.executeQuery();
				if (rs.next())
				{
					ledgPostConf = rs.getString(1)== null ? "N":rs.getString(1);
				}
				else
				{
					sql = " SELECT LEDG_POST_CONF FROM TRANSETUP WHERE TRAN_WINDOW = 'GENERAL' ";
					pstmt1 = conn.prepareStatement(sql);
					rs1 = pstmt1.executeQuery();
					if (rs1.next())
					{
						ledgPostConf = rs1.getString(1)== null ? "N":rs1.getString(1);
					}
					else
					{
						System.out.println("Ledger Posting Conf not declared in transetup for voucher or GENERAL window");
						retString = "F";
						writeLog(filePtr,"Ledger Posting Conf not declared in transetup for voucher or GENERAL window",true); //ADDED BY ALKA 22/08/07 FOR WRITING IN LOG FILE FOR EXCEPTION THROWN
						throw new Exception("Ledger Posting Conf not declared in transetup for voucher or GENERAL window");
					}
					rs1.close(); rs1 = null;
					pstmt1.close(); pstmt1 = null;
				}
				rs.close(); rs = null;
				pstmt.close(); pstmt = null;
				
				SimpleDateFormat sdf = new SimpleDateFormat(genericUtility.getApplDateFormat());
				tranDate = tranDatePorcp;
				if (ledgPostConf.equalsIgnoreCase("Y"))
				{
					tranDate = sdf.format(new Date()).toString();
				}
				
//				UPDATING VOUCHER HEADER
				if (tranSer.equalsIgnoreCase("P-RET"))
				{
					actAmount = 0 - actAmount;
					tax = 0 - tax;
					discount = 0 - discount;
					dueDate = tranDate;
				}
				else
				{
					System.out.println("tranDatePorcp :: " + tranDatePorcp + " ::: effDate  ::: " + effDate + " :::: tranDate :::: " + tranDate);
					writeLog(filePtr,"tranDatePorcp :: " + tranDatePorcp + " ::: effDate  ::: " + effDate + " :::: tranDate :::: " + tranDate,true); //ADDED BY ALKA 24/08/07 FOR SOPs TO BE ADDED IN LOG FILE
					dueDate =  genericUtility.getValidDateString(calcDueDate(crTerm, tranDatePorcp, effDate, tranDate, conn),genericUtility.getApplDateFormat());
				}
				if (remarks == null || remarks.trim().length() == 0)
				{
					remarks = "Against purchase receipt no. : " + tranIdPorcp;
				}
				
				totAmt = actAmount;
				netAmt = totAmt;
				netAmtBase = totAmt * exchRate;
			
/*COMMENTED BECAUSE THIS PARAMETER NOT AVAILABLE IN GNL CODE
 * //				IF INV_TO_DC = 'Y', ONLY THEN THE DC_NO WILL BE SET 
				convert = finCommon.getFinparams("999999", "INV_TO_DC", conn);
				if (convert != null && convert.equalsIgnoreCase("Y"))
				{*/
					if (invoiceNo == null || invoiceNo.trim().length() == 0)
					{
						invoiceNo = dcNo;
					}
					if (invoiceDate == null || invoiceDate.trim().length() == 0)
					{
						invoiceDate = dcDate;
					}
/*				}
					
				sql = " SELECT COUNT(*) FROM VOUCHER WHERE LTRIM(RTRIM(BILL_NO)) = ? AND SUPP_CODE = ? AND TRAN_ID <> ? ";
				if (invoiceDate != null)
				{
					sql = sql + " AND BILL_DATE = ?  ";
				}
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1, invoiceNo);
				pstmt.setString(2, suppCode);
				pstmt.setString(3, tranIdPorcp);
				if (invoiceDate != null)
				{
					pstmt.setTimestamp(4, Timestamp.valueOf(genericUtility.getValidDateTimeString(invoiceDate,genericUtility.getApplDateFormat(), genericUtility.getDBDateFormat())));
				}
				
				rs = pstmt.executeQuery();
				if (rs.next())
				{
					cnt = rs.getInt(1);
				}
				rs.close();
				pstmt.close();
		
				if (cnt > 0)
				{
					System.out.println("Voucher already exist for invoice no. :::: " + invoiceNo);
					retString = "F";
					throw new Exception("Voucher already exist for invoice no. :::: " + invoiceNo);
				}
			
				sql =	" SELECT ACCT_CODE__CF, ACCT_CODE__CF_AP, CCTR_CODE__CF, CCTR_CODE__CF_AP	FROM BANK	WHERE BANK_CODE = ? ";
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1, bankCode);
				rs = pstmt.executeQuery();
				if (rs.next())
				{
					acctCodeCf = rs.getString(1);
					acctCodeCfAp = rs.getString(2);
					cctrCodeCf = rs.getString(3);
					cctrCodeCfAp = rs.getString(4);
				}
				rs.close();
				pstmt.close();
				
				if (acctCodeCfAp == null || acctCodeCfAp.trim().length() == 0)
				{
					acctCodeCfAp = acctCodeCf;
				}
				if (cctrCodeCfAp == null || cctrCodeCfAp.trim().length() == 0)
				{
					cctrCodeCfAp = cctrCodeCf;
				}
*/			
				xmlBuff.append("<?xml version='1.0' encoding='ISO-8859-1'?>\n");
				xmlBuff.append("<DocumentRoot>");
				xmlBuff.append("<description>").append("Datawindow Root").append("</description>");
				xmlBuff.append("<group0>");
				xmlBuff.append("<description>").append("Group0 description").append("</description>");
				xmlBuff.append("<Header0>");
		
				xmlBuff.append("<objName><![CDATA[").append("voucher").append("]]></objName>");
				xmlBuff.append("<pageContext><![CDATA[").append("1").append("]]></pageContext>");
				xmlBuff.append("<objContext><![CDATA[").append("1").append("]]></objContext>");
				xmlBuff.append("<editFlag><![CDATA[").append("A").append("]]></editFlag>");
				xmlBuff.append("<focusedColumn><![CDATA[").append("").append("]]></focusedColumn>");
				xmlBuff.append("<action><![CDATA[").append("SAVE").append("]]></action>");
				xmlBuff.append("<elementName><![CDATA[").append("").append("]]></elementName>");
				xmlBuff.append("<keyValue><![CDATA[").append("1").append("]]></keyValue>");
				xmlBuff.append("<taxKeyValue><![CDATA[").append("").append("]]></taxKeyValue>");
				xmlBuff.append("<saveLevel><![CDATA[").append("1").append("]]></saveLevel>");
				xmlBuff.append("<forcedSave><![CDATA[").append(true).append("]]></forcedSave>");
				xmlBuff.append("<taxInFocus><![CDATA[").append(false).append("]]></taxInFocus>");
				xmlBuff.append("<description>").append("Header0 members").append("</description>");
			
				xmlBuff.append("<Detail1 objContext =\"1\"").append(" objName=\"voucher\" domID=\"1\" dbID=\"\">");
				xmlBuff.append("<attribute selected=\"N\" updateFlag=\"A\" status=\"N\" pkNames=\"\"/>");
				xmlBuff.append("<tran_id/>");
				xmlBuff.append("<tran_date><![CDATA[").append(tranDate).append("]]></tran_date>");
//				xmlBuff.append("<tran_type><![CDATA[").append(tranType).append("]]></tran_type>");
				xmlBuff.append("<eff_date><![CDATA[").append(tranDatePorcp).append("]]></eff_date>");
				xmlBuff.append("<supp_code><![CDATA[").append(suppCode).append("]]></supp_code>"); //CHANGED TO SUPPCODE INSTEAD OF SUPPCODEPAY AS IN GNL CODE
				xmlBuff.append("<bill_no><![CDATA[").append(invoiceNo==null?"":invoiceNo).append("]]></bill_no>");
				xmlBuff.append("<bill_date><![CDATA[").append(invoiceDate==null?"":invoiceDate).append("]]></bill_date>");
				xmlBuff.append("<purc_order><![CDATA[").append(purcOrder).append("]]></purc_order>");
				xmlBuff.append("<curr_code><![CDATA[").append(currCode).append("]]></curr_code>");
				xmlBuff.append("<exch_rate><![CDATA[").append(exchRate).append("]]></exch_rate>");
				xmlBuff.append("<acct_code><![CDATA[").append(acctCodeAp).append("]]></acct_code>");
				xmlBuff.append("<cctr_code><![CDATA[").append(cctrCodeAp).append("]]></cctr_code>");
				xmlBuff.append("<bank_code><![CDATA[").append(bankCode).append("]]></bank_code>");
				xmlBuff.append("<auto_pay><![CDATA[").append("Y").append("]]></auto_pay>");
				xmlBuff.append("<adv_amt><![CDATA[").append("0").append("]]></adv_amt>");
				xmlBuff.append("<cr_term><![CDATA[").append(crTerm==null?"":crTerm).append("]]></cr_term>");
				xmlBuff.append("<due_date><![CDATA[").append(dueDate).append("]]></due_date>");
				xmlBuff.append("<chg_user/>");
				xmlBuff.append("<chg_date/>");
				xmlBuff.append("<chg_term/>");
				xmlBuff.append("<site_code><![CDATA[").append(siteCode).append("]]></site_code>");
				xmlBuff.append("<fin_entity><![CDATA[").append(finEntity).append("]]></fin_entity>");
				xmlBuff.append("<bill_amt><![CDATA[").append(actAmount).append("]]></bill_amt>");
				xmlBuff.append("<tax_amt><![CDATA[").append(tax).append("]]></tax_amt>");
				xmlBuff.append("<tot_amt><![CDATA[").append(totAmt).append("]]></tot_amt>");
				xmlBuff.append("<tax_date><![CDATA[").append(taxDate).append("]]></tax_date>");
				xmlBuff.append("<vouch_type><![CDATA[").append("P").append("]]></vouch_type>");
				xmlBuff.append("<proj_code><![CDATA[").append(projCode==null?"":projCode).append("]]></proj_code>");			
				xmlBuff.append("<confirmed><![CDATA[").append("N").append("]]></confirmed>");
				xmlBuff.append("<pay_mode><![CDATA[").append(payMode).append("]]></pay_mode>");
				xmlBuff.append("<net_amt><![CDATA[").append(netAmt).append("]]></net_amt>");			
				xmlBuff.append("<net_amt__bc><![CDATA[").append(netAmtBase).append("]]></net_amt__bc>");			
				xmlBuff.append("<diff_amt__exch><![CDATA[").append("0").append("]]></diff_amt__exch>");	
				xmlBuff.append("<supp_bill_amt><![CDATA[").append(actAmount).append("]]></supp_bill_amt>");		
				xmlBuff.append("<remarks><![CDATA[").append(remarks).append("]]></remarks>");
				xmlBuff.append("<rnd_off><![CDATA[").append(rndOff).append("]]></rnd_off>");			
				xmlBuff.append("<rnd_to><![CDATA[").append(rndTo).append("]]></rnd_to>");
//				xmlBuff.append("<tran_mode><![CDATA[").append("A").append("]]></tran_mode>");
//				xmlBuff.append("<acct_code__cf><![CDATA[").append(acctCodeCfAp).append("]]></acct_code__cf>");
//				xmlBuff.append("<cctr_code__cf><![CDATA[").append(cctrCodeCfAp).append("]]></cctr_code__cf>");
				xmlBuff.append("<tran_id__rcp><![CDATA[").append(tranIdPorcp).append("]]></tran_id__rcp>");
				xmlBuff.append("</Detail1>");
		
//				UPDATING VOUCHER DETAIL FOR RECEIPT
				sql = " SELECT LINE_NO, PURC_ORDER, LINE_NO__ORD, ITEM_CODE, QUANTITY, UNIT, RATE, CASE WHEN "
					+ " DISCOUNT IS NULL THEN 0 ELSE DISCOUNT END, TAX_AMT, NET_AMT, QUANTITY__STDUOM, " 
					+ " RATE__STDUOM, ACCT_CODE__DR, CCTR_CODE__DR, DEPT_CODE, ACCT_CODE__CR, CCTR_CODE__CR  "
					+ " FROM PORCPDET WHERE TRAN_ID = ? ";			
				System.out.println("sql ::: " + sql + " ---------- tranIdPorcp ::::: " + tranIdPorcp);	
				writeLog(filePtr,"sql ::: " + sql + " ---------- tranIdPorcp ::::: " + tranIdPorcp,true); //ADDED BY ALKA 24/08/07 FOR SOPs TO BE ADDED IN LOG FILE
				pstmt1 = conn.prepareStatement(sql);	
				pstmt1.setString(1, tranIdPorcp);
				rs1 = pstmt1.executeQuery();
		
				while (rs1.next())
				{
					System.out.println("lineNo for vouchRcp ::" + lineNo + ":");
					writeLog(filePtr,"lineNo for vouchRcp ::" + lineNo + ":",true); //ADDED BY ALKA 24/08/07 FOR SOPs TO BE ADDED IN LOG FILE
					rcpLineNo = rs1.getString(1);
					purcOrdRcp = rs1.getString(2);
					purcOrdLine = rs1.getString(3);
					rcpItem = rs1.getString(4);
					rcpQty = rs1.getDouble(5);
					rcpUnit = rs1.getString(6);
					rcpRate = rs1.getDouble(7);
					rcpDiscount = rs1.getDouble(8);
//					rcpTax = rs1.getDouble(9);  //commented by Alka as tax is not to be picked up from porcpdet
					rcpNet = rs1.getDouble(10);
					rcpQtyStd = rs1.getDouble(11);
					rcpRateStd = rs1.getDouble(12);
					acctCodeDr = rs1.getString(13);
					cctrCodeDr = rs1.getString(14);
					rateClg = rs1.getString(15);
					deptCode = rs1.getString(15)== null ? "":rs1.getString(15);
					acctCodeCr = rs1.getString(16);
					cctrCodeCr = rs1.getString(17);
	
					lineNo = lineNo + 1;
	
					/*commented by Alka 09/06/07 because not in GNL code for porcp voucher
					 * rateStr = getPurcRate(purcOrdRcp, rcpItem, conn);
					if (rateStr.indexOf(",") > -1)
					{
						purcRate = Double.parseDouble(rateStr.substring(0,rateStr.indexOf(",")));
						purcDisc = Double.parseDouble(rateStr.substring(rateStr.indexOf(",")+1));
						if (purcRate != -999)
						{
							purcRateStd = rcpRateStd * purcRate / rcpRate;
							rcpRate = purcRate;
							rcpRateStd = purcRateStd;
							rcpDiscount = purcDisc;
						}
					}
					else
					{
						System.out.println("Purchase Rate not found in getPurcRate for purcOrdRcp :::  " + purcOrdRcp);
						retString = "F";
						throw new Exception("Purchase Rate not found in getPurcRate for purcOrdRcp :::  " + purcOrdRcp);
					}
*/
					if (invOnline.equalsIgnoreCase("Y"))
					{
						acctCodePh = acctCodePr;
						
						if ((cctrCodePh == null || cctrCodePh.trim().length() == 0) && (cctrCodePr != null || cctrCodePr.trim().length() > 0))
						{
							cctrCodePh = cctrCodePr;
						}
					}
					
					if ((cctrCodePh == null || cctrCodePh.trim().length() == 0))
					{
						cctrCodePh = cctrCodeCr;
						if ((cctrCodePh == null || cctrCodePh.trim().length() == 0))
						{
							cctrCodePh = cctrCodeDr;
						}
					}
					
					/*Not included in GNL Code but in included Server Source0
					 * if (invOnline.equalsIgnoreCase("Y"))
					{
						acctCodePh = acctCodeCr;
						cctrCodePh = cctrCodeCr;
					}
					else
					{
						acctCodePh = acctCodeDr;
						cctrCodePh = cctrCodeDr;
					}
					
//					CHECKING FOR THE TAKING TAX DETAIL AS PER VARIABLE DEFINED
					taxVar = validatorEJB.getEnvDis("999999", "TA_VOUCH", conn);
					
					System.out.println("taxVar from disparms ::: " + taxVar);
					if (taxVar != null && taxVar.equalsIgnoreCase("NULLFOUND"))
					{
						if (taxVar.indexOf("P-RCP") > -1)
						{
							taxVar2 = "P-RCP";
						}
						if (taxVar.indexOf("P-RET") > -1)
						{
							taxVar1 = "P-RET";
						}
					}
					System.out.println("sql ::: " + sql);
					
					if ((taxVar2.equals("P-RCP") && tranSer.equals("P-RCP")) || (taxVar1.equals("P-RET") && tranSer.equals("P-RET")))
					{
						sql = " SELECT TAX_CLASS,TAX_CHAP,TAX_ENV, RATE__CLG, STD_RATE FROM PORCPDET " 
							+ " WHERE PURC_ORDER = ? AND LINE_NO = ? " ;
						pstmt = conn.prepareStatement(sql);
						pstmt.setString(1, tranIdPorcp);
						pstmt.setString(2, rcpLineNo);
						System.out.println("sql  for taxclass ::: " + sql);
						System.out.println("Setting params :::: tranIdPorcp " + tranIdPorcp + " -- rcpLineNo ::: " + rcpLineNo);
						rs = pstmt.executeQuery();
		
						if (rs.next())
						{
							taxClass = rs.getString(1)== null?"":rs.getString(1);
							taxChap = rs.getString(2)== null?"":rs.getString(2);
							taxEnv = rs.getString(3)== null?"":rs.getString(3);
							rateClg = rs.getString(4)== null?"":rs.getString(4);
							stdRate = rs.getString(5)== null?"":rs.getString(5);
						}
						rs.close();
						pstmt.close();
					}
					else
					{
					*/
						sql = " SELECT TAX_CLASS,TAX_CHAP,TAX_ENV, RATE__CLG, STD_RATE FROM PORDDET " 
								+	" WHERE PURC_ORDER = ? AND LINE_NO = ? " ;
						pstmt = conn.prepareStatement(sql);
						pstmt.setString(1, purcOrdRcp);
						pstmt.setString(2, purcOrdLine);
						System.out.println("sql  for taxclass ::: " + sql);
						System.out.println("Setting params :::: purcOrdRcp " + purcOrdRcp + " -- purcOrdLine ::: " + purcOrdLine);
						writeLog(filePtr,"sql  for taxclass ::: " + sql,true); //ADDED BY ALKA 24/08/07 FOR SOPs TO BE ADDED IN LOG FILE
						writeLog(filePtr,"Setting params :::: purcOrdRcp " + purcOrdRcp + " -- purcOrdLine ::: " + purcOrdLine,true); //ADDED BY ALKA 24/08/07 FOR SOPs TO BE ADDED IN LOG FILE
						rs = pstmt.executeQuery();
		
						if (rs.next())
						{
							taxClass = rs.getString(1)== null?"":rs.getString(1);
							taxChap = rs.getString(2)== null?"":rs.getString(2);
							taxEnv = rs.getString(3)== null?"":rs.getString(3);
							rateClg = rs.getString(4)== null?"":rs.getString(4);
							stdRate = rs.getString(5)== null?"":rs.getString(5);
						}
						rs.close();
						pstmt.close();
//					}
					System.out.println("taxClass ::: " + taxClass + " -- taxChap ::: " + taxChap);
					System.out.println("taxEnv " + taxEnv + " -- rateClg ::: " + rateClg + " -- stdRate ::: " + stdRate); 
					writeLog(filePtr,"taxClass ::: " + taxClass + " -- taxChap ::: " + taxChap,true); //ADDED BY ALKA 24/08/07 FOR SOPs TO BE ADDED IN LOG FILE
					writeLog(filePtr,"taxEnv " + taxEnv + " -- rateClg ::: " + rateClg + " -- stdRate ::: " + stdRate,true); //ADDED BY ALKA 24/08/07 FOR SOPs TO BE ADDED IN LOG FILE
					
					rcpDiscAmt = (rcpQtyStd * rcpRateStd * rcpDiscount)/100;
					rcpAmount = (rcpQtyStd * rcpRateStd) - rcpDiscAmt;
					vouchQuantity = rcpQty;
					vouchAmount = rcpQtyStd * rcpRateStd;
					if (tranSer.equals("P-RET"))
					{
						rcpDiscAmt = 0 - rcpDiscAmt;
						rcpTax = 0 - rcpTax;
						rcpNet = 0 - rcpNet;
						vouchQuantity = 0 - vouchQuantity;
						vouchAmount = 0 - vouchAmount;
						rcpQtyStd = 0 - rcpQtyStd;
						rcpAmount = 0 - rcpAmount;
					}
/* NOT APPLICABLE FOR THE GNL PAYMENT REQUEST CASE
 * //					IF POSTING IS STANDARD THEN PASTE ACCT CODE AND CCTR CODE FROM ITEM ACCT DETR. PPV A/C
					if (postType.equalsIgnoreCase("S"))
					{
						sql = "SELECT ITEM_SER FROM ITEM WHERE ITEM_CODE = ? ";
						pstmt = conn.prepareStatement(sql);
						pstmt.setString(1, rcpItem);	
						rs = pstmt.executeQuery();
		
						if (rs.next())
						{
							itemSer = rs.getString(1);
						}
						rs.close();
						pstmt.close();	
		
//						IF POSTING IS STANDARD THEN PASTE ACCT CODE AND CCTR CODE FROM ITEM ACCT DETR. PPV A/C
						acctStr = finCommon.getAcctDetrTtype(rcpItem, itemSer, "PPV", pOType, conn);
						if (acctStr != null && acctStr.trim().length() > 0)
						{
							acctCodeDiff = acctStr.substring(0,acctStr.indexOf(","));
							cctrCodeDiff = acctStr.substring(acctStr.indexOf(",")+1);
						}
					}
*/	
//					rcpNet = rcpAmount + rcpTax;
					sumRcpTax = sumRcpTax + rcpTax;  //FOR CALC OF ADVANCE ADJUSTMENT
					sumVouchAmt = sumVouchAmt + (vouchAmount-rcpDiscAmt);//FOR CALC OF ADVANCE ADJUSTMENT
					sumNetAmt = sumNetAmt + rcpNet;//FOR CALC OF ADVANCE ADJUSTMENT
					
					
					xmlBuff.append("<Detail3 objContext=\"3\" objName=\"voucher\" domID=\"").append(lineNo).append("\" dbID=''>");
					xmlBuff.append("<attribute selected=\"N\" updateFlag=\"A\" status=\"N\" pkNames=\"\"/>");
					xmlBuff.append("<tran_id/>");
					xmlBuff.append("<line_no><![CDATA[").append(lineNo).append("]]></line_no>");
					xmlBuff.append("<prcp_id><![CDATA[").append(tranIdPorcp).append("]]></prcp_id>");
					xmlBuff.append("<line_no__rcp><![CDATA[").append(rcpLineNo).append("]]></line_no__rcp>");
					xmlBuff.append("<item_code><![CDATA[").append(rcpItem).append("]]></item_code>");
					xmlBuff.append("<unit><![CDATA[").append(rcpUnit).append("]]></unit>");
					xmlBuff.append("<rcp_qty><![CDATA[").append(rcpQty).append("]]></rcp_qty>");				
					xmlBuff.append("<vouch_qty><![CDATA[").append(vouchQuantity).append("]]></vouch_qty>");
					xmlBuff.append("<rcp_rate><![CDATA[").append(rcpRate).append("]]></rcp_rate>");
					xmlBuff.append("<vouch_rate><![CDATA[").append(rcpRate).append("]]></vouch_rate>");
					xmlBuff.append("<purc_order><![CDATA[").append(purcOrdRcp).append("]]></purc_order>");
					xmlBuff.append("<line_no__pord><![CDATA[").append(purcOrdLine).append("]]></line_no__pord>");
					xmlBuff.append("<vouch_amt><![CDATA[").append(vouchAmount).append("]]></vouch_amt>");
					xmlBuff.append("<rcp_amt><![CDATA[").append(rcpAmount).append("]]></rcp_amt>");			
					xmlBuff.append("<tax_class><![CDATA[").append(taxClass).append("]]></tax_class>");
					xmlBuff.append("<tax_chap><![CDATA[").append(taxChap).append("]]></tax_chap>");				
					xmlBuff.append("<tax_env><![CDATA[").append(taxEnv).append("]]></tax_env>");
					xmlBuff.append("<discount><![CDATA[").append(rcpDiscount).append("]]></discount>");
					xmlBuff.append("<disc_amt><![CDATA[").append(rcpDiscAmt).append("]]></disc_amt>");
					xmlBuff.append("<tax_amt><![CDATA[").append(rcpTax).append("]]></tax_amt>");
					xmlBuff.append("<net_amt><![CDATA[").append(rcpNet).append("]]></net_amt>");
					xmlBuff.append("<post_diff><![CDATA[").append("I").append("]]></post_diff>");
					xmlBuff.append("<chg_user/>");
					xmlBuff.append("<chg_date/>");
					xmlBuff.append("<chg_term/>");
					xmlBuff.append("<item_ser><![CDATA[").append(itemSer).append("]]></item_ser>");
					xmlBuff.append("<vouch_qty__stduom><![CDATA[").append(rcpQtyStd).append("]]></vouch_qty__stduom>");
					xmlBuff.append("<vouch_rate__stduom><![CDATA[").append(rcpRateStd).append("]]></vouch_rate__stduom>");
					xmlBuff.append("<acct_code><![CDATA[").append(acctCodePh).append("]]></acct_code>");
					xmlBuff.append("<cctr_code><![CDATA[").append(cctrCodePh).append("]]></cctr_code>");
					xmlBuff.append("<rate__clg><![CDATA[").append(rateClg).append("]]></rate__clg>");
//					xmlBuff.append("<std_rate><![CDATA[").append(stdRate).append("]]></std_rate>");
//					xmlBuff.append("<acct_code__diff><![CDATA[").append(acctCodeDiff).append("]]></acct_code__diff>");
//					xmlBuff.append("<cctr_code__diff><![CDATA[").append(cctrCodeDiff).append("]]></cctr_code__diff>");
//					xmlBuff.append("<dept_code><![CDATA[").append(deptCode).append("]]></dept_code>"); //dept code added for budget calc.
					xmlBuff.append("</Detail3>");
				}
				rs1.close(); rs1 = null;
				pstmt1.close(); pstmt1 = null;
				
				if (tranSer.equalsIgnoreCase("P-RCP")) //UPDATING THE VOUCHER FOR RETURN
				{
					sql = " SELECT B.TRAN_ID, B.LINE_NO, B.PURC_ORDER, B.LINE_NO__ORD, B.ITEM_CODE, "
						+ " B.QUANTITY,   B.UNIT, B.RATE, CASE WHEN B.DISCOUNT IS NULL THEN 0 ELSE B.DISCOUNT END, "
						+ " B.TAX_AMT, B.NET_AMT, B.QUANTITY__STDUOM, B.RATE__STDUOM, B.ACCT_CODE__CR, "
						+ " B.CCTR_CODE__CR FROM  PORCP A, PORCPDET B WHERE A.TRAN_ID = B.TRAN_ID "
						+ " AND A.TRAN_ID__REF = ? AND A.TRAN_SER = 'P-RET' AND A.CONFIRMED = 'Y' "
						+ " AND (A.VOUCH_CREATED = 'N' OR A.VOUCH_CREATED IS NULL) ";
					System.out.println("sql ::: " + sql + " ---------- tranIdPorcp ::::: " + tranIdPorcp);
					writeLog(filePtr,"sql ::: " + sql + " ---------- tranIdPorcp :::::" + tranIdPorcp + ":",true); //ADDED BY ALKA 24/08/07 FOR SOPs TO BE ADDED IN LOG FILE
					pstmt1 = conn.prepareStatement(sql);
					pstmt1.setString(1, tranIdPorcp);
					rs1 = pstmt1.executeQuery();
			
					while (rs1.next())
					{
						rcpRetId = rs1.getString(1);
						rcpLineNo = rs1.getString(2);
						purcOrdRcp = rs1.getString(3);
						purcOrdLine = rs1.getString(4);
						rcpItem = rs1.getString(5);
						rcpQty = rs1.getDouble(6);
						rcpUnit = rs1.getString(7);
						rcpRate = rs1.getDouble(8);
						rcpDiscount = rs1.getDouble(9);
//						rcpTax = rs1.getDouble(10);  //commented by Alka as tax is not to be picked up from porcpdet
						rcpNet = rs1.getDouble(11);
						rcpQtyStd = rs1.getDouble(12);
						rcpRateStd = rs1.getDouble(13);
						acctCodePh = rs1.getString(14);
						cctrCodePh = rs1.getString(15);
		
						lineNo = lineNo + 1;
						
						sql = " SELECT TAX_CLASS,TAX_CHAP,TAX_ENV, RATE__CLG, STD_RATE FROM PORDDET " 
							+	" WHERE PURC_ORDER = ? AND LINE_NO = ? " ;
						pstmt = conn.prepareStatement(sql);
						pstmt.setString(1, purcOrdRcp);
						pstmt.setString(2, purcOrdLine);
						System.out.println("sql  for taxclass ::: " + sql);
						System.out.println("Setting params :::: purcOrdRcp " + purcOrdRcp + " -- purcOrdLine ::: " + purcOrdLine);
						writeLog(filePtr,"sql  for taxclass ::: " + sql,true); //ADDED BY ALKA 24/08/07 FOR SOPs TO BE ADDED IN LOG FILE
						writeLog(filePtr,"Setting params :::: purcOrdRcp ::" + purcOrdRcp + ":: purcOrdLine :::" + purcOrdLine + ":",true); //ADDED BY ALKA 24/08/07 FOR SOPs TO BE ADDED IN LOG FILE

						rs = pstmt.executeQuery();
		
						if (rs.next())
						{
							taxClass = rs.getString(1)== null?"":rs.getString(1);
							taxChap = rs.getString(2)== null?"":rs.getString(2);
							taxEnv = rs.getString(3)== null?"":rs.getString(3);
							rateClg = rs.getString(4)== null?"":rs.getString(4);
							stdRate = rs.getString(5)== null?"":rs.getString(5);
						}
						rs.close();
						pstmt.close();
						
						System.out.println("taxClass ::: " + taxClass + " -- taxChap ::: " + taxChap);
						System.out.println("taxEnv " + taxEnv + " -- rateClg ::: " + rateClg + " -- stdRate ::: " + stdRate);
						writeLog(filePtr,"taxClass :::" + taxClass + ":: taxChap :::" + taxChap + "::",true); //ADDED BY ALKA 24/08/07 FOR SOPs TO BE ADDED IN LOG FILE
						writeLog(filePtr,"taxEnv :::" + taxEnv + ":: rateClg :::" + rateClg + ":: stdRate :::" + stdRate + "::",true); //ADDED BY ALKA 24/08/07 FOR SOPs TO BE ADDED IN LOG FILE
						
						rcpDiscAmt = 0 - (rcpQtyStd * rcpRateStd * rcpDiscount)/100;
						rcpAmount = (rcpQtyStd * rcpRateStd) - rcpDiscAmt;
						vouchQuantity = 0 - rcpQty;
						vouchAmount = 0 - (rcpQtyStd * rcpRateStd);
						rcpNet = 0 - rcpNet;
//						 as discount amt to be added back
						totAmt = totAmt + vouchAmount - rcpDiscAmt;
						netAmt = totAmt;
						netAmtBase = totAmt * exchRate;
						rcpQtyStd = 0 - rcpQtyStd;
						
						sumRcpTax = sumRcpTax + rcpTax;  //FOR CALC OF ADVANCE ADJUSTMENT
						sumVouchAmt = sumVouchAmt + (vouchAmount-rcpDiscAmt);
						sumNetAmt = sumNetAmt + rcpNet;
						
						xmlBuff.append("<Detail3 objContext=\"3\" objName=\"voucher\" domID=\"").append(lineNo).append("\" dbID=''>");
						xmlBuff.append("<attribute selected=\"N\" updateFlag=\"A\" status=\"N\" pkNames=\"\"/>");
						xmlBuff.append("<tran_id/>");
						xmlBuff.append("<line_no><![CDATA[").append(lineNo).append("]]></line_no>");
						xmlBuff.append("<prcp_id><![CDATA[").append(rcpRetId).append("]]></prcp_id>");
						xmlBuff.append("<line_no__rcp><![CDATA[").append(rcpLineNo).append("]]></line_no__rcp>");
						xmlBuff.append("<item_code><![CDATA[").append(rcpItem).append("]]></item_code>");
						xmlBuff.append("<unit><![CDATA[").append(rcpUnit).append("]]></unit>");
						xmlBuff.append("<rcp_qty><![CDATA[").append(rcpQty).append("]]></rcp_qty>");				
						xmlBuff.append("<vouch_qty><![CDATA[").append(vouchQuantity).append("]]></vouch_qty>");
						xmlBuff.append("<rcp_rate><![CDATA[").append(rcpRate).append("]]></rcp_rate>");
						xmlBuff.append("<vouch_rate><![CDATA[").append(rcpRate).append("]]></vouch_rate>");
						xmlBuff.append("<purc_order><![CDATA[").append(purcOrdRcp).append("]]></purc_order>");
						xmlBuff.append("<line_no__pord><![CDATA[").append(purcOrdLine).append("]]></line_no__pord>");
						xmlBuff.append("<vouch_amt><![CDATA[").append(vouchAmount).append("]]></vouch_amt>");
						xmlBuff.append("<rcp_amt><![CDATA[").append(rcpAmount).append("]]></rcp_amt>");			
						xmlBuff.append("<tax_class><![CDATA[").append(taxClass).append("]]></tax_class>");
						xmlBuff.append("<tax_chap><![CDATA[").append(taxChap).append("]]></tax_chap>");				
						xmlBuff.append("<tax_env><![CDATA[").append(taxEnv).append("]]></tax_env>");
						xmlBuff.append("<discount><![CDATA[").append(rcpDiscount).append("]]></discount>");
						xmlBuff.append("<disc_amt><![CDATA[").append(rcpDiscAmt).append("]]></disc_amt>");
						xmlBuff.append("<tax_amt><![CDATA[").append(rcpTax).append("]]></tax_amt>");
						xmlBuff.append("<net_amt><![CDATA[").append(rcpNet).append("]]></net_amt>");
						xmlBuff.append("<post_diff><![CDATA[").append("I").append("]]></post_diff>");
						xmlBuff.append("<chg_user/>");
						xmlBuff.append("<chg_date/>");
						xmlBuff.append("<chg_term/>");
						xmlBuff.append("<item_ser><![CDATA[").append(itemSer).append("]]></item_ser>");
						xmlBuff.append("<vouch_qty__stduom><![CDATA[").append(rcpQtyStd).append("]]></vouch_qty__stduom>");
						xmlBuff.append("<vouch_rate__stduom><![CDATA[").append(rcpRateStd).append("]]></vouch_rate__stduom>");
						xmlBuff.append("<acct_code><![CDATA[").append(acctCodePh).append("]]></acct_code>");
						xmlBuff.append("<cctr_code><![CDATA[").append(cctrCodePh).append("]]></cctr_code>");
						xmlBuff.append("</Detail3>");
					}
					rs1.close(); rs1 = null;
					pstmt1.close(); pstmt1 = null;
				}
/*to be clarified from manohanran sir
 * 
 * if ls_transer = 'P-RCP' then
		//Updating voucher detail for return
			update porcp set vouch_created = 'Y' where tran_id = :ls_retid;
			if sqlca.sqlcode < 0 then
				ls_errcode = 'DS000' + string(sqlca.sqldbcode)
				exit
			elseif sqlca.sqlnrows <> 1 then
				ls_errcode = 'DS000NR'
				exit
			end if	

		  	fetch next cur_retdet into :ls_retid,:ls_rcp_line,:ls_po_rcp,:ls_po_line,:ls_rcp_item,
 		                   :lc_rcp_quantity,:ls_rcp_unit,:lc_rcp_rate,:lc_rcp_discount,
                         :lc_rcp_tax,:lc_rcp_net,:lc_rcp_qty_std,
   							 :lc_rcp_rate_std,:ls_acct_ph,:ls_cctr_ph;
	   loop
   	close cur_retdet;
 * 
 */
				
//				LOGIC FOR ADVANCE ADJUSTMENT ADDED BY ALKA ON 08/06/07
//				ADDING FOR CALCULATION OF ADVANCE ALREADY GIVEN
//				PICKUP THE NET AMOUNT FOR ADVANCE ADJUSTMENT.
			
				netAmtAdj = sumVouchAmt + sumRcpTax; 
				System.out.println("netAmtAdj = sumVouchAmt + sumRcpTax");
				System.out.println(netAmtAdj + " = " + sumVouchAmt + " + " + sumRcpTax);
				writeLog(filePtr,"netAmtAdj = sumVouchAmt + sumRcpTax",true); //ADDED BY ALKA 24/08/07 FOR SOPs TO BE ADDED IN LOG FILE
				writeLog(filePtr,netAmtAdj + " = " + sumVouchAmt + " + " + sumRcpTax,true); //ADDED BY ALKA 24/08/07 FOR SOPs TO BE ADDED IN LOG FILE

				if (netAmtAdj > 0)
				{
					lineNo = 1;
					/*sql = " SELECT TRAN_SER, REF_NO, CURR_CODE, EXCH_RATE, ACCT_CODE, CCTR_CODE, TOT_AMT, "
						+ " TOT_AMT - ADJ_AMT, SUNDRY_TYPE, SUNDRY_CODE FROM MISC_PAYABLES WHERE "
						+ " ( TRAN_SER IN ('P-ADV', 'LC-ADV', 'M-ADV') OR ( TRAN_SER LIKE 'P-IBC%' "
						+ " AND REF_SER__ORG IN ('P-ADV', 'LC-ADV', 'M-ADV') ) ) AND PURC_ORDER = ? "
						+ " AND TOT_AMT - ADJ_AMT <> 0 AND SITE_CODE = ? ";*/
					sql = "SELECT TRAN_SER, REF_NO, CURR_CODE, EXCH_RATE, ACCT_CODE, CCTR_CODE, TOT_AMT, "
						+ " TOT_AMT - ADJ_AMT, SUNDRY_TYPE, SUNDRY_CODE FROM MISC_PAYABLES WHERE "
						+ " TRAN_SER IN ('P-ADV', 'LC-ADV', 'M-ADV') AND PURC_ORDER = ? "
						+ " AND TOT_AMT - ADJ_AMT <> 0";
					pstmt1 = conn.prepareStatement(sql);
					System.out.println("sql for advance ========== " + sql);
					System.out.println("Setting params ===== purcOrder ::" + purcOrder + " siteCode :::: "+ siteCode);
					writeLog(filePtr,"sql for advance ========== " + sql,true); //ADDED BY ALKA 24/08/07 FOR SOPs TO BE ADDED IN LOG FILE
					writeLog(filePtr,"Setting params ===== purcOrder ::" + purcOrder + ": siteCode ::::"+ siteCode + ":",true); //ADDED BY ALKA 24/08/07 FOR SOPs TO BE ADDED IN LOG FILE
					pstmt1.setString(1, purcOrder);
//					pstmt1.setString(2, siteCode);
					rs1 = pstmt1.executeQuery();
					while (rs1.next() && sumNetAmt > 0)
					{
						lineNo = lineNo++;
						refSer = rs1.getString(1);
						refNo = rs1.getString(2);
						currAdv = rs1.getString(3);
						exchAdv = rs1.getDouble(4);
						acctCodeAdv = rs1.getString(5);
						cctrCodeAdv = rs1.getString(6);
						totAmtAdv = rs1.getDouble(7);
						advAmt = rs1.getDouble(8);
						sType = rs1.getString(9);
						sCode = rs1.getString(10);
						
						if (!currAdv.equals(currCode))
						{
							System.out.println("currAdv " + currAdv + " does not match the currCode ::: " + currCode);
							retString = "F";
							writeLog(filePtr,"currAdv " + currAdv + " does not match the currCode ::: " + currCode,true); //ADDED BY ALKA 22/08/07 FOR WRITING IN LOG FILE FOR EXCEPTION THROWN
							throw new Exception("currAdv " + currAdv + " does not match the currCode ::: " + currCode);
						}
						if (!sType.equalsIgnoreCase("S") || !sCode.trim().equalsIgnoreCase(suppCode.trim()))
						{//VTSUPPMIS
							System.out.println("sType [" + sType + "] is not 'S' or sCode [" + sCode + "] does not match the suppCode ::: [" + suppCode + "]");
							retString = "F";
							writeLog(filePtr,"sType [" + sType + "] is not 'S' or sCode [" + sCode + "] does not match the suppCode ::: [" + suppCode + "]",true); //ADDED BY ALKA 22/08/07 FOR WRITING IN LOG FILE FOR EXCEPTION THROWN
							throw new Exception("sType [" + sType + "] is not 'S' or sCode [" + sCode + "] does not match the suppCode ::: [" + suppCode + "]");
						}
						
	//					CONSIDER THE AMOUNT AUTOMATICALLY ADJUSTED AND BUT VOUCHER NOT CONFIRMED.
						sql = " SELECT SUM(ADJ_AMT) FROM VOUCHER A, VOUCHADV B WHERE A.TRAN_ID = B.TRAN_ID "
							+ " AND B.REF_SER = ? AND B.REF_NO = ? AND A.CONFIRMED = 'N' AND A.TRAN_MODE = 'A'";
						pstmt = conn.prepareStatement(sql);
						pstmt.setString(1, refSer);
						pstmt.setString(2, refNo);
						System.out.println("sql automatically adjusted  ========== " + sql);
						System.out.println("Setting params ===== refSer ::" + refSer + " refNo :::: "+ refNo);
						writeLog(filePtr,"sql automatically adjusted  ========== " + sql,true); //ADDED BY ALKA 24/08/07 FOR SOPs TO BE ADDED IN LOG FILE
						writeLog(filePtr,"Setting params ===== refSer ::" + refSer + ": refNo ::::"+ refNo + ":",true); //ADDED BY ALKA 24/08/07 FOR SOPs TO BE ADDED IN LOG FILE
						rs = pstmt.executeQuery();
						if (rs.next())
						{
							advAdj = rs.getDouble(1);
						}
						rs.close(); rs = null;
						pstmt.close(); pstmt = null;
						
						advAmt = advAmt - advAdj;
						if (advAmt != 0)
						{
						
							refNo = refNo.trim();
							advAmt = Math.abs(advAmt) > sumNetAmt ? 0 - sumNetAmt:advAmt;
//							advAmt = Math.abs(advAmt) > netAmt ? 0 - netAmt:advAmt;
							diffExch = 0;
							diffExch = (advAmt * exchAdv) - (advAmt * exchRate);
							diffTot = diffTot + diffExch;
							advTot = advTot + advAmt;
							sumNetAmt = sumNetAmt + advAmt;
							netAmtAdj = netAmtAdj + advAmt;
						
							xmlBuff.append("<Detail2 objContext=\"2\" objName=\"voucher\" domID=\"").append(lineNo).append("\" dbID=''>");
							xmlBuff.append("<attribute selected=\"N\" updateFlag=\"A\" status=\"N\" pkNames=\"\"/>");
							xmlBuff.append("<tran_id/>");
							xmlBuff.append("<line_no><![CDATA[").append(lineNo).append("]]></line_no>");
							xmlBuff.append("<ref_ser><![CDATA[").append(refSer).append("]]></ref_ser>");
							xmlBuff.append("<ref_no><![CDATA[").append(refNo).append("]]></ref_no>");
							xmlBuff.append("<adj_amt><![CDATA[").append(advAmt).append("]]></adj_amt>");
							xmlBuff.append("<tot_amt><![CDATA[").append(totAmtAdv).append("]]></tot_amt>");
							xmlBuff.append("<diff_amt__exch><![CDATA[").append(diffExch).append("]]></diff_amt__exch>");				
							xmlBuff.append("<curr_code><![CDATA[").append(currAdv).append("]]></curr_code>");
							xmlBuff.append("<exch_rate__vouch><![CDATA[").append(exchAdv).append("]]></exch_rate__vouch>");
							xmlBuff.append("<acct_code><![CDATA[").append(acctCodeAdv).append("]]></acct_code>");
							xmlBuff.append("<cctr_code><![CDATA[").append(cctrCodeAdv).append("]]></cctr_code>");
							xmlBuff.append("<auto_generated><![CDATA[A]]></auto_generated>");
							xmlBuff.append("</Detail2>");
						}
					}
					rs1.close(); rs1 = null;
					pstmt1.close(); pstmt1 = null;
//					CHANGE ENDED -- BY ALKA ON 08/06/07
				}

				xmlBuff.append("</Header0>");
				xmlBuff.append("</group0>");
				xmlBuff.append("</DocumentRoot>");
			}

			xmlString = xmlBuff.toString();	
			retString = saveData(siteCode,xmlString);
			
			if (retString.indexOf("Success") > -1)
			{
				retString = "S";
				retString =  insertDrCrNote(xmlDataAll, dueDate);
				if (retString.equalsIgnoreCase("F"))
				{
					System.out.println("Debit Note / Credit Note not generated");
					writeLog(filePtr,"Debit Note / Credit Note not generated",true); //ADDED BY ALKA 22/08/07 FOR WRITING IN LOG FILE FOR EXCEPTION THROWN
					throw new Exception("Debit Note / Credit Note not generated");
				}
			}
			else
			{
				System.out.println("Voucher Not Generated for Purchase Receipt NO. ::: " + tranIdPorcp);
				writeLog(filePtr,"Voucher Not Generated for Purchase Receipt NO. ::: " + tranIdPorcp,true); //ADDED BY ALKA 22/08/07 FOR WRITING IN LOG FILE FOR EXCEPTION THROWN
				throw new Exception("Voucher Not Generated for Purchase Receipt NO. ::: " + tranIdPorcp);
			}
		}
		catch (ITMException ie)
		{
			System.out.println("ITMException in insertVoucher ::: " + ie.getMessage());
			ie.printStackTrace();
			throw ie;
		}
		catch (Exception e)
		{
			System.out.println("Error in insertVoucher ::: " + e.getMessage());
			e.printStackTrace();
			throw new ITMException(e);
		}
		finally
		{
			try
			{
				conn.close(); conn = null;
			}
			catch(Exception ex){}
		}
		System.out.println("retString in insertVoucher ::::: 11111111111111111111111111111111111 :::::/n" + retString);
		writeLog(filePtr,"retString in insertVoucher ::::: 11111111111111111111111111111111111 :::::/n" + retString,true); //ADDED BY ALKA 24/08/07 FOR WRITING IN LOG FILE FOR SOPs
		return retString;
	}

	private String insertDrCrNote(String xmlDataAll, String dueDate) throws ITMException
	{
		Document dom = null;
		NodeList headerList = null;
		Node header = null;
		Connection conn = null;
		ConnDriver connDriver = new ConnDriver();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = null, xmlString = "", retString ="";
		StringBuffer xmlBuff = new StringBuffer();
		
		String tranIdPorcp = "", suppCode = "", invoiceNo = "", invoiceDate = "", crTerm = "", voucherNo = "";
		String acctCodeDr = "", cctrCodeDr = "", currCode = "", exchRate = "", siteCode = "";//, dueDate = "";  COMMENTED BY ALKA ON 25/06/07 AS ERROR IN CALCULATING DUE DATE
		String tranDatePorcp = "", finEntity = "",acctCodeSupp = "", cctrCodeSupp = "";//, effDate = ""; COMMENTED BY ALKA ON 25/06/07 AS ERROR IN CALCULATING DUE DATE
		double amountToPay = 0, amountToBePaid = 0, diffAmount = 0, amountPaid = 0, advPaid = 0; //VARIABLES ADDED BY ALKA 23/08/07

		
		try
		{
			conn = connDriver.getConnectDB("DriverITM");
			connDriver = null;
			dom =  genericUtility.parseString(xmlDataAll);
			headerList = dom.getElementsByTagName("Detail1");			
			header = headerList.item(0);
			tranIdPorcp = genericUtility.getColumnValueFromNode("tran_id__porcp", header);
			suppCode = genericUtility.getColumnValueFromNode("supp_code", header);
			invoiceNo = genericUtility.getColumnValueFromNode("invoice_no", header);
			invoiceDate = genericUtility.getColumnValueFromNode("invoice_date", header);
			System.out.println("tranIdPorcp ::::: " + tranIdPorcp);
			writeLog(filePtr,"Inside insertDrCrNote >>>>>>>>>> tranIdPorcp ::::: " + tranIdPorcp,true); //ADDED BY ALKA 24/08/07 FOR SOPs TO BE ADDED IN LOG FILE
			
			amountToPay = Double.parseDouble(genericUtility.getColumnValueFromNode("amount_topay", header)); //COMMENTED BY ALKA AS NOT REQUIRED 23/08/07
			crTerm = genericUtility.getColumnValueFromNode("cr_term", header);
			tranDatePorcp = genericUtility.getColumnValue("tran_date", dom);
			
//			sql = "	SELECT TRAN_ID FROM VOUCHRCP WHERE PRCP_ID = ? ";
			sql = "	SELECT A.TRAN_ID, A.NET_AMT, A.ADV_AMT FROM VOUCHER A, VOUCHRCP B "
				+ " WHERE A.TRAN_ID = B.TRAN_ID AND B.PRCP_ID = ? ";  //SQL CHANGED BY ALKA 23/08/07 FOR RETRIEVING THE AMOUNT PAID THRU VOUCHER FOR CALCULATION
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, tranIdPorcp);
			rs = pstmt.executeQuery();
			if (rs.next())
			{
				voucherNo = rs.getString(1);
				amountPaid = rs.getDouble(2);
				advPaid = rs.getDouble(3);
				System.out.println("voucherNo ::::" + voucherNo + ": amountPaid ::::" + amountPaid + ":");  //CHANGED BY ALKA 24/08/07
				writeLog(filePtr,"voucherNo ::::" + voucherNo + ": amountPaid ::::" + amountPaid + ":",true); //ADDED BY ALKA 24/08/07 FOR SOPs TO BE ADDED IN LOG FILE
			}
			rs.close(); pstmt.close();
			rs = null; pstmt = null;  //ADDED BY ALKA 23/08/07

//			CHANGED THE ORDER OF EXECUTING SQLS TO OPTIMISE CODE -- CHANGED BY ALKA 23/08/07
			
			sql = "SELECT AMOUNT, CURR_CODE, EXCH_RATE, SITE_CODE, EFF_DATE FROM PORCP WHERE TRAN_ID = ?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, tranIdPorcp);
			rs = pstmt.executeQuery();
			if (rs.next())
			{
				amountToBePaid = rs.getDouble(1);
				currCode = rs.getString(2);
				exchRate = rs.getString(3);
				siteCode = rs.getString(4);
//				effDate = genericUtility.getValidDateString(rs.getTimestamp(5).toString(), genericUtility.getDBDateFormat(), genericUtility.getApplDateFormat());
			}
			rs.close(); rs = null;
			pstmt.close(); pstmt = null;

			diffAmount = amountToBePaid - (amountToPay + (advPaid*(-1)));
			writeLog(filePtr, "amountToBePaid - (amountToPay + (advPaid*(-1))", true);
			writeLog(filePtr, amountToBePaid +" - ("+ amountToPay + " + (" + advPaid + "*(-1))", true);

			if (diffAmount == 0)
			{
				System.out.println("diffAmount ===================== ["+diffAmount+"]");
				writeLog(filePtr, "diffAmount ===================== ["+diffAmount+"]", true);
				return "S";
			}
//			END OF CHANGE BY ALKA 23/08/07 

			sql =	" SELECT ACCT_CODE__AP, CCTR_CODE__AP FROM SUPPLIER WHERE SUPP_CODE = ? ";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, suppCode);
			rs = pstmt.executeQuery();
			if (rs.next())
			{
				acctCodeSupp = rs.getString(1)== null ? " ":rs.getString(1);
				cctrCodeSupp = rs.getString(2)== null ? " ":rs.getString(2);
			}
			rs.close(); rs = null;
			pstmt.close(); pstmt = null;
			
			sql =	" SELECT ACCT_CODE__DR, CCTR_CODE__DR FROM PORCPDET WHERE TRAN_ID = ? ";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, tranIdPorcp);
			rs = pstmt.executeQuery();
			while (rs.next())
			{
				acctCodeDr = rs.getString(1)== null ? "":rs.getString(1);
				cctrCodeDr = rs.getString(2)== null ? "":rs.getString(2);
				if (acctCodeDr.trim().length() > 0)
				{
					break;
				}
			}
			rs.close();  rs = null;
			pstmt.close(); pstmt = null;
			
			sql =	" SELECT FIN_ENTITY FROM SITE WHERE SITE_CODE = ? ";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, siteCode);
			rs = pstmt.executeQuery();
			if (rs.next())
			{
				finEntity = rs.getString(1)== null ? "":rs.getString(1);
			}
			rs.close(); rs = null;
			pstmt.close(); pstmt = null;
			
//			COMMENTED BY ALKA ON 25/06/07 AS ERROR IN CALCULATING DUE DATE
//			dueDate =  genericUtility.getValidDateString(calcDueDate(crTerm, tranDatePorcp, effDate, invoiceDate, conn),genericUtility.getApplDateFormat());   
/*			SHIFTED ABOVE BY ALKA 23/08/07
			diffAmount = amountToBePaid - amountToPay;

			if (diffAmount == 0)
			{
				System.out.println("diffAmount ===================== ["+diffAmount+"]");
				return "S";
			} 
*/

			if (diffAmount > 0)  //debit note
			{
				xmlBuff.append("<?xml version='1.0' encoding='ISO-8859-1'?>\n");
				xmlBuff.append("<DocumentRoot>");
				xmlBuff.append("<description>").append("Datawindow Root").append("</description>");
				xmlBuff.append("<group0>");
				xmlBuff.append("<description>").append("Group0 description").append("</description>");
				xmlBuff.append("<Header0>");
		
				xmlBuff.append("<objName><![CDATA[").append("drcrpay_dr").append("]]></objName>");
				xmlBuff.append("<pageContext><![CDATA[").append("1").append("]]></pageContext>");
				xmlBuff.append("<objContext><![CDATA[").append("1").append("]]></objContext>");
				xmlBuff.append("<editFlag><![CDATA[").append("A").append("]]></editFlag>");
				xmlBuff.append("<focusedColumn><![CDATA[").append("").append("]]></focusedColumn>");
				xmlBuff.append("<action><![CDATA[").append("SAVE").append("]]></action>");
				xmlBuff.append("<elementName><![CDATA[").append("").append("]]></elementName>");
				xmlBuff.append("<keyValue><![CDATA[").append("1").append("]]></keyValue>");
				xmlBuff.append("<taxKeyValue><![CDATA[").append("").append("]]></taxKeyValue>");
				xmlBuff.append("<saveLevel><![CDATA[").append("1").append("]]></saveLevel>");
				xmlBuff.append("<forcedSave><![CDATA[").append(true).append("]]></forcedSave>");
				xmlBuff.append("<taxInFocus><![CDATA[").append(false).append("]]></taxInFocus>");
				xmlBuff.append("<description>").append("Header0 members").append("</description>");
			
				xmlBuff.append("<Detail1 objContext =\"1\"").append(" objName=\"drcrpay_dr\" domID=\"1\" dbID=\"\">");
				xmlBuff.append("<attribute selected=\"N\" updateFlag=\"A\" status=\"N\" pkNames=\"\"/>");
				xmlBuff.append("<tran_id/>");
				xmlBuff.append("<tran_ser><![CDATA[").append("DRNPAY").append("]]></tran_ser>");
				xmlBuff.append("<tran_date><![CDATA[").append(genericUtility.getValidDateString(new Timestamp(System.currentTimeMillis()).toString(),genericUtility.getDBDateFormat(), genericUtility.getApplDateFormat())).append("]]></tran_date>");
				xmlBuff.append("<fin_entity><![CDATA[").append(finEntity==null?"":finEntity).append("]]></fin_entity>");
				xmlBuff.append("<supp_code><![CDATA[").append(suppCode).append("]]></supp_code>");
				if(acctCodeSupp != null && acctCodeSupp.trim().length()>0)
				{
					System.out.println("acctCodeSupp["+acctCodeSupp+"]");
					writeLog(filePtr,"acctCodeSupp["+acctCodeSupp+"]",true); //ADDED BY ALKA 24/08/07 FOR SOPs TO BE ADDED IN LOG FILE
					xmlBuff.append("<acct_code><![CDATA[").append(acctCodeSupp).append("]]></acct_code>");
				}
				else
				{
					System.out.println("acctCodeDr["+acctCodeDr+"]");
					writeLog(filePtr,"acctCodeDr["+acctCodeDr+"]",true); //ADDED BY ALKA 24/08/07 FOR SOPs TO BE ADDED IN LOG FILE
					xmlBuff.append("<acct_code><![CDATA[").append(acctCodeDr).append("]]></acct_code>");

				}
				if(cctrCodeSupp != null && cctrCodeSupp.trim().length()>0)
				{
					System.out.println("cctrCodeSupp["+cctrCodeSupp+"]");
					writeLog(filePtr,"cctrCodeSupp["+cctrCodeSupp+"]",true); //ADDED BY ALKA 24/08/07 FOR SOPs TO BE ADDED IN LOG FILE
					xmlBuff.append("<cctr_code><![CDATA[").append(cctrCodeSupp).append("]]></cctr_code>");
				}
				else
				{
					System.out.println("cctrCodeDr["+cctrCodeDr+"]");
					writeLog(filePtr,"cctrCodeDr["+cctrCodeDr+"]",true); //ADDED BY ALKA 24/08/07 FOR SOPs TO BE ADDED IN LOG FILE
					xmlBuff.append("<cctr_code><![CDATA[").append(cctrCodeDr).append("]]></cctr_code>");

				}
				xmlBuff.append("<eff_date><![CDATA[").append(tranDatePorcp).append("]]></eff_date>");
				xmlBuff.append("<vouch_no><![CDATA[").append(voucherNo).append("]]></vouch_no>");
				xmlBuff.append("<amount><![CDATA[").append(diffAmount).append("]]></amount>");
				xmlBuff.append("<curr_code><![CDATA[").append(currCode==null?"":currCode).append("]]></curr_code>");
				xmlBuff.append("<exch_rate><![CDATA[").append(exchRate==null?"":exchRate).append("]]></exch_rate>");
				xmlBuff.append("<site_code><![CDATA[").append(siteCode==null?"":siteCode).append("]]></site_code>");
				xmlBuff.append("<bill_no><![CDATA[").append(invoiceNo==null?"":invoiceNo).append("]]></bill_no>");
				xmlBuff.append("<bill_date><![CDATA[").append(invoiceDate==null?"":invoiceDate).append("]]></bill_date>");
				xmlBuff.append("<drcr_flag><![CDATA[").append("D").append("]]></drcr_flag>");
				xmlBuff.append("<cr_term><![CDATA[").append(crTerm==null?"":crTerm).append("]]></cr_term>");
				xmlBuff.append("<confirmed><![CDATA[").append("N").append("]]></confirmed>");			
				xmlBuff.append("<due_date><![CDATA[").append(dueDate==null?"":dueDate).append("]]></due_date>");
				xmlBuff.append("<chg_user/>");
				xmlBuff.append("<chg_date/>");
				xmlBuff.append("<chg_term/>");
				xmlBuff.append("</Detail1>");
				
				xmlBuff.append("<Detail3 objContext=\"3\" objName=\"drcrpay_dr\" domID=\"").append("1").append("\" dbID=''>");
				xmlBuff.append("<attribute selected=\"N\" updateFlag=\"A\" status=\"N\" pkNames=\"\"/>");
				xmlBuff.append("<tran_id/>");
				xmlBuff.append("<line_no><![CDATA[").append("1").append("]]></line_no>");
				xmlBuff.append("<amount><![CDATA[").append(diffAmount).append("]]></amount>");
				xmlBuff.append("<acct_code><![CDATA[").append(acctCodeDr).append("]]></acct_code>");//(07052007)
				xmlBuff.append("<cctr_code><![CDATA[").append(cctrCodeDr).append("]]></cctr_code>");//(07052007)
				xmlBuff.append("</Detail3>");
			}
			else if (diffAmount < 0) //credit note
			{
				xmlBuff.append("<?xml version='1.0' encoding='ISO-8859-1'?>\n");
				xmlBuff.append("<DocumentRoot>");
				xmlBuff.append("<description>").append("Datawindow Root").append("</description>");
				xmlBuff.append("<group0>");
				xmlBuff.append("<description>").append("Group0 description").append("</description>");
				xmlBuff.append("<Header0>");
		
				xmlBuff.append("<objName><![CDATA[").append("drcrpay_cr").append("]]></objName>");
				xmlBuff.append("<pageContext><![CDATA[").append("1").append("]]></pageContext>");
				xmlBuff.append("<objContext><![CDATA[").append("1").append("]]></objContext>");
				xmlBuff.append("<editFlag><![CDATA[").append("A").append("]]></editFlag>");
				xmlBuff.append("<focusedColumn><![CDATA[").append("").append("]]></focusedColumn>");
				xmlBuff.append("<action><![CDATA[").append("SAVE").append("]]></action>");
				xmlBuff.append("<elementName><![CDATA[").append("").append("]]></elementName>");
				xmlBuff.append("<keyValue><![CDATA[").append("1").append("]]></keyValue>");
				xmlBuff.append("<taxKeyValue><![CDATA[").append("").append("]]></taxKeyValue>");
				xmlBuff.append("<saveLevel><![CDATA[").append("1").append("]]></saveLevel>");
				xmlBuff.append("<forcedSave><![CDATA[").append(true).append("]]></forcedSave>");
				xmlBuff.append("<taxInFocus><![CDATA[").append(false).append("]]></taxInFocus>");
				xmlBuff.append("<description>").append("Header0 members").append("</description>");
			
				xmlBuff.append("<Detail1 objContext =\"1\"").append(" objName=\"drcrpay_cr\" domID=\"1\" dbID=\"\">");
				xmlBuff.append("<attribute selected=\"N\" updateFlag=\"A\" status=\"N\" pkNames=\"\"/>");
				xmlBuff.append("<tran_id/>");
				xmlBuff.append("<tran_ser><![CDATA[").append("CRNPAY").append("]]></tran_ser>");
				xmlBuff.append("<tran_date><![CDATA[").append(genericUtility.getValidDateString(new Timestamp(System.currentTimeMillis()).toString(),genericUtility.getDBDateFormat(), genericUtility.getApplDateFormat())).append("]]></tran_date>");
				xmlBuff.append("<fin_entity><![CDATA[").append(finEntity).append("]]></fin_entity>");
				xmlBuff.append("<supp_code><![CDATA[").append(suppCode).append("]]></supp_code>");
				if(acctCodeSupp != null && acctCodeSupp.trim().length()>0)
				{
					System.out.println("acctCodeSupp["+acctCodeSupp+"]");
					writeLog(filePtr,"acctCodeSupp["+acctCodeSupp+"]",true); //ADDED BY ALKA 24/08/07 FOR SOPs TO BE ADDED IN LOG FILE
					xmlBuff.append("<acct_code><![CDATA[").append(acctCodeSupp).append("]]></acct_code>");
				}
				else
				{
					System.out.println("acctCodeDr["+acctCodeDr+"]");
					writeLog(filePtr,"acctCodeDr["+acctCodeDr+"]",true); //ADDED BY ALKA 24/08/07 FOR SOPs TO BE ADDED IN LOG FILE
					xmlBuff.append("<acct_code><![CDATA[").append(acctCodeDr).append("]]></acct_code>");

				}
				if(cctrCodeSupp != null && cctrCodeSupp.trim().length()>0)
				{
					System.out.println("cctrCodeSupp["+cctrCodeSupp+"]");
					writeLog(filePtr,"cctrCodeSupp["+cctrCodeSupp+"]",true); //ADDED BY ALKA 24/08/07 FOR SOPs TO BE ADDED IN LOG FILE
					xmlBuff.append("<cctr_code><![CDATA[").append(cctrCodeSupp).append("]]></cctr_code>");
				}
				else
				{
					System.out.println("cctrCodeDr["+cctrCodeDr+"]");
					writeLog(filePtr,"cctrCodeDr["+cctrCodeDr+"]",true); //ADDED BY ALKA 24/08/07 FOR SOPs TO BE ADDED IN LOG FILE
					xmlBuff.append("<cctr_code><![CDATA[").append(cctrCodeDr).append("]]></cctr_code>");
				}
				xmlBuff.append("<eff_date><![CDATA[").append(tranDatePorcp).append("]]></eff_date>");
				xmlBuff.append("<vouch_no><![CDATA[").append(voucherNo).append("]]></vouch_no>");
				xmlBuff.append("<amount><![CDATA[").append(diffAmount).append("]]></amount>");
				xmlBuff.append("<curr_code><![CDATA[").append(currCode).append("]]></curr_code>");
				xmlBuff.append("<exch_rate><![CDATA[").append(exchRate).append("]]></exch_rate>");
				xmlBuff.append("<site_code><![CDATA[").append(siteCode).append("]]></site_code>");
				xmlBuff.append("<bill_no><![CDATA[").append(invoiceNo==null?"":invoiceNo).append("]]></bill_no>");
				xmlBuff.append("<bill_date><![CDATA[").append(invoiceDate==null?"":invoiceDate).append("]]></bill_date>");
				xmlBuff.append("<drcr_flag><![CDATA[").append("C").append("]]></drcr_flag>");
				xmlBuff.append("<cr_term><![CDATA[").append(crTerm==null?"":crTerm).append("]]></cr_term>");
				xmlBuff.append("<confirmed><![CDATA[").append("N").append("]]></confirmed>");			
				xmlBuff.append("<due_date><![CDATA[").append(dueDate).append("]]></due_date>");
				xmlBuff.append("<amt__bc/>");			
				xmlBuff.append("<chg_user/>");
				xmlBuff.append("<chg_date/>");
				xmlBuff.append("<chg_term/>");
				xmlBuff.append("</Detail1>");
				xmlBuff.append("<Detail3 objContext=\"3\" objName=\"drcrpay_cr\" domID=\"").append("1").append("\" dbID=''>");
				xmlBuff.append("<attribute selected=\"N\" updateFlag=\"A\" status=\"N\" pkNames=\"\"/>");
				xmlBuff.append("<tran_id/>");
				xmlBuff.append("<line_no><![CDATA[").append("1").append("]]></line_no>");
				xmlBuff.append("<amount><![CDATA[").append(diffAmount).append("]]></amount>");
				xmlBuff.append("<acct_code><![CDATA[").append(acctCodeDr).append("]]></acct_code>");//(07052007)
				xmlBuff.append("<cctr_code><![CDATA[").append(cctrCodeDr).append("]]></cctr_code>");//(`)
				xmlBuff.append("</Detail3>");
			}
			xmlBuff.append("</Header0>");
			xmlBuff.append("</group0>");
			xmlBuff.append("</DocumentRoot>");
			xmlString = xmlBuff.toString();	
			System.out.println("Master statefull xmlString:------------->"+xmlString);
			writeLog(filePtr,"Master statefull xmlString:------------->"+xmlString,true); //ADDED BY ALKA 24/08/07 FOR SOPs TO BE ADDED IN LOG FILE
			System.out.println("DiffAmount:::::["+diffAmount+"]");
			writeLog(filePtr,"DiffAmount:::::["+diffAmount+"]",true); //ADDED BY ALKA 24/08/07 FOR SOPs TO BE ADDED IN LOG FILE
			
			if(diffAmount != 0)
			{
				retString = saveData(siteCode,xmlString);
			}
			
			if (retString.indexOf("Success") > -1)
			{
				retString = "S";
			}
			else
			{
				retString = "F";
			}
		}		
		catch (Exception e)
		{
			System.out.println("Error in insertDrCrNote ::: " + e.getMessage());
			e.printStackTrace();
			writeLog(filePtr,e,true); //ADDED BY ALKA 22/08/07 FOR WRITING IN LOG FILE FOR EXCEPTION THROWN
			throw new ITMException(e);
		}
		finally
		{
			try
			{
				conn.close(); conn = null;
			}
			catch(Exception ex)
			{
				writeLog(filePtr,ex,true); //ADDED BY ALKA 22/08/07 FOR WRITING IN LOG FILE FOR EXCEPTION THROWN
			}
		}
		System.out.println("retString in insertDrCrNote ::::: 888888888888888888888888888888 :::::" + retString);
		writeLog(filePtr,"retString in insertDrCrNote ::::: 888888888888888888888888888888 :::::" + retString,true); //ADDED BY ALKA 24/08/07 FOR SOPs TO BE ADDED IN LOG FILE
		return retString;
	}
	
	private String saveData(String siteCode,String xmlString) throws ITMException
	{
		  System.out.println("saving data...........");
		  writeLog(filePtr,"saving data...........",true); //ADDED BY ALKA 24/08/07 FOR SOPs TO BE ADDED IN LOG FILE
		  InitialContext ctx = null;
		  String retString = null;
		  MasterStatefulLocal masterStateful = null;	// for ejb3
		  try
		  {
			   AppConnectParm appConnect = new AppConnectParm();
			   ctx = new InitialContext(appConnect.getProperty());
			   // for ejb3
			    masterStateful = (MasterStatefulLocal)ctx.lookup("ibase/MasterStatefulEJB/local");
			   //MasterStateful masterStateful = masterStatefulHome.create();
			   String [] authencate = new String[2];
			   authencate[0] = "";
			   authencate[1] = "";
			   System.out.println("xmlString :::: " + xmlString);
			   writeLog(filePtr,"xmlString in saveData :::: " + xmlString,true); //ADDED BY ALKA 24/08/07 FOR SOPs TO BE ADDED IN LOG FILE
			   retString = masterStateful.processRequest(authencate, siteCode, true, xmlString);
		 }
		 catch(ITMException itme)
		 {
		   	System.out.println("ITMException :PaymentRequestEJB :saveData :==>");
			writeLog(filePtr,itme,true); //ADDED BY ALKA 22/08/07 FOR WRITING IN LOG FILE FOR EXCEPTION THROWN
			throw itme;
		 }
		 catch(Exception e)
		 {
		  	System.out.println("Exception :PaymentRequestEJB :saveData :==>");
			writeLog(filePtr,e,true); //ADDED BY ALKA 22/08/07 FOR WRITING IN LOG FILE FOR EXCEPTION THROWN
			throw new ITMException(e);
		 }
	  	 return retString;
	}

	public java.util.Date calcDueDate(String crTerm, String tranDate, String effDate, String billDate, Connection conn) throws ITMException
	{
		java.util.Date dueDate = new Date();
		int crDays = 0;
		String startFrom = "";
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = null;
		try
		{
			sql =	" SELECT CR_DAYS, START_FROM FROM CRTERM WHERE CR_TERM = ?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, crTerm);
			rs = pstmt.executeQuery();
			if (rs.next())
			{
				crDays = rs.getInt(1);
				startFrom = rs.getString(2);
			}
			rs.close();
			pstmt.close();
			
			if (startFrom.equalsIgnoreCase("R") && tranDate != null && tranDate.trim().length() != 0)
			{
				dueDate = utilMethods.RelativeDate(genericUtility.getDateObject(tranDate), crDays);
			}
			else if ((startFrom.equalsIgnoreCase("D") || startFrom.equalsIgnoreCase("Q")) && effDate != null && effDate.trim().length() != 0)
			{
				dueDate = utilMethods.RelativeDate(genericUtility.getDateObject(effDate), crDays);
			}
			else if (startFrom.equalsIgnoreCase("B") && billDate != null && billDate.trim().length() != 0)
			{
				dueDate = utilMethods.RelativeDate(genericUtility.getDateObject(billDate), crDays);
			}
			else
			{
				writeLog(filePtr,"Date is not in proper format or is null",true); //ADDED BY ALKA 22/08/07 FOR WRITING IN LOG FILE FOR EXCEPTION THROWN
				throw new Exception("Date is not in proper format or is null");
			}
		}
		catch (Exception e)
		{
			System.out.println("Error in calcDueDate ::: " + e.getMessage());
			e.printStackTrace();
			writeLog(filePtr,"Error in calcDueDate ::: " + e.getMessage(),true); //ADDED BY ALKA 22/08/07 FOR WRITING IN LOG FILE FOR EXCEPTION THROWN
			throw new ITMException(e);
		}
		return dueDate;
	}
	
	public String getPurcRate(String purcOrder, String itemCode, Connection conn)
	{
		String provPurcOrder = "", disc = "", rate = "", retString = "";
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = null;
		try
		{
			sql =	" SELECT CASE WHEN PROVI_TRAN_ID IS NULL THEN ' ' ELSE PROVI_TRAN_ID END FROM PORDER WHERE PURC_ORDER = ?";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, purcOrder);
			rs = pstmt.executeQuery();
			if (rs.next())
			{
				provPurcOrder = rs.getString(1);
			}
			rs.close();
			pstmt.close();
			
			if (purcOrder == provPurcOrder)
			{
				sql = " SELECT RATE, DISCOUNT FROM PORDER A, PORDDET B WHERE A.PURC_ORDER = B.PURC_ORDER "
					+ " AND A.PURC_ORDER <> ? AND A.PROVI_TRAN_ID = ? AND B.ITEM_CODE = ? AND A.CONFIRMED = 'Y' ";
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1, purcOrder);
				pstmt.setString(2, purcOrder);
				pstmt.setString(3, itemCode);
				rs = pstmt.executeQuery();
				if (rs.next())
				{
					rate = rs.getString(1);
					disc = rs.getString(2);
				}
				rs.close();
				pstmt.close();
			}
			else
			{
				rate = "-999";
				disc = "-999";
			}
		}
		catch (Exception e)
		{
			System.out.println("Error in getPurcRate ::: " + e.getMessage());
			writeLog(filePtr,e,true); //ADDED BY ALKA 22/08/07 FOR WRITING IN LOG FILE FOR EXCEPTION THROWN
			e.printStackTrace();
		}
		retString = rate + "," + disc;
		System.out.println("retString from getPurcRate :::" + retString);
		writeLog(filePtr,"retString from getPurcRate :::" + retString,true); //ADDED BY ALKA 24/08/07 FOR SOPs TO BE ADDED IN LOG FILE
		return retString;
	}
	private String updateMessage(String resultString,String message)
    {
        StringBuffer stbf = new StringBuffer();
        try{
            System.out.println("resultString : "+resultString);
            stbf.append(resultString.substring(0,resultString.indexOf("<trace>")));
            if(message != null && message.trim().length() > 0){
                stbf.append("<trace>"+message+"</trace>");
                stbf.append(resultString.substring(resultString.indexOf("</trace>")+8));
            }else{
                stbf.append(resultString.substring(resultString.indexOf("<trace>")));
            }
            System.out.println("Resulting String : "+stbf.toString());
            
        }catch(Exception e){
            System.out.println("Exception in updateMessage : "+e);
			writeLog(filePtr,e,true); //ADDED BY ALKA 22/08/07 FOR WRITING IN LOG FILE FOR EXCEPTION THROWN
            e.printStackTrace();
        }
     return stbf.toString();   
    }

	    private static void writeLog(File f,String ex,boolean flag)
	{
		try
		{
			PrintWriter pw = new PrintWriter((new FileOutputStream(f,flag)),flag);
			pw.println(ex);
			pw.close();
		}
		catch(Exception exWm){exWm.printStackTrace();}
	}
	
	private static void writeLog(File f,Exception ex,boolean flag)
	{
		try
		{
			PrintWriter pw = new PrintWriter((new FileOutputStream(f,flag)),flag);
			ex.printStackTrace(pw);
			pw.close();
		}
		catch(Exception exWe){exWe.printStackTrace();}
	}
}