Merge branch 'mod/flujo_facturas' into 'main'

terminado el flujo completo de facturas, incluyendo cambio en el texto de...

See merge request jjimenez/safekat!693
This commit is contained in:
2025-04-12 15:48:21 +00:00
15 changed files with 138 additions and 23 deletions

View File

@ -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,

View File

@ -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 = [

View File

@ -0,0 +1,33 @@
<?php
namespace App\Database\Migrations;
use CodeIgniter\Database\Migration;
use CodeIgniter\Database\RawSql;
class AddCantidadConceptoPedidoLinea extends Migration
{
protected array $USER_COLUMNS = [
"cantidad" => [
"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));
}
}

View File

@ -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
{

View File

@ -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")

View File

@ -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){

View File

@ -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";

View File

@ -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");

View File

@ -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,

View File

@ -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();
}
});
});

View File

@ -455,14 +455,9 @@ var tableLineas = $('#tableOfLineasFactura').DataTable({
autoNumericIVA.set(totalIVA);
autoNumericTotal.set(totalTotal);
<?php if($facturaEntity->estado == 'borrador') :?>
var pendientePago = totalTotal;
var total_pagos = 0;
<?php else: ?>
var total_pagos = autoNumericTotalCobrado.getNumber();
var pendientePago = totalTotal - total_pagos;
<?php endif; ?>
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){
<?= csrf_token() ?? "token" ?> : <?= csrf_token() ?>v
}
}).done((data, textStatus, jqXHR) => {
tablelineas.clearPipeline().footerCallback().draw();
$('#tableOfLineasPagos').DataTable().clearPipeline().footerCallback().draw();
}).fail((jqXHR, textStatus, errorThrown) => {
popErrorAlert(jqXHR.responseJSON.messages.error)

View File

@ -185,6 +185,7 @@ editor_pagos.on( 'postSubmit', function ( e, json, data, action ) {
yeniden(json.<?= csrf_token() ?>);
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
<?php if($facturaEntity->estado == 'borrador') :?>
var pendientePago = autoNumericTotal.getNumber();
var total_pagos = 0;
<?php else: ?>
var total_pagos = autoNumericTotalCobrado.getNumber();
var pendientePago = autoNumericTotal.getNumber() - total_pagos;
<?php endif; ?>
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('<?= lang('Facturas.pagada') ?>');

View File

@ -22,7 +22,7 @@
<?php endif; ?>
<?= view("themes/vuexy/form/facturas/_facturaLineasItems") ?>
<?php if($facturaEntity->estado !='borrador' && (strpos($facturaEntity->numero, "REC ") !== 0) ) : ?>
<?php if((strpos($facturaEntity->numero, "REC ") !== 0) ) : ?>
<?= view("themes/vuexy/form/facturas/_pagosFacturasItems") ?>
<?php endif; ?>
<?php if($facturaEntity->estado !='borrador' && (strpos($facturaEntity->numero, "REC ") === 0) ) : ?>
@ -38,6 +38,9 @@
name="validarFactura"
value="<?= lang("Facturas.validarFactura") ?>"
/>
<?php endif; ?>
<?php if($facturaEntity->showDeleteButton == true && $facturaEntity->estado !='validada') : ?>
<input type="button"
class="btn btn-danger float-start me-sm-3 me-1"
id="borrarFactura"

View File

@ -86,7 +86,7 @@
<label for="total_precio" class="form-label">
<?= lang('Pedidos.total_precio') ?>
</label>
<input readonly id="total_precio" name="total_precio" tabindex="1" maxLength="11" class="form-control" value="<?= old('total_precio', $pedidoEntity->total_precio) ?>" >
<input readonly id="total_precio" name="total_precio" tabindex="1" maxLength="11" class="form-control autonumeric" value="<?= old('total_precio', $pedidoEntity->total_precio) ?>" >
</div>
</div><!--//.mb-3 -->
@ -210,6 +210,15 @@ if(<?= $pedidoEntity->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: <?= $pedidoEntity->fecha_entrega_real_text ? "'".$pedidoEntity->fecha_entrega_real_text."'" : 'null' ?>,