<?php
/*This class contains all functions related to admin affiliate programs*/
class Admin_affiliate_program_setup_model extends CI_Model
{

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

    function saveAdminAffiliateProgram($data)
    {
        if($data['use_for_tell_friend'])
        {
            $sql = "UPDATE tbl_admin_aff_program SET use_for_tell_friend = 0 WHERE admin_user_id = " . $_SESSION['adminUserId'];
            $this->db->query($sql);
        }

        if($data['admin_aff_program_row_id'])
        {
            $data['modified_on'] = getCurrentDateTimeForDB();
            $this->db->where('admin_aff_program_row_id',$data['admin_aff_program_row_id']);
            $this->db->update('tbl_admin_aff_program',$data);
            $admin_aff_program_row_id = $data['admin_aff_program_row_id'];
        }
        else
        {
            $data['created_on'] = getCurrentDateTimeForDB();
            $this->db->insert('tbl_admin_aff_program',$data);
            $admin_aff_program_row_id = $this->db->insert_id();
        }
        return $admin_aff_program_row_id;
    }

    function checkUniqueAdminAffProgramName($data)
    {
        if($data['admin_aff_program_row_id'])
        {
            $presentData = $this->getAdminAffiliateProgramInfo($data['admin_aff_program_row_id']);
            $present_name = $presentData['program_name'];
            $checkData = $this->getAllAffiliateProgramByAdmin($data['admin_user_id']);
            for($i=0;$i<count($checkData);$i++)
            {
                if($checkData[$i]['program_name']!=$present_name)
                    $names[$i] = $checkData[$i]['program_name'];
            }

            if(in_array($data['program_name'],$names))
                return false;
            else
                return true;
        }
        else
        {
            $this->db->select('count(*) as CNTPRGM');
            $this->db->from('tbl_admin_aff_program');
            $this->db->where('program_name',$data['program_name']);
            $this->db->where('admin_user_id',$data['admin_user_id']);
            $this->db->where('remove_flag',0);

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

            if($count>0)
                return false;
            else
                return true;
        }
    }

    function getAdminAffiliateProgramInfo($admin_aff_program_row_id,$admin_user_id = '')
    {
        $this->db->select('*');
        $this->db->from('tbl_admin_aff_program');
        $this->db->where('admin_aff_program_row_id',$admin_aff_program_row_id);
        if($admin_user_id)
        {
            $this->db->where('admin_user_id',$admin_user_id);
        }
        $res = $this->db->get();
        $result = $res->row_array();
        return $result;
    }

    function getAllAffiliateProgramByAdmin($admin_user_id)
    {
        $this->db->select('*');
        $this->db->from('tbl_admin_aff_program');
        $this->db->where('admin_user_id',$admin_user_id);
        $this->db->where('remove_flag',1);
        $res = $this->db->get();
        $result = $res->result_array();
        return $result;
    }

    function getAdminAffProgramInfoByAdminId($admin_user_id, $program_name)
    {
        $this->db->select('*');
        $this->db->from('tbl_admin_aff_program');
        $this->db->where('admin_user_id',$admin_user_id);
        $this->db->where('program_name',$program_name);
        $res = $this->db->get();
        $result = $res->row_array();
        return $result;
    }

    function getAdminAffProgramSpecificLinkInfo($admin_aff_program_row_id)
    {
        $this->db->select('*');
        $this->db->from('tbl_admin_ads_program_specific_affiliate_link');
        $this->db->where('admin_aff_program_row_id',$admin_aff_program_row_id);

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

    function saveAdminProgramSpecificLink($programData)
    {
        $this->db->select('count(*) as CNT');
        $this->db->from('tbl_admin_ads_program_specific_affiliate_link');
        $this->db->where('admin_aff_program_row_id',$programData['admin_aff_program_row_id']);
        $resource = $this->db->get();
        $row = $resource->row();
        $count = $row->CNT;

        if($count>0)
        {
            $this->db->set('admin_user_id',$programData['admin_user_id']);
            $this->db->set('admin_aff_program_row_id',$programData['admin_aff_program_row_id']);
            $this->db->set('generated_link',$programData['generated_link']);
            $this->db->set('destination_url',$programData['destination_url']);
            $this->db->where('admin_aff_program_row_id',$programData['admin_aff_program_row_id']);
            $this->db->update('tbl_admin_ads_program_specific_affiliate_link');
        }
        else
        {

            $programData['created_on'] = getCurrentDateTimeForDB();
            $this->db->insert('tbl_admin_ads_program_specific_affiliate_link',$programData);
        }
    }

    function getAssignedPackageRowIdArray($admin_user_id,$admin_aff_program_row_id)
    {
        $this->db->select('package_id');
        $this->db->from('tbl_admin_aff_program_package');
        $this->db->where('admin_aff_program_row_id',$admin_aff_program_row_id);
        $ress = $this->db->get();
        $result = $ress->result();
        $cnt = count($result);
        if($cnt)
        {
            $lastIndex = $cnt-1;

            for($i = 0; $i < $cnt; $i++)
            {
                $assignedProduct.= $result[$i]->package_id;
                if($i != $lastIndex)
                {
                    $assignedProduct.=',';
                }
            }
            $assignedProduct = '('.$assignedProduct.')';
        }

        $assignedProductRowIdArray = array();

        if($assignedProduct)
        {
            $sql = 'SELECT package_id
                    FROM tbl_package
                    WHERE package_id IN '.$assignedProduct;

            $res = $this->db->query($sql);
            $result = $res->result_array();
            $cnt = count($result);
            for($i = 0; $i < $cnt; $i++)
            {
                $assignedProductRowIdArray[$i] = $result[$i]['package_id'];
            }
        }
        return $assignedProductRowIdArray;

    }

    function getUnassignedPackageRowIdArray($admin_user_id,$admin_aff_program_row_id)
    {
        $this->db->select('package_id');
        $this->db->from('tbl_admin_aff_program_package');
        $this->db->where('admin_aff_program_row_id',$admin_aff_program_row_id);
        $ress = $this->db->get();
        $result = $ress->result();
        if(count($result)>0)
        {
            for($i=0;$i<count($result);$i++)
            {
                $assignedProduct.= $result[$i]->package_id;
                if($i!=(count($result)-1))
                $assignedProduct.=',';
            }
            $assignedProduct = '('.$assignedProduct.')';
        }
        $unassignedProductRowIdArray = array();

        $sql = "SELECT package_id
                FROM tbl_package ";
        if($assignedProduct)
        {
            $sql = $sql.' WHERE package_id NOT IN '.$assignedProduct;
        }
        $res = $this->db->query($sql);
        $result = $res->result_array();
        $cnt = count($result);
        for($i = 0; $i < $cnt; $i++)
        {
            $unassignedProductRowIdArray[$i] = $result[$i]['package_id'];
        }

        return $unassignedProductRowIdArray;

    }

    function showPackageGrid($admin_user_id,$admin_aff_program_row_id)
    {
        $this->db->select('package_id');
        $this->db->from('tbl_admin_aff_program_package');
        $this->db->where('admin_aff_program_row_id',$admin_aff_program_row_id);
        $ress = $this->db->get();
        $result = $ress->result();

        $cnt = count($result);
        if($cnt)
        {
            $lastIndex = $cnt - 1;
            for($i = 0; $i < $cnt; $i++)
            {
                $assignedProduct.= $result[$i]->package_id;
                if($i != $lastIndex)
                {
                    $assignedProduct.=',';
                }
            }

            $sql = '(SELECT     package_id,
                            package_id AS select_package,
                            package_name,
                            package_number,
                            monthly_price,
                            annual_price
                    FROM tbl_package p
                    WHERE p.package_id IN('.$assignedProduct.')
                    AND active_status = 1) AS abc';
        }
        else
        {
            $sql = '( SELECT * FROM tbl_package p WHERE package_id = 0) AS abc';
        }

        $this->load->library ( 'rapyd' );
        $this->rapyd->load('datagrid');
        $grid = new DataGrid('', $sql);

        $grid->showFeaturedBox = TRUE;
        $grid->currentPageNo = $this->config->item('affiliateProgram2');
        $grid->order_by('package_id', 'asc');
        //$grid->per_page = 10;

        $grid->column('Select Package', 'select_package', 'select_package', 'align=\'left\'');
        $grid->column_orderby('Package Name', 'package_name', 'package_name', 'align=\'left\'');
        $grid->column_orderby('Package Number', 'package_number', 'package_number', 'align=\'left\'');
        $grid->column_orderby('Package Monthly Price', '$'.'<#monthly_price#>', 'product_price', 'align=\'left\'');
        $grid->column_orderby('Package Annual Price', '$'.'<#annual_price#>', 'product_price', 'align=\'left\'');
        $grid->build();
        return $grid->output;
    }

    function unassignedPackageGrid($admin_user_id,$admin_aff_program_row_id)
    {
        $this->db->select('package_id');
        $this->db->from('tbl_admin_aff_program_package');
        $this->db->where('admin_aff_program_row_id',$admin_aff_program_row_id);
        $ress = $this->db->get();
        $result = $ress->result();
        //echo $this->db->last_query();
        if(count($result)>0)
        {
            for($i=0;$i<count($result);$i++)
            {
                $assignedProduct.= $result[$i]->package_id;
                if($i!=(count($result)-1))
                $assignedProduct.=',';
            }
        }

        $sql = 'SELECT package_id,
                package_id AS select_package,
                SUBSTRING(package_name,1,30) AS package_name, package_number, monthly_price,annual_price,
                IFNULL((SELECT GROUP_CONCAT(b.program_name SEPARATOR \', \') AS program_nm
                         FROM tbl_admin_aff_program_package AS a JOIN
                         tbl_admin_aff_program AS b ON (a.admin_aff_program_row_id = b.admin_aff_program_row_id)
                         WHERE a.package_id = p.package_id ORDER BY b.admin_aff_program_row_id),
                         (SELECT program_name FROM tbl_admin_aff_program WHERE admin_user_id='.$admin_user_id.' AND is_default=1 )
                     )  AS program_name
                FROM tbl_package p
                WHERE p.active_status=1';

        if(count($result)>0)
        {
            $sql.= ' AND p.package_id NOT IN('.$assignedProduct.')';
        }

        $sql = '('.$sql.' ) AS abc';

        $this->load->library ( 'rapyd' );
        $this->rapyd->load('datagrid');
        $grid = new DataGrid('', $sql);

        $grid->showFeaturedBox = TRUE;
        //$grid->currentPageNo = $this->config->item('unAssignProductList');
        $grid->currentPageNo = 22;
        $grid->order_by('package_id', 'asc');
        $grid->per_page = 10;

        $grid->column('Select Package', 'select_package', 'select_package', 'align=\'left\'');
        $grid->column_orderby('Package Name', '<#package_name#>', 'package_name', 'align=\'left\'');
        $grid->column_orderby('Package Number', 'package_number', 'package_number', 'align=\'left\'');
        $grid->column('Assigned Affiliate Program', 'program_name','align=\'left\'');
        $grid->column_orderby('Package Monthly Price', '$'.'<#monthly_price#>', 'monthly_price', 'align=\'left\'');
        $grid->column_orderby('Package Annual Price', '$'.'<#annual_price#>', 'annual_price', 'align=\'left\'');

        $grid->build();
        return $grid->output;
    }

    function getAdminDefaultAffiliateProgramInfo($admin_user_id)
    {
        $sql = "(SELECT a.admin_aff_program_row_id,
                       a.program_name,
                       (CASE a.use_for_tell_friend
                            WHEN  1 THEN 'YES'
                            ELSE 'NO'
                       END) AS tell_friend,
                       (SELECT SUM(sales_commission)
                               FROM tbl_admin_sales_commission ads
                               WHERE ads.admin_user_id=a.admin_user_id
                               AND ads.admin_aff_program_row_id=a.admin_aff_program_row_id
                               AND a.is_default=1
                               AND a.remove_flag=0)as aff_sales_commission,
                       (SELECT COUNT(admin_sales_commission_row_id      )
                               FROM tbl_admin_sales_commission ads
                               WHERE ads.admin_user_id=a.admin_user_id
                               AND ads.admin_aff_program_row_id=a.admin_aff_program_row_id
                               AND a.is_default=1
                               AND a.remove_flag=0)as aff_sales_count,
                       (SELECT SUM(package_sales_value      )
                               FROM tbl_admin_sales_commission ads
                               WHERE ads.admin_user_id=a.admin_user_id
                               AND ads.admin_aff_program_row_id=a.admin_aff_program_row_id
                               AND a.is_default=1
                               AND a.remove_flag=0)as aff_sales_value
                       FROM tbl_admin_aff_program a
                       WHERE a.admin_user_id=$admin_user_id
                       AND a.is_default=1
                       AND a.remove_flag=0)  ";

        $result = $this->db->query($sql);
        return $result->row_array();
    }
    function getAdminDefaultAffiliateProgramGrid($defaultArray)
    {
        $this->load->library ( 'rapyd' );
        $this->rapyd->load('datagrid');
        $grid = new DataGrid('', $defaultArray);

        $grid->order_by('admin_aff_program_row_id', 'asc');
        $grid->per_page = 10;

        $grid->column_detail('Program Name', 'program_name', 'administration/admin_affiliate_program_setup/createAdminAffiliateProgram/<#admin_aff_program_row_id#>', 'align=\'left\'');
        $grid->column('Total Sales Count','aff_sales_count','aff_sales_count', 'align=\'left\'');
        $grid->column('Total Sales Value','aff_sales_value','aff_sales_value', 'align=\'left\'');
        $grid->column('Total Sales Commission','aff_sales_commission','aff_sales_commission', 'align=\'left\'');
        $grid->column('Use for Tell a Friend','tell_friend','tell_friend', 'align=\'center\'');

        $grid->build();
        return $grid->output;

    }

    function adminAffiliateProgramList($admin_user_id)
    {
         $sql = 'SELECT program_name,
                admin_aff_program_row_id AS program_row_id,
                DATE_FORMAT(created_on,\'' . $_SESSION['date_format'] . '\') AS date,
               "Edit" AS update_program,
               "Remove" AS remove_program,
               (CASE use_for_tell_friend
                    WHEN  1 THEN \'YES\'
                    ELSE \'NO\'
               END) AS tell_friend
                FROM tbl_admin_aff_program
                WHERE admin_user_id = '.$admin_user_id. '
                AND is_default = 0
                AND remove_flag = 0';
        $sql = "(".$sql.") as ABC";

        $this->load->library ( 'rapyd' );
        $this->rapyd->load('datagrid');
        $grid = new DataGrid('', $sql);

        $grid->column_detail('Affiliate Program Name','program_name','administration/admin_affiliate_program_setup/createAdminAffiliateProgram/<#program_row_id#>', 'align="left"',true,'class="block_page"',true,'align="left"');
        $grid->column('Create Date','date','date', 'align="center"');
        $grid->column('Use for Tell a Friend','tell_friend','tell_friend', 'align=\'center\'');
        $grid->column_detail('Action','update_program','administration/admin_affiliate_program_setup/createAdminAffiliateProgram/<#program_row_id#>', 'align="left"', false, 'class="block_page"', true, 'colspan="3"');
        $grid->column_detail('Remove','remove_program','administration/admin_affiliate_program_setup/removeAdminAffProgram/<#program_row_id#>', 'align="left"', false, 'class="block_page" onclick = "return checkDelete();"', false);

        //$grid->column_detail('View Program Stat','view_stat','administration/admin_affiliate_program_setup/viewAdminAffProgramStat/<#program_row_id#>', 'align="left"', false, 'class="submodal-750-500"', false);

        $grid->build();
        return $grid->output;

    }

    function removeAdminAffProgram($admin_aff_program_row_id)
    {
        $program_product_array = $this->getAllPackagesByAdminAffProgram($admin_aff_program_row_id);
        for($i=0;$i<count($program_product_array);$i++)
        {
            $product_row_id_array[$i] = $program_product_array[$i]['package_id'];
        }
        $this->unassignPackage($product_row_id_array,$admin_aff_program_row_id);

        $this->db->set('remove_flag',1);
        $this->db->where('admin_aff_program_row_id',$admin_aff_program_row_id);
        $this->db->update('tbl_admin_aff_program');
    }

    function getAllPackagesByAdminAffProgram($admin_aff_program_row_id)
    {
        $this->db->select('*');
        $this->db->from('tbl_admin_aff_program_package');
        $this->db->where('admin_aff_program_row_id',$admin_aff_program_row_id);

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

    function unassignPackage($product_row_id_array,$admin_aff_program_row_id)
    {
        $sql = 'DELETE FROM tbl_admin_aff_program_package
                            WHERE admin_aff_program_row_id=' . $admin_aff_program_row_id . ' AND package_id IN(' . implode(',', $product_row_id_array) . ')';
        $this->db->query($sql);
    }

    function assignPackage($product_row_id_array,$admin_aff_program_row_id)
    {
        for($i=0;$i<count($product_row_id_array);$i++)
        {
            $sql_values[] = "(" . $admin_aff_program_row_id . ", " . $product_row_id_array[$i]  . ")";
        }

        if($sql_values)
        {
            $sql = 'INSERT INTO tbl_admin_aff_program_package(admin_aff_program_row_id, package_id) VALUES' . implode(',', $sql_values);
            $this->db->query($sql);
        }
    }

     function getAdminMediaCount($admin_user_id)
    {
        /*$this->db->select('count(*) as cntAd');
        $this->db->from('tbl_admin_ads_main');
        $this->db->where('admin_user_id',$admin_user_id);
        $this->db->where('active_status',1);

        $res1 = $this->db->get();
        $result1 = $res1->row_array();*/

        $this->db->select('count(*) as cntTextAd');
        $this->db->from('tbl_admin_ads_text_links');
        $this->db->where('admin_user_id',$admin_user_id);
        $this->db->where('active_status',1);

        $res2 = $this->db->get();
        $result2 = $res2->row_array();

        $this->db->select('count(*) as cntBannerAd');
        $this->db->from('tbl_admin_ads_banner');
        $this->db->where('admin_user_id',$admin_user_id);
        $this->db->where('active_status',1);

        $res5 = $this->db->get();
        $result5 = $res5->row_array();

        $this->db->select('count(*) as cntFlashAd');
        $this->db->from('tbl_admin_flash_ads');
        $this->db->where('admin_user_id',$admin_user_id);
        $this->db->where('active_status',1);

        $res6 = $this->db->get();
        $result6 = $res6->row_array();

        $this->db->select('count(*) as cntPopupAd');
        $this->db->from('tbl_admin_popup_ads');
        $this->db->where('admin_user_id',$admin_user_id);
        $this->db->where('active_status',1);

        $res7 = $this->db->get();
        $result7 = $res7->row_array();

        //$data['adCount'] = $result1['cntAd'];
        $data['textLinkCount'] = $result2['cntTextAd'];
        $data['bannerCount'] = $result5['cntBannerAd'];
        $data['flashAdCount'] = $result6['cntFlashAd'];
        $data['popupCount'] = $result7['cntPopupAd'];
        return $data;
    }

    function showAdminTextAdInfoGrid($data)
    {
        $sql = 'SELECT text_links_ads_title,
                DATE_FORMAT(created_on,\'' . $this->config->item('show_datetime_fmt') . '\') AS created_on,
                text_links_admin_ads_row_id,
                text_links_admin_ads_row_id as select_text_ad,
                "Edit" as edit_ad,
                "Delete" as delete_ad
                FROM tbl_admin_ads_text_links
                WHERE admin_user_id = '.$data['admin_user_id']. '
                AND admin_aff_program_row_id = '.$data['admin_aff_program_row_id']. '
                AND active_status = 1';

        $sql = "(".$sql.") as ABC";

        $this->load->library ( 'rapyd' );
        $this->rapyd->load('datagrid');
        $grid = new DataGrid('', $sql);

        $grid->showFeaturedBox = true;
        $grid->currentPageNo = $this->config->item('textAdGridAssigned');

        $grid->column('Select','select_text_ad','select_text_ad', 'align=\'center\'');
        $grid->column('Text Link Ad Title','text_links_ads_title','text_links_ads_title', 'align=\'center\'');
        $grid->column('Create Date','created_on','created_on', 'align=\'center\'');
        $grid->column_detail('Action','edit_ad','administration/admin_affiliate_program_setup/showAdminCreateTextAd/<#text_links_admin_ads_row_id#>', 'align=\'center\'', false, 'class="submodal-500-280"', true, 'colspan="3"');
        $grid->column_detail('Delete','delete_ad','administration/admin_affiliate_program_setup/deleteAdminTextAd/<#text_links_admin_ads_row_id#>', 'align=\'center\'', false, 'onclick = "return checkDelete();"', false);
        $grid->column_detail('Preview','Preview','administration/admin_affiliate_program_setup/previewAdminTextLinkAd/<#text_links_admin_ads_row_id#>', 'align=\'center\'', false, 'class="submodal-500-280"', false);

        $grid->build();
        return $grid->output;
    }

    function showAdminAssignedTextAdStatGrid($data)
    {
        $sql = 'SELECT text_links_ads_title,
                DATE_FORMAT(created_on,\'' . $this->config->item('show_datetime_fmt') . '\') AS created_on,
                text_links_admin_ads_row_id,
                text_links_admin_ads_row_id as select_text_ad,
                "Edit" as edit_ad,
                "Delete" as delete_ad
                FROM tbl_admin_ads_text_links
                WHERE admin_user_id = '.$data['admin_user_id']. '
                AND admin_aff_program_row_id = '.$data['admin_aff_program_row_id']. '
                AND active_status = 1';

        $sql = "(".$sql.") as ABC";

        $this->load->library ( 'rapyd' );
        $this->rapyd->load('datagrid');
        $grid = new DataGrid('', $sql);

        $grid->showFeaturedBox = true;
        $grid->currentPageNo = $this->config->item('textAdGridAssigned');

        $grid->column('Select','select_text_ad','select_text_ad', 'align=\'center\'');
        $grid->column('Text Link Ad Title','text_links_ads_title','text_links_ads_title', 'align=\'center\'');
        $grid->column('Create Date','created_on','created_on', 'align=\'center\'');
        $grid->column_detail('Action','edit_ad','administration/admin_affiliate_program_setup/showAdminCreateTextAd/<#text_links_admin_ads_row_id#>', 'align=\'center\'', false, 'class="submodal-500-280"', true, 'colspan="3"');
        $grid->column_detail('Delete','delete_ad','administration/admin_affiliate_program_setup/deleteAdminTextAd/<#text_links_admin_ads_row_id#>', 'align=\'center\'', false, 'onclick = "return checkDelete();"', false);
        $grid->column_detail('Preview','Preview','administration/admin_affiliate_program_setup/previewAdminTextLinkAd/<#text_links_admin_ads_row_id#>', 'align=\'center\'', false, 'class="submodal-500-280"', false);

        $grid->build();
        return $grid->output;
    }

    function showAdminUnassignedTextAdGrid($data)
    {
        $sql = 'SELECT text_links_ads_title,
                DATE_FORMAT(created_on,\'' . $this->config->item('show_datetime_fmt') . '\') AS created_on,
                text_links_admin_ads_row_id,
                text_links_admin_ads_row_id AS select_text_ad,
                "Edit" AS edit_ad,
                "Delete" AS delete_ad
                FROM tbl_admin_ads_text_links
                WHERE admin_user_id = '.$data['admin_user_id']. '
                AND admin_aff_program_row_id <> '.$data['admin_aff_program_row_id']. '
                AND active_status = 1';

        $sql = "(".$sql.") as ABC";

        $this->load->library ( 'rapyd' );
        $this->rapyd->load('datagrid');
        $grid = new DataGrid('', $sql);

        $grid->showFeaturedBox = true;
        $grid->currentPageNo = $this->config->item('textAdGridUnassigned');

        $grid->column('Select','select_text_ad','select_text_ad', 'align=\'center\'');
        $grid->column('Text Link Ad Title','text_links_ads_title','text_links_ads_title', 'align=\'center\'');
        $grid->column('Create Date','created_on','created_on', 'align=\'center\'');
        $grid->column_detail('Action','edit_ad','administration/admin_affiliate_program_setup/showAdminCreateTextAd/<#text_links_admin_ads_row_id#>', 'align=\'center\'', false, 'class="submodal-500-280"', true, 'colspan="3"');
        $grid->column_detail('Delete','delete_ad','administration/admin_affiliate_program_setup/deleteAdminTextAd/<#text_links_admin_ads_row_id#>', 'align=\'center\'', false, 'onclick = "return checkDelete();"', false);
        $grid->column_detail('Preview','Preview','administration/admin_affiliate_program_setup/previewAdminTextLinkAd/<#text_links_admin_ads_row_id#>', 'align=\'center\'', false, 'class="submodal-500-280"', false);

        $grid->build();
        return $grid->output;
    }

    function showAdminBannerAdInfoGrid($data)
    {
        $sql = 'SELECT banner_name,
                DATE_FORMAT(created_on,\'' . $this->config->item('show_datetime_fmt') . '\') AS created_on,
                banner_admin_ads_row_id,
                banner_admin_ads_row_id as select_banner_ad,
                "Edit" as edit_ad,
                "Delete" as delete_ad
                FROM tbl_admin_ads_banner
                WHERE admin_user_id = '.$data['admin_user_id']. '
                AND admin_aff_program_row_id = '.$data['admin_aff_program_row_id']. '
                AND active_status = 1';

        $sql = "(".$sql.") as ABC";

        $this->load->library ( 'rapyd' );
        $this->rapyd->load('datagrid');
        $grid = new DataGrid('', $sql);

        $grid->showFeaturedBox = true;
        $grid->currentPageNo = $this->config->item('bannerAdGridAssigned');

        $grid->column('Select','select_banner_ad','select_banner_ad', 'align=\'center\'');
        $grid->column('Banner Name','banner_name','banner_name', 'align=\'center\'');
        $grid->column('Create Date','created_on','created_on', 'align=\'center\'');
        $grid->column_detail('Action','edit_ad','administration/admin_affiliate_program_setup/showAdminCreateBannerAd/<#banner_admin_ads_row_id#>', 'align=\'center\'', false, 'class="submodal-600-340"', true, 'colspan="3"');
        $grid->column_detail('Delete','delete_ad','administration/admin_affiliate_program_setup/deleteAdminBannerAd/<#banner_admin_ads_row_id#>', 'align=\'center\'', false, 'onclick = "return checkDelete();"', false);
        $grid->column_detail('Preview','Preview','administration/admin_affiliate_program_setup/previewAdminBannerAd/<#banner_admin_ads_row_id#>', 'align=\'center\'', false, 'class="submodal-800-400"', false);

        $grid->build();
        return $grid->output;
    }
    function showAdminAssignedBannerAdStatGrid($data)
    {
        $sql = 'SELECT banner_name,
                DATE_FORMAT(created_on,\'' . $this->config->item('show_datetime_fmt') . '\') AS created_on,
                banner_admin_ads_row_id,
                banner_admin_ads_row_id as select_banner_ad,
                "Edit" as edit_ad,
                "Delete" as delete_ad
                FROM tbl_admin_ads_banner
                WHERE admin_user_id = '.$data['admin_user_id']. '
                AND admin_aff_program_row_id = '.$data['admin_aff_program_row_id']. '
                AND active_status = 1';

        $sql = "(".$sql.") as ABC";

        $this->load->library ( 'rapyd' );
        $this->rapyd->load('datagrid');
        $grid = new DataGrid('', $sql);

        $grid->showFeaturedBox = true;
        $grid->currentPageNo = $this->config->item('bannerAdGridAssigned');

        $grid->column('Select','select_banner_ad','select_banner_ad', 'align=\'center\'');
        $grid->column('Banner Name','banner_name','banner_name', 'align=\'center\'');
        $grid->column('Create Date','created_on','created_on', 'align=\'center\'');
        $grid->column_detail('Action','edit_ad','administration/admin_affiliate_program_setup/showAdminCreateBannerAd/<#banner_admin_ads_row_id#>', 'align=\'center\'', false, 'class="submodal-600-340"', true, 'colspan="3"');
        $grid->column_detail('Delete','delete_ad','administration/admin_affiliate_program_setup/deleteAdminBannerAd/<#banner_admin_ads_row_id#>', 'align=\'center\'', false, 'onclick = "return checkDelete();"', false);
        $grid->column_detail('Preview','Preview','administration/admin_affiliate_program_setup/previewAdminBannerAd/<#banner_admin_ads_row_id#>', 'align=\'center\'', false, 'class="submodal-800-400"', false);

        $grid->build();
        return $grid->output;
    }
    function showAdminUnassignedBannerAdGrid($data)
    {
        $sql = 'SELECT banner_name,
                DATE_FORMAT(created_on,\'' . $this->config->item('show_datetime_fmt') . '\') AS created_on,
                banner_admin_ads_row_id,
                banner_admin_ads_row_id as select_banner_ad,
                "Edit" as edit_ad,
                "Delete" as delete_ad
                FROM tbl_admin_ads_banner
                WHERE admin_user_id = '.$data['admin_user_id']. '
                AND admin_aff_program_row_id <> '.$data['admin_aff_program_row_id']. '
                AND active_status = 1';

        $sql = "(".$sql.") as ABC";

        $this->load->library ( 'rapyd' );
        $this->rapyd->load('datagrid');
        $grid = new DataGrid('', $sql);

        $grid->showFeaturedBox = true;
        $grid->currentPageNo = $this->config->item('bannerAdGridUnassigned');

        $grid->column('Select','select_banner_ad','select_banner_ad', 'align=\'center\'');
        $grid->column('Banner Name','banner_name','banner_name', 'align=\'center\'');
        $grid->column('Create Date','created_on','created_on', 'align=\'center\'');
        $grid->column_detail('Action','edit_ad','administration/admin_affiliate_program_setup/showAdminCreateBannerAd/<#banner_admin_ads_row_id#>', 'align=\'center\'', false, 'class="submodal-600-340"', true, 'colspan="3"');
        $grid->column_detail('Delete','delete_ad','administration/admin_affiliate_program_setup/deleteAdminBannerAd/<#banner_admin_ads_row_id#>', 'align=\'center\'', false, 'onclick = "return checkDelete();"', false);
        $grid->column_detail('Preview','Preview','administration/admin_affiliate_program_setup/previewAdminBannerAd/<#banner_admin_ads_row_id#>', 'align=\'center\'', false, 'class="submodal-800-400"', false);

        $grid->build();
        return $grid->output;
    }

    function showAdminPopupAdInfoGrid($data)
    {
        $sql = 'SELECT popup_ads_title,
                DATE_FORMAT(created_on,\'' .  $this->config->item('show_datetime_fmt') . '\') AS created_on,
                popup_admin_ads_row_id,
                popup_admin_ads_row_id as select_popup_ad,
                "Edit" as edit_ad,
                "Delete" as delete_ad
                FROM tbl_admin_popup_ads
                WHERE admin_user_id = '.$data['admin_user_id']. '
                AND admin_aff_program_row_id = '.$data['admin_aff_program_row_id']. '
                AND active_status = 1';
        $sql = "(".$sql.") as ABC";

        $this->load->library ( 'rapyd' );
        $this->rapyd->load('datagrid');
        $grid = new DataGrid('', $sql);

        $grid->showFeaturedBox = true;
        $grid->currentPageNo = $this->config->item('popupAdGridAssigned');

        $grid->column('Select','select_popup_ad','select_popup_ad', 'align=\'center\'');
        $grid->column('Popup Ad Title','popup_ads_title','popup_ads_title', 'align=\'center\'');
        $grid->column('Create Date','created_on','created_on', 'align=\'center\'');
        $grid->column_detail('Action','edit_ad','administration/admin_affiliate_program_setup/showAdminCreatePopupAd/<#popup_admin_ads_row_id#>', 'align=\'center\'', false, 'class="submodal-800-550"', true, 'colspan="3"');
        $grid->column_detail('Delete','delete_ad','administration/admin_affiliate_program_setup/deleteAdminPopupAd/<#popup_admin_ads_row_id#>', 'align=\'center\'', false, 'onclick = "return checkDelete();"', false);
        $grid->column_detail('Preview','Preview','administration/admin_affiliate_program_setup/previewAdminPopupAd/<#popup_admin_ads_row_id#>', 'align=\'center\'', false, 'class="submodal-800-550"', false);

        $grid->build();
        return $grid->output;
    }

    function showAdminAssignedPopupAdStatGrid($data)
    {
        $sql = 'SELECT popup_ads_title,
                DATE_FORMAT(created_on,\'' .  $this->config->item('show_datetime_fmt') . '\') AS created_on,
                popup_admin_ads_row_id,
                popup_admin_ads_row_id as select_popup_ad,
                "Edit" as edit_ad,
                "Delete" as delete_ad
                FROM tbl_admin_popup_ads
                WHERE admin_user_id = '.$data['admin_user_id']. '
                AND admin_aff_program_row_id = '.$data['admin_aff_program_row_id']. '
                AND active_status = 1';
        $sql = "(".$sql.") as ABC";

        $this->load->library ( 'rapyd' );
        $this->rapyd->load('datagrid');
        $grid = new DataGrid('', $sql);

        $grid->showFeaturedBox = true;
        $grid->currentPageNo = $this->config->item('popupAdGridAssigned');

        $grid->column('Select','select_popup_ad','select_popup_ad', 'align=\'center\'');
        $grid->column('Popup Ad Title','popup_ads_title','popup_ads_title', 'align=\'center\'');
        $grid->column('Create Date','created_on','created_on', 'align=\'center\'');
        $grid->column_detail('Action','edit_ad','administration/admin_affiliate_program_setup/showAdminCreatePopupAd/<#popup_admin_ads_row_id#>', 'align=\'center\'', false, 'class="submodal-800-550"', true, 'colspan="3"');
        $grid->column_detail('Delete','delete_ad','administration/admin_affiliate_program_setup/deleteAdminPopupAd/<#popup_admin_ads_row_id#>', 'align=\'center\'', false, 'onclick = "return checkDelete();"', false);
        $grid->column_detail('Preview','Preview','administration/admin_affiliate_program_setup/previewAdminPopupAd/<#popup_admin_ads_row_id#>', 'align=\'center\'', false, 'class="submodal-800-550"', false);

        $grid->build();
        return $grid->output;
    }

    function showAdminUnassignedPopupAdGrid($data)
    {
        $sql = 'SELECT popup_ads_title,
                DATE_FORMAT(created_on,\'' .  $this->config->item('show_datetime_fmt') . '\') AS created_on,
                popup_admin_ads_row_id,
                popup_admin_ads_row_id as select_popup_ad,
                "Edit" as edit_ad,
                "Delete" as delete_ad
                FROM tbl_admin_popup_ads
                WHERE admin_user_id = '.$data['admin_user_id']. '
                AND admin_aff_program_row_id <> '.$data['admin_aff_program_row_id']. '
                AND active_status = 1';

        $sql = "(".$sql.") as ABC";

        $this->load->library ( 'rapyd' );
        $this->rapyd->load('datagrid');
        $grid = new DataGrid('', $sql);

        $grid->showFeaturedBox = true;
        $grid->currentPageNo = $this->config->item('popupAdGridUnassigned');

        $grid->column('Select','select_popup_ad','select_popup_ad', 'align=\'center\'');
        $grid->column('Popup Ad Title','popup_ads_title','popup_ads_title', 'align=\'center\'');
        $grid->column('Create Date','created_on','created_on', 'align=\'center\'');
        $grid->column_detail('Action','edit_ad','administration/admin_affiliate_program_setup/showAdminCreatePopupAd/<#popup_admin_ads_row_id#>', 'align=\'center\'', false, 'class="submodal-800-550"', true, 'colspan="3"');
        $grid->column_detail('Delete','delete_ad','administration/admin_affiliate_program_setup/deleteAdminPopupAd/<#popup_admin_ads_row_id#>', 'align=\'center\'', false, 'onclick = "return checkDelete();"', false);
        $grid->column_detail('Preview','Preview','administration/admin_affiliate_program_setup/previewAdminPopupAd/<#popup_admin_ads_row_id#>', 'align=\'center\'', false, 'class="submodal-800-550"', false);

        $grid->build();
        return $grid->output;
    }

    function showAdminFlashAdInfoGrid($data)
    {
        $sql = 'SELECT flash_file_name,
                       DATE_FORMAT(created_on,\'' .  $this->config->item('show_datetime_fmt') . '\') AS created_on,
                       flash_admin_ads_row_id,
                       flash_admin_ads_row_id as select_flash_ad,
                       "Edit" as edit_ad,
                       "Delete" as delete_ad
                FROM tbl_admin_flash_ads
                WHERE admin_user_id = '.$data['admin_user_id']. '
                AND admin_aff_program_row_id = '.$data['admin_aff_program_row_id']. '
                AND active_status = 1';

        $sql = "(".$sql.") as ABC";

        $this->load->library ( 'rapyd' );
        $this->rapyd->load('datagrid');
        $grid = new DataGrid('', $sql);

        $grid->showFeaturedBox = true;
        $grid->currentPageNo = $this->config->item('flashAdGridAssigned');

        $grid->column('Select','select_flash_ad','select_flash_ad', 'align=\'left\'');
        $grid->column('Flash File Name','flash_file_name','flash_file_name', 'align=\'left\'');
        $grid->column('Create Date','created_on','created_on', 'align=\'left\'');
        $grid->column_detail('Action','edit_ad','administration/admin_affiliate_program_setup/showAdminCreateFlashAd/<#flash_admin_ads_row_id#>', 'align=\'left\'', false, 'class="submodal-640-325"', true, 'colspan="3"');
        $grid->column_detail('Delete','delete_ad','administration/admin_affiliate_program_setup/deleteAdminFlashAd/<#flash_admin_ads_row_id#>', 'align=\'left\'', false, 'onclick = "return checkDelete();"', false);
        $grid->column_detail('Preview','Preview','administration/admin_affiliate_program_setup/previewAdminFlashAd/<#flash_admin_ads_row_id#>', 'align=\'left\'', false, 'class="submodal-640-325"', false);

        $grid->build();
        return $grid->output;
    }

    function showAdminAssignedFlashAdStatGrid($data)
    {
        $sql = 'SELECT flash_file_name,
                       DATE_FORMAT(created_on,\'' .  $this->config->item('show_datetime_fmt') . '\') AS created_on,
                       flash_admin_ads_row_id,
                       flash_admin_ads_row_id as select_flash_ad,
                       "Edit" as edit_ad,
                       "Delete" as delete_ad
                FROM tbl_admin_flash_ads
                WHERE admin_user_id = '.$data['admin_user_id']. '
                AND admin_aff_program_row_id = '.$data['admin_aff_program_row_id']. '
                AND active_status = 1';

        $sql = "(".$sql.") as ABC";

        $this->load->library ( 'rapyd' );
        $this->rapyd->load('datagrid');
        $grid = new DataGrid('', $sql);

        $grid->showFeaturedBox = true;
        $grid->currentPageNo = $this->config->item('flashAdGridAssigned');

        $grid->column('Select','select_flash_ad','select_flash_ad', 'align=\'left\'');
        $grid->column('Flash File Name','flash_file_name','flash_file_name', 'align=\'left\'');
        $grid->column('Create Date','created_on','created_on', 'align=\'left\'');
        $grid->column_detail('Action','edit_ad','administration/admin_affiliate_program_setup/showAdminCreateFlashAd/<#flash_admin_ads_row_id#>', 'align=\'left\'', false, 'class="submodal-640-325"', true, 'colspan="3"');
        $grid->column_detail('Delete','delete_ad','administration/admin_affiliate_program_setup/deleteAdminFlashAd/<#flash_admin_ads_row_id#>', 'align=\'left\'', false, 'onclick = "return checkDelete();"', false);
        $grid->column_detail('Preview','Preview','administration/admin_affiliate_program_setup/previewAdminFlashAd/<#flash_admin_ads_row_id#>', 'align=\'left\'', false, 'class="submodal-640-325"', false);

        $grid->build();
        return $grid->output;
    }

    function showAdminUnassignedFlashAdGrid($data)
    {
        $sql = 'SELECT flash_file_name,
                       DATE_FORMAT(created_on,\'' .  $_SESSION['date_format'] . '\') AS created_on,
                       flash_admin_ads_row_id,
                       flash_admin_ads_row_id as select_flash_ad,
                       "Edit" AS edit_ad,
                       "Delete" AS delete_ad
                FROM tbl_admin_flash_ads
                WHERE admin_user_id = '.$data['admin_user_id']. '
                AND admin_aff_program_row_id <> '.$data['admin_aff_program_row_id']. '
                AND active_status = 1';


        $sql = "(".$sql.") as ABC";

        $this->load->library ( 'rapyd' );
        $this->rapyd->load('datagrid');
        $grid = new DataGrid('', $sql);

        $grid->showFeaturedBox = true;
        $grid->currentPageNo = $this->config->item('flashAdGridUnassigned');

        $grid->column('Select','select_flash_ad','select_flash_ad', 'align=\'left\'');
        $grid->column('Flash File Name','flash_file_name','flash_file_name', 'align=\'left\'');
        $grid->column('Create Date','created_on','created_on', 'align=\'left\'');
        $grid->column_detail('Action','edit_ad','administration/admin_affiliate_program_setup/showAdminCreateFlashAd/<#flash_admin_ads_row_id#>', 'align=\'left\'', false, 'class="submodal-640-325"', true, 'colspan="3"');
        $grid->column_detail('Delete','delete_ad','administration/admin_affiliate_program_setup/deleteAdminFlashAd/<#flash_admin_ads_row_id#>', 'align=\'left\'', false, 'onclick = "return checkDelete();"', false);
        $grid->column_detail('Preview','Preview','administration/admin_affiliate_program_setup/previewAdminFlashAd/<#flash_admin_ads_row_id#>', 'align=\'left\'', false, 'class="submodal-640-325"', false);

        $grid->build();
        return $grid->output;
    }
    function saveAdminTextAd($data)
    {
        $this->db->insert('tbl_admin_ads_text_links',$data);
        $text_links_admin_ads_row_id = $this->db->insert_id();
        return $text_links_admin_ads_row_id;
    }
    function getAdminMediaInfo($media_table_name, $media_row_id_name, $media_row_id)
    {
        $this->db->select('*');
        $this->db->from('`'.$media_table_name.'`');
        $this->db->where('`'.$media_row_id_name.'`',$media_row_id);
        $this->db->where('active_status',1);

        $res = $this->db->get();
        $result = $res->row_array();
        return $result;
    }
    function updateAdminMediaInfo($data,$Adata)
    {
        $this->db->where('`'.$Adata['media_row_id_name'].'`',$Adata['media_row_id']);
        $this->db->update('`'.$Adata['media_table_name'].'`',$data);
    }
    function deleteAdminMediaInfo($media_table_name, $media_row_id_name, $media_row_id)
    {
        $sql = "UPDATE `".$media_table_name."` SET active_status=0 WHERE `".$media_row_id_name."`=".$media_row_id;
        $this->db->query($sql);
    }

    function saveAdminBannerAd($data)
    {
        $this->db->insert('tbl_admin_ads_banner',$data);
        $banner_admin_ads_row_id = $this->db->insert_id();
        return $banner_admin_ads_row_id;
    }

    function saveAdminPopupAd($data)
    {
        $this->db->insert('tbl_admin_popup_ads',$data);
        $popup_admin_ads_row_id = $this->db->insert_id();
        return $popup_admin_ads_row_id;
    }

    function saveAdminFlashAd($data)
    {
        $this->db->insert('tbl_admin_flash_ads',$data);
        $flash_admin_ads_row_id = $this->db->insert_id();
        return $flash_admin_ads_row_id;
    }

    function unAssignAdminAdToProgram($data)
    {
        for($i=0;$i<count($data['ad_row_id_array']);$i++)
        {
            $sql = "UPDATE `".$data['table_name']."` SET admin_aff_program_row_id = 0 WHERE `".$data['row_id_name']."` = ".$data['ad_row_id_array'][$i];
            $this->db->query($sql);
        }
    }
    function assignAdminAdToProgram($data)
    {
        for($i=0;$i<count($data['ad_row_id_array']);$i++)
        {
            $sql = "UPDATE `".$data['table_name']."` SET admin_aff_program_row_id = ".$data['admin_aff_program_row_id']." WHERE `".$data['row_id_name']."` = ".$data['ad_row_id_array'][$i];

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

    function updateAdminTextAdLink($link,$text_links_admin_ads_row_id)
    {
        $this->db->set('generated_link',$link);
        $this->db->where('text_links_admin_ads_row_id',$text_links_admin_ads_row_id);
        $this->db->update('tbl_admin_ads_text_links');
    }
    function updateAdminBannerAdLink($link_imp,$link_clk,$banner_admin_ads_row_id)
    {
        $this->db->set('generated_link_imp',$link_imp);
        $this->db->set('generated_link_clk',$link_clk);
        $this->db->where('banner_admin_ads_row_id',$banner_admin_ads_row_id);
        $this->db->update('tbl_admin_ads_banner');
    }
    function updateAdminPopupAdLink($link,$popup_admin_ads_row_id)
    {
        $this->db->set('generated_link',$link);
        $this->db->where('popup_admin_ads_row_id',$popup_admin_ads_row_id);
        $this->db->update('tbl_admin_popup_ads');
    }
    function updateAdminFlashAdLink($link_imp,$link_clk,$flash_admin_ads_row_id)
    {
        $this->db->set('generated_link_imp',$link_imp);
        $this->db->set('generated_link_clk',$link_clk);
        $this->db->where('flash_admin_ads_row_id',$flash_admin_ads_row_id);
        $this->db->update('tbl_admin_flash_ads');
    }

    function trackAdminClickInfo($data, $timeLimit)
    {
        $sql =" SELECT COUNT(admin_track_click_temp_row_id) as CNT
                FROM tbl_admin_track_click_temp
                WHERE unique_click_hash_key='".$data['unique_click_hash_key']."'
                 AND DATE_ADD(last_unique_click_time, INTERVAL ".$timeLimit." MINUTE) > '".$data['last_unique_click_time'] ."'";
        $resultSet = $this->db->query($sql);
        $result = $resultSet->row_array();
        return $result['CNT'];
    }

    function saveAdminClickInfo($data)
    {
        $this->db->insert('tbl_admin_track_click',$data);
    }

    function saveAdminTempClickTrackInfo($data)
    {
        $sql =" SELECT admin_track_click_temp_row_id
                FROM tbl_admin_track_click_temp
                WHERE unique_click_hash_key='".$data['unique_click_hash_key']."' ";
        //echo $sql;
        //die();
        $resultSet = $this->db->query($sql);
        $result = $resultSet->row_array();
        if($result['admin_track_click_temp_row_id'])
        {
            $this->db->set('last_unique_click_time', $data['last_unique_click_time']);
            $this->db->where('admin_track_click_temp_row_id', $result['admin_track_click_temp_row_id']);
            $this->db->update('tbl_admin_track_click_temp');
        }
        else
        {
            $this->db->insert('tbl_admin_track_click_temp', $data);
        }

    }

    function getAdminAdInfo($ad_table_name,$row_id_name,$enc_ad_row_id)
    {
        $sql = " SELECT * FROM `".$ad_table_name."` WHERE MD5(".$row_id_name.")='".$enc_ad_row_id."'";
        $resource = $this->db->query($sql);
        $result = $resource->row_array();
        return $result;
    }

    function saveAdminAffiliateCommission($merchant_row_id, $package_id)
    {
        $getInstance = &get_instance();
        $getInstance->load->model('merchant/Merchant_model', 'merchantModel', TRUE);
        $merchantInfo = $getInstance->merchantModel->getMerchant($merchant_row_id);
        //dumpVar($merchantInfo);
        $admin_user_id = $merchantInfo['admin_id'];

        $this->load->helper('cookie');
        $this->load->config('superAdmin_config');
        $cookieString = 'admin_affiliate_cookie_'.md5($admin_user_id);

        if($this->input->cookie($cookieString))
        {
            $affiliate_cookie = unserialize(stripslashes($this->input->cookie($cookieString)));
            $enc_affiliate_row_id = $affiliate_cookie[0];
            $affiliate_mediaInfoArray = explode(',',$affiliate_cookie[1]);
            $mediaInfo = explode('_',$affiliate_mediaInfoArray[0]);
            $adType = $mediaInfo[0];
            $enc_ad_row_id = $mediaInfo[1];

            $data = $this->getAdminMediaInformation($adType,$enc_ad_row_id);
            $data['admin_user_id'] = $admin_user_id;

            /***affiliate level commission***/

            $getInstance->load->model('administration/Admin_affiliate_signup_model', 'admin_affiliate_signup_model', TRUE);
            $affiliateInfo = $getInstance->admin_affiliate_signup_model->getAdminAffDetailsByEncAffRowId($enc_affiliate_row_id);
            $checkAffiliateStatus = $getInstance->admin_affiliate_signup_model->checkAdminAffiliateStatus($data['admin_affiliate_row_id']);
            //$affiliate_setup_first_level_commission = $affiliateInfo->first_level_commision;
            //$affiliate_setup_second_level_commission = $affiliateInfo->second_level_commision;

            $data['admin_affiliate_row_id'] = $affiliateInfo['admin_affiliate_row_id'];
            $data['merchant_row_id'] = $merchant_row_id;
            $data['package_id'] = $package_id;
            $data['created_on'] = getCurrentDateTimeForDB();

            if($checkAffiliateStatus)
            {
                $getInstance->load->model('administration/Package_model', 'packageModel', TRUE);
                $packageInfo =  $getInstance->packageModel->getPackage($package_id);
                $merchantPackageInfo = $getInstance->packageModel->getMerchantPackageInfo($package_id, $merchant_row_id);
                $programDetails = $this->getAdminAffiliateProgramInfo($data['admin_aff_program_row_id'], $admin_user_id);
                $data['subscription_type'] = $merchantPackageInfo['subscription_type'];
                if($merchantPackageInfo['subscription_type'] == $this->config->item('monthly_subcription'))
                {
                    $data['package_sales_value'] = $packageInfo['monthly_price'];

                }
                else if($merchantPackageInfo['subscription_type'] == $this->config->item('annual_subcription'))
                {
                    $data['package_sales_value']  = $packageInfo['annual_price'];
                }
                else
                {
                   //trial account
                   $data['package_sales_value']  = $packageInfo['trial_amount'];
                }

            $number_of_merchant = $getInstance->admin_affiliate_signup_model->getNumberOfMerachant($data['admin_affiliate_row_id']);
            if($number_of_merchant <=1)
            $commission_in_percentage = $this->config->item('uptoonereferral');
            else if($number_of_merchant >=2 and $number_of_merchant <=5)
            $commission_in_percentage = $this->config->item('twotofivereferral');
            else if($number_of_merchant >=6 and $number_of_merchant <=10)
            $commission_in_percentage = $this->config->item('sixtotenreferral');
            else
            $commission_in_percentage = $this->config->item('tenplusreferral');

            $data['sales_commission']  =  (($data['package_sales_value'] * $commission_in_percentage )/100);

            $data['affiliate_level'] = 1;
            $data['is_recurring'] = 1;
            //dumpVar($data);
            $this->db->insert('tbl_admin_sales_commission',$data);
            $admin_sales_commission_row_id = $this->db->insert_id();

            }
        }
    }

     function getAdminAffiliatePrograms($admin_user_id)
    {
        $this->db->select('*');
        $this->db->from('tbl_admin_aff_program');
        $this->db->where('admin_user_id',$admin_user_id);
        $this->db->where('remove_flag',0);
        $res = $this->db->get();
        $result = $res->result_array();
        return $result;
    }
    function getAdminTotalAdCountByAffProgram($admin_aff_program_row_id,$admin_user_id)
    {

        $sql3 = "SELECT count(banner_admin_ads_row_id) as bannerCount
                FROM tbl_admin_ads_banner
                WHERE active_status = 1
                AND admin_aff_program_row_id = ".$admin_aff_program_row_id;
        $resource3 = $this->db->query($sql3);
        $result3 = $resource3->row();
        $bannerCount = $result3->bannerCount;

        $sql4 = "SELECT count(text_links_admin_ads_row_id) as linkCount
                FROM tbl_admin_ads_text_links
                WHERE active_status = 1
                AND admin_aff_program_row_id = ".$admin_aff_program_row_id;
        $resource4 = $this->db->query($sql4);
        $result4 = $resource4->row();
        $linkCount = $result4->linkCount;

        $sql6 = "SELECT count(popup_admin_ads_row_id) as popCount
                FROM tbl_admin_popup_ads
                WHERE active_status = 1
                AND admin_aff_program_row_id = ".$admin_aff_program_row_id;
        $resource6 = $this->db->query($sql6);
        $result6 = $resource6->row();
        $popCount = $result6->popCount;

        $sql7 = "SELECT count(flash_admin_ads_row_id) as flashCount
                FROM tbl_admin_flash_ads
                WHERE active_status = 1
                AND admin_aff_program_row_id = ".$admin_aff_program_row_id;
        $resource7 = $this->db->query($sql7);
        $result7 = $resource7->row();
        $flashCount = $result7->flashCount;

        $c=0;$l=0;$s=0;

        $getInstance = &get_instance();
        $getInstance->load->model('affiliateAdmin/Affiliate_Setup_model', 'Affiliate_Setup_model', true);
        $programInformation = $this->getAdminAffiliateProgramInfo($admin_aff_program_row_id,$admin_user_id);

        if($programInformation['first_level_commission']>0)
        {
            $s++;
        }
        $type = "";
        if($s>0)
        {
            $type.= ' S ';
        }
        $total_ads_of_this_program = $bannerCount + $flashCount + $popCount + $linkCount;
        $data['total_ads_of_this_program'] = $total_ads_of_this_program;
        $data['type'] = $type;
        return $data;
    }

    function getAdminAffiliateWiseProgramOrder($admin_affiliate_row_id)
    {
        $this->db->select('affiliate_program_order');
        $this->db->from('tbl_admin_affiliate_program_order');
        $this->db->where('admin_affiliate_row_id',$admin_affiliate_row_id);
        $res = $this->db->get();
        $result = $res->row_array();
        return $result['affiliate_program_order'];
    }

    function saveAdminAffiliateWiseProgramOrder($data)
    {
        $orderExists = $this->checkIfAffiliateProgramOrderExists($data['admin_affiliate_row_id']);
        //exists, so update the existing order list
        if($orderExists)
        {
            $this->db->where('admin_affiliate_row_id', $data['admin_affiliate_row_id']);
            return $this->db->update('tbl_admin_affiliate_program_order', $data);
        }
        // not exists, insert an order list in the db
        else
        {
            $this->db->insert('tbl_admin_affiliate_program_order', $data);
            return $this->db->insert_id();
        }
    }

    function checkIfAffiliateProgramOrderExists($admin_affiliate_row_id)
    {
        $this->db->select('COUNT(admin_affiliate_row_id) as total');
        $this->db->from('tbl_admin_affiliate_program_order');
        $this->db->where('admin_affiliate_row_id',$admin_affiliate_row_id);
        $res = $this->db->get();
        $result = $res->row_array();
        return $result['total'];
    }

    /*function getAdminAffiliateDetailsByAffRowId($admin_affiliate_row_id)
    {
        $this->db->select('*');
        $this->db->from('tbl_admin_affiliate');
        $this->db->where('admin_affiliate_row_id',$admin_affiliate_row_id);

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

        if(is_array($result))
        {
            if(trim($result['affiliate_tax_id']))
            {
                $result['affiliate_tax_id'] = decrypt($result['affiliate_tax_id']);
            }
        }
        return $result;
    }

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

    function getAdminDefaultAffiliateProgram($admin_user_id)
    {
        $this->db->select('*');
        $this->db->from('tbl_admin_aff_program');
        $this->db->where('admin_user_id',$admin_user_id);
        $this->db->where('is_default',1);
        $res = $this->db->get();
        $result = $res->row_array();
        return $result;
    }
    function getAllAdminAffiliateProgramByAdmin($admin_user_id)
    {
        $this->db->select('*');
        $this->db->from('tbl_admin_aff_program');
        $this->db->where('admin_user_id',$admin_user_id);
        $this->db->where('remove_flag',0);
        $res = $this->db->get();
        $result = $res->result_array();
        return $result;
    }
    function getAdminMediacountForAffPanel($admin_user_id,$admin_aff_program_row_id)
    {

        $this->db->select('count(*) as cntTextAd');
        $this->db->from('tbl_admin_ads_text_links');
        $this->db->where('admin_user_id',$admin_user_id);
        $this->db->where('admin_aff_program_row_id',$admin_aff_program_row_id);
        $this->db->where('active_status',1);

        $res2 = $this->db->get();
        $result2 = $res2->row_array();

        $this->db->select('count(*) as cntBannerAd');
        $this->db->from('tbl_admin_ads_banner');
        $this->db->where('admin_user_id',$admin_user_id);
        $this->db->where('admin_aff_program_row_id',$admin_aff_program_row_id);
        $this->db->where('active_status',1);

        $res5 = $this->db->get();
        $result5 = $res5->row_array();

        $this->db->select('count(*) as cntFlashAd');
        $this->db->from('tbl_admin_flash_ads');
        $this->db->where('admin_user_id',$admin_user_id);
        $this->db->where('admin_aff_program_row_id',$admin_aff_program_row_id);
        $this->db->where('active_status',1);

        $res6 = $this->db->get();
        $result6 = $res6->row_array();

        $this->db->select('count(*) as cntPopupAd');
        $this->db->from('tbl_admin_popup_ads');
        $this->db->where('admin_user_id',$admin_user_id);
        $this->db->where('admin_aff_program_row_id',$admin_aff_program_row_id);
        $this->db->where('active_status',1);

        $res7 = $this->db->get();
        $result7 = $res7->row_array();

        $data['textLinkCount'] = $result2['cntTextAd'];
        $data['bannerCount'] = $result5['cntBannerAd'];
        $data['flashAdCount'] = $result6['cntFlashAd'];
        $data['popupCount'] = $result7['cntPopupAd'];

        return $data;
    }

    function getAdminProgramSpecificLinkInfo($admin_aff_program_row_id)
    {
        $this->db->select('*');
        $this->db->from('tbl_admin_ads_program_specific_affiliate_link');
        $this->db->where('admin_aff_program_row_id',$admin_aff_program_row_id);

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

    function getAdminLinksAdDetails($admin_user_id,$admin_aff_program_row_id)
    {
        $this->db->select('*');
        $this->db->from('tbl_admin_ads_text_links ');
        $this->db->where('admin_user_id',$admin_user_id);
        $this->db->where('admin_aff_program_row_id',$admin_aff_program_row_id);
        $this->db->where('active_status',1);

        $resultSet = $this->db->get();
        return $resultSet->result_array();
    }

    function getAdminBannerAdDetails($admin_user_id,$admin_aff_program_row_id)
    {
        $this->db->select('*');
        $this->db->from('tbl_admin_ads_banner');
        $this->db->where('admin_user_id',$admin_user_id);
        $this->db->where('admin_aff_program_row_id',$admin_aff_program_row_id);
        $this->db->where('active_status',1);

        $resultSet = $this->db->get();
        return $resultSet->result_array();
    }

    function getAdminPopupAdDetails($admin_user_id,$admin_aff_program_row_id)
    {
        $this->db->select('*');
        $this->db->from('tbl_admin_popup_ads');
        $this->db->where('admin_user_id',$admin_user_id);
        $this->db->where('admin_aff_program_row_id',$admin_aff_program_row_id);
        $this->db->where('active_status',1);

        $resultSet = $this->db->get();
        return $resultSet->result_array();
    }

    function getAdminFlashAdDetails($admin_user_id,$admin_aff_program_row_id)
    {
        $this->db->select('*');
        $this->db->from('tbl_admin_flash_ads');
        $this->db->where('admin_user_id',$admin_user_id);
        $this->db->where('admin_aff_program_row_id',$admin_aff_program_row_id);
        $this->db->where('active_status',1);

        $resultSet = $this->db->get();
        return $resultSet->result_array();
    }
    function getAdminFlashAdDetailsByRowId($flash_admin_ads_row_id)
    {
        $this->db->select('*');
        $this->db->from('tbl_admin_flash_ads');
        $this->db->where('flash_admin_ads_row_id',$flash_admin_ads_row_id);
        $this->db->where('active_status',1);

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

    function getAdminPopupAdDetailsByRowId($popup_admin_ads_row_id)
    {
        $this->db->select('*');
        $this->db->from('tbl_admin_popup_ads');
        $this->db->where('popup_admin_ads_row_id',$popup_admin_ads_row_id);
        $this->db->where('active_status',1);

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

    function getAdminBannerDetailsByRowId($admin_aff_program_row_id, $banner_admin_ads_row_id)
     {
        $this->db->select('*');
        $this->db->from('tbl_admin_ads_banner ab');
        $this->db->where('ab.admin_aff_program_row_id',$admin_aff_program_row_id);
        $this->db->where('ab.banner_admin_ads_row_id',$banner_admin_ads_row_id);
        $res = $this->db->get();
        $result = $res->row_array();
        return $result;

     }

     function getAdminMediaInformation($adType,$enc_ad_row_id)
    {
        switch ($adType)
        {
            case '2112'    : $data['media_type'] = 9;                    //Program Specific Affiliate Link
            //$refererData['advertise_type'] = "Program Specific Affiliate Link";
            $ad_table_name = 'tbl_admin_ads_program_specific_affiliate_link';
            $row_id_name = 'admin_ads_program_specific_affiliate_link_row_id';
            $adInfo = $this->getAdminAdInfo($ad_table_name,$row_id_name,$enc_ad_row_id);
            $data['media_row_id'] = $adInfo['admin_ads_program_specific_affiliate_link_row_id'];
            $data['admin_aff_program_row_id'] = $adInfo['admin_aff_program_row_id'];
            //$destination_url = $adInfo->destination_url;
            break;
            /*case '2001' : $data['media_type'] = 1;                    //Ad
            $ad_table_name = 'tbl_admin_ads_main';
            $row_id_name = 'main_ads_row_id';
            $adInfo = $this->getAdminAdInfo($ad_table_name,$row_id_name,$enc_ad_row_id);
            $data['media_row_id'] = $adInfo->main_ads_row_id;
            $data['merchant_aff_program_row_id'] = $adInfo->merchant_aff_program_row_id;
            break; */

            case '2011' : $data['media_type'] = 3;                    //Text Ad
            $ad_table_name = 'tbl_admin_ads_text_links';
            $row_id_name = 'text_links_admin_ads_row_id';
            $adInfo = $this->getAdminAdInfo($ad_table_name,$row_id_name,$enc_ad_row_id);
            $data['media_row_id'] = $adInfo['text_links_admin_ads_row_id'];
            $data['admin_aff_program_row_id'] = $adInfo['admin_aff_program_row_id'];
            break;

            case '2100' : $data['media_type'] = 4;                    //Banner Ad
            $ad_table_name = 'tbl_admin_ads_banner';
            $row_id_name = 'banner_admin_ads_row_id';
            $adInfo = $this->getAdminAdInfo($ad_table_name,$row_id_name,$enc_ad_row_id);
            $data['media_row_id'] = $adInfo['banner_admin_ads_row_id'];
            $data['admin_aff_program_row_id'] = $adInfo['admin_aff_program_row_id'];
            break;

            case '2110' : $data['media_type'] = 6;                    //Popup Ad
            $ad_table_name = 'tbl_admin_popup_ads';
            $row_id_name = 'popup_admin_ads_row_id';
            $adInfo = $this->getAdminAdInfo($ad_table_name,$row_id_name,$enc_ad_row_id);
            $data['media_row_id'] = $adInfo['popup_admin_ads_row_id'];
            $data['admin_aff_program_row_id'] = $adInfo['admin_aff_program_row_id'];
            break;

            case '2111' : $data['media_type'] = 7;                    //Flash Ad
            $ad_table_name = 'tbl_admin_flash_ads';
            $row_id_name = 'flash_admin_ads_row_id';
            $adInfo = $this->getAdminAdInfo($ad_table_name,$row_id_name,$enc_ad_row_id);
            $data['media_row_id'] = $adInfo['flash_admin_ads_row_id'];
            $data['admin_aff_program_row_id'] = $adInfo['admin_aff_program_row_id'];
            break;
        }
        return $data;
    }

    //Return Tell  friend usable program if not exist then default program.
    function getTellFriendSpecificLinkInfo()
    {
        $sql = 'SELECT a.admin_aff_program_row_id,
                       a.generated_link
                       FROM tbl_admin_ads_program_specific_affiliate_link AS a JOIN
                            tbl_admin_aff_program  AS b ON (a.admin_aff_program_row_id = b.admin_aff_program_row_id)
                            WHERE b.use_for_tell_friend = 1 OR b.is_default = 1 ORDER BY b.use_for_tell_friend DESC';
        $query = $this->db->query($sql);
        return $query->row_array();
    }

}
?>