updateeeeee

This commit is contained in:
lubiana 2025-06-10 20:14:19 +02:00
parent 0c758749e0
commit ca9819a436
Signed by: lubiana
SSH key fingerprint: SHA256:vW1EA0fRR3Fw+dD/sM0K+x3Il2gSry6YRYHqOeQwrfk
12 changed files with 214 additions and 156 deletions

View file

@ -6,7 +6,11 @@ namespace App\Controller;
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;
@ -18,10 +22,10 @@ use Symfony\Component\Routing\Attribute\Route;
final class DrinkTypeController extends AbstractController
{
#[Route(name: 'app_drink_type_index', methods: ['GET'])]
public function index(InventoryService $inventoryService): Response
public function index(DrinkTypeRepository $drinkTypeRepository): Response
{
return $this->render('drink_type/index.html.twig', [
'drink_stocks' => $inventoryService->getAllDrinkTypesWithStockLevels(true),
'drink_types' => $drinkTypeRepository->findAll(),
]);
}
@ -46,28 +50,20 @@ final class DrinkTypeController extends AbstractController
}
#[Route(path: '/{id}', name: 'app_drink_type_show', methods: ['GET'])]
public function show(DrinkType $drinkType, PropertyChangeLogRepository $propertyChangeLogRepository): Response
public function show(
DrinkType $drinkType,
GetStockHistory $getStockHistory,
GetWantedHistory $getWantedHistory,
): Response
{
// Get orders that contain this drink type
$orderItems = $drinkType->getOrderItems();
// Get inventory history for this drink type
$inventoryRecords = $drinkType->getInventoryRecords();
// Get desired stock history from PropertyChangeLog
$desiredStockHistory = $propertyChangeLogRepository->findBy([
'entityClass' => DrinkType::class,
'propertyName' => 'desiredStock',
'entityId' => $drinkType->getId(),
], [
'changeDate' => 'DESC',
]);
return $this->render('drink_type/show.html.twig', [
'drink_type' => $drinkType,
'order_items' => $orderItems,
'inventory_records' => $inventoryRecords,
'desired_stock_history' => $desiredStockHistory,
'stock_history' => $getStockHistory($drinkType),
'wanted_history' => $getWantedHistory($drinkType),
]);
}
@ -89,6 +85,24 @@ final class DrinkTypeController extends AbstractController
]);
}
#[Route(path: '/{id}/update-stock', name: 'app_drink_type_update_stock', methods: ['GET', 'POST'])]
public function updateStock(Request $request, DrinkType $drinkType, EntityManagerInterface $entityManager): Response
{
$form = $this->createForm(DrinkTypeFormCurrentStockForm::class, $drinkType);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$entityManager->flush();
return $this->redirectToRoute('app_index', [], Response::HTTP_SEE_OTHER);
}
return $this->render('drink_type/edit.html.twig', [
'drink_type' => $drinkType,
'form' => $form,
]);
}
#[Route(path: '/{id}', name: 'app_drink_type_delete', methods: ['POST'])]
public function delete(Request $request, DrinkType $drinkType, EntityManagerInterface $entityManager): Response
{

View file

@ -4,6 +4,7 @@ declare(strict_types=1);
namespace App\Controller;
use App\Repository\DrinkTypeRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
@ -11,8 +12,11 @@ use Symfony\Component\Routing\Attribute\Route;
#[Route(path: '/', name: 'app_index')]
final class Index extends AbstractController
{
public function __invoke(): Response
public function __invoke(DrinkTypeRepository $drinkTypeRepository): Response
{
return new Response('<h1>Hello World!</h1>');
return $this->render('index.html.twig', [
'drinkTypes' => $drinkTypeRepository->findWanted(),
]);
}
}

View file

@ -14,7 +14,11 @@ class DrinkTypeForm extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder->add('name')->add('description')->add('desiredStock', NumberType::class);
$builder
->add('name')
->add('description')
->add('currentStock', NumberType::class)
->add('wantedStock', NumberType::class);
}
public function configureOptions(OptionsResolver $resolver): void

View file

@ -5,34 +5,23 @@ declare(strict_types=1);
namespace App\Form;
use App\Entity\DrinkType;
use App\Entity\InventoryRecord;
use Symfony\Bridge\Doctrine\Form\Type\EntityType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;
class InventoryRecordForm extends AbstractType
class DrinkTypeFormCurrentStockForm extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
->add('quantity', NumberType::class)
->add('drinkType', EntityType::class, [
'class' => DrinkType::class,
'choice_label' => 'id',
'attr' => [
'style' => 'display: none;',
],
'label' => false,
])
;
->add('currentStock', NumberType::class);
}
public function configureOptions(OptionsResolver $resolver): void
{
$resolver->setDefaults([
'data_class' => InventoryRecord::class,
'data_class' => DrinkType::class,
]);
}
}