<?php
namespace App\Models\Tarifas;

class TarifapreimpresionModel extends \App\Models\BaseModel
{
    protected $table = "lg_tarifa_preimpresion";

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

    protected $allowedFields = [
        "nombre",
        "code",
        "comment",
        "precio",
        "margen",
        "mostrar_en_presupuesto",
        "mostrar_en_presupuesto_cliente",
        "deleted_at",
        "is_deleted",
        "user_created_id", 
        "user_updated_id"];
    protected $returnType = "App\Entities\Tarifas\TarifapreimpresionEntity";

    protected $useTimestamps = true;
    protected $useSoftDeletes = false;

    protected $createdField = "created_at";
    protected $updatedField = "updated_at";
    protected $deletedField  = 'deleted_at';

    public static $labelField = "nombre";

    protected $validationRules = [
        "nombre" => [
            "label" => "Tarifapreimpresion.nombre",
            "rules" => "trim|required|max_length[255]",
        ],
        "code" => [
            "label" => "Tarifapreimpresion.code",
            "rules" => "trim|required|max_length[5]",
        ],
        "comment" => [
            "label" => "Tarifapreimpresion.comment",
            "rules" => "trim|max_length[255]",
        ],
        "precio" => [
            "label" => "Tarifapreimpresion.precio",
            "rules" => "required|decimal",
        ],
        "margen" => [
            "label" => "Tarifapreimpresion.margen",
            "rules" => "required|decimal",
        ],
    ];

    protected $validationMessages = [
        "nombre" => [
            "max_length" => "Tarifapreimpresion.validation.nombre.max_length",
            "required" => "Tarifapreimpresion.validation.nombre.required",
        ],
        "code" => [
            "max_length" => "Tarifapreimpresion.validation.code.max_length",
            "required" => "Tarifapreimpresion.validation.code.required",
        ],
        "comment" => [
            "max_length" => "Tarifapreimpresion.validation.comment.max_length",
            "required" => "Tarifapreimpresion.validation.comment.required",
        ],
        "precio" => [
            "decimal" => "Tarifapreimpresion.validation.precio.decimal",
            "required" => "Tarifapreimpresion.validation.precio.required",
        ],
        "margen" => [
            "required" => "Tarifapreimpresion.validation.margen.required",
            "decimal" => "Tarifapreimpresion.validation.margen.decimal",
        ],
    ];

    public function getServiciosPreimpresionSelector()
    {
        /*
            Todos los servicios de preimpresion activas que se pueden usar en presupuestos
        */
        $builder = $this->db
            ->table($this->table . " t1")
            ->select(
                "t1.id as value, t1.nombre AS label"
            )
            ->where("t1.is_deleted", 0)
            ->where("t1.mostrar_en_presupuesto", 1);

        return $builder->orderBy("t1.nombre", "asc")->get()->getResultObject();
    }

    public function getTarifaPresupuestoPreimpresion($tarifa_id){
      
        $builder = $this->db
            ->table($this->table . " t1")
            ->select(
                "t1.id AS tarifa_preimpresion_id, t1.nombre AS tarifa_preimpresion_nombre, t1.precio AS precio, t1.margen AS margen"
            )
            ->where("t1.is_deleted", 0);
            //->where("t1.mostrar_en_presupuesto", 1)
            
        $builder->where('t1.id =', $tarifa_id);
        
        return $builder->get()->getResultObject();
    }

    public function getNombreTarifaPreimpresion($id=-1)
    {
        /*
            Todos los servicios de encuadernacion activas que se pueden usar en presupuestos
        */
        $builder = $this->db
            ->table($this->table . " t1")
            ->select(
                "t1.nombre AS nombre"
            )
            ->where("t1.id", $id)
            ->where("t1.is_deleted", 0);

        return $builder->orderBy("t1.nombre", "asc")->get()->getResultObject();
    }

}
