| 1 |
<?php |
| 2 |
/** |
| 3 |
* Report service interface |
| 4 |
* |
| 5 |
* @package SeQura/WC |
| 6 |
* @subpackage SeQura/WC/Services |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace SeQura\WC\Services\Report; |
| 10 |
|
| 11 |
use SeQura\Core\BusinessLogic\Domain\CountryConfiguration\RepositoryContracts\CountryConfigurationRepositoryInterface; |
| 12 |
use SeQura\Core\BusinessLogic\Domain\Multistore\StoreContext; |
| 13 |
use SeQura\Core\BusinessLogic\Domain\OrderReport\Tasks\OrderReportTask; |
| 14 |
use SeQura\Core\BusinessLogic\Domain\StatisticalData\RepositoryContracts\StatisticalDataRepositoryInterface; |
| 15 |
use SeQura\Core\BusinessLogic\Domain\Stores\Services\StoreService; |
| 16 |
use SeQura\Core\BusinessLogic\Webhook\Services\ShopOrderService; |
| 17 |
use SeQura\WC\Core\Extension\Infrastructure\Configuration\Configuration; |
| 18 |
use SeQura\WC\Services\Order\Interface_Order_Service; |
| 19 |
use WC_Order; |
| 20 |
|
| 21 |
/** |
| 22 |
* Report service |
| 23 |
*/ |
| 24 |
class Report_Service implements Interface_Report_Service { |
| 25 |
|
| 26 |
/** |
| 27 |
* Configuration |
| 28 |
* |
| 29 |
* @var Configuration |
| 30 |
*/ |
| 31 |
private $configuration; |
| 32 |
|
| 33 |
/** |
| 34 |
* Store service |
| 35 |
* |
| 36 |
* @var StoreService |
| 37 |
*/ |
| 38 |
private $store_service; |
| 39 |
|
| 40 |
/** |
| 41 |
* Shop order service |
| 42 |
* |
| 43 |
* @var ShopOrderService |
| 44 |
*/ |
| 45 |
private $shop_order_service; |
| 46 |
|
| 47 |
/** |
| 48 |
* Statistical data repository |
| 49 |
* |
| 50 |
* @var StatisticalDataRepositoryInterface |
| 51 |
*/ |
| 52 |
private $statistical_data_repository; |
| 53 |
|
| 54 |
/** |
| 55 |
* Country configuration repository |
| 56 |
* |
| 57 |
* @var CountryConfigurationRepositoryInterface |
| 58 |
*/ |
| 59 |
private $country_configuration_repository; |
| 60 |
|
| 61 |
/** |
| 62 |
* Order service |
| 63 |
* |
| 64 |
* @var Interface_Order_Service |
| 65 |
*/ |
| 66 |
private $order_service; |
| 67 |
|
| 68 |
/** |
| 69 |
* Store context |
| 70 |
* |
| 71 |
* @var StoreContext |
| 72 |
*/ |
| 73 |
private $store_context; |
| 74 |
|
| 75 |
/** |
| 76 |
* Constructor. |
| 77 |
*/ |
| 78 |
public function __construct( |
| 79 |
Configuration $configuration, |
| 80 |
StoreService $store_service, |
| 81 |
ShopOrderService $shop_order_service, |
| 82 |
StatisticalDataRepositoryInterface $statistical_data_repository, |
| 83 |
CountryConfigurationRepositoryInterface $country_configuration_repository, |
| 84 |
Interface_Order_Service $order_service, |
| 85 |
StoreContext $store_context |
| 86 |
) { |
| 87 |
$this->configuration = $configuration; |
| 88 |
$this->store_service = $store_service; |
| 89 |
$this->shop_order_service = $shop_order_service; |
| 90 |
$this->statistical_data_repository = $statistical_data_repository; |
| 91 |
$this->country_configuration_repository = $country_configuration_repository; |
| 92 |
$this->order_service = $order_service; |
| 93 |
$this->store_context = $store_context; |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* Get store id for current context |
| 98 |
* |
| 99 |
* @throws Throwable |
| 100 |
*/ |
| 101 |
public function send_delivery_report_for_current_store(): void { |
| 102 |
$store_id = $this->configuration->get_store_id(); |
| 103 |
$this->store_context::doWithStore( $store_id, array( $this, 'send_delivery_report' ) ); |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
/** |
| 108 |
* Send the delivery report |
| 109 |
* |
| 110 |
* @throws Throwable |
| 111 |
*/ |
| 112 |
public function send_delivery_report(): void { |
| 113 |
|
| 114 |
$report_order_ids = array_map( 'strval', $this->shop_order_service->getReportOrderIds( 0, -1 ) ); |
| 115 |
$statistics_order_ids = null; |
| 116 |
$statistical_data = $this->statistical_data_repository->getStatisticalData(); |
| 117 |
if ( $statistical_data && $statistical_data->isSendStatisticalData() ) { |
| 118 |
$statistics_order_ids = array_map( 'strval', $this->shop_order_service->getStatisticsOrderIds( 0, -1 ) ); |
| 119 |
} |
| 120 |
|
| 121 |
$merchantId = null; |
| 122 |
$countryConfiguration = $this->country_configuration_repository->getCountryConfiguration(); |
| 123 |
if ( isset( $countryConfiguration[0] ) ) { |
| 124 |
$merchantId = $countryConfiguration[0]->getMerchantId(); |
| 125 |
} |
| 126 |
|
| 127 |
if ( ( empty( $report_order_ids ) && empty( $statistics_order_ids ) ) || ! $merchantId ) { |
| 128 |
return; |
| 129 |
} |
| 130 |
|
| 131 |
$task = new OrderReportTask( $merchantId, $report_order_ids, $statistics_order_ids ); |
| 132 |
$task->execute(); |
| 133 |
|
| 134 |
// Mark orders as reported. |
| 135 |
foreach ( $report_order_ids as $order_id ) { |
| 136 |
$order = \wc_get_order( (int) $order_id ); |
| 137 |
if ( ! $order instanceof WC_Order ) { |
| 138 |
continue; |
| 139 |
} |
| 140 |
$this->order_service->set_as_sent_to_sequra( $order ); |
| 141 |
} |
| 142 |
} |
| 143 |
} |
| 144 |
|