Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
movestorebatchaction.php
1<?php
3
4use Bitrix\Catalog\EO_StoreDocumentElement;
13
18{
20 private int $storeFromId;
21
22 private int $storeToId;
23 private int $storeId;
24
25 private ?EO_StoreDocumentElement $storeDocumentElement;
26 public function __construct(
27 int $storeFromId,
28 int $storeToId,
29 int $productId,
30 float $amount,
31 int $documentElementId = null,
32 )
33 {
34 $this->storeFromId = $storeFromId;
35 $this->storeToId = $storeToId;
36
37 parent::__construct($storeToId, $productId, $amount, $documentElementId);
38
40 {
41 $this->storeDocumentElement = StoreDocumentElementTable::getList([
42 'filter' => [
43 '=ID' => $documentElementId,
44 ],
45 'limit' => 1
46 ])
47 ->fetchObject()
48 ;
49 }
50 }
51
52 public function canExecute(): Result
53 {
54 $this->productId = $this->storeDocumentElement->getElementId();
55 $this->storeId = $this->storeDocumentElement->getStoreFrom();
56
57 return $this->checkStoreAmount($this->storeDocumentElement);
58 }
59
63 public function execute(): Result
64 {
65 $result = new Result();
66
67 $distributor = new DeductDocument(
68 new BatchManager($this->productId),
69 $this->storeDocumentElement
70 );
71
72 $writeOffResult = $distributor->writeOff($this->amount);
73
74 if (!$writeOffResult->isSuccess())
75 {
76 return $writeOffResult;
77 }
78
79 $writeOffBindings = StoreBatchDocumentElementTable::getList([
80 'filter' => [
81 '=DOCUMENT_ELEMENT_ID' => $this->storeDocumentElement->getId(),
82 '<AMOUNT' => 0,
83 ],
84 ]);
85
86 while ($writeOffBinding = $writeOffBindings->fetchObject())
87 {
88 $batch = null;
89 $moveAmount = abs($writeOffBinding->getAmount());
90 if (CostPriceCalculator::getMethod() === CostPriceCalculator::METHOD_AVERAGE)
91 {
92 $batch = $this->loadBatch($this->storeToId);
93
94 if ($batch !== null)
95 {
96 $this->recalculateBatch(
97 $batch,
98 $moveAmount,
99 $writeOffBinding->getBatchPrice(),
100 $writeOffBinding->getBatchCurrency(),
101 );
102 $resultUpdate = $batch->save();
103 if (!$resultUpdate->isSuccess())
104 {
105 $result->addError(
106 new Error(Loc::getMessage('CATALOG_STORE_DOCS_ERR_CANT_MOVE_STORE_PRODUCT'))
107 );
108
109 return $result;
110 }
111 }
112 }
113
114 if ($batch === null)
115 {
116 $batch = $this->createBatch(
117 $this->storeToId,
118 $moveAmount,
119 $writeOffBinding->getBatchPrice(),
120 $writeOffBinding->getBatchCurrency(),
121 );
122 }
123
124 if ($batch === null)
125 {
126 $result->addError(
127 new Error(Loc::getMessage('CATALOG_STORE_DOCS_ERR_CANT_MOVE_STORE_PRODUCT'))
128 );
129
130 return $result;
131 }
132
134 $batch,
135 $moveAmount,
136 $writeOffBinding->getBatchPrice(),
137 $writeOffBinding->getBatchCurrency(),
138 );
139 }
140
141 return $result;
142 }
143}
__construct(int $storeFromId, int $storeToId, int $productId, float $amount, int $documentElementId=null,)
recalculateBatch(EO_StoreBatch $batch, float $amount, float $purchasingPrice, 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())
checkStoreAmount(EO_StoreDocumentElement $storeDocumentElement=null)