package com.reallinkbilling.ajax;

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.PurchaseInvoiceInformationDAO;

public class GetOtherExpenseChangeAmount extends ActionSupport implements SessionAware{
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 8702063103770204026L;
	private HttpSession session;
	private SessionMap<String,Object> sessionMap;  

	private Float frightCharges;
	private Float otherExpense;
	private Float tcsAmount;
	private Float totalPurchaseAmount;
	

	@Override
	public String execute() throws Exception {
		
	
			 
			String result = "login";;
			session=ServletActionContext.getRequest().getSession(false); 
			
		        if(session==null || session.getAttribute("login")==null){ 
		        	addActionMessage("Please Login First !!!");
		        	result = "login";
		        }  
		        else{
		        	
		        	PurchaseInvoiceInformationDAO infoDAO = (PurchaseInvoiceInformationDAO) this.getSessionMap().get("purchaseInformation");
			        
		        
		        	if(this.getOtherExpense() != null || this.getOtherExpense() > 0)
		        	{
		        	
		        		
		        		float basicPayableAmount = infoDAO.getTotalNettAmount() 
					        				+ infoDAO.getTotalCGST() 
					        				+ infoDAO.getTotalSGST()
					        				+ infoDAO.getTotalIGST();
		        		
		        		basicPayableAmount = basicPayableAmount+ + this.getFrightCharges() + this.getOtherExpense();
		        		
		        		float tcsAmtLocal = basicPayableAmount * (infoDAO.getTcsPercentage() / 100F);
		        		float totalPurchaseAmtLocal = basicPayableAmount + tcsAmtLocal;
		       		
		        		this.setTcsAmount((float) (Math.round(tcsAmtLocal * 100) / 100.0));
		        		this.setTotalPurchaseAmount((float) Math.round(totalPurchaseAmtLocal));
		        
		        		
		        		infoDAO.setPurchaseTotalAmount((float) Math.round(totalPurchaseAmtLocal));
		        		infoDAO.setTcsAmount((float) (Math.round(tcsAmtLocal * 100) / 100.0));
		        		infoDAO.setFrightCharges(this.getFrightCharges());
		        		infoDAO.setOtherExpense(this.getOtherExpense());
		        		this.getSessionMap().put("purchaseInformation", infoDAO);
		        		result = SUCCESS;
		        	}
		        	else
		        	{
		        		this.setFrightCharges(0F);
		        		result = ERROR;
		        	}
		        	
		        	
		        	
		        }
		        
		        return result;
		        
	}


	public Float getFrightCharges() {
		return frightCharges;
	}


	public void setFrightCharges(Float frightCharges) {
		this.frightCharges = frightCharges;
	}


	public Float getOtherExpense() {
		return otherExpense;
	}


	public void setOtherExpense(Float otherExpense) {
		this.otherExpense = otherExpense;
	}


	public Float getTcsAmount() {
		return tcsAmount;
	}


	public void setTcsAmount(Float tcsAmount) {
		this.tcsAmount = tcsAmount;
	}


	public Float getTotalPurchaseAmount() {
		return totalPurchaseAmount;
	}


	public void setTotalPurchaseAmount(Float totalPurchaseAmount) {
		this.totalPurchaseAmount = totalPurchaseAmount;
	}


	public static long getSerialversionuid() {
		return serialVersionUID;
	}

	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;  
	}
	
	

}
