package com.reallinkbilling.jspRedirect;


import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Map;

import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.SessionAware;

import com.reallinkbilling.DAO.RejectionProductListProductDAO;
import com.reallinkbilling.db.GetDBConnection;
import com.opensymphony.xwork2.ActionSupport;

public class ViewRejectionDetailsEntry extends ActionSupport implements SessionAware{

	/**
	 * 
	 */
	private static final long serialVersionUID = 6208411094853732491L;
	private SessionMap<String,Object> sessionMap; 
	
	private String rejectionAddDate;
	private String batchId;
	
	private String fromDate;
	private String toDate;
	
	private Boolean rejectionSaved = true;

	private ArrayList<RejectionProductListProductDAO> rejectionProductList = new ArrayList<RejectionProductListProductDAO> ();
	
	public String execute() {
		 
		String result = "login";
	
		
	        if(this.getSessionMap()==null || this.getSessionMap().get("login")==null){ 
	        	addActionMessage("Please Logout and Login again !");
	        	result = "login";
	        }  
	        else{
	        		
	        	String shopId = (String) this.getSessionMap().get("shopId");
	    		String shopCode = (String) this.getSessionMap().get("shopCode");
	    	
				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_all_details,"
							+ "product_id,"
							+ "product_name,"
							+ "quantity,"
							+ "purchase_rate,"
							+ "total_purchase_amount,"
							+ "tax_rate,"
							+ "tax_amount,"
							+ "total_amount "
							+ "FROM "
							+ "rejection_details "
							+ "WHERE "
							+ "shop_id = ? "
							+ "AND "
							+ "shop_code = ? "
							+ "AND "
							+ "rejection_date = STR_TO_DATE(?,'%d-%m-%Y') "
							+ "AND "
							+ "batch_id = ?";
					
					preparedStatement = connection.prepareStatement(sql);
					
					preparedStatement.setString(1, shopId);
					preparedStatement.setString(2, shopCode);
					preparedStatement.setString(3, this.getRejectionAddDate());
					preparedStatement.setString(4, this.getBatchId());
					
					resultSet = preparedStatement.executeQuery();
					
					while(resultSet.next())
					{
						RejectionProductListProductDAO dao = new RejectionProductListProductDAO();
						
						dao.setProductAllDetails(resultSet.getString(1));
						dao.setProductId(resultSet.getString(2));
						dao.setProductName(resultSet.getString(3));
						dao.setQuantity(resultSet.getFloat(4));
						dao.setPurchaseRate(resultSet.getFloat(5));
						dao.setTotalPurchaseAmount(resultSet.getFloat(6));
						dao.setTaxRate(resultSet.getFloat(7));
						dao.setTaxAmount(resultSet.getFloat(8));
						dao.setTotalAmount(resultSet.getFloat(9));
						
					
						this.getRejectionProductList().add(dao);
						
						
					}
					
					if(this.getRejectionProductList().size() > 0)
					{
						
						this.getSessionMap().put("rejectionList", this.getRejectionProductList());
						result = SUCCESS;
					}
					else
					{
						result = ERROR;
						
						addActionError("Details not found! Please try again!");
					}
					
					
					
				}
				catch (SQLException e) {
					e.printStackTrace();
					result = "error";
					
					addActionError(e.getErrorCode()+" || "+e.getMessage() +" || Something Went Wrong !");
				}
				catch (Exception e) {
					e.printStackTrace();
						result = "error";
					
					addActionError(e.getMessage() +" || Something Went Wrong !");
				}
				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 result;
	}
	
	

	public String getRejectionAddDate() {
		return rejectionAddDate;
	}



	public void setRejectionAddDate(String rejectionAddDate) {
		this.rejectionAddDate = rejectionAddDate;
	}



	public String getBatchId() {
		return batchId;
	}


	public void setBatchId(String batchId) {
		this.batchId = batchId;
	}


	public ArrayList<RejectionProductListProductDAO> getRejectionProductList() {
		return rejectionProductList;
	}


	public void setRejectionProductList(ArrayList<RejectionProductListProductDAO> rejectionProductList) {
		this.rejectionProductList = rejectionProductList;
	}


	public static long getSerialversionuid() {
		return serialVersionUID;
	}


	public String getFromDate() {
		return fromDate;
	}


	public void setFromDate(String fromDate) {
		this.fromDate = fromDate;
	}


	public String getToDate() {
		return toDate;
	}


	public void setToDate(String toDate) {
		this.toDate = toDate;
	}


	public Boolean getRejectionSaved() {
		return rejectionSaved;
	}


	public void setRejectionSaved(Boolean rejectionSaved) {
		this.rejectionSaved = rejectionSaved;
	}


	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;  
	}
	

}



