112 lines
2.9 KiB
PHP
112 lines
2.9 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace External\FlexiBeeBundle\Synchronizers;
|
|
|
|
use AbraFlexi\SkladovaKarta;
|
|
use KupShop\StoresBundle\Utils\StoresInStore;
|
|
|
|
class SupplySynchronizer extends BaseSynchronizer
|
|
{
|
|
protected static string $type = 'supply';
|
|
protected static ?string $evidenceClass = SkladovaKarta::class;
|
|
|
|
private ?StoresInStore $storesInStore = null;
|
|
|
|
/**
|
|
* @required
|
|
*/
|
|
public function setStoresInStore(?StoresInStore $storesInStore): void
|
|
{
|
|
$this->storesInStore = $storesInStore;
|
|
}
|
|
|
|
public function syncSingleItem(int $id, ?int $flexiId = null): void
|
|
{
|
|
$flexiId ??= $this->flexiBeeUtil->getProductFlexiId($id);
|
|
|
|
if (!$flexiId) {
|
|
return;
|
|
}
|
|
|
|
foreach ($this->flexiBeeApi->getStockCards(flexiProductIds: [$flexiId]) as $item) {
|
|
$this->processItem($item);
|
|
}
|
|
}
|
|
|
|
protected function process(?int $lastSyncTime = null): void
|
|
{
|
|
parent::process($lastSyncTime);
|
|
|
|
$this->flexiBeeUtil->recalculateStores();
|
|
}
|
|
|
|
protected function getItems(?int $lastSyncTime = null): iterable
|
|
{
|
|
if ($this->mode === self::MODE_NORMAL) {
|
|
return parent::getItems($lastSyncTime);
|
|
}
|
|
|
|
if ($this->mode === self::MODE_FULL) {
|
|
return $this->flexiBeeApi->getStockCards();
|
|
}
|
|
|
|
return [];
|
|
}
|
|
|
|
protected function processItem(array $item): void
|
|
{
|
|
$flexiId = $this->flexiBeeUtil->getFlexiIdFromRef($item['cenik']->ref);
|
|
|
|
// nemame product
|
|
if (!($mapping = $this->flexiBeeUtil->getItemMapping($flexiId))) {
|
|
return;
|
|
}
|
|
|
|
[$productId, $variationId] = $mapping;
|
|
|
|
// najdu ID skladu
|
|
$storeId = $this->getStoreId(
|
|
$this->flexiBeeUtil->getFlexiIdFromRef($item['sklad']->ref)
|
|
);
|
|
|
|
// pokud nemam ID skladu, tak ten sklad pravdepodobne nesynchronizujeme, takze muzu ignorovat
|
|
if (!$storeId) {
|
|
return;
|
|
}
|
|
|
|
// aktualizuju sklad produktu na danem skladu
|
|
$this->storesInStore->updateStoreItem(
|
|
[
|
|
'id_store' => $storeId,
|
|
'id_product' => $productId,
|
|
'id_variation' => $variationId,
|
|
// use floor so for example 0.5 is floored to 0
|
|
'quantity' => floor((float) $item['dostupMj']),
|
|
],
|
|
false
|
|
);
|
|
}
|
|
|
|
protected function getStoreId(int $flexiId): ?int
|
|
{
|
|
static $storeIdCache = [];
|
|
|
|
if ($storeIdCache[$flexiId] ?? false) {
|
|
return $storeIdCache[$flexiId];
|
|
}
|
|
|
|
if (!($storeId = $this->flexiBeeUtil->getMapping(StoreSynchronizer::getType(), $flexiId))) {
|
|
return null;
|
|
}
|
|
|
|
return $storeIdCache[$flexiId] = $storeId;
|
|
}
|
|
|
|
protected function getItemsFilter(): array
|
|
{
|
|
return ['ucetObdobi' => 'code:'.$this->flexiBeeApi->getCurrentAccountingPeriod()];
|
|
}
|
|
}
|