123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101 |
- <?php
- namespace MarketplaceRepository;
- class StockRepository
- extends Repository
- implements \Core\App\Repository\Marketplace\StockInterface
- {
- public $stockLocations;
- public $localProducts;
- public function setLocalProducts($localProducts)
- {
- if(!empty($localProducts))
- {
- foreach($localProducts as $localProduct)
- {
- $this->localProducts[] = (array) $localProduct;
- }
- }
- }
-
- public function update($remoteStock)
- {
- \Core\Log\FileLog::write('Facturis Stock: ' . json_encode($remoteStock), \Core\Log\FileLog::DATA);
- \Core\Log\FileLog::write('Shop Products: ' . json_encode($this->localProducts), \Core\Log\FileLog::DATA);
- $this->updateStock($remoteStock);
- }
- public function getShopLocations()
- {
- $shopLocations = array();
- $locations = $this->getLocations();
- if(!empty($locations))
- {
- foreach($locations as $location)
- {
- $shopLocations[] = array('id' => $location['id'], 'name' => $location['name']);
- }
- }
-
- return $shopLocations;
- }
- public function setStockLocations($locations)
- {
- $this->stockLocations = $locations;
- }
- //update
- private function updateStock($remoteStock)
- {
- if(!empty($remoteStock))
- {
- foreach($remoteStock as $remoteStockItem)
- {
- if (in_array($remoteStockItem['product'], array_column($this->localProducts, $remoteStockItem['type']))) {
- $index = array_search($remoteStockItem['product'], array_column($this->localProducts, $remoteStockItem['type']));
- $product_ids = explode('_', $this->localProducts[$index]['prod_cod1']);
- $variant_ids = $product_ids;
- unset($variant_ids[0]);
- unset($variant_ids[1]);
- if(!empty($variant_ids))
- {
- foreach($variant_ids as $variant_id)
- {
- $this->updateQty($remoteStockItem['qty'], $product_ids[1], $variant_id);
- }
- }
- }
- }
- }
- }
- private function updateQty($qty, $product_id, $variant_id)
- {
- $productVariant = $this->getProductVariant($product_id, $variant_id);
- if(!empty($productVariant))
- {
- $resultInventoryLevels = $this->getInventoryLevels($productVariant['inventory_item_id']);
- if(!empty($resultInventoryLevels))
- {
- foreach($resultInventoryLevels as $inventoryLevel)
- {
- $adjustQty = ((-1) * $inventoryLevel['available']) + (int) $qty;
-
- if((is_array($this->stockLocations) && in_array($inventoryLevel['location_id'], $this->stockLocations)) ||
- ($this->stockLocations == \Core\App\Factory\SettingsFactory::DEFAULT_LOCATIONS))
- {
- $this->adjustInventoryLevel($inventoryLevel['location_id'], $inventoryLevel['inventory_item_id'], $adjustQty, $inventoryLevel['available']);
- }
- }
- }
- }
- }
- }
|