falta ordenar por cajas

This commit is contained in:
2025-05-04 13:05:37 +02:00
parent 3f90665c39
commit 39639d9ff8
10 changed files with 519 additions and 60 deletions

View File

@ -7,14 +7,14 @@ use App\Entities\Etiquetas\EtiquetaTituloLinea;
class EtiquetasTitulosLineasModel extends Model
{
protected $table = 'etiquetas_titulos_lineas';
protected $primaryKey = 'id';
protected $table = 'etiquetas_titulos_lineas';
protected $primaryKey = 'id';
protected $useAutoIncrement = true;
protected $returnType = EtiquetaTituloLinea::class;
protected $useSoftDeletes = true;
protected $returnType = EtiquetaTituloLinea::class;
protected $useSoftDeletes = true;
protected $allowedFields = [
protected $allowedFields = [
'etiqueta_titulos_id',
'ot_id',
'unidades',
@ -24,16 +24,17 @@ class EtiquetasTitulosLineasModel extends Model
'user_deleted_at',
];
protected $useTimestamps = true;
protected $createdField = 'created_at';
protected $updatedField = 'updated_at';
protected $deletedField = 'deleted_at';
protected $useTimestamps = true;
protected $createdField = 'created_at';
protected $updatedField = 'updated_at';
protected $deletedField = 'deleted_at';
protected $validationRules = [];
protected $validationRules = [];
protected $validationMessages = [];
protected $skipValidation = false;
protected $skipValidation = false;
protected $beforeDelete = ['addUserDeleted'];
protected $beforeUpdate = ['addUserUpdated'];
protected function addUserDeleted(array $data)
{
@ -46,36 +47,60 @@ class EtiquetasTitulosLineasModel extends Model
if (!empty($data['id'])) {
$builder = $this->builder();
$builder->whereIn($this->primaryKey, (array) $data['id'])
->update(['user_deleted_at' => $userId]);
->update(['user_deleted_at' => $userId]);
}
return $data;
}
public function getDatatableQuery($etiqueta_id, $direccion = null){
protected function addUserUpdated(array $data)
{
$userId = auth()->user()->id;
if (!isset($data['data'])) {
$data['data'] = [];
}
if (!empty($data['id'])) {
$builder = $this->builder();
$builder->whereIn($this->primaryKey, (array) $data['id'])
->update(['user_updated_at' => $userId]);
}
return $data;
}
public function getDatatableQuery($etiqueta_id, $direccion = null)
{
$direccionNormalizada = str_replace(' ', '', strtolower(trim($direccion)));
$direccionSQL = $this->db->escape($direccionNormalizada);
// Subconsulta: suma de pesos por presupuesto
$subPeso = $this->db->table('presupuesto_linea')
->select('presupuesto_id, ROUND(SUM(peso)/1000, 2) as pesoUnidad')
->groupBy('presupuesto_id');
$builder = $this->db->table('etiquetas_titulos_lineas etl')
->select('etl.ot_id as ot, pr.titulo as titulo, etl.unidades as unidades, etl.numero_caja as numero_caja,
pd.cantidad as unidadesTotal, etl.id as id, etl.unidades as unidadesRaw,
(SELECT ROUND(SUM(peso)/1000, 2)
FROM presupuesto_linea
WHERE presupuesto_id = pr.id) AS pesoUnidad')
->select('
etl.ot_id as ot,
pr.titulo as titulo,
etl.unidades as unidades,
etl.numero_caja as numero_caja,
pd.cantidad as unidadesTotal,
etl.id as id,
etl.unidades as unidadesRaw,
peso_sub.pesoUnidad
')
->join('etiquetas_titulos et', 'et.id = etl.etiqueta_titulos_id')
->join('ordenes_trabajo ot', 'ot.id = etl.ot_id')
->join('pedidos p', 'p.id = ot.pedido_id')
->join('pedidos_linea pl', 'pl.pedido_id = p.id')
->join('presupuestos pr', 'pr.id = pl.presupuesto_id')
->join('presupuesto_linea plinea', 'pr.id = plinea.presupuesto_id')
->join("({$subPeso->getCompiledSelect()}) as peso_sub", 'peso_sub.presupuesto_id = pr.id', 'left')
->join('presupuesto_direcciones pd', 'pd.presupuesto_id = pr.id')
->where('etl.deleted_at IS NULL')
->where('et.id', $etiqueta_id)
->where("REPLACE(LOWER(TRIM(pd.direccion)), ' ', '') = $direccionSQL", null, false)
->groupBy('etl.numero_caja');
->where("REPLACE(LOWER(TRIM(pd.direccion)), ' ', '') = '{$direccionNormalizada}'", null, false)
->orderBy('etl.numero_caja');
return $builder;
}
}

View File

@ -35,6 +35,7 @@ class EtiquetasTitulosModel extends Model
protected $beforeDelete = ['addUserDeleted'];
protected $beforeUpdate = ['addUserUpdated'];
protected function addUserDeleted(array $data)
{
@ -52,6 +53,22 @@ class EtiquetasTitulosModel extends Model
return $data;
}
protected function addUserUpdated(array $data)
{
$userId = auth()->user()->id;
if (!isset($data['data'])) {
$data['data'] = [];
}
if (!empty($data['id'])) {
$builder = $this->builder();
$builder->whereIn($this->primaryKey, (array) $data['id'])
->update(['user_updated_at' => $userId]);
}
return $data;
}
public function getEtiquetasTitulos()
{
return $this->db->table('etiquetas_titulos et')