Completed
Push — master ( 275242...920d34 )
by Dmitry
26:19 queued 22:43
created

BillTypesProvider::keepUnusedTypes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
/**
3
 * Finance module for HiPanel
4
 *
5
 * @link      https://github.com/hiqdev/hipanel-module-finance
6
 * @package   hipanel-module-finance
7
 * @license   BSD-3-Clause
8
 * @copyright Copyright (c) 2015-2019, HiQDev (http://hiqdev.com/)
9
 */
10
11
namespace hipanel\modules\finance\providers;
12
13
use hipanel\helpers\ArrayHelper;
14
use hipanel\models\Ref;
15
use hipanel\modules\finance\models\Bill;
16
use yii\base\Application;
17
18
/**
19
 * Class BillTypesProvider.
20
 */
21
class BillTypesProvider
22
{
23
    /**
24
     * @var Application
25
     */
26
    private $app;
27
28
    /**
29
     * @var bool
30
     */
31
    private $showUnusedTypes = false;
32
33
    public function __construct(Application $app)
34
    {
35
        $this->app = $app;
36
    }
37
38
    /**
39
     * Returns key-value list of bill types.
40
     * `key` - type name
41
     * `value` - type label (translated).
42
     * @return array
43
     */
44
    public function getTypesList()
45
    {
46
        return ArrayHelper::map($this->getTypes(), 'name', 'label');
47
    }
48
49
    /**
50
     * Returns array of types.
51
     * When user can not support, filters out unused types.
52
     * @return Ref[]
53
     */
54
    public function getTypes()
55
    {
56
        $options = ['select' => 'full', 'orderby' => 'name_asc', 'with_hierarchy' => true];
57
        $types = Ref::findCached('type,bill', 'hipanel:finance', $options);
58
59
        if (!$this->app->user->can('support')) {
60
            $types = $this->removeUnusedTypes($types);
61
        }
62
63
        return $types;
64
    }
65
66
    /**
67
     * @param Ref[] $types
68
     * @return Ref[]
69
     */
70
    private function removeUnusedTypes($types)
71
    {
72
        if ($this->showUnusedTypes) {
73
            return $types;
74
        }
75
        $ids = $this->app->cache->getOrSet([__METHOD__, $this->app->user->id], function () use ($types) {
76
            return ArrayHelper::getColumn(Bill::perform('get-used-types', [], ['batch' => true]), 'id');
77
        }, 3600);
78
79
        return array_filter($types, function ($model) use ($ids) {
80
            return in_array($model->id, $ids, true);
81
        });
82
    }
83
84
    /**
85
     * @return array
86
     */
87
    public function getGroupedList()
88
    {
89
        $billTypes = [];
90
        $billGroupLabels = [];
91
92
        $types = $this->getTypesList();
93
94
        foreach ($types as $key => $title) {
95
            list($type, $name) = explode(',', $key);
96
97
            if (!isset($billTypes[$type])) {
98
                $billTypes[$type] = [];
99
                $billGroupLabels[$type] = ['label' => $title];
100
            }
101
102
            if (isset($name)) {
103
                foreach ($types as $k => $t) {
104
                    if (strpos($k, $type . ',') === 0) {
105
                        $billTypes[$type][$k] = $t;
106
                    }
107
                }
108
            }
109
        }
110
111
        return [$billTypes, $billGroupLabels];
112
    }
113
114
    /**
115
     * This method prevents removing unused user types of payments.
116
     */
117
    public function keepUnusedTypes(): void
118
    {
119
        $this->showUnusedTypes = true;
120
    }
121
}
122