diff --git a/ci4/app/Controllers/Pedidos/Pedido.php b/ci4/app/Controllers/Pedidos/Pedido.php index 08fd9325..0cefa80a 100755 --- a/ci4/app/Controllers/Pedidos/Pedido.php +++ b/ci4/app/Controllers/Pedidos/Pedido.php @@ -276,6 +276,13 @@ class Pedido extends \App\Controllers\BaseResourceController $this->viewData['pedidoEntity'] = $pedidoEntity; + + if($pedidoEntity->estado == 'validacion'){ + $clienteModel = model('App\Models\Clientes\ClienteModel'); + $pendiente = $clienteModel->getPendienteCobro($pedidoEntity->cliente_id); + $pendiente = $pendiente[0] + $pendiente[1]; + $this->viewData['importePendiente'] = $pendiente; + } $this->viewData['boxTitle'] = lang('Basic.global.edit2') . ' ' . lang('Pedidos.moduleTitle') . ' ' . lang('Basic.global.edit3'); @@ -471,11 +478,12 @@ class Pedido extends \App\Controllers\BaseResourceController $pedido = $this->model->find($pedido_id); $serviceProduction->setPedido($pedido); if($pedido->orden_trabajo()){ - return $this->response->setJSON(["data"=>$pedido->orden_trabajo(),"message" => "Ya existe una orden de trabajo para este pedido"]); + return $this->response->setJSON(["status"=>false,"data"=>$pedido->orden_trabajo(),"message" => "Ya existe una orden de trabajo para este pedido"]); }else{ $r = $serviceProduction->createOrdenTrabajo(); - return $this->response->setJSON(["data"=>$r,"message" => "Orden trabajo creada correctamente"]); + $this->model->set(['estado' => 'produccion'])->where('id', $pedido_id)->update(); + return $this->response->setJSON(["status"=>true, "data"=>$r,"message" => "Orden trabajo creada correctamente"]); } } diff --git a/ci4/app/Language/es/Pedidos.php b/ci4/app/Language/es/Pedidos.php index bb9fc447..aa9c20e2 100644 --- a/ci4/app/Language/es/Pedidos.php +++ b/ci4/app/Language/es/Pedidos.php @@ -14,6 +14,7 @@ return [ 'tiradas' => 'Tiradas', 'total_presupuesto' => 'Total', 'estado' => 'Estado', + 'importePendiente' => 'Importe Pendiente', 'validacion' => 'Validación', 'produccion' => 'Producción', diff --git a/ci4/app/Models/Clientes/ClienteModel.php b/ci4/app/Models/Clientes/ClienteModel.php index 29bcf168..a09c6cd6 100755 --- a/ci4/app/Models/Clientes/ClienteModel.php +++ b/ci4/app/Models/Clientes/ClienteModel.php @@ -309,9 +309,26 @@ class ClienteModel extends \App\Models\BaseModel /* TO-DO: Implementar la lógica de negocio para el crédito disponible */ - public function creditoDisponible($cliente_id) + public function creditoDisponible($cliente_id, $total_pedido = 0) { - return true; + $builder = $this->db + ->table($this->table . " t1") + ->select( + "t1.limite_credito AS limite_credito" + ) + ->where("t1.is_deleted", 0) + ->where("t1.id", $cliente_id); + $limite = $builder->get()->getResultObject(); + + if($limite){ + + $pendiente = $this->getPendienteCobro($cliente_id); + $credito_disponible = floatval($limite[0]->limite_credito) - $pendiente[0] - $pendiente[1] - floatval($total_pedido); + if($credito_disponible < 0) + return false; + return true; + } + return false; } public function getClienteDataFacturas($cliente_id) @@ -403,34 +420,81 @@ class ClienteModel extends \App\Models\BaseModel } - public function getPendienteCobro($cliente_id = -1){ + public function getPendienteCobro($cliente_id = -1) + { $pendiente_old = $this->getTotalPendienteOldERP($cliente_id); - $db = \Config\Database::connect('default'); // Conectar a olderp - $builder = $db->table('vista_importe_pendiente_cliente t1') - ->select('*') - ->where('t1.cliente_id', $cliente_id); + + // Subconsulta para verificar la existencia en facturas_lineas + $subquery_facturas = "(SELECT 1 + FROM facturas_lineas fl + JOIN facturas f2 ON fl.factura_id = f2.id + WHERE (fl.pedido_linea_impresion_id = p.id OR fl.pedido_maquetacion_id = p.id) + AND f2.estado = 'validada')"; + + // Subconsulta para calcular el total de pedidos pendientes + $subquery_total_pedidos = $this->db->table('pedidos p') + ->select('SUM(p.total_precio)', false) + ->join('pedidos_linea pl', 'p.id = pl.pedido_id', 'left') + ->join('presupuestos pr', 'pl.presupuesto_id = pr.id', 'left') + ->where('pr.cliente_id', $cliente_id) + ->whereIn('p.estado', ['produccion', 'finalizado', 'enviado']) + ->where("NOT EXISTS $subquery_facturas", null, false) // Implementación manual de NOT EXISTS + ->getCompiledSelect(); + + // Construcción de la consulta principal + $builder = $this->db->table('(SELECT DISTINCT cliente_id FROM presupuestos UNION SELECT DISTINCT cliente_id FROM facturas) c'); + $builder->select('c.cliente_id'); + $builder->select('COALESCE(SUM(f.pendiente), 0) AS total_facturas'); + $builder->select("COALESCE(($subquery_total_pedidos), 0) AS total_pedidos", false); + $builder->select("COALESCE(SUM(f.pendiente), 0) + COALESCE(($subquery_total_pedidos), 0) AS total_pendiente", false); + $builder->join('facturas f', 'c.cliente_id = f.cliente_id AND f.deleted_at IS NULL AND f.estado = "validada"', 'left'); + $builder->where('c.cliente_id', $cliente_id); + $builder->groupBy('c.cliente_id'); + $query = $builder->get(); $valor = $query->getRow(); - if($valor){ - return [floatval($valor->total_pendiente) , floatval($pendiente_old)]; - }else{ - return [0 , floatval($pendiente_old)]; + + if ($valor) { + return [round(floatval($valor->total_pendiente), 2), round(floatval($pendiente_old), 2)]; + } else { + return [0, round(floatval($pendiente_old), 2)]; } } - private function getTotalPendienteOldERP($customer_id = -1){ + private function getTotalPendienteOldERP($customer_id = -1) + { $db = \Config\Database::connect('old_erp'); // Conectar a olderp + $builder = $db->table('safekat.facturas f'); + $builder->select('f.customer_id'); + $builder->select('SUM(f.pendiente) + COALESCE(( + SELECT SUM(pl.total_raw) + FROM safekat.pedido_libro pl + WHERE pl.factura_id IS NULL + AND pl.estado IN ("aceptado", "finalizado", "validado") + AND pl.total_raw IS NOT NULL + AND pl.total_raw != 0 + AND pl.inc_rei IS NULL + AND pl.customer_id = f.customer_id + ), 0) AS total_pendiente'); + $builder->where('f.deleted_at IS NULL'); + $builder->where('f.estado', 'open'); + $builder->where('f.customer_id', $customer_id); + $builder->groupBy('f.customer_id'); + + $query = $builder->get(); + $valor = $query->getRow(); + /* $builder = $db->table('vista_importe_pendiente_cliente t1') ->select('*') ->where('t1.customer_id', $customer_id); $query = $builder->get(); - $valor = $query->getRow(); - if($valor){ + $valor = $query->getRow();*/ + if ($valor) { return $valor->total_pendiente; - }else{ + } else { return 0; } } diff --git a/ci4/app/Services/PresupuestoService.php b/ci4/app/Services/PresupuestoService.php index c6ee31fb..1cf7524c 100755 --- a/ci4/app/Services/PresupuestoService.php +++ b/ci4/app/Services/PresupuestoService.php @@ -2,6 +2,7 @@ namespace App\Services; +use App\Controllers\Pedidos\Pedido; use CodeIgniter\Config\BaseService; use App\Models\Configuracion\MaquinasTarifasImpresionModel; @@ -1877,7 +1878,7 @@ class PresupuestoService extends BaseService $data_pedido = [ 'total_precio' => $datos_presupuesto->total_aceptado, 'total_tirada' => $datos_presupuesto->tirada, - 'estado' => $model_cliente->creditoDisponible($datos_presupuesto->cliente_id) ? "produccion" : "validacion", + 'estado' => $model_cliente->creditoDisponible($datos_presupuesto->cliente_id, $datos_presupuesto->total_aceptado) ? "produccion" : "validacion", 'user_created_id' => auth()->user()->id, 'user_updated_id' => auth()->user()->id, ]; @@ -1896,6 +1897,9 @@ class PresupuestoService extends BaseService } if ($id_linea != 0 && $pedido_id != 0) { + if($data_pedido['estado'] == "produccion"){ + $response = (new Pedido())->to_produccion($pedido_id); + } return true; } return false; diff --git a/ci4/app/Views/themes/vuexy/form/pedidos/_cabeceraItems.php b/ci4/app/Views/themes/vuexy/form/pedidos/_cabeceraItems.php index 9af77005..85711c1b 100644 --- a/ci4/app/Views/themes/vuexy/form/pedidos/_cabeceraItems.php +++ b/ci4/app/Views/themes/vuexy/form/pedidos/_cabeceraItems.php @@ -56,7 +56,19 @@ - + estado == 'validacion' && (!(auth()->user()->inGroup('cliente-admin') || auth()->user()->inGroup('cliente-editor')))): ?> +
+ + +