package com.reallinkbilling.utils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.SessionAware;

import com.reallinkbilling.db.GetDBConnection;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

public class GetProductStockByName extends ActionSupport{

	/**
	 * 
	 */
	private static final long serialVersionUID = -2236686235982908953L;
	public static String getProductStockByName(String productId, String productCode, SessionMap<String, Object> sessioMap)
	{
		String productStock = "";
		
		
		Map<String, Object> session = ActionContext.getContext().getSession();
		
		
		
		Connection connection = null;
		PreparedStatement preparedStatement= null;
		ResultSet  resultSet = null;
		
		String sql = "";
		String db = (String) session.get("db");
	
		
		try
		{
			String shopId = (String) sessioMap.get("shopId");
    		String shopCode = (String) sessioMap.get("shopCode");
    		
    		
			connection = GetDBConnection.getDBConnection(db);
			sql = "SELECT "
					+ "in_stock_quantity "
					+ "FROM "
					+ "shop_product_master "
					+ "WHERE "
					+ "product_id = ? "
					+ "AND "
					+ "shop_id = ? "
					+ "AND "
					+ "shop_code = ? ";
			
			preparedStatement = connection.prepareStatement(sql);
			
			preparedStatement.setString(1, productId);
			preparedStatement.setString(2, shopId);
			preparedStatement.setString(3, shopCode);
			
		//System.out.println(preparedStatement.toString());
			resultSet = preparedStatement.executeQuery();
			
			if(resultSet.next())
			{
				productStock = resultSet.getString(1);
			}
			
		}catch (SQLException e) {
	 		
			productStock = "NA";
	 		
	 	}
		catch (Exception e) {
			productStock = "NA";
		}
		finally
		{
			
			if(resultSet != null)
			{
				try {
					resultSet.close();
					resultSet = null;
				} catch (SQLException e) {
				
				}
			}
			if(preparedStatement != null)
			{
				try {
					preparedStatement.close();
					preparedStatement = null;
				} catch (SQLException e) {
					
				}
			}
			
			if(connection != null)
			{
				try {
					connection.close();
					connection = null;
				} catch (SQLException e) {
					
				}
			}
			
			
		}
		
		
		return productStock;
	}
	
	






public static String getProductStockByName2ndQty(String productId, String productCode, SessionMap<String, Object> sessioMap)
{
	String productStock = "";
	
	
	Map<String, Object> session = ActionContext.getContext().getSession();
	
	
	
	Connection connection = null;
	PreparedStatement preparedStatement= null;
	ResultSet  resultSet = null;
	
	String sql = "";
	String db = (String) session.get("db");

	
	try
	{
		String shopId = (String) sessioMap.get("shopId");
		String shopCode = (String) sessioMap.get("shopCode");
		
		
		connection = GetDBConnection.getDBConnection(db);
		sql = "SELECT "
				+ "in_stock_quantity_2 "
				+ "FROM "
				+ "shop_product_master "
				+ "WHERE "
				+ "product_short_code = ? "
				+ "AND "
				+ "product_id = ? "
				+ "AND "
				+ "shop_id = ? "
				+ "AND "
				+ "shop_code = ? ";
		
		preparedStatement = connection.prepareStatement(sql);
		
		preparedStatement.setString(1, productCode);
		preparedStatement.setString(2, productId);
		preparedStatement.setString(3, shopId);
		preparedStatement.setString(4, shopCode);
		
	
		resultSet = preparedStatement.executeQuery();
		
		if(resultSet.next())
		{
			productStock = resultSet.getString(1);
		}
		
	}catch (SQLException e) {
 		
		productStock = "NA";
 		
 	}
	catch (Exception e) {
		productStock = "NA";
	}
	finally
	{
		
		if(resultSet != null)
		{
			try {
				resultSet.close();
				resultSet = null;
			} catch (SQLException e) {
			
			}
		}
		if(preparedStatement != null)
		{
			try {
				preparedStatement.close();
				preparedStatement = null;
			} catch (SQLException e) {
				
			}
		}
		
		if(connection != null)
		{
			try {
				connection.close();
				connection = null;
			} catch (SQLException e) {
				
			}
		}
		
		
	}
	
	
	return productStock;
}



}