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\models; |
12
|
|
|
|
13
|
|
|
use hipanel\modules\finance\behaviors\BillNegation; |
14
|
|
|
use Yii; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class Bill. |
18
|
|
|
* |
19
|
|
|
* @author Dmytro Naumenko <[email protected]> |
20
|
|
|
* |
21
|
|
|
* @property string $type |
22
|
|
|
* @property string $unit |
23
|
|
|
* @property string $quantity |
24
|
|
|
* @property Charge[] charges |
25
|
|
|
*/ |
26
|
|
|
class Bill extends \hipanel\base\Model |
27
|
|
|
{ |
28
|
|
|
use \hipanel\base\ModelTrait; |
29
|
|
|
|
30
|
|
|
public $time_from; |
31
|
|
|
public $time_till; |
32
|
|
|
|
33
|
|
|
const SCENARIO_CREATE = 'create'; |
34
|
|
|
const SCENARIO_UPDATE = 'update'; |
35
|
|
|
const SCENARIO_DELETE = 'delete'; |
36
|
|
|
const SCENARIO_TRANSFER = 'create-transfer'; |
37
|
|
|
|
38
|
|
|
public static $i18nDictionary = 'hipanel:finance'; |
39
|
|
|
|
40
|
|
|
public function behaviors() |
41
|
|
|
{ |
42
|
|
|
return [ |
43
|
|
|
[ |
44
|
|
|
'class' => BillNegation::class, |
45
|
|
|
'negativeTypes' => static::negativeTypes(), |
46
|
|
|
], |
47
|
|
|
]; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
|
|
public function rules() |
54
|
|
|
{ |
55
|
|
|
return [ |
56
|
|
|
[['client_id', 'seller_id', 'id'], 'integer'], |
57
|
|
|
[['object_id', 'tariff_id'], 'integer'], |
58
|
|
|
[['client', 'seller', 'bill', 'unit'], 'safe'], |
59
|
|
|
[['domain', 'server'], 'safe'], |
60
|
|
|
[['sum', 'balance', 'quantity'], 'number'], |
61
|
|
|
[['currency', 'label', 'descr'], 'safe'], |
62
|
|
|
[['object', 'domains', 'tariff'], 'safe'], |
63
|
|
|
[['type', 'gtype', 'class', 'ftype'], 'safe'], |
64
|
|
|
[['class_label'], 'safe'], |
65
|
|
|
[['type_label', 'gtype_label'], 'safe'], |
66
|
|
|
[['time'], 'date', 'format' => 'php:Y-m-d H:i:s'], |
67
|
|
|
|
68
|
|
|
[['id'], 'required', 'on' => [self::SCENARIO_UPDATE, self::SCENARIO_DELETE]], |
69
|
|
|
[['client_id'], 'integer', 'on' => [self::SCENARIO_CREATE]], |
70
|
|
|
[['currency', 'sum', 'type', 'label'], 'required', 'on' => [self::SCENARIO_CREATE, self::SCENARIO_UPDATE]], |
71
|
|
|
['sum', function ($attribute, $params, $validator) { |
|
|
|
|
72
|
|
|
if ($this->{$attribute} < 0 && in_array($this->type, static::negativeTypes(), true)) { |
73
|
|
|
$this->addError($attribute, Yii::t('hipanel:finance', 'The entered value for the selected payment type can not be negative.')); |
74
|
|
|
} |
75
|
|
|
}, 'on' => [self::SCENARIO_CREATE, self::SCENARIO_UPDATE]], |
76
|
|
|
[['client_id', 'sum', 'time'], 'required', 'on' => [self::SCENARIO_CREATE]], |
77
|
|
|
[['client_id', 'receiver_id', 'currency_id'], 'integer', 'on' => [self::SCENARIO_TRANSFER]], |
78
|
|
|
[['client_id', 'receiver_id', 'sum', 'currency', 'time'], 'required', 'on' => [self::SCENARIO_TRANSFER]], |
79
|
|
|
[['client'], 'safe', 'on' => [self::SCENARIO_CREATE, self::SCENARIO_TRANSFER]], |
80
|
|
|
[['receiver'], 'safe', 'on' => [self::SCENARIO_TRANSFER]], |
81
|
|
|
[['no'], 'safe'], |
82
|
|
|
]; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* {@inheritdoc} |
87
|
|
|
*/ |
88
|
|
|
public function attributeLabels() |
89
|
|
|
{ |
90
|
|
|
return $this->mergeAttributeLabels([ |
91
|
|
|
'object' => Yii::t('hipanel', 'Object'), |
92
|
|
|
'client' => Yii::t('hipanel', 'Client'), |
93
|
|
|
'time' => Yii::t('hipanel', 'Time'), |
94
|
|
|
'currency' => Yii::t('hipanel', 'Currency'), |
95
|
|
|
'quantity' => Yii::t('hipanel', 'Quantity'), |
96
|
|
|
'balance' => Yii::t('hipanel', 'Balance'), |
97
|
|
|
'gtype' => Yii::t('hipanel', 'Type'), |
98
|
|
|
'gtype_label' => Yii::t('hipanel', 'Type'), |
99
|
|
|
'sum' => Yii::t('hipanel:finance', 'Sum'), |
100
|
|
|
'tariff' => Yii::t('hipanel:finance', 'Tariff'), |
101
|
|
|
'tariff_id' => Yii::t('hipanel:finance', 'Tariff'), |
102
|
|
|
]); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
public function getCharges() |
106
|
|
|
{ |
107
|
|
|
return $this->hasMany(Charge::class, ['bill_id' => 'id'])->inverseOf('bill'); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
public static function negativeTypes() |
111
|
|
|
{ |
112
|
|
|
return [ |
113
|
|
|
'transfer,minus', |
114
|
|
|
'correction,negative', |
115
|
|
|
'overuse,backup_du', |
116
|
|
|
'overuse,backup_traf', |
117
|
|
|
'overuse,domain_num', |
118
|
|
|
'overuse,ip_num', |
119
|
|
|
'overuse,isp5', |
120
|
|
|
'overuse,server_traf', |
121
|
|
|
'overuse,server_traf95', |
122
|
|
|
'overuse,server_traf95_in', |
123
|
|
|
'overuse,server_traf95_max', |
124
|
|
|
'overuse,server_du', |
125
|
|
|
'overuse,server_traf_in', |
126
|
|
|
'overuse,server_traf_max', |
127
|
|
|
'overuse,support_time', |
128
|
|
|
]; |
129
|
|
|
} |
130
|
|
|
} |
131
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.