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.ChallanInfoListForConvertDAO;
import com.reallinkbilling.db.GetDBConnection;
import com.opensymphony.xwork2.ActionSupport;

public class GetListOfChallanToConvertInv extends ActionSupport implements SessionAware{

	/**
	 * 
	 */
	private static final long serialVersionUID = 6208411094853732491L;
	private SessionMap<String,Object> sessionMap; 
	
	private String fromDate;
	private String toDate;
	
	private ArrayList<ChallanInfoListForConvertDAO> challanDAOList = new ArrayList<ChallanInfoListForConvertDAO> ();
	
	private ArrayList<ChallanInfoListForConvertDAO> challanDAOList2 = new ArrayList<ChallanInfoListForConvertDAO> ();
	
	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 "
							+ "challan_number,"
							+ "DATE_FORMAT(challan_date,'%d-%m-%Y'),"
							+ "customer_id,"
							+ "customer_name,"
							+ "challan_total_amount,"
							+ "challan_amount_received "
							+ "FROM "
							+ "shop_challan_info_master "
							+ "WHERE "
							+ "shop_id = ? "
							+ "AND "
							+ "shop_code = ? "
							+ "AND "
							+ "(challan_date BETWEEN STR_TO_DATE(?,'%d-%m-%Y') AND STR_TO_DATE(?,'%d-%m-%Y'))";
					
					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();
					
					while(resultSet.next())
					{
						ChallanInfoListForConvertDAO dao = new ChallanInfoListForConvertDAO();
						
						dao.setChallanNumber(resultSet.getString(1));
						dao.setChallanDate(resultSet.getString(2));
						dao.setCustomerId(resultSet.getString(3));
						dao.setCustomerName(resultSet.getString(4).split("-")[1]);
						dao.setTotalChallanValue(resultSet.getFloat(5));
						dao.setTotalAmountPaid(resultSet.getFloat(6));
					
						
						this.getChallanDAOList().add(dao);
						
					}
					
					
					if(this.getChallanDAOList().size() > 0)
					{
						
						
						
						
						for(ChallanInfoListForConvertDAO dao : this.getChallanDAOList())
						{
							
							float totalCGSTAmount = 0.0f;
							float totalSGSTAmount = 0.0F;
							float totalNettAmount = 0.0F;
							float totalIGSTAmount = 0.0F;
							
							float totalRemaingCGSTAmt = 0.0F;
							float totalRemaingSGSTAmt = 0.0F;
							
							String getGSTDetailsSQL = "SELECT "
									+ "product_net_amount, "
									+ "product_igst_amount,"
									+ "product_cgst_amount,"
									+ "product_sgst_amount,"
									+ "remaining_quanity_for_invoice,"
									+ "product_quantity "
									+ "FROM "
									+ "shop_challan_product_info_master "
									+ "WHERE "
									+ "challan_number = ? "
									+ "AND "
									+ "shop_id = ? "
									+ "AND "
									+ "shop_code = ? ";
							
							PreparedStatement getGSTDetailsPSMT = connection.prepareStatement(getGSTDetailsSQL);
							ResultSet getGSTDetailsRS = null;
							
							getGSTDetailsPSMT.setString(1, dao.getChallanNumber());
							getGSTDetailsPSMT.setString(2, shopId);
							getGSTDetailsPSMT.setString(3, shopCode);
							
							getGSTDetailsRS = getGSTDetailsPSMT.executeQuery();
							
							while(getGSTDetailsRS.next())
							{
								float actualQty = getGSTDetailsRS.getFloat(6);
								float perQtyCGST= getGSTDetailsRS.getFloat(3) / actualQty;
								float perQtySGST= getGSTDetailsRS.getFloat(4) / actualQty;
								
								dao.setRemainingProductQtyForConvert(getGSTDetailsRS.getFloat(5));
								dao.setProductQuantity(getGSTDetailsRS.getFloat(6));
								
								totalNettAmount = totalNettAmount + getGSTDetailsRS.getFloat(1);
								totalIGSTAmount = totalIGSTAmount + getGSTDetailsRS.getFloat(2);
								totalCGSTAmount = totalCGSTAmount + getGSTDetailsRS.getFloat(3);
								totalSGSTAmount = totalSGSTAmount + getGSTDetailsRS.getFloat(4);
								
								totalRemaingCGSTAmt = totalRemaingCGSTAmt + (perQtySGST*dao.getRemainingProductQtyForConvert());
								totalRemaingSGSTAmt = totalRemaingSGSTAmt + (perQtyCGST*dao.getRemainingProductQtyForConvert());
								
								
								
							}
						
							dao.setTotalTaxableAmount(totalNettAmount);
							dao.setTotalIgstAmount(totalIGSTAmount);
							dao.setTotalCgstAmount(totalCGSTAmount);
							dao.setTotalSgstAmotun(totalSGSTAmount);
							dao.setRemainingCGSTAmount(totalRemaingCGSTAmt);
							dao.setRemainingSGSTAmount(totalRemaingSGSTAmt);
							
							this.getChallanDAOList2().add(dao);
						
						}
						
					  	this.getSessionMap().remove("challanToInvProductList");
				 	       
			        	
						
						result = SUCCESS;
					}
					else
					{
						result = ERROR;
						addActionError("Details Not Found!");
					}
					
				}
				catch (SQLException e) {
					result = "error";
					e.printStackTrace();
					
					addActionError(e.getErrorCode()+" || "+e.getMessage() +" || Something Went Wrong !");
				}
				catch (Exception e) {
						result = "error";
					e.printStackTrace();
					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<ChallanInfoListForConvertDAO> getChallanDAOList() {
		return challanDAOList;
	}


	public void setChallanDAOList(ArrayList<ChallanInfoListForConvertDAO> challanDAOList) {
		this.challanDAOList = challanDAOList;
	}


	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 ArrayList<ChallanInfoListForConvertDAO> getChallanDAOList2() {
		return challanDAOList2;
	}


	public void setChallanDAOList2(ArrayList<ChallanInfoListForConvertDAO> challanDAOList2) {
		this.challanDAOList2 = challanDAOList2;
	}


	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;  
	}
	

}



