<?php
namespace App\Models\Tarifas\Acabados;

class TarifaAcabadoModel extends \App\Models\BaseModel
{
    protected $table = "lg_tarifa_acabado";

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

    const SORTABLE = [
        0 => "t1.nombre",
        1 => "precio_min",
        2 => "importe_fijo",
        3 => "t1.mostrar_en_presupuesto",
    ];

    protected $allowedFields = [
        "nombre",
        "code",
        "comment",
        "precio_min",
        "importe_fijo",
        "mostrar_en_presupuesto",
        "mostrar_en_presupuesto_cliente",
        "acabado_cubierta",
        "acabado_sobrecubierta",
        "deleted_at",
        "is_deleted",
        "user_created_id",
        "user_updated_id",
    ];
    protected $returnType = 'App\Entities\Tarifas\Acabados\TarifaAcabadoEntity';

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

    protected $createdField = "created_at";

    protected $updatedField = "updated_at";

    public static $labelField = "nombre";

    protected $validationRules = [
        "nombre" => [
            "label" => "Tarifaacabado.nombre",
            "rules" => "trim|required|max_length[255]",
        ],
        "precio_min" => [
            "label" => "Tarifaacabado.precioMin",
            "rules" => "required|decimal",
        ],
        "importe_fijo" => [
            "label" => "Tarifaacabado.importeFijo",
            "rules" => "required|decimal",
        ],
    ];

    protected $validationMessages = [
        "nombre" => [
            "max_length" => "Tarifaacabado.validation.nombre.max_length",
            "required" => "Tarifaacabado.validation.nombre.required",
        ],
        "precio_min" => [
            "required" => "Tarifaacabado.validation.precio_min.required",
            "decimal" => "Tarifaacabado.validation.precio_min.decimal",
        ],
        "importe_fijo" => [
            "required" => "Tarifaacabado.validation.importe_fijo.required",
            "decimal" => "Tarifaacabado.validation.importe_fijo.decimal",
        ],

    ];

    /**
     * Get resource data.
     *
     * @param string $search
     *
     * @return \CodeIgniter\Database\BaseBuilder
     */
    public function getResource(string $search = "")
    {
        $builder = $this->db->table($this->table . " t1")->select(
            "t1.id AS id, t1.nombre AS nombre,t1.code AS code, t1.precio_min AS precio_min, t1.importe_fijo AS importe_fijo, 
            t1.mostrar_en_presupuesto AS mostrar_en_presupuesto,t1.mostrar_en_presupuesto_cliente AS mostrar_en_presupuesto_cliente, t1.acabado_cubierta AS acabado_cubierta, t1.acabado_sobrecubierta AS acabado_sobrecubierta"
        );

        //JJO
        $builder->where("t1.is_deleted", 0);

        return empty($search)
            ? $builder
            : $builder
                ->groupStart()
                ->like("t1.nombre", $search)
                ->groupEnd();
    }

    public function getServiciosAcabadoSelector()
    {
        /*
            Todos los servicios de acabado activos 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 getTarifaPresupuestoAcabado($tarifa_id, $tirada, $proveedor_id = -1)
    {

        $builder = $this->db
            ->table($this->table . " t1")
            ->select(
                "t1.id AS tarifa_acabado_id, t1.nombre AS tarifa_acabado_nombre, t1.precio_min AS tarifa_precio_min, t1.importe_fijo AS tarifa_importe_fijo,
                t1.acabado_cubierta AS acabado_cubierta, t1.acabado_sobrecubierta AS acabado_sobrecubierta, t2.id AS tarifa_linea_id, t2.tirada_min AS tirada_min, t2.tirada_max AS tirada_max, 
                t2.precio_min AS precio_min, t2.precio_max AS precio_max, t2.margen AS margen, t2.proveedor_id AS proveedor_id, t3.nombre AS proveedor_nombre"
            )
            ->join("tarifa_acabado_lineas t2", "t1.id = t2.tarifa_acabado_id", "left")
            ->join("lg_proveedores t3", "t2.proveedor_id = t3.id", "left")
            ->where("t1.is_deleted", 0)
            //->where("t1.mostrar_en_presupuesto", 1)
            ->where("t2.is_deleted", 0);

        $builder->where('t1.id =', $tarifa_id);
        $builder->where('t2.tirada_min <=', $tirada);
        $builder->where('t2.tirada_max >=', $tirada);

        if ($proveedor_id != -1) {
            $builder->where('t2.proveedor_id', $proveedor_id);
        }
        return $builder->get()->getResultObject();
    }

    public function getNombreTarifaAcabado($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();
    }


    public function getServiciosAcabadoCubierta()
    {
        $builder = $this->db
            ->table($this->table . " t1")
            ->select(
                "t1.id as id, t1.nombre AS label"
            )
            ->where("t1.is_deleted", 0)
            ->where("t1.mostrar_en_presupuesto", 1)
            ->where("t1.acabado_cubierta", 1);

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


    public function getServiciosAcabadoSobrecubierta()
    {
        $builder = $this->db
            ->table($this->table . " t1")
            ->select(
                "t1.id as id, t1.nombre AS label"
            )
            ->where("t1.is_deleted", 0)
            ->where("t1.mostrar_en_presupuesto", 1)
            ->where("t1.acabado_sobrecubierta", 1);

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

    public function getCodeFromId($id = 0)
    {

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

        $data = $builder->get()->getResultObject();
        if (count($data) > 0) {
            return $data[0]->code;
        } else {
            return "";
        }
    }
}
