Bitrix-D7
23.9
Загрузка...
Поиск...
Не найдено
upsertstorebatchaction.php
1
<?php
2
3
namespace
Bitrix\Catalog\Document\Action\Store
;
4
5
use
Bitrix\Catalog\Document\Action
;
6
use Bitrix\Catalog\Document\Action\ProductAndStoreInfo;
7
use Bitrix\Catalog\EO_StoreBatch;
8
use
Bitrix\Catalog\Product\Store\CostPriceCalculator
;
9
use
Bitrix\Catalog\StoreBatchDocumentElementTable
;
10
use
Bitrix\Catalog\StoreBatchTable
;
11
use
Bitrix\Main\Config\Option
;
12
use
Bitrix\Main\Error
;
13
use
Bitrix\Main\Loader
;
14
use
Bitrix\Main\Localization\Loc
;
15
use
Bitrix\Main\Result
;
16
20
class
UpsertStoreBatchAction
implements
Action
21
{
22
use
ProductAndStoreInfo
;
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
103
$this->
addDocumentElementBatchBinding
(
104
$batch,
105
$this->amount,
106
$this->purchasingPrice,
107
$this->purchasingCurrency,
108
);
109
110
return
$result;
111
}
112
113
protected
function
getDocumentElementId
(): int
114
{
115
return
$this->documentElementId
;
116
}
117
118
protected
function
loadBatch
(
int
$storeId): ?EO_StoreBatch
119
{
120
return
StoreBatchTable::getList
([
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
156
protected
function
addDocumentElementBatchBinding
(
157
EO_StoreBatch $batch,
158
float
$amount
,
159
float
$purchasingPrice
=
null
,
160
string
$purchasingCurrency
=
null
,
161
): void
162
{
163
StoreBatchDocumentElementTable::add
([
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(
205
$purchasingPrice
,
206
$purchasingCurrency
,
207
$newCurrency
208
);
209
}
210
}
Bitrix\Catalog\Document\Action\Store\UpsertStoreBatchAction
Definition
upsertstorebatchaction.php:21
Bitrix\Catalog\Document\Action\Store\UpsertStoreBatchAction\recalculateBatch
recalculateBatch(EO_StoreBatch $batch, float $amount, float $purchasingPrice, string $purchasingCurrency=null,)
Definition
upsertstorebatchaction.php:172
Bitrix\Catalog\Document\Action\Store\UpsertStoreBatchAction\execute
execute()
Definition
upsertstorebatchaction.php:55
Bitrix\Catalog\Document\Action\Store\UpsertStoreBatchAction\$documentElementId
int $documentElementId
Definition
upsertstorebatchaction.php:27
Bitrix\Catalog\Document\Action\Store\UpsertStoreBatchAction\$purchasingCurrency
string $purchasingCurrency
Definition
upsertstorebatchaction.php:29
Bitrix\Catalog\Document\Action\Store\UpsertStoreBatchAction\__construct
__construct(int $storeId, int $productId, float $amount, int $documentElementId=null, float $purchasingPrice=null, string $purchasingCurrency=null)
Definition
upsertstorebatchaction.php:30
Bitrix\Catalog\Document\Action\Store\UpsertStoreBatchAction\$purchasingPrice
float $purchasingPrice
Definition
upsertstorebatchaction.php:28
Bitrix\Catalog\Document\Action\Store\UpsertStoreBatchAction\$productId
int $productId
Definition
upsertstorebatchaction.php:25
Bitrix\Catalog\Document\Action\Store\UpsertStoreBatchAction\canExecute
canExecute()
Definition
upsertstorebatchaction.php:47
Bitrix\Catalog\Document\Action\Store\UpsertStoreBatchAction\addDocumentElementBatchBinding
addDocumentElementBatchBinding(EO_StoreBatch $batch, float $amount, float $purchasingPrice=null, string $purchasingCurrency=null,)
Definition
upsertstorebatchaction.php:156
Bitrix\Catalog\Document\Action\Store\UpsertStoreBatchAction\$amount
float $amount
Definition
upsertstorebatchaction.php:26
Bitrix\Catalog\Document\Action\Store\UpsertStoreBatchAction\loadBatch
loadBatch(int $storeId)
Definition
upsertstorebatchaction.php:118
Bitrix\Catalog\Document\Action\Store\UpsertStoreBatchAction\getDocumentElementId
getDocumentElementId()
Definition
upsertstorebatchaction.php:113
Bitrix\Catalog\Product\Store\CostPriceCalculator
Definition
costpricecalculator.php:19
Bitrix\Catalog\StoreBatchDocumentElementTable
Definition
storebatchdocumentelementtable.php:28
Bitrix\Catalog\StoreBatchTable
Definition
storebatchtable.php:27
Bitrix\Main\Config\Option
Definition
option.php:15
Bitrix\Main\Error
Definition
error.php:14
Bitrix\Main\Loader
Definition
loader.php:12
Bitrix\Main\Localization\Loc
Definition
loc.php:11
Bitrix\Main\Localization\Loc\getMessage
static getMessage($code, $replace=null, $language=null)
Definition
loc.php:29
Bitrix\Main\ORM\Data\DataManager\getList
static getList(array $parameters=array())
Definition
datamanager.php:441
Bitrix\Main\ORM\Data\DataManager\add
static add(array $data)
Definition
datamanager.php:874
Bitrix\Main\ORM\Data\Result
Definition
result.php:16
Bitrix\Main\Result
Definition
result.php:14
Bitrix\Catalog\Document\Action
Definition
action.php:11
Bitrix\Catalog\Document\Action\ProductAndStoreInfo
trait ProductAndStoreInfo
Definition
productandstoreinfo.php:15
Bitrix\Catalog\Document\Action\Store
Definition
basestorequantityaction.php:3
Bitrix\Catalog\Document\Action
Bitrix\Catalog\Document\Action\getProductId
getProductId()
Definition
productandstoreinfo.php:41
modules
catalog
lib
document
action
store
upsertstorebatchaction.php
Создано системой
1.10.0