add orderstuff
This commit is contained in:
parent
51fb951b2b
commit
d7a61f6d0e
9 changed files with 209 additions and 10 deletions
|
@ -6,6 +6,7 @@ namespace App\Controller;
|
||||||
|
|
||||||
use App\Repository\DrinkTypeRepository;
|
use App\Repository\DrinkTypeRepository;
|
||||||
use App\Service\DrinkType\FilterLowStockDrinks;
|
use App\Service\DrinkType\FilterLowStockDrinks;
|
||||||
|
use App\Service\OrderService;
|
||||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||||
use Symfony\Component\HttpFoundation\Response;
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
use Symfony\Component\Routing\Attribute\Route;
|
use Symfony\Component\Routing\Attribute\Route;
|
||||||
|
@ -15,13 +16,16 @@ final class Index extends AbstractController
|
||||||
{
|
{
|
||||||
public function __invoke(
|
public function __invoke(
|
||||||
DrinkTypeRepository $drinkTypeRepository,
|
DrinkTypeRepository $drinkTypeRepository,
|
||||||
FilterLowStockDrinks $filterLowStockDrinks
|
FilterLowStockDrinks $filterLowStockDrinks,
|
||||||
|
OrderService $orderService,
|
||||||
): Response {
|
): Response {
|
||||||
$wanted = $drinkTypeRepository->findWanted();
|
$wanted = $drinkTypeRepository->findWanted();
|
||||||
$lowStock = $filterLowStockDrinks($wanted);
|
$lowStock = $filterLowStockDrinks($wanted);
|
||||||
|
$orders = $orderService->getActiveOrders();
|
||||||
return $this->render('index.html.twig', [
|
return $this->render('index.html.twig', [
|
||||||
'drinkTypes' => $lowStock,
|
'drinkTypes' => $lowStock,
|
||||||
'lowStock' => $lowStock,
|
'lowStock' => $lowStock,
|
||||||
|
'orders' => $orders,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,6 +7,7 @@ namespace App\Controller;
|
||||||
use App\Entity\Order;
|
use App\Entity\Order;
|
||||||
use App\Form\OrderForm;
|
use App\Form\OrderForm;
|
||||||
use App\Repository\OrderRepository;
|
use App\Repository\OrderRepository;
|
||||||
|
use App\Service\DrinkType\CreateNewOrderItems;
|
||||||
use Doctrine\ORM\EntityManagerInterface;
|
use Doctrine\ORM\EntityManagerInterface;
|
||||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||||
use Symfony\Component\HttpFoundation\Request;
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
@ -25,9 +26,10 @@ final class OrderController extends AbstractController
|
||||||
}
|
}
|
||||||
|
|
||||||
#[Route('/new', name: 'app_order_new', methods: ['GET', 'POST'])]
|
#[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();
|
$order = new Order();
|
||||||
|
$createNewOrderItems($order);
|
||||||
$form = $this->createForm(OrderForm::class, $order);
|
$form = $this->createForm(OrderForm::class, $order);
|
||||||
$form->handleRequest($request);
|
$form->handleRequest($request);
|
||||||
|
|
||||||
|
@ -35,7 +37,7 @@ final class OrderController extends AbstractController
|
||||||
$entityManager->persist($order);
|
$entityManager->persist($order);
|
||||||
$entityManager->flush();
|
$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', [
|
return $this->render('order/new.html.twig', [
|
||||||
|
|
|
@ -44,6 +44,7 @@ class Order
|
||||||
$this->createdAt = new DateTimeImmutable();
|
$this->createdAt = new DateTimeImmutable();
|
||||||
$this->updatedAt = new DateTimeImmutable();
|
$this->updatedAt = new DateTimeImmutable();
|
||||||
$this->orderItems = new ArrayCollection();
|
$this->orderItems = new ArrayCollection();
|
||||||
|
$this->status = OrderStatus::NEW;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function getId(): null|int
|
public function getId(): null|int
|
||||||
|
|
|
@ -5,7 +5,10 @@ declare(strict_types=1);
|
||||||
namespace App\Form;
|
namespace App\Form;
|
||||||
|
|
||||||
use App\Entity\Order;
|
use App\Entity\Order;
|
||||||
|
use App\Enum\OrderStatus;
|
||||||
use Symfony\Component\Form\AbstractType;
|
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\Form\FormBuilderInterface;
|
||||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||||
|
|
||||||
|
@ -14,7 +17,15 @@ class OrderForm extends AbstractType
|
||||||
public function buildForm(FormBuilderInterface $builder, array $options): void
|
public function buildForm(FormBuilderInterface $builder, array $options): void
|
||||||
{
|
{
|
||||||
$builder
|
$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',
|
||||||
|
])
|
||||||
;
|
;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
46
src/Form/OrderItemType.php
Normal file
46
src/Form/OrderItemType.php
Normal 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,
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
|
@ -16,6 +16,9 @@
|
||||||
<li>{{ drinkType.name }} ({{ drinkType.currentStock }} / {{ drinkType.wantedStock }})</li>
|
<li>{{ drinkType.name }} ({{ drinkType.currentStock }} / {{ drinkType.wantedStock }})</li>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</ul>
|
</ul>
|
||||||
|
{% if orders != [] %}
|
||||||
|
<a href="{{ path('app_order_new') }}" class="btn btn-primary">Create New Order</a>
|
||||||
|
{% endif %}
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
|
|
|
@ -1,4 +1,114 @@
|
||||||
{{ form_start(form) }}
|
{{ form_start(form) }}
|
||||||
{{ form_widget(form) }}
|
{{ form_row(form.status) }}
|
||||||
<button class="btn">{{ button_label|default('Save') }}</button>
|
|
||||||
|
<h3>Order Items</h3> <div class="order-items-wrapper"
|
||||||
|
data-prototype="{{ form_widget(form.orderItems.vars.prototype)|e('html_attr') }}"
|
||||||
|
data-index="{{ form.orderItems|length }}">
|
||||||
|
<table class="table">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>Drink Type</th>
|
||||||
|
<th>Quantity</th>
|
||||||
|
<th>Actions</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody class="order-items-list">
|
||||||
|
{% for orderItemForm in form.orderItems %}
|
||||||
|
<tr class="order-item-row">
|
||||||
|
<td>{{ form_widget(orderItemForm.drinkType) }}</td>
|
||||||
|
<td>{{ form_widget(orderItemForm.quantity) }}</td>
|
||||||
|
<td>
|
||||||
|
<button type="button" class="btn btn-danger remove-order-item">Remove</button>
|
||||||
|
</td>
|
||||||
|
</tr>
|
||||||
|
{% endfor %}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
<button type="button" class="btn btn-success add-order-item">Add Item</button>
|
||||||
|
</div>
|
||||||
|
|
||||||
|
<button class="btn btn-primary mt-3">{{ button_label|default('Save') }}</button>
|
||||||
{{ form_end(form) }}
|
{{ form_end(form) }}
|
||||||
|
|
||||||
|
<script>
|
||||||
|
document.addEventListener('DOMContentLoaded', function() {
|
||||||
|
// Add new item
|
||||||
|
document.querySelector('.add-order-item').addEventListener('click', function() {
|
||||||
|
const wrapper = document.querySelector('.order-items-wrapper');
|
||||||
|
const index = parseInt(wrapper.dataset.index);
|
||||||
|
|
||||||
|
// Get the prototype HTML
|
||||||
|
const prototype = wrapper.dataset.prototype;
|
||||||
|
|
||||||
|
// Create a new row
|
||||||
|
const row = document.createElement('tr');
|
||||||
|
row.className = 'order-item-row';
|
||||||
|
|
||||||
|
// Create cells for drink type, quantity, and actions
|
||||||
|
const drinkTypeCell = document.createElement('td');
|
||||||
|
const quantityCell = document.createElement('td');
|
||||||
|
const actionsCell = document.createElement('td');
|
||||||
|
|
||||||
|
// Extract the form elements from the prototype
|
||||||
|
const tempDiv = document.createElement('div');
|
||||||
|
tempDiv.innerHTML = prototype.replace(/__name__/g, index);
|
||||||
|
|
||||||
|
// Find the drink type and quantity inputs
|
||||||
|
const drinkTypeInput = tempDiv.querySelector('[name$="[drinkType]"]');
|
||||||
|
const quantityInput = tempDiv.querySelector('[name$="[quantity]"]');
|
||||||
|
|
||||||
|
// Move the inputs to their respective cells
|
||||||
|
if (drinkTypeInput) {
|
||||||
|
drinkTypeCell.appendChild(drinkTypeInput.parentNode);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (quantityInput) {
|
||||||
|
quantityCell.appendChild(quantityInput.parentNode);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Create remove button
|
||||||
|
const removeButton = document.createElement('button');
|
||||||
|
removeButton.type = 'button';
|
||||||
|
removeButton.className = 'btn btn-danger remove-order-item';
|
||||||
|
removeButton.textContent = 'Remove';
|
||||||
|
actionsCell.appendChild(removeButton);
|
||||||
|
|
||||||
|
// Add cells to the row
|
||||||
|
row.appendChild(drinkTypeCell);
|
||||||
|
row.appendChild(quantityCell);
|
||||||
|
row.appendChild(actionsCell);
|
||||||
|
|
||||||
|
// Add the row to the table
|
||||||
|
document.querySelector('.order-items-list').appendChild(row);
|
||||||
|
|
||||||
|
// Update the index
|
||||||
|
wrapper.dataset.index = index + 1;
|
||||||
|
|
||||||
|
// Add event listener to the new remove button
|
||||||
|
removeButton.addEventListener('click', function() {
|
||||||
|
row.remove();
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// Remove existing items
|
||||||
|
document.querySelectorAll('.remove-order-item').forEach(function(button) {
|
||||||
|
button.addEventListener('click', function() {
|
||||||
|
// Find the row
|
||||||
|
const row = this.closest('.order-item-row');
|
||||||
|
|
||||||
|
// Add a hidden input to mark this item for deletion
|
||||||
|
const orderItemId = row.querySelector('input[name$="[id]"]');
|
||||||
|
if (orderItemId) {
|
||||||
|
const hiddenInput = document.createElement('input');
|
||||||
|
hiddenInput.type = 'hidden';
|
||||||
|
hiddenInput.name = orderItemId.name.replace('[id]', '[_delete]');
|
||||||
|
hiddenInput.value = 1;
|
||||||
|
row.appendChild(hiddenInput);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Hide the row instead of removing it
|
||||||
|
row.style.display = 'none';
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
|
@ -21,7 +21,7 @@
|
||||||
<td>{{ order.id }}</td>
|
<td>{{ order.id }}</td>
|
||||||
<td>{{ order.createdAt ? order.createdAt|date('Y-m-d H:i:s') : '' }}</td>
|
<td>{{ order.createdAt ? order.createdAt|date('Y-m-d H:i:s') : '' }}</td>
|
||||||
<td>{{ order.updatedAt ? order.updatedAt|date('Y-m-d H:i:s') : '' }}</td>
|
<td>{{ order.updatedAt ? order.updatedAt|date('Y-m-d H:i:s') : '' }}</td>
|
||||||
<td>{{ order.status }}</td>
|
<td>{{ order.status.value }}</td>
|
||||||
<td>
|
<td>
|
||||||
<a href="{{ path('app_order_show', {'id': order.id}) }}">show</a>
|
<a href="{{ path('app_order_show', {'id': order.id}) }}">show</a>
|
||||||
<a href="{{ path('app_order_edit', {'id': order.id}) }}">edit</a>
|
<a href="{{ path('app_order_edit', {'id': order.id}) }}">edit</a>
|
||||||
|
|
|
@ -21,14 +21,36 @@
|
||||||
</tr>
|
</tr>
|
||||||
<tr>
|
<tr>
|
||||||
<th>Status</th>
|
<th>Status</th>
|
||||||
<td>{{ order.status }}</td>
|
<td>{{ order.status.value }}</td>
|
||||||
</tr>
|
</tr>
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
|
|
||||||
<a href="{{ path('app_order_index') }}">back to list</a>
|
<h2>Order Items</h2>
|
||||||
|
{% if order.orderItems|length > 0 %}
|
||||||
|
<table class="table">
|
||||||
|
<thead>
|
||||||
|
<tr>
|
||||||
|
<th>Drink Type</th>
|
||||||
|
<th>Quantity</th>
|
||||||
|
</tr>
|
||||||
|
</thead>
|
||||||
|
<tbody>
|
||||||
|
{% for item in order.orderItems %}
|
||||||
|
<tr>
|
||||||
|
<td>{{ item.drinkType.name }}</td>
|
||||||
|
<td>{{ item.quantity }}</td>
|
||||||
|
</tr>
|
||||||
|
{% endfor %}
|
||||||
|
</tbody>
|
||||||
|
</table>
|
||||||
|
{% else %}
|
||||||
|
<p>No items in this order.</p>
|
||||||
|
{% endif %}
|
||||||
|
|
||||||
<a href="{{ path('app_order_edit', {'id': order.id}) }}">edit</a>
|
<a href="{{ path('app_order_index') }}" class="btn btn-secondary">Back to list</a>
|
||||||
|
|
||||||
|
<a href="{{ path('app_order_edit', {'id': order.id}) }}" class="btn btn-primary">Edit</a>
|
||||||
|
|
||||||
{{ include('order/_delete_form.html.twig') }}
|
{{ include('order/_delete_form.html.twig') }}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue