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 static function tableName() |
28
|
|
|
{ |
29
|
|
|
return 'resource'; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
const TYPE_CERT_REGISTRATION = 'certificate_purchase'; |
33
|
|
|
const TYPE_CERT_RENEW = 'certificate_renew'; |
34
|
|
|
|
35
|
|
View Code Duplication |
public function rules() |
|
|
|
|
36
|
|
|
{ |
37
|
|
|
$rules = parent::rules(); |
38
|
|
|
$rules['create-required'] = [ |
39
|
|
|
['object_id'], |
40
|
|
|
'required', |
41
|
|
|
'on' => ['create', 'update'], |
42
|
|
|
'when' => function ($model) { |
43
|
|
|
/** @var self $model */ |
44
|
|
|
return $model->isTypeCorrect(); |
45
|
|
|
}, |
46
|
|
|
]; |
47
|
|
|
$rules[] = [['certificateType'], 'safe']; |
48
|
|
|
$rules[] = [['data'], 'validatePrices', 'on' => ['create', 'update']]; |
49
|
|
|
|
50
|
|
|
return $rules; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return array |
55
|
|
|
*/ |
56
|
|
|
public static function getPeriods() |
57
|
|
|
{ |
58
|
|
|
return [ |
59
|
|
|
1 => Yii::t('hipanel:finance:tariff', '{n, plural, one{# year} other{# years}}', ['n' => 1]), |
60
|
|
|
2 => Yii::t('hipanel:finance:tariff', '{n, plural, one{# year} other{# years}}', ['n' => 2]), |
61
|
|
|
3 => Yii::t('hipanel:finance:tariff', '{n, plural, one{# year} other{# years}}', ['n' => 3]), |
62
|
|
|
]; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
public function getPriceForPeriod($period) |
66
|
|
|
{ |
67
|
|
|
if (!isset(self::getPeriods()[$period])) { |
68
|
|
|
throw new InvalidConfigException('Period ' . $period . ' is not available'); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return $this->data['prices'][$period]; |
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function validatePrices() |
75
|
|
|
{ |
76
|
|
|
$periods = $this->getPeriods(); |
77
|
|
|
$validator = new NumberValidator(); |
78
|
|
|
|
79
|
|
|
foreach (array_keys($periods) as $period) { |
80
|
|
|
$validation = $validator->validate($this->data['prices'][$period]); |
|
|
|
|
81
|
|
|
if ($validation === false) { |
82
|
|
|
unset($this->data['prices'][$period]); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
$this->data = ['prices' => $this->data['prices']]; |
|
|
|
|
87
|
|
|
|
88
|
|
|
return true; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @return array |
93
|
|
|
*/ |
94
|
|
View Code Duplication |
public function getTypes() |
|
|
|
|
95
|
|
|
{ |
96
|
|
|
return [ |
97
|
|
|
static::TYPE_CERT_REGISTRATION => Yii::t('hipanel:finance:tariff', 'Registration'), |
98
|
|
|
static::TYPE_CERT_RENEW => Yii::t('hipanel:finance:tariff', 'Renewal'), |
99
|
|
|
]; |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|
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.