<?php 
class Maintenance_model extends Model 
{
	/**
	* Instanciar o CI
	*/
	public function Maintenance_model()
    {
        parent::Model();
		$this->CI =& get_instance();
		$this->load->database();
		$this->load->library('session'); 
    }
	
	public function validate() {
		$this->form_validation->set_rules('group_name', 'Group Name');
		return parent::validate();
	}
	function save($db_array, $id=NULL, $set_flashdata = TRUE) {
		$success = TRUE;
		$this->db->insert("maintenances", $db_array);
		return $success;
	}
	
	function saveOperationType($db_array, $id=NULL, $set_flashdata = TRUE) {
		$success = TRUE;
		$this->db->insert("operations", $db_array);
		return $success;
	}
	
	public function checkOperationDuplicate($operationName,$id)
	{
		$qry="select operation_name from operations where ";
	
		if($id!="")
		{
			$qry.=" id!=".$id." AND ";
		}
		$qry.=" operation_name='".$operationName."'";
		$rarr=$this->db->query($qry);
		if($rarr->num_rows()<1)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	function getAllData(){
		$page = isset($_GET["page"])?$_GET["page"]:1; 
		$limit = isset($_GET["rows"])?$_GET["rows"]:3; 
		$sidx = isset($_GET['sidx'])?$_GET['sidx']:'user_id'; 
		$sord = isset($_GET['sord'])?$_GET['sord']:'';     
		$alert_sdate = isset($_GET['alert_sdate'])?$_GET['alert_sdate']:'';
		$alert_edate = isset($_GET['alert_edate'])?$_GET['alert_edate']:'';
		$start = $limit*$page - $limit; 
		$start = ($start<0)?0:$start; 

		$where = ""; 
		$searchField = isset($_GET['searchField']) ? $_GET['searchField'] : false;
		$searchOper = isset($_GET['searchOper']) ? $_GET['searchOper']: false;
		$searchString = isset($_GET['searchString']) ? $_GET['searchString'] : false;

		if (isset($_GET['_search']) && $_GET['_search'] == 'true') {
			$ops = array(
			'eq'=>'=', 
			'ne'=>'<>',
			'lt'=>'<', 
			'le'=>'<=',
			'gt'=>'>', 
			'ge'=>'>=',
			'bw'=>'LIKE',
			'bn'=>'NOT LIKE',
			'in'=>'LIKE', 
			'ni'=>'NOT LIKE', 
			'ew'=>'LIKE', 
			'en'=>'NOT LIKE', 
			'cn'=>'LIKE', 
			'nc'=>'NOT LIKE' 
			);
			foreach ($ops as $key=>$value){
				if ($searchOper==$key) {
					$ops = $value;
				}
			}
			if($searchOper == 'eq' ) $searchString = $searchString;
			if($searchOper == 'bw' || $searchOper == 'bn') $searchString .= '%';
			if($searchOper == 'ew' || $searchOper == 'en' ) $searchString = '%'.$searchString;
			if($searchOper == 'cn' || $searchOper == 'nc' || $searchOper == 'in' || $searchOper == 'ni') $searchString = '%'.$searchString.'%';

			$where = "$searchField $ops '$searchString' "; 

		}

		if(!$sidx) 
			$sidx =1;
			
		
		if(!empty($alert_sdate) && !empty($alert_edate))
		{
			
			$alert_sdate = date("Y-m-d", strtotime($alert_sdate));
			$alert_edate = date("Y-m-d", strtotime($alert_edate));
			$where = " m.debut_alerte >= DATE('$alert_sdate') AND m.fin_alerte <= DATE('$alert_edate')";
				
		}
		
		if($this->session->userdata('user_id')!=1){
			$SQL = "SELECT m.id,m.created,m.debut_alerte,m.fin_alerte,m.kms_debut_alerte,m.kms_fin_alerte,m.duree_debut_alerte,m.duree_fin_alerte,m.cout,m.statut,m.comments,am.assets_name,o.operation_name,o.operation_type FROM `maintenances` m , `assests_master` am , `operations` o  WHERE m.assets_id = am.id and o.id=m.id_operation";
			// AND gm.add_uid = ".$this->session->userdata('user_id');
		}
		
		if($where != "")
			$SQL .= " AND $where";
		$result = $this->db->query($SQL);
		$count = $result->num_rows();
		if( $count > 0 ) {
			$total_pages = ceil($count/$limit);    
		} else {
			$total_pages = 0;
		}

		if ($page > $total_pages) 
			$page=$total_pages;
			
		
		$SQL .= " ORDER BY $sidx $sord LIMIT $start, $limit";
		
		$query = $this->db->query($SQL);
		
		
		$data = array();
		$data['result'] = $query->result();
		$data['page'] = $page;
		$data['total_pages'] = $total_pages;
		$data['count'] = $count;
		return $data;
	}
	
	function typeOperationgetAllData()
	{
		$page = isset($_GET["page"])?$_GET["page"]:1;
		$limit = isset($_GET["rows"])?$_GET["rows"]:3;
		$sidx = isset($_GET['sidx'])?$_GET['sidx']:'user_id';
		$sord = isset($_GET['sord'])?$_GET['sord']:'';
		$start = $limit*$page - $limit;
		$start = ($start<0)?0:$start;
		
		$where = "";
		$searchField = isset($_GET['searchField']) ? $_GET['searchField'] : false;
		$searchOper = isset($_GET['searchOper']) ? $_GET['searchOper']: false;
		$searchString = isset($_GET['searchString']) ? $_GET['searchString'] : false;
		
		if (isset($_GET['_search']) && $_GET['_search'] == 'true') {
			$ops = array(
					'eq'=>'=', 
					'ne'=>'<>',
					'lt'=>'<', 
					'le'=>'<=',
					'gt'=>'>', 
					'ge'=>'>=',
					'bw'=>'LIKE',
					'bn'=>'NOT LIKE',
					'in'=>'LIKE', 
					'ni'=>'NOT LIKE', 
					'ew'=>'LIKE', 
					'en'=>'NOT LIKE', 
					'cn'=>'LIKE', 
					'nc'=>'NOT LIKE' 
			);
			foreach ($ops as $key=>$value){
				if ($searchOper==$key) {
					$ops = $value;
				}
			}
			if($searchOper == 'eq' ) $searchString = $searchString;
			if($searchOper == 'bw' || $searchOper == 'bn') $searchString .= '%';
			if($searchOper == 'ew' || $searchOper == 'en' ) $searchString = '%'.$searchString;
			if($searchOper == 'cn' || $searchOper == 'nc' || $searchOper == 'in' || $searchOper == 'ni') $searchString = '%'.$searchString.'%';
		
			$where = "$searchField $ops '$searchString' ";
		
		}
		
		if(!$sidx)
		$sidx =1;
			
		
		if($this->session->userdata('user_id')!=1){
		$SQL = "SELECT * FROM `operations`";
					// AND gm.add_uid = ".$this->session->userdata('user_id');
		}
		
		if($where != "")
		$SQL .= " AND $where";
		$result = $this->db->query($SQL);
		$count = $result->num_rows();
		if( $count > 0 ) {
		$total_pages = ceil($count/$limit);
		} else {
		$total_pages = 0;
		}
		
		if ($page > $total_pages)
		$page=$total_pages;
			
		$SQL .= " ORDER BY $sidx $sord LIMIT $start, $limit";
		
		$query = $this->db->query($SQL);
		
		$data = array();
		$data['result'] = $query->result();
		$data['page'] = $page;
		$data['total_pages'] = $total_pages;
		$data['count'] = $count;
				return $data;
	}	
	
	function typeOperationgetAllDataBYtype($type)
	{
		$SQL = "SELECT * FROM `operations` WHERE operation_type='".$type."'";

		$query = $this->db->query($SQL);

		$data= $query->result();
		return $data;
	}


}
?>