This commit is contained in:
lubiana 2025-06-14 18:32:57 +02:00
parent 16533b1495
commit ab0677c463
Signed by: lubiana
SSH key fingerprint: SHA256:vW1EA0fRR3Fw+dD/sM0K+x3Il2gSry6YRYHqOeQwrfk
14 changed files with 705 additions and 38 deletions

View file

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace App\Controller;
use App\Form\BulkEditDrinkTypeStockForm;
use App\Form\BulkEditDrinkTypeWantedStockForm;
use App\Repository\DrinkTypeRepository;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
@ -38,7 +39,7 @@ final class DrinkTypeBulkController extends AbstractController
$entityManager->flush();
$this->addFlash('success', 'Wanted stock levels updated successfully!');
$this->addFlash('success', 'Current stock levels updated successfully!');
return $this->redirectToRoute('app_drink_type_bulk_edit_stock');
}
@ -48,4 +49,38 @@ final class DrinkTypeBulkController extends AbstractController
'drinkTypes' => $drinkTypes,
]);
}
#[Route('/bulk-edit-wanted-stock', name: 'app_drink_type_bulk_edit_wanted_stock')]
public function bulkEditWantedStock(
Request $request,
DrinkTypeRepository $drinkTypeRepository,
EntityManagerInterface $entityManager
): Response {
$drinkTypes = $drinkTypeRepository->findAll();
$form = $this->createForm(BulkEditDrinkTypeWantedStockForm::class, [
'drinkTypes' => $drinkTypes,
]);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$data = $form->getData();
foreach ($data['drinkTypes'] as $drinkType) {
$entityManager->persist($drinkType);
}
$entityManager->flush();
$this->addFlash('success', 'Wanted stock levels updated successfully!');
return $this->redirectToRoute('app_drink_type_bulk_edit_wanted_stock');
}
return $this->render('drink_type/bulk_edit_wanted_stock.html.twig', [
'form' => $form->createView(),
'drinkTypes' => $drinkTypes,
]);
}
}

View file

@ -25,7 +25,7 @@ class BulkEditDrinkTypeStockForm extends AbstractType
'by_reference' => false,
])
->add('save', SubmitType::class, [
'label' => 'Update Wanted Stock Levels',
'label' => 'Update Current Stock Levels',
'attr' => [
'class' => 'btn btn-primary',
],

View file

@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace App\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\SubmitType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class BulkEditDrinkTypeWantedStockForm extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('drinkTypes', CollectionType::class, [
'entry_type' => DrinkTypeWantedStockEditType::class,
'entry_options' => [
'label' => false,
],
'allow_add' => false,
'allow_delete' => false,
'by_reference' => false,
])
->add('save', SubmitType::class, [
'label' => 'Update Wanted Stock Levels',
'attr' => [
'class' => 'btn btn-primary',
],
]);
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => null,
]);
}
}

View file

@ -11,6 +11,8 @@ use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\GreaterThanOrEqual;
use Symfony\Component\Validator\Constraints\NotBlank;
class DrinkTypeStockEditType extends AbstractType
{
@ -26,13 +28,22 @@ class DrinkTypeStockEditType extends AbstractType
'readonly' => true,
],
])
->add('wantedStock', NumberType::class, [
'label' => 'Wanted Stock',
->add('currentStock', NumberType::class, [
'label' => 'Current Stock',
'attr' => [
'min' => 0,
'step' => 1,
'class' => 'form-control',
],
'constraints' => [
new NotBlank([
'message' => 'Current stock cannot be blank',
]),
new GreaterThanOrEqual([
'value' => 0,
'message' => 'Current stock must not be negative',
]),
],
]);
}

View file

@ -0,0 +1,56 @@
<?php
declare(strict_types=1);
namespace App\Form;
use App\Entity\DrinkType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\GreaterThanOrEqual;
use Symfony\Component\Validator\Constraints\NotBlank;
class DrinkTypeWantedStockEditType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('id', HiddenType::class, [
'mapped' => false,
])
->add('name', TextType::class, [
'disabled' => true,
'attr' => [
'readonly' => true,
],
])
->add('wantedStock', NumberType::class, [
'label' => 'Wanted Stock',
'attr' => [
'min' => 0,
'step' => 1,
'class' => 'form-control',
],
'constraints' => [
new NotBlank([
'message' => 'Wanted stock cannot be blank',
]),
new GreaterThanOrEqual([
'value' => 0,
'message' => 'Wanted stock must not be negative',
]),
],
]);
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => DrinkType::class,
]);
}
}

View file

@ -0,0 +1,24 @@
<?php
declare(strict_types=1);
namespace App\Service\DrinkType;
use App\Entity\DrinkType;
use App\Service\Config\LowStockMultiplier;
final readonly class FilterLowStockDrinks
{
public function __construct(private LowStockMultiplier $lowStockMultiplier) {}
/** @param DrinkType[] $drinkTypes */
public function __invoke(array $drinkTypes): array
{
return array_filter(
$drinkTypes,
fn(DrinkType $drinkType): bool =>
$drinkType->getCurrentStock() < ($drinkType->getWantedStock() * $this->lowStockMultiplier->getValue())
);
}
}