Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
correctioncheck.php
1<?php
2
3namespace Bitrix\Sale\Cashbox;
4
7
12abstract class CorrectionCheck extends AbstractCheck
13{
14 public const CORRECTION_TYPE_SELF = 'self';
15 public const CORRECTION_TYPE_INSTRUCTION = 'instruction';
16
17 private const CHECK_CURRENCY_RUB = 'RUB';
18
19 protected $correction = [];
20
21 public function __construct()
22 {
23 parent::__construct();
24
25 $this->fields['ENTITY_REGISTRY_TYPE'] = Registry::REGISTRY_TYPE_ORDER;
26 $this->fields['CURRENCY'] = self::CHECK_CURRENCY_RUB;
27 }
28
33 public function save()
34 {
35 $isNew = (int)$this->fields['ID'] === 0;
36
37 $result = parent::save();
38 if (!$result->isSuccess())
39 {
40 return $result;
41 }
42
43 if ($isNew)
44 {
45 $r = Internals\CashboxCheckCorrectionTable::add([
46 'CHECK_ID' => $this->fields['ID'],
47 'CORRECTION_TYPE' => $this->correction['CORRECTION_TYPE'],
48 'DOCUMENT_NUMBER' => $this->correction['DOCUMENT_NUMBER'],
49 'DOCUMENT_DATE' => $this->correction['DOCUMENT_DATE'],
50 'DESCRIPTION' => $this->correction['DESCRIPTION'],
51 'CORRECTION_PAYMENT' => $this->correction['CORRECTION_PAYMENT'],
52 'CORRECTION_VAT' => $this->correction['CORRECTION_VAT'],
53 ]);
54
55 if (!$r->isSuccess())
56 {
57 $result->addErrors($r->getErrors());
58 }
59 }
60
61 return $result;
62 }
63
64 public function getDataForCheck()
65 {
66 $result = [
67 'type' => static::getType(),
68 'unique_id' => $this->getField('ID'),
69 'date_create' => new Main\Type\DateTime(),
70 'calculated_sign' => static::getCalculatedSign()
71 ];
72
73 $data = $this->extractData();
74 if ($data)
75 {
76 $result['correction_info'] = [
77 'type' => $data['CORRECTION_TYPE'],
78 'document_number' => $data['DOCUMENT_NUMBER'],
79 'document_date' => $data['DOCUMENT_DATE'],
80 'description' => $data['DESCRIPTION'],
81 'total_sum' => $data['TOTAL_SUM'],
82 ];
83
84 if (isset($data['PAYMENTS']))
85 {
86 $result['payments'] = [];
87
88 foreach ($data['PAYMENTS'] as $payment)
89 {
90 $result['payments'][] = [
91 'type' => $payment['TYPE'],
92 'sum' => $payment['SUM'],
93 ];
94 }
95 };
96
97 if (isset($data['VATS']))
98 {
99 $result['vats'] = [];
100
101 foreach ($data['VATS'] as $vat)
102 {
103 $result['vats'][] = [
104 'type' => $vat['TYPE'],
105 'sum' => $vat['SUM'],
106 ];
107 }
108 }
109 }
110
111 return $result;
112 }
113
114 protected function extractDataInternal()
115 {
116 $result = [
117 'CORRECTION_TYPE' => $this->correction['CORRECTION_TYPE'],
118 'DOCUMENT_NUMBER' => $this->correction['DOCUMENT_NUMBER'],
119 'DOCUMENT_DATE' => $this->correction['DOCUMENT_DATE'],
120 'DESCRIPTION' => $this->correction['DESCRIPTION'],
121 'PAYMENTS' => $this->correction['CORRECTION_PAYMENT'],
122 'TOTAL_SUM' => 0
123 ];
124
125 if ($this->correction['CORRECTION_VAT'])
126 {
127 $result['VATS'] = [];
128
129 foreach ($this->correction['CORRECTION_VAT'] as $vat)
130 {
131 $result['VATS'][] = [
132 'TYPE' => $this->getVatIdByVatRate($vat['TYPE']),
133 'SUM' => $vat['SUM'],
134 ];
135 }
136 }
137
138 if ($this->correction['CORRECTION_PAYMENT'])
139 {
140 foreach ($this->correction['CORRECTION_PAYMENT'] as $payment)
141 {
142 $result['TOTAL_SUM'] += $payment['SUM'];
143 }
144 }
145
146 return $result;
147 }
148
149 public function setAvailableCashbox(array $cashboxList)
150 {
151 foreach ($cashboxList as $item)
152 {
153 $cashbox = Cashbox::create($item);
154 if (!$cashbox || !$cashbox->isCorrection())
155 {
156 throw new Main\SystemException('Cashbox '.$cashbox::getName().' is not supported correction check');
157 }
158 }
159
160 parent::setAvailableCashbox($cashboxList);
161 }
162
168 public function setCorrectionField($name, $value)
169 {
170 if (!$this->isCorrectionFieldAvailable($name))
171 {
172 throw new Main\ArgumentException('Incorrect field '.$name);
173 }
174
175 if ($name === 'DOCUMENT_DATE')
176 {
177 $value = new Main\Type\Date($value);
178 }
179
180 $this->correction[$name] = $value;
181 }
182
188 {
189 foreach ($fields as $name => $value)
190 {
191 $this->setCorrectionField($name, $value);
192
193 if ($name === 'CORRECTION_PAYMENT')
194 {
195 $this->fields['SUM'] = $this->calculateSumByPayments($value);
196 }
197 }
198 }
199
200 private function calculateSumByPayments(array $payments)
201 {
202 $result = 0;
203
204 foreach ($payments as $item)
205 {
206 $result += $item['SUM'];
207 }
208
209 return $result;
210 }
211
218 public function getCorrectionFields()
219 {
220 if ($this->correction)
221 {
222 return $this->correction;
223 }
224
225 if ($this->getField('ID') > 0)
226 {
227 $dbRes = Internals\CashboxCheckCorrectionTable::getList([
228 'select' => static::getAvailableCorrectionFields(),
229 'filter' => [
230 '=CHECK_ID' => $this->getField('ID')
231 ]
232 ]);
233 if ($data = $dbRes->fetch())
234 {
235 return $data;
236 }
237 }
238
239 return [];
240 }
241
245 private function getAvailableCorrectionFields()
246 {
247 $fields = array_keys(Internals\CashboxCheckCorrectionTable::getMap());
248
249 return array_filter(
250 $fields,
251 function ($name)
252 {
253 return !in_array($name, ['CHECK', 'ID', 'CHECK_ID']);
254 }
255 );
256 }
257
262 private function isCorrectionFieldAvailable($name)
263 {
264 $fields = $this->getAvailableCorrectionFields();
265
266 return in_array($name, $fields);
267 }
268
272 public static function getSupportedEntityType()
273 {
274 return static::SUPPORTED_ENTITY_TYPE_NONE;
275 }
276}
create()