Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
paysysteminner.php
1<?php
2
4
6use Bitrix\Main\Entity\EntityError;
7use Bitrix\Main\Entity\Result;
13
14Loc::loadMessages(__FILE__);
15
17{
18 const OPERATION_DEBIT = 0; //from account
19 const OPERATION_CREDIT = 1; //to account
20 const OPERATION_RETURN = 2; //to account (return)
21
22 const ACTION_FILE_TEXT = 'INNER_BUDGET';
23 const CACHE_ID = "BITRIX_SALE_INNER_BUDGET_ID";
24 const TTL = 31536000;
25
31 public static function getId($useCache = true)
32 {
33 $id = 0;
34 $cacheManager = \Bitrix\Main\Application::getInstance()->getManagedCache();
35 $ttl = $useCache ? self::TTL : 0;
36
37 if($cacheManager->read($ttl, self::CACHE_ID))
38 $id = $cacheManager->get(self::CACHE_ID);
39
40 if(intval($id) <= 0)
41 {
42 $dbRes = PaySystem\Manager::getList(array(
43 'filter' => array('ACTION_FILE' => self::ACTION_FILE_TEXT),
44 'select' => array('PAY_SYSTEM_ID')
45 )
46 );
47
48 if($res = $dbRes->fetch())
49 {
50 $id = $res['PAY_SYSTEM_ID'];
51 $cacheManager->set(self::CACHE_ID, $id);
52 }
53 }
54
55 return intval($id);
56 }
57
65 public static function createOperation(Order &$order, Payment &$payment, $operation)
66 {
67 $result = new Result();
68 $paymentSum = $payment->getSum();
69
70 if($operation == self::OPERATION_DEBIT)
71 {
72 $userBudget = UserBudgetPool::getUserBudgetByOrder($order);
73
74 if($userBudget >= $paymentSum)
75 {
76 UserBudgetPool::addPoolItem($order, ( $paymentSum * -1 ), UserBudgetPool::BUDGET_TYPE_ORDER_PAY, $payment);
77// $payment->setField('PAID', 'Y');
78 }
79 else
80 {
81 $result->addError(new EntityError(Loc::getMessage('ORDER_PS_INNER_ERROR_INSUFFICIENT_MONEY')));
82 }
83 }
84 elseif($operation == self::OPERATION_CREDIT)
85 {
87
88// $payment->setField('PAID', 'N');
89 }
90 elseif($operation == self::OPERATION_RETURN)
91 {
92 $sumPaid = $order->getSumPaid();
94 $finalSumPaid = $paymentSum + $sumTrans;
95
96 if ($finalSumPaid > 0)
97 {
98 $paymentSum = $paymentSum - $finalSumPaid;
99 }
100
101// Internals\UserBudgetPool::addPoolItem($order->getUserId(), ( $paymentSum ), UserBudgetPool::BUDGET_TYPE_CANCEL_RETURN, $order, $payment);
102// $payment->setField('PAID', 'N');
103 $payment->setField('IS_RETURN', 'Y');
104 }
105 else
106 {
107 throw new InvalidOperationException('Wrong operation type!');
108 }
109
110 return $result;
111 }
112
116 public static function add()
117 {
118 $id = self::getId(false);
119
120 if($id > 0)
121 return $id;
122
123 $result = 0;
124
125 $res = PaySystemTable::add(array(
126 'NAME' => Loc::getMessage('ORDER_PS_INNER_NAME'),
127 'DESCRIPTION' => Loc::getMessage('ORDER_PS_INNER_DESCRIPTION'),
128 'SORT' => 10,
129 'LID' => '',
130 'CURRENCY' => ''
131 ));
132
133 if($res->isSuccess())
134 {
135 $cacheManager = \Bitrix\Main\Application::getInstance()->getManagedCache();
136 $cacheManager->set(self::CACHE_ID, $res->getId());
137
138 $res = PaySystem\Manager::add(array(
139 'PAY_SYSTEM_ID' => $res->getId(),
140 'PERSON_TYPE_ID' => 0,
141 'NAME' => Loc::getMessage('ORDER_PS_INNER_NAME'),
142 'ACTION_FILE' => self::ACTION_FILE_TEXT
143 )
144 );
145
146 if($res->isSuccess())
147 $result = $res->getId();
148 }
149
150 return $result;
151 }
152}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static createOperation(Order &$order, Payment &$payment, $operation)
static getUserBudgetByOrder(Sale\Order $order)
static getUserBudgetTransForOrder(Sale\Order $order)
static addPoolItem(Sale\Order $order, $value, $type, Sale\Payment $payment=null)