Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix the issue with sending statistical data #32

Merged
merged 2 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 22 additions & 16 deletions Services/BusinessLogic/OrderService.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
use SeQura\Core\BusinessLogic\Domain\Order\Service\OrderService as SeQuraOrderService;
use Sequra\Core\Model\Api\Builders\CreateOrderRequestBuilderFactory;
use Sequra\Core\Services\BusinessLogic\Utility\SeQuraTranslationProvider;
use Magento\Sales\Model\ResourceModel\Order\CollectionFactory as OrderCollectionFactory;

/**
* Class OrderService
Expand All @@ -47,6 +48,10 @@ class OrderService implements ShopOrderService
* @var SearchCriteriaBuilder
*/
private $searchOrderCriteriaBuilder;
/**
* @var OrderCollectionFactory
*/
private $collectionFactory;
/**
* @var OrderRepositoryInterface
*/
Expand Down Expand Up @@ -77,17 +82,19 @@ class OrderService implements ShopOrderService
private $createOrderRequestBuilderFactory;

public function __construct(
SearchCriteriaBuilder $searchOrderCriteriaBuilder,
OrderRepositoryInterface $shopOrderRepository,
OrderManagementInterface $orderManagement,
CartManagementInterface $cartManagement,
SeQuraOrderRepositoryInterface $seQuraOrderRepository,
SeQuraTranslationProvider $translationProvider,
CartRepositoryInterface $cartProvider,
SearchCriteriaBuilder $searchOrderCriteriaBuilder,
OrderCollectionFactory $collectionFactory,
OrderRepositoryInterface $shopOrderRepository,
OrderManagementInterface $orderManagement,
CartManagementInterface $cartManagement,
SeQuraOrderRepositoryInterface $seQuraOrderRepository,
SeQuraTranslationProvider $translationProvider,
CartRepositoryInterface $cartProvider,
CreateOrderRequestBuilderFactory $createOrderRequestBuilderFactory
)
{
$this->searchOrderCriteriaBuilder = $searchOrderCriteriaBuilder;
$this->collectionFactory = $collectionFactory;
$this->shopOrderRepository = $shopOrderRepository;
$this->orderManagement = $orderManagement;
$this->cartManagement = $cartManagement;
Expand All @@ -114,19 +121,18 @@ public function getStatisticsOrderIds(int $page, int $limit = 5000): array
$fromDate = clone $toDate;
$fromDate->modify('-7 days');

$searchCriteria = $this->searchOrderCriteriaBuilder
->addFilter('created_at', $fromDate->format('Y-m-d H:i:s'), 'gteq')
->addFilter('created_at', $toDate->format('Y-m-d H:i:s'), 'lteq')
->setCurrentPage($page + 1)
->setPageSize($limit)
->create();
$collection = $this->collectionFactory
->create()
->setPage($page, $limit)
->addFieldToFilter('created_at', ['gteq' => $fromDate->format('Y-m-d H:i:s')])
->addFieldToFilter('created_at', ['lteq' => $toDate->format('Y-m-d H:i:s')]);

$orderList = $this->shopOrderRepository->getList($searchCriteria);
if ($page * $limit > $orderList->getTotalCount()) {
$count = $collection->getSize();
if ($page !== 1 && $page > round($count / $limit)) {
return [];
}

return array_column($orderList->getData(), 'entity_id');
return array_column($collection->getData(), 'entity_id');
}

/**
Expand Down
33 changes: 17 additions & 16 deletions Services/BusinessLogic/StatisticalDataService.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

namespace Sequra\Core\Services\BusinessLogic;

use SeQura\Core\BusinessLogic\Domain\SendReport\Models\SendReport;
use SeQura\Core\BusinessLogic\Domain\StatisticalData\Models\StatisticalData;
use SeQura\Core\BusinessLogic\Domain\StatisticalData\Services\StatisticalDataService as CoreStatisticalDataService;
use SeQura\Core\BusinessLogic\Domain\Stores\Services\StoreService;
use SeQura\Core\Infrastructure\ServiceRegister;
use SeQura\Core\Infrastructure\Utility\TimeProvider;

/**
* Class StatisticalDataService
Expand All @@ -16,33 +14,36 @@
class StatisticalDataService extends CoreStatisticalDataService
{
private const SCHEDULE_TIME = '4 am';
private const SCHEDULE_TIME_NEXT_DAY = '4 am +1 day';

/**
* @inheirtDoc
*/
public function saveStatisticalData(StatisticalData $statisticalData): void
{
$this->statisticalDataRepository->setStatisticalData($statisticalData);
}

/**
* @inheirtDoc
*/
public function getContextsForSendingReport(): array
{
if ($this->timeProvider->getCurrentLocalTime()->getTimestamp()
!== strtotime(self::SCHEDULE_TIME)) {
return [];
if (!$statisticalData->isSendStatisticalData()) {
return;
}

if ($this->timeProvider->getCurrentLocalTime()->getTimestamp() <= strtotime(self::SCHEDULE_TIME)) {
$sendReport = new SendReport(strtotime(self::SCHEDULE_TIME));
} else {
$sendReport = new SendReport(strtotime(self::SCHEDULE_TIME_NEXT_DAY));
}

return $this->getStoreService()->getConnectedStores();
$this->sendReportRepository->setSendReport(
$sendReport
);
}

/**
* @return StoreService
* @inheirtDoc
*/
private function getStoreService(): StoreService
public function setSendReportTime(): void
{
return ServiceRegister::getService(StoreService::class);
$time = strtotime(self::SCHEDULE_TIME_NEXT_DAY);
$this->sendReportRepository->setSendReport(new SendReport($time));
}
}
Loading