
package ibase.webitm.ejb.wms;
import ibase.system.config.*;
import ibase.webitm.ejb.*;
import ibase.webitm.ejb.dis.DistCommon;
import ibase.webitm.ejb.dis.InvAllocTraceBean;
import ibase.webitm.ejb.dis.StockUpdate;
import ibase.webitm.utility.GenericUtility;
import ibase.webitm.utility.ITMException;
import ibase.webitm.ejb.ITMDBAccessEJB;
import ibase.webitm.utility.TransIDGenerator;
import ibase.utility.CommonConstants;

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

import org.w3c.dom.*;

import javax.ejb.*;
import javax.naming.InitialContext;
@javax.ejb.Stateless
public class ShipmentPos extends ValidatorEJB implements ShipmentPosLocal, ShipmentPosRemote
{
	public String postSave()throws RemoteException,ITMException
	{
		return "";
	}
	public String postSave(String domString, String tranId,String editFlag, String xtraParams, Connection conn ) throws RemoteException,ITMException
	{

		String errString = "";
		String sql = "";
		String transporterCode = "";
		String waveTranCode = "";
		String despTranCode = "";
		String refId = "";
		String shipmentId = "";
		String waveId = "";
		String PTCN = "";
		int shipupdCnt = 0;
		int desupdCnt = 0;
		int waveupdCnt = 0;
		Document dom = null;
		GenericUtility genericUtility = GenericUtility.getInstance();
		java.util.Date currDate1 = new java.util.Date();
		DistCommon distCommon = new DistCommon();
		PreparedStatement pstmt = null;
		ResultSet rs = null;

		try
		{
			String applDateFormat = genericUtility.getApplDateFormat();
			String dbDateFormat = genericUtility.getDBDateFormat();
			SimpleDateFormat sdf = new SimpleDateFormat(applDateFormat);
			String currDateStr = sdf.format(currDate1);
			java.sql.Timestamp tranDate = java.sql.Timestamp.valueOf(genericUtility.getValidDateString( currDateStr , applDateFormat, dbDateFormat ) + " 00:00:00.0") ;
			conn.setAutoCommit(false);
			dom = genericUtility.parseString(domString);
			shipmentId = checkNull(genericUtility.getColumnValueFromNode("shipment_id", dom.getElementsByTagName("Detail1").item(0)));
			
		//  Changed by sankara on 16/08/13 for checking transporter code null	
			//transporterCode = genericUtility.getColumnValueFromNode("tran_code", dom.getElementsByTagName("Detail1").item(0));
			transporterCode = checkNull(genericUtility.getColumnValueFromNode("tran_code", dom.getElementsByTagName("Detail1").item(0)));
			//Changed By Pragyan to get PTCN
			/*sql = " SELECT D.REF_ID AS REF_ID, P.WAVE_ID AS WAVE_ID  FROM SHIP_DOCS D, SHIPMENT S, DESPATCH P WHERE S.SHIPMENT_ID = D.SHIPMENT_ID" +
					" AND P.DESP_ID = D.REF_ID AND  S.SHIPMENT_ID = ?  ";*/
			sql = " SELECT D.REF_ID AS REF_ID, P.WAVE_ID AS WAVE_ID,D.PTCN AS PTCN  FROM SHIP_DOCS D, SHIPMENT S, DESPATCH P WHERE S.SHIPMENT_ID = D.SHIPMENT_ID" +
			" AND P.DESP_ID = D.REF_ID AND  S.SHIPMENT_ID = ?  ";

			pstmt = conn.prepareStatement(sql);
			pstmt.setString( 1, shipmentId);
			rs = pstmt.executeQuery();
			if(rs.next())
			{
				refId = checkNull(rs.getString("REF_ID"));
				waveId = checkNull(rs.getString("WAVE_ID"));
				PTCN = checkNull(rs.getString("PTCN"));
			}
			rs.close(); 
			rs = null;
			pstmt.close();
			pstmt = null;

			//sql = " SELECT TRAN_CODE FROM WAVE_STATUS_ORG WHERE WAVE_ID = ? ";
			sql = " SELECT TRAN_CODE FROM WAVE_STATUS_ORG WHERE PTCN = ? ";
			pstmt = conn.prepareStatement(sql);
			//pstmt.setString( 1, waveId);
			pstmt.setString( 1, PTCN);
			rs = pstmt.executeQuery();
			
			if(rs.next())
			{
				waveTranCode = checkNull(rs.getString("TRAN_CODE"));
				System.out.println("WAVETRANCODE=="+waveTranCode);

			}
			rs.close(); 
			rs = null;
			pstmt.close();
			pstmt = null;

			/*if( ! transporterCode.trim().equalsIgnoreCase( waveTranCode.trim() ) )
			{*/
				sql = " UPDATE SHIP_DOCS SET TRAN_CODE = ? WHERE SHIPMENT_ID IN ( SELECT D.SHIPMENT_ID FROM SHIPMENT S, SHIP_DOCS D " +
						" WHERE S.SHIPMENT_ID = D.SHIPMENT_ID AND S.SHIPMENT_ID = ? ) ";		
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1, transporterCode);
				pstmt.setString(2, shipmentId);
				shipupdCnt = pstmt.executeUpdate();
				pstmt.close();
				pstmt = null;    

				sql = " UPDATE DESPATCH SET TRAN_CODE = ? WHERE DESP_ID = ? ";
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1, transporterCode);
				pstmt.setString(2, refId);
				desupdCnt = pstmt.executeUpdate();
				pstmt.close();
				pstmt = null;

				//Changed By Pragyan 
				//sql = " UPDATE WAVE_STATUS_ORG SET PRINT_STATUS = 'N' WHERE WAVE_ID = ? ";
				sql = " UPDATE WAVE_STATUS_ORG SET PRINT_STATUS = ? WHERE PTCN = ? ";
				pstmt = conn.prepareStatement(sql);
				//pstmt.setString(1, waveId);
				if( ! transporterCode.trim().equalsIgnoreCase( waveTranCode.trim() )  || isPacketsChanged(PTCN,conn))
				{
					pstmt.setString(1, "N");
				}
				else
				{
					pstmt.setString(1, "Y");
				}
				pstmt.setString(2, PTCN);
				waveupdCnt = pstmt.executeUpdate();
				pstmt.close();
				pstmt = null;
			//}
			
			if( shipupdCnt > 0 && desupdCnt > 0 && waveupdCnt > 0  )
			{
				System.out.println("SHIPUPDCNT=["+shipupdCnt+"] DESUPDCNT=["+desupdCnt+"] WAVEUPDCNT=["+waveupdCnt+"] " );
			}	
			else
			{
				System.out.println("Updated Rows count is zero");
			}
			//Changed by Rohan to update gross weight in shipment content from shipmet headere.start
			
			if((!"".equalsIgnoreCase(editFlag) || editFlag != null) )
			{
				sql = " UPDATE SHIPMENT_CONTENT SET GROSS_WEIGHT = (SELECT GROSS_WEIGHT FROM SHIPMENT WHERE SHIPMENT_ID = ? ) WHERE SHIPMENT_ID = ? ";
				pstmt = conn.prepareStatement(sql);
				pstmt.setString(1,shipmentId);
				pstmt.setString(2,shipmentId);
				int cnt = pstmt.executeUpdate();
				if(cnt > 0)
				{
					System.out.println("Gross weight update suceesfuly");
				}
				pstmt.close();
				pstmt = null;
						
			}
			//Changed by Rohan to update gross weight in shipment content from shipment header.end
		}
		catch(Exception e)
		{
			System.out.println("Exception "+e.getMessage());
			e.printStackTrace();
			throw new ITMException(e);
		}
		finally
		{
			try
			{
				if( rs != null )
				{
					rs.close();
					rs = null;
				}
				if( pstmt != null )
				{
					pstmt.close();
					pstmt = null;
				}
			}
			catch(Exception e)
			{
				System.err.println("Exception :DespatchTranPos :\n"+e.getMessage());
				e.printStackTrace();
				throw new ITMException(e);
			}
		}
		return errString;
	}
	//Changed by sankara on 16/08/13 for checking null start.
	private String checkNull( String input )	
	{
	  if ( input == null )
	  {
			input = "";
	  }
	   return input;
	}
	//Changed by sankara on 16/08/13 for checking null end.
	
	private boolean isPacketsChanged(String PTCN, Connection conn) throws ITMException
	{
		String sql = "";
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		double count = 0;
		boolean isPacketDataChanged = false;
		
		try
		{
			sql = "SELECT COUNT(*) FROM CARTON_MASTER WHERE PTCN = ? AND CHANGE_STATUS  = ? ";
			pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, PTCN);
			pstmt.setString(2, "Y");
			
			rs = pstmt.executeQuery();
			if( rs.next())
			{
				count = rs.getDouble(1);
			}
			rs.close(); rs = null;
			pstmt.close(); pstmt = null;
			System.out.println(" item count ["+count+"]");
			
			if(count > 0)
			{
				isPacketDataChanged = true;
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
			throw new ITMException(e);
		}
		finally
		{
			try
			{
				if( rs != null)
				{
					rs.close(); rs = null;
				}
				if( pstmt != null )
				{
					pstmt.close(); pstmt = null;
				}
			}
			catch(Exception es)
			{
				es.printStackTrace();
				throw new ITMException(es);
			}
		}
		
		return isPacketDataChanged;
	}
}

