Issues (464)

Core/Base/AjaxForms/PurchasesFooterHTML.php (2 issues)

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\Contract\PurchasesModInterface;
23
use FacturaScripts\Core\Base\Translator;
24
use FacturaScripts\Core\Model\Base\PurchaseDocument;
25
use FacturaScripts\Core\Model\User;
26
use FacturaScripts\Core\Tools;
27
28
/**
29
 * Description of PurchasesFooterHTML
30
 *
31
 * @author Carlos Garcia Gomez           <[email protected]>
32
 * @author Jose Antonio Cuello Principal <[email protected]>
33
 * @author Daniel Fernández Giménez      <[email protected]>
34
 */
35
class PurchasesFooterHTML
36
{
37
    use CommonSalesPurchases;
0 ignored issues
show
The trait FacturaScripts\Core\Base...ms\CommonSalesPurchases requires some properties which are not provided by FacturaScripts\Core\Base...rms\PurchasesFooterHTML: $generadoc, $hora, $admin, $codigo, $nick, $operacion, $femail, $codpago, $icon, $idempresa, $total, $nombre, $cifnif, $fecha, $codalmacen, $tipo, $codserie, $coddivisa, $fechadevengo, $actualizastock, $idfacturarect, $descripcion, $tasaconv, $nombrecorto, $netosindto, $editable, $level, $idestado
Loading history...
38
39
    /** @var PurchasesModInterface[] */
40
    private static $mods = [];
41
42
    public static function addMod(PurchasesModInterface $mod)
43
    {
44
        self::$mods[] = $mod;
45
    }
46
47
    public static function apply(PurchaseDocument &$model, array $formData, User $user)
48
    {
49
        // mods
50
        foreach (self::$mods as $mod) {
51
            $mod->applyBefore($model, $formData, $user);
52
        }
53
54
        self::$columnView = $formData['columnView'] ?? Tools::settings('default', 'columnetosubtotal', 'subtotal');
55
56
        $model->dtopor1 = isset($formData['dtopor1']) ? (float)$formData['dtopor1'] : $model->dtopor1;
57
        $model->dtopor2 = isset($formData['dtopor2']) ? (float)$formData['dtopor2'] : $model->dtopor2;
58
        $model->observaciones = $formData['observaciones'] ?? $model->observaciones;
59
60
        // mods
61
        foreach (self::$mods as $mod) {
62
            $mod->apply($model, $formData, $user);
63
        }
64
    }
65
66
    public static function assets()
67
    {
68
        // mods
69
        foreach (self::$mods as $mod) {
70
            $mod->assets();
71
        }
72
    }
73
74
    public static function render(PurchaseDocument $model): string
75
    {
76
        if (empty(self::$columnView)) {
77
            self::$columnView = Tools::settings('default', 'columnetosubtotal', 'subtotal');
78
        }
79
80
        if (empty($model->codproveedor)) {
81
            return '';
82
        }
83
84
        $i18n = new Translator();
0 ignored issues
show
Deprecated Code introduced by
The class FacturaScripts\Core\Base\Translator has been deprecated: since FacturaScripts 2023.06 ( Ignorable by Annotation )

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

84
        $i18n = /** @scrutinizer ignore-deprecated */ new Translator();
Loading history...
85
        return '<div class="container-fluid mt-3">'
86
            . '<div class="form-row">'
87
            . self::renderField($i18n, $model, '_productBtn')
88
            . self::renderField($i18n, $model, '_newLineBtn')
89
            . self::renderField($i18n, $model, '_sortableBtn')
90
            . self::renderField($i18n, $model, '_fastLineInput')
91
            . self::renderField($i18n, $model, '_subtotalNetoBtn')
92
            . '</div>'
93
            . '<div class="form-row">'
94
            . self::renderField($i18n, $model, 'observaciones')
95
            . self::renderNewFields($i18n, $model)
96
            . self::renderField($i18n, $model, 'netosindto')
97
            . self::renderField($i18n, $model, 'dtopor1')
98
            . self::renderField($i18n, $model, 'dtopor2')
99
            . self::renderField($i18n, $model, 'neto')
100
            . self::renderField($i18n, $model, 'totaliva')
101
            . self::renderField($i18n, $model, 'totalrecargo')
102
            . self::renderField($i18n, $model, 'totalirpf')
103
            . self::renderField($i18n, $model, 'total')
104
            . '</div>'
105
            . '<div class="form-row">'
106
            . '<div class="col-auto">'
107
            . self::renderField($i18n, $model, '_deleteBtn')
108
            . '</div>'
109
            . '<div class="col text-right">'
110
            . self::renderNewBtnFields($i18n, $model)
111
            . self::renderField($i18n, $model, '_modalFooter')
112
            . self::renderField($i18n, $model, '_undoBtn')
113
            . self::renderField($i18n, $model, '_saveBtn')
114
            . '</div>'
115
            . '</div>'
116
            . '</div>';
117
    }
118
119
    private static function modalFooter(Translator $i18n, PurchaseDocument $model): string
120
    {
121
        $htmlModal = self::renderNewModalFields($i18n, $model);
122
123
        if (empty($htmlModal)) {
124
            return '';
125
        }
126
127
        return '<button class="btn btn-outline-secondary mr-2" type="button" data-toggle="modal" data-target="#footerModal">'
128
            . '<i class="fas fa-plus fa-fw" aria-hidden="true"></i></button>'
129
            . self::modalFooterHtml($i18n, $htmlModal);
130
    }
131
132
    private static function modalFooterHtml(Translator $i18n, string $htmlModal): string
133
    {
134
        return '<div class="modal fade" id="footerModal" tabindex="-1" aria-labelledby="footerModalLabel" aria-hidden="true">'
135
            . '<div class="modal-dialog modal-dialog-centered modal-lg">'
136
            . '<div class="modal-content">'
137
            . '<div class="modal-header">'
138
            . '<h5 class="modal-title">' . $i18n->trans('detail') . ' ' . $i18n->trans('footer') . '</h5>'
139
            . '<button type="button" class="close" data-dismiss="modal" aria-label="Close">'
140
            . '<span aria-hidden="true">&times;</span>'
141
            . '</button>'
142
            . '</div>'
143
            . '<div class="modal-body">'
144
            . '<div class="form-row">'
145
            . $htmlModal
146
            . '</div>'
147
            . '</div>'
148
            . '<div class="modal-footer">'
149
            . '<button type="button" class="btn btn-secondary" data-dismiss="modal">' . $i18n->trans('close') . '</button>'
150
            . '<button type="button" class="btn btn-primary" data-dismiss="modal">' . $i18n->trans('accept') . '</button>'
151
            . '</div>'
152
            . '</div>'
153
            . '</div>'
154
            . '</div>';
155
    }
156
157
    private static function renderField(Translator $i18n, PurchaseDocument $model, string $field): ?string
158
    {
159
        foreach (self::$mods as $mod) {
160
            $html = $mod->renderField($i18n, $model, $field);
161
            if ($html !== null) {
162
                return $html;
163
            }
164
        }
165
166
        switch ($field) {
167
            case '_deleteBtn':
168
                return self::deleteBtn($i18n, $model, 'purchasesFormSave');
169
170
            case '_fastLineInput':
171
                return self::fastLineInput($i18n, $model, 'purchasesFastLine');
172
173
            case '_modalFooter':
174
                return self::modalFooter($i18n, $model);
175
176
            case '_newLineBtn':
177
                return self::newLineBtn($i18n, $model, 'purchasesFormAction');
178
179
            case '_productBtn':
180
                return self::productBtn($i18n, $model);
181
182
            case '_saveBtn':
183
                return self::saveBtn($i18n, $model, 'purchasesFormSave');
184
185
            case '_sortableBtn':
186
                return self::sortableBtn($i18n, $model);
187
188
            case '_subtotalNetoBtn':
189
                return self::subtotalNetoBtn($i18n);
190
191
            case '_undoBtn':
192
                return self::undoBtn($i18n, $model);
193
194
            case 'dtopor1':
195
                return self::dtopor1($i18n, $model, 'purchasesFormActionWait');
196
197
            case 'dtopor2':
198
                return self::dtopor2($i18n, $model, 'purchasesFormActionWait');
199
200
            case 'neto':
201
                return self::column($i18n, $model, 'neto', 'net', true);
202
203
            case 'netosindto':
204
                return self::netosindto($i18n, $model);
205
206
            case 'observaciones':
207
                return self::observaciones($i18n, $model);
208
209
            case 'total':
210
                return self::column($i18n, $model, 'total', 'total', true);
211
212
            case 'totalirpf':
213
                return self::column($i18n, $model, 'totalirpf', 'irpf', true);
214
215
            case 'totaliva':
216
                return self::column($i18n, $model, 'totaliva', 'taxes', true);
217
218
            case 'totalrecargo':
219
                return self::column($i18n, $model, 'totalrecargo', 're', true);
220
        }
221
222
        return null;
223
    }
224
225
    private static function renderNewBtnFields(Translator $i18n, PurchaseDocument $model): string
226
    {
227
        // cargamos los nuevos campos
228
        $newFields = [];
229
        foreach (self::$mods as $mod) {
230
            foreach ($mod->newBtnFields() as $field) {
231
                if (false === in_array($field, $newFields)) {
232
                    $newFields[] = $field;
233
                }
234
            }
235
        }
236
237
        // renderizamos los campos
238
        $html = '';
239
        foreach ($newFields as $field) {
240
            foreach (self::$mods as $mod) {
241
                $fieldHtml = $mod->renderField($i18n, $model, $field);
242
                if ($fieldHtml !== null) {
243
                    $html .= $fieldHtml;
244
                    break;
245
                }
246
            }
247
        }
248
        return $html;
249
    }
250
251
    private static function renderNewFields(Translator $i18n, PurchaseDocument $model): string
252
    {
253
        // cargamos los nuevos campos
254
        $newFields = [];
255
        foreach (self::$mods as $mod) {
256
            foreach ($mod->newFields() as $field) {
257
                if (false === in_array($field, $newFields)) {
258
                    $newFields[] = $field;
259
                }
260
            }
261
        }
262
263
        // renderizamos los campos
264
        $html = '';
265
        foreach ($newFields as $field) {
266
            foreach (self::$mods as $mod) {
267
                $fieldHtml = $mod->renderField($i18n, $model, $field);
268
                if ($fieldHtml !== null) {
269
                    $html .= $fieldHtml;
270
                    break;
271
                }
272
            }
273
        }
274
        return $html;
275
    }
276
277
    private static function renderNewModalFields(Translator $i18n, PurchaseDocument $model): string
278
    {
279
        // cargamos los nuevos campos
280
        $newFields = [];
281
        foreach (self::$mods as $mod) {
282
            foreach ($mod->newModalFields() as $field) {
283
                if (false === in_array($field, $newFields)) {
284
                    $newFields[] = $field;
285
                }
286
            }
287
        }
288
289
        // renderizamos los campos
290
        $html = '';
291
        foreach ($newFields as $field) {
292
            foreach (self::$mods as $mod) {
293
                $fieldHtml = $mod->renderField($i18n, $model, $field);
294
                if ($fieldHtml !== null) {
295
                    $html .= $fieldHtml;
296
                    break;
297
                }
298
            }
299
        }
300
301
        return $html;
302
    }
303
}
304