| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255 | 
							- <?php
 
- /**
 
-  * Copyright © Magento, Inc. All rights reserved.
 
-  * See COPYING.txt for license details.
 
-  */
 
- namespace Magento\CatalogInventory\Helper;
 
- use Magento\Customer\Api\GroupManagementInterface;
 
- use Magento\Framework\App\ObjectManager;
 
- use Magento\Framework\Serialize\Serializer\Json;
 
- use Magento\Store\Model\Store;
 
- /**
 
-  * MinSaleQty value manipulation helper
 
-  */
 
- class Minsaleqty
 
- {
 
-     /**
 
-      * Core store config
 
-      *
 
-      * @var \Magento\Framework\App\Config\ScopeConfigInterface
 
-      */
 
-     protected $scopeConfig;
 
-     /**
 
-      * @var \Magento\Framework\Math\Random
 
-      */
 
-     protected $mathRandom;
 
-     /**
 
-      * @var GroupManagementInterface
 
-      */
 
-     protected $groupManagement;
 
-     /**
 
-      * @var Json
 
-      */
 
-     private $serializer;
 
-     /**
 
-      * @var array
 
-      */
 
-     private $minSaleQtyCache = [];
 
-     /**
 
-      * @param \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig
 
-      * @param \Magento\Framework\Math\Random $mathRandom
 
-      * @param GroupManagementInterface $groupManagement
 
-      * @param Json|null $serializer
 
-      */
 
-     public function __construct(
 
-         \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
 
-         \Magento\Framework\Math\Random $mathRandom,
 
-         GroupManagementInterface $groupManagement,
 
-         Json $serializer = null
 
-     ) {
 
-         $this->scopeConfig = $scopeConfig;
 
-         $this->mathRandom = $mathRandom;
 
-         $this->groupManagement = $groupManagement;
 
-         $this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class);
 
-     }
 
-     /**
 
-      * Retrieve fixed qty value
 
-      *
 
-      * @param int|float|string|null $qty
 
-      * @return float|null
 
-      */
 
-     protected function fixQty($qty)
 
-     {
 
-         return !empty($qty) ? (float) $qty : null;
 
-     }
 
-     /**
 
-      * Generate a storable representation of a value
 
-      *
 
-      * @param int|float|string|array $value
 
-      * @return string
 
-      */
 
-     protected function serializeValue($value)
 
-     {
 
-         if (is_numeric($value)) {
 
-             $data = (float) $value;
 
-             return (string) $data;
 
-         } elseif (is_array($value)) {
 
-             $data = [];
 
-             foreach ($value as $groupId => $qty) {
 
-                 if (!array_key_exists($groupId, $data)) {
 
-                     $data[$groupId] = $this->fixQty($qty);
 
-                 }
 
-             }
 
-             if (count($data) == 1 && array_key_exists($this->getAllCustomersGroupId(), $data)) {
 
-                 return (string) $data[$this->getAllCustomersGroupId()];
 
-             }
 
-             return $this->serializer->serialize($data);
 
-         } else {
 
-             return '';
 
-         }
 
-     }
 
-     /**
 
-      * Create a value from a storable representation
 
-      *
 
-      * @param int|float|string $value
 
-      * @return array
 
-      */
 
-     protected function unserializeValue($value)
 
-     {
 
-         if (is_numeric($value)) {
 
-             return [$this->getAllCustomersGroupId() => $this->fixQty($value)];
 
-         } elseif (is_string($value) && !empty($value)) {
 
-             return $this->serializer->unserialize($value);
 
-         } else {
 
-             return [];
 
-         }
 
-     }
 
-     /**
 
-      * Check whether value is in form retrieved by _encodeArrayFieldValue()
 
-      *
 
-      * @param string|array $value
 
-      * @return bool
 
-      */
 
-     protected function isEncodedArrayFieldValue($value)
 
-     {
 
-         if (!is_array($value)) {
 
-             return false;
 
-         }
 
-         unset($value['__empty']);
 
-         foreach ($value as $row) {
 
-             if (!is_array($row)
 
-                 || !array_key_exists('customer_group_id', $row)
 
-                 || !array_key_exists('min_sale_qty', $row)
 
-             ) {
 
-                 return false;
 
-             }
 
-         }
 
-         return true;
 
-     }
 
-     /**
 
-      * Encode value to be used in \Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray
 
-      *
 
-      * @param array $value
 
-      * @return array
 
-      */
 
-     protected function encodeArrayFieldValue(array $value)
 
-     {
 
-         $result = [];
 
-         foreach ($value as $groupId => $qty) {
 
-             $resultId = $this->mathRandom->getUniqueHash('_');
 
-             $result[$resultId] = ['customer_group_id' => $groupId, 'min_sale_qty' => $this->fixQty($qty)];
 
-         }
 
-         return $result;
 
-     }
 
-     /**
 
-      * Decode value from used in \Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray
 
-      *
 
-      * @param array $value
 
-      * @return array
 
-      */
 
-     protected function decodeArrayFieldValue(array $value)
 
-     {
 
-         $result = [];
 
-         unset($value['__empty']);
 
-         foreach ($value as $row) {
 
-             if (!is_array($row)
 
-                 || !array_key_exists('customer_group_id', $row)
 
-                 || !array_key_exists('min_sale_qty', $row)
 
-             ) {
 
-                 continue;
 
-             }
 
-             $groupId = $row['customer_group_id'];
 
-             $qty = $this->fixQty($row['min_sale_qty']);
 
-             $result[$groupId] = $qty;
 
-         }
 
-         return $result;
 
-     }
 
-     /**
 
-      * Retrieve min_sale_qty value from config
 
-      *
 
-      * @param int $customerGroupId
 
-      * @param null|string|bool|int|Store $store
 
-      * @return float|null
 
-      */
 
-     public function getConfigValue($customerGroupId, $store = null)
 
-     {
 
-         $key = $customerGroupId . '-' . $store;
 
-         if (!isset($this->minSaleQtyCache[$key])) {
 
-             $value = $this->scopeConfig->getValue(
 
-                 \Magento\CatalogInventory\Model\Configuration::XML_PATH_MIN_SALE_QTY,
 
-                 \Magento\Store\Model\ScopeInterface::SCOPE_STORE,
 
-                 $store
 
-             );
 
-             $value = $this->unserializeValue($value);
 
-             if ($this->isEncodedArrayFieldValue($value)) {
 
-                 $value = $this->decodeArrayFieldValue($value);
 
-             }
 
-             $result = null;
 
-             foreach ($value as $groupId => $qty) {
 
-                 if ($groupId == $customerGroupId) {
 
-                     $result = $qty;
 
-                     break;
 
-                 } elseif ($groupId == $this->getAllCustomersGroupId()) {
 
-                     $result = $qty;
 
-                 }
 
-             }
 
-             $this->minSaleQtyCache[$key] = $this->fixQty($result);
 
-         }
 
-         return $this->minSaleQtyCache[$key];
 
-     }
 
-     /**
 
-      * Make value readable by \Magento\Config\Block\System\Config\Form\Field\FieldArray\AbstractFieldArray
 
-      *
 
-      * @param string|array $value
 
-      * @return array
 
-      */
 
-     public function makeArrayFieldValue($value)
 
-     {
 
-         $value = $this->unserializeValue($value);
 
-         if (!$this->isEncodedArrayFieldValue($value)) {
 
-             $value = $this->encodeArrayFieldValue($value);
 
-         }
 
-         return $value;
 
-     }
 
-     /**
 
-      * Make value ready for store
 
-      *
 
-      * @param string|array $value
 
-      * @return string
 
-      */
 
-     public function makeStorableArrayFieldValue($value)
 
-     {
 
-         if ($this->isEncodedArrayFieldValue($value)) {
 
-             $value = $this->decodeArrayFieldValue($value);
 
-         }
 
-         $value = $this->serializeValue($value);
 
-         return $value;
 
-     }
 
-     /**
 
-      * Return the all customer group id
 
-      *
 
-      * @return int
 
-      */
 
-     protected function getAllCustomersGroupId()
 
-     {
 
-         return $this->groupManagement->getAllCustomersGroup()->getId();
 
-     }
 
- }
 
 
  |