93 lines
2.8 KiB
PHP
93 lines
2.8 KiB
PHP
<?php
|
|
|
|
require_once plugin_dir_path(__FILE__) . 'PolkurierAPI.php';
|
|
|
|
|
|
/**
|
|
* Class Polkurier_OrderStatusUpdater
|
|
*/
|
|
class PolkurierOrderStatusUpdater
|
|
{
|
|
|
|
/**
|
|
* Co ile sekund sprawdzać status
|
|
* @var int
|
|
*/
|
|
private static $UPDATE_STATUS_INTERVAL = 1440; // 4H
|
|
|
|
/**
|
|
* Przez ile dni po utworzeniu zamówienia sprawdzać status
|
|
* @var int
|
|
*/
|
|
private static $UPDATE_STATUS_DAYS = 21;
|
|
|
|
/**
|
|
* @var \Polkurier\ApiClient
|
|
*/
|
|
private $api;
|
|
|
|
|
|
public function __construct()
|
|
{
|
|
$this->api = new \Polkurier\ApiClient(get_option('polkurier_account_id'), get_option('polkurier_apikey'));
|
|
}
|
|
|
|
|
|
/**
|
|
* @param PolkurierOrder $polkurierOrder
|
|
* @return bool
|
|
* @throws Exception
|
|
*/
|
|
public function shouldUpdateOrderStatus($polkurierOrder)
|
|
{
|
|
$lastStatusUpdateTime = (int)$polkurierOrder->lastStatusUpdateTime;
|
|
$dateTo = (clone $polkurierOrder->getDateCreated());
|
|
$dateTo->add(new DateInterval('P' . self::$UPDATE_STATUS_DAYS . 'D'));
|
|
return
|
|
!empty($polkurierOrder['order_number']) &&
|
|
$lastStatusUpdateTime + self::$UPDATE_STATUS_INTERVAL < time() &&
|
|
$dateTo >= new DateTime() &&
|
|
in_array((string)$polkurierOrder->status_code, array('', 'O', 'OC', 'P', 'WP', 'PZ', 'Z'))
|
|
;
|
|
}
|
|
|
|
|
|
/**
|
|
* @param PolkurierOrder $polkurierOrder
|
|
* @throws \Polkurier\Exception\ApiException
|
|
*/
|
|
public function updateOrderStatus($polkurierOrder)
|
|
{
|
|
if (!empty($polkurierOrder['order_number'])) {
|
|
try {
|
|
$polkurierStatus = $this->api->getStatus($polkurierOrder['order_number']);
|
|
if (!empty($polkurierStatus)) {
|
|
$polkurierOrder->status = $polkurierStatus['status'];
|
|
$polkurierOrder->status_code = $polkurierStatus['status_code'];
|
|
$polkurierOrder->status_date = isset($polkurierStatus['status_date']) ? $polkurierStatus['status_date'] : null;
|
|
$polkurierOrder->delivered_date = $polkurierOrder->params['status_code'] === 'D' ? $polkurierStatus['delivered_date'] : '';
|
|
$polkurierOrder->lastStatusUpdateSuccess = true;
|
|
} else {
|
|
$polkurierOrder->lastStatusUpdateSuccess = false;
|
|
}
|
|
$polkurierOrder->lastStatusUpdateTime = time();
|
|
$polkurierOrder->save();
|
|
} catch (Exception $e) {
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
/**
|
|
* Aktualizuje wszystkie statusy
|
|
*/
|
|
public function updateAllOrdersStatuses()
|
|
{
|
|
$orders = PolkurierOrder::getAll(["status_code IN ('', 'O', 'OC', 'P', 'WP', 'PZ', 'Z')" => null]);
|
|
foreach ($orders as $order) {
|
|
$this->updateOrderStatus($order);
|
|
}
|
|
}
|
|
|
|
}
|