StockRepository.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. <?php
  2. namespace MarketplaceRepository;
  3. class StockRepository
  4. extends Repository
  5. implements \Core\App\Repository\Marketplace\StockInterface
  6. {
  7. public $stockLocations;
  8. public $localProducts;
  9. public function setLocalProducts($localProducts)
  10. {
  11. if(!empty($localProducts))
  12. {
  13. foreach($localProducts as $localProduct)
  14. {
  15. $this->localProducts[] = (array) $localProduct;
  16. }
  17. }
  18. }
  19. public function update($remoteStock)
  20. {
  21. \Core\Log\FileLog::write('Facturis Stock: ' . json_encode($remoteStock), \Core\Log\FileLog::DATA);
  22. \Core\Log\FileLog::write('Shop Products: ' . json_encode($this->localProducts), \Core\Log\FileLog::DATA);
  23. $this->updateStock($remoteStock);
  24. }
  25. public function getShopLocations()
  26. {
  27. $shopLocations = array();
  28. $locations = $this->getLocations();
  29. if(!empty($locations))
  30. {
  31. foreach($locations as $location)
  32. {
  33. $shopLocations[] = array('id' => $location['id'], 'name' => $location['name']);
  34. }
  35. }
  36. return $shopLocations;
  37. }
  38. public function setStockLocations($locations)
  39. {
  40. $this->stockLocations = $locations;
  41. }
  42. //update
  43. private function updateStock($remoteStock)
  44. {
  45. if(!empty($remoteStock))
  46. {
  47. foreach($remoteStock as $remoteStockItem)
  48. {
  49. if (in_array($remoteStockItem['product'], array_column($this->localProducts, $remoteStockItem['type']))) {
  50. $index = array_search($remoteStockItem['product'], array_column($this->localProducts, $remoteStockItem['type']));
  51. $product_ids = explode('_', $this->localProducts[$index]['prod_cod1']);
  52. $variant_ids = $product_ids;
  53. unset($variant_ids[0]);
  54. unset($variant_ids[1]);
  55. if(!empty($variant_ids))
  56. {
  57. foreach($variant_ids as $variant_id)
  58. {
  59. $this->updateQty($remoteStockItem['qty'], $product_ids[1], $variant_id);
  60. }
  61. }
  62. }
  63. }
  64. }
  65. }
  66. private function updateQty($qty, $product_id, $variant_id)
  67. {
  68. $productVariant = $this->getProductVariant($product_id, $variant_id);
  69. if(!empty($productVariant))
  70. {
  71. $resultInventoryLevels = $this->getInventoryLevels($productVariant['inventory_item_id']);
  72. if(!empty($resultInventoryLevels))
  73. {
  74. foreach($resultInventoryLevels as $inventoryLevel)
  75. {
  76. $adjustQty = ((-1) * $inventoryLevel['available']) + (int) $qty;
  77. if((is_array($this->stockLocations) && in_array($inventoryLevel['location_id'], $this->stockLocations)) ||
  78. ($this->stockLocations == \Core\App\Factory\SettingsFactory::DEFAULT_LOCATIONS))
  79. {
  80. $this->adjustInventoryLevel($inventoryLevel['location_id'], $inventoryLevel['inventory_item_id'], $adjustQty, $inventoryLevel['available']);
  81. }
  82. }
  83. }
  84. }
  85. }
  86. }