<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Servicos_model extends CI_Model {

	function __construct() {
        parent::__construct();
    }

    
    function get($table,$fields,$where='',$perpage=0,$start=0,$one=false,$array='array'){
        
        $this->db->select($fields);
        $this->db->from($table);
        $this->db->limit($perpage,$start);
//        $this->db->order_by('ID_SERVICO','desc');
        if($where){
            $this->db->where($where);
        }
        
        $query = $this->db->get();
        
        $result =  !$one  ? $query->result() : $query->row();
        return $result;
    }

    function getById($id){
//        $this->db->join('imagemfuncionario','imagemfuncionario.funcionario_id = funcionario.idFuncionario', 'left');
        $this->db->where('ID_SERVICO',$id);
        $this->db->limit(1);
        return $this->db->get('tbl_013_servicos')->row();
    }

    function getPecasPorServico($idServico) {
//        $this->db->join('tbl_013_servicos as servico', 'servico.ID_SERVICO = pecasservico.SERVICO_ID', 'left');
        $this->db->join('tbl_012_pecas as pecas', 'pecas.ID_PECA = pecasservico.PECA_ID', 'left');
        $this->db->where('SERVICO_ID', $idServico);
        return $this->db->get('tbl_rel_001_pecas_por_servico as pecasservico')->result();
    }


    function getServicosRealizados() {
        $this->db->select('*, ADDDATE(manutencao.DT_PREVENTIVA, INTERVAL servico.NU_MESES_GARANTIA MONTH) AS DT_PROXIMO_SERVICO,');
        $this->db->join('tbl_009_manutencao as manutencao', 'servicosprev.PREVENTIVA_ID = manutencao.ID_PREVENTIVA', 'left');
        $this->db->join('tbl_013_servicos as servico', 'servico.ID_SERVICO = servicosprev.SERVICO_ID', 'left');
        $this->db->join('tbl_008_veiculos as veiculo', 'veiculo.ID_VEICULOS = manutencao.VEICULO_ID', 'left');
        $this->db->limit(10);
        return $this->db->get('tbl_rel_002_servico_por_preventiva as servicosprev')->result();
    }
    
    function add($table,$data,$returnId = false){
        $this->db->insert($table, $data);         
        if ($this->db->affected_rows() == '1')
		{
                        if($returnId == true){
                            return $this->db->insert_id($table);
                        }
			return TRUE;
		}
		
		return FALSE;       
    }
    
    function addFotos($table,$data,$returnId = false){
        $this->db->insert($table, $data);         
        if ($this->db->affected_rows() == '1')
		{
                        if($returnId == true){
                            return $this->db->insert_id($table);
                        }
			return TRUE;
		}
		
		return FALSE;       
    }
    
    function edit($table,$data,$fieldID,$ID){
        $this->db->where($fieldID,$ID);
        $this->db->update($table, $data);

        if ($this->db->affected_rows() >= 0)
		{
			return TRUE;
		}
		
		return FALSE;       
    }
    
    function delete($table,$fieldID,$ID){
        $this->db->where($fieldID,$ID);
        $this->db->delete($table);
        if ($this->db->affected_rows() == '1')
		{
			return TRUE;
		}
		
		return FALSE;        
    }   

    function count($table){
	return $this->db->count_all($table);
    }

    public function autoCompleteServico($q) {
        $this->db->select('*');
        $this->db->limit(15);
        $this->db->like('DS_DESCRICAO_SERVICO', $q);
//        $this->db->where('situacaoMorador', 1);
        $query = $this->db->get('tbl_013_servicos');

        if ($query->num_rows() > 0) {
            foreach ($query->result_array() as $row) {
                $row_set[] = array(
                    'label' => $row['DS_DESCRICAO_SERVICO'] ,
                    'garantia' => $row['NU_MESES_GARANTIA'],
                    'idServico' => $row['ID_SERVICO'],
                    'grupo' => $row['DS_GRUPO'],
//                    'email_morador' => $row['email_morador'],
//                    'nr_celular' => $row['nr_celular']
                );
            }
            echo json_encode($row_set);
        }
    }

    public function autoCompleteFuncionario($q) {

        $this->db->select('*');
        $this->db->limit(5);
        $this->db->like('nomeFuncionario', $q);
        $query = $this->db->get('funcionario');
        if ($query->num_rows > 0) {
            foreach ($query->result_array() as $row) {
                $row_set[] = array('label' => $row['nomeFuncionario'] . ' | Função: ' . $row['funcao'], 'id' => $row['idFuncionario']);
            }
            echo json_encode($row_set);
        }
    }
    
    public function autoCompleteProduto($q){

        $this->db->select('*');
        $this->db->limit(5);
        $this->db->like('descricao', $q);
        $query = $this->db->get('produtos');
        if($query->num_rows > 0){
            foreach ($query->result_array() as $row){
                $row_set[] = array('label'=>$row['descricao'].' | Preço: R$ '.$row['precoVenda'].' | Estoque: '.$row['estoque'],'estoque'=>$row['estoque'],'id'=>$row['idProdutos'],'preco'=>$row['precoVenda']);
            }
            echo json_encode($row_set);
        }
    }

    public function autoCompleteUsuario($q){
        echo 'aqui';
        $this->db->select('*');
        $this->db->limit(5);
        $this->db->like('nome', $q);
        $this->db->where('situacao',1);
        $query = $this->db->get('usuarios');
        if($query->num_rows > 0){
            foreach ($query->result_array() as $row){
                $row_set[] = array('label'=>$row['nome'].' | Telefone: '.$row['telefone'],'id'=>$row['idUsuarios']);
            }
            echo json_encode($row_set);
        }
    }
    
    public function autoCompleteImovel($q){

        $this->db->select('*');
        $this->db->limit(5);
        $this->db->like('CODIMOVEL', $q);
        $this->db->where('STATUSIMOVEL',"Disponível");
        $query = $this->db->get('imoveis');
        if($query->num_rows > 0){
            foreach ($query->result_array() as $row){
                $row_set[] = array('label'=>$row['CODIMOVEL'].
                        ' | '.$row['TIPOIMOVEL'].
                        ' | '.$row['ENDERECOIMOVEL'].
                        ' - '.$row['COMPENDIMOVEL'].
                        ' | '.$row['BAIRROIMOVEL'].
                        ' | '.$row['CIDADEIMOVEL'].
                        ' | '.$row['CEPIMOVEL']
                        ,'id'=>$row['CODIMOVEL']);
            }
            echo json_encode($row_set);
        }
    }
    
    function atualizarImovel($table,$data,$fieldID,$ID){
        $this->db->where($fieldID,$ID);
        $this->db->update($table, $data);

        if ($this->db->affected_rows() >= 0)
		{
			return TRUE;
		}
		
		return FALSE;       
    }

    
//       public function buscarNoticiasSite(){
//        
//
//        $query = "SELECT * FROM noticia";
//        return $this->db->query($query)->result();
//    }

    
        public function buscarNoticiasSite(){
        $this->db->select('*');
        $this->db->from('noticia');
         $this->db->join('imagensnoticia','imagensnoticia.noticia_id = noticia.idNoticia');
         $this->db->limit(10);
        return $this->db->get()->result();
    }

}

/* End of file vendas_model.php */
/* Location: ./application/models/vendas_model.php */