OSDN Git Service

Изменён тип хранения информации о проинвентаризированных, учтённых, и непроверенных...
[invent/invent.git] / models / Site.php
1 <?php
2
3 namespace app\models;
4
5 use Yii;
6 use yii\data\ActiveDataProvider;
7
8 /**
9  * Эта модель используется для формирования сводныз данных на стартовой странице,
10  * исполшьзуя данные из связанных аблиц ({%items}), {{%types}}, {{%locations}}, {{%regions}}/
11  *
12  * @property ActiveDataProvider|null regionsDataProvider Сводная таблица, содержащая количество оборудования по регионам
13  * @property ActiveDataProvider|null typesDataProvider   Сводная таблица, содержащая количество оборудования по типам
14  */
15
16 class Site extends \yii\data\ActiveDataProvider
17 {
18
19     public static function regionsDataProvider()
20     {
21
22         $subQuery = Moving::find()
23                 ->select('MAX(id) AS id')
24                 ->distinct('item_id')
25                 ->groupBy(['item_id']);
26
27         $query = Regions::find()
28             ->select(Regions::tableName() . '.name, count(' . Items::tableName() . '.id) AS icount, count(c.tid) AS ccount')
29             ->joinWith(['locations', 'moving', 'items'])
30             ->leftJoin(['c' => Regions::find()
31                     ->select(Regions::tableName() . '.id, ' . Items::tableName() . '.id AS tid')
32                     ->joinWith(['locations', 'moving', 'items'])
33                     ->where(['in', Moving::tableName() . '.id', $subQuery])
34                     ->andWhere(Items::tableName() . '.checked = 1')
35                 ], Regions::tableName() . '.id = c.id AND ' . Items::tableName() . '.id = c.tid')
36             ->groupBy(Regions::tableName() . '.id')
37             ->where(['in', Moving::tableName() . '.id', $subQuery]);
38
39         $dataProvider = new ActiveDataProvider([
40             'query' => $query,
41         ]);
42         $dataProvider->setSort([
43             'defaultOrder' => [
44                 'name' => SORT_ASC,
45             ],
46         ]);
47
48         return $dataProvider;
49     }
50
51     public static function typesDataProvider()
52     {
53
54         $query = Types::find()
55             ->select(Types::tableName() . '.name, count(' . Items::tableName() . '.id) AS icount, count( c.tid ) AS ccount')
56             ->joinWith([ 'items', 'models' ])
57             ->leftJoin(['c' => Items::find()
58                     ->select('id AS tid')
59                     ->where(['checked' => 1 ])
60                 ], Items::tableName() . '.id = c.tid')
61             ->groupBy(Types::tableName() . '.id');
62
63         $dataProvider = new ActiveDataProvider([
64             'query' => $query,
65         ]);
66         $dataProvider->setSort([
67             'defaultOrder' => [
68                 'name' => SORT_ASC,
69             ],
70         ]);
71         return $dataProvider;
72     }
73 }