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.DecimalFormat;
import java.util.HashMap;
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.cipher.EncryptionDecryption;
import com.reallinkbilling.db.GetDBConnection;
import com.reallinkbilling.utils.GetSupplierDebtAmountById;

public class UpdateSupplierPaymentJsp extends ActionSupport implements SessionAware{

	/**
	 * 
	 */
	private static final long serialVersionUID = -1867603398762964527L;
	private String paymentId;
	private HttpSession session;
	private String paymentDate;
	private String supplierName;
	private String key;
	private String amountPaid;
	private String paymentDetails;
	private String debtAmount;
	private Map<String,String> customerSearchOptionList = new HashMap<String,String>();
	private String customerSearchOption;
	private String defaultValue;
	private SessionMap<String,Object> sessionMap;
	private String supplierId;
	
	public String execute(){
		
		String result = "login";
	
	        if(this.getSessionMap()==null || this.getSessionMap().get("login")==null){ 
	        	addActionMessage("Please Login First !!!");
	        	result = "login";
	        }  
	        else{
	        	
	        	
	        	String shopId = (String) this.getSessionMap().get("shopId");
	    		String shopCode = (String) this.getSessionMap().get("shopCode");
				
	    		DecimalFormat df = new DecimalFormat("#.##");
	        	customerSearchOptionList.put("BYNAME", "BY NAME");
	        	customerSearchOptionList.put("BYMOBILE", "BY MOBILE");
	        	customerSearchOptionList.put("BYADDRESS","BY ADDRESS");
				setCustomerSearchOption("BYNAME");
				setDefaultValue("BYNAME");
	        	
	        	
	        	Connection connection = null;
	       		PreparedStatement preparedStatement= null;
	       		ResultSet resultSet = null;
	       		
	       		String sql = "";
	       		String db = (String) this.getSessionMap().get("db");
	       		try 
	       		{
	       			
	       			
					connection = GetDBConnection.getDBConnection(db);
	       			String actualPaymentId = EncryptionDecryption.decrypt(this.getPaymentId());
	       			
	       			sql = "SELECT "
	       					+ "sppd.supplier_id,"
	       					+ "ssm.suplier_name,"
	       					+ "DATE_FORMAT(sppd.payment_date,'%d-%m-%Y'),"
	       					+ "sppd.payment_amount,"
	       					+ "sppd.payment_type,"
	       					+ "sppd.payement_details "
	       					+ "FROM "
	       					+ "shop_supplier_payment_details AS sppd "
	       					+ "INNER JOIN "
	       					+ "shop_supplier_master AS ssm "
	       					+ "ON "
	       					+ "ssm.supplier_id = sppd.supplier_id "
	       					+ "WHERE "
	       					+ "sppd.entry_id = ? "
	       					+ "AND "
	       					+ "sppd.shop_id = ? "
	       					+ "AND "
	       					+ "sppd.shop_code = ? "
	       					+ "AND "
	       					+ "ssm.shop_id = ? "
	       					+ "AND "
	       					+ "ssm.shop_code = ? ";
	       			
	       			preparedStatement = connection.prepareStatement(sql);
	       			
	       			preparedStatement.setString(1, actualPaymentId);
	       			preparedStatement.setString(2, shopId);
	       			preparedStatement.setString(3, shopCode);
	       			preparedStatement.setString(4, shopId);
	       			preparedStatement.setString(5, shopCode);
	       			
	       			resultSet = preparedStatement.executeQuery();
	       			
	       			if(resultSet.next())
	       			{
	       				this.setSupplierName(resultSet.getString(2));
	       				this.setPaymentDate(resultSet.getString(3));
	       				this.setAmountPaid(resultSet.getString(4));
	       				this.setPaymentDetails(resultSet.getString(5));
	       				this.setKey(EncryptionDecryption.encrypt(actualPaymentId));
	       				
	    	        	this.setSupplierId(EncryptionDecryption.encrypt(resultSet.getString(1)));
	    	        	this.setDebtAmount(df.format(GetSupplierDebtAmountById.getSupplierDebtAmount(resultSet.getString(1), this.getSessionMap())));
	    	        
	       				result = SUCCESS;
	       			}
	       			else
	       			{
	       				result = "error";
	       				addActionError("URL Tampered! Please try again!");
	       			}
	       			
	       			
	       		}
	       		catch (SQLException e) {
		       		result = "error";				
					addActionMessage(e.getErrorCode()+"||"+e.getMessage()+"|| SOME ERROR WHILE GETTING INFORMATION!");
			
	       	 	} catch (IOException e) {
				
				result = "error";
				addActionMessage(e.getMessage()+"|| SOME ERROR");
			}	
			finally
			{
				if(resultSet != null)
				{
					try {
						resultSet.close();
					} catch (SQLException e) {
						
					}
				}
				if(preparedStatement != null)
				{
					try {preparedStatement.close();} catch (SQLException e) {}
				}
				if(connection != null)
				{
					try {connection.close();} catch (SQLException e) {}
				}
				
			}
		
	        	
	        
	        }
	
	        return result;
}

public String getPaymentId() {
	return paymentId;
}

public void setPaymentId(String paymentId) {
	this.paymentId = paymentId;
}

public HttpSession getSession() {
	return session;
}

public void setSession(HttpSession session) {
	this.session = session;
}

public String getPaymentDate() {
	return paymentDate;
}

public void setPaymentDate(String paymentDate) {
	this.paymentDate = paymentDate;
}

public String getSupplierName() {
	return supplierName;
}

public void setSupplierName(String supplierName) {
	this.supplierName = supplierName;
}

public String getKey() {
	return key;
}

public void setKey(String key) {
	this.key = key;
}

public String getAmountPaid() {
	return amountPaid;
}

public void setAmountPaid(String amountPaid) {
	this.amountPaid = amountPaid;
}

public String getPaymentDetails() {
	return paymentDetails;
}

public void setPaymentDetails(String paymentDetails) {
	this.paymentDetails = paymentDetails;
}

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 String getDefaultValue() {
	return defaultValue;
}

public void setDefaultValue(String defaultValue) {
	this.defaultValue = defaultValue;
}

public static long getSerialversionuid() {
	return serialVersionUID;
}


public String getDebtAmount() {
	return debtAmount;
}

public void setDebtAmount(String debtAmount) {
	this.debtAmount = debtAmount;
}

public void setSession(Map<String, Object> map) {  
sessionMap=(SessionMap<String, Object>)map;  
}

public SessionMap<String, Object> getSessionMap() {
return sessionMap;
}

public void setSessionMap(SessionMap<String, Object> sessionMap) {
this.sessionMap = sessionMap;
}

public String getSupplierId() {
	return supplierId;
}

public void setSupplierId(String supplierId) {
	this.supplierId = supplierId;
}


	
	

}
