12 private const STATUS_LOCKED_NOW = 1;
13 private const STATUS_LOCKED_EARLIER = -1;
14 private const STATUS_NOT_LOCKED = 0;
16 private $statusLock = self::STATUS_NOT_LOCKED;
34 $this->userId = $userId;
58 "CURRENCY" => $order->getCurrency(),
59 "TYPE" => $budgetType,
63 if ($payment !==
null)
65 $fields[
'PAYMENT'] = $payment;
68 $this->items[] = $fields;
77 if ($this->statusLock === self::STATUS_NOT_LOCKED)
79 $connection = Main\Application::getConnection();
80 $name = $connection->getSqlHelper()->forSql($this->getUniqLockName());
81 $dbRes = $connection->query(
"SELECT GET_LOCK('{$name}', 0) as L");
82 $result = $dbRes->fetch();
83 if ($result[
'L'] ===
'0')
85 $this->statusLock = self::STATUS_LOCKED_EARLIER;
90 $this->statusLock = self::STATUS_LOCKED_NOW;
94 private function getUniqLockName() : string
96 return "user_budget_{$this->userId}";
105 if ($this->statusLock === self::STATUS_LOCKED_NOW)
107 $connection = Main\Application::getConnection();
108 $name = $connection->getSqlHelper()->forSql($this->getUniqLockName());
109 $connection->query(
"SELECT RELEASE_LOCK('{$name}')");
111 $this->statusLock = self::STATUS_NOT_LOCKED;
118 $this->statusLock === self::STATUS_LOCKED_EARLIER
119 || $this->statusLock === self::STATUS_LOCKED_NOW
125 return $this->statusLock === self::STATUS_LOCKED_EARLIER;
131 public function get()
133 if (isset($this->items))
146 public function delete($index)
148 if (isset($this->items) && isset($this->items[$index]))
150 unset($this->items[$index]);
151 if (count($this->items) === 0)
168 if (!isset(static::$userBudgetPool[$userId]))
170 static::$userBudgetPool[$userId] =
new static($userId);
173 return static::$userBudgetPool[$userId];
185 if (floatval($value) == 0)
188 $userId = $order->getUserId();
189 $pool = static::getUserBudgetPool($userId);
190 $pool->add($value, $type, $order, $payment);
201 $pool = static::getUserBudgetPool($userId);
203 if ($pool->isStatusLockEarlier())
205 return $result->addError(
212 foreach ($pool->get() as $key => $budgetDat)
217 if (isset($budgetDat[
'ORDER'])
220 $orderId = $budgetDat[
'ORDER']->getId();
223 if (isset($budgetDat[
'PAYMENT'])
224 && ($budgetDat[
'PAYMENT'] instanceof
Sale\
Payment))
226 $paymentId = $budgetDat[
'PAYMENT']->getId();
229 if (!\CSaleUserAccount::UpdateAccount($userId, $budgetDat[
'SUM'], $budgetDat[
'CURRENCY'], $budgetDat[
'TYPE'], $orderId,
'', $paymentId))
231 $result->addError(
new Sale\
ResultError(
Loc::getMessage(
"SALE_PROVIDER_USER_BUDGET_".$budgetDat[
'TYPE'].
"_ERROR"),
"SALE_PROVIDER_USER_BUDGET_".$budgetDat[
'TYPE'].
"_ERROR") );
246 $ignoreTypes = array(
247 static::BUDGET_TYPE_ORDER_PAY
251 if ($order->getId() > 0)
253 $resTrans = \CSaleUserTransact::GetList(
254 array(
"TRANSACT_DATE" =>
"DESC"),
256 "ORDER_ID" => $order->getId(),
260 array(
"AMOUNT",
"CURRENCY",
"DEBIT")
262 while ($transactDat = $resTrans->Fetch())
264 if ($transactDat[
'DEBIT'] ==
"Y")
266 $sumTrans += $transactDat[
'AMOUNT'];
270 $sumTrans -= $transactDat[
'AMOUNT'];
279 if (in_array($userBudgetDat[
'TYPE'], $ignoreTypes))
282 $sumTrans += $userBudgetDat[
'SUM'];
295 $budget = static::getUserBudget($order->getUserId(), $order->getCurrency());
300 $budget += $userBudgetDat[
'SUM'];
315 if ($userAccount = \CSaleUserAccount::GetByUserId($userId, $currency))
317 if ($userAccount[
'LOCKED'] !=
'Y')
318 $budget = floatval($userAccount[
'CURRENT_BUDGET']);
static loadMessages($file)
static getMessage($code, $replace=null, $language=null)
const BUDGET_TYPE_ORDER_CANCEL_PART
const BUDGET_TYPE_OUT_CHARGE_OFF
add($sum, $budgetType, Sale\Order $order, Sale\Payment $payment=null)
static getUserBudget($userId, $currency)
const BUDGET_TYPE_ORDER_PART_RETURN
const BUDGET_TYPE_ORDER_PAY
const BUDGET_TYPE_EXCESS_SUM_PAID
static getUserBudgetByOrder(Sale\Order $order)
static getUserBudgetTransForOrder(Sale\Order $order)
static getUserBudgetPool($userId)
static onUserBudgetSave($userId)
const BUDGET_TYPE_ORDER_UNPAY
const BUDGET_TYPE_ORDER_PAY_PART
static addPoolItem(Sale\Order $order, $value, $type, Sale\Payment $payment=null)