<?php
/*
@author: G J Jidni
*/
class Package_model extends CI_Model
{

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

	function getPackages ()
	{
		$sql = '(SELECT package_id, package_name,package_number,
					(CASE WHEN monthly_price > 0 THEN CONCAT(\'$\',\'\',monthly_price) ELSE \'N/A\' END) as monthly_price,
					(CASE WHEN annual_price > 0 THEN CONCAT(\'$\',\'\',annual_price) ELSE \'N/A\' END) as annual_price,
					(CASE WHEN trial_amount > 0 THEN trial_amount ELSE \'N/A\' END) as trial_amount,
					(CASE WHEN active_status = \'1\' THEN \'Active\' ELSE \'Inactive\' END) as active_status
					from tbl_package) AS abc';

		$getInstance = & get_instance();
		$getInstance->load->library('rapyd');
		$getInstance->rapyd->load('datagrid');

		$grid = new DataGrid('', $sql);
		$grid->order_by('package_number', 'asc');
		$grid->per_page = 20;
		$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('Trial Price', 'trial_amount', 'trial_amount', 'align=\'left\'');
		$grid->column_orderby('Monthly Price', 'monthly_price', 'monthly_price', 'align=\'left\'');
		$grid->column_orderby('Annual Price', 'annual_price', 'annual_price', 'align=\'left\'');
		$grid->column_orderby('Active Status', 'active_status', 'active_status', 'align=\'left\'');
		$link_edit = anchor('administration/package/editPackage/<#package_id#>','Edit');
		$grid->column('Actions',$link_edit);

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

	function getPackagesForPackageNumber()
	{
		$sql = "SELECT *
				FROM tbl_package";

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

		return $result;
	}

    function getPackageForLookUp()
    {
        $sql = 'SELECT package_id,
                package_name
                FROM tbl_package
                WHERE active_status = 1';
        $resultSet = $this->db->query($sql);
        return $resultSet->result_array();
    }

	function isPackageNameExist($packageData)
	{
		$this->db->select('*');
		$this->db->from('tbl_package');
		if($packageData['package_id'])
		{
			$this->db->where('package_id !=', $packageData['package_id']);
		}
		#$this->db->where('active_status',1);
		$this->db->where('package_name', $packageData['package_name']);
		$resultSet = $this->db->get();
		return $resultSet->num_rows();
	}

	function savePackage($packageData)
	{
		$this->db->set('package_name',$packageData['package_name']);
		$this->db->set('package_number',$packageData['package_number']);
		$this->db->set('monthly_price',$packageData['monthly_price']);
		$this->db->set('monthly_price_to_show',$packageData['monthly_price_to_show']);
		$this->db->set('annual_price',$packageData['annual_price']);
		$this->db->set('database_limit',$packageData['database_limit']);
		#$this->db->set('client_limit',$packageData['client_limit']);
		$this->db->set('active_status',$packageData['active_status']);
		$this->db->set('is_trial',$packageData['is_trial']);
        $this->db->set('is_visible',$packageData['is_visible']);
        $this->db->set('is_chargeable',$packageData['is_chargeable']);
		$this->db->set('trail_duration',$packageData['trail_duration']);
		$this->db->set('trial_amount',$packageData['trial_amount']);
		$this->db->set('package_description',$packageData['package_description']);

		if($packageData['package_id'])
		{
			$this->db->where('package_id',$packageData['package_id']);
			$this->db->update('tbl_package');
			$package_id = $packageData['package_id'];
		}
		else
		{
			$this->db->insert('tbl_package');
			$package_id = $this->db->insert_id();
		}

		$this->db->where('package_id', $package_id);
		$this->db->delete('tbl_package_module');

		foreach ($packageData['selectedModules'] as $aModulesId)
		{
			$this->db->set('package_id',$package_id);
			$this->db->set('module_id',$aModulesId);
			$this->db->insert('tbl_package_module');
		}

		return $package_id;
	}

	function getPackageModules($packageId)
	{
		$this->db->select('module_id');
		$this->db->from('tbl_package_module');
		$this->db->where('package_id', $packageId);
		$resultSet = $this->db->get();
		return $resultSet->result();
	}

	function getPackage($packageId)
	{
        if($packageId)
        {
		    $this->db->select('*');
		    $this->db->from('tbl_package');
		    $this->db->where('package_id', $packageId);
		    $resultSet = $this->db->get();
		    return $resultSet->row_array();
        }
        return false;
	}

	function getActivePackages($isVisible = 0, $admin = null, $packages = null)
	{                      
        if(!$admin)
        {
           if($isVisible == 0 || $isVisible == 1)
              $visible = " AND is_visible = {$isVisible}";
        }
        
        $packageCon = $packages ? " AND package_id IN(" . implode(', ', $packages) . ") " : ''; 
        return $this->db->query("SELECT * FROM tbl_package WHERE active_status = 1 {$visible} {$packageCon} ORDER BY package_number ASC")->result();
	}

	function isModuleExistPackage($packageId, $moduleId)
	{
		$this->db->select('module_id');
		$this->db->from('tbl_package_module');
		$this->db->where('package_id',$packageId);
		$this->db->where('module_id',$moduleId);

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

	/**
	 * added by Himel Nag Rana
	 */
	function updatePackageNumber($package_id,$new_package_number)
	{
		$this->db->set('package_number',$new_package_number);
		$this->db->where('package_id',$package_id);
		$this->db->update('tbl_package');
	}

	function getMaxPackageNumber($packages = null)
	{
		$where = $packages ? " AND package_id IN(" . implode(', ', $packages) . ") " : ''; 
        $result = $this->db->query("SELECT MIN(package_number) as maxNum FROM tbl_package WHERE active_status = 1 {$where}")->row();
		return $result->maxNum;
	}

    function getMerchantPackageInfo($package_id, $merchant_row_id)
    {
        $this->db->select('*');
        $this->db->from('tbl_merchant_package_history');
        $this->db->where('package_id',$package_id);
        $this->db->where('merchant_row_id',$merchant_row_id);

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

    }
}
?>