1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
userbudgetpool.php
См. документацию.
1<?php
2namespace Bitrix\Sale\Internals;
3
4use Bitrix\Main\Localization\Loc;
5use Bitrix\Sale;
6use Bitrix\Main;
7
9{
10 private const STATUS_LOCKED_NOW = 1;
11 private const STATUS_LOCKED_EARLIER = -1;
12 private const STATUS_NOT_LOCKED = 0;
13
14 private $statusLock = self::STATUS_NOT_LOCKED;
15 private $userId;
16
17 protected static $userBudgetPool = [];
18
19 protected $items = [];
20
21 const BUDGET_TYPE_ORDER_CANCEL_PART = 'ORDER_CANCEL_PART';
22 const BUDGET_TYPE_ORDER_UNPAY = 'ORDER_UNPAY';
23 const BUDGET_TYPE_ORDER_PART_RETURN = 'ORDER_PART_RETURN';
24 const BUDGET_TYPE_OUT_CHARGE_OFF = 'OUT_CHARGE_OFF';
25 const BUDGET_TYPE_EXCESS_SUM_PAID = 'EXCESS_SUM_PAID';
26 const BUDGET_TYPE_MANUAL = 'MANUAL';
27 const BUDGET_TYPE_ORDER_PAY = 'ORDER_PAY';
28 const BUDGET_TYPE_ORDER_PAY_PART = 'ORDER_PAY_PART';
29
30 protected function __construct($userId)
31 {
32 $this->userId = $userId;
33 }
34
41 public function add($sum, $budgetType, Sale\Order $order, Sale\Payment $payment = null)
42 {
43 if (!$this->isLocked())
44 {
45 $this->lock();
46 }
47
48 if ($this->isStatusLockEarlier())
49 {
50 return;
51 }
52
53 $fields = [
54 "SUM" => $sum,
55 "CURRENCY" => $order->getCurrency(),
56 "TYPE" => $budgetType,
57 "ORDER" => $order,
58 ];
59
60 if ($payment !== null)
61 {
62 $fields['PAYMENT'] = $payment;
63 }
64
65 $this->items[] = $fields;
66
67 }
68
72 protected function lock(): void
73 {
74 if ($this->statusLock === self::STATUS_NOT_LOCKED)
75 {
77 if (!$connection->lock($this->getUniqLockName()))
78 {
79 $this->statusLock = self::STATUS_LOCKED_EARLIER;
80
81 return;
82 }
83
84 $this->statusLock = self::STATUS_LOCKED_NOW;
85 }
86 }
87
88 private function getUniqLockName() : string
89 {
90 return "user_budget_{$this->userId}";
91 }
92
96 protected function unlock(): void
97 {
98 if ($this->statusLock === self::STATUS_LOCKED_NOW)
99 {
101 $connection->unlock($this->getUniqLockName());
102 unset($connection);
103
104 $this->statusLock = self::STATUS_NOT_LOCKED;
105 }
106 }
107
108 protected function isLocked(): bool
109 {
110 return
111 $this->statusLock === self::STATUS_LOCKED_EARLIER
112 || $this->statusLock === self::STATUS_LOCKED_NOW
113 ;
114 }
115
116 protected function isStatusLockEarlier(): bool
117 {
118 return $this->statusLock === self::STATUS_LOCKED_EARLIER;
119 }
120
124 public function get()
125 {
126 if (isset($this->items))
127 {
128 return $this->items;
129 }
130
131 return false;
132 }
133
139 public function delete($index)
140 {
141 if (isset($this->items) && isset($this->items[$index]))
142 {
143 unset($this->items[$index]);
144 if (count($this->items) === 0)
145 {
146 $this->unlock();
147 }
148
149 return true;
150 }
151
152 return false;
153 }
154
159 public static function getUserBudgetPool($userId)
160 {
161 if (!isset(static::$userBudgetPool[$userId]))
162 {
163 static::$userBudgetPool[$userId] = new static($userId);
164 }
165
166 return static::$userBudgetPool[$userId];
167 }
168
176 public static function addPoolItem(Sale\Order $order, $value, $type, Sale\Payment $payment = null)
177 {
178 if (floatval($value) == 0)
179 return;
180
181 $userId = $order->getUserId();
182 $pool = static::getUserBudgetPool($userId);
183 $pool->add($value, $type, $order, $payment);
184 }
185
190 public static function onUserBudgetSave($userId)
191 {
192 $result = new Sale\Result();
193
194 $pool = static::getUserBudgetPool($userId);
195
196 if ($pool->isStatusLockEarlier())
197 {
198 return $result->addError(
199 new Sale\ResultError(
200 Loc::getMessage('SALE_PROVIDER_USER_BUDGET_LOCKED')
201 )
202 );
203 }
204
205 foreach ($pool->get() as $key => $budgetDat)
206 {
207 $orderId = null;
208 $paymentId = null;
209
210 if (isset($budgetDat['ORDER'])
211 && ($budgetDat['ORDER'] instanceof Sale\OrderBase))
212 {
213 $orderId = $budgetDat['ORDER']->getId();
214 }
215
216 if (isset($budgetDat['PAYMENT'])
217 && ($budgetDat['PAYMENT'] instanceof Sale\Payment))
218 {
219 $paymentId = $budgetDat['PAYMENT']->getId();
220 }
221
222 if (!\CSaleUserAccount::UpdateAccount($userId, $budgetDat['SUM'], $budgetDat['CURRENCY'], $budgetDat['TYPE'], $orderId, '', $paymentId))
223 {
224 $result->addError( new Sale\ResultError(Loc::getMessage("SALE_PROVIDER_USER_BUDGET_".$budgetDat['TYPE']."_ERROR"), "SALE_PROVIDER_USER_BUDGET_".$budgetDat['TYPE']."_ERROR") );
225 }
226
227 $pool->delete($key);
228 }
229
230 return $result;
231 }
232
237 public static function getUserBudgetTransForOrder(Sale\Order $order)
238 {
239 $ignoreTypes = array(
240 static::BUDGET_TYPE_ORDER_PAY
241 );
242 $sumTrans = 0;
243
244 if ($order->getId() > 0)
245 {
246 $resTrans = \CSaleUserTransact::GetList(
247 array("TRANSACT_DATE" => "DESC"),
248 array(
249 "ORDER_ID" => $order->getId(),
250 ),
251 false,
252 false,
253 array("AMOUNT", "CURRENCY", "DEBIT")
254 );
255 while ($transactDat = $resTrans->Fetch())
256 {
257 if ($transactDat['DEBIT'] == "Y")
258 {
259 $sumTrans += $transactDat['AMOUNT'];
260 }
261 else
262 {
263 $sumTrans -= $transactDat['AMOUNT'];
264 }
265 }
266 }
267
268 if ($userBudgetPool = static::getUserBudgetPool($order->getUserId()))
269 {
270 foreach ($userBudgetPool->get() as $userBudgetDat)
271 {
272 if (in_array($userBudgetDat['TYPE'], $ignoreTypes))
273 continue;
274
275 $sumTrans += $userBudgetDat['SUM'];
276 }
277 }
278
279 return $sumTrans;
280 }
281
286 public static function getUserBudgetByOrder(Sale\Order $order)
287 {
288 $budget = static::getUserBudget($order->getUserId(), $order->getCurrency());
289 if ($userBudgetPool = static::getUserBudgetPool($order->getUserId()))
290 {
291 foreach ($userBudgetPool->get() as $userBudgetDat)
292 {
293 $budget += $userBudgetDat['SUM'];
294 }
295 }
296
297 return $budget;
298 }
299
305 public static function getUserBudget($userId, $currency)
306 {
307 $budget = null;
308 if ($userAccount = \CSaleUserAccount::GetByUserId($userId, $currency))
309 {
310 if ($userAccount['LOCKED'] != 'Y')
311 $budget = floatval($userAccount['CURRENT_BUDGET']);
312 }
313
314 return $budget;
315 }
316
317 public function __destruct()
318 {
319 $this->unlock();
320 }
321}
$connection
Определения actionsdefinitions.php:38
$sum
Определения checkout.php:6
$type
Определения options.php:106
static getConnection($name="")
Определения application.php:638
const BUDGET_TYPE_ORDER_CANCEL_PART
Определения userbudgetpool.php:21
add($sum, $budgetType, Sale\Order $order, Sale\Payment $payment=null)
Определения userbudgetpool.php:41
static getUserBudget($userId, $currency)
Определения userbudgetpool.php:305
const BUDGET_TYPE_ORDER_PART_RETURN
Определения userbudgetpool.php:23
static getUserBudgetByOrder(Sale\Order $order)
Определения userbudgetpool.php:286
static getUserBudgetTransForOrder(Sale\Order $order)
Определения userbudgetpool.php:237
static getUserBudgetPool($userId)
Определения userbudgetpool.php:159
static onUserBudgetSave($userId)
Определения userbudgetpool.php:190
static addPoolItem(Sale\Order $order, $value, $type, Sale\Payment $payment=null)
Определения userbudgetpool.php:176
Определения payment.php:19
static UpdateAccount($userID, $sum, $currency, $description="", $orderID=0, $notes="", $paymentId=null)
Определения user.php:589
static GetList($arOrder=array(), $arFilter=array(), $arGroupBy=false, $arNavStartParams=false, $arSelectFields=array())
Определения user_transact.php:32
$orderId
Определения payment.php:5
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
$payment
Определения payment.php:14
$order
Определения payment.php:8
if(empty($signedUserToken)) $key
Определения quickway.php:257
</p ></td >< td valign=top style='border-top:none;border-left:none;border-bottom:solid windowtext 1.0pt;border-right:solid windowtext 1.0pt;padding:0cm 2.0pt 0cm 2.0pt;height:9.0pt'>< p class=Normal align=center style='margin:0cm;margin-bottom:.0001pt;text-align:center;line-height:normal'>< a name=ТекстовоеПоле54 ></a ><?=($taxRate > count( $arTaxList) > 0) ? $taxRate."%"
Определения waybill.php:936
$currency
Определения template.php:266
$fields
Определения yandex_run.php:501