Test Setup Failed
Push — master ( c39af1...761570 )
by Carlos
06:13 queued 14s
created

SubcuentaSaldo::getSQLFrom()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 2
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * This file is part of FacturaScripts
4
 * Copyright (C) 2018-2019 Carlos Garcia Gomez <[email protected]>
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as
8
 * published by the Free Software Foundation, either version 3 of the
9
 * License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public License
17
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18
 */
19
namespace FacturaScripts\Core\Model\ModelView;
20
21
use FacturaScripts\Core\Model\Base\ModelView;
22
use FacturaScripts\Core\Base\DataBase\DataBaseWhere;
23
24
/**
25
 * Auxiliary model to load a sumary of subaccount
26
 *
27
 * @author Artex Trading sa     <[email protected]>
28
 * @author Carlos García Gómez  <[email protected]>
29
 *
30
 * @property double $debe
31
 * @property double $haber
32
 * @property double $saldo
33
 * @property int $idsubcuenta
34
 * @property int $mes
35
 * @property int $canal
36
 */
37
class SubcuentaSaldo extends ModelView
38
{
39
40
    /**
41
     * Reset the values of all model view properties.
42
     */
43
    public function clear()
44
    {
45
        parent::clear();
46
        $this->debe = 0.0;
47
        $this->haber = 0.0;
48
        $this->saldo = 0.0;
49
        $this->mes = 0;
50
        $this->canal = 0;
51
    }
52
53
    /**
54
     * List of fields or columns to select clausule
55
     */
56
    protected function getFields(): array
57
    {
58
        return [
59
            'idsubcuenta' => 'partidas.idsubcuenta',
60
            'codsubcuenta' => 'partidas.codsubcuenta',
61
            'codejercicio' => 'asientos.codejercicio',
62
            'canal' => 'asientos.canal',
63
            'mes' => 'EXTRACT(MONTH FROM asientos.fecha)',
64
            'debe' => 'SUM(partidas.debe)',
65
            'haber' => 'SUM(partidas.haber)',
66
        ];
67
    }
68
69
    /**
70
     * Return Group By fields
71
     *
72
     * @return string
73
     */
74
    protected function getGroupFields(): string
75
    {
76
        return 'partidas.idsubcuenta,'
77
            . 'partidas.codsubcuenta,'
78
            . 'asientos.codejercicio,'
79
            . 'asientos.canal,'
80
            . 'EXTRACT(MONTH FROM asientos.fecha)';
81
    }
82
83
    /**
84
     * List of tables related to from clausule
85
     */
86
    protected function getSQLFrom(): string
87
    {
88
        return 'partidas'
89
            . ' INNER JOIN asientos ON asientos.idasiento = partidas.idasiento';
90
    }
91
92
    /**
93
     * List of tables required for the execution of the view.
94
     */
95
    protected function getTables(): array
96
    {
97
        return [
98
            'asientos',
99
            'partidas'
100
        ];
101
    }
102
103
    /**
104
     * Assign the values of the $data array to the model view properties.
105
     *
106
     * @param array $data
107
     */
108
    protected function loadFromData($data)
109
    {
110
        parent::loadFromData($data);
111
        $this->saldo = $this->debe - $this->haber;
112
    }
113
114
    /**
115
     * Load in an array "detail" the monthly and channel balances of a sub-account
116
     * and return the sum of them.
117
     *
118
     * @param int   $idSubAccount
119
     * @param int   $channel
120
     * @param array $detail
121
     *
122
     * @return float
123
     */
124
    public function setSubAccountBalance($idSubAccount, $channel, &$detail): float
125
    {
126
        $result = 0;
127
        $where = [
128
            new DataBaseWhere('partidas.idsubcuenta', $idSubAccount),
129
            new DataBaseWhere('asientos.canal', $channel)
130
        ];
131
        foreach ($this->all($where, ['mes' => 'ASC']) as $values) {
132
            $detail[$values->mes - 1] = round($values->saldo, (int) FS_NF0);
0 ignored issues
show
Bug Best Practice introduced by
The property mes does not exist on FacturaScripts\Core\Model\Base\ModelView. Since you implemented __get, consider adding a @property annotation.
Loading history...
Bug Best Practice introduced by
The property saldo does not exist on FacturaScripts\Core\Model\Base\ModelView. Since you implemented __get, consider adding a @property annotation.
Loading history...
133
            $result += $values->saldo;
134
        }
135
136
        return round($result, (int) FS_NF0);
137
    }
138
}
139