<?php
declare(strict_types=1);
namespace App\Controller;
use App\Entity\Order\OrderItem;
use App\Service\OrderPricesRecalculator;
use Sylius\Bundle\OrderBundle\Controller\OrderItemController as OrderItemControllerBase;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\KernelInterface;
class OrderItemController extends OrderItemControllerBase
{
public function getCart(KernelInterface $kernel){
$entityManager = $this->getDoctrine()->getManager();
$cart = $this->getContext()->getCart();
$items = $cart->getItems();
$data = [
'item_count' => $cart->getTotalQuantity(),
'total' => null,
'items' => []
];
$filesystem = new Filesystem();
$service_center_id = isset($_COOKIE['partnerId']) ? $_COOKIE['partnerId'] : null;
$priceDir = $kernel->getProjectDir().'/private/service_center/prices/';
$prices_file = $priceDir.$service_center_id.'.json';
$prices = [];
if ($filesystem->exists($prices_file)) {
$dataPrices = json_decode(file_get_contents($prices_file), true);
foreach ($dataPrices as $sku => $_data) {
$practicedPrice = isset($_data['practicedPriceTTC']) && !empty($_data['practicedPriceTTC']) ? $_data['practicedPriceTTC'] * 100 : null;
$vendorPrice = isset($_data['vendorPriceTTC']) && !empty($_data['vendorPriceTTC']) ? $_data['vendorPriceTTC'] * 100 : null;
$prices[$sku] = $practicedPrice !== null ? $practicedPrice : $vendorPrice;
}
}
/** @var OrderItem[] $items */
foreach ($items as $item){
$variantCode = $item->getVariant()->getCode();
// Find and update the ChannelPricing entity
$channelPricings = $item->getVariant()->getChannelPricings();
$channelPricing = $channelPricings->get('default');
// Check if a custom price exists for this variant
if (isset($prices[$variantCode])) {
$customPrice = $prices[$variantCode];
$item->setUnitPrice($customPrice);
$channelPricing->setPrice($customPrice);
} else {
$item->setUnitPrice($channelPricing->getOriginalPrice());
$channelPricing->setPrice($channelPricing->getOriginalPrice());
}
$entityManager->persist($channelPricing);
$entityManager->flush();
$product = $item->getProduct();
$data['items'][] = [
'id' => $item->getId(),
'quantity' => $item->getQuantity(),
'price' => $item->getUnitPrice(),
'quotation' => $product->getRequire_quotation(),
'in_stock' => $item->getVariant()->isInStock(),
'product_title' => $item->getProductName(),
'sku' => $variantCode
];
}
$data['total'] = $cart->getTotal();
$cartManager = $this->getCartManager();
$cartManager->persist($cart);
$cartManager->flush();
return $this->json($data);
}
public function clearCart(){
$cart = $this->getContext()->getCart();
$cart->clearItems();
$cartManager = $this->getCartManager();
$cartManager->persist($cart);
$cartManager->flush();
return $this->json([]);
}
public function refreshCart(OrderPricesRecalculator $orderPricesRecalculator, KernelInterface $kernel){
$cart = $this->getContext()->getCart();
$orderPricesRecalculator->process($cart);
$cartManager = $this->getCartManager();
$cartManager->persist($cart);
$cartManager->flush();
return $this->getCart($kernel);
}
public function updateCart(Request $request, KernelInterface $kernel){
$cart = $this->getContext()->getCart();
$data = json_decode($request->getContent(), true);
if (!isset($data['line']) || !isset($data['quantity'])) {
return new JsonResponse(['error' => 'Invalid data'], 400);
}
$itemId = (int)$data['line'];
$quantity = (int)$data['quantity'];
$orderItemQuantityModifier = $this->container->get('sylius.order_item_quantity_modifier');
$entityManager = $this->container->get('doctrine.orm.entity_manager');
$items = $cart->getItems();
if (!$items[$itemId]) {
throw new NotFoundHttpException('Cart item not found.');
}
$orderItemQuantityModifier->modify($items[$itemId], $quantity);
$entityManager->flush();
return $this->getCart($kernel);
}
}