<?php

class Cuenta_model extends CI_Model
{

	public function save($tabla, $data)
	{
		$this->db->insert($tabla, $data);
		return $this->db->insert_id();
	}

	public function get_by_id($id, $tabla)
	{
		$this->db->from($tabla);
		$this->db->where('id', $id);
		$query = $this->db->get();
		return $query->row();
	}

	public function update($where, $data, $tabla)
	{
		$this->db->update($tabla, $data, $where);
		return $this->db->affected_rows();
	}

	public function delete_by_id($id, $tabla)
	{
		$this->db->where('id', $id);
		$this->db->delete($tabla);
	}

	public function count_allI($venta)
	{
		$this->db->from('ingreso');
		$this->db->where('venta', $venta);
		return $this->db->count_all_results();
	}

	public function getAll($tabla)
	{
		return $this->db->get($tabla)->result();
	}

	public function get($id, $tabla)
	{
		return $this->db->where('id', $id)->get($tabla)->row();
	}

	public function maximo($tabla)
	{
		return $this->db->get($tabla)->last_row();
	}
	public function check($documento)
	{
		return $this->db->where('documento', $documento)->get('cliente')->row();
	}

	public function autocompletar($q)
	{
		$this->db->like('nombre', $q);
		$this->db->or_like('apellido', $q);
		$this->db->or_like('documento', $q);
		$query = $this->db->get('cliente');
		if ($query->num_rows() > 0) {
			foreach ($query->result() as $row) {
				$row_set[] = array(
					'label' => $row->documento . ' | ' . $row->nombre . ' ' . $row->apellido,
					'cliente' => $row->id,
				);
			}
		} else {
			$row_set[] = array('label' => 'EL CLIENTE NO ESTA REGISTRADO', 'cliente' => '');
		}
		echo json_encode($row_set);
	}
}
