Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
statistic.php
1<?php
2
3
5
20
22{
25
26 protected $app;
27 protected $collection;
28 protected $exchange;
29
30 public function __construct()
31 {
32 $this->app = new IntegrationB24();
33 $this->collection = new Collection();
34 $this->exchange = new Exchange(ProviderType::B24_INTEGRATION_NAME);
35 }
36
40 public function getCollection(): Collection
41 {
42 return $this->collection;
43 }
44
45 public function isOn()
46 {
47 return (new Manager())->isOn();
48 }
49
50 public function modify()
51 {
52 if($this->isOn())
53 {
54 $provider = $this->getProvider();
55 if(is_null($provider))
56 {
57 return false;
58 }
59
60 $list = $this->getListByParams($provider)
61 ->getCollection()
62 ->toArray();
63
64 if(count($list)>0)
65 {
66 return (new Statistics())
67 ->modify($list)
68 ->isSuccess();
69 }
70 }
71
72 return true;
73 }
74
75 protected function getStatisticsStartDate()
76 {
77 $startDate = new \Bitrix\Main\Type\DateTime();
78 $startDate->add('-1 month');
79
80 return $startDate;
81 }
82
83 protected function getProvider()
84 {
86 'xmlId'=>$this->app
87 ->getCode()
88 ]);
89
90 return (isset($res['error']) == false) ? $res:null;
91 }
92
93 protected function prepareParamsByProviderFields($provider)
94 {
95 $lastDateUpdate = null;
96 if(isset($provider['statisticProviders'][0]['settings']))
97 {
98 $lastDateUpdate = $provider['statisticProviders'][0]['settings']['lastDateUpdate'];
99 }
100
101 return [
102 'ID'=> (int)$provider['statisticProviders'][0]['id'],
103 'LAST_DATE_UPDATE'=> $lastDateUpdate
104 ];
105 }
106
107 protected function getListByParams($provider)
108 {
109 $this->deleteOldEffectedRows();
110
111 $startDate = $this->getStatisticsStartDate()
112 ->toString();
113
114 \CTimeZone::Disable();
115
116 $providerFields = $this->prepareParamsByProviderFields($provider);
117
118 if(is_null($providerFields['LAST_DATE_UPDATE']))
119 {
120 $time = $startDate;
121 }
122 else
123 {
124 $lastDateUpdate = strtotime($providerFields['LAST_DATE_UPDATE']);
125 $time = DateTime::createFromTimestamp($lastDateUpdate)
126 ->toString();
127 }
128
129 $filter['>=DATE_UPDATE'] = $time;
130 $filter['=RUNNING'] = 'N';
131
132 $logs = $this->exchange->getEffectedRows(
133 $time,
136
137 $r = OrderTable::getList([
138 'order'=>['DATE_UPDATE'=>'ASC'],
139 'filter'=>$filter,
140 'limit' => static::STATISTIC_IMPORT_PACKAGE_LIMIT
141 ]);
142
143 while($res = $r->fetch())
144 {
145 if($this->exchange->isEffected($res, $logs))
146 {
147 continue;
148 }
149
150 $res['AMOUNT'] = $res['PRICE'];
151 $res['STATUS'] = $this->resolveStatusId($res);
152 $res['ENTITY_ID'] = $res['ID'];
153 $res['PROVIDER_ID'] = $providerFields['ID'];
154
155 $this->collection->addItem(
156 Item::create(
158 ->setInternalIndex($res['ID'])
159 );
160
161 $this->addEffectedRows($res);
162 }
163
164 \CTimeZone::Enable();
165
166 return $this;
167 }
168
169 protected function resolveStatusId($fields)
170 {
171 if($fields['PAYED'] == 'Y' || $fields['DEDUCTED'] == 'Y')
172 {
173 return StatusType::SUCCESS_NAME;
174 }
175 elseif ($fields['CANCELED'] == 'Y')
176 {
177 return StatusType::FAULTY_NAME;
178 }
179 else
180 {
181 return StatusType::PROCESS_NAME;
182 }
183 }
184
185 protected function deleteOldEffectedRows()
186 {
187 $this->exchange->deleteOldRecords(ManagerExport::EXCHANGE_DIRECTION_EXPORT, static::LOGGER_DAYS_INTERVAL);
188 }
189
190 protected function addEffectedRows(array $fields)
191 {
192 $this->exchange->add([
193 'XML_ID' => $fields['XML_ID'],
194 'ENTITY_ID' => $fields['ID'],
196 'ENTITY_TYPE_ID' => EntityType::ORDER,
197 'ENTITY_DATE_UPDATE' => $fields['DATE_UPDATE']
198 ]);
199 }
200
201}
static createFromTimestamp($timestamp)
Definition datetime.php:246