| 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\OrderReport\Tasks\OrderReportTask; |
| 13 |
use SeQura\Core\BusinessLogic\Domain\StatisticalData\RepositoryContracts\StatisticalDataRepositoryInterface; |
| 14 |
use SeQura\Core\BusinessLogic\Webhook\Services\ShopOrderService; |
| 15 |
use SeQura\WC\Services\Order\Interface_Order_Service; |
| 16 |
use Throwable; |
| 17 |
use WC_Order; |
| 18 |
|
| 19 |
/** |
| 20 |
* Report service |
| 21 |
*/ |
| 22 |
class Report_Service implements Interface_Report_Service { |
| 23 |
|
| 24 |
/** |
| 25 |
* Shop order service |
| 26 |
* |
| 27 |
* @var ShopOrderService |
| 28 |
*/ |
| 29 |
private $shop_order_service; |
| 30 |
|
| 31 |
/** |
| 32 |
* Statistical data repository |
| 33 |
* |
| 34 |
* @var StatisticalDataRepositoryInterface |
| 35 |
*/ |
| 36 |
private $statistical_data_repository; |
| 37 |
|
| 38 |
/** |
| 39 |
* Country configuration repository |
| 40 |
* |
| 41 |
* @var CountryConfigurationRepositoryInterface |
| 42 |
*/ |
| 43 |
private $country_configuration_repository; |
| 44 |
|
| 45 |
/** |
| 46 |
* Order service |
| 47 |
* |
| 48 |
* @var Interface_Order_Service |
| 49 |
*/ |
| 50 |
private $order_service; |
| 51 |
|
| 52 |
/** |
| 53 |
* Constructor. |
| 54 |
*/ |
| 55 |
public function __construct( |
| 56 |
ShopOrderService $shop_order_service, |
| 57 |
StatisticalDataRepositoryInterface $statistical_data_repository, |
| 58 |
CountryConfigurationRepositoryInterface $country_configuration_repository, |
| 59 |
Interface_Order_Service $order_service |
| 60 |
) { |
| 61 |
$this->shop_order_service = $shop_order_service; |
| 62 |
$this->statistical_data_repository = $statistical_data_repository; |
| 63 |
$this->country_configuration_repository = $country_configuration_repository; |
| 64 |
$this->order_service = $order_service; |
| 65 |
} |
| 66 |
|
| 67 |
/** |
| 68 |
* Get store id for current context |
| 69 |
* |
| 70 |
* @throws Throwable |
| 71 |
*/ |
| 72 |
public function send_delivery_report_for_current_store(): void { |
| 73 |
$this->send_delivery_report(); |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
/** |
| 78 |
* Send the delivery report |
| 79 |
* |
| 80 |
* @throws Throwable |
| 81 |
*/ |
| 82 |
public function send_delivery_report(): void { |
| 83 |
|
| 84 |
$report_order_ids = array_map( 'strval', $this->shop_order_service->getReportOrderIds( 0, -1 ) ); |
| 85 |
$statistics_order_ids = null; |
| 86 |
$statistical_data = $this->statistical_data_repository->getStatisticalData(); |
| 87 |
if ( $statistical_data && $statistical_data->isSendStatisticalData() ) { |
| 88 |
$statistics_order_ids = array_map( 'strval', $this->shop_order_service->getStatisticsOrderIds( 0, -1 ) ); |
| 89 |
} |
| 90 |
|
| 91 |
$merchantId = null; |
| 92 |
$countryConfiguration = $this->country_configuration_repository->getCountryConfiguration(); |
| 93 |
if ( isset( $countryConfiguration[0] ) ) { |
| 94 |
$merchantId = $countryConfiguration[0]->getMerchantId(); |
| 95 |
} |
| 96 |
|
| 97 |
if ( ( empty( $report_order_ids ) && empty( $statistics_order_ids ) ) || ! $merchantId ) { |
| 98 |
return; |
| 99 |
} |
| 100 |
|
| 101 |
$task = new OrderReportTask( $merchantId, $report_order_ids, $statistics_order_ids ); |
| 102 |
$task->execute(); |
| 103 |
|
| 104 |
// Mark orders as reported. |
| 105 |
foreach ( $report_order_ids as $order_id ) { |
| 106 |
$order = \wc_get_order( (int) $order_id ); |
| 107 |
if ( ! $order instanceof WC_Order ) { |
| 108 |
continue; |
| 109 |
} |
| 110 |
$this->order_service->set_as_sent_to_sequra( $order ); |
| 111 |
} |
| 112 |
} |
| 113 |
} |
| 114 |
|