add orderstuff

This commit is contained in:
lubiana 2025-06-14 22:43:27 +02:00
parent 51fb951b2b
commit d7a61f6d0e
Signed by: lubiana
SSH key fingerprint: SHA256:vW1EA0fRR3Fw+dD/sM0K+x3Il2gSry6YRYHqOeQwrfk
9 changed files with 209 additions and 10 deletions

View file

@ -6,6 +6,7 @@ namespace App\Controller;
use App\Repository\DrinkTypeRepository;
use App\Service\DrinkType\FilterLowStockDrinks;
use App\Service\OrderService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
@ -15,13 +16,16 @@ final class Index extends AbstractController
{
public function __invoke(
DrinkTypeRepository $drinkTypeRepository,
FilterLowStockDrinks $filterLowStockDrinks
FilterLowStockDrinks $filterLowStockDrinks,
OrderService $orderService,
): Response {
$wanted = $drinkTypeRepository->findWanted();
$lowStock = $filterLowStockDrinks($wanted);
$orders = $orderService->getActiveOrders();
return $this->render('index.html.twig', [
'drinkTypes' => $lowStock,
'lowStock' => $lowStock,
'orders' => $orders,
]);
}

View file

@ -7,6 +7,7 @@ namespace App\Controller;
use App\Entity\Order;
use App\Form\OrderForm;
use App\Repository\OrderRepository;
use App\Service\DrinkType\CreateNewOrderItems;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
@ -25,9 +26,10 @@ final class OrderController extends AbstractController
}
#[Route('/new', name: 'app_order_new', methods: ['GET', 'POST'])]
public function new(Request $request, EntityManagerInterface $entityManager): Response
public function new(Request $request, EntityManagerInterface $entityManager, CreateNewOrderItems $createNewOrderItems): Response
{
$order = new Order();
$createNewOrderItems($order);
$form = $this->createForm(OrderForm::class, $order);
$form->handleRequest($request);
@ -35,7 +37,7 @@ final class OrderController extends AbstractController
$entityManager->persist($order);
$entityManager->flush();
return $this->redirectToRoute('app_order_index', [], Response::HTTP_SEE_OTHER);
return $this->redirectToRoute('app_index', [], Response::HTTP_SEE_OTHER);
}
return $this->render('order/new.html.twig', [

View file

@ -44,6 +44,7 @@ class Order
$this->createdAt = new DateTimeImmutable();
$this->updatedAt = new DateTimeImmutable();
$this->orderItems = new ArrayCollection();
$this->status = OrderStatus::NEW;
}
public function getId(): null|int

View file

@ -5,7 +5,10 @@ declare(strict_types=1);
namespace App\Form;
use App\Entity\Order;
use App\Enum\OrderStatus;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\EnumType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
@ -14,7 +17,15 @@ class OrderForm extends AbstractType
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('status')
->add('status', EnumType::class, ['class' => OrderStatus::class])
->add('orderItems', CollectionType::class, [
'entry_type' => OrderItemType::class,
'entry_options' => ['label' => false],
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false,
'label' => 'Order Items',
])
;
}

View file

@ -0,0 +1,46 @@
<?php
declare(strict_types=1);
namespace App\Form;
use App\Entity\DrinkType;
use App\Entity\OrderItem;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
use Symfony\Component\Validator\Constraints\GreaterThan;
use Symfony\Component\Validator\Constraints\NotNull;
class OrderItemType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('drinkType', EntityType::class, [
'class' => DrinkType::class,
'choice_label' => 'name',
'placeholder' => 'Select a drink type',
'required' => true,
'constraints' => [
new NotNull(message: 'Please select a drink type'),
],
])
->add('quantity', IntegerType::class, [
'required' => true,
'constraints' => [
new NotNull(message: 'Please enter a quantity'),
new GreaterThan(value: 0, message: 'Quantity must be greater than 0'),
],
]);
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => OrderItem::class,
]);
}
}