diff --git a/app/config/routing/admin_accounting/journal.yml b/app/config/routing/admin_accounting/journal.yml index 5ef2494ca..0de103c76 100644 --- a/app/config/routing/admin_accounting/journal.yml +++ b/app/config/routing/admin_accounting/journal.yml @@ -33,3 +33,11 @@ admin_accounting_journal_export: path: /export defaults: _controller: AppBundle\Controller\Admin\Accounting\Journal\ExportAction + + +admin_accounting_journal_update_info: + path: /update-info/{id}/{modification} + defaults: + _controller: AppBundle\Controller\Admin\Accounting\Journal\UpdateInfoAction + requirements: + id: \d+ diff --git a/htdocs/pages/administration/compta_journal.php b/htdocs/pages/administration/compta_journal.php index aca9b103b..15434a10c 100755 --- a/htdocs/pages/administration/compta_journal.php +++ b/htdocs/pages/administration/compta_journal.php @@ -252,99 +252,6 @@ $smarty->assign('formulaire', genererFormulaire($formulaire)); } -/* - * This action is used in AJAX in order to update "compta" data. - * Only 4 columns are available for update: - * - categorie - * - reglement - * - evenement - * - comment - * - attachment_required - * The new value is passed with the `val` variable (POST). - * The column and the "compta" identifier are passed with GET vars. - * - * There is no content return on failure, only headers. - * If the update succeed we display a simple JSON element with a 200 status code. - * - * This action is added to perform Ajax updates directly on the "journal" list - * in order to improve utilization. - */ elseif ($action === 'modifier_colonne') { - try { - // Bad request? - if (!isset($_POST['val']) || !isset($_GET['column']) || !isset($_GET['id']) || !($line = $compta->obtenir((int) $_GET['id']))) { - throw new Exception("Please verify parameters", 400); - } - - // Test line existence - if (!$line['id']) { - throw new Exception("Not found", 404); - } - - $allowEmpty = false; - - switch ($_GET['column']) { - case 'categorie': - $column = 'idcategorie'; - $value = (int) $_POST['val']; - break; - case 'reglement': - $column = 'idmode_regl'; - $value = (int) $_POST['val']; - break; - case 'evenement': - $column = 'idevenement'; - $value = (int) $_POST['val']; - break; - case 'comment': - $column = 'comment'; - $value = (string) $_POST['val']; - $allowEmpty = true; - break; - case 'attachment_required': - $column = 'attachment_required'; - $value = (int) $_POST['val']; - $allowEmpty = true; - break; - default: - throw new Exception("Bad column name", 400); - } - - // No value? - if (!$allowEmpty && !$value) { - throw new Exception("Bad value", 400); - } - - if ($compta->modifierColonne($line['id'], $column, $value)) { - $response = [ - 'success' => true, - ]; - - // Done! - header('Content-Type: application/json; charset=utf-8'); - header('HTTP/1.1 200 OK'); - die(json_encode($response)); - } else { - throw new Exception("An error occurred", 409); - } - } catch (Exception $e) { - switch ($e->getCode()) { - case 404: - $httpStatus = "Not Found"; - break; - case 409: - $httpStatus = "Conflict"; - break; - case 400: - default: - $httpStatus = "Bad Request"; - break; - } - header('HTTP/1.1 ' . $e->getCode() . ' ' . $httpStatus); - header('X-Info: ' . $e->getMessage()); - exit; - } -} - /** * Upload an attachment and save it on the specific line. * We save the uploads in a directory at the same month of the line diff --git a/htdocs/templates/administration/compta_journal.html b/htdocs/templates/administration/compta_journal.html index 0f4856ed2..01edf0bce 100644 --- a/htdocs/templates/administration/compta_journal.html +++ b/htdocs/templates/administration/compta_journal.html @@ -96,7 +96,7 @@

Journal

{if $ecriture.evenement=='A déterminer'}
- {foreach from=$events item=event_name key=event_id} {/foreach} @@ -109,7 +109,7 @@

Journal

{if $ecriture.categorie=='A déterminer'}
- {foreach from=$categories item=cat_name key=cat_id} {/foreach} @@ -133,7 +133,7 @@

Journal

{if $ecriture.reglement=='A déterminer'}
- {foreach from=$payment_methods item=method_name key=method_id} {/foreach} diff --git a/sources/AppBundle/Accounting/TransactionModification.php b/sources/AppBundle/Accounting/TransactionModification.php new file mode 100644 index 000000000..2367c22d1 --- /dev/null +++ b/sources/AppBundle/Accounting/TransactionModification.php @@ -0,0 +1,42 @@ + 'categoryId', + self::PaymentType => 'paymentTypeId', + self::Event => 'eventId', + self::Comment => 'comment', + self::RequiredAttachment => 'attachmentRequired', + }; + } + + public function convertValue(mixed $value): int|string|bool + { + return match ($this) { + self::Category, self::PaymentType, self::Event => (int) $value, + self::Comment => (string) $value, + self::RequiredAttachment => (bool) $value, + }; + } + + public function allowsEmpty(): bool + { + return match ($this) { + self::Comment, self::RequiredAttachment => true, + default => false, + }; + } +} diff --git a/sources/AppBundle/Controller/Admin/Accounting/Journal/UpdateInfoAction.php b/sources/AppBundle/Controller/Admin/Accounting/Journal/UpdateInfoAction.php new file mode 100644 index 000000000..eb46c5591 --- /dev/null +++ b/sources/AppBundle/Controller/Admin/Accounting/Journal/UpdateInfoAction.php @@ -0,0 +1,47 @@ +transactionRepository->get($id); + if (!$transaction instanceof Transaction) { + throw $this->createNotFoundException(); + } + + try { + $value = $modification->convertValue($request->getPayload()->get('val')); + + if (!$modification->allowsEmpty() && empty($value)) { + throw new \InvalidArgumentException(sprintf('Value cannot be empty for "%s"', $value)); + } + + $setter = 'set' . ucfirst($modification->getPropertyName()); + $transaction->{$setter}($value); + $this->transactionRepository->save($transaction); + } catch (\InvalidArgumentException $e) { + return new JsonResponse(['error' => $e->getMessage()], Response::HTTP_BAD_REQUEST); + } + + return new JsonResponse(['success' => true]); + } +}