Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
prepaymentcheck.php
1<?php
2
3namespace Bitrix\Sale\Cashbox;
4
8
9Main\Localization\Loc::loadMessages(__FILE__);
10
16{
20 public static function getType()
21 {
22 return 'prepayment';
23 }
24
29 public static function getCalculatedSign()
30 {
31 return static::CALCULATED_SIGN_INCOME;
32 }
33
37 public static function getName()
38 {
39 return Main\Localization\Loc::getMessage('SALE_CASHBOX_PREPAYMENT_NAME');
40 }
41
45 public static function getSupportedEntityType()
46 {
47 return static::SUPPORTED_ENTITY_TYPE_PAYMENT;
48 }
49
53 public static function getSupportedRelatedEntityType()
54 {
55 return static::SUPPORTED_ENTITY_TYPE_SHIPMENT;
56 }
57
69 protected function extractDataInternal()
70 {
71 $result = parent::extractDataInternal();
72
73 $result = $this->correlatePrices($result);
74
75 foreach ($result['PRODUCTS'] as $i => $item)
76 {
77 $result['PRODUCTS'][$i]['PAYMENT_OBJECT'] = static::PAYMENT_OBJECT_PAYMENT;
78 }
79
80 if (!empty($result['DELIVERY']) && \is_array($result['DELIVERY']))
81 {
82 foreach ($result['DELIVERY'] as $i => $item)
83 {
84 $result['DELIVERY'][$i]['PAYMENT_OBJECT'] = static::PAYMENT_OBJECT_PAYMENT;
85 }
86 }
87
88 return $result;
89 }
90
91 protected function needPrintMarkingCode($basketItem) : bool
92 {
93 return false;
94 }
95
101 private function correlatePrices($result)
102 {
103 $paymentSum = 0;
104 foreach ($result['PAYMENTS'] as $payment)
105 {
106 $paymentSum += $payment['SUM'];
107 }
108
110 $order = $result['ORDER'];
111
112 $rate = $paymentSum / $order->getPrice();
113
114 $countProductPositions = \count($result['PRODUCTS']);
115 $countDeliveryPositions = $result['DELIVERY'] ? \count($result['DELIVERY']) : 0;
116
117 if ($countDeliveryPositions === 0)
118 {
119 $totalSum = 0;
120 for ($i = 0; $i < $countProductPositions - 1; $i++)
121 {
122 $sum = PriceMaths::roundPrecision($result['PRODUCTS'][$i]['SUM'] * $rate);
123 $totalSum += $sum;
124 $result['PRODUCTS'][$i]['SUM'] = $sum;
125
126 $price = PriceMaths::roundPrecision($sum / $result['PRODUCTS'][$i]['QUANTITY']);
127 $result['PRODUCTS'][$i]['BASE_PRICE'] = $result['PRODUCTS'][$i]['PRICE'] = $price;
128
129
130 if (isset($result['PRODUCTS'][$i]['DISCOUNT']))
131 {
132 unset($result['PRODUCTS'][$i]['DISCOUNT']);
133 }
134 }
135
136 if (isset($result['PRODUCTS']))
137 {
138 $lastElement = $countProductPositions - 1;
139 $result['PRODUCTS'][$lastElement]['SUM'] = PriceMaths::roundPrecision($paymentSum - $totalSum);
140 $price = PriceMaths::roundPrecision($result['PRODUCTS'][$lastElement]['SUM'] / $result['PRODUCTS'][$lastElement]['QUANTITY']);
141 $result['PRODUCTS'][$lastElement]['BASE_PRICE'] = $result['PRODUCTS'][$lastElement]['PRICE'] = $price;
142
143 if (isset($result['PRODUCTS'][$lastElement]['DISCOUNT']))
144 {
145 unset($result['PRODUCTS'][$lastElement]['DISCOUNT']);
146 }
147 }
148 }
149 else
150 {
151 $totalSum = 0;
152 for ($i = 0; $i < $countProductPositions; $i++)
153 {
154 $sum = PriceMaths::roundPrecision($result['PRODUCTS'][$i]['SUM'] * $rate);
155 $totalSum += $sum;
156 $result['PRODUCTS'][$i]['SUM'] = $sum;
157
158 $price = PriceMaths::roundPrecision($sum / $result['PRODUCTS'][$i]['QUANTITY']);
159 $result['PRODUCTS'][$i]['BASE_PRICE'] = $result['PRODUCTS'][$i]['PRICE'] = $price;
160
161 if (isset($result['PRODUCTS'][$i]['DISCOUNT']))
162 {
163 unset($result['PRODUCTS'][$i]['DISCOUNT']);
164 }
165 }
166
167 if ($countDeliveryPositions === 1)
168 {
169 $result['DELIVERY'][0]['SUM'] = PriceMaths::roundPrecision($paymentSum - $totalSum);
170 $price = PriceMaths::roundPrecision($result['DELIVERY'][0]['SUM'] / $result['DELIVERY'][0]['QUANTITY']);
171 $result['DELIVERY'][0]['BASE_PRICE'] = $result['DELIVERY'][0]['PRICE'] = $price;
172
173 if (isset($result['DELIVERY'][0]['DISCOUNT']))
174 {
175 unset($result['DELIVERY'][0]['DISCOUNT']);
176 }
177 }
178 else
179 {
180 for ($i = 0; $i < $countDeliveryPositions - 1; $i++)
181 {
182 $sum = PriceMaths::roundPrecision($result['DELIVERY'][$i]['SUM'] * $rate);
183 $totalSum += $sum;
184 $result['DELIVERY'][$i]['SUM'] = $sum;
185
186 $price = PriceMaths::roundPrecision($sum / $result['DELIVERY'][$i]['QUANTITY']);
187 $result['DELIVERY'][$i]['BASE_PRICE'] = $result['DELIVERY'][$i]['PRICE'] = $price;
188
189 if (isset($result['DELIVERY'][$i]['DISCOUNT']))
190 {
191 unset($result['DELIVERY'][$i]['DISCOUNT']);
192 }
193 }
194
195 if (isset($result['DELIVERY']))
196 {
197 $lastElement = $countDeliveryPositions - 1;
198 $result['DELIVERY'][$lastElement]['SUM'] = PriceMaths::roundPrecision($paymentSum - $totalSum);
199 $price = PriceMaths::roundPrecision($result['DELIVERY'][$lastElement]['SUM'] / $result['DELIVERY'][$lastElement]['QUANTITY']);
200 $result['DELIVERY'][$lastElement]['BASE_PRICE'] = $result['DELIVERY'][$lastElement]['PRICE'] = $price;
201
202 if (isset($result['DELIVERY'][$lastElement]['DISCOUNT']))
203 {
204 unset($result['DELIVERY'][$lastElement]['DISCOUNT']);
205 }
206 }
207 }
208 }
209
210 return $result;
211 }
212}
static roundPrecision($value)