Completed
Push — master ( 754979...bc690a )
by Andrii
04:27
created

Purse::getServiceInvoices()   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 string month
27
 * @property Client clientModel
28
 * @property Document[] contracts
29
 * @property Document[] probations
30
 * @property Document[] acceptances
31
 * @property Document[] invoices
32
 * @property Document[] purchase_invoices
33
 * @property Document[] service_invoices
34
 * @property Document[] ndas
35
 */
36
class Purse extends \hipanel\base\Model
37
{
38
    use \hipanel\base\ModelTrait;
39
40
    /**
41
     * {@inheritdoc}
42
     */
43
    public function rules()
44
    {
45
        return [
46
            [['id', 'client_id', 'seller_id'], 'integer'],
47
            [['client', 'seller'], 'safe'],
48
            [['provided_services'], 'safe'],
49
            [['contact_id', 'requisite_id'], 'integer'],
50
            [['currency_id'], 'integer'],
51
            [['currency'], 'safe'],
52
            [['no', 'count'], 'integer'],
53
            [['credit', 'balance'], 'number'],
54
55
            [['id', 'contact_id'], 'required', 'on' => ['update-contact']],
56
            [['id', 'requisite_id'], 'required', 'on' => ['update-requisite']],
57
58
            [['month'], 'date', 'format' => 'php:Y-m', 'on' => ['generate-and-save-monthly-document']],
59
            [['month'], 'required', 'on' => ['generate-and-save-monthly-document']],
60
            [['type'], 'string', 'on' => ['generate-and-save-monthly-document', 'generate-and-save-document']],
61
        ];
62
    }
63
64
    public function getFiles()
65
    {
66
        return $this->hasMany(File::class, ['object_id' => 'id']);
67
    }
68
69
    public function getDocuments()
70
    {
71
        return $this->hasMany(Document::class, ['object_id' => 'id']);
72
    }
73
74
    public function getInvoices()
75
    {
76
        return $this->getDocumentsOfType('invoice');
77
    }
78
79
    public function getServiceInvoices()
80
    {
81
        return $this->getDocumentsOfType('service_invoice');
82
    }
83
84
    public function getPurchaseInvoices()
85
    {
86
        return $this->getDocumentsOfType('purchase_invoice');
87
    }
88
89
    public function getContracts()
90
    {
91
        return $this->getDocumentsOfType('contract');
92
    }
93
94
    public function getProbations()
95
    {
96
        return $this->getDocumentsOfType('probation');
97
    }
98
99
    public function getNdas()
100
    {
101
        return $this->getDocumentsOfType('nda');
102
    }
103
104
    public function getAcceptances()
105
    {
106
        return $this->getDocumentsOfType('acceptance');
107
    }
108
109
    public function getDocumentsOfType($type)
110
    {
111
        if (Yii::$app->user->can('document.read') === false) {
112
            return [];
113
        }
114
115
        $res = [];
116
        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...
117
            if ($doc->type === $type) {
118
                $res[$id] = $doc;
119
            }
120
        }
121
122
        return $res;
123
    }
124
125
    public function getClientModel()
126
    {
127
        return $this->hasOne(Client::class, ['id' => 'client_id']);
128
    }
129
130
    public function getContact()
131
    {
132
        return $this->hasOne(Contact::class, ['id' => 'contact_id']);
133
    }
134
135
    public function getRequisite()
136
    {
137
        return $this->hasOne(Contact::class, ['id' => 'requisite_id']);
138
    }
139
140
    /**
141
     * {@inheritdoc}
142
     */
143
    public function attributeLabels()
144
    {
145
        return $this->mergeAttributeLabels([
146
            'provided_services' => Yii::t('hipanel:finance', 'Provided services'),
147
            'currency' => Yii::t('hipanel:finance', 'Currency'),
148
            'invoices' => Yii::t('hipanel:finance', 'Invoices'),
149
            'serviceInvoices' => Yii::t('hipanel:finance', 'Service Invoices'),
150
            'purchaseInvoices' => Yii::t('hipanel:finance', 'Purchase Invoices'),
151
            'acceptances' => Yii::t('hipanel:finance', 'Acceptance reports'),
152
            'contracts' => Yii::t('hipanel:finance', 'Contracts'),
153
            'probations' => Yii::t('hipanel:finance', 'Probation'),
154
            'ndas' => Yii::t('hipanel:finance', 'NDA'),
155
            'contact_id' => Yii::t('hipanel:finance', 'Contact'),
156
            'requisite_id' => Yii::t('hipanel:finance', 'Requisite'),
157
            'month' => Yii::t('hipanel:finance', 'Period'),
158
        ]);
159
    }
160
161
    public function scenarioActions()
162
    {
163
        return [
164
            'update-contact' => 'update',
165
            'update-requisite' => 'update',
166
        ];
167
    }
168
169
    /**
170
     * Full available budget, including the credit
171
     */
172
    public function getBudget(): float
173
    {
174
        return (float)$this->balance + (float)$this->credit;
175
    }
176
}
177