/**
 * Copyright (c) 2010 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.ib104_fundtransfer.thirdparty;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

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

import com.broadvision.data.client.Content;
import com.broadvision.data.client.DataManager;
import com.broadvision.data.common.TableData;

import my.com.kfh.enterprise.services.GenericESBServicesImpl;
import my.com.kfh.enterprise.services.IBEnterpriseServices;
import my.com.kfh.enterprise.services.IBEnterpriseServicesWorkspace;
import my.com.kfh.enterprise.services.IBUtils;
import my.com.kfh.enterprise.services.ib101_access.AccessConstants;
import my.com.kfh.enterprise.services.ib101_access.IBAccessDBUtils;
import my.com.kfh.enterprise.services.ib104_fundtransfer.others.IBStdDailyLimitBaseServices;
import my.com.kfh.enterprise.services.ib104_fundtransfer.own.OwnTransferDBUtils;
import my.com.kfh.enterprise.services.ib112_si.IBGenericSIServices;
import my.com.kfh.enterprise.services.ib112_si.SIConstants;
import my.com.kfh.enterprise.services.ib112_si.SIDBUtils;
import my.com.kfh.enterprise.services.ib112_si.SI_PAYMENT_MODE;
import my.com.kfh.error.exception.IBBusinessException;
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.esb.response.ESBAccountEnquiryTC100Bean;
import my.com.kfh.esb.response.ESBCompanyProfileBean;
import my.com.kfh.esb.response.ESBThirdFundTransferBean;
import my.com.kfh.esb.response.ESBTransferGLtoCASABean;
import my.com.kfh.esb.response.ESBTransferToGLBean;
import my.com.kfh.sms.services.SMSServices;
import my.com.kfh.web.utils.BVI_DateTime;
import my.com.kfh.web.utils.DataValidator;
import my.com.kfh.web.utils.DateTimeUtils;
import my.com.kfh.web.utils.FormatterUtils;
import my.com.kfh.web.utils.GSTUtils;
import my.com.kfh.web.utils.IBLoadProperty;
import my.com.kfh.web.utils.IBLogWriter;
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.ResourcesUtils;
import my.com.kfh.web.utils.constant.CurrencyCode;
import my.com.kfh.web.utils.constant.ESBConstants;
import my.com.kfh.web.utils.constant.LogTables;
import my.com.kfh.web.utils.constant.ServiceInfo;
import static org.mockito.Mockito.*;

import com.broadvision.data.client.Content;
import com.broadvision.data.client.DataManager;
import com.broadvision.data.common.TableData;

/**
 * <pre>
 * PROGRAMMER: Ang Wei Cheng
 * CHANGE-NO:
 * TASK-NO:
 * DATE CREATED: Apr 5, 2010
 * TAG AS:
 * REASON(S):
 * MODIFICATION:
 * </pre>
 */

/**
 * <Class description>
 */
public class IB3rdPartySITransferResultsServices
{
	private static Class c_oClass = IB3rdPartySITransferResultsServices.class;

	public static void executeTransaction(String siId) throws IBGenericException
	{
        String m_sMethodName = "executeTransaction";
        IBTimer m_oTimer = IBLogger.logBSFNMethodEnteringMessage (c_oClass, m_sMethodName, String.class.getName ());

		String m_sSICharges = "0";
		String m_sSIChargesRefNo = null;
		boolean m_boSIChargeStatus = false;
		String m_sSIGSTCharges = "0";
		String m_sSIGSTChargesRefNo = null;
		boolean m_boSIGSTChargeStatus = false;
		String m_sSIChargesRevertRefNo = null;
		boolean m_boSIChargeRevertStatus = false;
		double m_dAvailableBal = 0.0;
		double m_dTransferAmount = 0.0;
		double m_dTotalAmount = 0.0;
		double m_dSICharges = 0.0;
		String m_sSIChargeResponseCode = null;
		boolean m_boIsFundSufficient = true;

		String m_sStatus = ResourcesUtils.getUnsuccessful ();
		String m_sReason=null;
		String m_sResponseCode=null;
		String m_sHostRefNo=null;
		boolean hasReceipt = false;
		String m_sEsbDescription = null;

		Float m_fUserLimit = null;
		String m_sEffectiveDate = null;
		Content m_oSIServiceInfoContent = null;
		String m_sSIServiceMaxLimit = null;
		boolean m_boSIIndicator = true;
		boolean m_boIsScheduler = true;
		
		long m_sUserId = 0;
		Float specialDailyLimit= null;
		Float userDailyLimit= null;
		String residencyType = "";
		String otherPaymentDetails = "";
		
		String SIGSTCharges = "0";
		String m_sFromAccNewBal = "";
		String m_sToAccNewBal = "";
		String m_sFromAccIsoCurr = CurrencyCode.MALAYSIA_RINGGIT;
		String m_sToAccIsoCurr = CurrencyCode.MALAYSIA_RINGGIT;
		GenericESBServicesImpl m_oEsbServices = null;
		
		try
		{
			BVI_DateTime m_oCurrentBVIDateTime = DateTimeUtils.getCurrentIBDateTimeObject ();

			IBSessionObjects m_oSessionObj = mock(IBSessionObjects.class);
			when(m_oSessionObj.getDataManager()).thenReturn(new DataManager());
			when(m_oSessionObj.getRemoteIPAddress ()).thenReturn("127.0.0.1");

			TableData table = SIDBUtils.getSICurrentTransId(new IBEnterpriseServicesWorkspace(m_oSessionObj), siId);
			String currentTrnxId  = table.getStringValue(0, LogTables.IB_SI.CURRENT_TRNX_ID.toString ());
			String siPaymentModeCode = table.getStringValue(0, LogTables.IB_SI.PAYMENT_MODE.toString ());
			String trnxTypeDisplay = table.getStringValue(0, LogTables.IB_SI.TRNX_TYPE.toString ());
			String m_sSIServiceCode = table.getStringValue (0, LogTables.IB_SI.SERVICE_CODE.toString ());

			m_sEsbDescription = ESBConstants.FUND_TRANSFER_3RD;
			m_sEsbDescription = IBGenericSIServices.getESBDescription (m_sEsbDescription, siPaymentModeCode);

			IBEnterpriseServices p_oEnterpriseServices = new IBEnterpriseServicesWorkspace(m_oSessionObj);

			//get user id from trnx
			table = OwnTransferDBUtils.getTransactionByTrnxId(p_oEnterpriseServices, currentTrnxId);
			String userId = table.getStringValue(0, LogTables.IB_INTRA_TRANSFER.USER_ID.toString ());
			
			m_sUserId = Long.parseLong(userId);
			userDailyLimit = IBUtils.getUserDailyLimit(p_oEnterpriseServices, m_sUserId);
			residencyType = IBUtils.getResidencyType(p_oEnterpriseServices, m_sUserId); 
        	specialDailyLimit = IBUtils.getSpecialDailyLimit(p_oEnterpriseServices, m_sUserId);
        	specialDailyLimit = IBStdDailyLimitBaseServices. getSystemDailyLimit(p_oEnterpriseServices, null, null, residencyType , specialDailyLimit);
        	
			com.broadvision.visitor.client.Visitor visitor = mock(com.broadvision.visitor.client.Visitor.class);
			when(visitor.getId()).thenReturn(Long.parseLong(userId));
			when(m_oSessionObj.getVisitor()).thenReturn(visitor);

			//get various data from transaction
			String m_sFromAccount = table.getStringValue(0, LogTables.IB_INTRA_TRANSFER.FROM_ACCOUNT_NO.toString ());
			String m_sToAccount = table.getStringValue(0, LogTables.IB_INTRA_TRANSFER.TO_ACCOUNT_NO.toString ());
			String m_sAmount = table.getStringValue(0, LogTables.IB_INTRA_TRANSFER.AMOUNT.toString ());
			String fromCurrency = table.getStringValue(0, LogTables.IB_INTRA_TRANSFER.FROM_CURRENCY.toString ());
			String toCurrency = table.getStringValue(0, LogTables.IB_INTRA_TRANSFER.TO_CURRENCY.toString ());
			String m_sEmail = table.getStringValue(0, LogTables.IB_INTRA_TRANSFER.EMAIL.toString ());
			String charges = "0";
			String m_sLocalAmount = table.getStringValue(0, LogTables.IB_INTRA_TRANSFER.LOCAL_AMOUNT.toString ());
			String m_sUserTransferRef = table.getStringValue(0, LogTables.IB_INTRA_TRANSFER.DESCRIPTION.toString ());
			String beneName = table.getStringValue(0, LogTables.IB_INTRA_TRANSFER.ACCOUNT_HOLDER_NAME.toString ());
			String senderName = table.getStringValue(0, LogTables.IB_INTRA_TRANSFER.SENDER_NAME.toString());
			otherPaymentDetails  = table.getStringValue(0, LogTables.IB_INTRA_TRANSFER.OTHER_PAYMENT_DETAILS.toString());
			
			//get user rim no
			String rimNo = IBAccessDBUtils.getRimNoByUserId(p_oEnterpriseServices, userId);

			when(m_oSessionObj.getHttpSessionAttrObject (AccessConstants.RIM_NO)).thenReturn(rimNo);

			//get new trnx id
			String m_sTransID = IBUtils.getTransactionId (m_oSessionObj.getDataManager ());

			/*
             * check SI limit against amount per transaction
             */
			m_sEffectiveDate = DateTimeUtils.formatIBDateTimeToYMD (m_oCurrentBVIDateTime);

            Map m_oSIBankLimitReturnMap = IB3rdPartyTransferBaseServices.checkIntraTransferDailyLimit (p_oEnterpriseServices,
            		null, null, m_sLocalAmount, m_sEffectiveDate, m_boSIIndicator, m_boIsScheduler, residencyType);

            m_fUserLimit = IBFundTransferDBUtils.getUserTransferLimit (m_oSessionObj, m_sSIServiceCode, m_boIsScheduler);

            m_oSIServiceInfoContent = IBGenericSIServices.getServiceInfoContent (m_sSIServiceCode);
            m_sSIServiceMaxLimit = (String) m_oSIServiceInfoContent.getStringValue (ServiceInfo.TABLE_ATT_MAX_LIMIT);

            Map m_oSIUserLimitReturnMap = IB3rdPartyTransferBaseServices.checkIntraTransferDailyLimitByServiceCode (p_oEnterpriseServices,
            		m_sLocalAmount, m_sSIServiceMaxLimit, m_fUserLimit, m_sSIServiceCode, m_sEffectiveDate, m_boSIIndicator, m_boIsScheduler , residencyType , 
            		m_oSIBankLimitReturnMap);
            
            /*
             * check daily limit
             */
            
            Map m_oStdBankingLimitReturnMap = IBStdDailyLimitBaseServices.checkBankDailyLimit(p_oEnterpriseServices, null, 
            		null , m_sAmount , specialDailyLimit, m_sEffectiveDate, m_boSIIndicator, m_boIsScheduler , residencyType);
            
            Map m_oStdUserLimitReturnMap = IBStdDailyLimitBaseServices.checkUserDailyLimit(p_oEnterpriseServices, null, 
            		null, m_sAmount , userDailyLimit, m_sEffectiveDate, m_boSIIndicator, m_boIsScheduler);
            
            if (!((Boolean) m_oSIUserLimitReturnMap.get (ThirdPartyTransferTags.STATUS)).booleanValue ())
            {
            	m_sStatus = ResourcesUtils.getUnsuccessful ();
            	m_sReason = ResourcesUtils.getBusinessExceptionErrorMessage (IBBusinessException.SI, SIConstants.ERR_USER_MAX_LIMIT,
						new String [] { FormatterUtils.formatMoneyDisplay ( (String)m_oSIUserLimitReturnMap.get (SIConstants.MAX_LIMIT),
								null, true) });
            }
            else if (!((Boolean) m_oSIBankLimitReturnMap.get (ThirdPartyTransferTags.STATUS)).booleanValue ()&&(0 <= specialDailyLimit.floatValue()))
            {
            	m_sStatus = ResourcesUtils.getUnsuccessful ();
            	m_sReason = ResourcesUtils.getBusinessExceptionErrorMessage (IBBusinessException.SI, SIConstants.ERR_BANK_MAX_LIMIT,
						new String [] { FormatterUtils.formatMoneyDisplay ( (String)m_oSIBankLimitReturnMap.get (SIConstants.MAX_LIMIT),
								null, true) });
            }
            else if (!((Boolean) m_oStdUserLimitReturnMap.get (ThirdPartyTransferTags.STATUS)).booleanValue ())
            {

            	m_sStatus = ResourcesUtils.getUnsuccessful ();
            	m_sReason = ResourcesUtils.getApplicationResourceMessage (ThirdPartyTransferTags.ERR_SI_USER_STD_LIMIT,
	                		new String [] { FormatterUtils.formatMoneyDisplay (String.valueOf(specialDailyLimit) ,
	                				CurrencyCode.MALAYSIA_RINGGIT, true) });
            	
            }
            else if ((!((Boolean) m_oStdBankingLimitReturnMap.get (ThirdPartyTransferTags.STATUS)).booleanValue ()))
            {

            	m_sStatus = ResourcesUtils.getUnsuccessful ();
            	m_sReason = ResourcesUtils.getApplicationResourceMessage (ThirdPartyTransferTags.ERR_SI_BANK_STD_LIMIT,
                		new String [] { FormatterUtils.formatMoneyDisplay ((String) m_oStdBankingLimitReturnMap.get (ThirdPartyTransferTags.MAX_LIMIT),
                				CurrencyCode.MALAYSIA_RINGGIT, true) });	
            }
            else
            {
				m_oEsbServices = new GenericESBServicesImpl (m_oSessionObj);

				ESBAccountEnquiryTC100Bean m_oTCEnquiryBean = m_oEsbServices.getAccountDetailsTC100 (m_sFromAccount);
				if (!DataValidator.isEmptyString (m_oTCEnquiryBean.getAvailable_Bal ()))
				{
					m_dAvailableBal = Double.parseDouble (m_oTCEnquiryBean.getAvailable_Bal ());
				}

				if (SI_PAYMENT_MODE.RECURRING_PAYMENT.getCode ().equals (siPaymentModeCode))
				{
					m_dSICharges = IBGenericSIServices.getSICharges (ServiceInfo.SI_FEE_INTRA);
	    			m_sSICharges = String.valueOf (m_dSICharges);

					if (m_dSICharges > 0)
					{
						if (m_dAvailableBal > m_dSICharges)
						{
							String m_sChargeGLAccount = IBGenericSIServices.getSICollectionAccountNo (m_sFromAccount);

							ESBTransferToGLBean m_oESBTransferToGLBean = m_oEsbServices.chargesTransfer (m_sFromAccount,
									m_sChargeGLAccount, m_sSICharges, ESBConstants.SI_CHARGES, null, senderName, beneName, m_sUserTransferRef, otherPaymentDetails);

							m_sSIChargeResponseCode = m_oESBTransferToGLBean.getResponseCode ();
							if (ESBConstants.IB_SUCCESS_RC.equalsIgnoreCase (m_sSIChargeResponseCode))
							{
								m_boSIChargeStatus = true;
								m_sSIChargesRefNo = m_oESBTransferToGLBean.getReferenceNo ();
								
								String SIGSTChargesRate = IBUtils.getGSTRate(new IBEnterpriseServicesWorkspace(m_oSessionObj), IBLoadProperty.getValue(GSTUtils.SI_CHARGES_CODE));
	    						SIGSTCharges = GSTUtils.gstAmount2Dec(m_sSICharges, SIGSTChargesRate);
	    						
	    						ESBTransferToGLBean m_oGSTESBTransferToGLBean = m_oEsbServices.chargesTransfer (m_sFromAccount,
	    								ResourcesUtils.getGLAccountResource ("gl.account.gst"), SIGSTCharges, ESBConstants.SI_GST_CHARGES, null, senderName, beneName, m_sUserTransferRef, otherPaymentDetails);
	    						
	    						String m_sSIGSTChargeResponseCode = m_oGSTESBTransferToGLBean.getResponseCode ();
		    					if (ESBConstants.IB_SUCCESS_RC.equalsIgnoreCase (m_sSIGSTChargeResponseCode)){
		    						m_boSIGSTChargeStatus = true;
		    						m_sSIGSTChargesRefNo = m_oGSTESBTransferToGLBean.getReferenceNo ();
		    					}else{
		    						m_boIsFundSufficient = false;
									hasReceipt = false;
									m_sStatus = ResourcesUtils.getUnsuccessful ();
									m_sReason = m_oESBTransferToGLBean.getErrorMessage ();
		    						
		    						ESBTransferGLtoCASABean m_oESBTransferGLtoCASABean = m_oEsbServices.transferGLtoCASA(m_sChargeGLAccount, m_sFromAccount, m_sSICharges, ESBConstants.SI_CHARGES_REVERT, null, beneName, otherPaymentDetails);
		    						
		    						String m_sSIGSTRevertChargeResponseCode = m_oESBTransferGLtoCASABean.getResponseCode ();
		    						if (ESBConstants.IB_SUCCESS_RC.equalsIgnoreCase (m_sSIGSTRevertChargeResponseCode)){
		    							m_boSIChargeRevertStatus = true;
		    							m_sSIChargesRevertRefNo = m_oESBTransferGLtoCASABean.getReferenceNo();
		    						}
		    					}
							}
							else
							{
								m_boIsFundSufficient = false;
								hasReceipt = false;
								m_sStatus = ResourcesUtils.getUnsuccessful ();
								m_sReason = m_oESBTransferToGLBean.getErrorMessage ();
							}
						}
						else
						{
							m_boIsFundSufficient = false;
							hasReceipt = false;
							m_sStatus = ResourcesUtils.getUnsuccessful ();
							m_sReason = ResourcesUtils.getApplicationResourceMessage (SIConstants.RESOURCES_INSUFFICIENT_FUND);
						}
					}
				}

				// Makesure m_boIsFundSufficient is NEVER set to false previously. Check is account balance sufficient to do transfer?
				if (m_boIsFundSufficient)
				{
					m_dTransferAmount = Double.parseDouble (m_sAmount);
					m_dTotalAmount = m_dTransferAmount + m_dSICharges + Double.parseDouble(SIGSTCharges);

					if(m_dAvailableBal < m_dTotalAmount)
					{
						m_boIsFundSufficient = false;
						hasReceipt = false;
						m_sStatus = ResourcesUtils.getUnsuccessful ();
						m_sReason = ResourcesUtils.getApplicationResourceMessage (SIConstants.RESOURCES_INSUFFICIENT_FUND);
					}
				}

				if (m_boIsFundSufficient)
				{
					try
		        	{
						ESBCompanyProfileBean toAccProfile = m_oEsbServices.getProfileByAccRim(m_sToAccount, null);
	            		String toAccRimo = toAccProfile.getRimNo();
	            		
						ESBThirdFundTransferBean m_oReturnedESBBean = m_oEsbServices.intrabankFundTransfer (
								m_sEsbDescription, m_sFromAccount, m_sToAccount, m_sUserTransferRef, otherPaymentDetails, m_sAmount, null, senderName, beneName, toAccRimo);

		        		m_sResponseCode = m_oReturnedESBBean.getResponseCode ().trim ();
		        		m_sFromAccNewBal = m_oReturnedESBBean.getFromAccountBalance ();
						m_sToAccNewBal = m_oReturnedESBBean.getToAccountBalance ();
		        		
		                if (ESBConstants.IB_SUCCESS_RC.equalsIgnoreCase (m_sResponseCode))
		    			{
		                	hasReceipt = true;
		                	m_sStatus = ResourcesUtils.getSuccessful ();
		                	if (!DataValidator.isEmptyString (m_oReturnedESBBean.getReferenceNo ()))
		                	{
		                		m_sHostRefNo = m_oReturnedESBBean.getReferenceNo ().trim ();
		                	}
		    			}
		                else
		                {
		                	m_sStatus = ResourcesUtils.getUnsuccessful ();
		                	m_sReason = m_oReturnedESBBean.getErrorMessage ();
		                	if (!DataValidator.isEmptyString (m_oReturnedESBBean.getReferenceNo ()))
		                	{
		                		m_sHostRefNo = m_oReturnedESBBean.getReferenceNo ().trim ();
		                	}

						}
		        	}
					catch (IBBusinessException esbBusinessException)
					{
						m_sStatus = ResourcesUtils.getUnsuccessful ();
						m_sReason = esbBusinessException.getErrorMessage ();
					}
				}
            }

			if(ESBConstants.IB_SUCCESS_RC.equalsIgnoreCase (m_sResponseCode))
			{
				String m_sAccountHolderName = new GenericESBServicesImpl (m_oSessionObj).getAccountHolderName(m_sFromAccount);
				
				if (!DataValidator.isEmptyString (m_sEmail))
                {
    				String m_sSubject = ResourcesUtils.getApplicationResourceMessage ("ib104.thirdparty.email.subject");
                	String m_sCustomerName = IBAccessDBUtils.getLastNameByUserId(p_oEnterpriseServices, userId);

                	

                	StringBuffer m_sbEmailMsg = new StringBuffer ();
                	m_sbEmailMsg.append (ResourcesUtils.getApplicationResourceMessage ("ib104.thirdparty.email.greetCustomer", new String [] {m_sSubject, beneName} ));
                	m_sbEmailMsg.append (ResourcesUtils.getApplicationResourceMessage ("ib104.thirdparty.email.note1",
                			new String [] {m_sCustomerName,
                				DateTimeUtils.formatDayMonthYear (m_oCurrentBVIDateTime).concat (" ").concat (m_oCurrentBVIDateTime.getTime()) } ));

                	IBLogWriter m_oTableCreater = new IBLogWriter();
                	m_sbEmailMsg.append (m_oTableCreater.RowCreater ("S", ResourcesUtils.getApplicationResourceMessage ("general.status"), m_sStatus, null, null)).append("\n");
                	m_sbEmailMsg.append (m_oTableCreater.RowCreater ("C", ResourcesUtils.getApplicationResourceMessage ("general.amount"), FormatterUtils.formatMoneyDisplay(m_sAmount, null, true), null, null)).append("\n");
                	m_sbEmailMsg.append (m_oTableCreater.RowCreater ("C", ResourcesUtils.getApplicationResourceMessage ("general.reference.number"), FormatterUtils.formatTransID(m_sTransID), null, null)).append("\n");
                	if(!DataValidator.isEmptyString (m_sHostRefNo))
    				{
                		m_sbEmailMsg.append (m_oTableCreater.RowCreater ("C", ResourcesUtils.getApplicationResourceMessage ("general.reference"), m_sHostRefNo, null, null)).append("\n");
    				}
                	m_sbEmailMsg.append (m_oTableCreater.RowCreater ("E", ResourcesUtils.getApplicationResourceMessage ("ib104.transferReference"),
                			DataValidator.isEmptyString (m_sUserTransferRef) ? "-" : m_sUserTransferRef, null, null)).append("\n");
                	m_sbEmailMsg.append (m_oTableCreater.RowCreater ("E", ResourcesUtils.getApplicationResourceMessage ("ib104.other.payment.details"),
                			DataValidator.isEmptyString (otherPaymentDetails) ? "-" : otherPaymentDetails, null, null)).append("\n");
                	
                	m_sbEmailMsg.append (ResourcesUtils.getApplicationResourceMessage ("ib104.thirdparty.email.note2"));
                	m_sbEmailMsg.append (ResourcesUtils.getApplicationResourceMessage ("ib104.thirdparty.email.disclaimer"));

                	IBUtils.sendEmail (p_oEnterpriseServices, null, m_sEmail, m_sSubject, m_sbEmailMsg.toString (), m_sStatus, null);
                }

				//receipt
				Map<String, String> m_oReceiptDetails = new LinkedHashMap <String, String> ();
				m_oReceiptDetails.put (ResourcesUtils.getApplicationResourceMessage ("receipt.transaction.type"), trnxTypeDisplay);
				m_oReceiptDetails.put (ResourcesUtils.getApplicationResourceMessage ("general.status"), m_sStatus);
	                m_oReceiptDetails.put (ResourcesUtils.getApplicationResourceMessage ("ib104.sender.name"), senderName);
	                m_oReceiptDetails.put (ResourcesUtils.getApplicationResourceMessage ("general.beneficiary.name"), m_sAccountHolderName);
				m_oReceiptDetails.put (ResourcesUtils.getApplicationResourceMessage ("general.toAccount"), m_sToAccount);
				m_oReceiptDetails.put (ResourcesUtils.getApplicationResourceMessage ("general.amount"),
						FormatterUtils.formatMoneyDisplay (m_sAmount, toCurrency, true));

	                if (!DataValidator.isEmptyString (m_sUserTransferRef))
	                {
	                	m_oReceiptDetails.put (ResourcesUtils.getApplicationResourceMessage ("ib104.transferReference"), m_sUserTransferRef);
	                }
	                if (!DataValidator.isEmptyString (otherPaymentDetails))
	                {
	                	m_oReceiptDetails.put (ResourcesUtils.getApplicationResourceMessage ("ib104.other.payment.details"), otherPaymentDetails);
	                }
	                if (!DataValidator.isEmptyString (m_sEmail))
	                {
	                	m_oReceiptDetails.put (ResourcesUtils.getApplicationResourceMessage ("ib104.beneficiary.email"), m_sEmail);
	                }
				m_oReceiptDetails.put (ResourcesUtils.getApplicationResourceMessage ("general.reference.number"), FormatterUtils.formatTransID (m_sTransID));
				if(!DataValidator.isEmptyString (m_sHostRefNo))
    			{
					m_oReceiptDetails.put (ResourcesUtils.getApplicationResourceMessage ("general.reference"), m_sHostRefNo);
    			}
				m_oReceiptDetails.put (ResourcesUtils.getApplicationResourceMessage ("general.transaction.date"), DateTimeUtils.formatDayMonthYear (m_oCurrentBVIDateTime));
				m_oReceiptDetails.put (ResourcesUtils.getApplicationResourceMessage ("general.transaction.time"), m_oCurrentBVIDateTime.getTime ());

				//insert into table IB_RECEIPT_DETAILS
				IBUtils.loopIntoReceiptDetails (p_oEnterpriseServices, m_oReceiptDetails, m_oCurrentBVIDateTime.getSimpleDate (), m_sTransID);
			}

			//log into si history
			IBLogger.logSIHistory(p_oEnterpriseServices, rimNo, siId, m_sTransID, m_oCurrentBVIDateTime.getJavaDate (),
					SIConstants.TRANSCODE_INTRA_TRANSFER,
					m_sFromAccount, m_sToAccount, fromCurrency, toCurrency,
					m_sAmount, "0", charges,
					m_sSICharges, // SICharges
					m_sStatus,
					m_sReason,
					m_sHostRefNo,
					"", // PhxRevRefNo
					false, // PhxRevStatus
					"", // EpayRefNo
					"", // PhxChargeRefNo
					false, // PhxChargeStatus
					"", // PhxChargeRevRefNo
					false, // PhxCharRevStatus
					m_sSIChargesRefNo, // SIChargeRefNo
					m_boSIChargeStatus,
					m_sSIChargesRevertRefNo, // SIChargeRevRefNo
					m_boSIChargeRevertStatus, // SIChargeRevStatus
					m_sSIServiceCode,
					hasReceipt,
					m_sSIGSTCharges, // SI GST Charges
					"", // Service GST Charges
					"", // SIGSTChargesRevertRefNo
					false); // SIGSTChargeRevertStatus

			//SMS Notification START
			SMSServices.sendCustomerSMSForSI (
					p_oEnterpriseServices, 
					userId, 
					m_sSIServiceCode, 
					SIConstants.TRANSCODE_INTRA_TRANSFER, 
					m_sStatus, 
					m_sTransID, 
					m_sFromAccount, 
					m_sAmount, 
					m_oCurrentBVIDateTime,
					FormatterUtils.formatMoneyDisplay (m_sFromAccNewBal, m_sFromAccIsoCurr, true),
					beneName.length()>15?beneName.substring(0, 15):beneName);
			
			ESBCompanyProfileBean residencyByRim = m_oEsbServices.getProfileByAccRim(m_sToAccount, null);
			String m_sBeneMobileNumber 	= residencyByRim.getPhone3() == null ? "" : residencyByRim.getPhone3().trim();
			
			SMSServices.sendBeneficiarySMSForSI(
					p_oEnterpriseServices, 
					userId, 
					"", 
					SIConstants.TRANSCODE_INTRA_TRANSFER, 
					m_sStatus, 
					m_sTransID, 
					m_sToAccount, 
					m_sBeneMobileNumber,
					m_sAmount, 
					m_oCurrentBVIDateTime,
					FormatterUtils.formatMoneyDisplay (m_sToAccNewBal, m_sToAccIsoCurr, true));
			//SMS Notification END
			
			
		}
		catch(Exception e)
		{
			e.printStackTrace();
        	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);
	}
}
