<?php
require_once("DBManager.class.php");

class Beneficiario {

    private $id;
    private $nombre;
    private $documento;
    private $telefono;
    private $email;

    // =========================================================
    //  REGISTRAR BENEFICIARIO
    // =========================================================
    public function Crear($nombre, $documento, $telefono, $email)
    {
        $resp = false;
        $db = new DBManager();

        $sql = "INSERT INTO beneficiarios(id, nombre, documento, telefono, email)
                VALUES(NULL, '$nombre', '$documento', '$telefono', '$email')";

        if ($db->_execute($sql)) {
            $resp = true;
        }
        return $resp;
    }

    // =========================================================
    //  ACTUALIZAR BENEFICIARIO
    // =========================================================
    public function Actualizar($id, $nombre, $documento, $telefono, $email, $idusuario)
    {
        $resp = false;
        $db = new DBManager();

        $sql = "UPDATE beneficiarios SET 
                    nombre = '$nombre',
                    documento = '$documento',
                    telefono = '$telefono',
                    email = '$email'
                WHERE id = $id";

        if ($db->_execute($sql)) {
            $resp = true;
        }
        return $resp;
    }

    // =========================================================
    //  ELIMINAR BENEFICIARIO
    // =========================================================
    public function Eliminar($id)
    {
        $resp = false;
        $db = new DBManager();

        $sql = "DELETE FROM beneficiarios WHERE id = $id";

        if ($db->_execute($sql)) {
            $resp = true;
        }
        return $resp;
    }

    // =========================================================
    //  BUSCAR POR ID
    // =========================================================
   public function BuscarPorCodigo($id)
    {
        $resp = false;
        $db = new DBManager();
        $rs = $db->execute("SELECT * FROM beneficiarios WHERE id=$id");

        if ($rs->getNumOfRows() > 0) {
            $resp = true;
            $rs->firstRow();
            while (!$rs->EOF) {
                $this->id = $rs->fields["id"];
                $this->nombre = $rs->fields["nombre"];
                $this->documento = $rs->fields["documento"];
                $this->telefono = $rs->fields["telefono"];
                $this->email = $rs->fields["email"];
                $rs->nextRow();
            }
        }
        return $resp;
    }
	
	public function BuscarPorDocumento($id)
    {
        $resp = false;
        $db = new DBManager();
        $rs = $db->execute("SELECT * FROM beneficiarios WHERE documento=$id");

        if ($rs->getNumOfRows() > 0) {
            $resp = true;
            $rs->firstRow();
            while (!$rs->EOF) {
                $this->id = $rs->fields["id"];
                $this->nombre = $rs->fields["nombre"];
                $this->documento = $rs->fields["documento"];
                $this->telefono = $rs->fields["telefono"];
                $this->email = $rs->fields["email"];
                $rs->nextRow();
            }
        }
        return $resp;
    }
	
	public function Listar($valor)
    {
        $vector = null;
        $db = new DBManager();
        $rs = $db->execute("SELECT * FROM beneficiarios 
                           WHERE nombre LIKE CONCAT('%','$valor','%')");
        $num_rows = $rs->getNumOfRows();

        if ($num_rows > 0) {
            $row = 0;
            $rs->firstRow();
            while (!$rs->EOF) {
                $vector[$row] = array(
                    "id" => $rs->fields["id"],
                    "nombre" => $rs->fields["nombre"],
                    "documento" => $rs->fields["documento"],
                    "telefono" => $rs->fields["telefono"],
                    "email" => $rs->fields["email"]
                );
                $row++;
                $rs->nextRow();
            }
            return $vector;
        }
        return null;
    }

    // Getters
    public function getId()        { return $this->id; }
    public function getNombre()    { return $this->nombre; }
    public function getDocumento() { return $this->documento; }
    public function getTelefono()  { return $this->telefono; }
    public function getEmail()     { return $this->email; }

}
?>

