Minsaleqty.php 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\CatalogInventory\Helper;
  7. use Magento\Customer\Api\GroupManagementInterface;
  8. use Magento\Framework\App\ObjectManager;
  9. use Magento\Framework\Serialize\Serializer\Json;
  10. use Magento\Store\Model\Store;
  11. /**
  12. * MinSaleQty value manipulation helper
  13. */
  14. class Minsaleqty
  15. {
  16. /**
  17. * Core store config
  18. *
  19. * @var \Magento\Framework\App\Config\ScopeConfigInterface
  20. */
  21. protected $scopeConfig;
  22. /**
  23. * @var \Magento\Framework\Math\Random
  24. */
  25. protected $mathRandom;
  26. /**
  27. * @var GroupManagementInterface
  28. */
  29. protected $groupManagement;
  30. /**
  31. * @var Json
  32. */
  33. private $serializer;
  34. /**
  35. * @var array
  36. */
  37. private $minSaleQtyCache = [];
  38. /**
  39. * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
  40. * @param \Magento\Framework\Math\Random $mathRandom
  41. * @param GroupManagementInterface $groupManagement
  42. * @param Json|null $serializer
  43. */
  44. public function __construct(
  45. \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
  46. \Magento\Framework\Math\Random $mathRandom,
  47. GroupManagementInterface $groupManagement,
  48. Json $serializer = null
  49. ) {
  50. $this->scopeConfig = $scopeConfig;
  51. $this->mathRandom = $mathRandom;
  52. $this->groupManagement = $groupManagement;
  53. $this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
  54. }
  55. /**
  56. * Retrieve fixed qty value
  57. *
  58. * @param int|float|string|null $qty
  59. * @return float|null
  60. */
  61. protected function fixQty($qty)
  62. {
  63. return !empty($qty) ? (float) $qty : null;
  64. }
  65. /**
  66. * Generate a storable representation of a value
  67. *
  68. * @param int|float|string|array $value
  69. * @return string
  70. */
  71. protected function serializeValue($value)
  72. {
  73. if (is_numeric($value)) {
  74. $data = (float) $value;
  75. return (string) $data;
  76. } elseif (is_array($value)) {
  77. $data = [];
  78. foreach ($value as $groupId => $qty) {
  79. if (!array_key_exists($groupId, $data)) {
  80. $data[$groupId] = $this->fixQty($qty);
  81. }
  82. }
  83. if (count($data) == 1 && array_key_exists($this->getAllCustomersGroupId(), $data)) {
  84. return (string) $data[$this->getAllCustomersGroupId()];
  85. }
  86. return $this->serializer->serialize($data);
  87. } else {
  88. return '';
  89. }
  90. }
  91. /**
  92. * Create a value from a storable representation
  93. *
  94. * @param int|float|string $value
  95. * @return array
  96. */
  97. protected function unserializeValue($value)
  98. {
  99. if (is_numeric($value)) {
  100. return [$this->getAllCustomersGroupId() => $this->fixQty($value)];
  101. } elseif (is_string($value) && !empty($value)) {
  102. return $this->serializer->unserialize($value);
  103. } else {
  104. return [];
  105. }
  106. }
  107. /**
  108. * Check whether value is in form retrieved by _encodeArrayFieldValue()
  109. *
  110. * @param string|array $value
  111. * @return bool
  112. */
  113. protected function isEncodedArrayFieldValue($value)
  114. {
  115. if (!is_array($value)) {
  116. return false;
  117. }
  118. unset($value['__empty']);
  119. foreach ($value as $row) {
  120. if (!is_array($row)
  121. || !array_key_exists('customer_group_id', $row)
  122. || !array_key_exists('min_sale_qty', $row)
  123. ) {
  124. return false;
  125. }
  126. }
  127. return true;
  128. }
  129. /**
  130. * Encode value to be used in \Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray
  131. *
  132. * @param array $value
  133. * @return array
  134. */
  135. protected function encodeArrayFieldValue(array $value)
  136. {
  137. $result = [];
  138. foreach ($value as $groupId => $qty) {
  139. $resultId = $this->mathRandom->getUniqueHash('_');
  140. $result[$resultId] = ['customer_group_id' => $groupId, 'min_sale_qty' => $this->fixQty($qty)];
  141. }
  142. return $result;
  143. }
  144. /**
  145. * Decode value from used in \Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray
  146. *
  147. * @param array $value
  148. * @return array
  149. */
  150. protected function decodeArrayFieldValue(array $value)
  151. {
  152. $result = [];
  153. unset($value['__empty']);
  154. foreach ($value as $row) {
  155. if (!is_array($row)
  156. || !array_key_exists('customer_group_id', $row)
  157. || !array_key_exists('min_sale_qty', $row)
  158. ) {
  159. continue;
  160. }
  161. $groupId = $row['customer_group_id'];
  162. $qty = $this->fixQty($row['min_sale_qty']);
  163. $result[$groupId] = $qty;
  164. }
  165. return $result;
  166. }
  167. /**
  168. * Retrieve min_sale_qty value from config
  169. *
  170. * @param int $customerGroupId
  171. * @param null|string|bool|int|Store $store
  172. * @return float|null
  173. */
  174. public function getConfigValue($customerGroupId, $store = null)
  175. {
  176. $key = $customerGroupId . '-' . $store;
  177. if (!isset($this->minSaleQtyCache[$key])) {
  178. $value = $this->scopeConfig->getValue(
  179. \Magento\CatalogInventory\Model\Configuration::XML_PATH_MIN_SALE_QTY,
  180. \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
  181. $store
  182. );
  183. $value = $this->unserializeValue($value);
  184. if ($this->isEncodedArrayFieldValue($value)) {
  185. $value = $this->decodeArrayFieldValue($value);
  186. }
  187. $result = null;
  188. foreach ($value as $groupId => $qty) {
  189. if ($groupId == $customerGroupId) {
  190. $result = $qty;
  191. break;
  192. } elseif ($groupId == $this->getAllCustomersGroupId()) {
  193. $result = $qty;
  194. }
  195. }
  196. $this->minSaleQtyCache[$key] = $this->fixQty($result);
  197. }
  198. return $this->minSaleQtyCache[$key];
  199. }
  200. /**
  201. * Make value readable by \Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray
  202. *
  203. * @param string|array $value
  204. * @return array
  205. */
  206. public function makeArrayFieldValue($value)
  207. {
  208. $value = $this->unserializeValue($value);
  209. if (!$this->isEncodedArrayFieldValue($value)) {
  210. $value = $this->encodeArrayFieldValue($value);
  211. }
  212. return $value;
  213. }
  214. /**
  215. * Make value ready for store
  216. *
  217. * @param string|array $value
  218. * @return string
  219. */
  220. public function makeStorableArrayFieldValue($value)
  221. {
  222. if ($this->isEncodedArrayFieldValue($value)) {
  223. $value = $this->decodeArrayFieldValue($value);
  224. }
  225. $value = $this->serializeValue($value);
  226. return $value;
  227. }
  228. /**
  229. * Return the all customer group id
  230. *
  231. * @return int
  232. */
  233. protected function getAllCustomersGroupId()
  234. {
  235. return $this->groupManagement->getAllCustomersGroup()->getId();
  236. }
  237. }