diff --git a/app/config/routing/admin_accounting/journal.yml b/app/config/routing/admin_accounting/journal.yml index 5ef2494ca..b10c03a51 100644 --- a/app/config/routing/admin_accounting/journal.yml +++ b/app/config/routing/admin_accounting/journal.yml @@ -33,3 +33,12 @@ 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 + methods: [POST] + 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..5071aac95 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} @@ -146,7 +146,7 @@

Journal

@@ -176,7 +176,7 @@

Journal

style="cursor:pointer;" data-position="left center" data-tooltip='Editer le commentaire{if $ecriture.comment} ("{$ecriture.comment}"){/if}' - data-post-url="index.php?page=compta_journal&action=modifier_colonne&column=comment&id={$ecriture.idtmp}" + data-post-url="/admin/accounting/journal/update-info/{$ecriture.idtmp}/comment" data-comment="{$ecriture.comment}"> diff --git a/sources/AppBundle/Accounting/TransactionModification.php b/sources/AppBundle/Accounting/TransactionModification.php new file mode 100644 index 000000000..f2fc252b9 --- /dev/null +++ b/sources/AppBundle/Accounting/TransactionModification.php @@ -0,0 +1,35 @@ + $transaction->setCategoryId((int) $value), + self::PaymentType => $transaction->setPaymentTypeId((int) $value), + self::Event => $transaction->setEventId((int) $value), + self::Comment => $transaction->setComment($value), + self::RequiredAttachment => $transaction->setAttachmentRequired((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..f236d5e9a --- /dev/null +++ b/sources/AppBundle/Controller/Admin/Accounting/Journal/UpdateInfoAction.php @@ -0,0 +1,45 @@ +transactionRepository->get($id); + if (!$transaction instanceof Transaction) { + throw $this->createNotFoundException(); + } + + try { + $value = $request->getPayload()->get('val'); + if (!$modification->allowsEmpty() && ($value === null || $value === '')) { + throw new \InvalidArgumentException(sprintf('Value cannot be empty for "%s"', $modification->value)); + } + + $modification->setValue($transaction, $value); + $this->transactionRepository->save($transaction); + } catch (\InvalidArgumentException $e) { + return new JsonResponse(['error' => $e->getMessage()], Response::HTTP_BAD_REQUEST); + } + + return new JsonResponse(['success' => true]); + } +}