Skip to content

Commit cef8511

Browse files
authored
Refonte - Trésorerie > Journal > Modifier colonne (#2121)
Co-authored-by: vgreb <vgreb@users.noreply.github.com>
1 parent 2a41e40 commit cef8511

File tree

5 files changed

+94
-98
lines changed

5 files changed

+94
-98
lines changed

app/config/routing/admin_accounting/journal.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,12 @@ admin_accounting_journal_export:
3333
path: /export
3434
defaults:
3535
_controller: AppBundle\Controller\Admin\Accounting\Journal\ExportAction
36+
37+
38+
admin_accounting_journal_update_info:
39+
path: /update-info/{id}/{modification}
40+
defaults:
41+
_controller: AppBundle\Controller\Admin\Accounting\Journal\UpdateInfoAction
42+
methods: [POST]
43+
requirements:
44+
id: \d+

htdocs/pages/administration/compta_journal.php

Lines changed: 0 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -252,99 +252,6 @@
252252
$smarty->assign('formulaire', genererFormulaire($formulaire));
253253
}
254254

255-
/*
256-
* This action is used in AJAX in order to update "compta" data.
257-
* Only 4 columns are available for update:
258-
* - categorie
259-
* - reglement
260-
* - evenement
261-
* - comment
262-
* - attachment_required
263-
* The new value is passed with the `val` variable (POST).
264-
* The column and the "compta" identifier are passed with GET vars.
265-
*
266-
* There is no content return on failure, only headers.
267-
* If the update succeed we display a simple JSON element with a 200 status code.
268-
*
269-
* This action is added to perform Ajax updates directly on the "journal" list
270-
* in order to improve utilization.
271-
*/ elseif ($action === 'modifier_colonne') {
272-
try {
273-
// Bad request?
274-
if (!isset($_POST['val']) || !isset($_GET['column']) || !isset($_GET['id']) || !($line = $compta->obtenir((int) $_GET['id']))) {
275-
throw new Exception("Please verify parameters", 400);
276-
}
277-
278-
// Test line existence
279-
if (!$line['id']) {
280-
throw new Exception("Not found", 404);
281-
}
282-
283-
$allowEmpty = false;
284-
285-
switch ($_GET['column']) {
286-
case 'categorie':
287-
$column = 'idcategorie';
288-
$value = (int) $_POST['val'];
289-
break;
290-
case 'reglement':
291-
$column = 'idmode_regl';
292-
$value = (int) $_POST['val'];
293-
break;
294-
case 'evenement':
295-
$column = 'idevenement';
296-
$value = (int) $_POST['val'];
297-
break;
298-
case 'comment':
299-
$column = 'comment';
300-
$value = (string) $_POST['val'];
301-
$allowEmpty = true;
302-
break;
303-
case 'attachment_required':
304-
$column = 'attachment_required';
305-
$value = (int) $_POST['val'];
306-
$allowEmpty = true;
307-
break;
308-
default:
309-
throw new Exception("Bad column name", 400);
310-
}
311-
312-
// No value?
313-
if (!$allowEmpty && !$value) {
314-
throw new Exception("Bad value", 400);
315-
}
316-
317-
if ($compta->modifierColonne($line['id'], $column, $value)) {
318-
$response = [
319-
'success' => true,
320-
];
321-
322-
// Done!
323-
header('Content-Type: application/json; charset=utf-8');
324-
header('HTTP/1.1 200 OK');
325-
die(json_encode($response));
326-
} else {
327-
throw new Exception("An error occurred", 409);
328-
}
329-
} catch (Exception $e) {
330-
switch ($e->getCode()) {
331-
case 404:
332-
$httpStatus = "Not Found";
333-
break;
334-
case 409:
335-
$httpStatus = "Conflict";
336-
break;
337-
case 400:
338-
default:
339-
$httpStatus = "Bad Request";
340-
break;
341-
}
342-
header('HTTP/1.1 ' . $e->getCode() . ' ' . $httpStatus);
343-
header('X-Info: ' . $e->getMessage());
344-
exit;
345-
}
346-
}
347-
348255
/**
349256
* Upload an attachment and save it on the specific line.
350257
* We save the uploads in a directory at the same month of the line

htdocs/templates/administration/compta_journal.html

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ <h2>Journal</h2>
9696
<td>
9797
{if $ecriture.evenement=='A déterminer'}
9898
<div class="ui form">
99-
<select class=" js-select-change" data-post-url="index.php?page=compta_journal&amp;action=modifier_colonne&amp;column=evenement&amp;id={$ecriture.idtmp}" style="min-width:130px">
99+
<select class=" js-select-change" data-post-url="/admin/accounting/journal/update-info/{$ecriture.idtmp}/event" style="min-width:130px">
100100
{foreach from=$events item=event_name key=event_id}
101101
<option value="{$event_id}" {if $event_name == "A déterminer"}selected="selected"{/if}>{$event_name}</option>
102102
{/foreach}
@@ -109,7 +109,7 @@ <h2>Journal</h2>
109109
<td>
110110
{if $ecriture.categorie=='A déterminer'}
111111
<div class="ui form">
112-
<select class="js-select-change" data-post-url="index.php?page=compta_journal&amp;action=modifier_colonne&amp;column=categorie&amp;id={$ecriture.idtmp}" style="min-width:130px">
112+
<select class="js-select-change" data-post-url="/admin/accounting/journal/update-info/{$ecriture.idtmp}/category" style="min-width:130px">
113113
{foreach from=$categories item=cat_name key=cat_id}
114114
<option value="{$cat_id}" {if $cat_name == "A déterminer"}selected="selected"{/if}>{$cat_name}</option>
115115
{/foreach}
@@ -133,7 +133,7 @@ <h2>Journal</h2>
133133
<td>
134134
{if $ecriture.reglement=='A déterminer'}
135135
<div class="ui form">
136-
<select class="js-select-change" data-post-url="index.php?page=compta_journal&amp;action=modifier_colonne&amp;column=reglement&amp;id={$ecriture.idtmp}" style="min-width:130px">
136+
<select class="js-select-change" data-post-url="/admin/accounting/journal/update-info/{$ecriture.idtmp}/paymentType" style="min-width:130px">
137137
{foreach from=$payment_methods item=method_name key=method_id}
138138
<option value="{$method_id}" {if $method_name == "A déterminer"}selected="selected"{/if}>{$method_name}</option>
139139
{/foreach}
@@ -146,7 +146,7 @@ <h2>Journal</h2>
146146
<td align="center">
147147
<input
148148
type="checkbox" class="js-attachment-change"
149-
data-post-url="index.php?page=compta_journal&amp;action=modifier_colonne&amp;column=attachment_required&amp;id={$ecriture.idtmp}"
149+
data-post-url="/admin/accounting/journal/update-info/{$ecriture.idtmp}/requiredAttachment"
150150
value="1"
151151
{if $ecriture.attachment_required}checked{/if}
152152
/>
@@ -176,7 +176,7 @@ <h2>Journal</h2>
176176
style="cursor:pointer;"
177177
data-position="left center"
178178
data-tooltip='Editer le commentaire{if $ecriture.comment} ("{$ecriture.comment}"){/if}'
179-
data-post-url="index.php?page=compta_journal&amp;action=modifier_colonne&amp;column=comment&amp;id={$ecriture.idtmp}"
179+
data-post-url="/admin/accounting/journal/update-info/{$ecriture.idtmp}/comment"
180180
data-comment="{$ecriture.comment}">
181181
<i class="comment {if $ecriture.comment}{else}outline{/if} icon"></i>
182182
</a>
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AppBundle\Accounting;
6+
7+
use AppBundle\Accounting\Model\Transaction;
8+
9+
enum TransactionModification: string
10+
{
11+
case Category = 'category';
12+
case PaymentType = 'paymentType';
13+
case Event = 'event';
14+
case Comment = 'comment';
15+
case RequiredAttachment = 'requiredAttachment';
16+
17+
public function setValue(Transaction $transaction, mixed $value): void
18+
{
19+
match ($this) {
20+
self::Category => $transaction->setCategoryId((int) $value),
21+
self::PaymentType => $transaction->setPaymentTypeId((int) $value),
22+
self::Event => $transaction->setEventId((int) $value),
23+
self::Comment => $transaction->setComment($value),
24+
self::RequiredAttachment => $transaction->setAttachmentRequired((bool) $value),
25+
};
26+
}
27+
28+
public function allowsEmpty(): bool
29+
{
30+
return match ($this) {
31+
self::Comment, self::RequiredAttachment => true,
32+
default => false,
33+
};
34+
}
35+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AppBundle\Controller\Admin\Accounting\Journal;
6+
7+
use AppBundle\Accounting\Model\Repository\TransactionRepository;
8+
use AppBundle\Accounting\Model\Transaction;
9+
use AppBundle\Accounting\TransactionModification;
10+
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
11+
use Symfony\Component\HttpFoundation\JsonResponse;
12+
use Symfony\Component\HttpFoundation\Request;
13+
use Symfony\Component\HttpFoundation\Response;
14+
15+
class UpdateInfoAction extends AbstractController
16+
{
17+
public function __construct(
18+
private readonly TransactionRepository $transactionRepository,
19+
) {}
20+
21+
public function __invoke(
22+
Request $request,
23+
int $id,
24+
TransactionModification $modification,
25+
): JsonResponse {
26+
$transaction = $this->transactionRepository->get($id);
27+
if (!$transaction instanceof Transaction) {
28+
throw $this->createNotFoundException();
29+
}
30+
31+
try {
32+
$value = $request->getPayload()->get('val');
33+
if (!$modification->allowsEmpty() && ($value === null || $value === '')) {
34+
throw new \InvalidArgumentException(sprintf('Value cannot be empty for "%s"', $modification->value));
35+
}
36+
37+
$modification->setValue($transaction, $value);
38+
$this->transactionRepository->save($transaction);
39+
} catch (\InvalidArgumentException $e) {
40+
return new JsonResponse(['error' => $e->getMessage()], Response::HTTP_BAD_REQUEST);
41+
}
42+
43+
return new JsonResponse(['success' => true]);
44+
}
45+
}

0 commit comments

Comments
 (0)