<?php
namespace App\Models\Tarifas;

class TarifaextraModel extends \App\Models\BaseModel
{
    protected $table = "tarifa_extra";

    /**
     * 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\TarifaextraEntity";

    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" => "Tarifaextra.nombre",
            "rules" => "trim|required|max_length[255]",
        ],
        "margen" => [
            "label" => "Tarifaextra.margen",
            "rules" => "required|decimal",
        ],
    ];

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

    public function getServiciosExtraSelector()
    {
        /*
            Todos los servicios de extra 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 getTarifaPresupuestoExtra($tarifa_id){
      
        $builder = $this->db
            ->table($this->table . " t1")
            ->select(
                "t1.id AS tarifa_extra_id, t1.nombre AS tarifa_extra_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 getNombreTarifaExtra($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();
    }

}
