<?php
namespace App\Models\Configuracion;

class SeriesFacturasModel extends \App\Models\BaseModel
{
    protected $table = "series";

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

    const SORTABLE = [
        1 => "t1.id",
        2 => "t1.nombre",
        3 => "t1.tipo",
        4 => "t1.formato",
        5 => "t1.next",
        6 => "t1.grupo"
    ];

    protected $allowedFields = ["nombre", "tipo", "formato", "next", "grupo"];
    protected $returnType = "App\Entities\Configuracion\SeriesFacturasEntity";

    public static $labelField = "nombre";

    protected $validationRules = [
        "nombre" => [
            "label" => "SeriesFacturas.nombre",
            "rules" => "trim|required|max_length[255]",
        ],
        "tipo" => [
            "label" => "SeriesFacturas.tipo",
            "rules" => "required",
        ],
        "formato" => [
            "label" => "SeriesFacturas.nombre",
            "rules" => "trim|required|max_length[255]",
        ],
        "next" => [
            "label" => "SeriesFacturas.next",
            "rules" => "required",
        ]
    ];

    protected $validationMessages = [
        "nombre" => [
            "max_length" => "SeriesFacturas.validation.nombre.max_length",
            "required" => "SeriesFacturas.validation.nombre.required",
        ],
        "tipo" => [
            "required" => "SeriesFacturas.validation.tipo.required",
        ],
        "formato" => [
            "max_length" => "SeriesFacturas.validation.formato.max_length",
            "required" => "SeriesFacturas.validation.formato.required",
        ],
        "next" => [
            "required" => "SeriesFacturas.validation.next.required",
        ],

    ];

    /**
     * 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.tipo AS tipo, t1.formato AS formato, 
                t1.next AS next, t1.grupo AS grupo"
            );

        return empty($search)
            ? $builder
            : $builder
                ->groupStart()
                ->like("t1.id", $search)
                ->orLike("t1.nombre", $search)
                ->orLike("t1.tipo", $search)
                ->orLike("t1.formato", $search)
                ->orLike("t1.next", $search)
                ->orLike("t1.grupo", $search)
                ->groupEnd();
    }

    public function getMenuItemsFacturas(){

        $resultSorting = $this->getPrimaryKeyName();

        $id = 'id AS id';
        $text = 'nombre AS text';
        
        $queryBuilder = $this->db->table($this->table);
        $queryBuilder->select([$id, $text]);
        $queryBuilder->where('tipo', 'facturacion');
        $queryBuilder->where('grupo', '1');
        $queryBuilder->orderBy($resultSorting);
        $result =  $queryBuilder->get()->getResult();

        return $result;
    }

    public function getSerieNumerada($id)
    {
        $number = $this->db->table($this->table)
            ->select("next, formato")
            ->where("id", $id)
            ->get()->getFirstRow();
        
        $this->db->table($this->table)
            ->where("id", $id)
            ->set("next", $number->next + 1, false)
            ->update();

        return str_replace("{number}", $number->next, $number->formato);
    }
}
