Skip to content

Commit

Permalink
Reworked tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
laurentmuller committed Oct 9, 2023
1 parent 60a7680 commit 170aa30
Show file tree
Hide file tree
Showing 37 changed files with 832 additions and 534 deletions.
12 changes: 6 additions & 6 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion src/Chart/MonthChart.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,8 +148,13 @@ private function getAllowedMonths(): array
{
$step = 6;
$maxMonths = $this->repository->countDistinctMonths();
if ($maxMonths <= $step) {
return [$maxMonths];
}

if ($maxMonths % $step > 0) {
$maxMonths += $step;
$delta = $step % $maxMonths;
$maxMonths += $delta;
}

return \range($step, $maxMonths, $step);
Expand Down
12 changes: 12 additions & 0 deletions src/Controller/ChartController.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@

use App\Chart\MonthChart;
use App\Chart\StateChart;
use App\Entity\Calculation;
use App\Enums\EntityPermission;
use App\Interfaces\RoleInterface;
use App\Report\CalculationByMonthReport;
use App\Report\CalculationByStateReport;
Expand Down Expand Up @@ -48,6 +50,7 @@ class ChartController extends AbstractController
#[Route(path: '/month', name: 'chart_by_month', methods: Request::METHOD_GET)]
public function month(Request $request, MonthChart $chart): Response
{
$this->checkAccess();
$key = 'chart_by_month';
$months = $this->getMonths($request);
$parameters = $chart->generate($months);
Expand All @@ -62,6 +65,7 @@ public function month(Request $request, MonthChart $chart): Response
#[Route(path: '/month/pdf', name: 'chart_by_month_pdf', methods: Request::METHOD_GET)]
public function monthPdf(Request $request, CalculationRepository $repository): PdfResponse
{
$this->checkAccess(EntityPermission::EXPORT);
$months = $this->getMonths($request);
$data = $repository->getByMonth($months);
$report = new CalculationByMonthReport($this, $data);
Expand All @@ -77,18 +81,26 @@ public function monthPdf(Request $request, CalculationRepository $repository): P
#[Route(path: '/state', name: 'chart_by_state', methods: Request::METHOD_GET)]
public function state(StateChart $chart): Response
{
$this->checkAccess();

return $this->render('chart/chart_state.html.twig', $chart->generate());
}

#[Route(path: '/state/pdf', name: 'chart_by_state_pdf', methods: Request::METHOD_GET)]
public function statePdf(CalculationStateRepository $repository): PdfResponse
{
$this->checkAccess(EntityPermission::EXPORT);
$data = $repository->getCalculations();
$report = new CalculationByStateReport($this, $data);

return $this->renderPdfDocument($report);
}

private function checkAccess(EntityPermission $permission = EntityPermission::SHOW): void
{
$this->denyAccessUnlessGranted($permission, Calculation::class);
}

private function getMonths(Request $request): int
{
$count = $this->getSessionInt('chart_by_month', 6);
Expand Down
4 changes: 2 additions & 2 deletions src/Controller/ThemeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
namespace App\Controller;

use App\Enums\Theme;
use App\Interfaces\RoleInterface;
use App\Service\ThemeService;
use Symfony\Component\ExpressionLanguage\Expression;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Attribute\AsController;
Expand All @@ -26,7 +26,7 @@
*/
#[AsController]
#[Route(path: '/theme')]
#[IsGranted(RoleInterface::ROLE_USER)]
#[IsGranted(new Expression('is_granted("ROLE_USER") and user.isEnabled()'))]
class ThemeController extends AbstractController
{
#[Route(path: '/dialog', name: 'theme_dialog', methods: Request::METHOD_GET)]
Expand Down
59 changes: 41 additions & 18 deletions src/Report/CalculationByMonthReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@

namespace App\Report;

use App\Pdf\PdfCell;
use App\Pdf\PdfColumn;
use App\Pdf\PdfStyle;
use App\Pdf\PdfTableBuilder;
use App\Pdf\PdfTextColor;
use App\Repository\CalculationRepository;
use App\Traits\MathTrait;
use App\Utils\FormatUtils;

/**
Expand All @@ -26,6 +30,8 @@
*/
class CalculationByMonthReport extends AbstractArrayReport
{
use MathTrait;

/**
* @psalm-param CalculationByMonthType[] $entities
*/
Expand All @@ -37,31 +43,31 @@ protected function doRender(array $entities): bool
$table = $this->createTable();

foreach ($entities as $entity) {
$table->startRow()
->add($this->formatDate($entity['date']))
->add(FormatUtils::formatInt($entity['count']))
->add(FormatUtils::formatInt($entity['items']))
->add(FormatUtils::formatInt($entity['total'] - $entity['items']))
->add(FormatUtils::formatPercent($entity['margin'], false))
->add(FormatUtils::formatInt($entity['total']))
->endRow();
$table->startRow()->addValues(
$this->formatDate($entity['date']),
FormatUtils::formatInt($entity['count']),
FormatUtils::formatInt($entity['items']),
FormatUtils::formatInt($entity['total'] - $entity['items']),
$this->formatPercent($entity['margin']),
FormatUtils::formatInt($entity['total'])
)->endRow();
}

// total
$count = $this->sum($entities, 'count');
$items = $this->sum($entities, 'items');
$total = $this->sum($entities, 'total');
$net = $total - $items;
$margin = 1.0 + $net / $items;
$margin = 1.0 + $this->safeDivide($net, $items);

$table->startHeaderRow()
->add($this->transChart('fields.total'))
->add(FormatUtils::formatInt($count))
->add(FormatUtils::formatInt($items))
->add(FormatUtils::formatInt($net))
->add(FormatUtils::formatPercent($margin, false))
->add(FormatUtils::formatInt($total))
->endRow();
$table->startHeaderRow()->addValues(
$this->transChart('fields.total'),
FormatUtils::formatInt($count),
FormatUtils::formatInt($items),
FormatUtils::formatInt($net),
$this->formatPercent($margin, true),
FormatUtils::formatInt($total)
)->endRow();

return true;
}
Expand All @@ -73,7 +79,7 @@ private function createTable(): PdfTableBuilder
PdfColumn::right($this->transChart('fields.count'), 25, true),
PdfColumn::right($this->transChart('fields.net'), 25, true),
PdfColumn::right($this->transChart('fields.margin_amount'), 25, true),
PdfColumn::right($this->transChart('fields.margin_percent'), 25, true),
PdfColumn::right($this->transChart('fields.margin_percent'), 20, true),
PdfColumn::right($this->transChart('fields.total'), 25, true),
];

Expand All @@ -87,6 +93,23 @@ private function formatDate(\DateTimeInterface $date): string
return \ucfirst(FormatUtils::formatDate(date: $date, pattern: 'MMMM Y'));
}

private function formatPercent(float $value, bool $bold = false): PdfCell
{
$cell = new PdfCell(FormatUtils::formatPercent($value, false));
$style = $bold ? PdfStyle::getHeaderStyle() : PdfStyle::getCellStyle();
if ($this->isMinMargin($value)) {
$style->setTextColor(PdfTextColor::red());
}
$cell->setStyle($style);

return $cell;
}

private function isMinMargin(float $value): bool
{
return !$this->isFloatZero($value) && $value < $this->controller->getMinMargin();
}

private function sum(array $entities, string $key): float
{
return \array_sum(\array_column($entities, $key));
Expand Down
72 changes: 46 additions & 26 deletions src/Report/CalculationByStateReport.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@

namespace App\Report;

use App\Pdf\PdfCell;
use App\Pdf\PdfColumn;
use App\Pdf\PdfStyle;
use App\Pdf\PdfTableBuilder;
use App\Pdf\PdfTextColor;
use App\Repository\CalculationStateRepository;
use App\Traits\MathTrait;
use App\Utils\FormatUtils;

/**
Expand All @@ -26,6 +30,8 @@
*/
class CalculationByStateReport extends AbstractArrayReport
{
use MathTrait;

/**
* @psalm-param QueryCalculationType[] $entities
*/
Expand All @@ -37,36 +43,38 @@ protected function doRender(array $entities): bool
$table = $this->createTable();

foreach ($entities as $entity) {
$table->startRow()
->add($entity['code'])
->add(FormatUtils::formatInt($entity['count']))
->add($this->formatPercent($entity['percentCalculation']))
->add(FormatUtils::formatInt($entity['items']))
->add(FormatUtils::formatInt($entity['marginAmount']))
->add(FormatUtils::formatPercent($entity['margin']))
->add(FormatUtils::formatInt($entity['total']))
->add($this->formatPercent($entity['percentAmount']))
->endRow();
$table->startRow()->addValues(
$entity['code'],
FormatUtils::formatInt($entity['count']),
$this->formatPercent($entity['percentCalculation'], 2),
FormatUtils::formatInt($entity['items']),
FormatUtils::formatInt($entity['marginAmount']),
$this->formatPercent($entity['margin'], 0, true),
FormatUtils::formatInt($entity['total']),
$this->formatPercent($entity['percentAmount'], 2)
)->endRow();
}

// total
$count = $this->sum($entities, 'count');
$percentCalculation = $this->sum($entities, 'percentCalculation');
$items = $this->sum($entities, 'items');
$marginAmount = $this->sum($entities, 'marginAmount');
$total = $this->sum($entities, 'total');
$net = $total - $items;
$margin = 1.0 + $net / $items;

$table->startHeaderRow()
->add($this->transChart('fields.total'))
->add(FormatUtils::formatInt($count))
->add($this->formatPercent(1.0))
->add(FormatUtils::formatInt($items))
->add(FormatUtils::formatInt($marginAmount))
->add($this->formatPercent($margin, 0))
->add(FormatUtils::formatInt($total))
->add($this->formatPercent(1.0))
->endRow();
$margin = 1.0 + $this->safeDivide($net, $items);
$percentAmount = $this->sum($entities, 'percentAmount');

$table->startHeaderRow()->addValues(
$this->transChart('fields.total'),
FormatUtils::formatInt($count),
$this->formatPercent($percentCalculation, 2, bold: true),
FormatUtils::formatInt($items),
FormatUtils::formatInt($marginAmount),
$this->formatPercent($margin, 0, bold: true),
FormatUtils::formatInt($total),
$this->formatPercent($percentAmount, 2, bold: true),
)->endRow();

return true;
}
Expand All @@ -76,22 +84,34 @@ private function createTable(): PdfTableBuilder
$columns = [
PdfColumn::left($this->transChart('fields.state'), 20),
PdfColumn::right($this->transChart('fields.count'), 25, true),
PdfColumn::right($this->transChart('fields.percent'), 25, true),
PdfColumn::right('%', 15, true),
PdfColumn::right($this->transChart('fields.net'), 20, true),
PdfColumn::right($this->transChart('fields.margin_amount'), 20, true),
PdfColumn::right($this->transChart('fields.margin_percent'), 20, true),
PdfColumn::right($this->transChart('fields.total'), 20, true),
PdfColumn::right($this->transChart('fields.percent'), 25, true),
PdfColumn::right('%', 15, true),
];

return PdfTableBuilder::instance($this)
->addColumns(...$columns)
->outputHeaders();
}

private function formatPercent(float $value, int $decimals = 1): string
private function formatPercent(float $value, int $decimals = 1, bool $useStyle = false, bool $bold = false): PdfCell
{
$style = $bold ? PdfStyle::getHeaderStyle() : PdfStyle::getCellStyle();
$cell = new PdfCell(FormatUtils::formatPercent($value, false, $decimals, \NumberFormatter::ROUND_HALFEVEN));
if ($useStyle && $this->isMinMargin($value)) {
$style->setTextColor(PdfTextColor::red());
}
$cell->setStyle($style);

return $cell;
}

private function isMinMargin(float $value): bool
{
return FormatUtils::formatPercent($value, true, $decimals, \NumberFormatter::ROUND_HALFEVEN);
return !$this->isFloatZero($value) && $value < $this->controller->getMinMargin();
}

private function sum(array $entities, string $key): float
Expand Down
Loading

0 comments on commit 170aa30

Please sign in to comment.