Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
updateproductpricesaction.php
1<?php
2
4
13
21{
22 private int $productId;
23 private ?float $purchasePrice;
24 private ?string $purchasePriceCurrency;
25 private ?float $basePrice;
26 private ?string $basePriceCurrency;
27
35 public function __construct(
36 int $productId,
37 ?float $purchasePrice,
38 ?string $purchasePriceCurrency,
39 ?float $basePrice = null,
40 ?string $basePriceCurrency = null
41 )
42 {
43 $this->productId = $productId;
44 $this->purchasePrice = $purchasePrice;
45 $this->purchasePriceCurrency = $purchasePriceCurrency;
46 $this->basePrice = $basePrice;
47 $this->basePriceCurrency = $basePriceCurrency;
48 }
49
58 private function getDefaultCurrency(): string
59 {
60 static $currency;
61
62 if ($currency)
63 {
64 return $currency;
65 }
66 elseif (Loader::includeModule('currency'))
67 {
68 $currency = CurrencyManager::getBaseCurrency();
69 }
70 else
71 {
72 $currency = LANGUAGE_ID === 'ru' ? 'RUB' : 'EUR';
73 }
74
75 return $currency;
76 }
77
83 private function getBasePriceRowId(): ?int
84 {
85 $row = PriceTable::getRow([
86 'select' => [
87 'ID',
88 ],
89 'filter' => [
90 '=PRODUCT_ID' => $this->productId,
91 '=CATALOG_GROUP_ID' => $this->getBasePriceGroupId(),
92 ],
93 'order' => [
94 'ID' => 'asc',
95 ],
96 ]);
97 return $row['ID'] ?? null;
98 }
99
105 private function getBasePriceGroupId(): ?int
106 {
108 }
109
113 public function canExecute(): Result
114 {
115 return new Result();
116 }
117
121 public function execute(): Result
122 {
123 $result = new Result();
124
125 if (isset($this->purchasePrice))
126 {
127 $saveResult = Product::update($this->productId, [
128 'PURCHASING_PRICE' => $this->purchasePrice ?: null, // if price is 0.0 - clear value
129 'PURCHASING_CURRENCY' => $this->purchasePriceCurrency ?? $this->getDefaultCurrency(),
130 ]);
131 if (!$saveResult->isSuccess())
132 {
133 $result->addErrors(
134 $saveResult->getErrors()
135 );
136 }
137 }
138
139 if (isset($this->basePrice))
140 {
141 $basePriceRowId = $this->getBasePriceRowId();
142 if ($basePriceRowId)
143 {
144 $saveResult = Price::update($basePriceRowId, [
145 'PRICE' => $this->basePrice,
146 'CURRENCY' => $this->basePriceCurrency,
147 ]);
148 }
149 else
150 {
151 $saveResult = Price::add([
152 'PRODUCT_ID' => $this->productId,
153 'CATALOG_GROUP_ID' => $this->getBasePriceGroupId(),
154 'PRICE' => $this->basePrice,
155 'CURRENCY' => $this->basePriceCurrency ?? $this->getDefaultCurrency(),
156 ]);
157 }
158
159 if (!$saveResult->isSuccess())
160 {
161 $result->addErrors(
162 $saveResult->getErrors()
163 );
164 }
165 }
166
167 return $result;
168 }
169}
__construct(int $productId, ?float $purchasePrice, ?string $purchasePriceCurrency, ?float $basePrice=null, ?string $basePriceCurrency=null)
static getBasePriceTypeId()
Definition group.php:248
static update($id, array $data)
Definition entity.php:229
static add(array $data)
Definition entity.php:150
static getRow(array $parameters)