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.List;
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.DAO.CustomerPaymentDetailsDAO;
import com.reallinkbilling.db.GetDBConnection;

public class GetCustPaymentDetailsReport extends ActionSupport implements SessionAware{

	/**
	 * 
	 */
	private static final long serialVersionUID = 6602710107876580601L;
	
	private String fromDate;
	private String toDate;
	private List<CustomerPaymentDetailsDAO> custPaymentDetailsDAOList = new ArrayList<CustomerPaymentDetailsDAO>();
	private String customerNameCode;
	private SessionMap<String,Object> sessionMap;  

		
	public String execute() throws Exception {
		String result = "login";
		  
	        if(this.getSessionMap()==null || this.getSessionMap().get("login")==null){ 
	        	addActionMessage("Please Login First !!!");
	        	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);
	    			
	    			if(this.getCustomerNameCode() == null || this.getCustomerNameCode().equals(""))
	    			{
	    			
		    			sql = "SELECT "
		    					+ "idcustpaymentdetails,"
		    					+ "custId,"
		    					+ "custName,"
		    					+ "DATE_FORMAT(date,'%d-%m-%Y'),"
		    					+ "amount,"
		    					+ "paymentdetails "
		    					+ "FROM "
		    					+ "custpaymentdetails "
		    					+ "WHERE "
		    					+ "date "
		    					+ "BETWEEN "
		    					+ "STR_TO_DATE(?,'%d-%m-%Y') AND STR_TO_DATE(?,'%d-%m-%Y')";
		    			
		    			preparedStatement = connection.prepareStatement(sql);
		    			preparedStatement.setString(1, this.getFromDate());
		    			preparedStatement.setString(2, this.getToDate());
		    			
		    			
		    			
	    			}
	    			else
	    			{
	    				sql = "SELECT "
		    					+ "idcustpaymentdetails,"
		    					+ "custId,"
		    					+ "custName,"
		    					+ "DATE_FORMAT(date,'%d-%m-%Y'),"
		    					+ "amount,"
		    					+ "paymentdetails "
		    					+ "FROM "
		    					+ "custpaymentdetails "
		    					+ "WHERE "
		    					+ "custName LIKE '%"+this.getCustomerNameCode().trim()+"%' "
		    					+ "OR "
		    					+ "custId LIKE '%"+this.getCustomerNameCode().trim()+"%'";
	    				
	    				preparedStatement = connection.prepareStatement(sql);
		    			
		    		
	    			}
	    			
	    			
	    			
	    			resultSet = preparedStatement.executeQuery();
	    			
	    			int rowCount = 0;
	    			resultSet.last();
	    			rowCount = resultSet.getRow();
	    			resultSet.beforeFirst();
	    			
	    			if(rowCount > 0)
	    			{
	    				
	    				
	    				while(resultSet.next())
	    				{
	    					CustomerPaymentDetailsDAO dao = new CustomerPaymentDetailsDAO();
	    					
	    					dao.setPaymentId(resultSet.getString(1));
	    					dao.setCustId(resultSet.getString(2));
	    					dao.setCustName(resultSet.getString(3));
	    					dao.setPaymentDate(resultSet.getString(4));
	    					dao.setAmount(resultSet.getString(5));
	    					dao.setDetails(resultSet.getString(6));
	    					
	    					this.getCustPaymentDetailsDAOList().add(dao);
	    				}
	    				
	    				result = "success";
	    			}
	    			else
	    			{
	    				result = "error";
	    				addActionError("Records not found for provided information!");
	    			}
	    			
	    		}	    		
	    		catch (SQLException e) {
	    			
	    			result = "error";
	    			
	    			addActionError(e.getErrorCode()+" || "+e.getMessage() +" || Something Went Wrong!");
	    		}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 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 List<CustomerPaymentDetailsDAO> getCustPaymentDetailsDAOList() {
		return custPaymentDetailsDAOList;
	}


	public void setCustPaymentDetailsDAOList(List<CustomerPaymentDetailsDAO> custPaymentDetailsDAOList) {
		this.custPaymentDetailsDAOList = custPaymentDetailsDAOList;
	}


	public String getCustomerNameCode() {
		return customerNameCode;
	}


	public void setCustomerNameCode(String customerNameCode) {
		this.customerNameCode = customerNameCode;
	}



	public void setSession(Map<String, Object> map) {
		// TODO Auto-generated method stub
		 sessionMap=(SessionMap<String, Object>)map;  
		
	}

	

	public SessionMap<String, Object> getSessionMap() {
		return sessionMap;
	}


	public void setSessionMap(SessionMap<String, Object> sessionMap) {
		this.sessionMap = sessionMap;
	}


	
}


