Files
2025-08-02 16:30:27 +02:00

58 lines
1.9 KiB
PHP

<?php
namespace External\CykloSpecialityBundle\Util;
use KupShop\CatalogBundle\ProductList\ProductCollection;
use KupShop\KupShopBundle\Util\DateUtil;
use KupShop\StoresBundle\Utils\StoresInStore;
use Symfony\Contracts\Service\Attribute\Required;
class DeliveryInfo extends \KupShop\OrderingBundle\Util\Order\DeliveryInfo
{
private const DEFAULT_DELIVERY_TIME = 0;
#[Required]
public StoresInStore $storesInStore;
protected function calculateCollectionDate(ProductCollection $productList, \Delivery $delivery, array &$cache): ?\DateTime
{
if ($delivery->isInPerson()) {
return parent::calculateCollectionDate($productList, $delivery, $cache);
}
$deliveryTime = 0;
foreach ($productList as $product) {
$productDeliveryTime = $this->getProductDeliveryTime($product);
$deliveryTime = max($deliveryTime, $productDeliveryTime);
}
return DateUtil::calcWorkingDays($deliveryTime);
}
private function getProductDeliveryTime(\Product $product): int
{
$piecesOrdered = $product['piecesOrdered'] ?? 1;
// nactu sklady ktere maji pozadovane mnozstvi skladem
$stores = $this->storesInStore->getProductInStores($product->id, $product->variationId ?? null);
$storesWithSufficientStock = array_filter($stores, fn ($x) => $x >= $piecesOrdered);
// nactu data fyzickejch skladu
$relevantStoresData = array_filter(
array_intersect_key($this->storesInStore->getStores(), $storesWithSufficientStock),
fn ($x) => $x['type'] === 0
);
// vyberu nejnizsi deliveryTime
$deliveryTimes = array_map(function ($storeData) {
return empty($storeData['data']['deliveryTime']) ? self::DEFAULT_DELIVERY_TIME : $storeData['data']['deliveryTime'];
}, $relevantStoresData);
if (empty($deliveryTimes)) {
return 0;
}
return min($deliveryTimes);
}
}