Completed
Push — master ( b93f48...f361ba )
by Paweł
03:49
created

AccountMonthlyDiff::getLastDiff()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created for IG Monitoring.
4
 * User: jakim <[email protected]>
5
 * Date: 19.06.2018
6
 */
7
8
namespace app\components\stats;
9
10
11
use app\components\stats\base\AccountDiff;
12
use Carbon\Carbon;
13
use yii\db\Expression;
14
use yii\helpers\ArrayHelper;
15
16
class AccountMonthlyDiff extends AccountDiff
17
{
18
    public function initLastDiff()
19
    {
20
        $accountIds = $this->getModelIds();
21
22
        $max1Ids = $this->getLastStatsIds(null, null, $accountIds);
23
        $max2Ids = $this->getLastStatsBeforeIds(null, null, $accountIds, 'account_stats.account_id', 30, $max1Ids);
24
25
        $accountsStats = $this->getAccountsStats($accountIds, ArrayHelper::merge($max1Ids, $max2Ids));
26
        $this->lastDiffCache = $this->prepareCache($accountsStats, 'account_id');
27
    }
28
29
    public function initDiff($olderDate, $newerDate = null)
30
    {
31
        $olderDate = (new Carbon($olderDate))->subDay()->startOfDay()->toDateTimeString();
32
        $newerDate = (new Carbon($newerDate))->endOfDay()->toDateTimeString();
33
34
        if (empty($this->diffCache)) {
35
            $accountIds = ArrayHelper::getColumn($this->models, 'id');
36
37
            //group by DATE Y-m
38
            $ids = $this->getLastStatsIds($olderDate, $newerDate, $accountIds, [
39
                'account_id',
40
                new Expression('DATE_FORMAT(created_at, \'%Y-%m\')'),
41
            ]);
42
43
            $accountsStats = $this->getAccountsStats($accountIds, $ids);
44
45
            $this->diffCache = $this->prepareCache($accountsStats, 'account_id');
46
        }
47
48
        return $this;
49
    }
50
}