Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
statistic.php
1<?php
2
3
5
6
16
17class Statistic extends Base
18{
19 //region Actions
20 public function getFieldsAction()
21 {
22 $view = $this->getViewManager()
23 ->getView($this);
24
25 return ['STATISTIC'=>$view->prepareFieldInfos(
26 $view->getFields()
27 )];
28 }
29
41 public function listAction($select = [], $filter = [], $order = [], PageNavigation $pageNavigation = null): Page
42 {
43 return new Page('STATISTIC',
44 $this->getList($select, $filter, $order, $pageNavigation),
45 $this->count($filter)
46 );
47 }
48
57 public function getAction($id)
58 {
59 $r = $this->exists($id);
60 if($r->isSuccess())
61 {
62 return ['STATISTIC'=>$this->get($id)];
63 }
64 else
65 {
66 $this->addErrors($r->getErrors());
67 return null;
68 }
69 }
70
79 public function upsertAction(array $fields)
80 {
81 $r = $this->checkProviderById($fields['PROVIDER_ID']);
82 if($r->isSuccess())
83 {
84 $entityTable = $this->getEntityTable();
85 $r = $entityTable::upsert($fields);
86 if($r->isSuccess())
87 {
88 return ['STATISTIC'=>$this->get($r->getPrimary())];
89 }
90 }
91
92 $this->addErrors($r->getErrors());
93 return null;
94 }
95
104 public function modifyAction(array $fields)
105 {
106 $statistics = $fields['STATISTICS'];
107 $providerId = $fields['PROVIDER']['ID'];
108
109 $r = $this->checkPackageLimit($statistics);
110 if($r->isSuccess())
111 {
112 $r = $this->checkProviderById($providerId);
113 if($r->isSuccess() === false)
114 {
115 $this->addErrors($r->getErrors());
116 return null;
117 }
118
119 $statistics = $this->onBeforeModify($providerId, $statistics);
120
121 $entityTable = $this->getEntityTable();
122 $r = $entityTable::modify($statistics);
123 if($r->isSuccess())
124 {
125 $this->onAfterModify($providerId);
126
127 $provider = new B24IntegrationStatProviderTable();
128
129 return [
130 'ITEMS' => [
131 'PROVIDER' => $provider::getRow(['filter'=>['ID'=>$providerId]]),
132 ]
133 ];
134 }
135 }
136
137 $this->addErrors($r->getErrors());
138 return null;
139 }
140
149 public function addAction(array $fields)
150 {
151 $r = $this->checkProviderById($fields['PROVIDER_ID']);
152 if($r->isSuccess())
153 {
154 $r = parent::add($fields);
155 if($r->isSuccess())
156 {
157 return ['STATISTIC'=>$this->get($r->getPrimary())];
158 }
159 }
160
161 $this->addErrors($r->getErrors());
162 return null;
163 }
164
174 public function updateAction($id, array $fields)
175 {
176 $r = parent::update($id, $fields);
177 if($r->isSuccess())
178 {
179 return ['STATISTIC'=>$this->get($id)];
180 }
181
182 $this->addErrors($r->getErrors());
183 return null;
184 }
185
194 public function deleteAction($id)
195 {
196 $r = parent::delete($id);
197 if($r->isSuccess())
198 {
199 return true;
200 }
201 else
202 {
203 $this->addErrors($r->getErrors());
204 return null;
205 }
206 }
207 //endregion
208
214 protected function onBeforeModify($providerId, $statistics)
215 {
216 foreach ($statistics as $k=>$statistic)
217 {
218 $statistics[$k]['PROVIDER_ID'] = $providerId;
219 }
220 return $statistics;
221 }
222
230 protected function onAfterModify($providerId)
231 {
232 $provider = new B24IntegrationStatProviderTable();
233
234 $fields = $this->getEntityTable()::getRow([
235 'filter'=>['PROVIDER_ID'=>$providerId],
236 'order'=>['DATE_UPDATE'=>'DESC']]
237 );
238
239 if(is_array($fields))
240 {
241 $provider::update($providerId, ['SETTINGS'=>['LAST_DATE_UPDATE'=>$fields['DATE_UPDATE']->format('c')]]);
242 }
243 }
244
245 protected function createViewManager(Action $action)
246 {
247 return new SaleViewManager($action);
248 }
249
250 protected function getEntityTable()
251 {
252 return new B24integrationStatTable();
253 }
254
255 protected function checkReadPermissionEntity()
256 {
257 return new Result();
258 }
259
260 protected function checkCreatePermissionEntity()
261 {
262 return new Result();
263 }
264
265 protected function checkUpdatePermissionEntity()
266 {
267 return new Result();
268 }
269
270 protected function checkDeletePermissionEntity()
271 {
272 return new Result();
273 }
274
275 protected function checkPermissionEntity($name, $arguments=[])
276 {
277 if($name == 'upsert' || $name == 'modify')
278 {
279 $r = $this->checkCreatePermissionEntity();
280 }
281 else
282 {
283 $r = parent::checkPermissionEntity($name);
284 }
285
286 return $r;
287 }
288
296 protected function checkProviderById($id)
297 {
298 $r = new Result();
299
300 if(is_null($this->getProviderById($id)))
301 {
302 $r->addError(new Error('Provider is not exists'));
303 }
304 return $r;
305 }
306
311 protected function checkPackageLimit(array $data)
312 {
313 $r = new Result();
314
315 if(count($data) > \Bitrix\Sale\Exchange\Integration\Manager\Statistic::STATISTIC_IMPORT_PACKAGE_LIMIT)
316 {
317 $r->addError(new Error('Batch exceeded the limit - '.\Bitrix\Sale\Exchange\Integration\Manager\Statistic::STATISTIC_IMPORT_PACKAGE_LIMIT));
318 }
319 return $r;
320 }
321
329 protected function getProviderById($id)
330 {
331 $result = B24IntegrationStatProviderTable::getByPrimary($id);
332 $row = $result->fetch();
333
334 return (is_array($row)? $row : null);
335 }
336}
getList(array $select, array $filter, array $order, PageNavigation $pageNavigation=null)
Definition base.php:248
listAction($select=[], $filter=[], $order=[], PageNavigation $pageNavigation=null)
Definition statistic.php:41