Completed
Push — master ( 753652...d0e0e5 )
by Dmitry
04:54
created

Purse::getBudget()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 4
cp 0
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
crap 2
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\models\File;
14
use hipanel\modules\client\models\Client;
15
use hipanel\modules\client\models\Contact;
16
use hipanel\modules\document\models\Document;
17
use Yii;
18
19
/**
20
 * Class Purse.
21
 *
22
 * @property string|int id
23
 * @property string|float currency
24
 * @property string|float balance
25
 * @property string credit
26
 * @property Client clientModel
27
 * @property Document[] contracts
28
 * @property Document[] probations
29
 * @property Document[] acceptances
30
 * @property Document[] invoices
31
 * @property Document[] ndas
32
 */
33
class Purse extends \hipanel\base\Model
34
{
35
    use \hipanel\base\ModelTrait;
36
37
    /**
38
     * {@inheritdoc}
39
     */
40
    public function rules()
41
    {
42
        return [
43
            [['id', 'client_id', 'seller_id'], 'integer'],
44
            [['client', 'seller'], 'safe'],
45
            [['provided_services'], 'safe'],
46
            [['contact_id', 'requisite_id'], 'integer'],
47
            [['currency_id'], 'integer'],
48
            [['currency'], 'safe'],
49
            [['no', 'count'], 'integer'],
50
            [['credit', 'balance'], 'number'],
51
52
            [['id', 'contact_id'], 'required', 'on' => ['update-contact']],
53
            [['id', 'requisite_id'], 'required', 'on' => ['update-requisite']],
54
55
            [['month'], 'date', 'format' => 'php:Y-m', 'on' => ['generate-and-save-monthly-document']],
56
            [['month'], 'required', 'on' => ['generate-and-save-monthly-document']],
57
            [['type'], 'string', 'on' => ['generate-and-save-monthly-document', 'generate-and-save-document']],
58
        ];
59
    }
60
61
    public function getFiles()
62
    {
63
        return $this->hasMany(File::class, ['object_id' => 'id']);
64
    }
65
66
    public function getDocuments()
67
    {
68
        return $this->hasMany(Document::class, ['object_id' => 'id']);
69
    }
70
71
    public function getInvoices()
72
    {
73
        return $this->getDocumentsOfType('invoice');
74
    }
75
76
    public function getContracts()
77
    {
78
        return $this->getDocumentsOfType('contract');
79
    }
80
81
    public function getProbations()
82
    {
83
        return $this->getDocumentsOfType('probation');
84
    }
85
86
    public function getNdas()
87
    {
88
        return $this->getDocumentsOfType('nda');
89
    }
90
91
    public function getAcceptances()
92
    {
93
        return $this->getDocumentsOfType('acceptance');
94
    }
95
96
    public function getDocumentsOfType($type)
97
    {
98
        if (Yii::$app->user->can('document.read') === false) {
99
            return [];
100
        }
101
102
        $res = [];
103
        foreach ($this->documents as $id => $doc) {
0 ignored issues
show
Documentation introduced by
The property documents does not exist on object<hipanel\modules\finance\models\Purse>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

If the property has read access only, you can use the @property-read annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
104
            if ($doc->type === $type) {
105
                $res[$id] = $doc;
106
            }
107
        }
108
109
        return $res;
110
    }
111
112
    public function getClientModel()
113
    {
114
        return $this->hasOne(Client::class, ['id' => 'client_id']);
115
    }
116
117
    public function getContact()
118
    {
119
        return $this->hasOne(Contact::class, ['id' => 'contact_id']);
120
    }
121
122
    public function getRequisite()
123
    {
124
        return $this->hasOne(Contact::class, ['id' => 'requisite_id']);
125
    }
126
127
    /**
128
     * {@inheritdoc}
129
     */
130
    public function attributeLabels()
131
    {
132
        return $this->mergeAttributeLabels([
133
            'provided_services' => Yii::t('hipanel:finance', 'Provided services'),
134
            'currency' => Yii::t('hipanel:finance', 'Currency'),
135
            'invoices' => Yii::t('hipanel:finance', 'Invoices'),
136
            'acceptances' => Yii::t('hipanel:finance', 'Acceptance reports'),
137
            'contracts' => Yii::t('hipanel:finance', 'Contracts'),
138
            'probations' => Yii::t('hipanel:finance', 'Probation'),
139
            'ndas' => Yii::t('hipanel:finance', 'NDA'),
140
            'contact_id' => Yii::t('hipanel:finance', 'Contact'),
141
            'requisite_id' => Yii::t('hipanel:finance', 'Requisite'),
142
            'month' => Yii::t('hipanel:finance', 'Period'),
143
        ]);
144
    }
145
146
    public function scenarioActions()
147
    {
148
        return [
149
            'update-contact' => 'update',
150
            'update-requisite' => 'update',
151
        ];
152
    }
153
154
    /**
155
     * Full available budget, including the credit
156
     */
157
    public function getBudget(): float
158
    {
159
        return (float)$this->balance + (float)$this->credit;
160
    }
161
}
162