<?php
class Common_model extends CI_Model 
{
	function __construct()
	{
		parent::__construct();
	}

	function insertData($table=null,$arr=null)
	{
		$this->db->insert($table,$arr);
		return $this->db->insert_id();
	}
	function getAllMultipleJoin($table=null,$where=null,$join=null,$order=null,$select='*',$group=null,$page='',$limit='',$like='',$not_like='')
	{
		$this->db->select($select);
		$this->db->from($table);
		if(!empty($join))
		{
			foreach($join as $row) // $join=array('user_profile','type','.inner..outer');
			{
				$this->db->join($row[0],$row[1],'left');
			}
		}
		if($where)
		{
			$this->db->where($where);
		}
		if($like)
		{
			$this->db->like($like);
		}
		if($not_like)
		{
			$this->db->not_like($not_like);
		}
		if($group)
		{
			$this->db->group_by($group);
		}
		if($order)
		{
			$this->db->order_by($order);
		}
		if(!empty($limit))
	    {
	      $this->db->limit($limit,$page);
	    }
		$res=$this->db->get();
		//echo $this->db->last_query();
		return $res->result_array();
	}
	function getAll($table=null,$where=null,$join=null,$order=null,$select='*',$group=null,$page='',$limit='',$like='')
	{
		$this->db->select($select);
		$this->db->from($table);
		if($join)
		{
			$this->db->join($join[0],$join[1]);
		}
		if($where)
		{
			$this->db->where($where);
		}
		if($like)
		{
			$this->db->like($like);
		}
		if($group)
		{
			$this->db->group_by($group);
		}
		if($order)
		{
			$this->db->order_by($order);
		}
		if(!empty($limit))
	    {
	      $this->db->limit($limit,$page);
	    }
		$res=$this->db->get();
		//echo $this->db->last_query();
		return $res->result_array();
	}
	function getAllRow($table=null,$where=null,$join=null,$order=null,$select='*',$like='')
	{
		$this->db->select($select);
		$this->db->from($table);
		if($join)
		{
			$this->db->join($join[0],$join[1]);
		}
		
		if($where)
		{
			$this->db->where($where);
		}
		if($like)
		{
			$this->db->like($like);
		}
		if($order)
		{
			$this->db->order_by($order);
		}
		$res=$this->db->get();
		return $res->row_array();
	}
	function getAllRowMultipleJoin($table=null,$where=null,$join=null,$order=null,$select='*',$group=null,$page='',$limit='',$like='')
	{
		$this->db->select($select);
		$this->db->from($table);
		if(!empty($join))
		{
			foreach($join as $row) // $join=array('user_profile','type','.inner..outer');
			{
				$this->db->join($row[0],$row[1],'left');
			}
		}
		if($where)
		{
			$this->db->where($where);
		}
		if($like)
		{
			$this->db->like($like);
		}
		if($group)
		{
			$this->db->group_by($group);
		}
		if($order)
		{
			$this->db->order_by($order);
		}
		if(!empty($limit))
	    {
	      $this->db->limit($limit,$page);
	    }
		$res=$this->db->get();
		//echo $this->db->last_query();
		return $res->row_array();
	}
	function UpdateData($table=null,$inst_arr=null,$where=null)
	{
		$this->db->where($where);
		$update=$this->db->update($table,$inst_arr);
		
		if($update)
		{
			
			$this->session->set_userdata('success_msg','Data updated successfully.');
			$return=1;
		}
		else
		{
			$this->session->set_userdata('error_msg','Failed to update data. Please try again.');
			$return=0;
		}
		return $this->db->affected_rows();
	}
	function deleteData($table=null,$where=null)
	{
		$this->db->where($where);
		$delete=$this->db->delete($table);
	}
	public function generateHash($password)
	{
	    return hash('sha256', '5326d68cec044' . $password);
	}
	public function getTodaysRecord($table = '')
	{
		$qry = "select * from ".$table." where DATE(`created`) = CURDATE()";
		$res= $this->db->query($qry);
		return $res->result_array();
	}

	public function getRevenue($paidStatus){
		$eventArrayOne = array();
		$query1 = "SELECT `shop_orders`.`ip_address`,`shop_booking_details`.`cart_id`,`shop_booking_details`.`event_id`,`evt_events`.`time_from`,`evt_events`.`title`,`shop_orders`.`total_sum` from `shop_orders`,`shop_booking_details`,`evt_events` WHERE `shop_orders`.`payed`=".$paidStatus." AND `shop_orders`.`cart_id`=`shop_booking_details`.`cart_id` AND `shop_booking_details`.`event_id`=`evt_events`.`id` AND `evt_events`.`time_from` < ".time();
		$result1 = $this->db->query($query1);
		if($result1->num_rows()>0){
			$incrementValue = 0;
			foreach($result1->result() as $getKey1 => $getValue1){
				$getTime = date('Y',$getValue1->time_from);
				if(!array_key_exists($getTime, $eventArrayOne)){
					$eventArrayOne[$getTime] = $getValue1->total_sum;
				}else{
					$getValueOne = $eventArrayOne[$getTime];
					$eventArrayOne[$getTime] = $getValueOne+$getValue1->total_sum;
				}
			}
		}
		
		$query2 = "SELECT `shop_orders`.`ip_address`,`shop_articles_carts`.`cart_id`,`shop_articles_carts`.`event_id`,`evt_events`.`time_from`,`evt_events`.`title`,`shop_orders`.`total_sum` from `shop_orders`,`shop_articles_carts`,`evt_events` WHERE `shop_orders`.`payed`=".$paidStatus." AND `shop_orders`.`cart_id`=`shop_articles_carts`.`cart_id` AND `shop_articles_carts`.`event_id`=`evt_events`.`id` AND `evt_events`.`time_from` < ".time();
		$result2 = $this->db->query($query2);
		if($result2->num_rows()>0){
			foreach ($result2->result() as $getKey2 => $getValue2) {
				$getTime = date('Y',$getValue2->time_from);
				if(!array_key_exists($getTime, $eventArrayOne)){
					$eventArrayOne[$getTime] = $getValue2->total_sum;
				}else{
					$getValueOne = $eventArrayOne[$getTime];
					$eventArrayOne[$getTime] = $getValueOne+$getValue2->total_sum;
				}
			}
		}
		ksort($eventArrayOne);
		$getFinalArray = array();
		foreach ($eventArrayOne as $eventArrayKey => $eventArrayValue) {
			$getFinalArray[] = array(
				'yearName' => $eventArrayKey,
				'amount' => $eventArrayValue/100
			);
		}
		return $getFinalArray;
	}


	function revenueSection(){
		$eventArrayOne = $uniquesGenerated = array();
		
		$query1 = "SELECT `shop_orders`.`ip_address`,`shop_booking_details`.`cart_id`,`shop_booking_details`.`event_id`,`evt_events`.`time_from`,`evt_events`.`title`,`shop_carts`.`user_id` from `shop_orders`,`shop_booking_details`,`evt_events`,`shop_carts` WHERE `shop_orders`.`payed`=1 AND `shop_orders`.`cart_id`=`shop_booking_details`.`cart_id` AND `shop_booking_details`.`event_id`=`evt_events`.`id` AND `shop_orders`.`cart_id`=`shop_carts`.`id` AND `evt_events`.`time_from` < ".time();
		//echo $query1;
		$result1 = $this->db->query($query1);
		
		if($result1->num_rows()>0){
			foreach($result1->result() as $getKey1 => $getValue1){
				$getTime = date('Y',$getValue1->time_from);
				if(!array_key_exists($getTime, $eventArrayOne)){
					$eventArrayOne[$getTime] = array($getValue1->user_id);
				}else{
					$incrementValue = $eventArrayOne[$getTime];
					$eventArrayOne[$getTime][] = $getValue1->user_id;
				}
			}
		}

		$query2 = "SELECT `shop_orders`.`ip_address`,`shop_articles_carts`.`cart_id`,`shop_articles_carts`.`event_id`,`evt_events`.`time_from`,`evt_events`.`title`,`shop_carts`.`user_id` from `shop_orders`,`shop_articles_carts`,`evt_events`,`shop_carts` WHERE `shop_orders`.`payed`=1 AND `shop_orders`.`cart_id`=`shop_articles_carts`.`cart_id` AND `shop_articles_carts`.`event_id`=`evt_events`.`id` AND `shop_orders`.`cart_id`=`shop_carts`.`id` AND `evt_events`.`time_from` < ".time();
		$result2 = $this->db->query($query2);
		if($result2->num_rows()>0){
			foreach ($result2->result() as $getKey2 => $getValue2) {
				$getTime = date('Y',$getValue2->time_from);
				if(!array_key_exists($getTime,$eventArrayOne)){
					$eventArrayOne[$getTime] = array($getValue2->user_id);;
				}else{
					$incrementValue = $eventArrayOne[$getTime];
					$eventArrayOne[$getTime][] = $getValue2->user_id;
				}
			}
		}
		ksort($eventArrayOne);
		foreach($eventArrayOne as $keyOne => $keyValue){
			$uniquesGenerated[$keyOne] = array_values(array_unique($eventArrayOne[$keyOne]));
		}
		$finalNewGeneratedArray = array();
		foreach($uniquesGenerated as $uniqueKey => $uniqueValue){
			$finalNewGeneratedArray[$uniqueKey]['firstCondition'] = 0;
			$finalNewGeneratedArray[$uniqueKey]['secondCondition'] = 0;
			$finalNewGeneratedArray[$uniqueKey]['thirdCondition'] = 0;
			$counter = array_count_values($eventArrayOne[$uniqueKey]);
			foreach($uniqueValue as $subCounterKey => $subCounterValue){
				$counterValue = $counter[$uniqueValue[$subCounterKey]];
				if($counterValue==1){
					$incrementOne = $finalNewGeneratedArray[$uniqueKey]['firstCondition'];
					$finalNewGeneratedArray[$uniqueKey]['firstCondition'] = $incrementOne+1;
				}
				if(($counterValue==2) || ($counterValue==3)){
					$incrementTwo = $finalNewGeneratedArray[$uniqueKey]['secondCondition'];
					$finalNewGeneratedArray[$uniqueKey]['secondCondition'] = $incrementTwo+1;
				}
				if($counterValue>=4){
					$incrementThree = $finalNewGeneratedArray[$uniqueKey]['thirdCondition'];
					$finalNewGeneratedArray[$uniqueKey]['thirdCondition'] = $incrementThree+1;
				}
			}
		}
		return $finalNewGeneratedArray;
	}

	public function userListDetails(){
		$arrayResult = array();
		$query = "SELECT * from `feu_userdetails`";
		$sql = $this->db->query($query);
		if($sql->num_rows()>0){
			foreach($sql->result() as $key => $value){
				$getTime = date('Y',$value->created);
				if(!array_key_exists($getTime, $arrayResult)){
					$arrayResult[$getTime] = 1;
				}else{
					$incrementNumber = $arrayResult[$getTime];
					$arrayResult[$getTime] = $incrementNumber+1;
				}
			}
		}
		ksort($arrayResult);
		return $arrayResult;
	}
}
