package ibase.webitm.ejb.sys;

import java.sql.*;
import java.util.*;
import java.text.NumberFormat;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import ibase.webitm.ejb.*;
import ibase.webitm.utility.*;
import ibase.webitm.utility.GenericUtility;

public class SysCommon
{
	GenericUtility genericUtility = GenericUtility.getInstance();
	public static String nfCheckPeriod(String modCode, java.sql.Timestamp tranDate , String siteCode, Connection conn) throws Exception
	{
		String errCode = "";
		String prdCode= "";
		String stat = "";
		String sql = "";
		java.sql.Timestamp procDate = null;
		ResultSet rs = null;
		PreparedStatement pstmt = null;
		String fldPart = null;
		GenericUtility genericUtility = new ibase.webitm.utility.GenericUtility();//.getInstance();
		try
		{
			if ("DIS".equals(modCode.toUpperCase()))
			{
				fldPart = "IC";
			}
			else
			{
				fldPart = modCode;
			}

			if("ADM".equals(modCode.toUpperCase()) || "FIN".equals(modCode.toUpperCase()) || "PUR".equals(modCode.toUpperCase())|| "SAL".equals(modCode.toUpperCase())|| "MFG".equals(modCode.toUpperCase())|| "IC".equals(modCode.toUpperCase())|| "DIS".equals(modCode.toUpperCase()) )
			{
				sql = "SELECT CODE FROM PERIOD WHERE ? BETWEEN FR_DATE AND TO_DATE";
				System.out.println( "sql in nbCheckPeriod:" + sql );
				pstmt = conn.prepareStatement(sql);
				pstmt.setTimestamp(1,tranDate);
				rs  = pstmt.executeQuery();
				if(rs.next())
				{
					prdCode = rs.getString(1);
				}
				else
				{
					errCode = "VTPRD1";
				}
				rs.close();
				pstmt.close();
				rs = null;
				pstmt = null;
				if (errCode.length() == 0)
				{
					sql = "SELECT STAT_" + fldPart + ",PROC_DATE FROM PERIOD_STAT WHERE SITE_CODE = '" + siteCode + "' AND PRD_CODE = '" + prdCode + "'";
					pstmt = conn.prepareStatement(sql);
					rs  = pstmt.executeQuery();

					if(rs.next())
					{
						stat = rs.getString(1);
						procDate = rs.getTimestamp(2);
						if (stat.equals("N"))
						{
							errCode = "VTPRD" + fldPart;
						}
						else if (procDate.compareTo(tranDate) > 0 )
						{
							errCode = "VTPRDT" + fldPart;
						}
					}
					rs.close();
					pstmt.close();
					rs = null;
					pstmt = null;
				}
			}
		}
		catch (Exception e)
		{
			System.out.println("Exception :SysCommon :nfCheckPeriod:" + e.getMessage() + ":");
			throw e;
		}
		return errCode;
	}
	public static void CheckValidity() throws Exception
	{
		//ibase.webitm.ejb.sys.GenValBean gvb= new ibase.webitm.ejb.sys.GenValBean();
		//gvb.CheckCredential();
		
	}	
}//class