get ready
This commit is contained in:
parent
ca9819a436
commit
6b0a478ca5
16 changed files with 395 additions and 124 deletions
51
src/Controller/DrinkTypeBulkController.php
Normal file
51
src/Controller/DrinkTypeBulkController.php
Normal file
|
@ -0,0 +1,51 @@
|
|||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Controller;
|
||||
|
||||
use App\Form\BulkEditDrinkTypeStockForm;
|
||||
use App\Repository\DrinkTypeRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
|
||||
#[Route('/drink-types')]
|
||||
final class DrinkTypeBulkController extends AbstractController
|
||||
{
|
||||
#[Route('/bulk-edit-stock', name: 'app_drink_type_bulk_edit_stock')]
|
||||
public function bulkEditStock(
|
||||
Request $request,
|
||||
DrinkTypeRepository $drinkTypeRepository,
|
||||
EntityManagerInterface $entityManager
|
||||
): Response {
|
||||
$drinkTypes = $drinkTypeRepository->findAll();
|
||||
|
||||
$form = $this->createForm(BulkEditDrinkTypeStockForm::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_stock');
|
||||
}
|
||||
|
||||
return $this->render('drink_type/bulk_edit_stock.html.twig', [
|
||||
'form' => $form->createView(),
|
||||
'drinkTypes' => $drinkTypes,
|
||||
]);
|
||||
}
|
||||
}
|
|
@ -8,10 +8,8 @@ use App\Entity\DrinkType;
|
|||
use App\Form\DrinkTypeForm;
|
||||
use App\Form\DrinkTypeFormCurrentStockForm;
|
||||
use App\Repository\DrinkTypeRepository;
|
||||
use App\Repository\PropertyChangeLogRepository;
|
||||
use App\Service\DrinkType\GetStockHistory;
|
||||
use App\Service\DrinkType\GetWantedHistory;
|
||||
use App\Service\InventoryService;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
|
@ -54,8 +52,7 @@ final class DrinkTypeController extends AbstractController
|
|||
DrinkType $drinkType,
|
||||
GetStockHistory $getStockHistory,
|
||||
GetWantedHistory $getWantedHistory,
|
||||
): Response
|
||||
{
|
||||
): Response {
|
||||
// Get orders that contain this drink type
|
||||
$orderItems = $drinkType->getOrderItems();
|
||||
|
||||
|
|
|
@ -9,6 +9,7 @@ use DateTimeImmutable;
|
|||
use Doctrine\Common\Collections\ArrayCollection;
|
||||
use Doctrine\Common\Collections\Collection;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Symfony\Component\Validator\Constraints as Assert;
|
||||
|
||||
#[ORM\Entity(repositoryClass: DrinkTypeRepository::class)]
|
||||
#[ORM\Table(name: 'drink_type')]
|
||||
|
@ -33,6 +34,7 @@ class DrinkType
|
|||
#[ORM\Column(type: 'text', nullable: true)]
|
||||
private null|string $description = null;
|
||||
#[ORM\Column(type: 'integer')]
|
||||
#[Assert\GreaterThanOrEqual(0, message: 'Wanted stock must not be negative.')]
|
||||
private int $wantedStock = 10;
|
||||
|
||||
#[ORM\Column(type: 'integer')]
|
||||
|
|
41
src/Form/BulkEditDrinkTypeStockForm.php
Normal file
41
src/Form/BulkEditDrinkTypeStockForm.php
Normal 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 BulkEditDrinkTypeStockForm extends AbstractType
|
||||
{
|
||||
public function buildForm(FormBuilderInterface $builder, array $options): void
|
||||
{
|
||||
$builder
|
||||
->add('drinkTypes', CollectionType::class, [
|
||||
'entry_type' => DrinkTypeStockEditType::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,
|
||||
]);
|
||||
}
|
||||
}
|
45
src/Form/DrinkTypeStockEditType.php
Normal file
45
src/Form/DrinkTypeStockEditType.php
Normal file
|
@ -0,0 +1,45 @@
|
|||
<?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;
|
||||
|
||||
class DrinkTypeStockEditType 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',
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
public function configureOptions(OptionsResolver $resolver): void
|
||||
{
|
||||
$resolver->setDefaults([
|
||||
'data_class' => DrinkType::class,
|
||||
]);
|
||||
}
|
||||
}
|
|
@ -19,7 +19,7 @@ final readonly class CreateNewOrderItems
|
|||
public function __invoke(Order $order): void
|
||||
{
|
||||
new ArrayCollection($this->drinkTypeRepository->findWanted())->forAll(
|
||||
function (int $index, DrinkType $drinkType) use ($order) {
|
||||
function (int $index, DrinkType $drinkType) use ($order): true {
|
||||
$quantity = $drinkType->getWantedStock() - $drinkType->getCurrentStock();
|
||||
if ($quantity > 0) {
|
||||
$order->addOrderItem(
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue