Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
QuantityPerSource.php
Go to the documentation of this file.
1 <?php
6 declare(strict_types=1);
7 
9 
18 
23 {
27  private $isSingleSourceMode;
28 
32  private $isSourceItemManagementAllowedForProductType;
33 
37  private $sourceRepository;
38 
42  private $getSourceItemsBySku;
43 
50  public function __construct(
51  IsSingleSourceModeInterface $isSingleSourceMode,
52  IsSourceItemManagementAllowedForProductTypeInterface $isSourceItemManagementAllowedForProductType,
53  SourceRepositoryInterface $sourceRepository,
54  GetSourceItemsBySkuInterface $getSourceItemsBySku
55  ) {
56  $this->isSingleSourceMode = $isSingleSourceMode;
57  $this->isSourceItemManagementAllowedForProductType = $isSourceItemManagementAllowedForProductType;
58  $this->sourceRepository = $sourceRepository;
59  $this->getSourceItemsBySku = $getSourceItemsBySku;
60  }
61 
65  public function modifyData(array $data)
66  {
67  if (0 === $data['totalRecords'] || true === $this->isSingleSourceMode->execute()) {
68  return $data;
69  }
70 
71  foreach ($data['items'] as &$item) {
72  $item['quantity_per_source'] = $this->isSourceItemManagementAllowedForProductType->execute(
73  $item['type_id']
74  ) === true ? $this->getSourceItemsData($item['sku']) : [];
75  }
76  unset($item);
77 
78  return $data;
79  }
80 
86  private function getSourceItemsData(string $sku): array
87  {
88  $sourceItems = $this->getSourceItemsBySku->execute($sku);
89 
90  $sourceItemsData = [];
91  foreach ($sourceItems as $sourceItem) {
92  $source = $this->sourceRepository->get($sourceItem->getSourceCode());
93  $qty = (float)$sourceItem->getQuantity();
94 
95  $sourceItemsData[] = [
96  'source_name' => $source->getName(),
97  'qty' => $qty,
98  ];
99  }
100  return $sourceItemsData;
101  }
102 
106  public function modifyMeta(array $meta)
107  {
108  if (true === $this->isSingleSourceMode->execute()) {
109  return $meta;
110  }
111 
112  $meta = array_replace_recursive($meta, [
113  'product_columns' => [
114  'children' => [
115  'quantity_per_source' => $this->getQuantityPerSourceMeta(),
116  'qty' => [
117  'arguments' => null,
118  ],
119  ],
120  ],
121  ]);
122  return $meta;
123  }
124 
128  private function getQuantityPerSourceMeta(): array
129  {
130  return [
131  'arguments' => [
132  'data' => [
133  'config' => [
134  'sortOrder' => 76,
135  'filter' => false,
136  'sortable' => false,
137  'label' => __('Quantity per Source'),
138  'dataType' => Text::NAME,
139  'componentType' => Column::NAME,
140  'component' => 'Magento_InventoryCatalogAdminUi/js/product/grid/cell/quantity-per-source',
141  ]
142  ],
143  ],
144  ];
145  }
146 }
$source
Definition: source.php:23
__construct(IsSingleSourceModeInterface $isSingleSourceMode, IsSourceItemManagementAllowedForProductTypeInterface $isSourceItemManagementAllowedForProductType, SourceRepositoryInterface $sourceRepository, GetSourceItemsBySkuInterface $getSourceItemsBySku)
__()
Definition: __.php:13
$sourceItems
$sourceRepository
Definition: source.php:20