mirror of
https://git.imnavajas.es/jjimenez/safekat.git
synced 2025-07-25 22:52:08 +00:00
falta ordenar por cajas
This commit is contained in:
@ -21,13 +21,13 @@ class EtiquetasTitulosService
|
||||
->join('pedidos_linea pl', 'p.id = pl.pedido_id')
|
||||
->join('presupuestos pr', 'pr.id = pl.presupuesto_id')
|
||||
->join('orden_trabajo_dates ot_dates', 'ot_dates.orden_trabajo_id = ot.id')
|
||||
->whereIn('p.estado', ['finalizado', 'produccion'])
|
||||
->where('ot_dates.embalaje_at IS NOT NULL');
|
||||
->whereIn('p.estado', ['finalizado', 'produccion']);
|
||||
|
||||
return $builder;
|
||||
}
|
||||
|
||||
public static function getDireccionesOT($ot_id){
|
||||
public static function getDireccionesOT($ot_id)
|
||||
{
|
||||
|
||||
$db = \Config\Database::connect();
|
||||
|
||||
@ -42,7 +42,6 @@ class EtiquetasTitulosService
|
||||
->join('ordenes_trabajo ot', 'ot.pedido_id = p.id')
|
||||
->join('orden_trabajo_dates ot_dates', 'ot_dates.orden_trabajo_id = ot.id')
|
||||
->whereIn('p.estado', ['finalizado', 'produccion'])
|
||||
->where('ot_dates.embalaje_at IS NOT NULL')
|
||||
->where('ot.id', $ot_id);
|
||||
|
||||
return $builder;
|
||||
@ -51,7 +50,7 @@ class EtiquetasTitulosService
|
||||
public static function addEtiqueta($data)
|
||||
{
|
||||
$db = \Config\Database::connect();
|
||||
|
||||
|
||||
$builder = $db->table('presupuesto_direcciones pd');
|
||||
$builder->select('pd.att, pd.direccion, pd.cantidad, pr.cliente_id');
|
||||
$builder->join('presupuestos pr', 'pr.id = pd.presupuesto_id');
|
||||
@ -75,8 +74,8 @@ class EtiquetasTitulosService
|
||||
'user_created_at' => $data['user_id'],
|
||||
]);
|
||||
$etiquetaId = $modelEtiquetasTitulos->getInsertID();
|
||||
|
||||
if($etiquetaId == null){
|
||||
|
||||
if ($etiquetaId == null) {
|
||||
return [
|
||||
'status' => false,
|
||||
'message' => lang('Logistica.errorInsertarEtiqueta'),
|
||||
@ -85,23 +84,108 @@ class EtiquetasTitulosService
|
||||
|
||||
$cantidad_restante = intval($data['cantidad']);
|
||||
$numero_caja = 1;
|
||||
while($cantidad_restante > 0){
|
||||
while ($cantidad_restante > 0) {
|
||||
$modelEtiquetasTitulosLineas = model('App\Models\Etiquetas\EtiquetasTitulosLineasModel');
|
||||
$modelEtiquetasTitulosLineas->insert([
|
||||
'etiqueta_titulos_id' => $etiquetaId,
|
||||
'ot_id' => $data['ot_id'],
|
||||
'unidades' => $cantidad_restante - intval($data['unidades_caja']) < 0 ? $cantidad_restante : intval($data['unidades_caja']),
|
||||
'numero_caja' => $numero_caja,
|
||||
'numero_caja' => $numero_caja,
|
||||
'user_created_at' => $data['user_id'],
|
||||
]);
|
||||
$cantidad_restante -= $data['unidades_caja'];
|
||||
$numero_caja++;
|
||||
}
|
||||
|
||||
|
||||
|
||||
return [
|
||||
'status' => true,
|
||||
'etiqueta' => $etiquetaId,
|
||||
];
|
||||
}
|
||||
|
||||
public static function findOTsWithAddress(int $etiqueta_id)
|
||||
{
|
||||
$db = \Config\Database::connect();
|
||||
|
||||
// 1. Dirección del envío actual
|
||||
$etiqueta = $db->table('etiquetas_titulos')->select('direccion')->where('id', $etiqueta_id)->get()->getRow();
|
||||
if (!$etiqueta) {
|
||||
return $db->table('(SELECT NULL AS id, NULL AS name, NULL as unidades) AS empty')->where('1 = 0');
|
||||
}
|
||||
|
||||
$direccionNormalizada = str_replace(' ', '', strtolower(trim($etiqueta->direccion)));
|
||||
$direccionSQL = $db->escape($direccionNormalizada);
|
||||
|
||||
// 2. Obtener presupuestos con esa dirección
|
||||
$presupuestosConEsaDireccion = $db->table('presupuesto_direcciones')
|
||||
->select('presupuesto_id')
|
||||
->where("REPLACE(LOWER(TRIM(direccion)), ' ', '') = $direccionSQL", null, false)
|
||||
->get()
|
||||
->getResultArray();
|
||||
|
||||
$presupuestoIds = array_column($presupuestosConEsaDireccion, 'presupuesto_id');
|
||||
if (empty($presupuestoIds)) {
|
||||
return $db->table('(SELECT NULL AS id, NULL AS name, NULL as unidades) AS empty')->where('1 = 0');
|
||||
}
|
||||
|
||||
// 3. Subconsulta principal
|
||||
$subBuilder = $db->table('pedidos_linea pl')
|
||||
->select("
|
||||
ot.id AS id,
|
||||
CONCAT('[', ot.id, '] - ', pr.titulo) AS name,
|
||||
pd.cantidad AS description
|
||||
")
|
||||
->join('pedidos p', 'p.id = pl.pedido_id')
|
||||
->join('presupuestos pr', 'pr.id = pl.presupuesto_id')
|
||||
->join('presupuesto_direcciones pd', 'pd.presupuesto_id = pr.id')
|
||||
->join('ordenes_trabajo ot', 'ot.pedido_id = p.id')
|
||||
->join('orden_trabajo_dates ot_dates', 'ot_dates.orden_trabajo_id = ot.id')
|
||||
->whereIn('pr.id', $presupuestoIds)
|
||||
->whereIn('p.estado', ['finalizado', 'produccion'])
|
||||
->groupBy('pl.id');
|
||||
|
||||
// 4. Envolver y filtrar por unidades pendientes
|
||||
$builder = $db->table("({$subBuilder->getCompiledSelect(false)}) AS sub");
|
||||
$builder->select('id, name, description');
|
||||
|
||||
return $builder;
|
||||
}
|
||||
|
||||
public static function addLineasEtiqueta($etiqueta_id, $ot_id, $unidades, $cajas)
|
||||
{
|
||||
|
||||
$unidades_caja = intdiv($unidades, $cajas);
|
||||
$unidades_caja = $unidades_caja == 0 ? $unidades : $unidades_caja;
|
||||
|
||||
$unidades_restantes = $unidades;
|
||||
|
||||
$modelEtitquetaLinea = model('\App\Models\Etiquetas\EtiquetasTitulosLineasModel');
|
||||
$next_caja = $modelEtitquetaLinea
|
||||
->selectMax('numero_caja')
|
||||
->where('etiqueta_titulos_id', $etiqueta_id)
|
||||
->first();
|
||||
$next_caja = $next_caja->numero_caja ?? 0;
|
||||
$next_caja++;
|
||||
|
||||
$user_id = auth()->user()->id;
|
||||
|
||||
while($unidades_restantes > 0){
|
||||
$modelEtitquetaLinea->insert([
|
||||
'etiqueta_titulos_id' => $etiqueta_id,
|
||||
'ot_id' => $ot_id,
|
||||
'unidades' => $unidades_restantes - $unidades_caja < 0 ? $unidades_restantes : intval($unidades_caja),
|
||||
'numero_caja' => $next_caja,
|
||||
'user_created_at' => $user_id,
|
||||
]);
|
||||
$unidades_restantes -= $unidades_caja;
|
||||
$next_caja++;
|
||||
}
|
||||
|
||||
return [
|
||||
'status' => true,
|
||||
'message' => lang('Logistica.success.successInsertLines'),
|
||||
];
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user