<?php

namespace App\Models\Pedidos;

class AlbaranLineaModel extends \App\Models\BaseModel
{
    protected $table = "albaranes_lineas";

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

    protected $primaryKey = 'id';
    protected $returnType = 'App\Entities\Pedidos\AlbaranLineaEntity';
    protected $allowedFields = [
        'albaran_id',
        'titulo',
        'isbn',
        'ref_cliente',
        'cantidad',
        'cajas',
        'ejemplares_por_caja',
        'precio_unidad',
        'total',
        'user_created_id',
        'user_updated_id',
        'created_at',
        'updated_at',
        'deleted_at',
    ];

    protected $useSoftDeletes = true;
    protected $useTimestamps = true;
    protected $createdField = 'created_at';
    protected $updatedField = 'updated_at';
    protected $deletedField  = 'deleted_at';

    /**
     * Get resource data for creating PDFs.
     *
     * @param string $search
     *
     * @return \CodeIgniter\Database\BaseBuilder
     */
    public function getResourceForPdf($albaran_id = -1)
    {
        $builder = $this->db

            ->table($this->table . " t1")
            ->select(
                "t1.id AS id, t1.albaran_id AS albaran_id, t1.titulo AS titulo, t1.isbn AS isbn, 
                t1.ref_cliente AS ref_cliente, t1.cantidad AS cantidad, t1.cajas AS cajas, 
                t1.ejemplares_por_caja AS ejemplares_por_caja, t1.precio_unidad AS precio_unidad, 
                t1.total AS total"
            );

        $builder->where("t1.deleted_at IS NULL");
        $builder->where("t1.albaran_id", $albaran_id);

        return $builder;
    }

}