package com.reallinkbilling.jspRedirect;


import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.reallinkbilling.DAO.ChallanProductInfoDAO;
import com.reallinkbilling.db.GetDBConnection;
import com.reallinkbilling.resource.GetListResource;
import com.reallinkbilling.utils.GetLastChallanNumber;


public class AddNewChallanJsp extends ActionSupport implements SessionAware{

	/**
	 * 
	 */
	private static final long serialVersionUID = 7362899112859144172L;
	private HttpSession session;
	private String challanDateTime;
	private String challanNumber;
	private Map<String,String> taxList = new LinkedHashMap<String,String>();
	private Map<String,String> unitList = new LinkedHashMap<String,String>();
	private Map<String,String> paymentTypeList = new  LinkedHashMap<String,String>();
	private Map<String,String> printTypeList = new LinkedHashMap<String,String>();
	
	private String defaultValue;
	private String printType;
	private String customerName;
	private SessionMap<String, Object> sessionMap;
	
	private Map<String,String> labourChargeTypeList = new LinkedHashMap<String,String>();
	private String labourChargeType;
	private String addLabourCharges;
	private Map<String,String> customerSearchOptionList = new HashMap<String,String>();
	private String customerSearchOption;

	private Map<String,String> rateTypeList = new LinkedHashMap<String,String>();
	private String rateType;
	private boolean addQuantity2;
	private Map<String,String> discountTypeList = new LinkedHashMap<String,String>();
	private String discountType;
	
	private String lastChallanNumber;
	private List<ChallanProductInfoDAO> productInfoList = new ArrayList<ChallanProductInfoDAO>();
	
	@SuppressWarnings("unchecked")
	public String execute(){
		
		String result = "login";
	
	        if(this.getSessionMap()==null || this.getSessionMap().get("login")==null){ 
	        	addActionMessage("Please Login First !!!");
	        	result = "login";
	        }  
	        else{
	        	
	        	this.getSessionMap().remove("productList");
	        	this.getSessionMap().remove("billInformation");
	        	this.getSessionMap().remove("deletedProductList");
	        	this.getSessionMap().remove("purchaseProductList");
	        	this.getSessionMap().remove("purchaseInformation");
	        	this.getSessionMap().remove("finishedProductInfoList");
	        	this.getSessionMap().remove("deletedProductInfoList");
	        	this.getSessionMap().remove("challanInformation");
	          	this.getSessionMap().remove("challanToInvProductList");
	 	    
	          	
	        	this.getSessionMap().remove("figureList");

	        	String shopId = (String) this.getSessionMap().get("shopId");
	    		String shopCode = (String) this.getSessionMap().get("shopCode");
	        	
	        	this.getCustomerSearchOptionList().put("BYNAME", "BY NAME");
	        	this.getCustomerSearchOptionList().put("BYMOBILE", "BY MOBILE");
	        	this.getCustomerSearchOptionList().put("BYADDRESS","BY ADDRESS");
				this.setCustomerSearchOption("BYNAME");
				this.setDefaultValue("BYNAME");
				
				
	        	Date d = new Date();
	        	SimpleDateFormat dateFormat = new SimpleDateFormat("dd-MM-yyyy");
	        	this.setChallanDateTime(dateFormat.format(d));
	        	paymentTypeList.put("CASH","CASH");
	        	paymentTypeList.put("CREDIT","CREDIT");
	        	
	        	this.getRateTypeList().put("INCLUDEGST", "WITH GST");
	        	this.getRateTypeList().put("EXCLUDEGST", "WITH-OUT GST");
	        	
	        	this.getDiscountTypeList().put("", "-- SELECT DEISCOUNT TYPE --");
	        	this.getDiscountTypeList().put("PERCENTAGE", "PERCENTAGE");
	        	this.getDiscountTypeList().put("PERQUANTITY", "PERQUANTITY");
	        	
	        	this.setDiscountType("");
	        
	        	HashMap<String, String> settings = (HashMap<String, String>) this.getSessionMap().get("shop_settings");
	        	if(settings == null)
	        	{
	        		settings = new HashMap<String, String>();
	        	}
	        
	        	this.setRateType(settings.get("default_rate_type"));
	        	this.setDiscountType(settings.get("discount_type"));
	        	
	        	this.setLastChallanNumber(GetLastChallanNumber.getLastChallanNumber(this.getSessionMap()));
				
	        	if("TRUE".equals(settings.get("add_quntity2")))
	        	{
	        		this.setAddQuantity2(true);
	        	}
	        	else
	        	{
	        		this.setAddQuantity2(false);
	        	}
	        	
	        	this.getLabourChargeTypeList().put("QUANTITYWISE", "BY QUANTITY");
	        	this.getLabourChargeTypeList().put("PERCENTAGEWISE", "BY PERCENTAGE");
	        	this.setAddLabourCharges(settings.get("add_making_charge_column"));
	        	this.setLabourChargeType("QUANTITYWISE");
	        	
	        	this.setDefaultValue("CASH");
	        	this.setProductInfoList(new ArrayList<ChallanProductInfoDAO>());
	     
	        	Object taxListFromSession = this.getSessionMap().get("tax_list");
	        	if(taxListFromSession instanceof Map)
	        	{
	        		this.setTaxList((Map<String, String>) taxListFromSession);
	        	}
	        	
	        	unitList = GetListResource.GetUnitList();
	        
	        	challanNumber = "1";
	        	
	        	
	        	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 "
	    					+ "customer_id,"
	    					+ "customer_name,"
	    					+ "mobile_number,"
	    					+ "customer_type "
	    					+ "FROM "
	    					+ "shop_customer_master "
	    					+ "WHERE "
	    					+ "customer_name LIKE '%CASH%' "
	    					+ "AND "
	    					+ "shop_id = ? "
	    					+ "AND "
	    					+ "shop_code= ? ";
	    			
	    			preparedStatement = connection.prepareStatement(sql);
	    			
	    			preparedStatement.setString(1, shopId);
	    			preparedStatement.setString(2, shopCode);
	    			
	    			resultSet = preparedStatement.executeQuery();
	    			
	    			while(resultSet.next())
	    			{
	    				this.setCustomerName(resultSet.getString(1)+"-"+resultSet.getString(2)+"-"+resultSet.getString(3)+"-"+resultSet.getString(4));
	    			}
	    			
	    			
	    				this.setChallanNumber("NA");
	    				
	    				
	    			result = SUCCESS;
	    			
	    		} catch (SQLException e) {
	    			
	    			result = ERROR;
	    			
	    			addActionError(e.getErrorCode()+" || "+e.getMessage() +" || Something Went Wrong!");
	    		} catch (IOException e) {
	    			result = ERROR;
	    			
	    			addActionError(e.getMessage()+" || "+e.getCause()+" || 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 HttpSession getSession() {
		return session;
	}

	public void setSession(HttpSession session) {
		this.session = session;
	}


	public String getChallanDateTime() {
		return challanDateTime;
	}

	public void setChallanDateTime(String challanDateTime) {
		this.challanDateTime = challanDateTime;
	}

	
	public String getChallanNumber() {
		return challanNumber;
	}

	public void setChallanNumber(String challanNumber) {
		this.challanNumber = challanNumber;
	}

	public Map<String, String> getTaxList() {
		return taxList;
	}

	public void setTaxList(Map<String, String> taxList) {
		this.taxList = taxList;
	}

	public Map<String, String> getUnitList() {
		return unitList;
	}

	public void setUnitList(Map<String, String> unitList) {
		this.unitList = unitList;
	}

	


	public Map<String, String> getPaymentTypeList() {
		return paymentTypeList;
	}

	public void setPaymentTypeList(Map<String, String> paymentTypeList) {
		this.paymentTypeList = paymentTypeList;
	}

	public String getDefaultValue() {
		return defaultValue;
	}

	public void setDefaultValue(String defaultValue) {
		this.defaultValue = defaultValue;
	}

	
	public Map<String, String> getPrintTypeList() {
		return printTypeList;
	}

	public void setPrintTypeList(Map<String, String> printTypeList) {
		this.printTypeList = printTypeList;
	}

	public String getPrintType() {
		return printType;
	}

	public void setPrintType(String printType) {
		this.printType = printType;
	}

	public String getCustomerName() {
		return customerName;
	}

	public void setCustomerName(String customerName) {
		this.customerName = customerName;
	}

	


	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;  
	}

	public Map<String, String> getCustomerSearchOptionList() {
		return customerSearchOptionList;
	}

	public void setCustomerSearchOptionList(Map<String, String> customerSearchOptionList) {
		this.customerSearchOptionList = customerSearchOptionList;
	}

	public String getCustomerSearchOption() {
		return customerSearchOption;
	}

	public void setCustomerSearchOption(String customerSearchOption) {
		this.customerSearchOption = customerSearchOption;
	}

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	public Map<String, String> getRateTypeList() {
		return rateTypeList;
	}

	public void setRateTypeList(Map<String, String> rateTypeList) {
		this.rateTypeList = rateTypeList;
	}

	public String getRateType() {
		return rateType;
	}

	public void setRateType(String rateType) {
		this.rateType = rateType;
	}

	public Map<String, String> getLabourChargeTypeList() {
		return labourChargeTypeList;
	}

	public void setLabourChargeTypeList(Map<String, String> labourChargeTypeList) {
		this.labourChargeTypeList = labourChargeTypeList;
	}

	public String getLabourChargeType() {
		return labourChargeType;
	}

	public void setLabourChargeType(String labourChargeType) {
		this.labourChargeType = labourChargeType;
	}

	public String getAddLabourCharges() {
		return addLabourCharges;
	}

	public void setAddLabourCharges(String addLabourCharges) {
		this.addLabourCharges = addLabourCharges;
	}

	public boolean isAddQuantity2() {
		return addQuantity2;
	}

	public void setAddQuantity2(boolean addQuantity2) {
		this.addQuantity2 = addQuantity2;
	}

	public Map<String, String> getDiscountTypeList() {
		return discountTypeList;
	}

	public void setDiscountTypeList(Map<String, String> discountTypeList) {
		this.discountTypeList = discountTypeList;
	}

	public String getDiscountType() {
		return discountType;
	}

	public void setDiscountType(String discountType) {
		this.discountType = discountType;
	}

	public String getLastChallanNumber() {
		return lastChallanNumber;
	}

	public void setLastChallanNumber(String lastChallanNumber) {
		this.lastChallanNumber = lastChallanNumber;
	}

	public List<ChallanProductInfoDAO> getProductInfoList() {
		return productInfoList;
	}

	public void setProductInfoList(List<ChallanProductInfoDAO> productInfoList) {
		this.productInfoList = productInfoList;
	}

	

	
	
	
	
	
}

