Completed
Push — master ( 4b4ef3...b565c2 )
by Dmitry
13:19
created

Plan::supportsSharedPrices()   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 3
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
1
<?php
2
3
namespace hipanel\modules\finance\models;
4
5
use hipanel\models\Ref;
6
use hipanel\modules\finance\models\query\PlanQuery;
7
use Yii;
8
9
/**
10
 * Class Plan
11
 *
12
 * @property string $id
13
 * @property string $name
14
 * @property string $type
15
 * @property string $currency
16
 * @property string $state
17
 * @property int $currency_id
18
 * @property bool $is_grouping
19
 *
20
 * @property Sale[] $sales
21
 * @property Price[]|CertificatePrice[] $prices
22
 * @property-read string[] typeOptions
23
 *
24
 * @author Dmytro Naumenko <[email protected]>
25
 */
26
class Plan extends \hipanel\base\Model
27
{
28
    public const TYPE_SERVER = 'server';
29
    public const TYPE_PCDN = 'pcdn';
30
    public const TYPE_VCDN = 'vcdn';
31
    public const TYPE_TEMPLATE = 'template';
32
    public const TYPE_CERTIFICATE = 'certificate';
33
    public const TYPE_DOMAIN = 'domain';
34
    public const TYPE_SWITCH = 'switch';
35
36
    use \hipanel\base\ModelTrait;
37
38
    /**
39
     * @var string
40
     */
41
    public $monthly;
42
43
    public $servers = [];
44
45
    public function rules()
46
    {
47
        return array_merge(parent::rules(), [
48
            [['id', 'type_id', 'state_id', 'client_id', 'currency_id'], 'integer'],
49
            [['type', 'state', 'client', 'name', 'plan', 'note', 'currency', 'is_grouping'], 'string'],
50
51
            [['type', 'name', 'currency'], 'required', 'on' => ['create', 'update']],
52
            [['id'], 'required', 'on' => ['update', 'delete', 'set-note']],
53
            [['id'], 'required', 'on' => ['delete', 'restore']],
54
            [['id', 'server_ids'], 'safe', 'on' => ['copy']],
55
        ]);
56
    }
57
58
    public function attributeLabels()
59
    {
60
        return array_merge(parent::attributeLabels(), [
61
            'client' => Yii::t('hipanel', 'Seller'),
62
            'name' => Yii::t('hipanel:finance', 'Name'),
63
            'server_ids' => Yii::t('hipanel.finance.plan', 'Servers'),
64
            'monthly' => Yii::t('hipanel.finance.plan', 'Monthly'),
65
            'is_grouping' => Yii::t('hipanel.finance.plan', 'Grouping'),
66
            'currency' => Yii::t('hipanel:finance', 'Currency'),
67
        ]);
68
    }
69
70
    public function getPrices()
71
    {
72
        if ($this->type === Plan::TYPE_CERTIFICATE) {
73
            return $this->hasMany(CertificatePrice::class, ['plan_id' => 'id'])->inverseOf('plan');
74
        }
75
        return $this->hasMany(Price::class, ['plan_id' => 'id'])->indexBy('id')->inverseOf('plan');
76
    }
77
78
    public function getDesiredPriceClass()
79
    {
80
        if ($this->type === Plan::TYPE_CERTIFICATE) {
81
            return CertificatePrice::class;
82
        }
83
84
        return Price::class;
85
    }
86
87
    public function getSales()
88
    {
89
        return $this->hasMany(Sale::class, ['tariff_id' => 'id']);
90
    }
91
92
    public function getTypeOptions()
93
    {
94
        return Ref::getList('type,tariff');
95
    }
96
97
    public function getStateOptions()
98
    {
99
        return Ref::getList('state,tariff');
100
    }
101
102
    public function isDeleted(): bool
103
    {
104
        return $this->state === 'deleted';
105
    }
106
107
    public function supportsSharedPrices(): bool
108
    {
109
        return !\in_array($this->type, [Plan::TYPE_TEMPLATE, Plan::TYPE_CERTIFICATE, Plan::TYPE_DOMAIN], true);
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     * @return PlanQuery
115
     */
116
    public static function find($options = [])
117
    {
118
        return new PlanQuery(get_called_class(), [
119
            'options' => $options,
120
        ]);
121
    }
122
}
123