Skip to content

Commit

Permalink
Removed the calculation query resolver. Updated exceptions.
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentmuller committed Jan 21, 2025
1 parent 0b89dd2 commit ae3e140
Show file tree
Hide file tree
Showing 40 changed files with 209 additions and 452 deletions.
74 changes: 38 additions & 36 deletions public/js/application/calculation_edit.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,11 +291,11 @@ const Application = {
});
const userMargin = $('#calculation_userMargin').floatVal() / 100.0;

return {
return JSON.stringify({
adjust: adjust,
userMargin: userMargin,
groups: groups,
};
});
},

/**
Expand Down Expand Up @@ -343,10 +343,6 @@ const Application = {
that.jqXHR = null;
}

// parameters
const url = $form.data('update');
const data = this.serializeForm(adjust);

/**
* @param {Object} response
* @param {boolean} response.result
Expand All @@ -356,37 +352,43 @@ const Application = {
* @param {number} response.user_margin
* @param {boolean} response.overall_below
*/
that.jqXHR = $.post(url, data, function (response) {
// error?
if (!response.result) {
return that.disable(response.message);
}
// update content
const $totalPanel = $('#totals-panel');
if (response.view) {
const $body = $('#totals-table > tbody');
$body.fadeOut(300, function () {
$body.html(response.view).fadeIn(300);
$totalPanel.fadeIn();
});
} else {
$totalPanel.fadeOut();
}
if (response.adjust && !$.isUndefined(response.user_margin) && !isNaN(response.user_margin)) {
$('#calculation_userMargin').intVal(response.user_margin * 100).selectFocus();
}
if (response.overall_below) {
$buttonAdjust.toggleDisabled(false).removeClass('cursor-default');
} else {
$buttonAdjust.toggleDisabled(true).addClass('cursor-default');
}
$('#calculation_customer').trigger('input');
$('#data-table-edit').updateErrors();
return that;
that.jqXHR = $.post({
url: $form.data('update'),
data: this.serializeForm(adjust),
contentType: 'application/json;',
success: function (response) {
// error?
if (!response.result) {
return that.disable(response.message);
}

}).fail(function (_jqXHR, textStatus) {
if (textStatus !== 'abort') {
return that.disable();
// update content
const $totalPanel = $('#totals-panel');
if (response.view) {
const $body = $('#totals-table > tbody');
$body.fadeOut(300, function () {
$body.html(response.view).fadeIn(300);
$totalPanel.fadeIn();
});
} else {
$totalPanel.fadeOut();
}
if (response.adjust && !$.isUndefined(response.user_margin) && !isNaN(response.user_margin)) {
$('#calculation_userMargin').intVal(response.user_margin * 100).selectFocus();
}
if (response.overall_below) {
$buttonAdjust.toggleDisabled(false).removeClass('cursor-default');
} else {
$buttonAdjust.toggleDisabled(true).addClass('cursor-default');
}
$('#calculation_customer').trigger('input');
$('#data-table-edit').updateErrors();
return that;
},
fail: function (_jqXHR, textStatus) {
if (textStatus !== 'abort') {
return that.disable();
}
}
});

Expand Down
6 changes: 3 additions & 3 deletions src/Chart/MonthChart.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use App\Service\ApplicationService;
use App\Traits\ArrayTrait;
use App\Utils\FormatUtils;
use Doctrine\ORM\Exception\ORMException;
use HighchartsBundle\Highcharts\ChartExpression;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Twig\Environment;
Expand Down Expand Up @@ -55,8 +56,7 @@ public function __construct(
/**
* Generate the chart data.
*
* @throws \Doctrine\ORM\Exception\ORMException
* @throws \Exception
* @throws \Exception|ORMException
*/
public function generate(int $months): array
{
Expand Down Expand Up @@ -123,7 +123,7 @@ private function formatDate(\DateTimeInterface $date): string
/**
* @return int[]
*
* @throws \Doctrine\ORM\Exception\ORMException
* @throws ORMException
*/
private function getAllowedMonths(): array
{
Expand Down
3 changes: 2 additions & 1 deletion src/Controller/AbstractEntityController.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use App\Utils\StringUtils;
use App\Word\WordDocument;
use Doctrine\Common\Collections\Criteria;
use Doctrine\ORM\Exception\ORMException;
use fpdf\PdfDocument;
use Psr\Log\LoggerInterface;
use Symfony\Component\Form\Extension\Core\Type\FormType;
Expand Down Expand Up @@ -212,7 +213,7 @@ protected function getEditTemplate(): string
*
* @psalm-return TEntity[]
*
* @throws \Doctrine\ORM\Exception\ORMException
* @throws ORMException
*/
protected function getEntities(
array|string $sortedFields = [],
Expand Down
29 changes: 11 additions & 18 deletions src/Controller/AjaxCalculationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
use App\Attribute\Post;
use App\Interfaces\RoleInterface;
use App\Model\CalculationQuery;
use App\Resolver\CalculationQueryResolver;
use App\Service\CalculationService;
use Doctrine\ORM\Exception\ORMException;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpKernel\Attribute\AsController;
Expand All @@ -26,41 +26,34 @@
use Symfony\Component\Security\Http\Attribute\IsGranted;

/**
* Controller to update calculation's total within XMLHttpRequest (Ajax) calls.
* Controller to update calculation's total or the user margin within XMLHttpRequest (Ajax) calls.
*/
#[AsController]
#[Route(path: '/calculation', name: 'calculation_')]
#[IsGranted(RoleInterface::ROLE_USER)]
class AjaxCalculationController extends AbstractController
{
/**
* Update the calculation's total.
*
* @throws \Doctrine\ORM\Exception\ORMException
* @throws ORMException
*/
#[Post(path: '/update', name: 'update')]
public function update(
CalculationService $service,
LoggerInterface $logger,
#[MapRequestPayload(resolver: CalculationQueryResolver::class)]
#[MapRequestPayload]
CalculationQuery $query = new CalculationQuery()
): JsonResponse {
try {
$parameters = $service->createParameters($query);
if (!$parameters['result']) {
return $this->json($parameters);
if ($parameters['result']) {
$view = $this->renderView('calculation/calculation_ajax_totals.html.twig', $parameters);
$parameters = \array_merge($parameters, [
'adjust' => $query->adjust,
'view' => $view,
]);
}

$view = $this->renderView('calculation/calculation_ajax_totals.html.twig', $parameters);

return $this->jsonTrue([
'view' => $view,
'adjust' => $query->adjust,
'user_margin' => $parameters['user_margin'],
'overall_margin' => $parameters['overall_margin'],
'overall_total' => $parameters['overall_total'],
'overall_below' => $parameters['overall_below'],
]);
return $this->json($parameters);
} catch (\Exception $e) {
$message = $this->trans('calculation.edit.error.update_total');
$context = $this->getExceptionContext($e);
Expand Down
6 changes: 4 additions & 2 deletions src/Controller/CalculationArchiveController.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
class CalculationArchiveController extends AbstractController
{
/**
* @throws ORMException|\DateException
* @throws \DateException
* @throws ORMException
*/
#[GetPost(path: '/archive', name: 'archive')]
public function invoke(Request $request, CalculationArchiveService $service): Response
Expand Down Expand Up @@ -74,7 +75,8 @@ public function invoke(Request $request, CalculationArchiveService $service): Re
/**
* @return FormInterface<mixed>
*
* @throws ORMException|\DateException
* @throws \DateException
* @throws ORMException
*/
private function createQueryForm(CalculationArchiveService $service, CalculationArchiveQuery $query): FormInterface
{
Expand Down
7 changes: 4 additions & 3 deletions src/Controller/CalculationBelowController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use App\Table\DataQuery;
use App\Traits\TableTrait;
use App\Utils\FormatUtils;
use Doctrine\ORM\Exception\ORMException;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
Expand All @@ -47,8 +48,8 @@ class CalculationBelowController extends AbstractController
* Export the calculations to a Spreadsheet document.
*
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException if the report cannot be rendered
* @throws \Doctrine\ORM\Exception\ORMException
* @throws \PhpOffice\PhpSpreadsheet\Exception
* @throws ORMException
*/
#[Get(path: '/excel', name: 'excel')]
public function excel(CalculationRepository $repository): Response
Expand Down Expand Up @@ -85,7 +86,7 @@ public function index(
/**
* Export calculations to a PDF document.
*
* @throws \Doctrine\ORM\Exception\ORMException
* @throws ORMException
*/
#[Get(path: '/pdf', name: 'pdf')]
public function pdf(CalculationRepository $repository): Response
Expand All @@ -109,7 +110,7 @@ public function pdf(CalculationRepository $repository): Response
/**
* Returns a response if no calculation is below the given margin.
*
* @throws \Doctrine\ORM\Exception\ORMException
* @throws ORMException
*/
private function getEmptyResponse(CalculationRepository $repository, float $minMargin): ?RedirectResponse
{
Expand Down
15 changes: 8 additions & 7 deletions src/Controller/CalculationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
use App\Spreadsheet\CalculationsDocument;
use App\Table\CalculationTable;
use App\Table\DataQuery;
use Doctrine\ORM\Exception\ORMException;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
Expand Down Expand Up @@ -65,7 +66,7 @@ public function __construct(
/**
* Add a new calculation.
*
* @throws \Doctrine\ORM\Exception\ORMException
* @throws ORMException
*/
#[GetPost(path: '/add', name: 'add')]
public function add(Request $request): Response
Expand All @@ -88,7 +89,7 @@ public function add(Request $request): Response
/**
* Edit a copy (cloned) calculation.
*
* @throws \Doctrine\ORM\Exception\ORMException
* @throws ORMException
*/
#[GetPost(path: '/clone/{id}', name: 'clone', requirements: self::ID_REQUIREMENT)]
public function clone(Request $request, Calculation $item): Response
Expand Down Expand Up @@ -117,7 +118,7 @@ public function delete(Request $request, Calculation $item, LoggerInterface $log
/**
* Edit a calculation.
*
* @throws \Doctrine\ORM\Exception\ORMException
* @throws ORMException
*/
#[GetPost(path: '/edit/{id}', name: 'edit', requirements: self::ID_REQUIREMENT)]
public function edit(Request $request, Calculation $item): Response
Expand All @@ -129,8 +130,8 @@ public function edit(Request $request, Calculation $item): Response
* Export the calculations to a Spreadsheet document.
*
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException if no calculation is found
* @throws \Doctrine\ORM\Exception\ORMException
* @throws \PhpOffice\PhpSpreadsheet\Exception
* @throws ORMException
*/
#[Get(path: '/excel', name: 'excel')]
public function excel(): SpreadsheetResponse
Expand Down Expand Up @@ -174,7 +175,7 @@ public function index(
* Export calculations to a PDF document.
*
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException if no calculation is found
* @throws \Doctrine\ORM\Exception\ORMException
* @throws ORMException
*/
#[Get(path: '/pdf', name: 'pdf')]
public function pdf(): PdfResponse
Expand Down Expand Up @@ -245,7 +246,7 @@ public function state(Request $request, Calculation $item): Response
/**
* @param Calculation $item
*
* @throws \Doctrine\ORM\Exception\ORMException
* @throws ORMException
*/
protected function editEntity(Request $request, EntityInterface $item, array $parameters = []): Response
{
Expand All @@ -267,7 +268,7 @@ protected function editEntity(Request $request, EntityInterface $item, array $pa
/**
* @psalm-param Calculation $item
*
* @throws \Doctrine\ORM\Exception\ORMException
* @throws ORMException
*/
protected function saveToDatabase(EntityInterface $item): void
{
Expand Down
7 changes: 4 additions & 3 deletions src/Controller/CalculationDuplicateController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use App\Table\CalculationDuplicateTable;
use App\Table\DataQuery;
use App\Traits\TableTrait;
use Doctrine\ORM\Exception\ORMException;
use Psr\Log\LoggerInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Response;
Expand All @@ -47,7 +48,7 @@ class CalculationDuplicateController extends AbstractController
* Export the duplicate items to a Spreadsheet document.
*
* @throws \PhpOffice\PhpSpreadsheet\Exception
* @throws \Doctrine\ORM\Exception\ORMException
* @throws ORMException
*/
#[Get(path: '/excel', name: 'excel')]
public function excel(CalculationRepository $repository): Response
Expand Down Expand Up @@ -78,7 +79,7 @@ public function index(
/**
* Exports the duplicate items in the calculations.
*
* @throws \Doctrine\ORM\Exception\ORMException
* @throws ORMException
*/
#[Get(path: '/pdf', name: 'pdf')]
public function pdf(CalculationRepository $repository): Response
Expand All @@ -96,7 +97,7 @@ public function pdf(CalculationRepository $repository): Response
/**
* Returns a response if no item is duplicated.
*
* @throws \Doctrine\ORM\Exception\ORMException
* @throws ORMException
*/
private function getEmptyResponse(CalculationRepository $repository): ?RedirectResponse
{
Expand Down
Loading

0 comments on commit ae3e140

Please sign in to comment.