Skip to content

Commit

Permalink
add chart field
Browse files Browse the repository at this point in the history
  • Loading branch information
Praesidiarius committed May 7, 2024
1 parent 6caa50c commit d016ce3
Show file tree
Hide file tree
Showing 9 changed files with 124 additions and 4 deletions.
1 change: 1 addition & 0 deletions docker/database/sql/db_live.sql
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ CREATE TABLE `dynamic_form_field`
`default_data` varchar(255) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`related_table` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`related_table_col` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`related_table_order` varchar(50) COLLATE utf8mb4_unicode_ci DEFAULT NULL,
`on_index_default` INT(1) NOT NULL DEFAULT '0',
`default_sort_id` INT(1) NOT NULL DEFAULT '0'
) ENGINE = InnoDB
Expand Down
6 changes: 4 additions & 2 deletions docker/database/sql/modules/item/extensions/price_history.sql
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,11 @@ SET @item_price_table_section_id = LAST_INSERT_ID();

INSERT INTO `dynamic_form_field` (`id`, `parent_field_id`, `section_id`, `dynamic_form_id`, `label`, `field_key`,
`field_type`, `columns`, `default_data`, `related_table`, `related_table_col`,
`on_index_default`, `default_sort_id`)
`related_table_order`, `on_index_default`, `default_sort_id`)
VALUES (NULL, NULL, @item_price_table_section_id, @item_form_id, 'prices', 'price', 'table', 12, NULL,
'item_price', 'item_id', 0, 7);
'item_price', 'item_id', 'date ASC', 0, 7),
(NULL, NULL, @item_price_table_section_id, @item_form_id, 'item.price_history.title', 'price_history', 'chart', 12, NULL,
'item_price', 'item_id', 'date ASC', 0, 8);

SET @item_price_table_field_id = LAST_INSERT_ID();

Expand Down
15 changes: 15 additions & 0 deletions src/Entity/DynamicFormField.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ class DynamicFormField
#[ORM\Column(length: 50, nullable: true)]
private ?string $relatedTableCol = null;

#[ORM\Column(length: 50, nullable: true)]
private ?string $relatedTableOrder = null;

#[ORM\ManyToOne(targetEntity: self::class, inversedBy: 'nestedFields')]
private ?self $parentField = null;

Expand Down Expand Up @@ -210,6 +213,18 @@ public function setRelatedTable(?string $relatedTable): static
return $this;
}

public function getRelatedTableOrder(): ?string
{
return $this->relatedTableOrder;
}

public function setRelatedTableOrder(?string $relatedTableOrder): static
{
$this->relatedTableOrder = $relatedTableOrder;

return $this;
}

public function getRelatedTableCol(): ?string
{
return $this->relatedTableCol;
Expand Down
11 changes: 11 additions & 0 deletions src/Model/DynamicDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Entity\DynamicFormField;
use App\Repository\DynamicFormFieldRepository;
use App\Service\ChartDataGenerator;
use DateTimeInterface;
use Doctrine\DBAL\Connection;

Expand All @@ -14,6 +15,7 @@ class DynamicDto
public function __construct(
private readonly DynamicFormFieldRepository $dynamicFormFieldRepository,
private readonly Connection $connection,
private readonly ?ChartDataGenerator $chartDataGenerator = null,
) {
}

Expand Down Expand Up @@ -161,6 +163,10 @@ public function serializeDataForApiByFormModel(string $formKey): void
: $this->data[$field->getFieldKey()] ?? 0
) ?? 0,
),
'chart' => $this->chartDataGenerator
? $this->chartDataGenerator->getSerializedChartData($field->getFieldKey(), $this->getId())
: []
,
'table' => $this->getSerializedTableFieldData($field),
'currency' => $this->getSerializedCurrencyFieldData($field, $this->data[$field->getFieldKey()] ?? 0),
default => array_key_exists($field->getFieldKey(), $this->data)
Expand All @@ -182,6 +188,11 @@ private function getSerializedTableFieldData(DynamicFormField $formField): array
'id' => $this->getId()
]);

if ($formField->getRelatedTableOrder()) {
$orderBy = explode(' ', $formField->getRelatedTableOrder());
$qb->orderBy($orderBy[0], $orderBy[1]);
}

return $qb->fetchAllAssociative();
}

Expand Down
4 changes: 3 additions & 1 deletion src/Model/JobDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use App\Repository\JobPositionRepository;
use App\Repository\SystemSettingRepository;
use App\Repository\VoucherRepository;
use App\Service\ChartDataGenerator;
use Doctrine\DBAL\Connection;
use Symfony\Contracts\Translation\TranslatorInterface;

Expand All @@ -28,8 +29,9 @@ public function __construct(
private readonly ItemVoucherCodeRepository $voucherCodeRepository,
private readonly VoucherRepository $voucherRepository,
private readonly SystemSettingRepository $systemSettings,
private readonly ChartDataGenerator $chartDataGenerator,
) {
parent::__construct($this->dynamicFormFieldRepository, $this->connection);
parent::__construct($this->dynamicFormFieldRepository, $this->connection, $this->chartDataGenerator);
}

protected function getSerializedSelectFieldData(DynamicFormField $selectField, int $selectedValue): array
Expand Down
16 changes: 16 additions & 0 deletions src/Repository/ItemPriceHistoryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ public function getDynamicDto(): DynamicDto
return new DynamicDto($this->dynamicFormFieldRepository, $this->connection);
}

public function getPricesForItem(int $itemId): array
{
$qb = $this->connection->createQueryBuilder();
$qb
->select('*')
->from($this->baseTable)
->where('item_id = :itemId')
->orderBy('date', 'ASC')
->setParameters([
'itemId' => $itemId,
])
;

return $qb->fetchAllAssociative();
}

public function getCurrentPriceForItem(int $itemId): float
{
$qb = $this->connection->createQueryBuilder();
Expand Down
8 changes: 7 additions & 1 deletion src/Repository/ItemRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
namespace App\Repository;

use App\Model\DynamicDto;
use App\Service\ChartDataGenerator;
use Doctrine\DBAL\Connection;

class ItemRepository extends AbstractRepository
{
public function __construct(
private readonly Connection $connection,
private readonly DynamicFormFieldRepository $dynamicFormFieldRepository,
private readonly ChartDataGenerator $chartDataGenerator,
) {
parent::__construct($this->connection, $this->dynamicFormFieldRepository);

Expand All @@ -18,7 +20,11 @@ public function __construct(

public function getDynamicDto(): DynamicDto
{
return new DynamicDto($this->dynamicFormFieldRepository, $this->connection);
return new DynamicDto(
$this->dynamicFormFieldRepository,
$this->connection,
$this->chartDataGenerator,
);
}

public function findBySearchAttributes(int $page, int $pageSize): array
Expand Down
3 changes: 3 additions & 0 deletions src/Repository/JobRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Model\DynamicDto;
use App\Model\JobDto;
use App\Service\ChartDataGenerator;
use Doctrine\DBAL\Connection;
use Symfony\Contracts\Translation\TranslatorInterface;

Expand All @@ -19,6 +20,7 @@ public function __construct(
private readonly ItemVoucherCodeRepository $voucherCodeRepository,
private readonly VoucherRepository $voucherRepository,
private readonly SystemSettingRepository $systemSettings,
private readonly ChartDataGenerator $chartDataGenerator,
) {
parent::__construct($this->connection, $this->dynamicFormFieldRepository);

Expand All @@ -37,6 +39,7 @@ public function getDynamicDto(): DynamicDto
$this->voucherCodeRepository,
$this->voucherRepository,
$this->systemSettings,
$this->chartDataGenerator,
);
}

Expand Down
64 changes: 64 additions & 0 deletions src/Service/ChartDataGenerator.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace App\Service;

use App\Repository\DynamicFormFieldRepository;
use App\Repository\ItemPriceHistoryRepository;
use Cake\Chronos\Chronos;
use Symfony\Contracts\Translation\TranslatorInterface;

readonly class ChartDataGenerator
{
public function __construct(
private ItemPriceHistoryRepository $priceHistoryRepository,
private DynamicFormFieldRepository $dynamicFormFieldRepository,
private TranslatorInterface $translator,
) {
}

public function getSerializedChartData(string $chartKey, int $relatedEntityId): array
{
return match ($chartKey) {
'price_history' => $this->getPriceHistoryChartData($relatedEntityId),
default => []
};
}

private function getPriceHistoryChartData(int $itemId): array
{
$itemPrices = $this->priceHistoryRepository->getPricesForItem($itemId);

$priceHistoryFields = $this->dynamicFormFieldRepository->getUserFieldsByFormKey('itemPrice');
$priceFields = [];
foreach ($priceHistoryFields as $formField) {
if (str_starts_with($formField->getFieldKey(), 'price_')) {
$priceFields[] = $formField;
}
}

$chartData = [];
foreach ($priceFields as $priceField) {
$priceData = [];
foreach ($itemPrices as $itemPrice) {
if (array_key_exists($formField->getFieldKey(), $itemPrice)) {
$priceData[] = $itemPrice[$priceField->getFieldKey()];
}
}
$chartData[] = [
'name' => $this->translator->trans($priceField->getLabel()),
'color' => '',
'data' => $priceData,
];
}

$chartCategories = [];
foreach ($itemPrices as $itemPrice) {
$chartCategories[] = Chronos::parse($itemPrice['date'])->format('Y-m-d');
}

return [
'data' => $chartData,
'categories' => $chartCategories,
];
}
}

0 comments on commit d016ce3

Please sign in to comment.