/**
 * Copyright (c) 2009 Penril Datability (SEA) Sdn Bhd All rights reserved.
 *
 * This software is copyrighted. Under the copyright laws, this software
 * may not be copied, in whole or in part, without prior written consent
 * of Penril Datability (SEA) Sdn Bhd or its assignees. This software is
 * provided under the terms of a license between Penril Datability (SEA)
 * Sdn Bhd and the recipient, and its use is subject to the terms of that
 * license.
 */
package my.com.kfh.enterprise.services.ib111_epf;

import java.util.LinkedHashMap;
import java.util.Map;

import my.com.kfh.enterprise.services.IBEnterpriseServicesWorkspace;
import my.com.kfh.enterprise.services.common.CommonConstants;
import my.com.kfh.error.exception.IBGenericException;
import my.com.kfh.error.exception.IBServiceException;
import my.com.kfh.error.handler.CommonErrorCode;
import my.com.kfh.error.handler.IBErrorHandler;
import my.com.kfh.web.utils.DataValidator;
import my.com.kfh.web.utils.FormatterUtils;
import my.com.kfh.web.utils.IBDynaBean;
import my.com.kfh.web.utils.IBDynaBeanManager;
import my.com.kfh.web.utils.IBLogger;
import my.com.kfh.web.utils.IBSessionObjects;
import my.com.kfh.web.utils.IBTimer;
import my.com.kfh.web.utils.constant.CurrencyCode;
import my.com.kfh.web.utils.constant.ServiceInfo;

/**
 * <pre>
 * PROGRAMMER: Chang Giat Yeng
 * CHANGE-NO:
 * TASK-NO:
 * DATE CREATED: Dec 10, 2009
 * TAG AS:
 * REASON(S):
 * MODIFICATION:
 * </pre>
 */

/**
 * <Class description>
 */
public class IBEpfConfirmServices extends IBEnterpriseServicesWorkspace
{
	private static Class c_oClass = IBEpfConfirmServices.class;

	/**
	 * @ Class constructor specifying session object.
	 *
	 * @param p_oIBSessionObjects Session object contains all information stored in HTTP session
	 */
	public IBEpfConfirmServices (IBSessionObjects m_oIBSessionObjects)
	{
		super (m_oIBSessionObjects);
	}

	/**
	 * <Method description>
	 *
	 * @throws IBGenericException
	 */
	public void confirmEpfPayment () throws IBGenericException
	{
		String m_sMethodName = "confirmEpfPayment";
		IBTimer m_oTimer = IBLogger.logBSFNMethodEnteringMessage (c_oClass, m_sMethodName);

		IBDynaBean m_oEpfView = null;
		String m_sPaymentType = null;

		IBDynaBean m_oAccountBean = null;
		String m_sFromAccount = null;
		String m_sFromAccountDisplay = null;
		Map m_oFromAccountMap = new LinkedHashMap();

		String m_sAmount = null;
		String m_sAmountDisplay = null;
		String m_sIsoCurrency = null;
		String m_sContribution = null;

		try
		{
			m_oEpfView = (IBDynaBean)getIBDynaBean ().get (EPFConstants.EPF_VIEW);
			if (m_oEpfView == null)
			{
				m_oEpfView = IBDynaBeanManager.createIBDynaBean (EPFConstants.EPF_VIEW, getIBDynaBean ().getActionMapping ());
			}

			m_sPaymentType = (String)m_oEpfView.get (EPFConstants.PAYMENT_TYPE);
			if (EPFConstants.PAYMENT_TYPE_REG.equalsIgnoreCase (m_sPaymentType))
			{
				Map m_oRegEpfAccountListMap = (Map)m_oEpfView.get (EPFConstants.REG_ACCOUN_LIST_MAP);
				String m_sEpfNo = (String)getIBDynaBean ().get (EPFConstants.FAV_EPF_BENE_NAME);
				Map m_sSelectedEpfMap = (Map)m_oRegEpfAccountListMap.get (m_sEpfNo);
				getIBDynaBean ().set (EPFConstants.EPF_BENE_NAME, m_sSelectedEpfMap.get (EPFConstants.EPF_BENE_NAME));
				getIBDynaBean ().set (EPFConstants.EPF_BENE_IC, m_sSelectedEpfMap.get (EPFConstants.EPF_BENE_IC));
				getIBDynaBean ().set (EPFConstants.EPF_NO, m_sEpfNo);
			}

			m_sFromAccount = (String)getIBDynaBean ().get (EPFConstants.FROM_ACCOUNT);
			m_oFromAccountMap = (Map)m_oEpfView.get (EPFConstants.FROM_ACCOUNT_MAP);
			m_oAccountBean = (IBDynaBean)m_oFromAccountMap.get (m_sFromAccount);
			m_sFromAccountDisplay = DataValidator.isEmptyString (m_sFromAccount) ?
					(String)m_oAccountBean.get (CommonConstants.ACCOUNT_NICKNAME) : m_sFromAccount.concat (" ").concat ((String)m_oAccountBean.get (CommonConstants.ACCOUNT_NICKNAME));
			m_oEpfView.set (EPFConstants.FROM_ACCOUNT_DISPLAY, m_sFromAccountDisplay);

			m_sIsoCurrency = (String)m_oAccountBean.get (CommonConstants.ISO_CURRENCY);
			m_sIsoCurrency = DataValidator.isEmptyString (m_sIsoCurrency) ? CurrencyCode.MALAYSIA_RINGGIT : m_sIsoCurrency;

			m_sAmount = (String)getIBDynaBean ().get (EPFConstants.AMOUNT);
			
			m_sContribution = (String)getIBDynaBean ().get (EPFConstants.EPF_CONTRIBUTION_MONTH);
			//check contribution month
			IBEpfDBUtils.checkContributionMonth(m_sContribution);
			
			//checking amount must be minimum rm50
			if(Double.parseDouble (m_sAmount) < 50)
			{
				throw IBErrorHandler.createBusinessException("epf.", "amountLessThan50");
			}
			
			m_sAmountDisplay = DataValidator.isEmptyString (m_sAmount) ? FormatterUtils.formatMoneyDisplay ("0", m_sIsoCurrency, true) : FormatterUtils.formatMoneyDisplay (m_sAmount, m_sIsoCurrency, true);
			
			//Pixakarft Bank Negara Changes 30th Mar 2013 start
			IBDynaBean m_oServiceInfoBean  = (IBDynaBean)m_oEpfView.get (ServiceInfo.SERVICE_INFO_BEAN);
			if (((Boolean) m_oServiceInfoBean.get (ServiceInfo.BEAN_ATT_TAC_REQUIRED)).booleanValue ())
			{
				IBDynaBean m_oSMSViewBean = (IBDynaBean) getIBSessionObjects().getHttpSessionAttrObject(CommonConstants.SMS_VIEW_BEAN);
				m_oSMSViewBean.set(CommonConstants.AMOUNT, m_sAmountDisplay);
				String beneName = (String) getIBDynaBean().get(EPFConstants.EPF_BENE_NAME);
				if(!DataValidator.isEmptyString(beneName)){
					m_oSMSViewBean.set(CommonConstants.BENE_NAME, beneName.length()>16?beneName.substring(0, 16):beneName);
				}
				getIBSessionObjects().setHttpSessionAttrObject(CommonConstants.SMS_VIEW_BEAN, m_oSMSViewBean);
			}
			//Pixakarft Bank Negara Changes 30th Mar 2013 start

					
			m_oEpfView.set (EPFConstants.EPF_CONTRIBUTION_MONTH, FormatterUtils.formatEPFContributionMonth(m_sContribution));
			m_oEpfView.set (EPFConstants.AMOUNT_DISPLAY, m_sAmountDisplay);

			getIBDynaBean ().set (EPFConstants.EPF_VIEW, m_oEpfView);
		}
		catch (Exception e)
		{
			if (e instanceof IBGenericException)
			{
				throw (IBGenericException) e;
			}

			throw IBErrorHandler.createServiceException(IBServiceException.GENERAL_SERVICE_ERROR, true,
					new String[] {CommonErrorCode.GENERIC_CODE, "", "", "", IBErrorHandler.generateErrorMessage(e)}, c_oClass);
		}

		IBLogger.logBSFNMethodLeavingMessage (c_oClass, m_sMethodName, m_oTimer);
	}
}
