mirror of
https://github.com/retailcrm/prestashop-module.git
synced 2025-04-06 14:43:37 +03:00
v2.2.9
This commit is contained in:
parent
4dfc7b8d1a
commit
058f007b36
3 changed files with 194 additions and 0 deletions
29
retailcrm/job/inventories.php
Normal file
29
retailcrm/job/inventories.php
Normal file
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
/**
|
||||
* @author Retail Driver LCC
|
||||
* @copyright RetailCRM
|
||||
* @license GPL
|
||||
* @version 2.2.0
|
||||
* @link https://retailcrm.ru
|
||||
*
|
||||
*/
|
||||
|
||||
$_SERVER['HTTPS'] = 1;
|
||||
|
||||
require(dirname(__FILE__) . '/../../../config/config.inc.php');
|
||||
require(dirname(__FILE__) . '/../../../init.php');
|
||||
require(dirname(__FILE__) . '/../bootstrap.php');
|
||||
|
||||
$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 {
|
||||
error_log('inventories: set api key & url first', 3, _PS_ROOT_DIR_ . '/retailcrm.log');
|
||||
exit();
|
||||
}
|
||||
|
||||
RetailcrmInventories::load_stocks();
|
53
retailcrm/lib/RetailcrmInventories.php
Normal file
53
retailcrm/lib/RetailcrmInventories.php
Normal file
|
@ -0,0 +1,53 @@
|
|||
<?php
|
||||
|
||||
|
||||
class RetailcrmInventories
|
||||
{
|
||||
public static $api;
|
||||
public static $default_lang;
|
||||
public static $apiVersion;
|
||||
/**
|
||||
* Load stock from retailCRM
|
||||
*
|
||||
* @return mixed
|
||||
*/
|
||||
public function load_stocks() {
|
||||
$page = 1;
|
||||
|
||||
do {
|
||||
$result = self::$api->storeInventories(array(), $page, 250);
|
||||
|
||||
if (!$result->isSuccessful()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$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() {
|
||||
|
||||
return $this->load_stocks();
|
||||
|
||||
}
|
||||
|
||||
}
|
112
tests/phpunit/RetailcrmInventoriesTest.php
Normal file
112
tests/phpunit/RetailcrmInventoriesTest.php
Normal file
|
@ -0,0 +1,112 @@
|
|||
<?php
|
||||
|
||||
class RetailcrmInventoriesTest extends RetailcrmTestCase
|
||||
{
|
||||
private $apiMock;
|
||||
private $product;
|
||||
|
||||
public function setUp()
|
||||
{
|
||||
parent::setUp();
|
||||
|
||||
$this->apiMock = $this->getMockBuilder('RetailcrmProxy')
|
||||
->disableOriginalConstructor()
|
||||
->setMethods(
|
||||
array(
|
||||
'storeInventories'
|
||||
)
|
||||
)
|
||||
->getMock();
|
||||
$catalog = new RetailcrmCatalog();
|
||||
$data = $catalog->getData();
|
||||
$this->product = $data[1][0];
|
||||
|
||||
$this->setConfig();
|
||||
}
|
||||
|
||||
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 $retailcrm
|
||||
* @param $response
|
||||
*
|
||||
* @dataProvider dataProviderLoadStocks
|
||||
*/
|
||||
public function test_load_stocks($apiVersion, $response)
|
||||
{
|
||||
if ($response['success'] == true) {
|
||||
$response['offers'][0]['externalId'] = $this->product['id'];
|
||||
$this->responseMock->expects($this->any())
|
||||
->method('isSuccessful')
|
||||
->willReturn(true);
|
||||
} elseif ($response['success'] == false) {
|
||||
$this->responseMock->expects($this->any())
|
||||
->method('isSuccessful')
|
||||
->willReturn(false);
|
||||
}
|
||||
|
||||
$this->responseMock->setResponse($response);
|
||||
|
||||
if ($retailcrm) {
|
||||
$retailcrm->expects($this->any())
|
||||
->method('storeInventories')
|
||||
->willReturn($this->responseMock);
|
||||
}
|
||||
|
||||
RetailcrmInventories::$apiVersion = $apiVersion;
|
||||
RetailcrmInventories::$api = $this->apiMock;
|
||||
|
||||
RetailcrmInventories::load_stocks();
|
||||
|
||||
}
|
||||
|
||||
public function dataProviderLoadStocks()
|
||||
{
|
||||
$this->setUp();
|
||||
|
||||
$response = $this->getResponseData();
|
||||
|
||||
return array(
|
||||
array(
|
||||
|
||||
'response' => $response['true'],
|
||||
'api_version' => 4
|
||||
),
|
||||
array(
|
||||
'api_version' => 5,
|
||||
'response' => $response['true']
|
||||
),
|
||||
array(
|
||||
'api_version' => 4,
|
||||
'response' => $response['false']
|
||||
),
|
||||
array(
|
||||
'api_version' => 5,
|
||||
'response' => $response['false']
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue