Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
StoreDocumentProductPositionRepository.php
1<?php
2
4
12
22{
23 protected const PRODUCT_PRICE_TYPE = 'PURCHASING_PRICE';
24
26 private static $instance;
27
28 private array $documentProductPositionListCollection = [];
29 private int $catalogId;
30
35 {
36 if (is_null(static::$instance))
37 {
38 static::$instance = new static();
39 }
40
41 return static::$instance;
42 }
43
51 public function getList(int $documentId, int $limit = 10): array
52 {
53 $productPositionList = $this->fetchDocumentProductPositionList($documentId);
54 return array_slice($productPositionList, 0, $limit);
55 }
56
63 public function getCount(int $documentId): int
64 {
65 return count($this->fetchDocumentProductPositionList($documentId));
66 }
67
68
69 private function fetchDocumentProductPositionList(int $documentId): array
70 {
71 if (!Loader::includeModule('sale'))
72 {
73 return [];
74 }
75
76 if (!empty($this->documentProductPositionListCollection[$documentId]))
77 {
78 return $this->documentProductPositionListCollection[$documentId];
79 }
80
81 $documentProductListData = \CCatalogStoreDocsElement::getList(
82 ['ID' => 'ASC'],
83 ['DOC_ID' => $documentId],
84 false,
85 false,
86 ['ELEMENT_ID', 'ELEMENT_NAME', 'AMOUNT', 'PURCHASING_PRICE', 'BASE_PRICE']
87 );
88
89 $documentProductList = [];
90 while ($product = $documentProductListData->Fetch())
91 {
92 $documentProductList[] = $this->formProductPositionData($product);
93 }
94
95 $this->documentProductPositionListCollection[$documentId] = $documentProductList;
96 return $documentProductList;
97 }
98
99 private function formProductPositionData(array $product): array
100 {
101 $productPositionData = [
102 'PRODUCT_NAME' => $product['ELEMENT_NAME'],
103 'SUM' => PriceMaths::roundPrecision((float)$product[self::PRODUCT_PRICE_TYPE] * (float)$product['AMOUNT']),
104 ];
105
106 $productId = $product['ELEMENT_ID'];
107 $productVariation = ServiceContainer::getRepositoryFacade()->loadVariation($product['ELEMENT_ID']);
108 if ($productVariation)
109 {
110 $image = $productVariation->getFrontImageCollection()->getFrontImage();
111 $productPositionData['PHOTO_URL'] = $image ? $image->getSource() : null;
112 $productPositionData['VARIATION_INFO'] = PropertyValue::getSkuPropertyDisplayValues($productVariation);
113
114 if (!State::isProductCardSliderEnabled())
115 {
116 $productId = $productVariation->getParent() ? $productVariation->getParent()->getId() : $productId;
117 }
118 }
119
120 $productPositionData['URL'] = $this->buildPositionUrl($productId);
121
122 return $productPositionData;
123 }
124
125 private function buildPositionUrl(int $productId): string
126 {
127 $urlBuilder = BuilderManager::getInstance()->getBuilder(InventoryBuilder::TYPE_ID);
128 if ($urlBuilder)
129 {
130 $urlBuilder->setIblockId($this->getCatalogId());
131 return $urlBuilder->getElementDetailUrl($productId);
132 }
133
134 return '';
135 }
136
137 private function getCatalogId(): int
138 {
139 if (!isset($this->catalogId))
140 {
141 $this->catalogId = 0;
142 if (\Bitrix\Main\Loader::includeModule('crm'))
143 {
144 $this->catalogId = \Bitrix\Crm\Product\Catalog::getDefaultId() ?? 0;
145 }
146 }
147
148 return $this->catalogId;
149 }
150}
static getSkuPropertyDisplayValues(BaseSku $sku)
static roundPrecision($value)