Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
paysystem.php
1<?php
3
9
10Loc::loadMessages(__FILE__);
11
12class PaySystem extends Base\Restriction
13{
14 public static $easeSort = 200;
15
19 public static function getClassTitle()
20 {
21 return Loc::getMessage("SALE_COMPANY_RULES_BY_PS_TITLE");
22 }
23
27 public static function getClassDescription()
28 {
29 return Loc::getMessage("SALE_COMPANY_RULES_BY_PS_DESC");
30 }
31
38 public static function check($params, array $restrictionParams, $serviceId = 0)
39 {
40 if ((int)$serviceId <= 0)
41 return true;
42
43 if (!$params)
44 return true;
45
46 $paySystemIds = self::getPaySystemsByCompanyId($serviceId);
47
48 if (empty($paySystemIds))
49 return true;
50
51 $diff = array_diff($params, $paySystemIds);
52
53 return empty($diff);
54 }
55
61 protected static function extractParams(Sale\Internals\Entity $entity)
62 {
63 $result = array();
64
66 $paymentCollection = null;
67
68 if ($entity instanceof Sale\Payment)
69 {
70 $paymentCollection = $entity->getCollection();
71 }
72 elseif ($entity instanceof Sale\Shipment)
73 {
75 $shipmentCollection = $entity->getCollection();
76 if ($shipmentCollection)
77 {
79 $order = $shipmentCollection->getOrder();
80 if ($order)
81 $paymentCollection = $order->getPaymentCollection();
82 }
83 }
84 elseif ($entity instanceOf Sale\Order)
85 {
86 $paymentCollection = $entity->getPaymentCollection();
87 }
88
89 if ($paymentCollection !== null)
90 {
92 foreach ($paymentCollection as $payment)
93 {
94 $paySystemId = $payment->getPaymentSystemId();
95 if ($paySystemId)
96 $result[] = $paySystemId;
97 }
98 }
99
100 return $result;
101 }
102
106 protected static function getPaySystemList()
107 {
108 $result = array();
109
110 $dbRes = Sale\PaySystem\Manager::getList(array('select' => array('ID', 'NAME')));
111 while ($paySystem = $dbRes->fetch())
112 $result[$paySystem['ID']] = $paySystem['NAME'].' ['.$paySystem['ID'].']';
113
114 return $result;
115 }
116
121 public static function getParamsStructure($entityId = 0)
122 {
123 $result = array(
124 "PAYSYSTEM" => array(
125 "TYPE" => "ENUM",
126 'MULTIPLE' => 'Y',
127 "LABEL" => Loc::getMessage("SALE_COMPANY_RULES_BY_PS"),
128 "OPTIONS" => self::getPaySystemList()
129 )
130 );
131
132 return $result;
133 }
134
140 protected static function getPaySystemsByCompanyId($companyId = 0)
141 {
142 $result = array();
143 if ($companyId == 0)
144 return $result;
145
146 $dbRes = CompanyServiceTable::getList(
147 array(
148 'select' => array('SERVICE_ID'),
149 'filter' => array(
150 'COMPANY_ID' => $companyId,
151 'SERVICE_TYPE' => Sale\Services\Company\Restrictions\Manager::SERVICE_TYPE_PAYMENT)
152 )
153 );
154
155 while ($data = $dbRes->fetch())
156 $result[] = $data['SERVICE_ID'];
157
158 return $result;
159 }
160
166 public static function save(array $fields, $restrictionId = 0)
167 {
168 $serviceIds = $fields["PARAMS"];
169 $fields["PARAMS"] = array();
170
171 if ($restrictionId > 0)
172 {
173 $dbRes = CompanyServiceTable::getList(
174 array(
175 'select' => array('SERVICE_ID'),
176 'filter' => array(
177 'SERVICE_TYPE' => Sale\Services\Company\Restrictions\Manager::SERVICE_TYPE_PAYMENT,
178 'COMPANY_ID' => $fields['SERVICE_ID']
179 )
180 )
181 );
182
183 while($data = $dbRes->fetch())
184 {
185 $key = array_search($data['SERVICE_ID'], $serviceIds['PAYSYSTEM']);
186 if (!$key)
187 {
188 CompanyServiceTable::delete(array('COMPANY_ID' => $fields['SERVICE_ID'], 'SERVICE_ID' => $data['SERVICE_ID'], 'SERVICE_TYPE' => Sale\Services\Company\Restrictions\Manager::SERVICE_TYPE_PAYMENT));
189 }
190 else
191 {
192 unset($serviceIds['PAYSYSTEM'][$key]);
193 }
194 }
195 }
196
197 $result = parent::save($fields, $restrictionId);
198
199 $addFields = array('COMPANY_ID' => $fields['SERVICE_ID'], 'SERVICE_TYPE' => Sale\Services\Company\Restrictions\Manager::SERVICE_TYPE_PAYMENT);
200 foreach ($serviceIds['PAYSYSTEM'] as $id)
201 {
202 $addFields['SERVICE_ID'] = $id;
203 CompanyServiceTable::add($addFields);
204 }
205
206 return $result;
207 }
208
214 public static function prepareParamsValues(array $paramsValues, $entityId = 0)
215 {
216 return array("PAYSYSTEM" => self::getPaySystemsByCompanyId($entityId));
217 }
218
225 public static function delete($restrictionId, $entityId = 0)
226 {
227 $dbRes = CompanyServiceTable::getList(
228 array(
229 'select' => array('SERVICE_ID'),
230 'filter' => array(
231 'SERVICE_TYPE' => Sale\Services\Company\Restrictions\Manager::SERVICE_TYPE_PAYMENT,
232 'COMPANY_ID' => $entityId
233 )
234 )
235 );
236
237 while ($data = $dbRes->fetch())
238 {
239 CompanyServiceTable::delete(array('COMPANY_ID' => $entityId, 'SERVICE_ID' => $data['SERVICE_ID'], 'SERVICE_TYPE' => Sale\Services\Company\Restrictions\Manager::SERVICE_TYPE_PAYMENT));
240 }
241
242 return parent::delete($restrictionId);
243 }
244}
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29
static prepareParamsValues(array $paramsValues, $entityId=0)
static check($params, array $restrictionParams, $serviceId=0)
Definition paysystem.php:38
static save(array $fields, $restrictionId=0)