Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
productinfo.php
1<?php
2
4
10
11final class ProductInfo
12{
13 private static array $productNameList = [];
14
15 private int $productId;
16 private float $quantity;
17 private static array $productPrice;
18
19 public function __construct(int $productId, float $quantity)
20 {
21 $this->productId = $productId;
22 $this->quantity = $quantity;
23 }
24
25 public function getProductId(): int
26 {
27 return $this->productId;
28 }
29
30 public function getQuantity(): float
31 {
32 return $this->quantity;
33 }
34
41 public function getPrice(): float
42 {
43 if (!isset(self::$productPrice[$this->productId]))
44 {
45 self::initBasePrice($this->productId);
46 }
47
48 if (!isset(self::$productPrice[$this->productId]))
49 {
50 self::$productPrice[$this->productId] = 0.0;
51 }
52
53 return self::$productPrice[$this->productId];
54 }
55
64 public static function initBasePrice(int ...$productId): void
65 {
66 $defaultCurrency = CurrencyManager::getBaseCurrency();
67 $productsData = ProductTable::getList([
68 'select' => [
69 'ID',
70 'PURCHASING_PRICE',
71 'PURCHASING_CURRENCY',
72 'PURCHASING_CURRENCY_AMOUNT' => 'CURRENCY_TABLE.CURRENT_BASE_RATE',
73 ],
74 'filter' => [
75 '=ID' => $productId,
76 ],
77 'runtime' => [
78 (new Reference(
79 'CURRENCY_TABLE',
80 CurrencyTable::class,
81 Join::on('this.PURCHASING_CURRENCY', 'ref.CURRENCY')
82 ))->configureJoinType(Join::TYPE_LEFT),
83 ],
84 ])->fetchAll();
85
86 foreach ($productsData as $product)
87 {
88 $productPrice = (float)$product['PURCHASING_PRICE'];
89 if ($product['PURCHASING_CURRENCY'] !== $defaultCurrency)
90 {
91 $defaultCurrencyAmount = (float)\CCurrency::getCurrency($defaultCurrency)['CURRENT_BASE_RATE'];
92 $currentCurrencyAmount = (float)$product['PURCHASING_CURRENCY_AMOUNT'];
93
94 $productPrice *= $currentCurrencyAmount;
95 $productPrice /= $defaultCurrencyAmount;
96 }
97
98 self::setPrice($product['ID'], $productPrice);
99 }
100 }
101
109 public static function setPrice(int $productId, float $productPrice): void
110 {
111 self::$productPrice[$productId] = $productPrice;
112 }
113}
static setPrice(int $productId, float $productPrice)
static getList(array $parameters=array())