Skip to content

Commit 3631abc

Browse files
committed
Refonte - Trésorerie > Journal > Modifier colonne
1 parent a533b75 commit 3631abc

File tree

4 files changed

+100
-3
lines changed

4 files changed

+100
-3
lines changed

app/config/routing/admin_accounting/journal.yml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,11 @@ 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+
requirements:
43+
id: \d+

htdocs/templates/administration/compta_journal.html

Lines changed: 3 additions & 3 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}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace AppBundle\Accounting;
6+
7+
enum TransactionModification: string
8+
{
9+
case Category = 'category';
10+
case PaymentType = 'paymentType';
11+
case Event = 'event';
12+
case Comment = 'comment';
13+
case RequiredAttachment = 'RequiredAttachment';
14+
15+
public function getPropertyName(): string
16+
{
17+
return match ($this) {
18+
self::Category => 'categoryId',
19+
self::PaymentType => 'paymentTypeId',
20+
self::Event => 'eventId',
21+
self::Comment => 'comment',
22+
self::RequiredAttachment => 'attachmentRequired',
23+
};
24+
}
25+
26+
public function convertValue(mixed $value): int|string|bool
27+
{
28+
return match ($this) {
29+
self::Category, self::PaymentType, self::Event => (int) $value,
30+
self::Comment => (string) $value,
31+
self::RequiredAttachment => (bool) $value,
32+
};
33+
}
34+
35+
public function allowsEmpty(): bool
36+
{
37+
return match ($this) {
38+
self::Comment, self::RequiredAttachment => true,
39+
default => false,
40+
};
41+
}
42+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
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 = $modification->convertValue($request->getPayload()->get('val'));
33+
34+
if (!$modification->allowsEmpty() && empty($value)) {
35+
throw new \InvalidArgumentException(sprintf('Value cannot be empty for "%s"', $value));
36+
}
37+
38+
$setter = 'set' . ucfirst($modification->getPropertyName());
39+
$transaction->{$setter}($value);
40+
$this->transactionRepository->save($transaction);
41+
} catch (\InvalidArgumentException $e) {
42+
return new JsonResponse(['error' => $e->getMessage()], Response::HTTP_BAD_REQUEST);
43+
}
44+
45+
return new JsonResponse(['success' => true]);
46+
}
47+
}

0 commit comments

Comments
 (0)