Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions app/config/routing/admin_accounting/journal.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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+
93 changes: 0 additions & 93 deletions htdocs/pages/administration/compta_journal.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions htdocs/templates/administration/compta_journal.html
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ <h2>Journal</h2>
<td>
{if $ecriture.evenement=='A déterminer'}
<div class="ui form">
<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">
<select class=" js-select-change" data-post-url="/admin/accounting/journal/update-info/{$ecriture.idtmp}/event" style="min-width:130px">
{foreach from=$events item=event_name key=event_id}
<option value="{$event_id}" {if $event_name == "A déterminer"}selected="selected"{/if}>{$event_name}</option>
{/foreach}
Expand All @@ -109,7 +109,7 @@ <h2>Journal</h2>
<td>
{if $ecriture.categorie=='A déterminer'}
<div class="ui form">
<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">
<select class="js-select-change" data-post-url="/admin/accounting/journal/update-info/{$ecriture.idtmp}/category" style="min-width:130px">
{foreach from=$categories item=cat_name key=cat_id}
<option value="{$cat_id}" {if $cat_name == "A déterminer"}selected="selected"{/if}>{$cat_name}</option>
{/foreach}
Expand All @@ -133,7 +133,7 @@ <h2>Journal</h2>
<td>
{if $ecriture.reglement=='A déterminer'}
<div class="ui form">
<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">
<select class="js-select-change" data-post-url="/admin/accounting/journal/update-info/{$ecriture.idtmp}/paymentType" style="min-width:130px">
{foreach from=$payment_methods item=method_name key=method_id}
<option value="{$method_id}" {if $method_name == "A déterminer"}selected="selected"{/if}>{$method_name}</option>
{/foreach}
Expand Down
42 changes: 42 additions & 0 deletions sources/AppBundle/Accounting/TransactionModification.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

declare(strict_types=1);

namespace AppBundle\Accounting;

enum TransactionModification: string
{
case Category = 'category';
case PaymentType = 'paymentType';
case Event = 'event';
case Comment = 'comment';
case RequiredAttachment = 'RequiredAttachment';

public function getPropertyName(): string
{
return match ($this) {
self::Category => '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,
};
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace AppBundle\Controller\Admin\Accounting\Journal;

use AppBundle\Accounting\Model\Repository\TransactionRepository;
use AppBundle\Accounting\Model\Transaction;
use AppBundle\Accounting\TransactionModification;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class UpdateInfoAction extends AbstractController
{
public function __construct(
private readonly TransactionRepository $transactionRepository,
) {}

public function __invoke(
Request $request,
int $id,
TransactionModification $modification,
): JsonResponse {
$transaction = $this->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]);
}
}
Loading