package com.reallinkbilling.update;

import java.io.IOException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.HashMap;
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.cipher.EncryptionDecryption;
import com.reallinkbilling.db.GetDBConnection;

public class UpdateSupplierPaymentDetails extends ActionSupport implements SessionAware{

	/**
	 * 
	 */
	private static final long serialVersionUID = 1556072854215463755L;
	
	private HttpSession session;
	
	private String paymentDate;
	private String supplierName;
	private String key;
	private Float amountPaid;
	private String paymentDetails;
	private String supplierId;
	private Map<String,String> customerSearchOptionList = new HashMap<String,String>();
	private String customerSearchOption;
	private String defaultValue;
	
	private SessionMap<String,Object> sessionMap;
	
	public String execute(){
		
		String result = "login";
		session=ServletActionContext.getRequest().getSession(false); 
		 
	        if(session==null || session.getAttribute("login")==null){ 
	        	addActionMessage("Please Login First !!!");
	        	result = "login";
	        }  
	        else{
	        	
	        	String fullUserName = (String) session.getAttribute("userFullName");
	        	
	        	String splitUserName [] = new String[5];
	        	
	        	String entryBy = "";
	        	try
	        	{
	        		splitUserName = fullUserName.split(" ");
	        		entryBy = splitUserName[0];
	        		
	        	}catch(Exception e)
	        	{
	        		entryBy =fullUserName;
	        	}
	        	
	        	customerSearchOptionList.put("BYNAME", "BY NAME");
	        	customerSearchOptionList.put("BYMOBILE", "BY MOBILE");
	        	customerSearchOptionList.put("BYADDRESS","BY ADDRESS");
				setCustomerSearchOption("BYNAME");
				setDefaultValue("BYNAME");
	        	
				 java.util.Date date = new java.util.Date();
       	      long t = date.getTime();	        	     
       	      java.sql.Time time = new java.sql.Time(t);
       	      
	        	Connection connection = null;
	       		PreparedStatement preparedStatement= null;
	       		ResultSet resultSet = null;
	       		
	       	
	       		String sql = "";
	       		String db = (String) this.getSessionMap().get("db");
	       		try 
	       		{
	       			String shopId = (String) this.getSessionMap().get("shopId");
		    		String shopCode = (String) this.getSessionMap().get("shopCode");
	       		 	String actualSupplierId = EncryptionDecryption.decrypt(this.getSupplierId());
	       			String paymentId = EncryptionDecryption.decrypt(this.getKey());
	       			
					connection = GetDBConnection.getDBConnection(db);
	       			
	       			sql = "UPDATE "
	       					+ "shop_supplier_payment_details "
	       					+ "SET "
	       					+ "payment_date = STR_TO_DATE(?,'%d-%m-%Y'),"
	       					+ "payment_time = ?,"
	       					+ "payment_amount = ?,"
	       					+ "payement_details = ?,"
	       					+ "entry_by = ? "
	       					+ "WHERE "
	       					+ "entry_id = ? "
	       					+ "AND "
	       					+ "supplier_id = ? "
	       					+ "AND "
	       					+ "shop_id = ? "
	       					+ "AND "
	       					+ "shop_code = ?";
	       			
	       			preparedStatement = connection.prepareStatement(sql);
	       			
	       			preparedStatement.setString(1, this.getPaymentDate());
	       			preparedStatement.setTime(2, time);
	       			preparedStatement.setFloat(3, this.getAmountPaid());
	       			preparedStatement.setString(4, this.getPaymentDetails());
	       			preparedStatement.setString(5, entryBy);
	       			preparedStatement.setString(6, paymentId);
	       			preparedStatement.setString(7, actualSupplierId);
	       			preparedStatement.setString(8, shopId);
	       			preparedStatement.setString(9, shopCode);
	       			
	       			System.out.println(preparedStatement.toString());
	       			
	       			
	       			int rowUpdated = preparedStatement.executeUpdate();
	       			
	       			if(rowUpdated > 0)
	       			{
	       				result = SUCCESS;
	       				addActionMessage("Suppliers' payment details updated successfully!");
	       			}
	       			else
	       			{
	       				result = ERROR;
	       				addActionError("Sorry! Suppliers' payments details not updated! Please try again!");
	       			}
	       			
	       		}
       		catch (SQLException e) {
       			e.printStackTrace();
       			result = ERROR;	
				addActionError(e.getErrorCode()+"||"+e.getMessage()+"|| SOME ERROR WHILE GETTING INFORMATION!");
		
       	 	} catch (IOException e) {
			
       	 		result = ERROR;
			addActionError(e.getMessage()+"|| SOME ERROR WHILE UPDATING PAYMENT DETAILS!");
			}	
			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;
}

@Override
public void validate() {
	if(!hasActionErrors())
	{
		if(this.getAmountPaid() ==null || this.getAmountPaid().equals(""))
		{
			addActionError("Please provide correct amount paid!");
		}
		else if(this.getAmountPaid() <= 0 )
		{
			addActionError("Amount paid cannot be less than equal to '0'!");
		}
	}
}
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 Float getAmountPaid() {
	return amountPaid;
}


public void setAmountPaid(Float 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 getSupplierId() {
	return supplierId;
}

public void setSupplierId(String supplierId) {
	this.supplierId = supplierId;
}

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;
}


}
