1
0
mirror of synced 2024-11-21 20:46:06 +03:00
This commit is contained in:
Sergey 2019-01-24 11:39:40 +03:00
parent b15f8ec5a8
commit a6d80bf154
11 changed files with 236 additions and 5 deletions

View File

@ -1,3 +1,6 @@
## 2018-12-25 v.2.4.0
* Добавлен функционал получения остатков из retailCRM
## 2018-12-25 v.2.3.2
* Добавлена выгрузка картинок категорий товаров в ICML

View File

@ -1 +1 @@
2.3.2
2.4.0

24
src/Cron/Inventories.php Normal file
View File

@ -0,0 +1,24 @@
<?php
namespace Retailcrm\Retailcrm\Cron;
class Inventories
{
private $inventoriesUpload;
private $helper;
public function __construct(
\Retailcrm\Retailcrm\Helper\Data $helper,
\Retailcrm\Retailcrm\Model\Service\InventoriesUpload $InventoriesUpload
) {
$this->helper = $helper;
$this->inventoriesUpload = $InventoriesUpload;
}
public function execute()
{
if ($this->helper->getInventoriesUpload() == true) {
$this->inventoriesUpload->uploadInventory();
}
}
}

View File

@ -131,6 +131,20 @@ class Data extends AbstractHelper
return false;
}
public function getInventoriesUpload()
{
$inventories = $this->scopeConfig->getValue(
self::XML_PATH_RETAILCRM . self::XML_PATH_INVENTORIES . 'active',
ScopeConfigInterface::SCOPE_TYPE_DEFAULT
);
if ($inventories) {
return true;
}
return false;
}
/**
* @param $website
*

View File

@ -0,0 +1,55 @@
<?php
namespace Retailcrm\Retailcrm\Model\Service;
use Magento\Catalog\Api\ProductRepositoryInterface;
class InventoriesUpload
{
private $productRepo;
private $api;
public function __construct(
ProductRepositoryInterface $productRepo,
\Retailcrm\Retailcrm\Helper\Proxy $api
) {
$this->productRepo = $productRepo;
$this->api = $api;
}
/**
* {@inheritdoc}
*/
public function uploadInventory()
{
if (!$this->api->isConfigured()) {
return false;
}
$page = 1;
do {
$response = $this->api->storeInventories(array(), $page, 250);
if ($response === false || !$response->isSuccessful()) {
return false;
}
foreach ($response['offers'] as $offer) {
if (isset($offer['externalId'])) {
$product = $this->productRepo->getById($offer['externalId']);
$product->setStockData(
['qty' => $offer['quantity'],
'is_in_stock' => $offer['quantity'] > 0]
);
$product->save();
}
}
$totalPageCount = $response['pagination']['totalPageCount'];
$page++;
} while ($page <= $totalPageCount);
return true;
}
}

View File

@ -2,7 +2,7 @@
namespace Retailcrm\Retailcrm\Model\Setting;
class DaemonCollector implements \Magento\Framework\Option\ArrayInterface
class Select implements \Magento\Framework\Option\ArrayInterface
{
public function toOptionArray()
{

View File

@ -0,0 +1,123 @@
<?php
namespace Retailcrm\Retailcrm\Test\Unit\Model\Service;
class InventoriesUploadTest extends \PHPUnit\Framework\TestCase
{
private $mockApi;
private $mockProductRepository;
private $mockResponse;
private $mockProduct;
public function setUp()
{
$this->mockApi = $this->getMockBuilder(\Retailcrm\Retailcrm\Helper\Proxy::class)
->disableOriginalConstructor()
->setMethods([
'storeInventories',
'isConfigured'
])
->getMock();
$this->mockProductRepository = $this->getMockBuilder(\Magento\Catalog\Api\ProductRepositoryInterface::class)
->disableOriginalConstructor()
->setMethods(['getById'])
->getMockForAbstractClass();
$this->mockResponse = $this->getMockBuilder(\RetailCrm\Response\ApiResponse::class)
->disableOriginalConstructor()
->setMethods(['isSuccessful'])
->getMock();
$this->mockProduct = $this->getMockBuilder(\Magento\Catalog\Model\Product::class)
->disableOriginalConstructor()
->setMethods([
'setStockData',
'save'
])
->getMock();
}
/**
* @param $response
*
* @dataProvider dataProviderLoadStocks
*/
public function testInventoriesUpdload($response)
{
if ($response != false) {
$responseInventories = new \RetailCrm\Response\ApiResponse(200, json_encode($response));
$this->mockResponse->expects($this->any())
->method('isSuccessful')
->willReturn(true);
$this->mockApi->expects($this->any())
->method('isConfigured')
->willReturn(true);
$this->mockApi->expects($this->any())
->method('storeInventories')
->willReturn($responseInventories);
$this->mockProductRepository->expects($this->any())
->method('getById')
->willReturn($this->mockProduct);
} else {
$this->mockResponse->expects($this->any())
->method('isSuccessful')
->willReturn($response);
}
$inventoriesUpload = new \Retailcrm\Retailcrm\Model\Service\InventoriesUpload($this->mockProductRepository, $this->mockApi);
$result = $inventoriesUpload->uploadInventory();
if (!$response['success']) {
$this->assertEquals(false, $result);
} else {
$this->assertEquals(true, $result);
}
}
private function getResponseData()
{
return array(
'true' => $this->getApiInventories(),
'false' => false
);
}
public function dataProviderLoadStocks()
{
$response = $this->getResponseData();
return array(
array(
'response' => $response['true']
),
array(
'response' => $response['false']
)
);
}
private function getApiInventories()
{
return array(
'success' => true,
'pagination' => array(
'limit' => 250,
'totalCount' => 1,
'currentPage' => 1,
'totalPageCount' => 1
),
'offers' => array(
array(
'externalId' => 1,
'xmlId' => 'xmlId',
'quantity' => 10
)
)
);
}
}

View File

@ -68,12 +68,19 @@
<label>Daemon Collector</label>
<field id="active" translate="label" type="select" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="0">
<label>Activate</label>
<source_model>Retailcrm\Retailcrm\Model\Setting\DaemonCollector</source_model>
<source_model>Retailcrm\Retailcrm\Model\Setting\Select</source_model>
</field>
<field id="key" translate="label" type="text" sortOrder="30" showInDefault="1" showInWebsite="1" showInStore="0">
<label>Site key</label>
</field>
</group>
<group id="inventories_upload" translate="label" type="select" sortOrder="90" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Synchronization of the stock balance</label>
<field id="active" translate="label" type="select" sortOrder="30" showInDefault="1" showInWebsite="0" showInStore="0">
<label>Activate</label>
<source_model>Retailcrm\Retailcrm\Model\Setting\Select</source_model>
</field>
</group>
</section>
</system>
</config>

View File

@ -7,5 +7,8 @@
<job name="order_hystory" instance="Retailcrm\Retailcrm\Cron\OrderHistory" method="execute">
<schedule>*/5 * * * *</schedule>
</job>
<job name="inventories" instance="Retailcrm\Retailcrm\Cron\Inventories" method="execute">
<schedule>*/15 * * * *</schedule>
</job>
</group>
</config>

View File

@ -23,4 +23,5 @@
"Send","Enviar"
"Default site","Tienda por defecto"
"Activate","Active"
"Site key","Clave de la página web"
"Site key","Clave de la página web"
"Synchronization of the stock balance","Sincronizar el stock"
1 Settings La configuración
23 Send Enviar
24 Default site Tienda por defecto
25 Activate Active
26 Site key Clave de la página web
27 Synchronization of the stock balance Sincronizar el stock

View File

@ -23,4 +23,5 @@
"Send","Выгрузить"
"Default site","Сайт по умолчанию"
"Activate","Активировать"
"Site key","Ключ сайта"
"Site key","Ключ сайта"
"Synchronization of the stock balance","Синхронизация остатков"
1 Settings Настройки
23 Send Выгрузить
24 Default site Сайт по умолчанию
25 Activate Активировать
26 Site key Ключ сайта
27 Synchronization of the stock balance Синхронизация остатков