Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
upsertstorebatchaction.php
1<?php
2
4
6use Bitrix\Catalog\Document\Action\ProductAndStoreInfo;
7use Bitrix\Catalog\EO_StoreBatch;
16
21{
23
24 private int $storeId;
25 protected int $productId;
26 protected float $amount;
27 protected ?int $documentElementId;
28 protected ?float $purchasingPrice;
29 protected ?string $purchasingCurrency;
30 public function __construct(
31 int $storeId,
32 int $productId,
33 float $amount,
34 int $documentElementId = null,
35 float $purchasingPrice = null,
36 string $purchasingCurrency = null
37 )
38 {
39 $this->storeId = $storeId;
40 $this->productId = $productId;
41 $this->amount = $amount;
42 $this->documentElementId = $documentElementId;
43 $this->purchasingPrice = $purchasingPrice;
44 $this->purchasingCurrency = $purchasingCurrency;
45 }
46
47 public function canExecute(): Result
48 {
49 return new Result();
50 }
51
55 public function execute(): Result
56 {
57 $result = new Result();
58
59 $batch = null;
60 if (CostPriceCalculator::getMethod() === CostPriceCalculator::METHOD_AVERAGE)
61 {
62 $batch = $this->loadBatch($this->storeId);
63
64 if ($batch !== null)
65 {
66 $this->recalculateBatch(
67 $batch,
68 $this->amount,
69 (float)$this->purchasingPrice,
70 $this->purchasingCurrency,
71 );
72 $resultUpdate = $batch->save();
73 if (!$resultUpdate->isSuccess())
74 {
75 $result->addError(
76 new Error(Loc::getMessage('CATALOG_STORE_DOCS_ERR_CANT_UPDATE_STORE_PRODUCT'))
77 );
78
79 return $result;
80 }
81 }
82 }
83
84 if ($batch === null)
85 {
86 $batch = $this->createBatch(
87 $this->storeId,
88 $this->amount,
89 $this->purchasingPrice,
90 $this->purchasingCurrency,
91 );
92 }
93
94 if ($batch === null)
95 {
96 $result->addError(
97 new Error(Loc::getMessage('CATALOG_STORE_DOCS_ERR_CANT_UPDATE_STORE_PRODUCT'))
98 );
99
100 return $result;
101 }
102
104 $batch,
105 $this->amount,
106 $this->purchasingPrice,
107 $this->purchasingCurrency,
108 );
109
110 return $result;
111 }
112
113 protected function getDocumentElementId(): int
114 {
116 }
117
118 protected function loadBatch(int $storeId): ?EO_StoreBatch
119 {
121 'filter' => [
122 'STORE_ID' => $storeId,
123 'ELEMENT_ID' => $this->getProductId(),
124 ],
125 'limit' => 1,
126 ])
127 ->fetchObject()
128 ;
129 }
130
131 protected function createBatch(
132 int $storeId,
133 float $amount,
134 float $purchasingPrice = null,
135 string $purchasingCurrency = null,
136 ): ?EO_StoreBatch
137 {
138 $resultAdd = StoreBatchTable::add([
139 'STORE_ID' => $storeId,
140 'ELEMENT_ID' => $this->getProductId(),
141 'AVAILABLE_AMOUNT' => $amount,
142 'PURCHASING_PRICE' => $purchasingPrice,
143 'PURCHASING_CURRENCY' => $purchasingCurrency,
144 ]);
145
146 if (!$resultAdd->isSuccess())
147 {
148 return null;
149 }
151 $batch = $resultAdd->getObject();
152
153 return $batch;
154 }
155
157 EO_StoreBatch $batch,
158 float $amount,
159 float $purchasingPrice = null,
160 string $purchasingCurrency = null,
161 ): void
162 {
164 'DOCUMENT_ELEMENT_ID' => $this->getDocumentElementId(),
165 'AMOUNT' => $amount,
166 'PRODUCT_BATCH_ID' => $batch->getId(),
167 'BATCH_PRICE' => $purchasingPrice,
168 'BATCH_CURRENCY' => $purchasingCurrency,
169 ]);
170 }
171
172 protected function recalculateBatch(
173 EO_StoreBatch $batch,
174 float $amount,
175 float $purchasingPrice,
176 string $purchasingCurrency = null,
177 ): void
178 {
179 if ($purchasingCurrency && $purchasingCurrency !== $batch->getPurchasingCurrency())
180 {
181 $purchasingPrice = $this->convertPrice($purchasingPrice, $purchasingCurrency, $batch->getPurchasingCurrency());
182 }
183
184 $precision = (int)Option::get('sale', 'value_precision', 2);
185 $newAvailableAmount = $batch->getAvailableAmount() + $amount;
186 $newPurchasingPrice = ($batch->getPurchasingPrice() * $batch->getAvailableAmount() + $purchasingPrice * $amount) / $newAvailableAmount;
187 $newPurchasingPrice = round($newPurchasingPrice, $precision);
188
189 $batch->setAvailableAmount($newAvailableAmount);
190 $batch->setPurchasingPrice($newPurchasingPrice);
191 }
192
193 private function convertPrice(
194 string $purchasingPrice,
195 string $purchasingCurrency,
196 string $newCurrency
197 ): float
198 {
199 if (!Loader::includeModule('currency'))
200 {
201 return $purchasingPrice;
202 }
203
204 return \CCurrencyRates::convertCurrency(
207 $newCurrency
208 );
209 }
210}
recalculateBatch(EO_StoreBatch $batch, float $amount, float $purchasingPrice, string $purchasingCurrency=null,)
__construct(int $storeId, int $productId, float $amount, int $documentElementId=null, float $purchasingPrice=null, string $purchasingCurrency=null)
addDocumentElementBatchBinding(EO_StoreBatch $batch, float $amount, float $purchasingPrice=null, string $purchasingCurrency=null,)
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static getList(array $parameters=array())