diff --git a/ci4/app/Controllers/Logistica/LogisticaController.php b/ci4/app/Controllers/Logistica/LogisticaController.php index 19444426..d9b41e7b 100755 --- a/ci4/app/Controllers/Logistica/LogisticaController.php +++ b/ci4/app/Controllers/Logistica/LogisticaController.php @@ -104,8 +104,8 @@ class LogisticaController extends BaseController $query = LogisticaService::findLineaEnvioPorEnvio($this->request->getGet('envio')); if ($this->request->getGet("q")) { $query->groupStart() - ->orLike("p.id", $this->request->getGet("q")) - ->orLike("pr.titulo", $this->request->getGet("q")) + ->orLike("id", $this->request->getGet("q")) + ->orLike("name", $this->request->getGet("q")) ->groupEnd(); } diff --git a/ci4/app/Services/LogisticaService.php b/ci4/app/Services/LogisticaService.php index de7440f7..e1196579 100644 --- a/ci4/app/Services/LogisticaService.php +++ b/ci4/app/Services/LogisticaService.php @@ -95,7 +95,9 @@ class LogisticaService } - $response_envio = LogisticaService::generateEnvio($result[0]->pedido_id); + $direccion = $PresupuestoDireccionesModel->select('direccion')->where('presupuesto_id', $result[0]->presupuesto_id) + ->first()->direccion; + $response_envio = LogisticaService::generateEnvio($result[0]->pedido_id, $direccion); if ($response_envio['status'] == false) { $response = [ 'status' => false, @@ -104,7 +106,7 @@ class LogisticaService return $response; } else { $response['data']->id_envio = $response_envio['data']['id_envio']; - $response['data']->multienvio = $response_envio['data']['multienvio']; + $response['data']->multienvio = false; } return $response; @@ -116,16 +118,16 @@ class LogisticaService { $db = \Config\Database::connect(); - // 1. Obtener dirección del envío actual + // 1. Dirección del envío actual $envio = $db->table('envios')->select('direccion')->where('id', $envio_id)->get()->getRow(); if (!$envio) { - return []; + return $db->table('(SELECT NULL AS id, NULL AS name) AS empty')->where('1 = 0'); } $direccionNormalizada = str_replace(' ', '', strtolower(trim($envio->direccion))); $direccionSQL = $db->escape($direccionNormalizada); - // 2. Obtener los presupuesto_id asociados a esa dirección + // 2. Obtener presupuestos con esa dirección $presupuestosConEsaDireccion = $db->table('presupuesto_direcciones') ->select('presupuesto_id') ->where("REPLACE(LOWER(TRIM(direccion)), ' ', '') = $direccionSQL", null, false) @@ -134,10 +136,10 @@ class LogisticaService $presupuestoIds = array_column($presupuestosConEsaDireccion, 'presupuesto_id'); if (empty($presupuestoIds)) { - return []; + return $db->table('(SELECT NULL AS id, NULL AS name) AS empty')->where('1 = 0'); } - // 3. Subconsulta principal con unidades pendientes + // 3. Subconsulta principal $subBuilder = $db->table('pedidos_linea pl') ->select(" pl.id AS id, @@ -158,10 +160,18 @@ class LogisticaService ->join('presupuesto_direcciones pd', 'pd.presupuesto_id = pr.id') ->whereIn('pr.id', $presupuestoIds) ->whereIn('p.estado', ['finalizado', 'produccion']) - ->where("REPLACE(LOWER(TRIM(pd.direccion)), ' ', '') = $direccionSQL", null, false) + ->where("NOT EXISTS ( + SELECT 1 + FROM envios_lineas el + WHERE el.envio_id = $envio_id + AND el.pedido_id = p.id + AND el.presupuesto_id = pr.id + GROUP BY el.pedido_id, el.presupuesto_id + HAVING SUM(el.unidades_envio) >= pd.cantidad + )", null, false) ->groupBy('pl.id'); - // 4. Envolver y filtrar por unidades pendientes reales + // 4. Envolver y filtrar por unidades pendientes $builder = $db->table("({$subBuilder->getCompiledSelect(false)}) AS sub"); $builder->select('id, name'); $builder->where('cantidad > unidades_enviadas'); @@ -175,6 +185,7 @@ class LogisticaService + public static function addLineaEnvio($envio_id = null, $pedido_id = null, $direccion = null) { $db = \Config\Database::connect();