package com.reallinkbilling.add;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

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.DAO.ProductBarcodeDetailsDAO;
import com.reallinkbilling.resource.GetListResource;

public class ChangeProductQuantityBarcodeBill extends ActionSupport implements SessionAware{

	/**
	 * 
	 */
	private static final long serialVersionUID = 4314678329260462055L;
	
	private SessionMap<String,Object> sessionMap;  
	

	private String changeQuantityFormkey;

	private String billDateTime;
	private String operator;
	private String paymentType;
	private String customerName;
	private String productName;
	private String productMarathiName;
	private String alias;
	private String salesUnit;
	private String taxId;
	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 Float transportCharge;
	private String comments;
	private boolean sendSMS;
	private String alternateMobileNumber;
	private Float amountPaid;
	private Float returnAmount;
	private boolean billSaved;
	private String billNumber;
	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 ArrayList<String> serailBatchIdList = new ArrayList<String>();
	private String serailNoBatchId;
	private List<BillProductInfoDAO> productInfoList ;
	private String defaultValue;
	private boolean addQuantity2;
	private Float totalAmount;
	private Float totalDiscount;
	private Float totalAmouAfterDisount;
	private String productCode;
	private boolean productReturn;
	private String removeVaules="false";
	
	private BillInformationDAO billInfoDAO = null;
	
	private String viewBill;
	
	private Map<String,String> rateTypeList = new LinkedHashMap<String,String>();
	private String rateType;
	private Float makingLabourAmount;
	
	private Map<String,String> labourChargeTypeList = new LinkedHashMap<String,String>();
	private String labourChargeType;
	private String addLabourCharges;
	private Float cashAmount;
	private Float bankRtgsUpiAmount;
	
	private Map<String,String> discountTypeList = new LinkedHashMap<String,String>();
	private String discountType;

	private String customerType;
	private String customerId;
	private String customerMobileNumber;

	private String customerIdNameAll;
	
	public String execute() {
		 
		String result = "login";
	       if(this.getSessionMap()==null || this.getSessionMap().get("login")==null){ 
	        	addActionMessage("Please Login First !!!");
	        	result = "login";
	        }  
	        else{
	        	
	        	this.setProductInfoList((List<BillProductInfoDAO>) this.getSessionMap().get("productList"));
	        	
	        	this.setBillInfoDAO((BillInformationDAO) this.getSessionMap().get("billInformation"));
				
	        	this.getBillInfoDAO().setBillSaved(false);
	      
    			String [] keyData = this.getChangeQuantityFormkey().split("-");
    			
    			
    			for(int i=0;i<keyData.length;i++)
    			{
    				String [] productData = keyData[i].split("_");
    			 		
    				String productCode = productData[0];
    				float productNewQuantity = Float.parseFloat(productData[1]);
    			
    			  	HashMap<String,ProductBarcodeDetailsDAO> productBarcodeDetailsMap = (HashMap<String, ProductBarcodeDetailsDAO>) this.getSessionMap().get("barcodeProductDetails");
    			       
    				for(BillProductInfoDAO infoDAO : this.getProductInfoList())
    	        	{
    	        		
    					if(infoDAO.getProductCode().equals(productCode))
    					{
    					   	ProductBarcodeDetailsDAO productBarcodeDetails = productBarcodeDetailsMap.get(productCode);
    					     
    				  
    						infoDAO.setQuantity(productNewQuantity);
    				       
    						float productPriceBeforeDis = productBarcodeDetails.getSalesPrice();
    			        	float productDiscount = productBarcodeDetails.getBarcodeMRP() - productPriceBeforeDis;
    			        	
    			        
    			        	float totalProductDiscount = productNewQuantity*productDiscount;
    			        	
    			        	float floatAmountAftDiscount = productBarcodeDetails.getSalesPrice() * productNewQuantity;
    			        	
    			        	float sgst = 0.0F;
    			        	float cgst = 0.0F;
    			        	float igst = 0.0F;
    			        	
    			        	if(this.getBillInfoDAO().getBillType().equals("TAX-INVOICE"))
    						{
    						
    							Map<String,String> taxInfo = GetListResource.GetTaxNamePercentage(""+productBarcodeDetails.getTaxId(),this.getSessionMap());
    				        	
    							String taxName = taxInfo.get("TAX-NAME");
    							String taxPercentage = taxInfo.get("TAX-PERCENTAGE");
    							
    							infoDAO.setTaxName(taxName);
    							infoDAO.setTaxPercentage(Float.parseFloat(taxPercentage));
    						
    							
    							if(infoDAO.getRateType().equals("INCLUDEGST"))
    							{
    								
    								float floatTaxRate = Float.parseFloat(taxPercentage);	
    								
    								float basicTax = floatTaxRate+100.0F;
    					        	
    					        	float grossAmount = (floatAmountAftDiscount / basicTax) * 100.0F;
    					        	
    					        	float totalTax = floatAmountAftDiscount - grossAmount;
    								
    								if(taxName.toUpperCase().contains("IGST"))
    								{
    									igst = totalTax;
    								}
    								else
    								{
    									sgst = totalTax/2.0F;
    						        	cgst = totalTax/2.0F;
    								}
    				        	
    				        	     
    								 DecimalFormat df = new DecimalFormat("0.00");
    							        
    						        	
    								 infoDAO.setProductTotalDiscount(totalProductDiscount);
    								 infoDAO.setGrossAmount(Float.valueOf(df.format(grossAmount)));
    								 infoDAO.setSgst(Float.valueOf(df.format(sgst)));
    								 infoDAO.setCgst(Float.valueOf(df.format(cgst)));
    								 infoDAO.setIgst(Float.valueOf(df.format(igst)));
    					        	 if(this.isProductReturn())
    						         {
    					        		 infoDAO.setAmountAfterDiscount(-(floatAmountAftDiscount));
    					        		 infoDAO.setAmount(-(productBarcodeDetails.getBarcodeMRP()*productNewQuantity));
    						         }
    					        	 else
    					        	 {
    					        		 infoDAO.setAmountAfterDiscount(floatAmountAftDiscount);
    					        		 infoDAO.setAmount(productBarcodeDetails.getBarcodeMRP()*productNewQuantity);

    					        	 }
    							}
    							
    							if(infoDAO.getRateType().equals("EXCLUDEGST"))
    							{
    								float floatTaxRate = Float.parseFloat(taxPercentage);	
    								float totalTax = floatAmountAftDiscount * (floatTaxRate/100.0F);
    								
    								if(taxName.toUpperCase().contains("IGST"))
    								{
    									igst = totalTax;
    								}
    								else
    								{
    									sgst = totalTax/2.0F;
    						        	cgst = totalTax/2.0F;
    								}
    								
    								
    								 DecimalFormat df = new DecimalFormat("0.00");
    							        
    						        	
    								 infoDAO.setProductTotalDiscount(totalProductDiscount);
    								 infoDAO.setGrossAmount(Float.valueOf(df.format(floatAmountAftDiscount)));
    								 infoDAO.setSgst(Float.valueOf(df.format(sgst)));
    								 infoDAO.setCgst(Float.valueOf(df.format(cgst)));
    								 infoDAO.setIgst(Float.valueOf(df.format(igst)));
    					        	
    								 infoDAO.setAmountAfterDiscount(floatAmountAftDiscount + totalTax);
    								 infoDAO.setAmount(productBarcodeDetails.getBarcodeMRP() + totalTax);
    							  
    					        	 
    							}
    						}
    						else
    						{
    							infoDAO.setProductTotalDiscount(totalProductDiscount);
    							infoDAO.setGrossAmount(floatAmountAftDiscount);
    							infoDAO.setSgst(0.0F);
    							infoDAO.setCgst(0.0F);
    							infoDAO.setIgst(0.0F);
    				        	
    				        		 infoDAO.setAmountAfterDiscount(floatAmountAftDiscount);
    				        		 infoDAO.setAmount(productBarcodeDetails.getBarcodeMRP()*productNewQuantity);
    					        
    				        	 
    						}
    					}
    	        		
    	        	}
    				
    			}
    			
    			
    		 	
	        	
	        	
	        	
	     				
			
	        	
	        	
	        	if(this.getBillInfoDAO() != null)
	        	{
	        		
	        		this.setBillNumber(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.setAlias(dao.getAlias());
	        	this.setProductName(dao.getProductId()+"-"+dao.getProductName()+"-"+dao.getProductCode());
	        	this.setProductMarathiName(dao.getProductNameMarathi());
	        	this.setSalesUnit(dao.getSalesUnit());
	       
	        	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());*/
	        	
	        	  
	        	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());
	        	
	        	
	        	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");
	        	
	    		
	    		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("billInformation", this.getBillInfoDAO());	 
	        	
	        	if(this.getPaymentType().equals("CASH"))
	        	{
	        		this.setAmountPaid(this.getTotalAmouAfterDisount());
	        		this.setReturnAmount(0.0F);
	        	}
	        	if(this.getPaymentType().equals("CREDIT")){
	        		this.setAmountPaid(0.0F);
	        		this.setReturnAmount(-this.getTotalAmouAfterDisount());
	        	}
	        	
	        	
	        	
	        	this.setCustomerIdNameAll(this.getBillInfoDAO().getCustomerIdNameAll());
	        	
	        //	System.out.println(this.getBillInfoDAO().getCustomerIdNameAll());

	        	this.setCustomerId(this.getBillInfoDAO().getCustomerIdNameAll().split("-")[0]);
	        	this.setCustomerName(this.getBillInfoDAO().getCustomerIdNameAll().split("-")[1]);
	        	this.setCustomerMobileNumber(this.getBillInfoDAO().getCustomerIdNameAll().split("-")[2]);
	        	this.setCustomerType(this.getBillInfoDAO().getCustomerIdNameAll().split("-")[3]);
	       
	        	
	        	result = SUCCESS;
	        	
	        }
	        
	     
	        return result;
	}

	
	public void validate() {}

	


	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 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 getTaxId() {
		return taxId;
	}


	public void setTaxId(String taxId) {
		this.taxId = taxId;
	}


	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 getQuantity2() {
		return quantity2;
	}


	public void setQuantity2(Float quantity2) {
		this.quantity2 = quantity2;
	}


	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 String getBillType() {
		return billType;
	}


	public void setBillType(String billType) {
		this.billType = billType;
	}


	public Float getAmountAfterDiscount() {
		return amountAfterDiscount;
	}


	public void setAmountAfterDiscount(Float amountAfterDiscount) {
		this.amountAfterDiscount = amountAfterDiscount;
	}


	public Float getTransportCharge() {
		return transportCharge;
	}


	public void setTransportCharge(Float transportCharge) {
		this.transportCharge = transportCharge;
	}


	public String getComments() {
		return comments;
	}


	public void setComments(String comments) {
		this.comments = comments;
	}


	public boolean isSendSMS() {
		return sendSMS;
	}


	public void setSendSMS(boolean sendSMS) {
		this.sendSMS = sendSMS;
	}


	public String getAlternateMobileNumber() {
		return alternateMobileNumber;
	}


	public void setAlternateMobileNumber(String alternateMobileNumber) {
		this.alternateMobileNumber = alternateMobileNumber;
	}


	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 boolean isBillSaved() {
		return billSaved;
	}


	public void setBillSaved(boolean billSaved) {
		this.billSaved = billSaved;
	}


	public String getBillNumber() {
		return billNumber;
	}


	public void setBillNumber(String billNumber) {
		this.billNumber = billNumber;
	}


	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 ArrayList<String> getSerailBatchIdList() {
		return serailBatchIdList;
	}


	public void setSerailBatchIdList(ArrayList<String> serailBatchIdList) {
		this.serailBatchIdList = serailBatchIdList;
	}


	public String getSerailNoBatchId() {
		return serailNoBatchId;
	}


	public void setSerailNoBatchId(String serailNoBatchId) {
		this.serailNoBatchId = serailNoBatchId;
	}


	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 boolean isAddQuantity2() {
		return addQuantity2;
	}


	public void setAddQuantity2(boolean addQuantity2) {
		this.addQuantity2 = addQuantity2;
	}


	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 getProductCode() {
		return productCode;
	}


	public void setProductCode(String productCode) {
		this.productCode = productCode;
	}


	public boolean isProductReturn() {
		return productReturn;
	}


	public void setProductReturn(boolean productReturn) {
		this.productReturn = productReturn;
	}


	public String getRemoveVaules() {
		return removeVaules;
	}


	public void setRemoveVaules(String removeVaules) {
		this.removeVaules = removeVaules;
	}


	public BillInformationDAO getBillInfoDAO() {
		return billInfoDAO;
	}


	public void setBillInfoDAO(BillInformationDAO billInfoDAO) {
		this.billInfoDAO = billInfoDAO;
	}


	public String getViewBill() {
		return viewBill;
	}


	public void setViewBill(String viewBill) {
		this.viewBill = viewBill;
	}


	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 Float getMakingLabourAmount() {
		return makingLabourAmount;
	}


	public void setMakingLabourAmount(Float makingLabourAmount) {
		this.makingLabourAmount = makingLabourAmount;
	}


	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 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> 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 getChangeQuantityFormkey() {
		return changeQuantityFormkey;
	}


	public void setChangeQuantityFormkey(String changeQuantityFormkey) {
		this.changeQuantityFormkey = changeQuantityFormkey;
	}


	public static long getSerialversionuid() {
		return serialVersionUID;
	}


	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;
	}


}

	
