package com.reallinkbilling.ajax;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.reallinkbilling.db.GetDBConnection;

public class GetPurchaseProductDetails extends ActionSupport implements SessionAware{
	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 4526288239167750659L;
	private HttpSession session;
	private String purchaseUnit;
	private String purchaseRate;
	private String tax;
	private String productFullName;
	private String aliasValue;
	
	private SessionMap<String,Object> sessionMap;  

	public String execute(){
		
		
		 
		String result = "login";;
		session=ServletActionContext.getRequest().getSession(false); 
		
	        if(session==null || session.getAttribute("login")==null){ 
	        	addActionMessage("Please Login First !!!");
	        	result = "login";
	        }  
	        else{
	        	
	        	String shopId = (String) this.getSessionMap().get("shopId");
	    		String shopCode = (String) this.getSessionMap().get("shopCode");
	    		
	    		String [] productData = new String [3];
	    		String prName = "";
	    		String prId = "";
	    		String prAlias = "";
	    		try
	    		{
	    			productData = this.getAliasValue().split("-");
	    			prName = productData[1].trim().toUpperCase();
	    			prId =  productData[0].trim().toUpperCase();
	    			prAlias = productData[2].trim().toUpperCase();
	    			
	    		}catch(Exception e)
	    		{
	    			prAlias = this.getAliasValue().trim().toUpperCase();
	    			prName = this.getAliasValue().trim().toUpperCase();
	    		}
	    		
	        	Connection connection = null;
				PreparedStatement preparedStatement= null;
				ResultSet resultSet = null;
				
				String sql = "";
				String db = (String) this.getSessionMap().get("db");
				
			
					try
					{
						
						connection = GetDBConnection.getDBConnection(db);
						
						sql = "SELECT "
								+ "product_id,"
								+ "product_name_eng,"
								+ "purchase_rate,"
								+ "purchase_unit,"
								+ "tax_id,"
								+ "tax_percentage,"
								+ "product_short_code "
								+ "FROM "
								+ "shop_product_master "
								+ "WHERE "
								+ "shop_id = ? "
								+ "AND "
								+ "shop_code = ? "
								+ "AND "
								+ "(product_alias = '"+prAlias+"' "
								+ "OR product_name_eng ='"+prName+"')";
					
						
						preparedStatement = connection.prepareStatement(sql);
					
						preparedStatement.setString(1, shopId);
						preparedStatement.setString(2, shopCode);
						resultSet = preparedStatement.executeQuery();
						
						resultSet.last();
						int rowCount = resultSet.getRow();
						resultSet.beforeFirst();
						if(rowCount>0)
						{
							
							while(resultSet.next())
							{
								this.setProductFullName(resultSet.getString(1)+"-"+resultSet.getString(2)+"-"+resultSet.getString(7));
								this.setPurchaseRate(resultSet.getString(3));
								this.setPurchaseUnit(resultSet.getString(4));								
								this.setTax(resultSet.getString(5));
								
							}
								
						}
						
						result = "success";
					}catch (SQLException e) {
						
					//e.printStackTrace();
					} catch (IOException e) {
						
					//e.printStackTrace();
					}finally {
						if(resultSet != null)
		   				{
		   					try {resultSet.close(); resultSet = null;}catch(SQLException sqe){}   					
		   				}
						if(preparedStatement != null)
						{
							try {preparedStatement.close(); preparedStatement = null;} catch (SQLException e) {}
						}
						if(connection != null)
						{
							
							try {connection.close(); connection = null;}catch (SQLException e) {}
						}
						
					}
	        }
	
	return result;
}


	public String getPurchaseUnit() {
		return purchaseUnit;
	}


	public void setPurchaseUnit(String purchaseUnit) {
		this.purchaseUnit = purchaseUnit;
	}


	public String getPurchaseRate() {
		return purchaseRate;
	}


	public void setPurchaseRate(String purchaseRate) {
		this.purchaseRate = purchaseRate;
	}


	public String getTax() {
		return tax;
	}


	public void setTax(String tax) {
		this.tax = tax;
	}


	public String getProductFullName() {
		return productFullName;
	}


	public void setProductFullName(String productFullName) {
		this.productFullName = productFullName;
	}


	public String getAliasValue() {
		return aliasValue;
	}


	public void setAliasValue(String aliasValue) {
		this.aliasValue = aliasValue;
	}
	
	
	public SessionMap<String, Object> getSessionMap() {
		return sessionMap;
	}

	public void setSessionMap(SessionMap<String, Object> sessionMap) {
		this.sessionMap = sessionMap;
	}

	
	public void setSession(Map<String, Object> map) {  
	    sessionMap=(SessionMap<String, Object>)map;  
	}

	
	
	

}
