StockRepository.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. <?php
  2. namespace Core\FacturisRepository;
  3. class StockRepository
  4. extends Repository
  5. implements \Core\App\Repository\Facturis\StockInterface
  6. {
  7. protected $remoteStock;
  8. public $optionStockFilter;
  9. public $localProducts;
  10. public function getAll()
  11. {
  12. $this->remoteStock = array();
  13. $pdl = '';
  14. $gestiune = '';
  15. $filters = explode('__', $this->optionStockFilter);
  16. $pdl = str_replace("_", " ", $filters[0]);
  17. if(isset($filters[1]))
  18. {
  19. $gestiune = str_replace("_", " ", $filters[1]);
  20. }
  21. $optionStockFilters = array(
  22. "pdl"=> $pdl,
  23. "gestiune" => $gestiune,
  24. "with_zero_stock" => true
  25. );
  26. $result = $this->getStockByStockFilter($optionStockFilters);
  27. $this->groupStock($result);
  28. return $this->remoteStock;
  29. }
  30. public function groupStock($data)
  31. {
  32. if(empty($data))
  33. {
  34. return array();
  35. }
  36. foreach($data as $item)
  37. {
  38. if($item['Cantitate'] != 0)
  39. {
  40. if($this->isValidProduct($item, 'Alt_Cod', 'prod_cod1'))
  41. {
  42. $this->addQty($item, 'Alt_Cod', 'prod_cod1');
  43. }
  44. else
  45. {
  46. if($this->isValidProduct($item, 'Cod_EAN', 'prod_cod'))
  47. {
  48. $this->addQty($item, 'Cod_EAN', 'prod_cod');
  49. }
  50. else
  51. {
  52. if($this->isValidProduct($item, 'Cod_SKU', 'prod_sku'))
  53. {
  54. $this->addQty($item, 'Cod_SKU', 'prod_sku');
  55. }
  56. else
  57. {
  58. if($this->isValidProduct($item, 'Denumire', 'prod_nume'))
  59. {
  60. $this->addQty($item, 'Denumire', 'prod_nume');
  61. }
  62. }
  63. }
  64. }
  65. }
  66. }
  67. }
  68. public function isValidProduct($item, $stockAttr, $productAttr)
  69. {
  70. if(
  71. !empty($item[$stockAttr]) &&
  72. in_array($item[$stockAttr], array_column($this->localProducts, $productAttr)))
  73. {
  74. return true;
  75. }
  76. else
  77. {
  78. return false;
  79. }
  80. }
  81. public function addQty($item, $stockAttr, $productAttr)
  82. {
  83. if(in_array($item[$stockAttr], array_column($this->remoteStock, 'product')))
  84. {
  85. $key = array_search($item[$stockAttr], array_column($this->remoteStock, 'product'));
  86. $this->remoteStock[$key]['qty'] = $this->remoteStock[$key]['qty'] + $item['Cantitate'];
  87. }
  88. else
  89. {
  90. $this->remoteStock[] = array(
  91. 'product' => $item[$stockAttr],
  92. 'type' => $productAttr,
  93. 'qty' => $item['Cantitate']
  94. );
  95. }
  96. }
  97. public function setOptionStockFilter($optionStockFilter)
  98. {
  99. $this->optionStockFilter = $optionStockFilter;
  100. }
  101. public function testAuth()
  102. {
  103. $this->test();
  104. }
  105. public function setLocalProducts($localProducts)
  106. {
  107. $this->localProducts = $localProducts;
  108. }
  109. }