Issues (461)

Core/Base/AjaxForms/AccountingModalHTML.php (1 issue)

1
<?php
2
/**
3
 * This file is part of FacturaScripts
4
 * Copyright (C) 2021-2023 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
20
namespace FacturaScripts\Core\Base\AjaxForms;
21
22
use FacturaScripts\Core\Base\DataBase\DataBaseWhere;
23
use FacturaScripts\Core\Tools;
24
use FacturaScripts\Core\Translator;
25
use FacturaScripts\Dinamic\Model\Asiento;
26
use FacturaScripts\Dinamic\Model\Subcuenta;
27
28
/**
29
 * Description of AccountingModalHTML
30
 *
31
 * @author Carlos Garcia Gomez           <[email protected]>
32
 * @author Jose Antonio Cuello Principal <[email protected]>
33
 */
34
class AccountingModalHTML
35
{
36
    /**
37
     * @var string
38
     */
39
    protected static $orden;
40
41
    /**
42
     * @var string
43
     */
44
    protected static $query;
45
46
    public static function apply(Asiento &$model, array $formData)
0 ignored issues
show
The parameter $model is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

46
    public static function apply(/** @scrutinizer ignore-unused */ Asiento &$model, array $formData)

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

Loading history...
47
    {
48
        self::$orden = $formData['fp_orden'] ?? 'ref_asc';
49
        self::$query = isset($formData['fp_query']) ? Tools::noHtml(mb_strtolower($formData['fp_query'], 'UTF8')) : '';
50
    }
51
52
    public static function render(Asiento $model): string
53
    {
54
        $i18n = new Translator();
55
        return static::modalSubaccount($i18n, $model);
56
    }
57
58
    public static function renderSubaccountList(Asiento $model): string
59
    {
60
        $tbody = '';
61
        $i18n = new Translator();
62
        foreach (static::getSubaccounts($model) as $subaccount) {
63
            $cssClass = $subaccount->saldo > 0 ? 'table-success clickableRow' : 'clickableRow';
64
            $onclick = '$(\'#findSubaccountModal\').modal(\'hide\');'
65
                . ' return newLineAction(\'' . $subaccount->codsubcuenta . '\');';
66
67
            $tbody .= '<tr class="' . $cssClass . '" onclick="' . $onclick . '">'
68
                . '<td><b>' . $subaccount->codsubcuenta . '</b> ' . $subaccount->descripcion . '</td>'
69
                . '<td class="text-right">' . Tools::money($subaccount->saldo) . '</td>'
70
                . '</tr>';
71
        }
72
73
        if (empty($tbody)) {
74
            $tbody .= '<tr class="table-warning"><td colspan="3">' . $i18n->trans('no-data') . '</td></tr>';
75
        }
76
77
        return '<table class="table table-hover mb-0">'
78
            . '<thead>'
79
            . '<tr>'
80
            . '<th>' . $i18n->trans('subaccount') . '</th>'
81
            . '<th class="text-right">' . $i18n->trans('balance') . '</th>'
82
            . '</tr>'
83
            . '</thead>'
84
            . '<tbody>' . $tbody . '</tbody>'
85
            . '</table>';
86
    }
87
88
    /**
89
     * @param Asiento $model
90
     *
91
     * @return Subcuenta[]
92
     */
93
    protected static function getSubaccounts(Asiento $model): array
94
    {
95
        $subaccount = new Subcuenta();
96
        if (empty($model->codejercicio)) {
97
            $model->setDate($model->fecha);
98
        }
99
        $where = [new DataBaseWhere('codejercicio', $model->codejercicio)];
100
        if (self::$query) {
101
            $where[] = new DataBaseWhere('descripcion|codsubcuenta', self::$query, 'XLIKE');
102
        }
103
104
        switch (self::$orden) {
105
            case 'desc_asc':
106
                $order = ['descripcion' => 'ASC'];
107
                break;
108
109
            case 'saldo_desc':
110
                $order = ['saldo' => 'DESC'];
111
                break;
112
113
            default:
114
                $order = ['codsubcuenta' => 'ASC'];
115
                break;
116
        }
117
118
        return $subaccount->all($where, $order);
119
    }
120
121
    protected static function modalSubaccount(Translator $i18n, Asiento $model): string
122
    {
123
        return '<div class="modal" id="findSubaccountModal" tabindex="-1" aria-hidden="true">'
124
            . '<div class="modal-dialog modal-xl">'
125
            . '<div class="modal-content">'
126
            . '<div class="modal-header">'
127
            . '<h5 class="modal-title"><i class="fas fa-book fa-fw"></i> ' . $i18n->trans('subaccounts') . '</h5>'
128
            . '<button type="button" class="close" data-dismiss="modal" aria-label="Close">'
129
            . '<span aria-hidden="true">&times;</span>'
130
            . '</button>'
131
            . '</div>'
132
            . '<div class="modal-body">'
133
            . '<div class="form-row">'
134
            . '<div class="col-sm">'
135
            . '<div class="input-group">'
136
            . '<input type="text" name="fp_query" class="form-control" id="findSubaccountInput" placeholder="' . $i18n->trans('search')
137
            . '" onkeyup="return findSubaccountSearch(\'find-subaccount\', \'0\', this);"/>'
138
            . '<div class="input-group-apend">'
139
            . '<button class="btn btn-primary" type="button" onclick="return accEntryFormAction(\'find-subaccount\', \'0\');"'
140
            . ' data-loading-text="<span class=\'spinner-border spinner-border-sm\' role=\'status\' aria-hidden=\'true\'></span>"><i class="fas fa-search"></i></button>'
141
            . '</div>'
142
            . '</div>'
143
            . '</div>'
144
            . static::orden($i18n)
145
            . '</div>'
146
            . '</div>'
147
            . '<div class="table-responsive" id="findSubaccountList">' . static::renderSubaccountList($model) . '</div>'
148
            . '</div>'
149
            . '</div>'
150
            . '</div>';
151
    }
152
153
    protected static function orden(Translator $i18n): string
154
    {
155
        return '<div class="col-sm">'
156
            . '<div class="input-group">'
157
            . '<div class="input-group-prepend"><span class="input-group-text"><i class="fas fa-sort-amount-down-alt"></i></span></div>'
158
            . '<select name="fp_orden" class="form-control" onchange="return accEntryFormAction(\'find-subaccount\', \'0\');">'
159
            . '<option value="code_asc">' . $i18n->trans('code') . '</option>'
160
            . '<option value="desc_asc">' . $i18n->trans('description') . '</option>'
161
            . '<option value="saldo_desc">' . $i18n->trans('balance') . '</option>'
162
            . '</select>'
163
            . '</div>'
164
            . '</div>';
165
    }
166
}
167