<?php
/**
 * added by G J Jidni
 *
 */
class Cart_Setup_model extends CI_Model
{

	public function __construct()
	{
		parent::__construct();
		$this->load->config('ftp_config');
	}

	function get_merchant_id ($merchant_email)
	{
		$this->db->select('*');
		$this->db->from('tbl_merchant');
		$this->db->where('email_address', $merchant_email);
		$resultSet = $this->db->get();
		return $resultSet->row();
	}

	function update_cart_setup ($data)
	{
		$this->db->select('count(merchant_row_id) as CNTMER');
		$this->db->from('tbl_cart_basic');
		$this->db->where('merchant_row_id', $data['merchant_row_id']);
		$resultSet = $this->db->get();
		$merchant_count = $resultSet->row();

		if (($merchant_count->CNTMER) > 0)
		{
			$this->db->where('merchant_row_id', $data['merchant_row_id']);
			$this->db->update('tbl_cart_basic', $data);
		}
        else
		{
			$this->db->insert('tbl_cart_basic', $data);
		}
	}

	function update_cart_customization ($data)
	{
		$this->db->select('count(merchant_row_id) as CNTMER');
		$this->db->from('tbl_cart_customized');
		$this->db->where('merchant_row_id', $data['merchant_row_id']);
		$resultSet = $this->db->get();
		$merchant_count = $resultSet->row();
		if (($merchant_count->CNTMER) > 0)
		{
			$this->db->set('templateTopHTML', $data['templateTopHTML']);
			$this->db->set('templateBottomHTML', $data['templateBottomHTML']);
			$this->db->set('ncTopHTML', $data['ncTopHTML']);
			$this->db->set('ncBottomHTML', $data['ncBottomHTML']);
			$this->db->set('OrderTopHtml', $data['OrderTopHtml']);
			$this->db->set('cartCheckoutBottomHtml', $data['cartCheckoutBottomHtml']);
            //Anjuman | D: 04052012 (mmddyyyy) -- right side content of the order checkout page
            $this->db->set('cartCheckoutRightHtml', $data['cartCheckoutRightHtml']);
			$this->db->set('GeneralInfo', $data['GeneralInfo']);
			$this->db->set('upTopHTML', $data['upTopHTML']);
			$this->db->set('upBottomHTML', $data['upBottomHTML']);
			$this->db->set('create_date', $data['create_date']);
			$this->db->where('merchant_row_id', $data['merchant_row_id']);
			$this->db->update('tbl_cart_customized');
		} else
		{
			$this->db->insert('tbl_cart_customized', $data);
		}
	}

	function get_basic_setup ($merchant_row_id, $fields = '*')
	{
		$this->db->select($fields);
		$this->db->from('tbl_cart_basic');
		$this->db->where('merchant_row_id', $merchant_row_id);
		$resultSet = $this->db->get();
		return $resultSet->row();
	}

	function get_customized_setup ($merchant_row_id)
	{
		$this->db->select('*');
		$this->db->from('tbl_cart_customized');
		$this->db->where('merchant_row_id', $merchant_row_id);
		$resultSet = $this->db->get();
		return $resultSet->row_array();
	}

	function getCatalogURL ($merchant_row_id)
	{
		$this->db->select('catalog_url');
		$this->db->from('tbl_cart_basic');
		$this->db->where('merchant_row_id', $merchant_row_id);
		$resultSet = $this->db->get();
		return $resultSet->row();
	}

	/**
	 * modified by G J Jidni
     * Modified by Mohammad Sajjad Hossain, added encryption
	 *
	 * @param array $gatewaySetupData
	 */
	function saveGatewaySetup ($gatewaySetupData)
	{
        /*if(trim($gatewaySetupData['paypal_api_user_name']))
        {
            $gatewaySetupData['paypal_api_user_name'] = encrypt($gatewaySetupData['paypal_api_user_name']);
        }

        if(trim($gatewaySetupData['paypal_api_password']))
        {
            $gatewaySetupData['paypal_api_password'] = encrypt($gatewaySetupData['paypal_api_password']);
        }

        if(trim($gatewaySetupData['paypal_api_signature']))
        {
            $gatewaySetupData['paypal_api_signature'] = encrypt($gatewaySetupData['paypal_api_signature']);
        }
        */
        $this->db->where('merchant_row_id', $gatewaySetupData['merchant_row_id']);
		$this->db->update('tbl_gateway_setup', $gatewaySetupData);
	}

    function getBasicCurrency($merchantRowId)
    {
        $sql = "SELECT currency
                FROM tbl_cart_basic
                WHERE merchant_row_id = $merchantRowId";
        $query = $this->db->query($sql);
        $row = $query->row();
        return $row->currency;
    }

    /**
    * Modified by Mohammad Sajjad Hossain, added decryption
    */
	function getGatewaySetup($merchant_row_id)
	{
         $sql = 'SELECT  gs.merchant_row_id,
                        gs.credit_cards_selected,
                        gs.payment_process_model,
                        gs.real_time_gateway,
                        gs.real_time_gateway_mobile,
                        gs.real_gateway_account_no,
                        gs.standard_gateway_paypal,
                        gs.standard_gateway_alertpay,
                        gs.standard_gateway_rbsworldpay,
                        gs.enable_realtime_rotator,
                        sg1.authorize_email AS paypal_user_email,
                        sg1.authorize_api_login_key AS paypal_api_user_name,
                        sg1.authorize_api_transaction_key AS paypal_api_password,
                        sg1.authorize_api_signature AS paypal_api_signature,
                        sg2.authorize_email AS alertpay_merchant_email,
                        sg2.authorize_api_signature AS alertpay_ipn_security_code,
                        sg3.authorize_api_login_key AS rbsworldpay_installation_id,
                        sg3.authorize_api_transaction_key AS rbsworldpay_response_password,
                        sg3.authorize_api_signature AS rbsworldpay_md5_password,
                        rg.authorize_api_login_key,
                        rg.authorize_api_transaction_key,
                        rg.authorize_api_signature,
                        rg.real_time_currency,
                        rg.transactions_per_cycle,
                        rg.cvv2_required
                FROM tbl_gateway_setup gs
                LEFT OUTER JOIN tbl_gateway_standard sg1 ON sg1.standard_gateway = gs.standard_gateway_paypal
                AND sg1.merchant_row_id = gs.merchant_row_id
                LEFT OUTER JOIN tbl_gateway_standard sg2 ON sg2.standard_gateway = gs.standard_gateway_alertpay
                AND sg2.merchant_row_id = gs.merchant_row_id
                LEFT OUTER JOIN tbl_gateway_standard sg3 ON sg3.standard_gateway = gs.standard_gateway_rbsworldpay
                AND sg3.merchant_row_id = gs.merchant_row_id
                LEFT OUTER JOIN tbl_gateway_real_time rg ON rg.merchant_row_id = gs.merchant_row_id
                AND rg.real_time_gateway = gs.real_time_gateway AND rg.gateway_account_no = gs.real_gateway_account_no
                WHERE gs.merchant_row_id = '. $merchant_row_id;

		$resultSet = $this->db->query($sql);
		$row = $resultSet->row();

        if(is_object($row))
        {
            if(trim($row->paypal_api_user_name))
            {
                $row->paypal_api_user_name = decrypt($row->paypal_api_user_name);
            }
            if(trim($row->paypal_api_password))
            {
                $row->paypal_api_password = decrypt($row->paypal_api_password);
            }
            if(trim($row->paypal_api_signature))
            {
                $row->paypal_api_signature = decrypt($row->paypal_api_signature);
            }
            if(trim($row->authorize_api_login_key))
            {
                $row->authorize_api_login_key = decrypt($row->authorize_api_login_key);
            }
            if(trim($row->authorize_api_transaction_key))
            {
                $row->authorize_api_transaction_key = decrypt($row->authorize_api_transaction_key);
            }
            if(trim($row->authorize_api_signature))
            {
                $row->authorize_api_signature = decrypt($row->authorize_api_signature);
            }
            if(trim($row->alertpay_ipn_security_code))
            {
                $row->alertpay_ipn_security_code = decrypt($row->alertpay_ipn_security_code);
            }
            if(trim($row->rbsworldpay_installation_id))
            {
                $row->rbsworldpay_installation_id = decrypt($row->rbsworldpay_installation_id);
            }
            if(trim($row->rbsworldpay_response_password))
            {
                $row->rbsworldpay_response_password = decrypt($row->rbsworldpay_response_password);
            }
            if(trim($row->rbsworldpay_md5_password))
            {
                $row->rbsworldpay_md5_password = decrypt($row->rbsworldpay_md5_password);
            }
            if(trim($row->klickandpay_installation_id))
            {
                $row->klickandpay_installation_id = decrypt($row->klickandpay_installation_id);
            }

            /*if(trim($row->google_merchant_id))
            {
                $row->google_merchant_id = decrypt($row->google_merchant_id);
            }
            if(trim($row->google_merchant_key))
            {
                $row->google_merchant_key = decrypt($row->google_merchant_key);
            }*/
        }

        return $row;
	}

    /**
    * Added by Alauddin Husain
    */
    function getSwipeGatewaySetup($merchant_row_id)
    {
        $sql = 'SELECT  gs.merchant_row_id,
                        gs.credit_cards_selected,
                        gs.payment_process_model,
                        gs.real_time_gateway_mobile AS real_time_gateway,
                        gs.real_gateway_account_no,
                        gs.standard_gateway_paypal,
                        gs.standard_gateway_alertpay,
                        gs.standard_gateway_rbsworldpay,
                        gs.enable_realtime_rotator,
                        rg.authorize_api_login_key,
                        rg.authorize_api_transaction_key,
                        rg.authorize_api_signature,
                        rg.real_time_currency,
                        rg.transactions_per_cycle,
                        rg.cvv2_required
                FROM tbl_gateway_setup gs
                LEFT OUTER JOIN tbl_gateway_real_time rg ON rg.merchant_row_id = gs.merchant_row_id
                AND rg.real_time_gateway = gs.real_time_gateway_mobile AND rg.gateway_account_no = gs.real_gateway_account_no
                WHERE gs.merchant_row_id = '. $merchant_row_id;

        $resultSet = $this->db->query($sql);
        $row = $resultSet->row();

        if(is_object($row))
        {
            if(trim($row->paypal_api_user_name))
            {
                $row->paypal_api_user_name = decrypt($row->paypal_api_user_name);
            }
            if(trim($row->paypal_api_password))
            {
                $row->paypal_api_password = decrypt($row->paypal_api_password);
            }
            if(trim($row->paypal_api_signature))
            {
                $row->paypal_api_signature = decrypt($row->paypal_api_signature);
            }
            if(trim($row->authorize_api_login_key))
            {
                $row->authorize_api_login_key = decrypt($row->authorize_api_login_key);
            }
            if(trim($row->authorize_api_transaction_key))
            {
                $row->authorize_api_transaction_key = decrypt($row->authorize_api_transaction_key);
            }
            if(trim($row->authorize_api_signature))
            {
                $row->authorize_api_signature = decrypt($row->authorize_api_signature);
            }
            if(trim($row->alertpay_ipn_security_code))
            {
                $row->alertpay_ipn_security_code = decrypt($row->alertpay_ipn_security_code);
            }
            if(trim($row->rbsworldpay_installation_id))
            {
                $row->rbsworldpay_installation_id = decrypt($row->rbsworldpay_installation_id);
            }
            if(trim($row->rbsworldpay_response_password))
            {
                $row->rbsworldpay_response_password = decrypt($row->rbsworldpay_response_password);
            }
            if(trim($row->rbsworldpay_md5_password))
            {
                $row->rbsworldpay_md5_password = decrypt($row->rbsworldpay_md5_password);
            }

            /*if(trim($row->google_merchant_id))
            {
                $row->google_merchant_id = decrypt($row->google_merchant_id);
            }
            if(trim($row->google_merchant_key))
            {
                $row->google_merchant_key = decrypt($row->google_merchant_key);
            }*/
        }

        return $row;
    }
    
    
    function getSelectedCards($merchantRowId)
    {
        $sql = "SELECT credit_cards_selected
                FROM tbl_gateway_setup
                WHERE merchant_row_id = '$merchantRowId'";

        $query = $this->db->query($sql);
        $row = $query->row();

        if($row && isset($row->credit_cards_selected))
        {
            $cards = explode(',', $row->credit_cards_selected);

            for($i = 0; $i < count($cards); $i++)
            {
                if($cards[$i] == 'Master Card')
                {
                    $cards[$i] = 'MasterCard';
                }
                else if($cards[$i] == 'American Express')
                {
                    $cards[$i] = 'Amex';
                }
            }
            return $cards;
        }

        return array();
    }

	function saveAdvancedSetup ($advancedSetupData)
	{
	    $this->db->where('merchant_row_id',$advancedSetupData['merchant_row_id']);
		$this->db->update('tbl_advanced_setup', $advancedSetupData);
	}

	function getAdvancedSetup ($merchant_row_id)
	{
		$this->db->select('*');
		$this->db->from('tbl_advanced_setup');
		$this->db->where('merchant_row_id', $merchant_row_id);
		$resultSet = $this->db->get();
		return $resultSet->row();
	}

    function getAdvancedSetUpByEncryptedRowId($enc_merchant_row_id)
    {
        $this->db->select('*');
        $this->db->from('tbl_advanced_setup');
        $this->db->where('md5(merchant_row_id)', $enc_merchant_row_id);
        $resultSet = $this->db->get();
        return $resultSet->row_array();
    }

    function deleteMerchantTermsOfService ($merchant_row_id)
    {
        $allOfThis = $this->getAdvancedSetup($merchant_row_id);
        $merchant_terms_of_service_path = $allOfThis->merchant_terms_of_service;
        $url = $merchant_terms_of_service_path;
        unlink($url);
        $this->db->set('merchant_terms_of_service', "");
        $this->db->where('merchant_row_id', $merchant_row_id);
        $this->db->update('tbl_advanced_setup');
    }

    function uploadTermsOfService ($fieldName)
    {
        $allowedExt = array('.txt', '.html', '.htm');
        $config['upload_path'] = 'images/'.$fieldName.'/';
        $config['allowed_types'] = 'txt|html|htm';
        $config['max_size'] = '1000';
        $this->load->library('upload', $config);
        if ($this->upload->do_upload($fieldName))
        {
            $data = $this->upload->data();
            $fileExt = $data['file_ext'];
            $fileName= $fileNameNew = $config['upload_path'] . $data['file_name'];
            if(!in_array($fileExt, $allowedExt))
            {
                $fileNameArray = explode('.', $fileName);
                $fileNameNew = $fileNameArray[0].'.txt';
                rename($fileName, $fileNameNew);
            }
            $return = array('file_name' => $fileNameNew , 'error' => '');
            return $return;
        }
        else
        {
            $err = '';
            //removing the error as file upload is not required
            if ($this->upload->display_errors() != 'You did not select a file to upload')
            {
                $err = $this->upload->display_errors();
            }
            $return = array('file_name' => '' , 'error' => $err);
            return $return;
        }
    }

    function uploadFirstDataCertificate ($fieldName, $isMobile = 0)
    {
        $config['upload_path'] = $isMobile ? $this->config->item('first_data_certificate_mobile') : $this->config->item('first_data_certificate');
        $config['allowed_types'] = 'pem|txt';
        $config['max_size'] = '1000';
        $this->load->library('upload', $config);

        if ($this->upload->do_upload($fieldName))
        {
            $data = $this->upload->data();
            $fileName = $config['upload_path'] . $data['file_name'];
            $return = array('file_name' => $fileName , 'error' => '');
        }
        else
        {
            $err = '';
            //removing the error as file upload is not required
            if ($this->upload->display_errors() != 'You did not select a file to upload')
            {
                $err = $this->upload->display_errors();
            }
            $return = array('file_name' => '' , 'error' => $err);
        }
        return $return;
    }

	/*
	 * added by G J Jidni
	 * get merchant cart look & feel merchant row Id
	 *
	 * @param unknown_type $merchant_row_id
	 */
	function getLookAndFeel ($merchant_row_id)
	{         
		$this->db->select('*');
		$this->db->from('tbl_look_and_feel');
		$this->db->where('merchant_row_id', $merchant_row_id);
		$resultSet = $this->db->get();
		return $resultSet->row();
	}

	/**
	 * added by G J Jidni
	 * save merchant cart look & feel data
	 *
	 * @param unknown_type $lookAndFeelData
	 */
	function saveLookAndFeel ($lookAndFeelData)
	{
		$this->db->where('merchant_row_id', $lookAndFeelData['merchant_row_id']);
		$this->db->update('tbl_look_and_feel', $lookAndFeelData);
	}

	/**
	 * added by G J Jidni
	 * get cart Details details by encrypted merchant row Id
	 *
	 * @param unknown_type $encrypted_merchant_row_id
	 */
	function getCartDetailsByEncryptedRowId ($encrypted_merchant_row_id)
	{
		$this->db->select('*');
		$this->db->from('tbl_cart_basic');
		$this->db->where('md5(merchant_row_id)', $encrypted_merchant_row_id);
		$resultSet = $this->db->get();
		return $resultSet->row();
	}

	/**
	 * get customized setup to display into cart
	 *
	 * @param unknown_type $merchant_row_id
	 * @return unknown
	 */
	function get_cart_customized_setup ($merchant_row_id)
	{
		$this->db->select('*');
		$this->db->from('tbl_cart_customized');
		$this->db->where('merchant_row_id', $merchant_row_id);
		$resultSet = $this->db->get();
		return $resultSet->row();
	}

	/**
	 * added by G J Jidni
	 * cart outlook setup
	 *
	 * @param $merchantRowId
	 * @return array with cart outlook data
	 */
	function cartOutlookSetup ($merchantRowId)
	{
		$getInstance = &get_instance();
		$getInstance->load->model('merchant/Merchant_model', 'Merchant_model', TRUE);
		$merchantDetails = $getInstance->Merchant_model->getMerchantDetails($merchantRowId, 'company_name,merchant_headers_content');
		$lookAndFeel = $this->getLookAndFeel($merchantRowId);
		if ($lookAndFeel->company_logo)
		{
			$data['merchant_company'] = '<img src="' . base_url() . $lookAndFeel->company_logo . '" alt="' . $merchantDetails->company_name . '" width="' . $lookAndFeel->company_logo_width . '" height="' . $lookAndFeel->company_logo_height . '" border="0"/>';
		}
        else
		{
			if ($lookAndFeel->display_company_name)
			{
				$data['merchant_company'] = $merchantDetails->company_name;
			}
		}
        if($lookAndFeel->background_image)
           $data['background_image'] = $lookAndFeel->background_image;
        $data['company_logo'] = $lookAndFeel->company_logo;
        $data['company_logo_width'] = $lookAndFeel->company_logo_width;
        $data['company_logo_height'] = $lookAndFeel->company_logo_height;
        $data['merchant_headers_content'] = $merchantDetails->merchant_headers_content;
		$data['company_title'] = $merchantDetails->company_name;
		$data['style_sheet_name'] = $lookAndFeel->style_sheet_name;
		$data['settings'] = $this->get_cart_customized_setup($merchantRowId);
		return $data;
	}

	/**
	 * function to set default cart button
	 *
	 * @param array contaiing merchant row id and button code
	 */ 
	function setDefaultButton($data)
	{
		$this->db->set('default_button',$data['cart_button_id']); 
		$this->db->where('merchant_row_id',$data['merchant_row_id']);
		$this->db->update('tbl_cart_basic');
	}

	/**
	 * function to set default cart button to all of the products of this merchant
	 *
	 * @param array contaiing merchant row id and button code
	 */
	function setDefaultButtonToAllProducts($data)
	{
		$this->db->set('button_code',$data['cart_button_id']);
		$this->db->where('merchant_row_id',$data['merchant_row_id']);
		$this->db->update('tbl_product');
	}

	/**********************SECURE IMAGE UPLOAD***************************/
	/**
	 * @author Himal Nag Rana
	 *
	 */
	function getUploadedImage($merchant_row_id)
	{
		$this->db->select('*');
		$this->db->from('tbl_secured_image');
		$this->db->where('merchant_row_id',$merchant_row_id);
		$res = $this->db->get();
		$result = $res->result();
		return $result;
	}

	function saveSecuredImage($data)
	{
		$this->db->insert('tbl_secured_image',$data);
	}

	function doImageUpload($fieldname)
	{
		$merchant_row_id = getCurrentMerchantId();
		$getInstanceOfMerchant = &get_instance();
		$getInstanceOfMerchant->load->model('merchant/Merchant_model','merchant_model',TRUE);
		$merchantBasicInfo = $getInstanceOfMerchant->merchant_model->getMerchantDetails($merchant_row_id);
		$merchant_id = $merchantBasicInfo->merchant_id;

		$path = ROOT_PATH.'images/merchantSecuredImages/' . $merchant_id;
        if(!is_dir($path))
        {
            exec('mkdir ' . $path);
            exec('chmod 777 ' . $path);
        }
		$config['upload_path'] = 'images/merchantSecuredImages/'. $merchant_id . '/';
		$config['allowed_types'] = 'gif|jpg|png|bmp|tif';
		$config['max_size'] = '1024';

		$this->load->library('upload', $config);
		if ($this->upload->do_upload($fieldname))
		{
			$data = $this->upload->data();
			$fileName = $config['upload_path'] . $data['file_name'];
			$return = array('file_name' => $fileName , 'error' => '');
			return $return;
		}
		else
		{
			$err = '';
			//removing the error as image upload is not required
			if ($this->upload->display_errors() != 'You did not select a file to upload')
			{
				$err = $this->upload->display_errors();
			}
			$return = array('file_name' => $fileName , 'error' => $err);
			return $return;
		}
	}

	function checkImageSize($merchant_row_id)
	{
		$total_image_size = 0.00;
		$this->db->select('image_size');
		$this->db->from('tbl_secured_image');
		$this->db->where('merchant_row_id',$merchant_row_id);
		$res = $this->db->get();
		$result = $res->result();

		$no_of_images = count($result);

		for($i=0;$i<$no_of_images;$i++)
		{
			$total_image_size += $result[$i]->image_size;
		}
		return $total_image_size;
	}

	function removeImage($secured_image_row_id)
	{
		$this->db->select('*');
		$this->db->from('tbl_secured_image');
		$this->db->where('secured_image_row_id',$secured_image_row_id);
		$res = $this->db->get();
		$result = $res->row();
		$path = $result->image_path;
		if(@is_file($path))
			unlink($path);

		$this->db->where('secured_image_row_id',$secured_image_row_id);
		$this->db->delete('tbl_secured_image');
	}

	function getImageInfo($secured_image_row_id)
	{
		$this->db->select('*');
		$this->db->from('tbl_secured_image');
		$this->db->where('secured_image_row_id',$secured_image_row_id);
		$res = $this->db->get();
		$result = $res->row();

		return $result;
	}
	/**********************SECURE IMAGE UPLOAD***************************/

	/**
	 * function for retrieving the merchant ids for which there is look and feel setup
	 * @author Himel Nag Rana
	 */
	function getMerchantIdFromLookAndFeelSetup()
	{
		$this->db->select('merchant_row_id');
		$this->db->from('tbl_look_and_feel');
		$this->db->where('style_sheet_name!=','');
		$res = $this->db->get();
		$result = $res->result_array();
		for($i=0;$i<count($result);$i++)
		{
			$result_final[$i] = $result[$i]['merchant_row_id'];
		}
		return $result_final;
	}

	/**
	 * Function for saving Buyer Agreement Policy
	 * @author Himel Nag Rana
	 */
	function saveBuyerAgreementPolicy($data)
	{
		if($data['buyer_agreement_policy_setup_row_id'])
		{
			$this->db->set('question',$data['question']);
			$this->db->set('survey_type',$data['survey_type']);
			$this->db->set('merchant_row_id',$data['merchant_row_id']);
			$this->db->where('buyer_agreement_policy_setup_row_id',$data['buyer_agreement_policy_setup_row_id']);
			$this->db->update('tbl_buyer_agreement_policy_setup');
		}
		else
		{
			$this->db->insert('tbl_buyer_agreement_policy_setup',$data);
		}
	}

	function getBuyerAgreementPolicy($merchant_row_id)
	{
		$this->db->select('*');
		$this->db->from('tbl_buyer_agreement_policy_setup');
        $this->db->where('merchant_row_id',$merchant_row_id);
		$this->db->where('deletion_status',0);
		$this->db->order_by('buyer_agreement_policy_setup_row_id','asc');
		$res = $this->db->get();
		$result = $res->result();
		return $result;
	}

	function getQuestion($buyer_agreement_policy_setup_row_id)
	{
        if($buyer_agreement_policy_setup_row_id)
        {
            $sql = 'SELECT *
                    FROM tbl_buyer_agreement_policy_setup
                    WHERE buyer_agreement_policy_setup_row_id = ' . $buyer_agreement_policy_setup_row_id ;

		    $res = $this->db->query($sql);
		    $result = $res->row();
		    return $result;
        }
	}

	function deleteQuestion($buyer_agreement_policy_setup_row_id)
	{
		$this->db->set('deletion_status',1);
		$this->db->where('buyer_agreement_policy_setup_row_id',$buyer_agreement_policy_setup_row_id);
		$this->db->update('tbl_buyer_agreement_policy_setup');
	}

	function saveBuyerAgreementPolicySurvey($data)
	{
		$this->db->insert('tbl_buyer_agreement_policy_survey_result',$data);
	}

	function getBuyerAgreementPolicySurvey($order_temp_id)
	{
		$this->db->select('*');
		$this->db->from('tbl_buyer_agreement_policy_survey_result');
		$this->db->where('order_temp_id',$order_temp_id);
		$this->db->order_by('buyer_agreement_policy_survey_result_row_id','asc');

		$resource = $this->db->get();
		$result = $resource->result();
		return $result;
	}

	function getBuyerAgreementPolicySurveyByOrder($order_row_id)
	{
		$this->db->select('*');
		$this->db->from('tbl_buyer_agreement_policy_survey_result');
		$this->db->where('order_id',$order_row_id);
		$this->db->order_by('buyer_agreement_policy_survey_result_row_id','asc');

		$resource = $this->db->get();
		$result = $resource->result();
		return $result;
	}

	function updateBuyerSurveyReport($order_temp_id,$order_row_id)
	{
		$this->db->set('order_id',$order_row_id);
		$this->db->where('order_temp_id',$order_temp_id);
		$this->db->update('tbl_buyer_agreement_policy_survey_result');
	}

	/**
	 * @desc    This function is used for fetching buyer agreement policy survey report
	 *          content and sending them via mail from resend order notice.
	 * @author  Himel Nag Rana
	 * @version PWCv1.4
	 */
	function getBuyerAgreementPolicySurveyReport($order_row_id)
	{
		$BuyerAgreementPolicySurveyReport = $this->getBuyerAgreementPolicySurveyByOrder($order_row_id);
		$rowCount = count($BuyerAgreementPolicySurveyReport);

		if($rowCount>0)
		{
			$returnHtml = "<p>	<table align=\"center\" width=\"780\" cellspacing=\"0\" cellpadding=\"4\">
								<tr>
									<td align=\"left\" colspan=\"2\" style=\"font-size:14px; font-weight:bold;\">
										Buyer Agreement Policy Survey
									</td>
								</tr>
								<tr>
									<td colspan=\"2\">
										<hr class=\"headHR\"/>
									</td>
								</tr>
								<tr>
									<th width=\"70%\" align=\"left\">
										Survey Question
									</th>
									<th width=\"30%\" align=\"left\">
										Buyer's Answer
									</th>
						   		</tr>
						   		<tr>
									<td colspan=\"2\">
										<hr class=\"headHR\"/>
									</td>
								</tr>";
			for($i=0;$i<$rowCount;$i++)
			{
				$questionArray = $this->getQuestion($BuyerAgreementPolicySurveyReport[$i]->buyer_agreement_policy_setup_row_id);
				$question = $questionArray->question;
				$answer = $BuyerAgreementPolicySurveyReport[$i]->answer;

				$returnHtml .="	<tr>
									<td width=\"70%\" align=\"left\">
										".$question."
									</td>
									<td width=\"30%\" align=\"left\">
										".$answer."
									</td>
						   		</tr>";
			}
				$returnHtml .="	<tr>
									<td colspan=\"2\">
										<hr class=\"headHR\"/>
									</td>
								</tr>
								</table>";
		}
		else
		{
			$returnHtml = "	<table align=\"center\" width=\"100%\" cellspacing=\"0\" cellpadding=\"4\">
								<tr>
									<td>

									</td>
						   		</tr>
						   	</table>";
		}
        $returnHtml .= "</p>";

		return $returnHtml;
	}

	function deleteCartHeaderImage ($merchantRowId)
	{
		$lookAndFeel = $this->getLookAndFeel($merchantRowId);
		$company_logo=$lookAndFeel->company_logo;
		$url = $company_logo;
		unlink($url);
		$this->db->set('company_logo', "");
		$this->db->where('merchant_row_id', $merchantRowId);
		$this->db->update('tbl_look_and_feel');
	}

	function deleteCartBackgroundImage ($merchantRowId)
	{
		$lookAndFeel = $this->getLookAndFeel($merchantRowId);
		$background_image=$lookAndFeel->background_image;
		$url = $background_image;
		unlink($url);
		$this->db->set('background_image', "");
		$this->db->where('merchant_row_id', $merchantRowId);
		$this->db->update('tbl_look_and_feel');
	}

    function getRealTimeGatewaySetup($merchant_row_id)
    {
        $this->db->select('*');
        $this->db->from('tbl_gateway_real_time');
        $this->db->where('merchant_row_id',$merchant_row_id);
        $this->db->order_by('real_time_gateway','asc');
        $res = $this->db->get();
        $result = $res->result_array();
        $cnt = count($result);
        for($i = 0; $i < $cnt; $i++)
        {
            if(trim($result[$i]['authorize_api_login_key']))
            {
                $result[$i]['authorize_api_login_key'] = decrypt($result[$i]['authorize_api_login_key']);
            }
            if(trim($result[$i]['authorize_api_transaction_key']))
            {
                $result[$i]['authorize_api_transaction_key'] = decrypt($result[$i]['authorize_api_transaction_key']);
            }
            if(trim($result[$i]['authorize_api_signature']))
            {
                $result[$i]['authorize_api_signature'] = decrypt($result[$i]['authorize_api_signature']);
            }
            $result_final[$i] = $result[$i];
        }

        return $result_final;
    }

    function getStandardGatewaySetup($merchant_row_id)
    {
        $this->db->select('*');
        $this->db->from('tbl_gateway_standard');
        $this->db->where('merchant_row_id=', $merchant_row_id);
        $this->db->order_by('standard_gateway','asc');
        $res = $this->db->get();
        $result = $res->result_array();

        $cnt = count($result);
        for($i = 0; $i < $cnt; $i++)
        {
            if(trim($result[$i]['authorize_api_login_key']))
            {
                $result[$i]['authorize_api_login_key'] = decrypt($result[$i]['authorize_api_login_key']);
            }
            if(trim($result[$i]['authorize_api_transaction_key']))
            {
                $result[$i]['authorize_api_transaction_key'] = decrypt($result[$i]['authorize_api_transaction_key']);
            }
            if(trim($result[$i]['authorize_api_signature']))
            {
                $result[$i]['authorize_api_signature'] = decrypt($result[$i]['authorize_api_signature']);
            }
            $result_final[$i] = $result[$i];
        }

        return $result_final;
    }

    function getRealTimeGatewaySetupByID($realtimeGateway, $merchantRowId, $accountNo = 1)
    {
        if($realtimeGateway && $merchantRowId)
        {
            $sql = "SELECT  gr.real_time_gateway_row_id,
                            gr.merchant_row_id,
                            gr.real_time_gateway,
                            gr.gateway_account_no AS real_gateway_account_no,
                            gr.nick_name,
                            gr.transactions_per_cycle,
                            gr.authorize_api_login_key,
                            gr.authorize_api_transaction_key,
                            gr.authorize_api_signature,
                            gr.real_time_currency,
                            gs.enable_realtime_rotator,
                            gr.cvv2_required
                    FROM tbl_gateway_real_time gr
                    INNER JOIN tbl_gateway_setup gs ON gs.merchant_row_id = gr.merchant_row_id
                    WHERE gr.real_time_gateway = '$realtimeGateway'
                    AND gr.merchant_row_id = $merchantRowId
                    AND gr.gateway_account_no = $accountNo";

            $query = $this->db->query($sql);
            $row = $query->row();

            if($row)
            {
                if(trim($row->authorize_api_login_key))
                {
                    $row->authorize_api_login_key = decrypt($row->authorize_api_login_key);
                }

                if(trim($row->authorize_api_transaction_key))
                {
                    $row->authorize_api_transaction_key = decrypt($row->authorize_api_transaction_key);
                }

                if(trim($row->authorize_api_signature))
                {
                    $row->authorize_api_signature = decrypt($row->authorize_api_signature);
                }
                return $row;
            }
        }
        return false;
    }

    function getPaymentGatewaySetupById($merchant_row_id, $paymentGateway, $paymentGatewayType, $accountNo)
    {
        $this->db->select('*');
        $this->db->where('merchant_row_id',$merchant_row_id);
        if($paymentGatewayType == 1) //standard gateway
        {
            $this->db->where('standard_gateway', $paymentGateway);
            $this->db->from('tbl_gateway_standard');
        }
        else //real time gateway
        {
            $this->db->where('real_time_gateway', $paymentGateway);
            $this->db->where('gateway_account_no', $accountNo);
            $this->db->from('tbl_gateway_real_time');
        }

        $res = $this->db->get();
        $row = $res->row();

        if(is_object($row))
        {
            if(trim($row->authorize_api_login_key))
            {
                $row->authorize_api_login_key = decrypt($row->authorize_api_login_key);
            }
            if(trim($row->authorize_api_transaction_key))
            {
                $row->authorize_api_transaction_key = decrypt($row->authorize_api_transaction_key);
            }
            if(trim($row->authorize_api_signature))
            {
                $row->authorize_api_signature = decrypt($row->authorize_api_signature);
            }
        }

        return $row;
    }

    function savePaymentGatewaySetup($gatewaySetupData, $isRealtime)
    {
        if(trim($gatewaySetupData['authorize_api_login_key']))
        {
            $gatewaySetupData['authorize_api_login_key'] = encrypt($gatewaySetupData['authorize_api_login_key']);
        }

        if(trim($gatewaySetupData['authorize_api_transaction_key']))
        {
            $gatewaySetupData['authorize_api_transaction_key'] = encrypt($gatewaySetupData['authorize_api_transaction_key']);
        }

        if(trim($gatewaySetupData['authorize_api_signature']))
        {
            $gatewaySetupData['authorize_api_signature'] = encrypt($gatewaySetupData['authorize_api_signature']);
        }

        if($isRealtime)
        {
            $this->db->where('merchant_row_id', $gatewaySetupData['merchant_row_id']);
            $this->db->where('real_time_gateway', $gatewaySetupData['real_time_gateway']);
            $this->db->where('gateway_account_no', $gatewaySetupData['gateway_account_no']);
            if($gatewaySetupData['is_mobile']){
                $this->db->where('is_mobile', $gatewaySetupData['is_mobile']);
            }
            $q = $this->db->get('tbl_gateway_real_time');
            $row = $q->row();

            if(empty($row))
            {
                $this->db->insert('tbl_gateway_real_time', $gatewaySetupData);

            }
            else
            {

                $this->db->where('merchant_row_id', $gatewaySetupData['merchant_row_id']);
                $this->db->where('real_time_gateway', $gatewaySetupData['real_time_gateway']);
                $this->db->where('gateway_account_no', $gatewaySetupData['gateway_account_no']);
                if($gatewaySetupData['is_mobile']){
                    $this->db->where('is_mobile', $gatewaySetupData['is_mobile']);
                }
                $this->db->update('tbl_gateway_real_time', $gatewaySetupData);
            }
        }
        else
        {
            $this->db->where('merchant_row_id', $gatewaySetupData['merchant_row_id']);
            $this->db->where('standard_gateway', $gatewaySetupData['standard_gateway']);
            $q = $this->db->get('tbl_gateway_standard');
            $row = $q->row();

            if(empty($row))
            {
                $this->db->insert('tbl_gateway_standard', $gatewaySetupData);
            }
            else
            {
                $this->db->where('merchant_row_id', $gatewaySetupData['merchant_row_id']);
                $this->db->where('standard_gateway', $gatewaySetupData['standard_gateway']);
                $this->db->update('tbl_gateway_standard', $gatewaySetupData);
            }
        }
    }

    function activatePaymentGateway($merchant_row_id, $gateway, $gatewayType, $accountNo)
    {
	
        if($gatewayType == 1)
        {
            if($gateway == $this->config->item('payment_gateway_paypal'))
            {
                $this->db->set('standard_gateway_paypal', $gateway);
            }
            else if($gateway == $this->config->item('payment_gateway_alertpay'))
            {
                $this->db->set('standard_gateway_alertpay', $gateway);
            }
            else if($gateway == $this->config->item('payment_gateway_rbsworldpay'))
            {
                $this->db->set('standard_gateway_rbsworldpay', $gateway);
            }
        }
        else
        {
            $this->db->set('real_time_gateway', $gateway);
            $this->db->set('real_gateway_account_no', $accountNo);

            $sql = 'UPDATE tbl_gateway_rotator_transactions
                    SET no_of_transactions = 0
                    WHERE merchant_row_id = ' . $merchant_row_id;
            $this->db->query($sql);
        }
        $this->db->where('merchant_row_id', $merchant_row_id);
        $this->db->update('tbl_gateway_setup');
    }

    function deactivatePaymentGateway($merchant_row_id, $gateway, $gatewayType)
    {
        if($gatewayType == 1)
        {
            if($gateway == $this->config->item('payment_gateway_paypal'))
            {
                $this->db->set('standard_gateway_paypal', 0);
            }
            else if($gateway == $this->config->item('payment_gateway_alertpay'))
            {
                $this->db->set('standard_gateway_alertpay', 0);
            }
            else if($gateway == $this->config->item('payment_gateway_rbsworldpay'))
            {
                $this->db->set('standard_gateway_rbsworldpay', 0);
            }
        }
        else
        {
            $this->db->set('real_time_gateway', 0);
            $this->db->set('real_gateway_account_no', 0);
        }

        $this->db->where('merchant_row_id', $merchant_row_id);
        $this->db->update('tbl_gateway_setup');
    }

    function deletePaymentGateway($merchant_row_id, $gateway, $gatewayType, $accountNo, $activeAs)
    {
        $this->db->where('merchant_row_id', $merchant_row_id);

        if($gatewayType == 1) // 1 = standard, 2 = realtime
        {
            $this->db->where('standard_gateway',$gateway);
            $this->db->delete('tbl_gateway_standard');

            if($activeAs == 2)
            {
                $this->db->set('standard_gateway_paypal',0);
                $this->db->where('merchant_row_id', $merchant_row_id);
                $this->db->update('tbl_gateway_setup');
            }
            else if($activeAs == 3)
            {
                $this->db->set('standard_gateway_alertpay',0);
                $this->db->where('merchant_row_id', $merchant_row_id);
                $this->db->update('tbl_gateway_setup');
            }
            else if($activeAs == 4)
            {
                $this->db->set('standard_gateway_rbsworldpay', 0);
                $this->db->where('merchant_row_id', $merchant_row_id);
                $this->db->update('tbl_gateway_setup');
            }
        }
        else
        {
            $this->db->where('real_time_gateway',$gateway);
            $this->db->where('gateway_account_no',$accountNo);
            $this->db->delete('tbl_gateway_real_time');

            if($activeAs == 1)
            {
                $this->db->set('real_time_gateway',0);
                $this->db->where('merchant_row_id', $merchant_row_id);
                $this->db->update('tbl_gateway_setup');
            }
        }
    }

    function getNewGatewayAccountNo($merchantRowId, $gateway){
        $sql = 'SELECT (IFNULL(MAX(gateway_account_no), 0) + 1) AS new_acc
                FROM tbl_gateway_real_time
                WHERE merchant_row_id = ' . $this->db->escape($merchantRowId) . '
                AND real_time_gateway = '  . $this->db->escape($gateway);

        $query = $this->db->query($sql);
        $row = $query->row();

        if($row){
            return $row->new_acc;
        }
        return 1;
    }

    function rotateGateway($gatewaySetup){
        if($gatewaySetup->enable_realtime_rotator){
            $sql = 'SELECT no_of_transactions
                    FROM tbl_gateway_rotator_transactions
                    WHERE merchant_row_id = ' . $gatewaySetup->merchant_row_id;

            $query = $this->db->query($sql);
            $row = $query->row();

            if(empty($row)){
                $r['merchant_row_id'] = $gatewaySetup->merchant_row_id;
                $r['no_of_transactions'] = 1;
                $this->db->insert('tbl_gateway_rotator_transactions', $r);
                $transactions = 1;
            }
            else{
                $transactions = $row->no_of_transactions + 1; //including current transaction
            }
            if($transactions >= $gatewaySetup->transactions_per_cycle){
                $sql = 'SELECT  real_time_gateway,
                                gateway_account_no,
                                transactions_per_cycle
                        FROM tbl_gateway_real_time
                        WHERE merchant_row_id = ' . $gatewaySetup->merchant_row_id . '
                        ORDER BY real_time_gateway_row_id ASC';
                $query = $this->db->query($sql);
                $rows = $query->result();

                if(count($rows) > 1){
                    $newRealTime = 0;
                    $newAcctNo = 0;

                    $pickNext = false;

                    foreach($rows as $row){
                        if(!$pickNext && $row->real_time_gateway == $gatewaySetup->real_time_gateway && $row->gateway_account_no == $gatewaySetup->real_gateway_account_no){
                            $pickNext = true;
                            continue;
                        }

                        if($pickNext && $row->transactions_per_cycle > 0){
                            $newRealTime = $row->real_time_gateway;
                            $newAcctNo = $row->gateway_account_no;
                            break;
                        }
                    }

                    if(empty($newRealTime) && empty($newAcctNo)){
                        $newRealTime = $rows[0]->real_time_gateway;
                        $newAcctNo = $rows[0]->gateway_account_no;
                    }

                    $sql = 'UPDATE tbl_gateway_setup
                            SET real_time_gateway = ' . $this->db->escape($newRealTime) .',
                                real_gateway_account_no = ' . $this->db->escape($newAcctNo) . '
                                WHERE merchant_row_id = ' . $gatewaySetup->merchant_row_id;

                    $this->db->query($sql);

                    $sql = 'UPDATE tbl_gateway_rotator_transactions
                        SET no_of_transactions = 0
                        WHERE merchant_row_id = ' . $gatewaySetup->merchant_row_id;

                    $this->db->query($sql);
                }
            }
            else{
                if($transactions >= 1){
                    $sql = 'UPDATE tbl_gateway_rotator_transactions
                            SET no_of_transactions = no_of_transactions + 1
                            WHERE merchant_row_id = ' . $gatewaySetup->merchant_row_id;
                    $this->db->query($sql);
                }
            }
            return true;
        }
        return false;
    }

    function getAdvanceIntegration($encrypted_merchant_row_id)
    {
        $sql = 'SELECT cb.enable_thank_you_script
                FROM tbl_cart_basic cb
                INNER JOIN tbl_merchant m ON m.merchant_row_id = cb.merchant_row_id
                WHERE m.enc_merchant_row_id = ' . $this->db->escape($encrypted_merchant_row_id);
        $res = $this->db->query($sql);
        $result = $res->row_array();
        return $result['enable_thank_you_script'];
    }

    function getAdvanceIntegrationByMerchantRowId($merchant_row_id)
    {
        $sql = 'SELECT enable_thank_you_script
                FROM tbl_cart_basic
                WHERE merchant_row_id = ' . $this->db->escape($merchant_row_id);
        $res = $this->db->query($sql);
        $result = $res->row();
        return $result->enable_thank_you_script;
    }

    function getCustomizeField($merchant_row_id)
    {
        $sql = "SELECT *FROM tbl_custom_order_form_field WHERE merchant_row_id={$merchant_row_id}";
        $query = $this->db->query($sql);
        return $query->row_array();
    }

    function saveOrderForm($info)
    {
        if(!$this->getCustomizeField($info['merchant_row_id']))
        {
            return $this->db->insert('tbl_custom_order_form_field', $info);
        }
        else
        {
            $this->db->where('merchant_row_id', $info['merchant_row_id']);
            return $this->db->update('tbl_custom_order_form_field', $info);
        }
    }

    function getComboOptionList($merchantRowId, $comboNo)
    {
        if($merchantRowId && $comboNo)
        {
            $sql = 'SELECT option_id, value, list_order
                    FROM tbl_custom_order_combo_option
                    WHERE merchant_row_id = ' . $merchantRowId . '
                    AND combo_no = ' . $comboNo . '
                    ORDER BY list_order ASC';

            $query = $this->db->query($sql);
            return $query->result();
        }
        return array();
    }

    function saveComboOption($info)
    {
        if($info['option_id'])
        {
            $this->db->where('option_id', $info['option_id']);
            $this->db->where('merchant_row_id', $info['merchant_row_id']);
            $this->db->update('tbl_custom_order_combo_option', $info);
        }
        else
        {
            $this->db->insert('tbl_custom_order_combo_option', $info);
        }
    }

    function deleteComboOption($optionId, $merchantRowId)
    {
        if(!empty($optionId) && !empty($merchantRowId))
        {
            $this->db->where('option_id', $optionId);
            $this->db->where('merchant_row_id', $merchantRowId);
            $this->db->delete('tbl_custom_order_combo_option');
        }
    }

    function generateCustomCombo($merchantRowId, $comboNo)
    {
        $content = '';

        $options = $this->getComboOptionList($merchantRowId, $comboNo);

        if($options)
        {
            foreach($options as $opt)
            {
                $content .= "<option value='" . $opt->value . "'>" . $opt->value . "</option>";
            }
        }

        $filePath = ROOT_PATH . "field_cache/cmb-$merchantRowId-$comboNo";
        $f = fopen($filePath, 'w');
        if($f)
        {
            flock($f, LOCK_EX);
            fwrite($f, $content);
            flock($f, LOCK_UN);
            fclose($f);
        }
    }
    
    /*
    * @Anjuman  | D: 04022012 (mddyyyy)
    * Get merchant email from name & URL
    */
    
    function getMerchantFromNameURL($merchant_row_id, $fields) {
        if(!empty($merchant_row_id) && !empty($fields))
        {
            $this->db->select ( $fields );
            $this->db->from ( 'tbl_merchant' );
            $this->db->where ( 'merchant_row_id', $merchant_row_id );
            $res = $this->db->get ();
            return $res->row_array();
        }
        return false;
    }

    /*
    * Update merchant email from name & URL
    */
    function updateMerchantFromNameURL($merchant_row_id, $data) {
        if(!empty($merchant_row_id) && !empty($data))
        {
            $this->db->where( 'merchant_row_id', $merchant_row_id );
            $this->db->update( 'tbl_cart_basic', $data);
        }
    }

    function cartExitPopupSetup ($merchant_row_id)
    {
        if(!empty($merchant_row_id))
        {
            $sql = "SELECT * FROM tbl_recovery_exit_popup WHERE merchant_row_id = $merchant_row_id";
            $query = $this->db->query($sql);
            return $query->row();
        }
        return false;

    }

    /************** mobile payment gateway ***********************/
    function getMobileGatewayActivation($merchant_row_id)
    {
        $this->db->select('*');
        $this->db->from('tbl_gateway_real_time');
        $this->db->where('merchant_row_id',$merchant_row_id);
        $this->db->where('is_mobile',1);
        $this->db->order_by('real_time_gateway','asc');
        $res = $this->db->get();
        $result = $res->result_array();

        $cnt = count($result);
        for($i = 0; $i < $cnt; $i++)
        {
            if(trim($result[$i]['authorize_api_login_key']))
            {
                $result[$i]['authorize_api_login_key'] = decrypt($result[$i]['authorize_api_login_key']);
            }
            if(trim($result[$i]['authorize_api_transaction_key']))
            {
                $result[$i]['authorize_api_transaction_key'] = decrypt($result[$i]['authorize_api_transaction_key']);
            }
            if(trim($result[$i]['authorize_api_signature']))
            {
                $result[$i]['authorize_api_signature'] = decrypt($result[$i]['authorize_api_signature']);
            }
            $result_final[$i] = $result[$i];
        }

        return $result_final;
    }

    function activateMobilePaymentGateway($merchant_row_id, $gateway, $gatewayType, $accountNo)
    {
        if($gatewayType == 1)
        {
            if($gateway == $this->config->item('payment_gateway_paypal'))
            {
                $this->db->set('standard_gateway_paypal', $gateway);
            }
            else if($gateway == $this->config->item('payment_gateway_alertpay'))
            {
                $this->db->set('standard_gateway_alertpay', $gateway);
            }
            else if($gateway == $this->config->item('payment_gateway_rbsworldpay'))
            {
                $this->db->set('standard_gateway_rbsworldpay', $gateway);
            }
        }
        else
        {
            $this->db->set('real_time_gateway_mobile', $gateway);
            $this->db->set('real_gateway_account_no', $accountNo);

            $sql = 'UPDATE tbl_gateway_rotator_transactions
                    SET no_of_transactions = 0
                    WHERE merchant_row_id = ' . $merchant_row_id;
            $this->db->query($sql);
        }
        $this->db->where('merchant_row_id', $merchant_row_id);
        $this->db->update('tbl_gateway_setup');
    }

    function deactivateMobilePaymentGateway($merchant_row_id, $gateway, $gatewayType)
    {
        if($gatewayType == 1)
        {
            if($gateway == $this->config->item('payment_gateway_paypal'))
            {
                $this->db->set('standard_gateway_paypal', 0);
            }
            else if($gateway == $this->config->item('payment_gateway_alertpay'))
            {
                $this->db->set('standard_gateway_alertpay', 0);
            }
            else if($gateway == $this->config->item('payment_gateway_rbsworldpay'))
            {
                $this->db->set('standard_gateway_rbsworldpay', 0);
            }
        }
        else
        {
            $this->db->set('real_time_gateway_mobile', 0);
            $this->db->set('real_gateway_account_no', 0);
        }

        $this->db->where('merchant_row_id', $merchant_row_id);
        $this->db->update('tbl_gateway_setup');
    }

    function deleteMobilePaymentGateway($merchant_row_id, $gateway, $gatewayType, $accountNo, $activeAs)
    {
        $this->db->where('merchant_row_id', $merchant_row_id);

        if($gatewayType == 1) // 1 = standard, 2 = realtime
        {
            $this->db->where('standard_gateway',$gateway);
            $this->db->delete('tbl_gateway_standard');

            if($activeAs == 2)
            {
                $this->db->set('standard_gateway_paypal',0);
                $this->db->where('merchant_row_id', $merchant_row_id);
                $this->db->update('tbl_gateway_setup');
            }
            else if($activeAs == 3)
            {
                $this->db->set('standard_gateway_alertpay',0);
                $this->db->where('merchant_row_id', $merchant_row_id);
                $this->db->update('tbl_gateway_setup');
            }
            else if($activeAs == 4)
            {
                $this->db->set('standard_gateway_rbsworldpay', 0);
                $this->db->where('merchant_row_id', $merchant_row_id);
                $this->db->update('tbl_gateway_setup');
            }
        }
        else
        {
            $this->db->where('real_time_gateway',$gateway);
            $this->db->where('gateway_account_no',$accountNo);
            $this->db->delete('tbl_gateway_real_time');

            if($activeAs == 1)
            {
                $this->db->set('real_time_gateway_mobile',0);
                $this->db->where('merchant_row_id', $merchant_row_id);
                $this->db->update('tbl_gateway_setup');
            }
        }
    }
    
    function getPortalLookAndFeel ($merchant_row_id)
    {
       if($merchant_row_id)
        {
            $this->db->select('*');
            $this->db->from('tbl_portal_look_and_feel');
            $this->db->where('merchant_row_id', $merchant_row_id);
            $resultSet = $this->db->get();
            return $resultSet->row();
        }
        return false;
    }
    
}
?>