package ibase.webitm.ejb.dis;
import java.rmi.RemoteException;
import ibase.webitm.utility.GenericUtility;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.sql.ResultSet;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.*;
import javax.ejb.*;
import ibase.webitm.ejb.*;
import org.w3c.dom.*;
import ibase.webitm.utility.ITMException;
import ibase.utility.CommonConstants;
import org.w3c.dom.*; 
import javax.xml.parsers.*;
import ibase.webitm.utility.*;
import ibase.system.config.*;
import javax.naming.InitialContext;
import ibase.webitm.utility.ITMException;
import ibase.webitm.utility.GenericUtility;
import java.io.File;
import ibase.system.config.ConnDriver;
import javax.ejb.Stateless; 


@Stateless 
public class DissIssuePosSave extends ValidatorEJB implements   DissIssuePosSaveLocal,DissIssuePosSaveRemote // SessionBean
{	

	public String postSave() throws RemoteException,ITMException
	{
		return "";
	}
	public String postSaveRec() throws RemoteException, ITMException
	{
		return "";
	}
	public String postSaveRec(String xmlString1, String domId, String objContext, String editFlag, String xtraParams, Connection conn) throws RemoteException, ITMException
	{
		return "";
	}


	public String postSave(String winName,String editFlag,String tranId,  String xtraParams,Connection conn) throws RemoteException,ITMException
	{
		String sql = "",shipmentId = "";
		GenericUtility genericUtility = GenericUtility.getInstance();
		PreparedStatement pstmt = null;
		String lotsl = "",siteCode="";
		double quantity = 0;
		ResultSet rs = null;		
		double amount = 0,netAmount=0,discAmount=0,taxAmount=0,noArt = 0;	//net_amt =amount+tax_amt-disc_amt
		double grossWeight = 0 ,tareWeight = 0 , netWeight = 0;
		try
		{
			
				
				conn = null;
				ConnDriver connDriver = new ConnDriver();
				conn = connDriver.getConnectDB("DriverITM");
				connDriver= null;
				


			
			
			conn.setAutoCommit(false);

			sql = "select lot_sl from distord_issdet where tran_id = ? ";
			pstmt= conn.prepareStatement(sql);
			//System.out.println("Second sql ["+sql + "]");
			pstmt.setString( 1, tranId );
			rs = pstmt.executeQuery();
			while(rs.next())
			{
				lotsl = rs.getString(1) == null ? "":rs.getString(1);
				
				sql = "select quantity,site_code from distord_issdet where tran_id = ? and lot_sl = ?";
				pstmt= conn.prepareStatement(sql);
				//System.out.println("Second sql ["+sql + "]");
				pstmt.setString( 1, tranId );
				pstmt.setString( 2, lotsl );
				rs = pstmt.executeQuery();
				if(rs.next())
				{
					quantity = rs.getDouble("quantity");
					siteCode = rs.getString("site_code") == null ? "":rs.getString("site_code");
				}
				rs.close();
				rs = null;
				pstmt.close();
				pstmt = null;
				
				sql = "UPDATE stock SET ALLOC_QTY = ? WHERE site_code = ? AND LOT_SL = ? AND QUANTITY > 0;";
				pstmt= conn.prepareStatement(sql);
				//System.out.println("First sql ["+ sql + "] tranId [" + tranId + "]");
				pstmt.setDouble( 1, quantity);
				pstmt.setString( 2, siteCode);
				pstmt.setString( 3, lotsl);
				pstmt.executeUpdate();
				conn.commit();
				pstmt.close();
				pstmt = null;		
				
			}
			rs.close();
			rs = null;
			pstmt.close();
			pstmt = null;
			

			sql = "update distord_issdet  set "
					+"	amount = quantity * rate , "
					+"	disc_amt = quantity * rate * discount/100 ,  " 
					+ "	net_amt = quantity * rate  + tax_amt - (quantity * rate * discount/100 )  "
					+ " where  tran_id = ? ";
			pstmt= conn.prepareStatement(sql);
			//System.out.println("First sql ["+ sql + "] tranId [" + tranId + "]");
			pstmt.setString( 1, tranId );
			pstmt.executeUpdate();
			conn.commit();
			pstmt.close();
			pstmt = null;		

			sql = "select sum(amount), sum(net_amt),sum(disc_amt),sum(tax_amt),sum(no_art) , sum(gross_weight) ,sum (tare_weight) , sum(net_weight )  "
					+ " from distord_issdet where tran_id = ? ";
			pstmt= conn.prepareStatement(sql);
			//System.out.println("Second sql ["+sql + "]");
			pstmt.setString( 1, tranId );
			rs = pstmt.executeQuery();
			if (rs.next())
			{
				amount = rs.getDouble(1);
				netAmount = rs.getDouble(2);
				discAmount = rs.getDouble(3);
				taxAmount = rs.getDouble(4);
				noArt = rs.getDouble(5);
				//added by kunal on 06/AUG/13
				grossWeight = rs.getDouble(6);
				tareWeight = rs.getDouble(7);
				netWeight = rs.getDouble(8);
			}
			rs.close();
			rs = null;
			pstmt.close();
			pstmt = null;

			//added by kunal on 06/AUG/13 
			sql = "select shipment_id  from  distord_iss where tran_id = ? ";
			pstmt= conn.prepareStatement(sql);
			pstmt.setString( 1, tranId );
			rs = pstmt.executeQuery();
			if (rs.next())
			{
				shipmentId = rs.getString(1);
			}
			rs.close();
			rs = null;
			pstmt.close();
			pstmt = null;

			if(shipmentId != null && shipmentId.trim().length() > 0)
			{
				grossWeight = 0;
				tareWeight = 0;
				netWeight = 0;
			}


			sql = " update distord_iss  set "
					+ " amount = ?, net_amt = ?, discount = ? , tax_amt = ? , no_art = ? ,gross_weight = ? , tare_weight = ? , net_weight = ? "
					+ "  where  tran_id = ? " ;
			pstmt= conn.prepareStatement( sql );
			//System.out.println("Fourth sql ["+sql + "]");

			pstmt.setDouble( 1, amount );
			pstmt.setDouble( 2, netAmount );
			pstmt.setDouble( 3, discAmount );
			pstmt.setDouble( 4, taxAmount );
			pstmt.setDouble( 5, noArt );
			pstmt.setDouble( 6, grossWeight );////added by kunal on 06/AUG/13
			pstmt.setDouble( 7, tareWeight );//added by kunal on 06/AUG/13
			pstmt.setDouble( 8, netWeight );//added by kunal on 06/AUG/13
			pstmt.setString( 9, tranId );			
			pstmt.executeUpdate();
			pstmt.close();
			pstmt = null;
			conn.commit();

		}
		catch(Exception e)
		{
			
			try {
				System.out.println(">>>>>>>>>>>>In catch Before rollback>>>");
				conn.rollback();
			} catch (SQLException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
				
			}
			
			e.printStackTrace();
			throw new ITMException(e);
			
		
			
		}
		finally
		{
			try
			{
				if(pstmt != null)pstmt.close();
				pstmt = null;
				
				if(conn != null)
				{
					conn.close();	
				}
			}catch(Exception d)
			{
				d.printStackTrace();
				throw new ITMException(d);
			}
		}
		return "";
	}

}// end of DissIssuePosSaveEJB class 
