package ibase.webitm.ejb.fin;

import ibase.webitm.ejb.ValidatorEJB;
import ibase.webitm.utility.GenericUtility;
import ibase.webitm.utility.ITMException;

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

import org.w3c.dom.Document;
@javax.ejb.Stateless
public class MiscRcpPostSave extends ValidatorEJB implements MiscRcpPostSaveLocal, MiscRcpPostSaveRemote
{
	public String postSave()throws RemoteException,ITMException
	{
		return "";
	}

	public String postSave( String domString, String editFlag, String xtraParams, Connection conn ) throws RemoteException,ITMException
	{

		System.out.println("In Misc RCP post Save ... ");

		PreparedStatement pstmt = null,pstmt1=null;
		Document dom = null;                
		ResultSet rs =null,rs1=null;
		boolean isError = false;

		String tranId = "";
		String sql = null,sql1=null;

		double Amt = 0, adjAmt = 0, netAmt = 0, netAmtBc = 0, vouchAmt = 0, billAmt = 0, taxAmt = 0, totAmt = 0,tottaxAmt = 0;
		double diffAmtExchLine=0.0;
		double netAmtRcp = 0, advAmt = 0, netAmtDet = 0, netAmtBcRcp = 0, taxAmtDet = 0, totAmtDet = 0, netAmtBcDet = 0 ;
		double advAmtAdj = 0, taxedAdjAmt = 0, advAmtDet = 0, taxAmtAdv = 0;
		String hdrVoucherType="",exchRate="";
		int lineno =1;		 
		int cnt=0,cnt1=0,listCnt=0,exchrate = 0;
		try
		{

			GenericUtility genericUtility = GenericUtility.getInstance();
			dom = genericUtility.parseString(domString);

			tranId = GenericUtility.getInstance().getColumnValue("tran_id",dom);
			
			/**********COde For Fetching Data from Detail Window*****/		
			
			// Detail Screen cross Update
						sql="select amount,tax_amt,line_no from misc_rcpdet where tran_id = ? ";
						pstmt = conn.prepareStatement(sql);
						pstmt.setString(1,tranId );
						
						rs = pstmt.executeQuery();
						while(rs.next()){
							Amt = rs.getDouble("amount");
							taxAmt = rs.getDouble("tax_amt");
							lineno = rs.getInt("line_no");
							netAmt = Amt +taxAmt;
							System.out.println("Amount Value >>>> "+Amt);
							System.out.println("Tax Amount Value >>>> "+taxAmt);
							System.out.println("Net Amount Value >>>> "+netAmt);
							
				 //To set value for net amt for details i.e summation f amount + tax amount
							sql1="update misc_rcpdet set net_amt = ? where tran_id = ?  and line_no = ?";
							pstmt1 = conn.prepareStatement(sql1);
							pstmt1.setDouble(1,netAmt);
							pstmt1.setString(2,tranId);
							pstmt1.setInt(3,lineno);
							cnt1 = pstmt1.executeUpdate();
							pstmt1.close();
							pstmt1 = null;
				System.out.println("Phase 1 executed ******");
							
						}
						rs.close();
						rs = null;
						pstmt.close();
						pstmt = null;
						
		  
					
			//selecting summation amounts from detail
			sql = "select sum(net_amt) as totnetamt,sum(amount)as totamt,sum(tax_amt) as tot_taxamt from misc_rcpdet where tran_id = ? ";
			  pstmt = conn.prepareStatement(sql);
			  pstmt.setString(1,tranId);
			  rs  = pstmt.executeQuery();
			  if(rs.next())
			  {
				  netAmtDet = rs.getDouble("totnetamt");
				  totAmt = rs.getDouble("totamt");
				  tottaxAmt = rs.getDouble("tot_taxamt");
			  }
			    rs.close();
				rs = null;
				pstmt.close();
				pstmt = null;
				
			System.out.println("Total net Amount for header = >>>>"+netAmtDet);
			
			// selecting exch rate from header
			sql = "select exch_rate from misc_receipt where tran_id = ? ";
			  pstmt = conn.prepareStatement(sql);
			  pstmt.setString(1,tranId);
			  rs  = pstmt.executeQuery();
			  if(rs.next())
			  {
				  exchrate = rs.getInt("exch_rate");
			  }
			    rs.close();
				rs = null;
				pstmt.close();
				pstmt = null;
			
				netAmtBc = exchrate * netAmtDet;
				
				// selecting DIFF_AMT__EXCH from header
				sql = "select sum(diff_amt__exch) as totdiffamt_exch from misc_pay_rcp_trace where tran_id__rcp = ?";
				  pstmt = conn.prepareStatement(sql);
				  pstmt.setString(1,tranId);
				  rs  = pstmt.executeQuery();
				  if(rs.next())
				  {
					  diffAmtExchLine = rs.getInt("totdiffamt_exch");
				  }
				    rs.close();
					rs = null;
					pstmt.close();
					pstmt = null;
					
			   //Updating Header
			sql="update misc_receipt set net_amt = ?,net_amt__bc = ?,amount =? ,tax_amt = ?,diff_amt__exch = ?   where tran_id = ? ";
			pstmt = conn.prepareStatement(sql);
			pstmt.setDouble(1,netAmtDet);
			pstmt.setDouble(2,netAmtBc);
			pstmt.setDouble(3, totAmt);
			pstmt.setDouble(4,tottaxAmt);
			pstmt.setDouble(5,diffAmtExchLine);
			pstmt.setString(6,tranId);
			cnt = pstmt.executeUpdate();
			pstmt.close();
			pstmt = null;
			
			System.out.println("Header updated *******");
			
		}
		catch(Exception e)
		{
			try {
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			isError = true;
			System.out.println("Exception :MiscRcpPostSaveEJB : :==>\n"+e.getMessage());
			e.printStackTrace();
			throw new ITMException(e);
		}
		finally
		{
			try
			{
				System.out.println("isError ["+isError+"]");
				if( conn != null )
				{

					if( isError )
					{
						conn.rollback();
					}
					else
					{
						conn.commit();
					}
				}
				if(pstmt != null)
				{
					pstmt.close();
					pstmt = null;
				}
			}
			catch(Exception e)
			{
				System.out.println("Exception :MiscRcpPostSaveEJB : :==>\n"+e.getMessage());
				try
				{
					System.out.println("Before rollback");
					conn.rollback();
				}

				catch(SQLException sqle)
				{
					System.out.println(sqle);
				}
				throw new ITMException(e);
			}
		}
		System.out.println("Out of Miscelleneous Receipt ");
		return "";                
	}
}
