Files
safekat/ci4/app/Models/Logistica/EnvioLineaModel.php

70 lines
2.2 KiB
PHP

<?php
namespace App\Models\Logistica;
use CodeIgniter\Model;
use CodeIgniter\Database\BaseBuilder;
class EnvioLineaModel extends Model
{
protected $table = 'envios_lineas';
protected $primaryKey = 'id';
protected $useAutoIncrement = true;
protected $returnType = \App\Entities\Logistica\EnvioLineaEntity::class;
protected $useSoftDeletes = false;
protected $allowedFields = [
'envio_id',
'pedido_id',
'unidades_envio',
'unidades_total',
'created_at',
'updated_at',
'created_by',
'updated_by',
'presupuesto_id',
];
protected $useTimestamps = true;
protected $createdField = 'created_at';
protected $updatedField = 'updated_at';
public function getDatatableQuery($envio_id = null): BaseBuilder
{
$builder = $this->db
->table($this->table . " t1")
->select(
"t1.id, t1.pedido_id as pedido, t3.id as presupuesto,
t3.titulo as titulo, t1.unidades_envio as unidadesEnvio, t1.unidades_envio as unidadesEnvioRaw,
t1.unidades_total as unidadesTotal,
IFNULL((
SELECT SUM(t_sub.unidades_envio)
FROM envios_lineas t_sub
JOIN envios e ON e.id = t_sub.envio_id
WHERE e.finalizado = 1
AND t_sub.pedido_id = t1.pedido_id
AND e.direccion = t2.direccion COLLATE utf8mb3_general_ci
AND (
t_sub.envio_id <> t1.envio_id
OR (t_sub.envio_id = t1.envio_id AND e.finalizado = 1)
)
), 0) AS unidadesEnviadas,
IFNULL((
SELECT ROUND(SUM(peso) / 1000, 1)
FROM presupuesto_linea
WHERE presupuesto_id = t3.id
), 0) AS pesoUnidad,
t2.finalizado as finalizado,"
);
$builder->join("envios t2", "t1.envio_id = t2.id", "left");
$builder->join("presupuestos t3", "t1.presupuesto_id = t3.id", "left");
$builder->where("t1.envio_id", $envio_id);
return $builder;
}
}