package com.reallinkbilling.report;


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.RejectionDetailsReportDAO;
import com.reallinkbilling.db.GetDBConnection;
import com.opensymphony.xwork2.ActionSupport;

public class GetRejectionDetailsReport extends ActionSupport implements SessionAware{

	/**
	 * 
	 */
	private static final long serialVersionUID = 6208411094853732491L;
	private SessionMap<String,Object> sessionMap;  
	private String fromDate;
	private String toDate;
	private Float totalReportRejectionCost;
	
	private ArrayList<RejectionDetailsReportDAO> rejectionDetailsList = new ArrayList<RejectionDetailsReportDAO>();
	
	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);
					
					this.setTotalReportRejectionCost(0.0F);
					
					sql = "SELECT "
							+ "DATE_FORMAT(entry_date,'%d-%m-%Y'),"
							+ "DATE_FORMAT(rejection_date,'%d-%m-%Y'),"
							+ "batch_id,"
							+ "ROUND(SUM(quantity),2),"
							+ "ROUND(SUM(total_amount),2) "
							+ "FROM "
							+ "rejection_details "
							+ "WHERE "
							+ "shop_id = ? "
							+ "AND "
							+ "shop_code = ? "
							+ "AND "
							+ "(rejection_date BETWEEN STR_TO_DATE(?,'%d-%m-%Y') AND STR_TO_DATE(?,'%d-%m-%Y')) "
							+ "GROUP BY entry_date,batch_id";
					
					preparedStatement = connection.prepareStatement(sql);
					
					preparedStatement.setString(1, shopId);
					preparedStatement.setString(2, shopCode);
					preparedStatement.setString(3, this.getFromDate());
					preparedStatement.setString(4, this.getToDate());
					
					
					
					resultSet = preparedStatement.executeQuery();
					int i = 1;
					
					while(resultSet.next())
					{
						RejectionDetailsReportDAO dao = new RejectionDetailsReportDAO();
						dao.setSrNo(""+i);
						dao.setEntryDate(resultSet.getString(1));
						dao.setRejectionDate(resultSet.getString(2));						
						dao.setBatchId(resultSet.getString(3));
						dao.setTotalItemQuantity(resultSet.getString(4));
						dao.setTotalCost(resultSet.getString(5));
						
						this.setTotalReportRejectionCost(this.getTotalReportRejectionCost() + resultSet.getFloat(5));
						
						String getCountSQL = "SELECT count(*) FROM "
								+ "rejection_details "
								+ "WHERE "
								+ "batch_id = ? "
								+ "AND "
								+ "rejection_date = STR_TO_DATE(?,'%d-%m-%Y') "
								+ "AND "
								+ "shop_id = ? "
								+ "AND "
								+ "shop_code = ? ";
						
						PreparedStatement getCountPSMT = connection.prepareStatement(getCountSQL);
						
						getCountPSMT.setString(1, resultSet.getString(3));
						getCountPSMT.setString(2, resultSet.getString(2));
						getCountPSMT.setString(3, shopId);
						getCountPSMT.setString(4, shopCode);
						
						ResultSet getCountRS = getCountPSMT.executeQuery();
						
						if(getCountRS.next())
						{
							dao.setTotalItemCount(getCountRS.getString(1));
						}
						
						if(getCountRS != null)
						{
							getCountRS.close();
							getCountRS = null;
						}
						
						if(getCountPSMT != null)
						{
							getCountPSMT.close();
							getCountPSMT = null;
						}
						
						i++;
						
						this.getRejectionDetailsList().add(dao);
						
					}
					
					result = SUCCESS;
				}
				catch (SQLException e) {
					result = "error";
					
					addActionError(e.getErrorCode()+" || "+e.getMessage() +" || Something Went Wrong !");
				}
				catch (Exception e) {
						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 ArrayList<RejectionDetailsReportDAO> getRejectionDetailsList() {
		return rejectionDetailsList;
	}



	public void setRejectionDetailsList(ArrayList<RejectionDetailsReportDAO> rejectionDetailsList) {
		this.rejectionDetailsList = rejectionDetailsList;
	}



	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 static long getSerialversionuid() {
		return serialVersionUID;
	}


	public Float getTotalReportRejectionCost() {
		return totalReportRejectionCost;
	}



	public void setTotalReportRejectionCost(Float totalReportRejectionCost) {
		this.totalReportRejectionCost = totalReportRejectionCost;
	}



	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;  
	}
	

}



