Bitrix-D7  20.5.0
userbudgetpool.php
См. документацию.
1 <?php
2 namespace Bitrix\Sale\Internals;
3 
5 use Bitrix\Sale;
6 
8 {
9  protected static $userBudgetPool = array();
10 
11  protected $items = array();
12 
13  const BUDGET_TYPE_ORDER_CANCEL_PART = 'ORDER_CANCEL_PART'; //
14  const BUDGET_TYPE_ORDER_UNPAY = 'ORDER_UNPAY'; //
15  const BUDGET_TYPE_ORDER_PART_RETURN = 'ORDER_PART_RETURN'; //
16  const BUDGET_TYPE_OUT_CHARGE_OFF = 'OUT_CHARGE_OFF'; //
17  const BUDGET_TYPE_EXCESS_SUM_PAID = 'EXCESS_SUM_PAID'; //
18  const BUDGET_TYPE_MANUAL = 'MANUAL'; //
19  const BUDGET_TYPE_ORDER_PAY = 'ORDER_PAY'; //
20  const BUDGET_TYPE_ORDER_PAY_PART = 'ORDER_PAY_PART'; //
21 
22  public function __construct()
23  {
24  }
25 
26  /**
27  * @param $sum
28  * @param $type
29  * @param Sale\Order $order
30  * @param Sale\Payment $payment
31  */
32  public function add($sum, $type, Sale\Order $order, Sale\Payment $payment = null)
33  {
34  $fields = array(
35  "SUM" => $sum,
36  "CURRENCY" => $order->getCurrency(),
37  "TYPE" => $type,
38  "ORDER" => $order,
39  );
40 
41  if ($payment !== null)
42  $fields['PAYMENT'] = $payment;
43 
44  $this->items[] = $fields;
45 
46  }
47 
48  /**
49  * @return array
50  */
51  public function get()
52  {
53  if (isset($this->items))
54  return $this->items;
55 
56  return false;
57  }
58 
59  /**
60  * @param $index
61  * @return bool
62  */
63  public function delete($index)
64  {
65  if (isset($this->items) && isset($this->items[$index]))
66  {
67  unset($this->items[$index]);
68  return true;
69  }
70 
71  return false;
72  }
73 
74  /**
75  * @param $key
76  * @return UserBudgetPool
77  */
78  public static function getUserBudgetPool($key)
79  {
80  if (!isset(static::$userBudgetPool[$key]))
81  static::$userBudgetPool[$key] = new static();
82 
83  return static::$userBudgetPool[$key];
84  }
85 
86  /**
87  * @param Sale\Order $order
88  * @param $value
89  * @param $type
90  * @param Sale\Payment $payment
91  */
92  public static function addPoolItem(Sale\Order $order, $value, $type, Sale\Payment $payment = null)
93  {
94  if (floatval($value) == 0)
95  return;
96 
97  $key = $order->getUserId();
98  $pool = static::getUserBudgetPool($key);
99  $pool->add($value, $type, $order, $payment);
100  }
101 
102  /**
103  * @param $userId
104  * @return Sale\Result
105  */
106  public static function onUserBudgetSave($userId)
107  {
108  $result = new Sale\Result();
109 
110  $pool = static::getUserBudgetPool($userId);
111  foreach ($pool->get() as $key => $budgetDat)
112  {
113 
114  $orderId = null;
115  $paymentId = null;
116 
117  if (isset($budgetDat['ORDER'])
118  && ($budgetDat['ORDER'] instanceof Sale\OrderBase))
119  {
120  $orderId = $budgetDat['ORDER']->getId();
121  }
122 
123  if (isset($budgetDat['PAYMENT'])
124  && ($budgetDat['PAYMENT'] instanceof Sale\Payment))
125  {
126  $paymentId = $budgetDat['PAYMENT']->getId();
127  }
128 
129 // if ($budgetDat['TYPE'] == Internals\UserBudgetPool::BUDGET_TYPE_ORDER_PAY_PART
130 // || $budgetDat['TYPE'] == Internals\UserBudgetPool::BUDGET_TYPE_ORDER_PAY)
131 // {
132 // if (!\CSaleUserAccount::Pay($userId, ($budgetDat['SUM'] * -1), $budgetDat['CURRENCY'], $orderId, false, $paymentId))
133 // {
134 // $result->addError( new ResultError(Loc::getMessage("SALE_PROVIDER_USER_BUDGET_".$budgetDat['TYPE']."_ERROR"), "SALE_PROVIDER_USER_BUDGET_".$budgetDat['TYPE']."_ERROR") );
135 // }
136 // }
137 // else
138 // {
139  if (!\CSaleUserAccount::UpdateAccount($userId, $budgetDat['SUM'], $budgetDat['CURRENCY'], $budgetDat['TYPE'], $orderId, '', $paymentId))
140  {
141  $result->addError( new Sale\ResultError(Loc::getMessage("SALE_PROVIDER_USER_BUDGET_".$budgetDat['TYPE']."_ERROR"), "SALE_PROVIDER_USER_BUDGET_".$budgetDat['TYPE']."_ERROR") );
142  }
143 // }
144 
145  $pool->delete($key);
146  }
147 
148  return $result;
149  }
150 
151  /**
152  * @param Sale\Order $order
153  * @return int
154  */
155  public static function getUserBudgetTransForOrder(Sale\Order $order)
156  {
157  $ignoreTypes = array(
158  static::BUDGET_TYPE_ORDER_PAY
159  );
160  $sumTrans = 0;
161 
162  if ($order->getId() > 0)
163  {
164  $resTrans = \CSaleUserTransact::GetList(
165  array("TRANSACT_DATE" => "DESC"),
166  array(
167  "ORDER_ID" => $order->getId(),
168  ),
169  false,
170  false,
171  array("AMOUNT", "CURRENCY", "DEBIT")
172  );
173  while ($transactDat = $resTrans->Fetch())
174  {
175  if ($transactDat['DEBIT'] == "Y")
176  {
177  $sumTrans += $transactDat['AMOUNT'];
178  }
179  else
180  {
181  $sumTrans -= $transactDat['AMOUNT'];
182  }
183  }
184  }
185 
186  if ($userBudgetPool = static::getUserBudgetPool($order->getUserId()))
187  {
188  foreach ($userBudgetPool->get() as $userBudgetDat)
189  {
190  if (in_array($userBudgetDat['TYPE'], $ignoreTypes))
191  continue;
192 
193  $sumTrans += $userBudgetDat['SUM'];
194  }
195  }
196 
197  return $sumTrans;
198  }
199 
200  /**
201  * @param Sale\Order $order
202  * @return int
203  */
204  public static function getUserBudgetByOrder(Sale\Order $order)
205  {
206  $budget = static::getUserBudget($order->getUserId(), $order->getCurrency());
207  if ($userBudgetPool = static::getUserBudgetPool($order->getUserId()))
208  {
209  foreach ($userBudgetPool->get() as $userBudgetDat)
210  {
211  $budget += $userBudgetDat['SUM'];
212  }
213  }
214 
215  return $budget;
216  }
217 
218  /**
219  * @param $userId
220  * @param $currency
221  * @return float|null
222  */
223  public static function getUserBudget($userId, $currency)
224  {
225  $budget = null;
226  if ($userAccount = \CSaleUserAccount::GetByUserId($userId, $currency))
227  {
228  if ($userAccount['LOCKED'] != 'Y')
229  $budget = floatval($userAccount['CURRENT_BUDGET']);
230  }
231 
232  return $budget;
233  }
234 }
static getMessage($code, $replace=null, $language=null)
Returns translation by message code.
Definition: loc.php:29
static getUserBudget($userId, $currency)
static getUserBudgetByOrder(Sale\Order $order)
static getUserBudgetTransForOrder(Sale\Order $order)
add($sum, $type, Sale\Order $order, Sale\Payment $payment=null)
static addPoolItem(Sale\Order $order, $value, $type, Sale\Payment $payment=null)
Class RestHandler \Handlers\PaySystem.