<?php
namespace App\Models\Configuracion;

class TipoPresupuestoModel extends \App\Models\BaseModel
{
    protected $table = "tipos_presupuestos";

    /**
     * Whether primary key uses auto increment.
     *
     * @var bool
     */
    protected $useAutoIncrement = true;

    protected $allowedFields = ["codigo", "encuadernado", "is_cosido", "is_deleted", 'deleted_at', 'updated_at', 'created_at'];
    protected $returnType = "App\Entities\Configuracion\TipoPresupuestoEntity";

    public static $labelField = "codigo";


    public function get_isCosido(int $id){

        $builder = $this->db
            ->table($this->table . " t1")
            ->select("t1.is_cosido AS is_cosido")
            ->where('t1.id', $id)
            ->where('t1.is_deleted', 0);

        $result = $builder->get()->getResultObject();
        if(count($result)>0){
            return $result[0]->is_cosido;
        }
        return null;
    }

    public function get_isTapaDura(int $id){

        $builder = $this->db
            ->table($this->table . " t1")
            ->select("t1.is_tapa_dura AS is_tapa_dura")
            ->where('t1.id', $id)
            ->where('t1.is_deleted', 0);

        $result = $builder->get()->getResultObject();
        if(count($result)>0){
            return $result[0]->is_tapa_dura;
        }
        return null;
    }


    public function getLibros($search = ""){

        $builder = $this->db
            ->table($this->table . " t1")
            ->select("t1.id as id, t1.encuadernacion as name")
            ->where('t1.codigo LIKE', 'libro%')
            ->where('t1.is_deleted', 0);

        return empty($search) ? 
            $builder->get()->getResultObject() : 
            $builder->groupStart()->
                like('t1.codigo', $search)->
                groupEnd()->get()->getResultObject();
    }
}
