1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace hipanel\modules\finance\models; |
4
|
|
|
|
5
|
|
|
use hipanel\models\Ref; |
6
|
|
|
use hipanel\modules\finance\models\factories\PriceModelFactory; |
7
|
|
|
use Yii; |
8
|
|
|
use yii\helpers\Inflector; |
9
|
|
|
use yii\helpers\StringHelper; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Class Price |
13
|
|
|
* |
14
|
|
|
* @property int $id |
15
|
|
|
* @property int $plan_id |
16
|
|
|
* @property string|int $object_id |
17
|
|
|
* @property string|float $price |
18
|
|
|
* @property string $currency |
19
|
|
|
* @property string|int $main_object_id |
20
|
|
|
* @property string $unit |
21
|
|
|
* |
22
|
|
|
* @property TargetObject $object |
23
|
|
|
* @property Plan $plan |
24
|
|
|
* |
25
|
|
|
* @author Dmytro Naumenko <[email protected]> |
26
|
|
|
*/ |
27
|
|
|
class Price extends \hipanel\base\Model |
28
|
|
|
{ |
29
|
|
|
use \hipanel\base\ModelTrait; |
30
|
|
|
|
31
|
|
|
const SCENARIO_CREATE = 'create'; |
32
|
|
|
const SCENARIO_UPDATE = 'update'; |
33
|
|
|
const SCENARIO_DELETE = 'delete'; |
34
|
|
|
|
35
|
|
|
public function rules() |
36
|
|
|
{ |
37
|
|
|
return array_merge(parent::rules(), [ |
38
|
|
|
[['id', 'parent_id', 'plan_id', 'object_id', 'type_id', 'unit_id', 'currency_id', 'main_object_id'], 'integer'], |
39
|
|
|
[['type', 'plan_name', 'unit', 'currency', 'note', 'data'], 'string'], |
40
|
|
|
[['quantity', 'price'], 'number'], |
41
|
|
|
[['class'], 'string'], // todo: probably, refactor is needed |
42
|
|
|
|
43
|
|
|
[['plan_id', 'type', 'price', 'currency'], 'required', 'on' => ['create', 'update']], |
44
|
|
|
[['id'], 'required', 'on' => ['update', 'set-note', 'delete']], |
45
|
|
|
[['class'], 'default', 'value' => function ($model) { |
|
|
|
|
46
|
|
|
return (new \ReflectionClass($this))->getShortName(); |
47
|
|
|
}], |
48
|
|
|
[['class'], 'string'], |
49
|
|
|
]); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
View Code Duplication |
public function attributeLabels() |
|
|
|
|
53
|
|
|
{ |
54
|
|
|
return [ |
55
|
|
|
'plan_id' => Yii::t('hipanel:finance', 'Plan'), |
56
|
|
|
'plan' => Yii::t('hipanel:finance', 'Plan'), |
57
|
|
|
'quantity' => Yii::t('hipanel:finance', 'Prepaid'), |
58
|
|
|
'unit' => Yii::t('hipanel:finance', 'Unit'), |
59
|
|
|
'price' => Yii::t('hipanel:finance', 'Price'), |
60
|
|
|
'note' => Yii::t('hipanel', 'Note'), |
61
|
|
|
'type' => Yii::t('hipanel', 'Type'), |
62
|
|
|
]; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
View Code Duplication |
public function getTypeOptions() |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
return Ref::getList('type,bill', null, [ |
68
|
|
|
'select' => 'oname_label', |
69
|
|
|
'pnames' => 'monthly,overuse', |
70
|
|
|
'with_recursive' => 1, |
71
|
|
|
'mapOptions' => ['from' => 'oname'], |
72
|
|
|
]); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
View Code Duplication |
public function getUnitOptions() |
|
|
|
|
76
|
|
|
{ |
77
|
|
|
return Ref::getList('type,unit', null, [ |
78
|
|
|
'with_recursive' => 1, |
79
|
|
|
'select' => 'oname_label', |
80
|
|
|
'mapOptions' => ['from' => 'oname'], |
81
|
|
|
]); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
public function getCurrencyOptions() |
85
|
|
|
{ |
86
|
|
|
return Ref::getList('type,currency'); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function getObject() |
90
|
|
|
{ |
91
|
|
|
return $this->hasOne(TargetObject::class, ['id' => 'id']); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function getPlan() |
95
|
|
|
{ |
96
|
|
|
return $this->hasOne(Plan::class, ['id' => 'plan_id']); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public static function tableName() |
100
|
|
|
{ |
101
|
|
|
return Inflector::camel2id(StringHelper::basename(__CLASS__), '-'); |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
/** |
105
|
|
|
* {@inheritdoc} |
106
|
|
|
*/ |
107
|
|
|
public static function instantiate($row) |
108
|
|
|
{ |
109
|
|
|
if (empty($row['class'])) { |
110
|
|
|
return parent::instance($row); |
|
|
|
|
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** @var PriceModelFactory $factory */ |
114
|
|
|
$factory = Yii::$container->get(PriceModelFactory::class); |
115
|
|
|
return $factory->build($row['class']); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.