package com.reallinkbilling.delete;


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.reallinkbilling.update.UpdateProductStockDetails;
import com.opensymphony.xwork2.ActionSupport;

public class DeleteRejectionDetailsEntry 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;
	
	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);
					
					connection.setAutoCommit(false);
					
					
					
					sql = "SELECT "
							+ "product_id,"
							+ "product_name,"
							+ "quantity "
							+ "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();
					
					boolean stockPlus = false;
					
					while(resultSet.next())
					{
						stockPlus = UpdateProductStockDetails.addStockToCurrentStock(connection, this.getSessionMap(), resultSet.getString(1),  resultSet.getFloat(3),  0.0f);
			        		
					}
					
					if(stockPlus)
					{
						String delSql = "DELETE FROM "
								+ "rejection_details "
								+ "WHERE "
								+ "shop_id = ? "
								+ "AND "
								+ "shop_code = ? "
								+ "AND "
								+ "rejection_date = STR_TO_DATE(?,'%d-%m-%Y') "
								+ "AND "
								+ "batch_id = ?";
						
						PreparedStatement delPSMT = connection.prepareStatement(delSql);
						
						delPSMT.setString(1, shopId);
						delPSMT.setString(2, shopCode);
						delPSMT.setString(3, this.getRejectionAddDate());
						delPSMT.setString(4, this.getBatchId());
						
						int k = delPSMT.executeUpdate();
						
						if(k>0)
						{
							result = SUCCESS;
							connection.commit();
							addActionMessage("Rejection details deleted successfully! Stock reversed!");
						}
						else
						{
							result = ERROR;
							connection.rollback();
							addActionError("Rejection details not deleted! Please try again! Error while stock reverse!");
						}
					}
					else
					{
						connection.rollback();
						addActionError("Error while stock addition! Please try again!");
						result = ERROR;
					}
				}
				catch (SQLException e) {
					result = "error";
					try {
						connection.rollback();
					} catch (SQLException e1) {}
					addActionError(e.getErrorCode()+" || "+e.getMessage() +" || Something Went Wrong !");
				}
				catch (Exception e) {
						result = "error";
						try {
							connection.rollback();
						} catch (SQLException e1) {}
					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 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 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;  
	}
	

}



