![]() Server : Apache System : Linux server2.corals.io 4.18.0-348.2.1.el8_5.x86_64 #1 SMP Mon Nov 15 09:17:08 EST 2021 x86_64 User : corals ( 1002) PHP Version : 7.4.33 Disable Function : exec,passthru,shell_exec,system Directory : /home/corals/old/vendor/extmag/shiplab/Cron/ |
<?php /** * Copyright © Extmag. All rights reserved. */ namespace Extmag\Shiplab\Cron; use Extmag\Shiplab\Helper\Manager; use Extmag\Shiplab\Helper\Track; use Extmag\Shiplab\Model\Label; use Extmag\Shiplab\Model\ResourceModel\Label\Collection; use Extmag\Shiplab\Model\ResourceModel\Label\CollectionFactory; use Extmag\Shiplab\Model\Source\CarrierCodes; use Exception; use Magento\Framework\Stdlib\DateTime\DateTime; use Psr\Log\LoggerInterface as Logs; class AutoGetTracking { /** * @var CollectionFactory */ protected $labelCollectionFactory; /** * @var Track */ protected $track; /** * @var Logs */ protected $log; /** * @var DateTime */ protected $dateTime; /** * @var Manager */ protected $manager; /** * @var array */ protected $allowedCarriers = []; /** * @var CarrierCodes */ protected $carrierCodes; /** * AutoCreatePickup constructor. * @param Track $track * @param CollectionFactory $labelCollectionFactory * @param Logs $log * @param DateTime $dateTime * @param Manager $manager * @param CarrierCodes $carrierCodes */ public function __construct( Track $track, CollectionFactory $labelCollectionFactory, Logs $log, DateTime $dateTime, Manager $manager, CarrierCodes $carrierCodes ) { $this->labelCollectionFactory = $labelCollectionFactory; $this->track = $track; $this->log = $log; $this->dateTime = $dateTime; $this->manager = $manager; $this->carrierCodes = $carrierCodes; } /** * @return void * @throws Exception */ public function execute() { $this->doesAllowedCarrier(array_keys($this->carrierCodes->toArray())); if (!empty($this->allowedCarriers)) { /** * @var Collection $labelCollection */ $labelCollection = $this->labelCollectionFactory->create(); $labelCollection->addFieldToFilter('status', 1); $labelCollection->addFieldToFilter('is_pickup', 1); $labelCollection->addFieldToFilter('type_direction', 'shipment'); $labelCollection->addFieldToFilter('track_status', ['neq' => 2]); $labelCollection->addFieldToFilter( 'track_first_time', [ 'gt' => $this->dateTime->date( "Y-m-d H:i:s", $this->dateTime->timestamp() - 86400 * 45 ), ] ); $labelCollection->addFieldToFilter( 'created_time', [ 'gt' => $this->dateTime->date( "Y-m-d H:i:s", $this->dateTime->timestamp() - 86400 * 120 ), ] ); $labelCollection->addFieldToFilter('carrier_code', ['in' => $this->allowedCarriers]); $labelCollection->setOrder('track_last_time', 'ASC'); $labelCollection->setPageSize(5); $labelCollection->setCurPage(1); if ($labelCollection->count() > 0) { $labelsByCarrier = []; /** * @var Label $label */ foreach ($labelCollection as $label) { $cronHour = $this->manager->getConfig($label->getCarrierCode() . '_track/schedule/time'); if ($label->getTrackLastTime() < $this ->dateTime ->date( "Y-m-d H:i:s", $this->dateTime->timestamp() - 60 * 60 * $cronHour ) ) { $labelsByCarrier[$label->getCarrierCode()][] = $label; } } if (!empty($labelsByCarrier)) { foreach ($labelsByCarrier as $carrierCode => $labels) { try { $result = $this->track->makeDataAndSaveTrack($carrierCode, $labels); //$this->log->info(json_encode($result)); } catch (Exception $e) { $this->log->info($e->getMessage()); $this->log->info($e->getTraceAsString()); } if (!empty($result['error'])) { $this->log->info('Tracking was not got for ' . strtoupper($carrierCode)); } } } } } } /** * @param $carrierCodes * @throws Exception */ protected function doesAllowedCarrier($carrierCodes) { if (!is_array($carrierCodes)) { $carrierCodes = [$carrierCodes]; } foreach ($carrierCodes as $carrierCode) { $daysOfWeek = explode(",", $this->manager->getConfig($carrierCode . '_track/schedule/days_of_week') ?? ''); if ($this->manager->getConfig($carrierCode . '_track/schedule/active') == 1 && !empty($this->manager->getConfig($carrierCode . '_track/schedule/time')) && in_array($this->dateTime->date("w"), $daysOfWeek) ) { $this->allowedCarriers[] = $carrierCode; } } } }