|
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-2017, HiQDev (http://hiqdev.com/) |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace hipanel\modules\finance\models; |
|
12
|
|
|
|
|
13
|
|
|
use hipanel\base\ModelTrait; |
|
14
|
|
|
use Yii; |
|
15
|
|
|
use yii\base\InvalidConfigException; |
|
16
|
|
|
use yii\validators\NumberValidator; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* Class CertificateResource |
|
20
|
|
|
* |
|
21
|
|
|
* @author Dmytro Naumenko <[email protected]> |
|
22
|
|
|
*/ |
|
23
|
|
|
class CertificateResource extends Resource |
|
24
|
|
|
{ |
|
25
|
|
|
use ModelTrait; |
|
26
|
|
|
|
|
27
|
|
|
public $certificateType; |
|
28
|
|
|
|
|
29
|
|
|
public static function tableName() |
|
30
|
|
|
{ |
|
31
|
|
|
return 'resource'; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
const TYPE_CERT_PURCHASE = 'certificate_purchase'; |
|
35
|
|
|
const TYPE_CERT_RENEWAL = 'certificate_renewal'; |
|
36
|
|
|
|
|
37
|
|
View Code Duplication |
public function rules() |
|
|
|
|
|
|
38
|
|
|
{ |
|
39
|
|
|
$rules = parent::rules(); |
|
40
|
|
|
$rules['create-required'] = [ |
|
41
|
|
|
['object_id'], |
|
42
|
|
|
'required', |
|
43
|
|
|
'on' => ['create', 'update'], |
|
44
|
|
|
'when' => function ($model) { |
|
45
|
|
|
/** @var self $model */ |
|
46
|
|
|
return $model->isTypeCorrect(); |
|
47
|
|
|
}, |
|
48
|
|
|
]; |
|
49
|
|
|
$rules[] = [['certificateType'], 'safe']; |
|
50
|
|
|
$rules[] = [['data'], 'validatePrices', 'on' => ['create', 'update']]; |
|
51
|
|
|
|
|
52
|
|
|
return $rules; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @return array |
|
57
|
|
|
*/ |
|
58
|
|
|
public static function getPeriods() |
|
59
|
|
|
{ |
|
60
|
|
|
return [ |
|
61
|
|
|
1 => Yii::t('hipanel:finance:tariff', '{n, plural, one{# year} other{# years}}', ['n' => 1]), |
|
62
|
|
|
2 => Yii::t('hipanel:finance:tariff', '{n, plural, one{# year} other{# years}}', ['n' => 2]), |
|
63
|
|
|
3 => Yii::t('hipanel:finance:tariff', '{n, plural, one{# year} other{# years}}', ['n' => 3]), |
|
64
|
|
|
]; |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* @return array |
|
69
|
|
|
*/ |
|
70
|
|
|
public function getAvailablePeriods() |
|
71
|
|
|
{ |
|
72
|
|
|
$periods = []; |
|
73
|
|
|
foreach ([1,2,3] as $period) { |
|
74
|
|
|
if ($this->hasPriceForPeriod($period)) { |
|
75
|
|
|
$periods[$period] = Yii::t('hipanel:finance:tariff', '{n, plural, one{# year} other{# years}}', ['n' => $period]); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
return $periods; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
public function getPriceForPeriod($period) |
|
83
|
|
|
{ |
|
84
|
|
|
if (!$this->hasPriceForPeriod($period)) { |
|
85
|
|
|
throw new InvalidConfigException('Period ' . $period . ' is not available'); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
return (float)$this->data['sums'][$period]; |
|
|
|
|
|
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
public function hasPriceForPeriod($period) |
|
92
|
|
|
{ |
|
93
|
|
|
return !empty($this->data['sums'][$period]); |
|
|
|
|
|
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
public function validatePrices() |
|
97
|
|
|
{ |
|
98
|
|
|
$periods = $this->getPeriods(); |
|
99
|
|
|
$validator = new NumberValidator(); |
|
100
|
|
|
|
|
101
|
|
|
foreach (array_keys($periods) as $period) { |
|
102
|
|
|
$validation = $validator->validate($this->data['sums'][$period]); |
|
|
|
|
|
|
103
|
|
|
if ($validation === false) { |
|
104
|
|
|
unset($this->data['sums'][$period]); |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
$this->data = ['sums' => $this->data['sums']]; |
|
|
|
|
|
|
109
|
|
|
|
|
110
|
|
|
return true; |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
/** |
|
114
|
|
|
* @return array |
|
115
|
|
|
*/ |
|
116
|
|
|
public function getTypes() |
|
117
|
|
|
{ |
|
118
|
|
|
return [ |
|
119
|
|
|
static::TYPE_CERT_PURCHASE => Yii::t('hipanel:finance:tariff', 'Purchase'), |
|
120
|
|
|
static::TYPE_CERT_RENEWAL => Yii::t('hipanel:finance:tariff', 'Renewal'), |
|
121
|
|
|
]; |
|
122
|
|
|
} |
|
123
|
|
|
} |
|
124
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.