Completed
Push — master ( c6b5de...db50e7 )
by Dmitry
04:45
created

src/models/Bill.php (2 issues)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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
37
    public static $i18nDictionary = 'hipanel:finance';
38
39
    public function behaviors()
40
    {
41
        return [
42
            [
43
                'class' => BillNegation::class,
44
                'negativeTypes' => static::negativeTypes(),
45
            ],
46
        ];
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function rules()
53
    {
54
        return [
55
            [['client_id', 'seller_id', 'id'], 'integer'],
56
            [['object_id', 'tariff_id'], 'integer'],
57
            [['client', 'seller', 'bill', 'unit'], 'safe'],
58
            [['domain', 'server'], 'safe'],
59
            [['sum', 'balance', 'quantity'], 'number'],
60
            [['currency', 'label', 'descr'], 'safe'],
61
            [['object', 'domains', 'tariff'], 'safe'],
62
            [['type', 'gtype', 'class', 'ftype'], 'safe'],
63
            [['class_label'], 'safe'],
64
            [['type_label', 'gtype_label'], 'safe'],
65
            [['time'], 'date', 'format' => 'php:Y-m-d H:i:s'],
66
67
            [['id'], 'required', 'on' => [self::SCENARIO_UPDATE, self::SCENARIO_DELETE]],
68
            [['client_id'], 'integer', 'on' => [self::SCENARIO_CREATE]],
69
            [['currency', 'sum', 'type', 'label'], 'required', 'on' => [self::SCENARIO_CREATE, self::SCENARIO_UPDATE]],
70
            ['sum', function ($attribute, $params, $validator) {
0 ignored issues
show
The parameter $params is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
The parameter $validator is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
71
                if ($this->{$attribute} < 0 && in_array($this->type, static::negativeTypes(), true)) {
72
                    $this->addError($attribute, Yii::t('hipanel:finance', 'The entered value for the selected payment type can not be negative.'));
73
                }
74
            }, 'on' => [self::SCENARIO_CREATE, self::SCENARIO_UPDATE]],
75
            [['client_id', 'sum', 'time'], 'required', 'on' => [self::SCENARIO_CREATE]],
76
            [['client'], 'safe', 'on' => [self::SCENARIO_CREATE]],
77
            [['no'], 'safe'],
78
        ];
79
    }
80
81
    /**
82
     * {@inheritdoc}
83
     */
84
    public function attributeLabels()
85
    {
86
        return $this->mergeAttributeLabels([
87
            'object' => Yii::t('hipanel', 'Object'),
88
            'client' => Yii::t('hipanel', 'Client'),
89
            'time' => Yii::t('hipanel', 'Time'),
90
            'currency' => Yii::t('hipanel', 'Currency'),
91
            'quantity' => Yii::t('hipanel', 'Quantity'),
92
            'balance' => Yii::t('hipanel', 'Balance'),
93
            'gtype' => Yii::t('hipanel', 'Type'),
94
            'gtype_label' => Yii::t('hipanel', 'Type'),
95
            'sum' => Yii::t('hipanel:finance', 'Sum'),
96
            'tariff' => Yii::t('hipanel:finance', 'Tariff'),
97
            'tariff_id' => Yii::t('hipanel:finance', 'Tariff'),
98
        ]);
99
    }
100
101
    public function getCharges()
102
    {
103
        return $this->hasMany(Charge::class, ['bill_id' => 'id'])->inverseOf('bill');
104
    }
105
106
    public static function negativeTypes()
107
    {
108
        return [
109
            'correction,negative',
110
            'overuse,backup_du',
111
            'overuse,backup_traf',
112
            'overuse,domain_num',
113
            'overuse,ip_num',
114
            'overuse,isp5',
115
            'overuse,server_traf',
116
            'overuse,server_traf95',
117
            'overuse,server_traf95_in',
118
            'overuse,server_traf95_max',
119
            'overuse,server_du',
120
            'overuse,server_traf_in',
121
            'overuse,server_traf_max',
122
            'overuse,support_time',
123
        ];
124
    }
125
}
126