Passed
Push — REFACTOR_MAIN_INC_241020 ( 0ab19e )
by Rafael
50:33
created

Constant::getConstants()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 28
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 13
nc 3
nop 0
dl 0
loc 28
rs 9.8333
c 0
b 0
f 0
1
<?php
2
3
/* Copyright (C) 2024       Rafael San José         <[email protected]>
4
 *
5
 * This program is free software; you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation; either version 3 of the License, or
8
 * any later version.
9
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU General Public License
16
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17
 */
18
19
namespace Dolibarr\Core\Model;
20
21
use Carbon\Carbon;
22
use Dolibarr\Core\Base\Model;
23
24
/**
25
 * Class Const
26
 *
27
 * @property int $rowid
28
 * @property string $name
29
 * @property int $entity
30
 * @property string $value
31
 * @property string|null $type
32
 * @property int $visible
33
 * @property string|null $note
34
 * @property Carbon|null $tms
35
 */
36
class Constant extends Model
37
{
38
    public $timestamps = false;
39
    protected $table = 'const';
40
    protected $primaryKey = 'rowid';
41
    protected $casts = [
42
        'entity' => 'int',
43
        'visible' => 'int',
44
        'tms' => 'datetime'
45
    ];
46
    protected $fillable = [
47
        'name',
48
        'entity',
49
        'value',
50
        'type',
51
        'visible',
52
        'note',
53
        'tms'
54
    ];
55
56
    public static function getConstants()
57
    {
58
        global $conf, $user; // Suponiendo que estás usando variables globales
59
60
        // Construir la consulta
61
        $query = Constant::select('rowid')
62
            ->selectRaw(static::decrypt('name') . ' AS name')
63
            ->selectRaw(static::decrypt('value') . ' AS value')
64
            ->addSelect('type', 'note', 'entity');
65
66
        // Condiciones basadas en el modo multicompany
67
        if (!isModEnabled('multicompany')) {
68
            // Si no está habilitado el modo multicompany
69
            $query->whereIn('entity', [0, $conf->entity]);
70
        } else {
71
            // Si está habilitado el modo multicompany
72
            if ($user->entity) {
73
                // Si el usuario tiene una entidad, restringir la consulta
74
                $query->whereIn('entity', explode(',', $user->entity . ',' . $conf->entity));
75
            }
76
        }
77
78
        // Ordenar los resultados
79
        $query->orderBy('entity')
80
            ->orderBy('name', 'ASC');
81
82
        // Obtener los resultados
83
        return $query->get();
84
    }
85
}
86