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\models\query\ChargeQuery; |
14
|
|
|
use Yii; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Class Charge. |
18
|
|
|
* |
19
|
|
|
* @author Dmytro Naumenko <[email protected]> |
20
|
|
|
* |
21
|
|
|
* @property int $id |
22
|
|
|
* @property string $unit |
23
|
|
|
* @property string $quantity |
24
|
|
|
* @property int $parent_id |
25
|
|
|
* @property string $type |
26
|
|
|
* @property string $name |
27
|
|
|
* @property string $ftype |
28
|
|
|
* @property string $currency |
29
|
|
|
* @property float $sum |
30
|
|
|
* @property float $is_payed |
31
|
|
|
* @property int object_id |
32
|
|
|
* @property TargetObject $commonObject |
33
|
|
|
* @property TargetObject $latestCommonObject |
34
|
|
|
* @property Bill $bill |
35
|
|
|
*/ |
36
|
|
|
class Charge extends Resource |
37
|
|
|
{ |
38
|
|
|
use \hipanel\base\ModelTrait; |
39
|
|
|
|
40
|
|
|
const SCENARIO_CREATE = 'create'; |
41
|
|
|
const SCENARIO_UPDATE = 'update'; |
42
|
|
|
|
43
|
|
|
private $isNewRecord; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @inheritDoc |
47
|
|
|
*/ |
48
|
|
|
public static function tableName() |
49
|
|
|
{ |
50
|
|
|
return 'resource'; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
public function rules() |
54
|
|
|
{ |
55
|
|
|
return [ |
56
|
|
|
[['id', 'type_id', 'object_id', 'bill_id', 'parent_id', 'client_id', 'tariff_id', 'seller_id', 'order_id'], 'integer'], |
57
|
|
|
[['class', 'name', 'unit', 'tariff', 'order_name', 'client', 'seller'], 'string'], |
58
|
|
|
[['type', 'label', 'ftype', 'time', 'type_label', 'currency'], 'safe'], |
59
|
|
|
[['is_payed'], 'boolean'], |
60
|
|
|
[['sum', 'quantity'], 'number'], |
61
|
|
|
[['unit'], 'default', 'value' => 'items', 'on' => [self::SCENARIO_CREATE, self::SCENARIO_UPDATE]], |
62
|
|
|
[['object_id', 'sum', 'type', 'quantity', 'unit'], 'required', 'on' => [self::SCENARIO_CREATE, self::SCENARIO_UPDATE]], |
63
|
|
|
[['id'], 'safe', 'on' => [self::SCENARIO_CREATE, self::SCENARIO_UPDATE]], |
64
|
|
|
]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
View Code Duplication |
public function attributeLabels() |
|
|
|
|
68
|
|
|
{ |
69
|
|
|
return array_merge(parent::attributeLabels(), [ |
70
|
|
|
'sum' => Yii::t('hipanel', 'Sum'), |
71
|
|
|
'type' => Yii::t('hipanel', 'Type'), |
72
|
|
|
'quantity' => Yii::t('hipanel', 'Quantity'), |
73
|
|
|
'label' => Yii::t('hipanel', 'Description'), |
74
|
|
|
'time' => Yii::t('hipanel', 'Time'), |
75
|
|
|
'object_id' => Yii::t('hipanel', 'Object Id'), |
76
|
|
|
'order_id' => Yii::t('hipanel', 'Order'), |
77
|
|
|
'is_payed' => Yii::t('hipanel:finance', 'Is payed?'), |
78
|
|
|
]); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
public function markAsNotNew() |
82
|
|
|
{ |
83
|
|
|
$this->isNewRecord = false; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
public function getCommonObject() |
87
|
|
|
{ |
88
|
|
|
return $this->hasOne(TargetObject::class, ['id' => 'id']); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
public function getLatestCommonObject() |
92
|
|
|
{ |
93
|
|
|
return $this->hasOne(TargetObject::class, ['id' => 'id']); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
public function getIsNewRecord() |
97
|
|
|
{ |
98
|
|
|
return $this->isNewRecord !== false && parent::getIsNewRecord(); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
public function getBill() |
102
|
|
|
{ |
103
|
|
|
return $this->hasOne(Bill::class, ['id' => 'id'])->inverseOf('charges'); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function isMonthly(): bool |
107
|
|
|
{ |
108
|
|
|
return strpos($this->ftype, 'monthly,') === 0; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* {@inheritdoc} |
113
|
|
|
* @return ChargeQuery |
114
|
|
|
*/ |
115
|
|
|
public static function find($options = []) |
116
|
|
|
{ |
117
|
|
|
return new ChargeQuery(get_called_class(), [ |
118
|
|
|
'options' => $options, |
119
|
|
|
]); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
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.