package com.reallinkbilling.delete;

import java.io.IOException;
import java.util.ArrayList;
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.ServletActionContext;
import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.SessionAware;

import com.opensymphony.xwork2.ActionSupport;
import com.reallinkbilling.DAO.BillInformationDAO;
import com.reallinkbilling.DAO.BillProductInfoDAO;
import com.reallinkbilling.resource.GetListResource;
import com.reallinkbilling.utils.GetAccountListForDrCr;
import com.reallinkbilling.utils.GetLastBillNumber;

public class DeleteEditProductFromQuotationList extends ActionSupport implements SessionAware{
	
	
	/**
	 * 
	 */
	private static final long serialVersionUID = -5627177987932888576L;
	private HttpSession session;
	private String billDateTime;
	private String operator;
	private String paymentType;
	private String customerName;
	private String customerId;
	private String customerMobileNumber;
	private String customerIdNameAll;
	private String productName;
	private String productMarathiName;
	private String alias;
	private String salesUnit;
	private String taxPercentage;
	private Float salesRate;
	private Float quantity;
	private Float quantity2;
	private Float discountPercentage;
	private Float discountRupees;
	private Float amount;
	private String billType;
	private Float amountAfterDiscount;
	private String productNameForDelete;
	private Float transportCharge;
	private String comments;
	private String id;
	private String serialNumber;
	private Float amountPaid;
	private String addLabourCharges;
	private SessionMap<String,Object> sessionMap;
	private String taxId;
	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> billTypeList = new  LinkedHashMap<String,String>();
	private List<BillProductInfoDAO> productInfoList;
	private List<BillProductInfoDAO> deletedProductInfoList;
	private String defaultValue;
	
	private String discountType;

	private Float totalAmount;
	private Float totalDiscount;
	private Float totalAmouAfterDisount;
	private String lastBillNumber;
	private Float returnAmount;
	
	private BillInformationDAO billInfoDAO = null;
	private String viewBill;
	
	private Map<String,String> rateTypeList = new LinkedHashMap<String,String>();
	private String rateType;
	private Map<String,String> labourChargeTypeList = new LinkedHashMap<String,String>();
	private String labourChargeType;
	private boolean addQuantity2;
	private Float cashAmount;
	private Float bankRtgsUpiAmount;
	private String accountId;
	private Map<String,String> accountList = new LinkedHashMap<String,String>();
	private List<String> paymentTypeList2 = new ArrayList<String>();
	private ArrayList<String> customerTypeList = new ArrayList<String>();
	private String customerType;
	
	private String quotationDateTime;
	private String quotationNumber;
	
		private Map<String,String> discountTypeList = new HashMap<String,String>();
	
		
	private String buyerOrderNumber;
	private String dispatchDocNumber;
	private String vehicleNumber;
	
	@SuppressWarnings("unchecked")
	public String execute() throws IOException{
		 
		String result = "login";
		session=ServletActionContext.getRequest().getSession(false); 
		
	        if(session==null || session.getAttribute("login")==null){ 
	        	addActionMessage("Please Logout and Login again !");
	        	result = "error";
	        }  
	        else{ 	
	        	
	        	
	        	paymentTypeList2.add("CASH");
	        	paymentTypeList2.add("UPI");
	        	paymentTypeList2.add("RTGS");
	        	paymentTypeList2.add("NEFT");
	        	paymentTypeList2.add("CHEQUE");
	        	
	        	  discountTypeList.put("", "Select Discount Type");
			      discountTypeList.put("By Quantity", "By Quantity");
			      discountTypeList.put("By Percentage", "By Percentage");
			      discountTypeList.put("By Direct Amount", "By Direct Amount");
	        	
	        	this.setAccountList(GetAccountListForDrCr.getAccountList(this.getSessionMap())); 
	        	
			
	        	this.setProductInfoList((List<BillProductInfoDAO>) this.getSessionMap().get("quotationProductList"));
	        	this.setBillInfoDAO((BillInformationDAO) this.getSessionMap().get("quotationInformation"));
	        	this.setDeletedProductInfoList((List<BillProductInfoDAO>) this.getSessionMap().get("deletedProductList"));
	        	
	        	if(this.getDeletedProductInfoList() == null )
	        	{
	        		this.setDeletedProductInfoList(new ArrayList<BillProductInfoDAO>());
	        	}
	        	int removeIndexCount = 0;
	        	int i = 0;
	        	boolean isProductRemovedFromDB = true;
	        	
					
    		   for(BillProductInfoDAO infoDAO : this.getProductInfoList())
	        	{
	        		String pName = infoDAO.getProductName();
	        		
	        		if(this.getProductNameForDelete().equals(pName) )
	        		{
	        			removeIndexCount = i;
	        			
	        		}
	        		i++;
	        		
	        	}
	        	
	        	if(this.getBillInfoDAO() != null)
	        	{
	        		this.setQuotationNumber(this.getBillInfoDAO().getBillNumber());	        	
	        		this.setPaymentType(this.getBillInfoDAO().getPaymentType());
	        		this.setBillType(this.getBillInfoDAO().getBillType());
	        		this.setCustomerName(this.getBillInfoDAO().getCustomerName());
	        		this.setTransportCharge(this.getBillInfoDAO().getTransportCharge());
	        		this.setComments(this.getBillInfoDAO().getComments());
	        		this.setDefaultValue(getPaymentType());
	        		
	        	}
	        	
	        	BillProductInfoDAO dao = this.getProductInfoList().get(removeIndexCount);
	        	
	        	this.getDeletedProductInfoList().add(dao);
	        	
	        	this.setAlias(dao.getAlias());
	        	this.setProductName(dao.getProductId()+"-"+dao.getProductName()+"-"+dao.getProductCode());
	        	this.setProductMarathiName(dao.getProductNameMarathi());
	        	this.setSalesUnit(dao.getSalesUnit());
	        	this.setTaxPercentage(""+dao.getTaxPercentage());
	        	this.setTaxId(dao.getTaxId());
	        	this.setSalesRate(dao.getSalesRate());
	        	this.setQuantity(dao.getQuantity());
	        	this.setDiscountPercentage(dao.getDiscountPecentage());
	        	this.setDiscountRupees(dao.getDiscountRupees());
	        	this.setAmount(dao.getAmount());
	        	this.setAmountAfterDiscount(dao.getAmountAfterDiscount());
	        	this.setQuantity2(dao.getQuantity2());
	        	
	        	  	
		        	if(isProductRemovedFromDB && ! this.getBillInfoDAO().isBillSaved())
	    			{
		        		this.getBillInfoDAO().setBillSaved(false);
		        		this.getProductInfoList().remove(removeIndexCount);
		        		addActionMessage("Product removed from list !");
	    			}	        	
		        	else if(isProductRemovedFromDB && this.getBillInfoDAO().isBillSaved())
		        	{
		        	
		        		this.getBillInfoDAO().setBillSaved(false);
		        		this.getProductInfoList().remove(removeIndexCount);
		        	}
	        	else
	        	{
	        		addActionError("Product not removed from list !");
	        	}
	        	float floatTotalAmount = 0.0F;
	        	float floatTotalDiscount = 0.0F;
	        	float floatTotalAmtAfterDiscount = 0.0F;
	        	
	        	
	        	
	        	
	        	
	        	for(BillProductInfoDAO infoDAO : this.getProductInfoList())
	        	{
	        		floatTotalAmount  = floatTotalAmount+ infoDAO.getAmount();
	        		floatTotalAmtAfterDiscount = floatTotalAmtAfterDiscount + infoDAO.getAmountAfterDiscount();
	        		
	        		
	        	}
	        	floatTotalDiscount = floatTotalAmount - floatTotalAmtAfterDiscount;
	        	
	        	this.setTotalAmount(floatTotalAmount);
	        	this.setTotalDiscount(floatTotalDiscount);
	        	this.setTotalAmouAfterDisount(floatTotalAmtAfterDiscount);
	        	this.setCashAmount(floatTotalAmtAfterDiscount);
	        	this.setBankRtgsUpiAmount(0.0F);
	        	this.setBillDateTime(this.getBillInfoDAO().getBillDate());
	        	this.setQuotationDateTime(this.getBillInfoDAO().getBillDate());
	        	
	        	paymentTypeList.put("CASH","CASH");
	        	paymentTypeList.put("CREDIT","CREDIT");
	        	       	
	        	
	        	

	       
	    			taxList = GetListResource.GetTaxList(this.getSessionMap());
	    			
	    	
	        	
	        	unitList = GetListResource.GetUnitList();
	        	billTypeList = GetListResource.GetBillTypeList();
	        	
	        	this.getRateTypeList().put("INCLUDEGST", "WITH GST");
	        	this.getRateTypeList().put("EXCLUDEGST", "WITH-OUT GST");
	         	
	        	HashMap<String, String> settings = (HashMap<String, String>) this.getSessionMap().get("shop_settings");
	        	this.setLastBillNumber(GetLastBillNumber.getLastBillNumber(settings.get("default_bill_type"), this.getSessionMap()));
	    	
	        	this.setAccountId(settings.get("default_account_to_collect"));
	    		
	    		if(settings.get("add_quntity2").equals("TRUE"))
	        	{
	        		this.setAddQuantity2(true);
	        	}
	        	else
	        	{
	        		this.setAddQuantity2(false);
	        	}
	        	
	        	this.setRateType( settings.get("default_rate_type"));
		        
	        	this.getLabourChargeTypeList().put("QUANTITYWISE", "BY QUANTITY");
	        	this.getLabourChargeTypeList().put("PERCENTAGEWISE", "BY PERCENTAGE");
	        	this.setAddLabourCharges((String) settings.get("add_making_charge_column"));
	        	this.setLabourChargeType("QUANTITYWISE");
	        	
	        	  
	        	this.getSessionMap().put("productList", this.getProductInfoList());
	        	this.getSessionMap().put("quotationInformation", this.getBillInfoDAO());	        	
	        	this.getSessionMap().put("quotationProductList", this.getProductInfoList());
	        							  
	        	
	        	
	        	this.setDiscountType((String) settings.get("discount_type"));
	        	
	           	this.setCustomerTypeList(GetListResource.getCustomerTypeList());
	    	      
	        
	           	this.setCustomerType(this.getBillInfoDAO().getCustomerType());
	           	this.setCustomerMobileNumber(this.getBillInfoDAO().getCustomerMobile());
	           	this.setCustomerId(this.getBillInfoDAO().getCustId());
	           	this.setCustomerName(this.getBillInfoDAO().getCustomerName());
	           	
	        	if(this.getBillInfoDAO().getBuyerOrderNumber() !=null)
	        	{
	        		this.setBuyerOrderNumber(this.getBillInfoDAO().getBuyerOrderNumber());
	        	}
	        	
	        	
	        	if(this.getBillInfoDAO().getDispatchDocNumber() != null)
	        	{
	        		this.setDispatchDocNumber(this.getBillInfoDAO().getDispatchDocNumber());
	        	}
	        	
	        	if(this.getBillInfoDAO().getVehicleNumber() != null)
	        	{
	        		this.setVehicleNumber(this.getBillInfoDAO().getVehicleNumber());
	        	}
	         	
	           	this.setCustomerIdNameAll(this.getBillInfoDAO().getCustId()+"-"+this.getBillInfoDAO().getCustomerName()+"-"+this.getBillInfoDAO().getCustomerMobile()+"-"+this.getBillInfoDAO().getCustomerType());
	        	
	        	this.setAccountList(GetAccountListForDrCr.getAccountList((this.getSessionMap()))); 
	        	result = SUCCESS;
	        	
	        }
	        
	        return result;
	}
	
	
	
	
	public HttpSession getSession() {
		return session;
	}
	public void setSession(HttpSession session) {
		this.session = session;
	}
	public String getBillDateTime() {
		return billDateTime;
	}
	public void setBillDateTime(String billDateTime) {
		this.billDateTime = billDateTime;
	}
	public String getOperator() {
		return operator;
	}
	public void setOperator(String operator) {
		this.operator = operator;
	}
	public String getPaymentType() {
		return paymentType;
	}
	public void setPaymentType(String paymentType) {
		this.paymentType = paymentType;
	}
	public String getCustomerName() {
		return customerName;
	}
	public void setCustomerName(String customerName) {
		this.customerName = customerName;
	}
	public String getProductName() {
		return productName;
	}
	public void setProductName(String productName) {
		this.productName = productName;
	}
	public String getProductMarathiName() {
		return productMarathiName;
	}
	public void setProductMarathiName(String productMarathiName) {
		this.productMarathiName = productMarathiName;
	}
	public String getAlias() {
		return alias;
	}
	public void setAlias(String alias) {
		this.alias = alias;
	}
	public String getSalesUnit() {
		return salesUnit;
	}
	public void setSalesUnit(String salesUnit) {
		this.salesUnit = salesUnit;
	}
	public String getTaxPercentage() {
		return taxPercentage;
	}
	public void setTaxPercentage(String taxPercentage) {
		this.taxPercentage = taxPercentage;
	}
	
	public String getBillType() {
		return billType;
	}
	public void setBillType(String billType) {
		this.billType = billType;
	}
	
	public Float getSalesRate() {
		return salesRate;
	}




	public void setSalesRate(Float salesRate) {
		this.salesRate = salesRate;
	}




	public Float getQuantity() {
		return quantity;
	}




	public void setQuantity(Float quantity) {
		this.quantity = quantity;
	}




	public Float getDiscountPercentage() {
		return discountPercentage;
	}




	public void setDiscountPercentage(Float discountPercentage) {
		this.discountPercentage = discountPercentage;
	}




	public Float getDiscountRupees() {
		return discountRupees;
	}




	public void setDiscountRupees(Float discountRupees) {
		this.discountRupees = discountRupees;
	}




	public Float getAmount() {
		return amount;
	}




	public void setAmount(Float amount) {
		this.amount = amount;
	}




	public Float getAmountAfterDiscount() {
		return amountAfterDiscount;
	}




	public void setAmountAfterDiscount(Float amountAfterDiscount) {
		this.amountAfterDiscount = amountAfterDiscount;
	}




	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 Map<String, String> getBillTypeList() {
		return billTypeList;
	}
	public void setBillTypeList(Map<String, String> billTypeList) {
		this.billTypeList = billTypeList;
	}
	public List<BillProductInfoDAO> getProductInfoList() {
		return productInfoList;
	}
	public void setProductInfoList(List<BillProductInfoDAO> productInfoList) {
		this.productInfoList = productInfoList;
	}
	public String getDefaultValue() {
		return defaultValue;
	}
	public void setDefaultValue(String defaultValue) {
		this.defaultValue = defaultValue;
	}
	
	public Float getTotalAmount() {
		return totalAmount;
	}




	public void setTotalAmount(Float totalAmount) {
		this.totalAmount = totalAmount;
	}




	public Float getTotalDiscount() {
		return totalDiscount;
	}




	public void setTotalDiscount(Float totalDiscount) {
		this.totalDiscount = totalDiscount;
	}




	public Float getTotalAmouAfterDisount() {
		return totalAmouAfterDisount;
	}




	public void setTotalAmouAfterDisount(Float totalAmouAfterDisount) {
		this.totalAmouAfterDisount = totalAmouAfterDisount;
	}




	public String getProductNameForDelete() {
		return productNameForDelete;
	}




	public void setProductNameForDelete(String productNameForDelete) {
		this.productNameForDelete = productNameForDelete;
	}




	public BillInformationDAO getBillInfoDAO() {
		return billInfoDAO;
	}




	public void setBillInfoDAO(BillInformationDAO billInfoDAO) {
		this.billInfoDAO = billInfoDAO;
	}




	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 Float getTransportCharge() {
		return transportCharge;
	}




	public void setTransportCharge(Float transportCharge) {
		this.transportCharge = transportCharge;
	}




	public static long getSerialversionuid() {
		return serialVersionUID;
	}




	public String getComments() {
		return comments;
	}




	public void setComments(String comments) {
		this.comments = comments;
	}




	public String getViewBill() {
		return viewBill;
	}




	public void setViewBill(String viewBill) {
		this.viewBill = viewBill;
	}




	public String getId() {
		return id;
	}




	public void setId(String id) {
		this.id = id;
	}




	public String getSerialNumber() {
		return serialNumber;
	}




	public void setSerialNumber(String serialNumber) {
		this.serialNumber = serialNumber;
	}




	public Float getAmountPaid() {
		return amountPaid;
	}




	public void setAmountPaid(Float amountPaid) {
		this.amountPaid = amountPaid;
	}




	public Float getReturnAmount() {
		return returnAmount;
	}




	public void setReturnAmount(Float returnAmount) {
		this.returnAmount = returnAmount;
	}




	public List<BillProductInfoDAO> getDeletedProductInfoList() {
		return deletedProductInfoList;
	}




	public void setDeletedProductInfoList(List<BillProductInfoDAO> deletedProductInfoList) {
		this.deletedProductInfoList = deletedProductInfoList;
	}




	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 Float getQuantity2() {
		return quantity2;
	}




	public void setQuantity2(Float quantity2) {
		this.quantity2 = quantity2;
	}




	public Float getCashAmount() {
		return cashAmount;
	}




	public void setCashAmount(Float cashAmount) {
		this.cashAmount = cashAmount;
	}




	public Float getBankRtgsUpiAmount() {
		return bankRtgsUpiAmount;
	}




	public void setBankRtgsUpiAmount(Float bankRtgsUpiAmount) {
		this.bankRtgsUpiAmount = bankRtgsUpiAmount;
	}




	public Map<String, String> getAccountList() {
		return accountList;
	}




	public void setAccountList(Map<String, String> accountList) {
		this.accountList = accountList;
	}




	public List<String> getPaymentTypeList2() {
		return paymentTypeList2;
	}




	public void setPaymentTypeList2(List<String> paymentTypeList2) {
		this.paymentTypeList2 = paymentTypeList2;
	}




	public String getTaxId() {
		return taxId;
	}




	public void setTaxId(String taxId) {
		this.taxId = taxId;
	}




	public String getAccountId() {
		return accountId;
	}




	public void setAccountId(String accountId) {
		this.accountId = accountId;
	}




	public String getDiscountType() {
		return discountType;
	}




	public void setDiscountType(String discountType) {
		this.discountType = discountType;
	}




	public String getLastBillNumber() {
		return lastBillNumber;
	}




	public void setLastBillNumber(String lastBillNumber) {
		this.lastBillNumber = lastBillNumber;
	}




	public ArrayList<String> getCustomerTypeList() {
		return customerTypeList;
	}




	public void setCustomerTypeList(ArrayList<String> customerTypeList) {
		this.customerTypeList = customerTypeList;
	}




	public String getCustomerType() {
		return customerType;
	}




	public void setCustomerType(String customerType) {
		this.customerType = customerType;
	}




	public String getCustomerId() {
		return customerId;
	}




	public void setCustomerId(String customerId) {
		this.customerId = customerId;
	}




	public String getCustomerMobileNumber() {
		return customerMobileNumber;
	}




	public void setCustomerMobileNumber(String customerMobileNumber) {
		this.customerMobileNumber = customerMobileNumber;
	}




	public String getCustomerIdNameAll() {
		return customerIdNameAll;
	}




	public void setCustomerIdNameAll(String customerIdNameAll) {
		this.customerIdNameAll = customerIdNameAll;
	}




	public String getBuyerOrderNumber() {
		return buyerOrderNumber;
	}




	public void setBuyerOrderNumber(String buyerOrderNumber) {
		this.buyerOrderNumber = buyerOrderNumber;
	}




	public String getDispatchDocNumber() {
		return dispatchDocNumber;
	}




	public void setDispatchDocNumber(String dispatchDocNumber) {
		this.dispatchDocNumber = dispatchDocNumber;
	}




	public String getVehicleNumber() {
		return vehicleNumber;
	}




	public void setVehicleNumber(String vehicleNumber) {
		this.vehicleNumber = vehicleNumber;
	}




	public Map<String, String> getDiscountTypeList() {
		return discountTypeList;
	}




	public void setDiscountTypeList(Map<String, String> discountTypeList) {
		this.discountTypeList = discountTypeList;
	}




	public String getQuotationDateTime() {
		return quotationDateTime;
	}




	public void setQuotationDateTime(String quotationDateTime) {
		this.quotationDateTime = quotationDateTime;
	}




	public String getQuotationNumber() {
		return quotationNumber;
	}




	public void setQuotationNumber(String quotationNumber) {
		this.quotationNumber = quotationNumber;
	}  
	
	
	
	
	
	
	

}
