diff --git a/ci4/app/Controllers/Facturacion/Facturas.php b/ci4/app/Controllers/Facturacion/Facturas.php index b9c445bc..d8feb554 100755 --- a/ci4/app/Controllers/Facturacion/Facturas.php +++ b/ci4/app/Controllers/Facturacion/Facturas.php @@ -230,6 +230,10 @@ class Facturas extends \App\Controllers\BaseResourceController $factura->updated_by = $userModel->getFullName($factura->user_updated_id); $factura->created_at_footer = $factura->created_at ? date(' H:i d/m/Y', strtotime($factura->created_at)) : ''; $factura->updated_at_footer = $factura->updated_at ? date(' H:i d/m/Y', strtotime($factura->updated_at)) : ''; + + $factura->showDeleteButton = model('App\Models\Facturas\FacturaPagoModel') + ->where('factura_id', $factura->id)->countAllResults() == 0; + $this->viewData['facturaEntity'] = $factura; $this->viewData['boxTitle'] = lang('Basic.global.edit2') . ' ' . lang('Facturas.factura') . ' ' . lang('Basic.global.edit3'); @@ -703,7 +707,7 @@ class Facturas extends \App\Controllers\BaseResourceController $data = (object) [ 'factura_id' => $factura_id, - 'pedido_linea_impresion_id' => $linea->pedido_id, + 'pedido_linea_impresion_id' => $pedido_linea_id, 'descripcion' => $descripcion[0]->concepto, 'cantidad' => $cantidad, 'iva' => $presupuesto->iva_reducido == 1 ? 4 : 21, diff --git a/ci4/app/Controllers/Presupuestos/Presupuestoadmin.php b/ci4/app/Controllers/Presupuestos/Presupuestoadmin.php index beef112f..75ac450d 100644 --- a/ci4/app/Controllers/Presupuestos/Presupuestoadmin.php +++ b/ci4/app/Controllers/Presupuestos/Presupuestoadmin.php @@ -366,6 +366,36 @@ class Presupuestoadmin extends \App\Controllers\BaseResourceController PresupuestoService::crearPedido($id); } + // modificar los datos del pedido y de la factura si no está la factura validada + if ($presupuestoEntity->estado_id == 2){ + $facturaModel = model('App\Models\Facturas\FacturaModel'); + if(!$facturaModel->presupuestoHasFacturaValidada($id)){ + // se actualiza primero el pedido + $pedidoModel = model('App\Models\Pedidos\PedidoLineaModel'); + $pedidoLineaId = $pedidoModel->where('presupuesto_id', $id)->first()->id; + $linea_pedido = $this->model->generarLineaPedido($id)[0]; + $idPedido = $pedidoModel->join('pedidos', 'pedidos_linea.pedido_id = pedidos.id') + ->where('pedidos_linea.presupuesto_id', $id) + ->first()->pedido_id; + $pedidoModel->update($pedidoLineaId, [ + 'cantidad' => $linea_pedido->unidades, + 'descripcion' => $linea_pedido->concepto + ]); + + // se actualiza la factura + $linea_pedido = $this->model->generarLineaPedido($id, true, $idPedido)[0]; + $facturaLineaModel = model('App\Models\Facturas\FacturaLineaModel'); + $facturaLineaId = $facturaLineaModel->where('pedido_linea_impresion_id', $pedidoLineaId)-> + where('deleted_at', null)->first()->id; + $facturaLineaModel->update($facturaLineaId, [ + 'cantidad' => $linea_pedido->unidades, + 'descripcion' => $linea_pedido->concepto + ]); + + } + + } + $newTokenHash = csrf_hash(); $csrfTokenName = csrf_token(); $data = [ diff --git a/ci4/app/Database/Migrations/2025-04-11-180001_AddUserIdFechaEntregaPedido.php b/ci4/app/Database/Migrations/2025-04-11-180001_AddUserIdFechaEntregaPedido copy.php similarity index 100% rename from ci4/app/Database/Migrations/2025-04-11-180001_AddUserIdFechaEntregaPedido.php rename to ci4/app/Database/Migrations/2025-04-11-180001_AddUserIdFechaEntregaPedido copy.php diff --git a/ci4/app/Database/Migrations/2025-04-12-130000_AddCantidadConceptoPedidoLinea.php b/ci4/app/Database/Migrations/2025-04-12-130000_AddCantidadConceptoPedidoLinea.php new file mode 100644 index 00000000..471151fd --- /dev/null +++ b/ci4/app/Database/Migrations/2025-04-12-130000_AddCantidadConceptoPedidoLinea.php @@ -0,0 +1,33 @@ + [ + "type" => "INT", + "unsigned" => true, + "constraint" => 10, + "null" => true, + ], + 'descripcion' => [ + 'type' => 'TEXT', + "null" => true, + ], + ]; + + public function up() + { + $this->forge->addColumn("pedidos_linea", $this->USER_COLUMNS); + } + + public function down() + { + $this->forge->dropColumn("pedidos_linea", array_keys($this->USER_COLUMNS)); + + } +} diff --git a/ci4/app/Entities/Pedidos/PedidoLineaEntity.php b/ci4/app/Entities/Pedidos/PedidoLineaEntity.php index dc059079..f103a8ca 100644 --- a/ci4/app/Entities/Pedidos/PedidoLineaEntity.php +++ b/ci4/app/Entities/Pedidos/PedidoLineaEntity.php @@ -16,6 +16,8 @@ class PedidoLineaEntity extends \CodeIgniter\Entity\Entity "user_updated_id" => null, "created_at" => null, "updated_at" => null, + "cantidad" => null, + "descripcion" => null, ]; @@ -23,6 +25,7 @@ class PedidoLineaEntity extends \CodeIgniter\Entity\Entity "pedido_id" => "int", "presupuesto_id" => "int", "ubicacion_id" => "int", + "cantidad" => "int", ]; public function ubicacion() : UbicacionesEntity { diff --git a/ci4/app/Models/Facturas/FacturaLineaModel.php b/ci4/app/Models/Facturas/FacturaLineaModel.php index 82888d06..cb13de58 100644 --- a/ci4/app/Models/Facturas/FacturaLineaModel.php +++ b/ci4/app/Models/Facturas/FacturaLineaModel.php @@ -39,7 +39,7 @@ class FacturaLineaModel extends \App\Models\BaseModel { t1.pedido_linea_impresion_id AS pedido_linea_impresion_id, t1.pedido_maquetacion_id AS pedido_maquetacion_id, t1.descripcion AS descripcion, t1.cantidad as cantidad, t1.iva AS iva, t1.base AS base, t1.total_iva AS total_iva, t1.total AS total, t1.data AS data, t2.pedido_id AS pedido_id, - t3.total_aceptado AS total_aceptado, t4.tirada_flexible AS tirada_flexible, t4.descuento_tirada_flexible AS descuento_tirada_flexible, + t3.total_aceptado_revisado AS total_aceptado, t4.tirada_flexible AS tirada_flexible, t4.descuento_tirada_flexible AS descuento_tirada_flexible, t6.cantidad AS cantidad_albaran" ) ->join("pedidos_linea t2", "t2.id = t1.pedido_linea_impresion_id", "left") diff --git a/ci4/app/Models/Facturas/FacturaModel.php b/ci4/app/Models/Facturas/FacturaModel.php index dc11b5e2..7c2c8142 100644 --- a/ci4/app/Models/Facturas/FacturaModel.php +++ b/ci4/app/Models/Facturas/FacturaModel.php @@ -145,6 +145,27 @@ class FacturaModel extends \App\Models\BaseModel return $builder; } + + public function presupuestoHasFacturaValidada($presupuesto_id = null) + { + if ($presupuesto_id == null) { + return false; + } + + $result = $this->db->table($this->table . " t1") + ->select("t1.id") + ->join("facturas_lineas t2", "t2.factura_id = t1.id", "left") + ->join("pedidos_linea t3", "t2.pedido_linea_impresion_id = t3.id", "left") + ->where("t3.presupuesto_id", $presupuesto_id) + ->where("t1.deleted_at IS NULL") + ->where("t2.deleted_at IS NULL") + ->where("t1.estado", "validada") + ->get() + ->getResultObject(); + + return !empty($result); + } + public function getSumatoriosFacturacionCliente($cliente_id = -1){ if($cliente_id == -1){ diff --git a/ci4/app/Models/Pedidos/PedidoLineaModel.php b/ci4/app/Models/Pedidos/PedidoLineaModel.php index ca0e9b56..1e19c4cf 100644 --- a/ci4/app/Models/Pedidos/PedidoLineaModel.php +++ b/ci4/app/Models/Pedidos/PedidoLineaModel.php @@ -36,6 +36,8 @@ class PedidoLineaModel extends \App\Models\BaseModel "user_updated_id", "created_at", "updated_at", + "cantidad", + "descripcion", ]; protected $returnType = "App\Entities\Pedidos\PedidoLineaEntity"; diff --git a/ci4/app/Models/Pedidos/PedidoModel.php b/ci4/app/Models/Pedidos/PedidoModel.php index 7a7bdbf0..a5379fba 100644 --- a/ci4/app/Models/Pedidos/PedidoModel.php +++ b/ci4/app/Models/Pedidos/PedidoModel.php @@ -117,15 +117,28 @@ class PedidoModel extends \App\Models\BaseModel $builder = $this->db ->table($this->table . " t1") ->select( - "t2.presupuesto_id" + "t2.presupuesto_id, t3.total_aceptado, t2.descripcion, t2.cantidad" ); $builder->where("t1.id", $pedido_id); $builder->join("pedidos_linea t2", "t2.pedido_id = t1.id", "left"); + $builder->join("presupuestos t3", "t2.presupuesto_id = t3.id", "left"); $model_presupuesto = model("App\Models\Presupuestos\PresupuestoModel"); $lineasPresupuesto = []; foreach ($builder->get()->getResultObject() as $row) { - array_push($lineasPresupuesto, $model_presupuesto->generarLineaPedido($row->presupuesto_id)[0]); + if($row->descripcion == null){ + array_push($lineasPresupuesto, $model_presupuesto->generarLineaPedido($row->presupuesto_id)[0]); + } + else{ + $presupuesto = (object) [ + 'numero' => $row->presupuesto_id, + 'unidades' => $row->cantidad, + 'total' => $row->total_aceptado, + 'concepto' => $row->descripcion, + ]; + array_push($lineasPresupuesto, $presupuesto); + } + } $builder->groupBy("t1.id"); diff --git a/ci4/app/Services/PresupuestoService.php b/ci4/app/Services/PresupuestoService.php index 894259a1..74c73088 100755 --- a/ci4/app/Services/PresupuestoService.php +++ b/ci4/app/Services/PresupuestoService.php @@ -1885,9 +1885,12 @@ class PresupuestoService extends BaseService $pedido_id = $model_pedido->insert($data_pedido); if ($pedido_id) { + $lineas_pedido = $model_presupuesto->generarLineaPedido($presupuesto_id)[0]; $data_pedido_linea = [ "pedido_id" => $pedido_id, "presupuesto_id" => $presupuesto_id, + 'cantidad' => $lineas_pedido->unidades, + 'descripcion' => $lineas_pedido->concepto, "ubicacion_id" => 1, // safetak por defecto "user_created_id" => auth()->user()->id, "user_updated_id" => auth()->user()->id, diff --git a/ci4/app/Views/themes/vuexy/form/facturas/_addPedidosItems.php b/ci4/app/Views/themes/vuexy/form/facturas/_addPedidosItems.php index c2db9dba..a6f390aa 100644 --- a/ci4/app/Views/themes/vuexy/form/facturas/_addPedidosItems.php +++ b/ci4/app/Views/themes/vuexy/form/facturas/_addPedidosItems.php @@ -121,6 +121,7 @@ $('#addNewPedidoImpresion').on('click', function(){ $('#pedidoImpresion').val(null).trigger('change'); // Se actualiza la tabla de lineas de factura $('#tableOfLineasFactura').DataTable().clearPipeline().draw(); + $('#tableOfLineasPagos').DataTable().clearPipeline().draw(); } }); }); diff --git a/ci4/app/Views/themes/vuexy/form/facturas/_facturaLineasItems.php b/ci4/app/Views/themes/vuexy/form/facturas/_facturaLineasItems.php index 5f3c7331..9cd3136e 100644 --- a/ci4/app/Views/themes/vuexy/form/facturas/_facturaLineasItems.php +++ b/ci4/app/Views/themes/vuexy/form/facturas/_facturaLineasItems.php @@ -455,14 +455,9 @@ var tableLineas = $('#tableOfLineasFactura').DataTable({ autoNumericIVA.set(totalIVA); autoNumericTotal.set(totalTotal); - estado == 'borrador') :?> - var pendientePago = totalTotal; - var total_pagos = 0; - - var total_pagos = autoNumericTotalCobrado.getNumber(); - var pendientePago = totalTotal - total_pagos; - - + var total_pagos = autoNumericTotalCobrado.getNumber(); + var pendientePago = totalTotal - total_pagos; + if (isNaN(pendientePago)) pendientePago = 0; autoNumericPendientePago.set(pendientePago); @@ -522,6 +517,8 @@ function deleteConfirmedLinea(params){ : v } }).done((data, textStatus, jqXHR) => { + tablelineas.clearPipeline().footerCallback().draw(); + $('#tableOfLineasPagos').DataTable().clearPipeline().footerCallback().draw(); }).fail((jqXHR, textStatus, errorThrown) => { popErrorAlert(jqXHR.responseJSON.messages.error) diff --git a/ci4/app/Views/themes/vuexy/form/facturas/_pagosFacturasItems.php b/ci4/app/Views/themes/vuexy/form/facturas/_pagosFacturasItems.php index 78cbed7d..943f8e8d 100644 --- a/ci4/app/Views/themes/vuexy/form/facturas/_pagosFacturasItems.php +++ b/ci4/app/Views/themes/vuexy/form/facturas/_pagosFacturasItems.php @@ -185,6 +185,7 @@ editor_pagos.on( 'postSubmit', function ( e, json, data, action ) { yeniden(json.); tablePagos.clearPipeline(); tablePagos.draw(); + updateFooterLineas(tableLineas); }); const autoNumericTotalCobrado = new AutoNumeric('#totalCobrado-sum', 0, { @@ -340,13 +341,8 @@ var tablePagos = $('#tableOfLineasPagos').DataTable({ function updateFooterLineas(table){ // Assuming pendiente de pago is totalTotal - totalIVA for this example - estado == 'borrador') :?> - var pendientePago = autoNumericTotal.getNumber(); - var total_pagos = 0; - - var total_pagos = autoNumericTotalCobrado.getNumber(); - var pendientePago = autoNumericTotal.getNumber() - total_pagos; - + var total_pagos = autoNumericTotalCobrado.getNumber(); + var pendientePago = autoNumericTotal.getNumber() - total_pagos; // Se comprueba si pendientePago es un numero o NAN if(isNaN(pendientePago)){ pendientePago = 0; @@ -363,10 +359,10 @@ function updateFooterLineas(table){ pendiente: pendientePago } }).done((data, textStatus, jqXHR) => { - if($('#pendiente-pago').html() == '0.00'){ + if(autoNumericPendientePago.getNumber() == 0){ $('#addPagoRow').hide(); } else { - $('#validarFactura').show(); + $('#addPagoRow').show(); } if(data.estado_pago == 'pagada'){ $('#estado_pago_text').text(''); diff --git a/ci4/app/Views/themes/vuexy/form/facturas/viewFacturaForm.php b/ci4/app/Views/themes/vuexy/form/facturas/viewFacturaForm.php index 0736152a..23911b81 100644 --- a/ci4/app/Views/themes/vuexy/form/facturas/viewFacturaForm.php +++ b/ci4/app/Views/themes/vuexy/form/facturas/viewFacturaForm.php @@ -22,7 +22,7 @@ - estado !='borrador' && (strpos($facturaEntity->numero, "REC ") !== 0) ) : ?> + numero, "REC ") !== 0) ) : ?> estado !='borrador' && (strpos($facturaEntity->numero, "REC ") === 0) ) : ?> @@ -38,6 +38,9 @@ name="validarFactura" value="" /> + + + showDeleteButton == true && $facturaEntity->estado !='validada') : ?> - + @@ -210,6 +210,15 @@ if(inaplazable?1:0 ?>){ $('.inaplazable-date').addClass('text-danger fw-bold'); } +const autonumericTotalPrecio = new AutoNumeric('#total_precio', { + decimalPlaces: 2, + digitGroupSeparator: '.', + decimalCharacter: ',', + allowDecimalPadding: true, + unformatOnSubmit: true, +}); + + $("#fecha_entrega_real").flatpickr({ defaultDate: fecha_entrega_real_text ? "'".$pedidoEntity->fecha_entrega_real_text."'" : 'null' ?>,