|
Apache/2.4.41 (Ubuntu) Linux vmi616275.contaboserver.net 5.4.0-84-generic #94-Ubuntu SMP Thu Aug 26 20:27:37 UTC 2021 x86_64 uid=33(www-data) gid=33(www-data) groups=33(www-data) server ip : 62.171.164.128 | your ip : 127.0.0.1 safemode OFF > / home / dev2.destoffenstraat.com / app / code / Amasty / CheckoutDeliveryDate / Model / |
Filename | /home/dev2.destoffenstraat.com/app/code/Amasty/CheckoutDeliveryDate/Model/DeliveryDateStatistic.php |
Size | 3.05 kb |
Permission | rwxrwxrwx |
Owner | root : root |
Create time | 17-Aug-2025 10:26 |
Last modified | 23-Nov-2022 12:33 |
Last accessed | 23-Aug-2025 01:52 |
Actions | edit | rename | delete | download (gzip) |
View | text | code | image |
<?php
declare(strict_types=1);
/**
* @author Amasty Team
* @copyright Copyright (c) 2022 Amasty (https://www.amasty.com)
* @package One Step Checkout Delivery Date for Magento 2 (System)
*/
namespace Amasty\CheckoutDeliveryDate\Model;
use Amasty\CheckoutCore\Api\DeliveryDateStatisticInterface;
use Amasty\CheckoutDeliveryDate\Model\ResourceModel\Delivery\Collection;
use Amasty\CheckoutDeliveryDate\Model\ResourceModel\Delivery\CollectionFactory;
class DeliveryDateStatistic implements DeliveryDateStatisticInterface
{
public const DELIVERY_INFO = [
'date' => 'Delivery Date',
'time' => 'Delivery Time',
'comment' => 'Delivery Comment',
];
public const DELIVERY_KEY = 'delivery';
public const DELIVERY_COUNT_KEY = 'delivery_total_count';
/**
* @var ConfigProvider
*/
private $configProvider;
/**
* @var CollectionFactory
*/
private $deliveryCollectionFactory;
public function __construct(ConfigProvider $configProvider, CollectionFactory $deliveryCollectionFactory)
{
$this->configProvider = $configProvider;
$this->deliveryCollectionFactory = $deliveryCollectionFactory;
}
/**
* @param array $quoteIds
* @param int $quoteTotalCount
* @return array
*/
public function collect(array $quoteIds = [], int $quoteTotalCount = 1): array
{
$conditions = [];
$statisticData[self::DELIVERY_KEY] = [];
$statisticData[self::DELIVERY_COUNT_KEY] = [];
$isDeliveryRequired = $this->configProvider->isDeliveryDateRequired();
/** @var Collection $delivery */
$delivery = $this->deliveryCollectionFactory->create();
$delivery->addSizeSelectByQuoteIds($quoteIds);
$totalCount = clone $delivery;
foreach (self::DELIVERY_INFO as $code => $label) {
$clone = clone $delivery;
$clone->addFieldToFilter($code, ['notnull' => true]);
$size = (int)$clone->fetchItem()->getSize();
$statisticData[self::DELIVERY_KEY][] = [
'size' => $size,
'label' => __($label)->getText(),
'rate' => $this->getRate($size, $quoteTotalCount)
];
if (!$isDeliveryRequired) {
$conditions[] = ['notnull' => true];
}
}
if ($isDeliveryRequired) {
$totalCount->addFieldToFilter('date', ['notnull' => true]);
} else {
$totalCount->addFieldToFilter(array_keys(self::DELIVERY_INFO), $conditions);
}
$statisticData[self::DELIVERY_COUNT_KEY] = $totalCount->getSize();
return [
self::DELIVERY_KEY => $statisticData[self::DELIVERY_KEY],
self::DELIVERY_COUNT_KEY => $statisticData[self::DELIVERY_COUNT_KEY]
];
}
/**
* @param int $size
* @param int $quoteTotalCount
* @return float
*/
private function getRate(int $size, int $quoteTotalCount)
{
if (!$size) {
return 0;
}
return round(($size / $quoteTotalCount) * 100, 2);
}
}
declare(strict_types=1);
/**
* @author Amasty Team
* @copyright Copyright (c) 2022 Amasty (https://www.amasty.com)
* @package One Step Checkout Delivery Date for Magento 2 (System)
*/
namespace Amasty\CheckoutDeliveryDate\Model;
use Amasty\CheckoutCore\Api\DeliveryDateStatisticInterface;
use Amasty\CheckoutDeliveryDate\Model\ResourceModel\Delivery\Collection;
use Amasty\CheckoutDeliveryDate\Model\ResourceModel\Delivery\CollectionFactory;
class DeliveryDateStatistic implements DeliveryDateStatisticInterface
{
public const DELIVERY_INFO = [
'date' => 'Delivery Date',
'time' => 'Delivery Time',
'comment' => 'Delivery Comment',
];
public const DELIVERY_KEY = 'delivery';
public const DELIVERY_COUNT_KEY = 'delivery_total_count';
/**
* @var ConfigProvider
*/
private $configProvider;
/**
* @var CollectionFactory
*/
private $deliveryCollectionFactory;
public function __construct(ConfigProvider $configProvider, CollectionFactory $deliveryCollectionFactory)
{
$this->configProvider = $configProvider;
$this->deliveryCollectionFactory = $deliveryCollectionFactory;
}
/**
* @param array $quoteIds
* @param int $quoteTotalCount
* @return array
*/
public function collect(array $quoteIds = [], int $quoteTotalCount = 1): array
{
$conditions = [];
$statisticData[self::DELIVERY_KEY] = [];
$statisticData[self::DELIVERY_COUNT_KEY] = [];
$isDeliveryRequired = $this->configProvider->isDeliveryDateRequired();
/** @var Collection $delivery */
$delivery = $this->deliveryCollectionFactory->create();
$delivery->addSizeSelectByQuoteIds($quoteIds);
$totalCount = clone $delivery;
foreach (self::DELIVERY_INFO as $code => $label) {
$clone = clone $delivery;
$clone->addFieldToFilter($code, ['notnull' => true]);
$size = (int)$clone->fetchItem()->getSize();
$statisticData[self::DELIVERY_KEY][] = [
'size' => $size,
'label' => __($label)->getText(),
'rate' => $this->getRate($size, $quoteTotalCount)
];
if (!$isDeliveryRequired) {
$conditions[] = ['notnull' => true];
}
}
if ($isDeliveryRequired) {
$totalCount->addFieldToFilter('date', ['notnull' => true]);
} else {
$totalCount->addFieldToFilter(array_keys(self::DELIVERY_INFO), $conditions);
}
$statisticData[self::DELIVERY_COUNT_KEY] = $totalCount->getSize();
return [
self::DELIVERY_KEY => $statisticData[self::DELIVERY_KEY],
self::DELIVERY_COUNT_KEY => $statisticData[self::DELIVERY_COUNT_KEY]
];
}
/**
* @param int $size
* @param int $quoteTotalCount
* @return float
*/
private function getRate(int $size, int $quoteTotalCount)
{
if (!$size) {
return 0;
}
return round(($size / $quoteTotalCount) * 100, 2);
}
}