From b6420b7b8acb9b111ebdb706bd6b37d5805de55e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jaime=20Jim=C3=A9nez?= Date: Wed, 10 Jul 2024 00:25:26 +0200 Subject: [PATCH] trabajando boton ver pedido --- ci4/app/Config/Routes.php | 2 + ci4/app/Controllers/Facturacion/Facturas.php | 90 ++++++++++--------- .../Facturacion/FacturasLineas.php | 32 +++++++ .../Entities/Facturas/FacturaLineaEntity.php | 2 +- ci4/app/Models/Facturas/FacturaLineaModel.php | 29 +++++- ci4/app/Models/Facturas/FacturaModel.php | 1 + .../_commonPartialsBs/_modalConfirmDialog.php | 40 ++++++--- .../vuexy/form/facturas/_addPedidosItems.php | 8 +- .../form/facturas/_facturaLineasItems.php | 49 +++++++++- .../vuexy/form/facturas/viewFacturaForm.php | 2 + 10 files changed, 191 insertions(+), 64 deletions(-) diff --git a/ci4/app/Config/Routes.php b/ci4/app/Config/Routes.php index 9ee4c0ed..fdbaf671 100644 --- a/ci4/app/Config/Routes.php +++ b/ci4/app/Config/Routes.php @@ -659,6 +659,8 @@ $routes->group('facturas', ['namespace' => 'App\Controllers\Facturacion'], funct $routes->post('datatable/(:any)', 'FacturasLineas::datatable/$1', ['as' => 'dataTableOfLineasFacturas']); $routes->post('menuPedidosPendientes/(:num)', 'Facturas::menuPedidosPendientes/$1', ['as' => 'menuPedidosPendientesImpresion']); $routes->post('addLineaPedidoImpresion/(:num)', 'Facturas::addLineaPedidoImpresion/$1', ['as' => 'addLineaPedidoImpresion2Factura']); + $routes->post('addLineaPedidoImpresion/(:num)', 'Facturas::addLineaPedidoImpresion/$1', ['as' => 'addLineaPedidoImpresion2Factura']); + $routes->get('deleteLinea/(:any)', 'FacturasLineas::deleteLinea/$1', ['as' => 'deleteLineaFactura']); }); diff --git a/ci4/app/Controllers/Facturacion/Facturas.php b/ci4/app/Controllers/Facturacion/Facturas.php index 7cad1cac..7f673e1e 100755 --- a/ci4/app/Controllers/Facturacion/Facturas.php +++ b/ci4/app/Controllers/Facturacion/Facturas.php @@ -318,55 +318,61 @@ class Facturas extends \App\Controllers\BaseResourceController if ($this->request->isAJAX()) { $model_pedido_linea = model('\App\Models\Pedidos\PedidoLineaModel'); - $model_presupuesto = model('\App\Models\Pedidos\PedidoLineaModel'); + $model_presupuesto = model('\App\Models\Presupuestos\PresupuestoModel'); $model_factura_linea = model('\App\Models\Facturas\FacturaLineaModel'); - $pedido_linea_id = $this->request->getPost('lineaPedido') ?? 0; - $linea = $model_pedido_linea->find($pedido_linea_id); - - if($linea){ - $presupuesto = $model_presupuesto->find($linea->presupuesto_id); - if($presupuesto){ - // Se añade la linea de factura - $descripcion = $model_presupuesto->generarLineaPedido($presupuesto->id, true, $linea->pedido_id); - $cantidad = intval($presupuesto->tirada) - intval($this->model->getCantidadLineaPedidoFacturada($linea->id)); - $base = $cantidad * floatval($presupuesto->total_precio_unidad); - $total_iva = $base * ($presupuesto->iva_reducido==1 ? 0.04 : 0.21); - // se redondea a dos decimales - $total_iva = round($total_iva, 2); - $total = $base + $total_iva; + try{ + $pedido_linea_id = $this->request->getPost('lineaPedido') ?? 0; + $linea = $model_pedido_linea->find($pedido_linea_id); + + if($linea){ + $presupuesto = $model_presupuesto->find($linea->presupuesto_id); + if($presupuesto){ + // Se añade la linea de factura + $descripcion = $model_presupuesto->generarLineaPedido($presupuesto->id, true, $linea->pedido_id); + $cantidad = intval($presupuesto->tirada) - intval($this->model->getCantidadLineaPedidoFacturada($linea->id)); + $base = $cantidad * floatval($presupuesto->total_precio_unidad); + $total_iva = $base * ($presupuesto->iva_reducido==1 ? 0.04 : 0.21); + // se redondea a dos decimales + $total_iva = round($total_iva, 2); + $total = $base + $total_iva; - $data = (object)[ - 'factura_id'=>$factura_id, - 'pedido_linea_impresion_id'=>$linea->pedido_linea_impresion_id, - 'descripcion'=>$descripcion, - 'cantidad'=>$cantidad, - 'precio_unidad'=>$presupuesto->total_precio_unidad, - 'iva' => $presupuesto->iva_reducido==1 ? 4 : 21, - 'base' => $base, - 'total_iva' => $total_iva, - 'total' => $total, - 'user_updated_id' => auth()->user()->id, - ]; - - $model_factura_linea->insert($data); - - $id = $model_factura_linea->getInsertID(); - - if($id){ - - - $newTokenHash = csrf_hash(); - $csrfTokenName = csrf_token(); - $data = [ - 'error' => 0, - 'id' => $id, - $csrfTokenName => $newTokenHash + $data = (object)[ + 'factura_id'=>$factura_id, + 'pedido_linea_impresion_id'=>$linea->pedido_id, + 'descripcion'=>$descripcion[0]->concepto, + 'cantidad'=>$cantidad, + 'precio_unidad'=>$presupuesto->total_precio_unidad, + 'iva' => $presupuesto->iva_reducido==1 ? 4 : 21, + 'base' => $base, + 'total_iva' => $total_iva, + 'total' => $total, + 'user_updated_id' => auth()->user()->id, ]; - return $this->respond($data); + + $model_factura_linea->insert($data); + + $id = $model_factura_linea->getInsertID(); + + if($id){ + + $model_factura_linea->addFacturaPedidoLinea($factura_id, $linea->id, $cantidad); + + $newTokenHash = csrf_hash(); + $csrfTokenName = csrf_token(); + $data = [ + 'error' => 0, + 'id' => $id, + $csrfTokenName => $newTokenHash + ]; + return $this->respond($data); + } } } } + catch(Exception $e){ + + } $newTokenHash = csrf_hash(); $csrfTokenName = csrf_token(); diff --git a/ci4/app/Controllers/Facturacion/FacturasLineas.php b/ci4/app/Controllers/Facturacion/FacturasLineas.php index 7d490cc1..18b8466c 100644 --- a/ci4/app/Controllers/Facturacion/FacturasLineas.php +++ b/ci4/app/Controllers/Facturacion/FacturasLineas.php @@ -42,4 +42,36 @@ class FacturasLineas extends \App\Controllers\BaseResourceController return $this->failUnauthorized('Invalid request', 403); } } + + + public function deleteLinea($factura_linea_id = 0){ + + if (!empty(static::$pluralObjectNameCc) && !empty(static::$singularObjectNameCc)) { + $objName = mb_strtolower(lang(ucfirst(static::$pluralObjectNameCc).'.'.static::$singularObjectNameCc)); + } else { + $objName = lang('Basic.global.record'); + } + + if($factura_linea_id == 0){ + return $this->failNotFound(lang('Basic.global.deleteError', [$objName])); + } + + $facturaLinea = $this->model->find($factura_linea_id); + if($facturaLinea == null){ + return $this->failNotFound(lang('Basic.global.deleteError', [$objName])); + } + + if($facturaLinea->pedido_linea_impresion_id != null){ + $this->model->deleteFacturasLineasPedido($facturaLinea->factura_id, $facturaLinea->pedido_linea_impresion_id, $facturaLinea->cantidad); + } + + if($facturaLinea->pedido_maquetacion_id != null){ + //$this->model->deleteFacturasLineasPedido($facturaLinea->factura_id, $facturaLinea->pedido_maquetacion_id, $facturaLinea->cantidad); + } + + $facturaLinea = $this->model->delete($factura_linea_id); + $message = lang('Basic.global.deleteSuccess', [lang('Basic.global.record')]); + $response = $this->respondDeleted(['id' => $factura_linea_id, 'msg' => $message]); + return $response; + } } \ No newline at end of file diff --git a/ci4/app/Entities/Facturas/FacturaLineaEntity.php b/ci4/app/Entities/Facturas/FacturaLineaEntity.php index 4cef1cd7..7ce467bb 100644 --- a/ci4/app/Entities/Facturas/FacturaLineaEntity.php +++ b/ci4/app/Entities/Facturas/FacturaLineaEntity.php @@ -19,7 +19,7 @@ class FacturaLineaEntity extends \CodeIgniter\Entity\Entity 'total' => null, 'data' => null, 'deleted_at' => null, - 'user_update_id' => null, + 'user_updated_id' => null, ]; diff --git a/ci4/app/Models/Facturas/FacturaLineaModel.php b/ci4/app/Models/Facturas/FacturaLineaModel.php index f27fe5c9..25fd90ec 100644 --- a/ci4/app/Models/Facturas/FacturaLineaModel.php +++ b/ci4/app/Models/Facturas/FacturaLineaModel.php @@ -21,7 +21,7 @@ class FacturaLineaModel extends \App\Models\BaseModel { 'total', 'data', 'deleted_at', - 'user_update_id' + 'user_updated_id' ]; protected $returnType = "App\Entities\Facturas\FacturaLineaEntity"; @@ -38,11 +38,32 @@ class FacturaLineaModel extends \App\Models\BaseModel { ->select( "t1.id AS id, t1.factura_id AS factura_id, t1.pedido_linea_impresion_id AS pedido_linea_impresion_id, t1.pedido_maquetacion_id AS pedido_maquetacion_id, - t1.descripcion AS concepto, t1.cantidad as cantidad, t1.precio_unidad AS precio_unidad, t1.iva AS iva, - t1.base AS base, t1.total_iva AS total_iva, t1.total AS total, t1.data AS data," + t1.descripcion AS concepto, t1.cantidad as unidades, t1.precio_unidad AS precio_unidad, t1.iva AS iva, + t1.base AS subtotal, t1.total_iva AS total_iva, t1.total AS total, t1.data AS data," ) - ->where("t1.factura_id", $factura_id); + ->where("t1.factura_id", $factura_id) + ->where("t1.deleted_at", null); return $builder; } + + public function addFacturaPedidoLinea($factura_id, $pedido_linea_id, $cantidad) + { + $data = [ + "factura_id" => $factura_id, + "pedido_linea_id" => $pedido_linea_id, + "cantidad" => $cantidad + ]; + + return $this->db->table("facturas_pedidos_lineas")->insert($data); + } + + public function deleteFacturasLineasPedido($factura_id, $pedido_linea_id, $cantidad){ + + $this->db->table("facturas_pedidos_lineas") + ->where("factura_id", $factura_id) + ->where("pedido_linea_id", $pedido_linea_id) + ->where("cantidad", $cantidad) + ->delete(); + } } \ No newline at end of file diff --git a/ci4/app/Models/Facturas/FacturaModel.php b/ci4/app/Models/Facturas/FacturaModel.php index 69eaf487..30e61341 100644 --- a/ci4/app/Models/Facturas/FacturaModel.php +++ b/ci4/app/Models/Facturas/FacturaModel.php @@ -98,4 +98,5 @@ class FacturaModel extends \App\Models\BaseModel { return $builder->get()->getRow()->cantidad; } + } \ No newline at end of file diff --git a/ci4/app/Views/themes/_commonPartialsBs/_modalConfirmDialog.php b/ci4/app/Views/themes/_commonPartialsBs/_modalConfirmDialog.php index e2b6d8c3..37339425 100755 --- a/ci4/app/Views/themes/_commonPartialsBs/_modalConfirmDialog.php +++ b/ci4/app/Views/themes/_commonPartialsBs/_modalConfirmDialog.php @@ -26,20 +26,34 @@ function asyncConfirmDialog(title, msg, yesCallbackFn, noCallbackFn) { - var $confirmDialog = $("#modalConfirmYesNo"); - $confirmDialog.modal('show'); - $("#labelTitleConfirmDialog").html(title); - $("#labelMsgConfirmDialog").html(msg); - $("#btnYesConfirmDialog").off('click').click(function () { - yesCallbackFn(); - $confirmDialog.modal("hide"); - }); - $("#btnNoConfirmDialog").off('click').click(function () { - noCallbackFn(); - $confirmDialog.modal("hide"); - }); - } + var $confirmDialog = $("#modalConfirmYesNo"); + $confirmDialog.modal('show'); + $("#labelTitleConfirmDialog").html(title); + $("#labelMsgConfirmDialog").html(msg); + $("#btnYesConfirmDialog").off('click').click(function () { + yesCallbackFn(); + $confirmDialog.modal("hide"); + }); + $("#btnNoConfirmDialog").off('click').click(function () { + noCallbackFn(); + $confirmDialog.modal("hide"); + }); +} +function asyncConfirmDialogWithParams(title, msg, yesCallbackFn, noCallbackFn, params) { + var $confirmDialog = $("#modalConfirmYesNo"); + $confirmDialog.modal('show'); + $("#labelTitleConfirmDialog").html(title); + $("#labelMsgConfirmDialog").html(msg); + $("#btnYesConfirmDialog").off('click').click(function () { + yesCallbackFn(params); + $confirmDialog.modal("hide"); + }); + $("#btnNoConfirmDialog").off('click').click(function () { + noCallbackFn(params); + $confirmDialog.modal("hide"); + }); +} endSection() ?> diff --git a/ci4/app/Views/themes/vuexy/form/facturas/_addPedidosItems.php b/ci4/app/Views/themes/vuexy/form/facturas/_addPedidosItems.php index a6cd7141..35e3ae98 100644 --- a/ci4/app/Views/themes/vuexy/form/facturas/_addPedidosItems.php +++ b/ci4/app/Views/themes/vuexy/form/facturas/_addPedidosItems.php @@ -116,7 +116,13 @@ $('#addNewPedidoImpresion').on('click', function(){ }, success: function(response) { yeniden(response.); - $('#lineaPedido').val(null).trigger('change'); + $('#pedidoImpresion').val(null).trigger('change'); + // Se actualiza la tabla de lineas de factura + $('#tableOfLineasFactura').DataTable().clearPipeline(); + $('#tableOfLineasFactura').DataTable().ajax.reload(); + // se ajustan el ancho de las columnas + $('#tableOfLineasFactura').DataTable().columns.adjust().draw(); + } }); }); diff --git a/ci4/app/Views/themes/vuexy/form/facturas/_facturaLineasItems.php b/ci4/app/Views/themes/vuexy/form/facturas/_facturaLineasItems.php index e1a65e87..82739ba0 100644 --- a/ci4/app/Views/themes/vuexy/form/facturas/_facturaLineasItems.php +++ b/ci4/app/Views/themes/vuexy/form/facturas/_facturaLineasItems.php @@ -52,9 +52,14 @@ const actionBtns = function(data) { return ` -
- - +
+
+ + +
+
+
+
`; }; @@ -160,5 +165,43 @@ var tableLineas = $('#tableOfLineasFactura').DataTable({ }); +// Delete row +$(document).on('click', '.btn-delete', function(e) { + //$(".btn-remove").attr('data-id', $(this).attr('data-id')); + const dataId = $(this).attr('data-id'); + const row = $(this).closest('tr'); + if ($.isNumeric(dataId)) { + asyncConfirmDialogWithParams( + "Borrar Linea de Factura", + "¿Está seguro de borrar la línea? Esta acción no se puede deshacer.", + deleteConfirmed, null, [dataId, row]) + } +}); + + +function deleteConfirmed(params){ + var factura_linea_id = params[0]; + var row = params[1]; + var url = ''; + url = url.replace(':id', factura_linea_id ); + $.ajax({ + url: url, + method: 'GET', + }).done((data, textStatus, jqXHR) => { + $('#tableOfLineasFactura').DataTable().clearPipeline(); + $('#tableOfLineasFactura').DataTable().row($(row)).invalidate().draw(); + }).fail((jqXHR, textStatus, errorThrown) => { + popErrorAlert(jqXHR.responseJSON.messages.error) + }) +} + + +$('.btn-view_pedido').on('click', function(e){ + var pedido_linea_id = $(this).data('id'); + var url = ''; + url = url.replace(':id', pedido_linea_id ); + window.open(url, '_blank'); +}); + endSection() ?> \ No newline at end of file diff --git a/ci4/app/Views/themes/vuexy/form/facturas/viewFacturaForm.php b/ci4/app/Views/themes/vuexy/form/facturas/viewFacturaForm.php index fd8ad424..4f3cf2cd 100644 --- a/ci4/app/Views/themes/vuexy/form/facturas/viewFacturaForm.php +++ b/ci4/app/Views/themes/vuexy/form/facturas/viewFacturaForm.php @@ -34,6 +34,8 @@
+ +