<?php
/**
 *@author Shafiul Alam
 *
 * @modified Jilani Jidni
 */
class Merchant_Signup_model extends CI_Model
{
	public function __construct()
	{
		parent::__construct();
	}

	function checkDuplicateEmail ($email_address)
	{
		$this->db->select('*');
		$this->db->from('tbl_merchant');
		$this->db->where('email_address', $email_address);
		$query_result = $this->db->get();

		if (count($query_result->result()) > 0)
		{
			return true;
		}
        else
		{
			return false;
		}
	}

	/**
	 *author Shafiul Alam
	 *This function save merchant information
	 *
	 */
	function saveMerchant ($merchant_signup_data)
	{
		/**
		 * insert into tbl_merchant table
		 */
		$this->db->set('first_name',$merchant_signup_data['first_name']);
		$this->db->set('last_name',$merchant_signup_data['last_name']);
		$this->db->set('company_name',$merchant_signup_data['company_name']);
		$this->db->set('email_address',$merchant_signup_data['email_address']);
        $this->db->set('public_email',$merchant_signup_data['email_address']);
		$this->db->set('user_password',$merchant_signup_data['user_password']);
		$this->db->set('merchant_contact_phone',$merchant_signup_data['merchant_contact_phone']);
		$this->db->set('merchant_address',$merchant_signup_data['merchant_address']);
		$this->db->set('merchant_city',$merchant_signup_data['merchant_city']);
		$this->db->set('state_province_name',$merchant_signup_data['state_province_name']);
		$this->db->set('merchant_zip_code',$merchant_signup_data['merchant_zip_code']);
        if($merchant_signup_data['pwp_only'])
            $this->db->set('pwp_only', 1);

		if($merchant_signup_data['hear_about_us'] == 'Other'){
			$this->db->set('hear_about_us',$merchant_signup_data['hear_about_us_txt']);
		}else{
			$this->db->set('hear_about_us',$merchant_signup_data['hear_about_us']);
		}

		$this->db->set('merchant_postal_code',$merchant_signup_data['merchant_postal_code']);
		$this->db->set('merchant_country',$merchant_signup_data['merchant_country']);
        
        if($merchant_signup_data['website_url'])
		   $this->db->set('website_url',$merchant_signup_data['website_url']);
		
        $this->db->set('package_id',$merchant_signup_data['package_id']);
		$this->db->set('is_trial',$merchant_signup_data['is_trial']);
		$this->db->set('subscription_type',$merchant_signup_data['subscription_type']);
		$this->db->set('signup_amount',$merchant_signup_data['signup_amount']);
		$this->db->set('active_status',$merchant_signup_data['active_status']);
		$this->db->set('activation_code',$merchant_signup_data['activation_code']);
		$this->db->set('activation_date',$merchant_signup_data['activation_date']);
        $this->db->set('last_transaction_date',$merchant_signup_data['last_transaction_date']);
        $this->db->set('next_charge_date',$merchant_signup_data['next_charge_date']);
		$this->db->set('last_due_date',$merchant_signup_data['last_due_date']);
        $this->db->set('admin_id',$merchant_signup_data['admin_id']);
		$this->db->set('created_on', getCurrentDateTimeForDB());
        $this->db->set('merchant_referrer_id',$merchant_signup_data['merchant_referrer_id']);

        $this->db->set('phone_type', $merchant_signup_data['phone_type'] ? $merchant_signup_data['phone_type'] : 1);
        $this->db->set('address_location',$merchant_signup_data['address_location'] ? $merchant_signup_data['address_location'] : 1);
        $this->db->set('white_label_id', $merchant_signup_data['white_label_id'] ? $merchant_signup_data['white_label_id'] : 1);
		$this->db->insert('tbl_merchant');

		$merchant_row_id = $this->db->insert_id();

		//////////////////////////////////////////
		//update merchant id
		$getInstance = &get_instance();
		$getInstance->load->model('administration/basic_setup_model','Basic_Setup_model',TRUE);
		// here admin_user_id is given 1 (for super admin) because
		// there will be only one super admin at a time (upto version 1.2)
		$basicData = $getInstance->Basic_Setup_model->getAdminBasicSetup(1);
		if($basicData->merchant_id_prefix)
		{
			$merchant_id_prefix = $basicData->merchant_id_prefix;
		}
		else
		{
			$merchant_id_prefix = $this->config->item('default_merchant_id_prefix');
		}

		$this->load->library('Application_Utility');
		//here 'MER'=>prefix for merchant id will be taken from basic setup table
		$merchant_id = $this->application_utility->generateId($merchant_id_prefix,$merchant_row_id);

		$this->db->set('merchant_id',$merchant_id);
		$this->db->where('merchant_row_id',$merchant_row_id);
		$this->db->update('tbl_merchant');
		/////////////////////////////////////////

		$this->db->set('merchant_row_id',$merchant_row_id);
		$this->db->set('billing_name',$merchant_signup_data['first_name'].' '.$merchant_signup_data['last_name']);
		$this->db->set('billing_email',$merchant_signup_data['email_address']);
		$this->db->set('billing_phone',$merchant_signup_data['merchant_contact_phone']);
		$this->db->set('billing_company',$merchant_signup_data['company_name']);
        
        //For opimized signup process 
        if($merchant_signup_data['billing_address'])
        {
            $this->db->set('billing_address',$merchant_signup_data['billing_address']);
            $this->db->set('billing_country',$merchant_signup_data['billing_country']);
            $this->db->set('billing_state',$merchant_signup_data['billing_state']);
            $this->db->set('billing_city',$merchant_signup_data['billing_city']);
            $this->db->set('billing_zip_code',$merchant_signup_data['billing_zip_code']);
            $this->db->set('billing_postal_code',$merchant_signup_data['billing_zip_code']);
        }
        else
        {
		    $this->db->set('billing_address',$merchant_signup_data['merchant_address']);
		    $this->db->set('billing_country',$merchant_signup_data['merchant_country']);
		    $this->db->set('billing_state',$merchant_signup_data['state_province_name']);
		    $this->db->set('billing_city',$merchant_signup_data['merchant_city']);
		    $this->db->set('billing_zip_code',$merchant_signup_data['merchant_zip_code']);
            $this->db->set('billing_postal_code',$merchant_signup_data['merchant_postal_code']);
        }
		$this->db->set('card_type',$merchant_signup_data['card_type']);

        if(isset($merchant_signup_data['card_number']) && is_number($merchant_signup_data['card_number']))
        {
		    $this->db->set('card_number', encrypt($merchant_signup_data['card_number']));
        }

        if(isset($merchant_signup_data['card_exp_month']) && $merchant_signup_data['card_exp_month'])
        {
		    $this->db->set('card_exp_month', encrypt($merchant_signup_data['card_exp_month']));
        }

        if(isset($merchant_signup_data['card_exp_year']) && $merchant_signup_data['card_exp_year'])
        {
		    $this->db->set('card_exp_year', encrypt($merchant_signup_data['card_exp_year']));
        }

        if(isset($merchant_signup_data['card_holder_contactname']) && $merchant_signup_data['card_holder_contactname'])
        {
		    $this->db->set('card_holder_contactname', encrypt($merchant_signup_data['card_holder_contactname']));
        }

		$this->db->insert('tbl_merchant_billing');

		/***save basic setup to tbl_cart_basic****/
		$currency = 'USD';
		$currency_symbol = '$';
		$thankyou_url = BASEURL . 'thank_you/thank_you_url/' . md5($merchant_row_id);
		$data1['merchant_row_id'] = $merchant_row_id;
		$data1['primary_destination'] = $merchant_signup_data['email_address'];
		$data1['from_address'] = $merchant_signup_data['email_address'];
        
        if($merchant_signup_data['website_url'])
		   $data1['catalog_url'] = $merchant_signup_data['website_url'];
		
        $data1['currency'] = $currency;
		$data1['currency_symbol'] = $currency_symbol;
		$data1['thankyou_url'] = $thankyou_url;

		/*setting auto-generated id prefix*/
		$data1['product_id_prefix'] = $this->config->item('default_product_id_prefix');//'PRO';
		$data1['order_id_prefix'] = $this->config->item('default_order_id_prefix');//'ORD';
		$data1['affiliate_id_prefix'] = $this->config->item('default_affiliate_id_prefix');//'AFF';

		$this->db->insert('tbl_cart_basic', $data1);


		$merchantInfo['merchant_row_id'] = $merchant_row_id;
		/**
		 * added by Jilani Jidni
		 * insert data to look & feel table
		 */
		$this->db->insert('tbl_look_and_feel', $merchantInfo);
		/**
		 * added by Jilani Jidni
		 * insert data to Gateway setup table
		 */
		$this->db->insert('tbl_gateway_setup', $merchantInfo);
		/**
		 * added by Jilani Jidni
		 * insert data to Gateway setup table
		 */
		$this->db->insert('tbl_advanced_setup', $merchantInfo);
		/**
		 * added by Jilani Jidni
		 * insert data to Auto Responder mail list setup table
		 */
		#$this->db->insert('tbl_autoresponder', $merchantInfo);

		return $merchant_row_id;
	}

	/**
	 *@author Shafiul Alam
	 *Using parameter this function merchant_row_id
	 *@param $merchant_row_id, $activationCode
	 */
	function getMerchant_row_id ($merchant_row_id, $activationCode)
	{
		$this->db->select('*');
		$this->db->where(merchant_row_id, $merchant_row_id);
		$this->db->where('activation_code', $activationCode);
		$query = $this->db->get('tbl_merchant');
		return $query->row_array();
	}

	/**
	 *@author Shafiul Alam
	 *This function activate the merchant account
	 */
	function activateAccount ($merchant_row_id, $activationCode)
	{
		$sql = 'UPDATE tbl_merchant
				SET active_status = 1,
				activation_date =\''.getCurrentDateTimeForDB().'\''.
				'WHERE merchant_row_id = ' . $this->db->escape($merchant_row_id).
				'AND activation_code = ' . $this->db->escape($activationCode);
		$query = $this->db->query($sql);
		return $this->db->affected_rows();
	}
}
?>