diff --git a/migrations/Version20250614171912.php b/migrations/Version20250614171912.php new file mode 100644 index 0000000..21ffd44 --- /dev/null +++ b/migrations/Version20250614171912.php @@ -0,0 +1,52 @@ +addSql(<<<'SQL' + ALTER TABLE system_config ADD COLUMN description CLOB DEFAULT '' NOT NULL + SQL); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql(<<<'SQL' + CREATE TEMPORARY TABLE __temp__system_config AS SELECT id, created_at, updated_at, "key", value FROM system_config + SQL); + $this->addSql(<<<'SQL' + DROP TABLE system_config + SQL); + $this->addSql(<<<'SQL' + CREATE TABLE system_config (id INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, created_at DATETIME NOT NULL --(DC2Type:datetime_immutable) + , updated_at DATETIME NOT NULL --(DC2Type:datetime_immutable) + , "key" VARCHAR(255) NOT NULL, value CLOB NOT NULL) + SQL); + $this->addSql(<<<'SQL' + INSERT INTO system_config (id, created_at, updated_at, "key", value) SELECT id, created_at, updated_at, "key", value FROM __temp__system_config + SQL); + $this->addSql(<<<'SQL' + DROP TABLE __temp__system_config + SQL); + $this->addSql(<<<'SQL' + CREATE UNIQUE INDEX UNIQ_C4049ABD8A90ABA9 ON system_config ("key") + SQL); + } +} diff --git a/src/Controller/InventoryRecordController.php b/src/Controller/InventoryRecordController.php deleted file mode 100644 index e33f24c..0000000 --- a/src/Controller/InventoryRecordController.php +++ /dev/null @@ -1,121 +0,0 @@ -render('inventory_record/index.html.twig', [ - 'inventory_records' => $inventoryRecordRepository->findAll(), - ]); - } - - #[Route('/new/{drinkType}', name: 'app_inventory_record_new', methods: ['GET', 'POST'])] - public function new(Request $request, EntityManagerInterface $entityManager, DrinkType $drinkType): Response - { - $inventoryRecord = new InventoryRecord(); - $inventoryRecord->setDrinkType($drinkType); - $form = $this->createForm(InventoryRecordForm::class, $inventoryRecord); - $form->handleRequest($request); - - if ($form->isSubmitted() && $form->isValid()) { - $entityManager->persist($inventoryRecord); - $entityManager->flush(); - - // If it's an HTMX request, return a redirect that HTMX will follow - if ($request->headers->has('HX-Request')) { - $response = new Response('', Response::HTTP_SEE_OTHER); - $response->headers->set('HX-Redirect', $this->generateUrl('app_index')); - return $response; - } - - return $this->redirectToRoute('app_index', [], Response::HTTP_SEE_OTHER); - } - - // Check if it's an HTMX request - if ($request->headers->has('HX-Request')) { - return $this->render('inventory_record/_modal_form.html.twig', [ - 'inventory_record' => $inventoryRecord, - 'form' => $form, - ]); - } - - return $this->render('inventory_record/new.html.twig', [ - 'inventory_record' => $inventoryRecord, - 'form' => $form, - ]); - } - - #[Route('/modal/{drinkType}', name: 'app_inventory_record_modal', methods: ['GET'])] - public function modal(DrinkType $drinkType, InventoryService $inventoryService): Response - { - $inventoryRecord = new InventoryRecord(); - $inventoryRecord->setDrinkType($drinkType); - $inventoryRecord->setQuantity( - $inventoryService->getLatestInventoryRecord($drinkType)->getQuantity() ?? 0 - ); - $form = $this->createForm(InventoryRecordForm::class, $inventoryRecord, [ - 'action' => $this->generateUrl('app_inventory_record_new', [ - 'drinkType' => $drinkType->getId(), - ]), - ]); - - return $this->render('inventory_record/_modal_form.html.twig', [ - 'inventory_record' => $inventoryRecord, - 'form' => $form, - ]); - } - - #[Route('/{id}', name: 'app_inventory_record_show', methods: ['GET'])] - public function show(InventoryRecord $inventoryRecord): Response - { - return $this->render('inventory_record/show.html.twig', [ - 'inventory_record' => $inventoryRecord, - ]); - } - - #[Route('/{id}/edit', name: 'app_inventory_record_edit', methods: ['GET', 'POST'])] - public function edit(Request $request, InventoryRecord $inventoryRecord, EntityManagerInterface $entityManager): Response - { - $form = $this->createForm(InventoryRecordForm::class, $inventoryRecord); - $form->handleRequest($request); - - if ($form->isSubmitted() && $form->isValid()) { - $entityManager->flush(); - - return $this->redirectToRoute('app_inventory_record_index', [], Response::HTTP_SEE_OTHER); - } - - return $this->render('inventory_record/edit.html.twig', [ - 'inventory_record' => $inventoryRecord, - 'form' => $form, - ]); - } - - #[Route('/{id}', name: 'app_inventory_record_delete', methods: ['POST'])] - public function delete(Request $request, InventoryRecord $inventoryRecord, EntityManagerInterface $entityManager): Response - { - if ($this->isCsrfTokenValid('delete' . $inventoryRecord->getId(), $request->getPayload()->getString('_token'))) { - $entityManager->remove($inventoryRecord); - $entityManager->flush(); - } - - return $this->redirectToRoute('app_inventory_record_index', [], Response::HTTP_SEE_OTHER); - } -} diff --git a/src/Controller/SystemConfigController.php b/src/Controller/SystemConfigController.php new file mode 100644 index 0000000..07f5146 --- /dev/null +++ b/src/Controller/SystemConfigController.php @@ -0,0 +1,61 @@ +findByKey($key); + } + + return $this->render('system_config/index.html.twig', [ + 'system_configs' => $configs, + ]); + } + + #[Route(path: '/{id}/edit', name: 'app_system_config_edit', methods: ['GET', 'POST'])] + public function edit(Request $request, SystemConfig $systemConfig, EntityManagerInterface $entityManager): Response + { + $form = $this->createForm(SystemConfigForm::class, $systemConfig); + $form->handleRequest($request); + + if ($form->isSubmitted() && $form->isValid()) { + $entityManager->flush(); + + return $this->redirectToRoute('app_system_config_index', [], Response::HTTP_SEE_OTHER); + } + + return $this->render('system_config/edit.html.twig', [ + 'system_config' => $systemConfig, + 'form' => $form, + ]); + } + + #[Route(path: '/{id}/reset', name: 'app_system_config_reset', methods: ['POST'])] + public function reset(Request $request, SystemConfig $systemConfig, ConfigurationService $configurationService): Response + { + if ($this->isCsrfTokenValid('reset' . $systemConfig->getId(), $request->getPayload()->getString('_token'))) { + $configurationService->reset($systemConfig->getKey()); + } + + return $this->redirectToRoute('app_system_config_index', [], Response::HTTP_SEE_OTHER); + } +} diff --git a/src/Entity/SystemConfig.php b/src/Entity/SystemConfig.php index 0743c2f..c726e05 100644 --- a/src/Entity/SystemConfig.php +++ b/src/Entity/SystemConfig.php @@ -14,11 +14,17 @@ use Doctrine\ORM\Mapping as ORM; class SystemConfig { public const string DEFAULT_STOCK_ADJUSTMENT_LOOKBACK_ORDERS = '3'; + public const string STOCK_ADJUSTMENT_LOOKBACK_ORDERS_DESCRIPTION = 'Number of orders to look back for stock adjustment'; public const string DEFAULT_DEFAULT_WANTED_STOCK = '2'; + public const string DEFAULT_DEFAULT_WANTED_STOCK_DESCRIPTION = 'Default wanted stock for new drink types'; public const string DEFAULT_SYSTEM_NAME = 'Zaufen'; + public const string DEFAULT_SYSTEM_NAME_DESCRIPTION = 'System name'; public const string DEFAULT_STOCK_INCREASE_AMOUNT = '1'; + public const string DEFAULT_STOCK_INCREASE_AMOUNT_DESCRIPTION = 'Amount to increase stock by for stock adjustment suggested by the system'; public const string DEFAULT_STOCK_DECREASE_AMOUNT = '1'; + public const string DEFAULT_STOCK_DECREASE_AMOUNT_DESCRIPTION = 'Amount to decrease stock by for stock adjustment suggested by the system'; public const string DEFAULT_STOCK_LOW_MULTIPLIER = '0.3'; + public const string DEFAULT_STOCK_LOW_MULTIPLIER_DESCRIPTION = 'Multiplier to to check if stock is low.'; #[ORM\Id] #[ORM\GeneratedValue] @@ -37,6 +43,12 @@ class SystemConfig #[ORM\Column(type: 'text')] private string $value; + #[ORM\Column(type: 'text', options: [ + 'default' => '', + ])] + private string $description; + + public function __construct( ) { $this->createdAt = new DateTimeImmutable(); @@ -85,4 +97,14 @@ class SystemConfig { $this->updatedAt = new DateTimeImmutable(); } + + public function getDescription(): string + { + return $this->description; + } + + public function setDescription(string $description): void + { + $this->description = $description; + } } diff --git a/src/Enum/SystemSettingKey.php b/src/Enum/SystemSettingKey.php index e4149f7..a084929 100644 --- a/src/Enum/SystemSettingKey.php +++ b/src/Enum/SystemSettingKey.php @@ -29,4 +29,16 @@ enum SystemSettingKey: string self::STOCK_LOW_MULTIPLIER => SystemConfig::DEFAULT_STOCK_LOW_MULTIPLIER, }; } + + public function defaultDescription(): string + { + return match ($this) { + self::STOCK_ADJUSTMENT_LOOKBACK_ORDERS => SystemConfig::STOCK_ADJUSTMENT_LOOKBACK_ORDERS_DESCRIPTION, + self::DEFAULT_WANTED_STOCK => SystemConfig::DEFAULT_DEFAULT_WANTED_STOCK_DESCRIPTION, + self::SYSTEM_NAME => SystemConfig::DEFAULT_SYSTEM_NAME_DESCRIPTION, + self::STOCK_INCREASE_AMOUNT => SystemConfig::DEFAULT_STOCK_INCREASE_AMOUNT_DESCRIPTION, + self::STOCK_DECREASE_AMOUNT => SystemConfig::DEFAULT_STOCK_DECREASE_AMOUNT_DESCRIPTION, + self::STOCK_LOW_MULTIPLIER => SystemConfig::DEFAULT_STOCK_LOW_MULTIPLIER_DESCRIPTION, + }; + } } diff --git a/src/Form/SystemConfigForm.php b/src/Form/SystemConfigForm.php new file mode 100644 index 0000000..f138fd9 --- /dev/null +++ b/src/Form/SystemConfigForm.php @@ -0,0 +1,35 @@ +add('value', TextType::class, [ + 'label' => 'Value', + 'help' => 'The value of the system setting', + ]) + ->add('description', TextType::class, [ + 'label' => 'Description', + 'help' => 'Description of the system setting', + 'disabled' => true, + ]); + } + + public function configureOptions(OptionsResolver $resolver): void + { + $resolver->setDefaults([ + 'data_class' => SystemConfig::class, + ]); + } +} diff --git a/src/Repository/SystemConfigRepository.php b/src/Repository/SystemConfigRepository.php index 0875798..c14f9f2 100644 --- a/src/Repository/SystemConfigRepository.php +++ b/src/Repository/SystemConfigRepository.php @@ -27,6 +27,11 @@ class SystemConfigRepository extends AbstractRepository $config = new SystemConfig(); $config->setKey($key); $config->setValue($key->defaultValue()); + $config->setDescription($key->defaultDescription()); + $this->save($config); + } + if ($config->getDescription() === '') { + $config->setDescription($key->defaultDescription()); $this->save($config); } return $config; diff --git a/templates/base.html.twig b/templates/base.html.twig index 756ef0d..318a0a4 100644 --- a/templates/base.html.twig +++ b/templates/base.html.twig @@ -24,6 +24,9 @@
Description | +Value | +Actions | +
---|---|---|
{{ system_config.description }} | +{{ system_config.value }} | ++ edit + | +
no records found | +