Рабочий пример сбора данных from_date null to_date null modelOrder Ord

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
// Рабочий пример сбора данных:
$from_date = null;
$to_date = null;
$modelOrder = Order::find();
if (Yii::$app->request->get()) {
$time = Yii::$app->request->get();
$from_date = $time['from_date'];
$to_date = $time['to_date'];
}
if ($from_date == null) {
$from_date = DateHelper::convertToLocal(time() - self::WEEK);
}
if ($to_date == null) {
$to_date = DateHelper::convertToLocal(time());
}
$partners = Partner::find()->all();
if ($partners) {
$headers = [
new ReportColumn('№', 'id', [], 200),
new ReportColumn(Yii::t('common','Компания'), 'company', [], 200),
new ReportColumn(Yii::t('common','Предложения'), 'offers', [], 200),
new ReportColumn(Yii::t('common','Показы'), 'shows', [], 200),
new ReportColumn(Yii::t('common','Заказы'), 'orders', [], 200),
new ReportColumn('Переходы', 'link', [], 200),
new ReportColumn(Yii::t('common','Сумма заказов'), 'sum_orders', [], 200),
];
$ordersSum = 0;
$ordersCount = 0;
$i = 0;
foreach ($partners as $partner) {
if ($partner) {
$offer = Offer::find()->where(['partner_id' => $partner->id]);
$offerCount = $offer->count();
$offers = $offer->all();
$showCount = 0;
$linkCount = 0;
$orderCount = 0;
$orderSum = 0;
foreach ($offers as $of) {
$tmpShow = OfferStat::find()
->where(['offer_id' => $of->id])
->andWhere(['offer_stat_type_id' => 1])
->andWhere(['>=', 'datetime', new Expression("to_timestamp('" . DateHelper::convertToLocal($from_date) . "', 'DD.MM.YYYY HH24:MI')")])
->andWhere(['<=', 'datetime', new Expression("to_timestamp('" . DateHelper::convertToLocal($to_date) . "', 'DD.MM.YYYY HH24:MI')")])->count();
$tmpLink = OfferStat::find()
->where(['offer_id' => $of->id])
->andWhere(['offer_stat_type_id' => 2])
->andWhere(['>=', 'datetime', new Expression("to_timestamp('" . DateHelper::convertToLocal($from_date) . "', 'DD.MM.YYYY HH24:MI')")])
->andWhere(['<=', 'datetime', new Expression("to_timestamp('" . DateHelper::convertToLocal($to_date) . "', 'DD.MM.YYYY HH24:MI')")])->count();
$tmpOrder = OfferStat::find()
->where(['offer_id' => $of->id])
->andWhere(['offer_stat_type_id' => 3])
->andWhere(['>=', 'datetime', new Expression("to_timestamp('" . DateHelper::convertToLocal($from_date) . "', 'DD.MM.YYYY HH24:MI')")])
->andWhere(['<=', 'datetime', new Expression("to_timestamp('" . DateHelper::convertToLocal($to_date) . "', 'DD.MM.YYYY HH24:MI')")])->count();
$showCount += $tmpShow;
$linkCount += $tmpLink;
$orderCount += $tmpOrder;
$tmpSum = $of->cost * $tmpOrder;
$orderSum += $tmpSum;
}
$ordersSum += $orderSum;
$ordersCount += $orderCount;
$data[] = [
'row_type' => ReportGrid::TYPE_TOTAL_MONTH,
'id' => $i + 1,
'company' => $partner->company_name,
'offers' => $offerCount,
'shows' => $showCount,
'link' => $linkCount,
'orders' => $orderCount,
'sum_orders' => $orderSum,
];
}
$i++;
}
$reportGrid = new ReportGrid($headers, $data, '');
} else {
$headers = [
new ReportColumn('', 'error', [], 200)];
$data[] = [
'row_type' => ReportGrid::TYPE_TOTAL_MONTH,
'error' => Yii::t('common','Нет данных')];
$reportGrid = new ReportGrid($headers, $data, '');
}
// После в view для отображения HTML страницы
<?= $model->renderHtml(); ?>
// Либо для выгрузки тех же данных в таком же формате в Xlsx
return $model->renderXlsx();