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.ChallanProductInfoDAO;
import com.reallinkbilling.db.GetDBConnection;
import com.opensymphony.xwork2.ActionSupport;

public class GetChallanProductListOverViewJsp extends ActionSupport implements SessionAware{

	/**
	 * 
	 */
	private static final long serialVersionUID = 6208411094853732491L;
	private SessionMap<String,Object> sessionMap;  
	private String challanNumber;
	private ArrayList<ChallanProductInfoDAO> productList = new ArrayList<ChallanProductInfoDAO>();
	
	private String customerName;
	private String challanDate;
	
	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_id,"
							+ "product_name,"
							+ "product_total_amount,"
							+ "product_quantity,"
							+ "product_total_amount_aft_discount,"
							+ "product_net_amount,"
							+ "product_igst_amount,"
							+ "product_cgst_amount,"
							+ "product_sgst_amount,"
							+ "remaining_quanity_for_invoice "
							+ "FROM "
							+ "shop_challan_product_info_master "
							+ "WHERE "
							+ "challan_number = ? "
							+ "AND "
							+ "shop_id = ? "
							+ "AND "
							+ "shop_code = ? ";
					
					preparedStatement = connection.prepareStatement(sql);
					preparedStatement.setString(1, this.getChallanNumber());
					preparedStatement.setString(2, shopId);
					preparedStatement.setString(3, shopCode);
					
					resultSet = preparedStatement.executeQuery();
					
					while(resultSet.next())
					{
						ChallanProductInfoDAO dao = new ChallanProductInfoDAO();
						dao.setProductId(resultSet.getString(1));
						dao.setProductName(resultSet.getString(2));
						dao.setAmount(resultSet.getFloat(3));
						dao.setQuantity(resultSet.getFloat(4));
						dao.setAmountAfterDiscount(resultSet.getFloat(5));
						dao.setGrossAmount(resultSet.getFloat(6));
						dao.setIgst(resultSet.getFloat(7));
						dao.setCgst(resultSet.getFloat(8));
						dao.setSgst(resultSet.getFloat(9));
						dao.setAvailableQtyForInv(resultSet.getFloat(10));
						
						this.getProductList().add(dao);
						
						
					}
					
					
					if(resultSet != null)
					{
						resultSet.close();
						resultSet = null;
					}
					
					if(preparedStatement != null)
					{
						preparedStatement.close();
						preparedStatement = null;
					}
					
					
					sql = "SELECT "
							+ "DATE_FORMAT(challan_date,'%d-%m-%Y'),"
							+ "customer_name "
							+ "FROM "
							+ "shop_challan_info_master "
							+ "WHERE "
							+ "challan_number = ? "
							+ "AND "
							+ "shop_id = ? "
							+ "AND "
							+ "shop_code = ?";
					
					preparedStatement = connection.prepareStatement(sql);
					preparedStatement.setString(1, this.getChallanNumber());
					preparedStatement.setString(2, shopId);
					preparedStatement.setString(3, shopCode);
					
					resultSet = preparedStatement.executeQuery();
					
					if(resultSet.next())
					{
						this.setChallanDate(resultSet.getString(1));
						this.setCustomerName(resultSet.getString(2).split("-")[1]);
					}
						
					
					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 String getChallanNumber() {
		return challanNumber;
	}



	public void setChallanNumber(String challanNumber) {
		this.challanNumber = challanNumber;
	}



	public ArrayList<ChallanProductInfoDAO> getProductList() {
		return productList;
	}



	public void setProductList(ArrayList<ChallanProductInfoDAO> productList) {
		this.productList = productList;
	}



	public static long getSerialversionuid() {
		return serialVersionUID;
	}



	public String getCustomerName() {
		return customerName;
	}



	public void setCustomerName(String customerName) {
		this.customerName = customerName;
	}



	public String getChallanDate() {
		return challanDate;
	}



	public void setChallanDate(String challanDate) {
		this.challanDate = challanDate;
	}



	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;  
	}
	

}



