<?php

/**
 * 
 * Model pentru client.
 * 
 */
class Client_model extends CI_Model{
    
    function __construct() 
    {
        parent::__construct();
    }

    function getAllData($start=NULL,$limit=NULL,$sidx=NULL,$sord=NULL,$where=NULL)
    {
        $ci = &get_instance();

        $q_w = ' 1 ';
        if($where != NULL)
            $q_w = $where;

        $query = 
            "
            SELECT  I.*
            FROM client as I
            WHERE $q_w ";
       
        if(isset($sidx) && isset($sord))
        	$query.=" ORDER BY $sidx $sord";
         
        if(isset($limit) && $limit)
            $query.=" LIMIT  $start ,$limit";


        $q = $this->db->query($query);
        if(isset($q) && $q!=null)
            if($q->num_rows() > 0)
                return $q->result();
        
        return false;
    }

    function getListaClienti()
    {
        $query = 
            "
            SELECT *
            FROM client 
            WHERE 1 ORDER BY denumire ASC";
        
        $q = $this->db->query($query);
        if(isset($q) && $q!=null)
            if($q->num_rows() > 0)
                return $q->result();
            
        return false;
    }
    
    function getClienti()
    {
        $query = 
            "
            SELECT *
            FROM client 
            WHERE activ>0 ORDER BY denumire ASC";
        
        $q = $this->db->query($query);
        if(isset($q) && $q!=null)
            if($q->num_rows() > 0)
                return $q->result();
            
        return false;
    }

    function getInfoClient($id)
    {
        $query = 
            "
            SELECT *
            FROM client 
            WHERE id_client = ".$id;
        
        $q = $this->db->query($query);
        if(isset($q) && $q!=null)
            if($q->num_rows() > 0)
                return $q->result()[0];
            
        return false;
    }

    function existClientCode($code)
    {
        $ci = &get_instance();
        
        $query = 
            "
            SELECT *
            FROM client 
            WHERE activ>0 AND cod = ".$ci->db->escape($code);
        
        $q = $this->db->query($query);
        if(isset($q) && $q!=null)
            if($q->num_rows() > 0)
                return $q->result()[0];
            
        return 0;
    }
}