Added cli command to update since id to the latest value

This commit is contained in:
max-baranikov 2021-04-29 18:24:20 +03:00 committed by GitHub
parent ad9ce38131
commit 1c86091b56
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 191 additions and 50 deletions

View File

@ -374,6 +374,7 @@ class RetailcrmCli
'RetailcrmIcmlEvent',
'RetailcrmSyncEvent',
'RetailcrmInventoriesEvent',
'RetailcrmUpdateSinceIdEvent',
'RetailcrmExportEvent'
);
}

View File

@ -1,4 +1,5 @@
<?php
/**
* MIT License
*
@ -1701,5 +1702,62 @@ class RetailcrmHistory
if (isset($upOrderItems['items']))
self::$api->ordersEdit($upOrderItems);
}
/**
* Updates sinceId for orders or customers to the latest value
*
* @param string $entity Can be either 'orders' or 'customers'
* @return bool
*/
public static function updateSinceId($entity)
{
if ($entity === 'orders') {
$key = 'RETAILCRM_LAST_ORDERS_SYNC';
$method = 'ordersHistory';
} elseif ($entity === 'customers') {
$key = 'RETAILCRM_LAST_CUSTOMERS_SYNC';
$method = 'customersHistory';
} else {
return false;
}
$currentSinceID = Configuration::get($key);
RetailcrmLogger::writeDebug(__METHOD__, "Current $entity sinceId: $currentSinceID");
$historyResponse = call_user_func_array(
array(self::$api, $method),
array(
array('sinceId' => $currentSinceID),
null,
20
)
);
if ($historyResponse instanceof RetailcrmApiResponse && $historyResponse->offsetExists('pagination')) {
$lastPage = $historyResponse['pagination']['totalPageCount'];
if ($lastPage > 1) {
$historyResponse = call_user_func_array(
array(self::$api, $method),
array(
array('sinceId' => $currentSinceID),
$lastPage,
20
)
);
}
if ($historyResponse instanceof RetailcrmApiResponse && $historyResponse->offsetExists('history') && !empty($historyResponse['history'])) {
$history = $historyResponse['history'];
$lastSinceId = end($history)['id'];
if ($currentSinceID !== strval($lastSinceId)) {
RetailcrmLogger::writeDebug(__METHOD__, "Updating to: $lastSinceId");
Configuration::updateValue($key, $lastSinceId);
}
}
}
return true;
}
}

View File

@ -0,0 +1,82 @@
<?php
/**
* MIT License
*
* Copyright (c) 2020 DIGITAL RETAIL TECHNOLOGIES SL
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* 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 DIGITAL RETAIL TECHNOLOGIES SL <mail@simlachat.com>
* @copyright 2020 DIGITAL RETAIL TECHNOLOGIES SL
* @license https://opensource.org/licenses/MIT The MIT License
*
* Don't forget to prefix your containers with your own identifier
* to avoid any conflicts with others containers.
*/
require_once(dirname(__FILE__) . '/../RetailcrmPrestashopLoader.php');
class RetailcrmUpdateSinceIdEvent extends RetailcrmAbstractEvent implements RetailcrmEventInterface
{
/**
* @inheritDoc
*/
public function execute()
{
if ($this->isRunning()) {
return false;
}
$this->setRunning();
$shops = $this->getShops();
foreach ($shops as $shop) {
RetailcrmTools::setShopContext(intval($shop['id_shop']));
$api = RetailcrmTools::getApiClient();
if (empty($api)) {
RetailcrmLogger::writeCaller(__METHOD__, 'Set api key & url first');
continue;
}
RetailcrmHistory::$api = $api;
RetailcrmHistory::updateSinceId('customers');
RetailcrmHistory::updateSinceId('orders');
}
return true;
}
/**
* @inheritDoc
*/
public function getName()
{
return 'RetailcrmUpdateSinceIdEvent';
}
}