forked from sameday-courier/prestashop-plugin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsync.php
97 lines (82 loc) · 3.08 KB
/
sync.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
/**
* 2007-2020 PrestaShop
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
* @author PrestaShop SA <[email protected]>
* @copyright 2007-2020 PrestaShop SA
* @license http://addons.prestashop.com/en/content/12-terms-and-conditions-of-use
* International Registered Trademark & Property of PrestaShop SA
*/
include(dirname(__FILE__) . '/libs/sameday-php-sdk/src/Sameday/autoload.php');
include(dirname(__FILE__).'/../../config/config.inc.php');
include(dirname(__FILE__).'/../../init.php');
include(dirname(__FILE__) . '/classes/SamedayAwbParcel.php');
include(dirname(__FILE__) . '/classes/SamedayConstants.php');
include(dirname(__FILE__) . '/classes/SamedayPersistenceDataHandler.php');
if (Tools::substr(Tools::encrypt(Configuration::get('SAMEDAY_CRON_TOKEN')), 0, 10) != Tools::getValue('token') ||
!Module::isInstalled('samedaycourier')
) {
die('Bad token');
}
$now = new DateTime();
$lastSync = (int)Configuration::get('SAMEDAY_LAST_SYNC', 0);
if (!$lastSync) {
$lastSync = $now->getTimestamp() - 7200;
}
$endTimestamp = $lastSync + 7200;
$country = (Configuration::get('SAMEDAY_HOST_COUNTRY')) ? Configuration::get('SAMEDAY_HOST_COUNTRY') : 'ro';
$testingMode = (Configuration::get('SAMEDAY_LIVE_MODE')) ? Configuration::get('SAMEDAY_LIVE_MODE') : '0';
$api = $testingMode ? SamedayConstants::SAMEDAY_ENVS[$country]['API_URL_PROD'] : SamedayConstants::SAMEDAY_ENVS[$country]['API_URL_DEMO'];
$logger = new FileLogger(0);
$logger->setFilename(dirname(__FILE__) . '/log/' . date('Ymd') . '_sameday_sync.log');
$logger->log('Start sync', 0);
try {
$sameday = new \Sameday\Sameday(
new \Sameday\SamedayClient(
Configuration::get('SAMEDAY_ACCOUNT_USER'),
Configuration::get('SAMEDAY_ACCOUNT_PASSWORD'),
$api,
'Prestashop',
_PS_VERSION_,
'curl',
new SamedayPersistenceDataHandler()
)
);
} catch (Exception $exception) {
$logger->log($exception->getMessage(), 3);
}
$request = new \Sameday\Requests\SamedayGetStatusSyncRequest($lastSync, $endTimestamp);
$statuses = array();
$page = 1;
while (true) {
$request->setPage($page++);
try {
$sync = $sameday->getStatusSync($request);
if (!$sync->getStatuses()) {
//no more statuses
break;
}
/** @var \Sameday\Objects\StatusSync\StatusObject $status */
foreach ($sync->getStatuses() as $status) {
$statuses[$status->getParcelAwbNumber()][] = $status;
}
} catch (\Exception $e) {
$logger->log($e->getMessage(), 3);
break;
}
}
foreach ($statuses as $awb => $statusSync) {
$parcel = SamedayAwbParcel::findByAwbNumber($awb);
if ($parcel) {
SamedayAwbParcel::updateStatusSync($parcel['id'], $status);
}
}
Configuration::updateValue('SAMEDAY_LAST_SYNC', $endTimestamp);
$logger->log('End sync', 0);
die('It works!');