Skip to content

Commit c00c20c

Browse files
author
Carlos Garcia
committed
Solucionados fallos al deshacer cambios cuando al cambiar de estado un documento no se puede completar: ahora se realiza dentro de una transacción.
1 parent 2aea1a1 commit c00c20c

2 files changed

Lines changed: 54 additions & 16 deletions

File tree

Core/Lib/AjaxForms/PurchasesController.php

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use FacturaScripts\Core\Lib\ExtendedController\PanelController;
2929
use FacturaScripts\Core\Model\Base\BusinessDocumentLine;
3030
use FacturaScripts\Core\Model\Base\PurchaseDocument;
31+
use FacturaScripts\Core\Model\LogMessage;
3132
use FacturaScripts\Core\Tools;
3233
use FacturaScripts\Dinamic\Lib\AssetManager;
3334
use FacturaScripts\Dinamic\Model\Proveedor;
@@ -362,16 +363,21 @@ protected function saveDocAction(bool $sendOk = true): bool
362363
return false;
363364
}
364365

365-
$this->dataBase->beginTransaction();
366+
$inTransaction = $this->db()->inTransaction();
367+
if (!$inTransaction) {
368+
$this->db()->beginTransaction();
369+
}
366370

367371
$model = $this->getModel();
368372
$formData = json_decode($this->request->input('data'), true);
369373
PurchasesHeaderHTML::apply($model, $formData);
370374
PurchasesFooterHTML::apply($model, $formData);
371375

372376
if (false === $model->save()) {
377+
if (!$inTransaction) {
378+
$this->db()->rollback();
379+
}
373380
$this->sendJsonWithLogs(['ok' => false]);
374-
$this->dataBase->rollback();
375381
return false;
376382
}
377383

@@ -381,33 +387,41 @@ protected function saveDocAction(bool $sendOk = true): bool
381387

382388
foreach ($lines as $line) {
383389
if (false === $line->save()) {
390+
if (!$inTransaction) {
391+
$this->db()->rollback();
392+
}
384393
$this->sendJsonWithLogs(['ok' => false]);
385-
$this->dataBase->rollback();
386394
return false;
387395
}
388396
}
389397

390398
// remove missing lines
391399
foreach ($model->getLines() as $oldLine) {
392400
if (in_array($oldLine->idlinea, PurchasesLineHTML::getDeletedLines()) && false === $oldLine->delete()) {
401+
if (!$inTransaction) {
402+
$this->db()->rollback();
403+
}
393404
$this->sendJsonWithLogs(['ok' => false]);
394-
$this->dataBase->rollback();
395405
return false;
396406
}
397407
}
398408

399409
$lines = $model->getLines();
400410
if (false === Calculator::calculate($model, $lines, true)) {
411+
if (!$inTransaction) {
412+
$this->db()->rollback();
413+
}
401414
$this->sendJsonWithLogs(['ok' => false]);
402-
$this->dataBase->rollback();
403415
return false;
404416
}
405417

418+
if (!$inTransaction) {
419+
$this->db()->commit();
420+
}
421+
406422
if ($sendOk) {
407423
$this->sendJsonWithLogs(['ok' => true, 'newurl' => $model->url() . '&action=save-ok']);
408424
}
409-
410-
$this->dataBase->commit();
411425
return true;
412426
}
413427

@@ -479,17 +493,22 @@ protected function saveStatusAction(): bool
479493
return false;
480494
}
481495

496+
$this->db()->beginTransaction();
497+
482498
if ($this->getModel()->editable && false === $this->saveDocAction(false)) {
499+
$this->db()->rollback();
483500
return false;
484501
}
485502

486503
$model = $this->getModel();
487504
$model->idestado = (int)$this->request->input('selectedLine');
488505
if (false === $model->save()) {
506+
$this->db()->rollback();
489507
$this->sendJsonWithLogs(['ok' => false]);
490508
return false;
491509
}
492510

511+
$this->db()->commit();
493512
$this->sendJsonWithLogs(['ok' => true, 'newurl' => $model->url() . '&action=save-ok']);
494513
return false;
495514
}
@@ -498,7 +517,7 @@ private function sendJsonWithLogs(array $data): void
498517
{
499518
$data['messages'] = [];
500519
foreach (Tools::log()::read('', $this->logLevels) as $message) {
501-
if ($message['channel'] != 'audit') {
520+
if (!in_array($message['channel'], [LogMessage::AUDIT_CHANNEL, LogMessage::DOCS_CHANNEL])) {
502521
$data['messages'][] = $message;
503522
}
504523
}

Core/Lib/AjaxForms/SalesController.php

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
use FacturaScripts\Core\Lib\ExtendedController\PanelController;
2929
use FacturaScripts\Core\Model\Base\SalesDocument;
3030
use FacturaScripts\Core\Model\Base\SalesDocumentLine;
31+
use FacturaScripts\Core\Model\LogMessage;
3132
use FacturaScripts\Core\Tools;
3233
use FacturaScripts\Dinamic\Lib\AssetManager;
3334
use FacturaScripts\Dinamic\Model\Cliente;
@@ -377,16 +378,21 @@ protected function saveDocAction(bool $sendOk = true): bool
377378
return false;
378379
}
379380

380-
$this->dataBase->beginTransaction();
381+
$inTransaction = $this->db()->inTransaction();
382+
if (!$inTransaction) {
383+
$this->db()->beginTransaction();
384+
}
381385

382386
$model = $this->getModel();
383387
$formData = json_decode($this->request->input('data'), true);
384388
SalesHeaderHTML::apply($model, $formData);
385389
SalesFooterHTML::apply($model, $formData);
386390

387391
if (false === $model->save()) {
392+
if (!$inTransaction) {
393+
$this->db()->rollback();
394+
}
388395
$this->sendJsonWithLogs(['ok' => false]);
389-
$this->dataBase->rollback();
390396
return false;
391397
}
392398

@@ -396,33 +402,41 @@ protected function saveDocAction(bool $sendOk = true): bool
396402

397403
foreach ($lines as $line) {
398404
if (false === $line->save()) {
405+
if (!$inTransaction) {
406+
$this->db()->rollback();
407+
}
399408
$this->sendJsonWithLogs(['ok' => false]);
400-
$this->dataBase->rollback();
401409
return false;
402410
}
403411
}
404412

405413
// remove missing lines
406414
foreach ($model->getLines() as $oldLine) {
407415
if (in_array($oldLine->idlinea, SalesLineHTML::getDeletedLines()) && false === $oldLine->delete()) {
416+
if (!$inTransaction) {
417+
$this->db()->rollback();
418+
}
408419
$this->sendJsonWithLogs(['ok' => false]);
409-
$this->dataBase->rollback();
410420
return false;
411421
}
412422
}
413423

414424
$lines = $model->getLines();
415425
if (false === Calculator::calculate($model, $lines, true)) {
426+
if (!$inTransaction) {
427+
$this->db()->rollback();
428+
}
416429
$this->sendJsonWithLogs(['ok' => false]);
417-
$this->dataBase->rollback();
418430
return false;
419431
}
420432

433+
if (!$inTransaction) {
434+
$this->db()->commit();
435+
}
436+
421437
if ($sendOk) {
422438
$this->sendJsonWithLogs(['ok' => true, 'newurl' => $model->url() . '&action=save-ok']);
423439
}
424-
425-
$this->dataBase->commit();
426440
return true;
427441
}
428442

@@ -493,17 +507,22 @@ protected function saveStatusAction(): bool
493507
return false;
494508
}
495509

510+
$this->db()->beginTransaction();
511+
496512
if ($this->getModel()->editable && false === $this->saveDocAction(false)) {
513+
$this->db()->rollback();
497514
return false;
498515
}
499516

500517
$model = $this->getModel();
501518
$model->idestado = (int)$this->request->input('selectedLine');
502519
if (false === $model->save()) {
520+
$this->db()->rollback();
503521
$this->sendJsonWithLogs(['ok' => false]);
504522
return false;
505523
}
506524

525+
$this->db()->commit();
507526
$this->sendJsonWithLogs(['ok' => true, 'newurl' => $model->url() . '&action=save-ok']);
508527
return false;
509528
}
@@ -512,7 +531,7 @@ private function sendJsonWithLogs(array $data): void
512531
{
513532
$data['messages'] = [];
514533
foreach (Tools::log()::read('', $this->logLevels) as $message) {
515-
if ($message['channel'] != 'audit') {
534+
if (!in_array($message['channel'], [LogMessage::AUDIT_CHANNEL, LogMessage::DOCS_CHANNEL])) {
516535
$data['messages'][] = $message;
517536
}
518537
}

0 commit comments

Comments
 (0)