<?php
/*This class contains all functions related to affiliate settings*/
  class Admin_affiliate_setup_model extends CI_Model
{

    //constructor
    public function __construct()
    {
        parent::__construct();
    }

    //get an admin users affiliate sign up page settings
    function getAdminAffiliateSignUpPageSetup($admin_user_id)
    {
        $this->db->select('*');
        $this->db->where('admin_user_id', $admin_user_id);
        $this->db->from('tbl_admin_aff_signup_page_setup');
        $res1 = $this->db->get();
        $result = $res1->row_array();
        return $result;
    }

    //get an admin users affiliate sign up page settings using admin user id
    function getAdminSignupInfo($admin_user_id)
    {
        $this->db->select('*');
        $this->db->from('tbl_admin_aff_signup_page_setup');
        $this->db->where('admin_user_id',$admin_user_id);

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

    function checkIfAdminAffSignupEntryExists($admin_user_id)
    {
        $this->db->select('count(*) as CNTENTRY');
        $this->db->from('tbl_admin_aff_signup_page_setup');
        $this->db->where('admin_user_id',$admin_user_id);

        $res = $this->db->get();
        $result = $res->row();
        $count = $result->CNTENTRY;
        if($count>0)
            return true;
        else
            return false;
    }

    function saveAdminAffSignupPageChanges($data)
    {
        $check = $this->checkIfAdminAffSignupEntryExists($data['admin_user_id']);
        if($check)
        {
            $this->db->where('admin_user_id',$data['admin_user_id']);
            $this->db->update('tbl_admin_aff_signup_page_setup',$data);
            return $data['admin_aff_signup_page_setup_row_id'];
        }
        else
        {
            $this->db->insert('tbl_admin_aff_signup_page_setup',$data);
            return $this->db->insert_id();
        }
    }

    function updateAdminAffCssFileName($fileName,$admin_user_id)
    {
        $this->db->set('signup_page_css_file_name',$fileName);
        $this->db->where('admin_user_id',$admin_user_id);
        $this->db->update('tbl_admin_aff_signup_page_setup');
        return;
    }

    function getAdminAffNotificationInfo($admin_user_id)
    {
        $this->db->select('*');
        $this->db->where('admin_user_id', $admin_user_id);
        $this->db->from('tbl_admin_aff_notification_setup');
        $res1 = $this->db->get();
        $result = $res1->row_array();
        return $result;
    }

    function checkIfAdminAffNotificationEntryExists($admin_user_id)
    {
        $this->db->select('count(*) as CNTENTRY');
        $this->db->from('tbl_admin_aff_notification_setup');
        $this->db->where('admin_user_id',$admin_user_id);

        $res = $this->db->get();
        $result = $res->row();
        $count = $result->CNTENTRY;
        if($count>0)
            return true;
        else
            return false;
    }

    function saveAdminAffNotification($data)
    {
        $check = $this->checkIfAdminAffNotificationEntryExists($data['admin_user_id']);
        if($check)
        {
            $this->db->where('admin_user_id',$data['admin_user_id']);
            $this->db->update('tbl_admin_aff_notification_setup',$data);
            return $data['admin_aff_notification_setup_row_id'];
        }
        else
        {
            $this->db->insert('tbl_admin_aff_notification_setup',$data);
            return $this->db->insert_id();
        }
    }

	function restoreDefaultSignupPageSetup($admin_user_id)
	{
			$signupPageInfo = $this->getAdminSignupInfo($admin_user_id);
			$cssFileName = $signupPageInfo['signup_page_css_file_name'];

			@unlink('styles/adminAffiliates/' . $cssFileName);
      @unlink($this->config->item('admin_affiliate_signup_banner_path') . $signupPageInfo['uploaded_banner']);
		 $sql = 'DELETE FROM tbl_admin_aff_signup_page_setup WHERE admin_user_id = '.$admin_user_id;
		 $this->db->query($sql);
	}

    function getAffiliateCommission ($admin_affiliate_row_id)
    {
        if ($admin_affiliate_row_id)
        {
        $sql = "SELECT SUM(sales_commission) AS totalCommission from tbl_admin_sales_commission WHERE admin_affiliate_row_id =$admin_affiliate_row_id  AND is_cancelled = 0";
        $query = $this->db->query($sql);
        return $query->row()->totalCommission;
        }

    }

}
?>