Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
BarcodeProvider.php
1<?php
2
4
9
11{
12 protected const ENTITY_ID = 'barcode';
13
14 public function fillDialog(Dialog $dialog): void
15 {
16 $dialog->loadPreselectedItems();
17
18 if ($dialog->getItemCollection()->count() > 0)
19 {
20 foreach ($dialog->getItemCollection() as $item)
21 {
22 $dialog->addRecentItem($item);
23 }
24 }
25 }
26
27 public function doSearch(SearchQuery $searchQuery, Dialog $dialog): void
28 {
29 $searchQuery->setCacheable(false);
30 $productIds = $this->getProductIdsByBarcode($searchQuery->getQuery());
31 if (!$productIds)
32 {
33 return;
34 }
35
36 $productIds = array_unique($productIds);
37 $products = $this->getProductsByIds($productIds);
38 $elementMap = [];
39 foreach ($products as $key => $product)
40 {
41 $elementMap[$product['ID']][] = $key;
42 }
43
44 if (!empty($products))
45 {
46 $barcodeRaw = \Bitrix\Catalog\StoreBarcodeTable::getList([
47 'filter' => [
48 '=PRODUCT_ID' => $productIds,
49 'BARCODE' => $searchQuery->getQuery() . '%'
50 ],
51 'select' => ['BARCODE', 'PRODUCT_ID']
52 ]);
53
54 while ($barcode = $barcodeRaw->fetch())
55 {
56 $productId = $barcode['PRODUCT_ID'];
57 if (!isset($elementMap[$productId]))
58 {
59 continue;
60 }
61
62 foreach ($elementMap[$productId] as $key)
63 {
64 $products[$key]['BARCODE'] = $barcode['BARCODE'];
65 }
66 }
67
68 foreach ($products as $product)
69 {
70 $dialog->addItem(
71 $this->makeItem($product)
72 );
73 }
74 }
75 }
76
77 public function handleBeforeItemSave(Item $item): void
78 {
79 $item->setSaveable(false);
80 }
81
82 private function getProductIdsByBarcode(string $barcodeString = ''): array
83 {
84 $barcodes = [];
85 $elementRaw = \CIBlockElement::GetList(
86 [],
87 [
88 'ACTIVE' => 'Y',
89 'CHECK_PERMISSIONS' => 'Y',
90 'PRODUCT_BARCODE' => $barcodeString . '%',
91 ],
92 false,
93 false,
94 ['ID']
95 );
96
97 while ($element = $elementRaw->Fetch())
98 {
99 $barcodes[] = $element['ID'];
100 }
101
102 return $barcodes;
103 }
104}
loadPreselectedItems($preselectedMode=true)
Definition dialog.php:389
setSaveable(bool $flag=true)
Definition item.php:552