From b64bb2599cf01ef356fe29388ff0797c73bf8752 Mon Sep 17 00:00:00 2001 From: Sergey Date: Mon, 21 Jan 2019 14:42:19 +0300 Subject: [PATCH] v2.2.9 --- CHANGELOG.md | 2 + README.md | 8 ++ README.ru.md | 8 ++ retailcrm/job/inventories.php | 5 +- retailcrm/lib/RetailcrmInventories.php | 47 ++++---- tests/phpunit/RetailcrmInventoriesTest.php | 123 ++++++++++++--------- 6 files changed, 108 insertions(+), 85 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3c09de5..f5bf55a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ ## v2.2.9 * Добавлена выгрузка габаритов в специальный тег dimensions +* Добавлена выгрузка остатков из CRM +* Добавлена передача номера заказа ## v2.2.8 * Добавлена выгрузка картинок категорий товаров в ICML diff --git a/README.md b/README.md index 60b891f..885e710 100644 --- a/README.md +++ b/README.md @@ -34,6 +34,14 @@ Add to cron: */7 * * * * /usr/bin/php /path/to/your/site/modules/retailcrm/job/sync.php ``` +#### Receiving balances from retailCRM + +Add to cron: + +``` +*/15 * * * * /usr/bin/php /path/to/your/site/modules/retailcrm/job/sync.php +``` + #### Single orders archive export to retailCRM ``` diff --git a/README.ru.md b/README.ru.md index d7a5e80..92904fc 100644 --- a/README.ru.md +++ b/README.ru.md @@ -29,6 +29,14 @@ Prestashop module */7 * * * * /usr/bin/php /path/to/your/site/modules/retailcrm/job/sync.php ``` +#### Получение остатков из retailCRM + +Добавьте в крон запись вида + +``` +*/15 * * * * /usr/bin/php /path/to/your/site/modules/retailcrm/job/sync.php +``` + #### Единоразовая выгрузка архива клиентов и заказов в retailCRM ``` diff --git a/retailcrm/job/inventories.php b/retailcrm/job/inventories.php index 05c8b74..04a992b 100644 --- a/retailcrm/job/inventories.php +++ b/retailcrm/job/inventories.php @@ -3,7 +3,7 @@ * @author Retail Driver LCC * @copyright RetailCRM * @license GPL - * @version 2.2.0 + * @version 2.2.9 * @link https://retailcrm.ru * */ @@ -18,7 +18,6 @@ $apiUrl = Configuration::get('RETAILCRM_ADDRESS'); $apiKey = Configuration::get('RETAILCRM_API_TOKEN'); $apiVersion = Configuration::get('RETAILCRM_API_VERSION'); - if (!empty($apiUrl) && !empty($apiKey)) { RetailcrmInventories::$api = new RetailcrmProxy($apiUrl, $apiKey, _PS_ROOT_DIR_ . '/retailcrm.log', $apiVersion); } else { @@ -26,4 +25,4 @@ if (!empty($apiUrl) && !empty($apiKey)) { exit(); } -RetailcrmInventories::load_stocks(); +RetailcrmInventories::loadStocks(); diff --git a/retailcrm/lib/RetailcrmInventories.php b/retailcrm/lib/RetailcrmInventories.php index eaa8bbc..a8c2486 100644 --- a/retailcrm/lib/RetailcrmInventories.php +++ b/retailcrm/lib/RetailcrmInventories.php @@ -1,53 +1,44 @@ storeInventories(array(), $page, 250); - if (!$result->isSuccessful()) { - return null; + if ($result === false) { + return $result; + } + + foreach ($result['offers'] as $offer) { + self::setQuantityOffer($offer); } $totalPageCount = $result['pagination']['totalPageCount']; $page++; - foreach ($result['offers'] as $offer) { - if (isset($offer['externalId'])) { - $invOffer = explode('#', $offer['externalId']); - - if (isset($invOffer[1])) { - StockAvailable::setQuantity($invOffer[0], $invOffer[1], $offer['quantity']); - } else { - StockAvailable::setQuantity($offer['externalId'], 0, $offer['quantity']); - } - } - } } while ($page <= $totalPageCount); - - return $success; } - /** - * Update stock quantity in WooCommerce - * - * @return mixed - */ - public function updateQuantity() { + private static function setQuantityOffer($offer) + { + if (isset($offer['externalId'])) { + $invOffer = explode('#', $offer['externalId']); - return $this->load_stocks(); - + if (isset($invOffer[1])) { + StockAvailable::setQuantity($invOffer[0], $invOffer[1], $offer['quantity']); + } else { + StockAvailable::setQuantity($offer['externalId'], 0, $offer['quantity']); + } + } } - } diff --git a/tests/phpunit/RetailcrmInventoriesTest.php b/tests/phpunit/RetailcrmInventoriesTest.php index e68543e..352b408 100644 --- a/tests/phpunit/RetailcrmInventoriesTest.php +++ b/tests/phpunit/RetailcrmInventoriesTest.php @@ -3,8 +3,12 @@ class RetailcrmInventoriesTest extends RetailcrmTestCase { private $apiMock; - private $product; + private $product1; + private $product2; + const PRODUCT1_QUANTITY = 10; + const PRODUCT2_QUANTITY = 15; + public function setUp() { parent::setUp(); @@ -17,83 +21,61 @@ class RetailcrmInventoriesTest extends RetailcrmTestCase ) ) ->getMock(); + $catalog = new RetailcrmCatalog(); $data = $catalog->getData(); - $this->product = $data[1][0]; - $this->setConfig(); + $this->product1 = $data[1][0]; + $this->product2 = $data[1][1]; } - private function getResponseData() - { - return array( - 'true' => array( - 'success' => true, - 'pagination' => array( - 'limit' => 250, - 'totalCount' => 1, - 'currentPage' => 1, - 'totalPageCount' => 1 - ), - 'offers' => array( - array( - 'id' => 1, - 'xmlId' => 'xmlId', - 'quantity' => 10 - ) - ) - ), - 'false' => array( - 'success' => false, - 'errorMsg' => 'Forbidden' - ) - ); - } /** * @param $apiVersion * @param $response * * @dataProvider dataProviderLoadStocks */ - public function test_load_stocks($apiVersion, $response) + public function testLoadStocks($apiVersion, $response) { if ($response['success'] == true) { - $response['offers'][0]['externalId'] = $this->product['id']; - $this->apiMock->expects($this->any()) - ->method('isSuccessful') - ->willReturn(true); - } elseif ($response['success'] == false) { - $this->apiMock->expects($this->any()) - ->method('isSuccessful') - ->willReturn(false); - } - - $this->apiMock->setResponse($response); - - $this->apiMock->expects($this->any()) ->method('storeInventories') - ->willReturn($this->apiMock); - - - RetailcrmInventories::$apiVersion = $apiVersion; + ->willReturn( + new RetailcrmApiResponse( + '200', + json_encode( + $this->getApiInventories() + ) + ) + ); + } else { + $this->apiMock->expects($this->any()) + ->method('storeInventories') + ->willReturn($response); + } + RetailcrmInventories::$api = $this->apiMock; - - RetailcrmInventories::load_stocks(); - + + RetailcrmInventories::loadStocks(); + + $product1Id = explode('#', $this->product1['id']); + $product2Id = explode('#', $this->product2['id']); + + $prod1Quantity = StockAvailable::getQuantityAvailableByProduct($product1Id[0], $product1Id[1] ); + $prod2Quantity = StockAvailable::getQuantityAvailableByProduct($product2Id[0], $product2Id[1] ); + + $this->assertEquals(self::PRODUCT1_QUANTITY, $prod1Quantity); + $this->assertEquals(self::PRODUCT2_QUANTITY, $prod2Quantity); } public function dataProviderLoadStocks() { - $this->setUp(); - $response = $this->getResponseData(); return array( array( - + 'api_version' => 4, 'response' => $response['true'], - 'api_version' => 4 ), array( 'api_version' => 5, @@ -109,4 +91,37 @@ class RetailcrmInventoriesTest extends RetailcrmTestCase ) ); } -} \ No newline at end of file + + private function getResponseData() + { + return array( + 'true' => $this->getApiInventories(), + 'false' => false + ); + } + + private function getApiInventories() + { + return array( + "success" => true, + "pagination"=> array( + "limit"=> 250, + "totalCount"=> 1, + "currentPage"=> 1, + "totalPageCount"=> 1 + ), + "offers" => array( + array( + 'externalId' => $this->product1['id'], + 'xmlId' => 'xmlId', + 'quantity' => self::PRODUCT1_QUANTITY, + ), + array( + 'externalId' => $this->product2['id'], + 'xmlId' => 'xmlId', + 'quantity' => self::PRODUCT2_QUANTITY, + ) + ) + ); + } +}