package com.reallinkbilling.jspRedirect;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedHashMap;
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.cipher.EncryptionDecryption;
import com.reallinkbilling.db.GetDBConnection;
import com.reallinkbilling.resource.GetListResource;

public class EditProductJsp extends ActionSupport implements SessionAware{

	/**
	 * 
	 */
	private static final long serialVersionUID = -3030419045969989995L;
	

	private HttpSession session;
	private Map<String,String> taxList = new LinkedHashMap<String,String>();
	private Map<String,String> unitList = new LinkedHashMap<String,String>();
	private Map<String,String> supplierList = new LinkedHashMap<String,String>();
	
	private String productAddDate;
	private String productName;
	private Float puchaseRate;
	private String productNameMarathi ;
	private String productCode;
	private Float customerSalesRate;
	private Float retailerSalesRate;
	private Float dealerSalesRate;
	private String rackNumber;
	private String taxId;
	private String Alias;
	private String saleUnit;
	private String purchaseUnit;
	private String lowStockCount;
	private String hsnCode;
	private String convertQuantity;
	
	private String id;
	private Map<String,String> discountTypeList = new LinkedHashMap<String,String>();
	private String discountType;
	
	private SessionMap<String,Object> sessionMap;  
	
	private Float defaultBarCodeQty;
	private Float mrprate;
	private Float discount;
	private Float salePrice;
	
	private String code;
	
	
	@Override
	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
	        {
	        	try {
					taxList = GetListResource.GetTaxList( this.getSessionMap());
					supplierList = GetListResource.GetSupplierList();
				} catch (IOException e) {
					
				}
	        	
	        	unitList = GetListResource.GetUnitList();
	        	
	        	this.getDiscountTypeList().put("PERCENTAGE", "PERCENTAGE");
	        	this.getDiscountTypeList().put("PERQUANTITY", "PERQUANTITY");
	        	
	        	Connection connection = null;
				PreparedStatement preparedStatement= null;
				ResultSet resultSet = null;
				
				String shopId = (String) this.getSessionMap().get("shopId");
	    		String shopCode = (String) this.getSessionMap().get("shopCode");
	         	
				String sql = "";
				String db = (String) this.getSessionMap().get("db");
				
				try
				{
				
					connection = GetDBConnection.getDBConnection(db);					
					sql = "SELECT "
	       					+ "product_name_eng,"
	       					+ "product_name_mar,"
	       					+ "product_alias,"
	       					+ "low_stock_count,"
	       					+ "product_short_code,"
	       					+ "purchase_rate,"
	       					+ "customer_sales_price,"
	       					+ "dealer_sales_price,"
	       					+ "retailer_sales_price,"
	       					+ "tax_id,"
	       					+ "purchase_unit,"
	       					+ "sales_unit,"
	       					+ "hsn_sac_code,"
	       					+ "stock_conversion_ratio,"	       					
	       					+ "rack_number,"
	       					+ "barcode_mrp,"
	       					+ "discount_type,"
	       					+ "discount_amount_percentage,"
	       					+ "sales_price,"
	       					+ "default_barcode_quantity "
	       					+ "FROM "
	       					+ "shop_product_master "
	       					+ "WHERE "
	       					+ "shop_id = ? "
	       					+ "AND "
	       					+ "shop_code = ? "
	       					+ "AND "
	       					+ "product_id =? "
	       					+ "AND "
	       					+ "product_short_code = ? ";
					
					
					preparedStatement = connection.prepareStatement(sql);
					preparedStatement.setString(1, shopId);
					preparedStatement.setString(2, shopCode);
					preparedStatement.setString(3, EncryptionDecryption.decrypt(this.getId()));
					preparedStatement.setString(4, this.getCode());
					
					
					resultSet = preparedStatement.executeQuery();
					
					resultSet.last();
					
					int rowCount = resultSet.getRow();
					
					resultSet.beforeFirst();
					
					if(rowCount>0)
					{
						
						
						if(resultSet.next())
						{
							this.setProductName(resultSet.getString(1));
							this.setProductNameMarathi(resultSet.getString(2));
							this.setAlias(resultSet.getString(3));
							this.setLowStockCount(resultSet.getString(4));
							this.setProductCode(resultSet.getString(5));
							this.setPuchaseRate(resultSet.getFloat(6));
							this.setCustomerSalesRate(resultSet.getFloat(7));
							this.setDealerSalesRate(resultSet.getFloat(8));
							this.setRetailerSalesRate(resultSet.getFloat(9));
							this.setTaxId(resultSet.getString(10));
							this.setPurchaseUnit(resultSet.getString(11));
							this.setSaleUnit(resultSet.getString(12));
							this.setHsnCode(resultSet.getString(13));
							this.setConvertQuantity(resultSet.getString(14));
							this.setRackNumber(resultSet.getString(15));
							this.setMrprate(resultSet.getFloat(16));
							this.setDiscountType(resultSet.getString(17));
							this.setDiscount(resultSet.getFloat(18));
							this.setSalePrice(resultSet.getFloat(19));
							this.setDefaultBarCodeQty(resultSet.getFloat(20));
							
						}
						
						result = "success";
					}
					else
					{
						result = "error";
						
						addActionError("URL Tampered ! Product with specified name not found !");
					}
					
				} catch (SQLException e) {
					result = "error";
					
					addActionError(e.getErrorCode()+" || "+e.getMessage() );
				} catch (IOException e) {
					result = "error";
					
					addActionError(e.getMessage()+" || "+e.getCause());
				}finally {
					if(resultSet != null)
    				{
    					try {
    						resultSet.close();
    					} catch (SQLException e) {
    						
    					}
    				}
					if(preparedStatement != null)
					{
						try {preparedStatement.close();} catch (SQLException e) {}
					}
					if(connection != null)
					{
						
						try {connection.close();}catch (SQLException e) {}
					}
					
				}
	        	
	        }
		
		return result;
	}

	public HttpSession getSession() {
		return session;
	}

	public void setSession(HttpSession session) {
		this.session = session;
	}

	public String getProductAddDate() {
		return productAddDate;
	}

	public void setProductAddDate(String productAddDate) {
		this.productAddDate = productAddDate;
	}

	public String getProductName() {
		return productName;
	}

	public void setProductName(String productName) {
		this.productName = productName;
	}


	public String getProductNameMarathi() {
		return productNameMarathi;
	}

	public void setProductNameMarathi(String productNameMarathi) {
		this.productNameMarathi = productNameMarathi;
	}

	
	public String getAlias() {
		return Alias;
	}

	public void setAlias(String alias) {
		Alias = alias;
	}

	public String getSaleUnit() {
		return saleUnit;
	}

	public void setSaleUnit(String saleUnit) {
		this.saleUnit = saleUnit;
	}

	public String getPurchaseUnit() {
		return purchaseUnit;
	}

	public void setPurchaseUnit(String purchaseUnit) {
		this.purchaseUnit = purchaseUnit;
	}

	public String getLowStockCount() {
		return lowStockCount;
	}

	public void setLowStockCount(String lowStockCount) {
		this.lowStockCount = lowStockCount;
	}

	public String getHsnCode() {
		return hsnCode;
	}

	public void setHsnCode(String hsnCode) {
		this.hsnCode = hsnCode;
	}

	public String getConvertQuantity() {
		return convertQuantity;
	}

	public void setConvertQuantity(String convertQuantity) {
		this.convertQuantity = convertQuantity;
	}

	
	public Map<String, String> getTaxList() {
		return taxList;
	}

	public void setTaxList(Map<String, String> taxList) {
		this.taxList = taxList;
	}

	public Map<String, String> getUnitList() {
		return unitList;
	}

	public void setUnitList(Map<String, String> unitList) {
		this.unitList = unitList;
	}

	public String getId() {
		return id;
	}

	public void setId(String id) {
		this.id = id;
	}

	public Map<String, String> getSupplierList() {
		return supplierList;
	}

	public void setSupplierList(Map<String, String> supplierList) {
		this.supplierList = supplierList;
	}


	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;  
	}

	public Float getPuchaseRate() {
		return puchaseRate;
	}

	public void setPuchaseRate(Float puchaseRate) {
		this.puchaseRate = puchaseRate;
	}

	public String getProductCode() {
		return productCode;
	}

	public void setProductCode(String productCode) {
		this.productCode = productCode;
	}

	public Float getCustomerSalesRate() {
		return customerSalesRate;
	}

	public void setCustomerSalesRate(Float customerSalesRate) {
		this.customerSalesRate = customerSalesRate;
	}

	public Float getRetailerSalesRate() {
		return retailerSalesRate;
	}

	public void setRetailerSalesRate(Float retailerSalesRate) {
		this.retailerSalesRate = retailerSalesRate;
	}

	public Float getDealerSalesRate() {
		return dealerSalesRate;
	}

	public void setDealerSalesRate(Float dealerSalesRate) {
		this.dealerSalesRate = dealerSalesRate;
	}

	public String getRackNumber() {
		return rackNumber;
	}

	public void setRackNumber(String rackNumber) {
		this.rackNumber = rackNumber;
	}

	public String getTaxId() {
		return taxId;
	}

	public void setTaxId(String taxId) {
		this.taxId = taxId;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public Map<String, String> getDiscountTypeList() {
		return discountTypeList;
	}

	public void setDiscountTypeList(Map<String, String> discountTypeList) {
		this.discountTypeList = discountTypeList;
	}

	public String getDiscountType() {
		return discountType;
	}

	public void setDiscountType(String discountType) {
		this.discountType = discountType;
	}

	public Float getDefaultBarCodeQty() {
		return defaultBarCodeQty;
	}

	public void setDefaultBarCodeQty(Float defaultBarCodeQty) {
		this.defaultBarCodeQty = defaultBarCodeQty;
	}

	public Float getMrprate() {
		return mrprate;
	}

	public void setMrprate(Float mrprate) {
		this.mrprate = mrprate;
	}

	public Float getDiscount() {
		return discount;
	}

	public void setDiscount(Float discount) {
		this.discount = discount;
	}

	public Float getSalePrice() {
		return salePrice;
	}

	public void setSalePrice(Float salePrice) {
		this.salePrice = salePrice;
	}

	public String getCode() {
		return code;
	}

	public void setCode(String code) {
		this.code = code;
	}

	
	
	
	
				

}
