package ibase.webitm.utility.wms;
import ibase.webitm.utility.ITMException;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
public  class CommonWmsUtil {
	
        public static  int ALL_PTCN_EX_COUNT;
        
        public CommonWmsUtil()
    	{
    	}

    	public static CommonWmsUtil getInstance()
    	{
    		return new ibase.webitm.utility.wms.CommonWmsUtil();
    	}
    	
      //Changed By Pragyan 11-AUG-14 To check and Facility Master implementation.start
    	public boolean isValFacLocation(String locCode,String siteCode,Connection conn) throws ITMException
    	{
    		PreparedStatement pstmt=null;
            ResultSet rs = null;
            double partialQty=0.0;
            double packSize=0.0;
            boolean isValidLocation = false;
            int count = 0;
            
    		try {
    			String sql ="SELECT FN_CHECK_VALID_LOC(?,?) FROM DUAL";
    			pstmt =conn.prepareStatement(sql);
    			pstmt.setString(1, siteCode);
    			pstmt.setString(2, locCode);

    			rs = pstmt.executeQuery();
    			
    			if(rs.next())
    			{
    				count = rs.getInt(1);
    			}
    			
    			if(count > 0)
    			{
    				isValidLocation = true;
    			}
    			
    			
    			if(rs != null)
    			{
    				rs.close();
    				rs=null;
    			}
    			
    			if(pstmt != null)
    			{
    				pstmt.close();
    				pstmt=null;
    			}
    			
    			
    			
    			
    		} catch (Exception e) {
    			// TODO Auto-generated catch block
    			throw new ITMException(e); 
    		}
    		
    		return isValidLocation;
    	}
  // Start changed by Pragyan on 19-SEP-14  [W14FSUN003]	
    	public String checkNull( String inputVal )
    	{
    		if ( inputVal == null )
    		{
    			inputVal = "";
    		}
    		return inputVal;
    	}
    	
    	public String getFacilityCode(String siteCode,Connection conn) throws ITMException
    	{
    		PreparedStatement pstmt=null;
            ResultSet rs = null;
            int count = 0;
            String facilityCode = "";
            
    		try {
    			String sql ="SELECT facility_code FROM site where site_code = ?";
    			pstmt =conn.prepareStatement(sql);
    			pstmt.setString(1, siteCode);
    			

    			rs = pstmt.executeQuery();
    			
    			if(rs.next())
    			{
    				facilityCode = checkNull(rs.getString(1));
    			}
    			  			
    			
    			if(rs != null)
    			{
    				rs.close();
    				rs=null;
    			}
    			
    			if(pstmt != null)
    			{
    				pstmt.close();
    				pstmt=null;
    			}
    			
    			
    			
    			
    		} 
    		catch (Exception e) 
    		{
    			// TODO Auto-generated catch block
    			throw new ITMException(e); 
    		}
    		
    		return facilityCode;
    	}
    	//Changed By Pragyan 11-AUG-14 To check and Facility Master implementation.end
    // End changed by Pragyan on 19-SEP-14   [W14FSUN003]	
     
    	 //changed by Dadaso Pawar on 04/02/15 [Start]
	    //For get Stock option from item master
	    //stk_opt 0 : No stk maint.
	    //stk_opt 1 : By item maint.
	    //stk_opt 2 : By lot maint.
	    public String getStockOption (String itemCode,Connection conn) throws ITMException,Exception
	    {
	    	System.out.println("-----------in getStockOption method ----------------");	    	
	    	String sql = "",stkOpt = "";
	    	ResultSet rs = null;
	    	PreparedStatement pstmt = null;
	    	itemCode = itemCode == null ? "" :itemCode.trim();
	    	try
	    	{
	    		sql = "SELECT STK_OPT FROM ITEM WHERE ITEM_CODE = ? ";
	    		pstmt = conn.prepareStatement(sql);
	    		pstmt.setString(1, itemCode);
	    		rs = pstmt.executeQuery();
	    		if(rs.next())
	    		{
	    			stkOpt = rs.getString(1) == null ? "" : rs.getString(1).trim();
	    		}
	    	}
	    	catch(Exception e)
	    	{
	    		System.out.println("Exception :CommonWmsUtil :getStockOption :==>\n"+e);
	    		e.printStackTrace();
	    		throw new ITMException(e);
	    	}
	    	return stkOpt;
	    }
}
