1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
storeproduct.php
См. документацию.
1<?php
2
3namespace Bitrix\Catalog\Controller;
4
5use Bitrix\Catalog\Access\ActionDictionary;
6use Bitrix\Catalog\Config\State;
7use Bitrix\Catalog\ProductTable;
8use Bitrix\Catalog\StoreProductTable;
9use Bitrix\Catalog\StoreTable;
10use Bitrix\Main\Application;
11use Bitrix\Main\Error;
12use Bitrix\Main\Result;
13use Bitrix\Catalog\Store\EnableWizard\Manager;
14
15final class StoreProduct extends Controller
16{
17 use ListAction; // default listAction realization
18 use GetAction; // default getAction realization
19 use CheckExists; // default implementation of existence check
20
21 private const BULK_SAVE_CHUNK_SIZE = 10000;
22 private const BULK_SAVE_PRODUCTS_CHUNK_SIZE = 1000;
23
24 //region Actions
25 public function getFieldsAction(): array
26 {
27 return [$this->getServiceItemName() => $this->getViewFields()];
28 }
29
34
39
40 public function bulkSaveAction(array $items)
41 {
42 if (!State::isUsedInventoryManagement())
43 {
44 $this->addError(
45 new Error(
46 'Inventory management is not enabled',
47 200040400010
48 )
49 );
50
51 return null;
52 }
53
54 if (!Manager::isOnecMode())
55 {
56 $this->addError(
57 new Error(
58 'Inventory management is not in 1C mode',
59 200040400020
60 )
61 );
62
63 return null;
64 }
65
66 $connection = Application::getConnection();
67 $sqlHelper = $connection->getSqlHelper();
68
69 $storeIds = array_flip(
70 array_map(
71 static fn($row) => (int)$row['ID'],
72 StoreTable::getList([
73 'select' => ['ID'],
74 'cache' => ['ttl' => 86400],
75 ])->fetchAll()
76 )
77 );
78
79 foreach (array_chunk($items, self::BULK_SAVE_CHUNK_SIZE) as $chunkItems)
80 {
81 $chunkProductIds = array_unique(
82 array_map(
83 \Closure::fromCallable('intval'),
84 array_column($chunkItems, 'productId')
85 )
86 );
87 $productIds = [];
88 if ($chunkProductIds)
89 {
90 $productIds = array_flip(
91 array_map(
92 static fn($row) => (int)$row['ID'],
94 ->setSelect(['ID'])
95 ->whereIn('ID', $chunkProductIds)
96 ->fetchAll()
97 )
98 );
99 }
100
101 $insertRows = [];
102 foreach ($chunkItems as $item)
103 {
104 $productId = isset($item['productId']) ? (int)$item['productId'] : 0;
105 if (!isset($productIds[$productId]))
106 {
107 continue;
108 }
109
110 $storeId = isset($item['storeId']) ? (int)$item['storeId'] : 0;
111 if (!isset($storeIds[$storeId]))
112 {
113 continue;
114 }
115
116 $amount = isset($item['value']['amount']) ? (float)$item['value']['amount'] : 0;
117 $quantityReserved = isset($item['value']['quantityReserved'])
118 ? (float)$item['value']['quantityReserved']
119 : 0
120 ;
121
122 $insertRows[] = [
123 'PRODUCT_ID' => $productId,
124 'STORE_ID' => $storeId,
125 'AMOUNT' => $amount,
126 'QUANTITY_RESERVED' => $quantityReserved,
127 ];
128 }
129
130 if (!$insertRows)
131 {
132 continue;
133 }
134
135 $sqls = $sqlHelper->prepareMergeMultiple(
137 [
138 'PRODUCT_ID',
139 'STORE_ID',
140 ],
141 $insertRows
142 );
143 foreach ($sqls as $sql)
144 {
145 $connection->query($sql);
146 }
147 }
148
149 $allProductIds = array_unique(
150 array_map(
151 \Closure::fromCallable('intval'),
152 array_column($items, 'productId')
153 )
154 );
155 foreach (array_chunk($allProductIds, self::BULK_SAVE_PRODUCTS_CHUNK_SIZE) as $chunkProductIds)
156 {
157 Application::getInstance()->addBackgroundJob(function() use ($chunkProductIds) {
158 \CCatalogStore::recalculateProductsBalances($chunkProductIds);
159 });
160 }
161
162 return true;
163 }
164 //endregion
165
166 protected function getEntityTable()
167 {
168 return new StoreProductTable();
169 }
170
171 protected function checkReadPermissionEntity()
172 {
173 $r = new Result();
174
175 if (!(
176 $this->accessController->check(ActionDictionary::ACTION_CATALOG_READ)
177 || $this->accessController->check(ActionDictionary::ACTION_STORE_VIEW)
178 ))
179 {
180 $r->addError($this->getErrorReadAccessDenied());
181 }
182 return $r;
183 }
184
185 protected function checkPermissionEntity($name, $arguments = [])
186 {
187 $name = mb_strtolower($name); //for ajax mode
188
189 if ($name == 'bulksave')
190 {
191 return $this->checkReadPermissionEntity();
192 }
193 else
194 {
195 $r = parent::checkPermissionEntity($name);
196 }
197
198 return $r;
199 }
200}
$connection
Определения actionsdefinitions.php:38
bulkSaveAction(array $items)
Определения storeproduct.php:40
checkPermissionEntity($name, $arguments=[])
Определения storeproduct.php:185
static getTableName()
Определения storeproduct.php:45
Определения error.php:15
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$name
Определения menu_edit.php:35
trait CheckExists
Определения checkexists.php:8
trait GetAction
Определения getaction.php:8
trait ListAction
Определения listaction.php:9
if(!function_exists("bx_hmac")) $amount
Определения payment.php:30
$items
Определения template.php:224