Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
reportcontext.php
1<?php
2
3namespace Bitrix\Conversion;
4
5use Bitrix\Main\Entity\Query;
6use Bitrix\Main\Entity\ReferenceField;
7use Bitrix\Main\Entity\ExpressionField;
10
12{
13 public function unsetAttribute($name, $value = null)
14 {
15 if (! is_string($name))
16 throw new ArgumentTypeException('name', 'string');
17
18 if ($this->id !== null)
19 throw new SystemException('Cannot modify existent context!');
20
21 unset($this->attributes[$name]);
22 }
23
25 private function getCountersDeprecated(array $filter = null, array $steps = null)
26 {
27 $query = new Query(Internals\ContextCounterDayTable::getEntity());
28
29 if ($filter)
30 {
31 $query->setFilter($filter);
32 }
33
34 $i = 0;
35
36 foreach ($this->attributes as $name => $value)
37 {
38 self::setAttributeFilter($query, '_conversion_attribute_'.(++ $i).'_', $name, $value);
39 }
40
41 $query->registerRuntimeField(null, new ExpressionField('VALUE_SUM', 'SUM(%s)', array('VALUE')));
42
43 $query->setSelect(array('NAME', 'VALUE_SUM'));
44
45 $query->addGroup('NAME');
46
47 if ($steps) // TODO
48 {
49 $query->addGroup('DAY');
50 $query->addSelect('DAY');
51 }
52
53 $result = $query->exec();
54
55 $counters = array();
56
57 if ($steps)
58 {
59 $steps = array(); // TODO
60
61 while ($row = $result->fetch())
62 {
63 $name = $row['NAME'];
64 $value = $row['VALUE_SUM'];
65
66 $counters[$name] += $value;
67
68 $steps[$row['DAY']->format('Y-m-d')][$name] = $value;
69 }
70
71 $counters['STEPS'] = $steps;
72 }
73 else
74 {
75 while ($row = $result->fetch())
76 {
77 $counters[$row['NAME']] = $row['VALUE_SUM'];
78 }
79 }
80
81 return $counters;
82 }
83
85 public function getRatesDeprecated(array $rateTypes, array $filter = null, array $steps = null)
86 {
87 if (! $filter)
88 {
89 $filter = array();
90 }
91
92 $filter['=NAME'] = RateManager::getRatesCounters($rateTypes);
93
94 $counters = $this->getCountersDeprecated($filter, $steps);
95
96 return self::getRatesCommonDeprecated($rateTypes, $counters, $steps);
97 }
98
99 static private function getRatesCommonDeprecated(array $rateTypes, array $counters, array $steps = null)
100 {
101 $rates = RateManager::getRatesCalculated($rateTypes, $counters);
102
103 if ($steps)
104 {
105 foreach ($rates as & $rate)
106 {
107 $rate['STEPS'] = array();
108 }
109 unset ($rate);
110
111 foreach ($counters['STEPS'] as $step => $stepCounters)
112 {
113 foreach (RateManager::getRatesCalculated($rateTypes, $stepCounters) as $name => $rate)
114 {
115 $rates[$name]['STEPS'][$step] = $rate['RATE'];
116 }
117 }
118 }
119
120 return $rates;
121 }
122
123 // splits
124
125 private function getSplitCountersDeprecated(array $splits, array $filter = null, array $steps = null)
126 {
127 $result = array();
128
129 $totalCounters = $this->getCountersDeprecated($filter, $steps);
130 $otherCounters = $totalCounters;
131
132 foreach ($splits as $splitKey => $attribute)
133 {
134 if ($attribute['NAME'])
135 {
136 $this->setAttribute($attribute['NAME'], $attribute['VALUE']);
137
138 $counters = $this->getCountersDeprecated($filter, $steps);
139
140 $result[$splitKey] = $counters;
141
142 self::subtructCounters($otherCounters, $counters, $steps);
143
144 $this->unsetAttribute($attribute['NAME'], $attribute['VALUE']);
145 }
146 }
147
148 $result['other'] = $otherCounters > 0 ? $otherCounters : 0; // can be -0 or maybe -something
149 $result['total'] = $totalCounters;
150
151 return $result;
152 }
153
154 private static function subtructCounters(array & $one, array $two, array $steps = null)
155 {
156 if ($steps)
157 {
158 $oneSteps = $one['STEPS'];
159 $twoSteps = $two['STEPS'];
160
161 unset($one['STEPS']);
162
163 if ($oneSteps && $twoSteps)
164 {
165 foreach ($oneSteps as $key => & $oneStep)
166 {
167 if ($twoStep = $twoSteps[$key])
168 {
169 self::subtructCountersOnce($oneStep, $twoStep);
170 }
171 }
172 }
173
174 self::subtructCountersOnce($one, $two);
175
176 $one['STEPS'] = $oneSteps;
177 }
178 else
179 {
180 self::subtructCountersOnce($one, $two);
181 }
182 }
183
184 private static function subtructCountersOnce(array & $one, array $two)
185 {
186 foreach ($one as $key => & $value)
187 {
188 $value -= $two[$key];
189 }
190 }
191
193 public function getSplitRatesDeprecated(array $splits, array $rateTypes, array $filter = null, array $steps = null)
194 {
195 if (! $filter)
196 {
197 $filter = array();
198 }
199
200 $filter['=NAME'] = RateManager::getRatesCounters($rateTypes);
201
202 $splitCounters = $this->getSplitCountersDeprecated($splits, $filter, $steps);
203
204 $result = array();
205
206 foreach ($splitCounters as $split => $counters)
207 {
208 $result[$split] = $this->getRatesCommonDeprecated($rateTypes, $counters, $steps);
209 }
210
211 return $result;
212 }
213
215
216 static private function setAttributeFilter(Query $query, $field, $name, $value = null)
217 {
218 $query->registerRuntimeField(null, new ReferenceField($field, Internals\ContextAttributeTable::getEntity(),
219 array('=this.CONTEXT_ID' => 'ref.CONTEXT_ID'),
220 array('join_type' => 'INNER')
221 ));
222
223 $query->addFilter("=$field.NAME" , $name);
224
225 if ($value !== null)
226 {
227 $query->addFilter("=$field.VALUE", $value);
228 }
229 }
230
231 public function getCounters(array $parameters = array())
232 {
233 $query = new Query(Internals\ContextCounterDayTable::getEntity());
234
235 if ($filter = $parameters['filter'])
236 {
237 $query->setFilter($filter);
238 }
239
240 $i = 0;
241
242 foreach ($this->attributes as $name => $value)
243 {
244 self::setAttributeFilter($query, '_conversion_attribute_'.(++ $i).'_', $name, $value);
245 }
246
247 $query->registerRuntimeField(null, new ExpressionField('VALUE_SUM', 'SUM(%s)', array('VALUE')));
248
249 $splitNames = array();
250
251 if ($split = $parameters['split'])
252 {
253 if (! is_array($split))
254 throw new ArgumentTypeException('parameters[split]', 'array');
255
256 foreach ($split as $name => $value)
257 {
258 switch ($name)
259 {
260 case 'ATTRIBUTE_NAME':
261
262 if (! is_string($value))
263 throw new ArgumentTypeException('parameters[split][ATTRIBUTE_NAME]', 'string');
264
265 self::setAttributeFilter($query, 'split_attribute', $value);
266 $query->addGroup('split_attribute.VALUE');
267 $query->addSelect('split_attribute.VALUE', 'ATTRIBUTE_VALUE');
268 $splitNames []= 'ATTRIBUTE_VALUE';
269
270 break;
271
272 default: throw new ArgumentTypeException('parameters[split]['.$name.']', 'not implemented');
273 }
274 }
275 }
276
277 $query->addGroup('NAME');
278 $query->addSelect('NAME');
279 $query->addSelect('VALUE_SUM');
280
281 $result = $query->exec();
282
283// return $result->fetchAll();
284
285 $counters = array();
286
287 while ($row = $result->fetch())
288 {
289 $level =& $counters;
290
291 foreach ($splitNames as $name)
292 {
293 if (! $level =& $level[$row[$name]])
294 {
295 $level = array();
296 }
297 }
298
299 $level[$row['NAME']] = $row['VALUE_SUM'];
300 }
301
302 return $counters;
303 }
304
305 public function getRates(array $rateTypes, array $parameters = array())
306 {
307 if (! $filter =& $parameters['filter'])
308 {
309 $filter = array();
310 }
311
312 $filter['=NAME'] = RateManager::getRatesCounters($rateTypes);
313
314 return self::getRatesRecursive($rateTypes, $this->getCounters($parameters), ($s = $parameters['split']) ? count($s) : 0);
315 }
316
317 static private function getRatesRecursive(array $rateTypes, array $counters, $level)
318 {
319 if ($level > 0)
320 {
321 $level--;
322
323 $rates = array();
324
325 foreach ($counters as $k => $v)
326 {
327 $rates[$k] = self::getRatesRecursive($rateTypes, $v, $level);
328 }
329
330 return $rates;
331 }
332 else
333 {
334 return RateManager::getRatesCalculated($rateTypes, $counters);
335 }
336 }
337}
static getRatesCounters(array $types)
static getRatesCalculated(array $types, array $counters)
getSplitRatesDeprecated(array $splits, array $rateTypes, array $filter=null, array $steps=null)
unsetAttribute($name, $value=null)
getRatesDeprecated(array $rateTypes, array $filter=null, array $steps=null)
getCounters(array $parameters=array())
getRates(array $rateTypes, array $parameters=array())
setFilter(array $filter)
Definition query.php:381
registerRuntimeField($name, $fieldInfo=null)
Definition query.php:831
addFilter($key, $value)
Definition query.php:394
addSelect($definition, $alias='')
Definition query.php:351