Issues (461)

Core/Base/AjaxForms/PurchasesHeaderHTML.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
use FacturaScripts\Dinamic\Model\Proveedor;
28
29
/**
30
 * Description of PurchasesHeaderHTML
31
 *
32
 * @author Carlos Garcia Gomez           <[email protected]>
33
 * @author Jose Antonio Cuello Principal <[email protected]>
34
 * @author Daniel Fernández Giménez      <[email protected]>
35
 */
36
class PurchasesHeaderHTML
37
{
38
    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\PurchasesHeaderHTML: $dtopor2, $generadoc, $admin, $codigo, $nick, $icon, $dtopor1, $idempresa, $total, $codalmacen, $observaciones, $tipo, $fechadevengo, $actualizastock, $idfacturarect, $descripcion, $nombrecorto, $netosindto, $editable, $level, $idestado
Loading history...
39
40
    /** @var PurchasesModInterface[] */
41
    private static $mods = [];
42
43
    public static function addMod(PurchasesModInterface $mod)
44
    {
45
        self::$mods[] = $mod;
46
    }
47
48
    public static function apply(PurchaseDocument &$model, array $formData, User $user)
49
    {
50
        // mods
51
        foreach (self::$mods as $mod) {
52
            $mod->applyBefore($model, $formData, $user);
53
        }
54
55
        $proveedor = new Proveedor();
56
        if (empty($model->primaryColumnValue())) {
57
            // new record. Sets user and supplier
58
            $model->setAuthor($user);
59
            if (isset($formData['codproveedor']) && $formData['codproveedor'] && $proveedor->loadFromCode($formData['codproveedor'])) {
60
                $model->setSubject($proveedor);
61
                if (empty($formData['action']) || $formData['action'] === 'set-supplier') {
62
                    return;
63
                }
64
            }
65
        } elseif (isset($formData['action'], $formData['codproveedor']) &&
66
            $formData['action'] === 'set-supplier' &&
67
            $proveedor->loadFromCode($formData['codproveedor'])) {
68
            // existing record and change supplier
69
            $model->setSubject($proveedor);
70
            return;
71
        }
72
73
        $model->setWarehouse($formData['codalmacen'] ?? $model->codalmacen);
74
        $model->cifnif = $formData['cifnif'] ?? $model->cifnif;
75
        $model->coddivisa = $formData['coddivisa'] ?? $model->coddivisa;
76
        $model->codpago = $formData['codpago'] ?? $model->codpago;
77
        $model->codproveedor = $formData['codproveedor'] ?? $model->codproveedor;
78
        $model->codserie = $formData['codserie'] ?? $model->codserie;
79
        $model->fecha = empty($formData['fecha']) ? $model->fecha : Tools::date($formData['fecha']);
80
        $model->femail = isset($formData['femail']) && !empty($formData['femail']) ? $formData['femail'] : $model->femail;
81
        $model->hora = $formData['hora'] ?? $model->hora;
82
        $model->nombre = $formData['nombre'] ?? $model->nombre;
83
        $model->numproveedor = $formData['numproveedor'] ?? $model->numproveedor;
84
        $model->operacion = $formData['operacion'] ?? $model->operacion;
85
        $model->tasaconv = (float)($formData['tasaconv'] ?? $model->tasaconv);
86
87
        foreach (['fechadevengo'] as $key) {
88
            if (isset($formData[$key])) {
89
                $model->{$key} = empty($formData[$key]) ? null : $formData[$key];
90
            }
91
        }
92
93
        // mods
94
        foreach (self::$mods as $mod) {
95
            $mod->apply($model, $formData, $user);
96
        }
97
    }
98
99
    public static function assets()
100
    {
101
        // mods
102
        foreach (self::$mods as $mod) {
103
            $mod->assets();
104
        }
105
    }
106
107
    public static function render(PurchaseDocument $model): string
108
    {
109
        $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

109
        $i18n = /** @scrutinizer ignore-deprecated */ new Translator();
Loading history...
110
        return '<div class="container-fluid">'
111
            . '<div class="form-row align-items-end">'
112
            . self::renderField($i18n, $model, 'codproveedor')
113
            . self::renderField($i18n, $model, 'codalmacen')
114
            . self::renderField($i18n, $model, 'codserie')
115
            . self::renderField($i18n, $model, 'fecha')
116
            . self::renderNewFields($i18n, $model)
117
            . self::renderField($i18n, $model, 'numproveedor')
118
            . self::renderField($i18n, $model, 'codpago')
119
            . self::renderField($i18n, $model, 'total')
120
            . '</div>'
121
            . '<div class="form-row align-items-end">'
122
            . self::renderField($i18n, $model, '_detail')
123
            . self::renderField($i18n, $model, '_parents')
124
            . self::renderField($i18n, $model, '_children')
125
            . self::renderField($i18n, $model, '_email')
126
            . self::renderNewBtnFields($i18n, $model)
127
            . self::renderField($i18n, $model, '_paid')
128
            . self::renderField($i18n, $model, 'idestado')
129
            . '</div>'
130
            . '</div>';
131
    }
132
133
    private static function codproveedor(Translator $i18n, PurchaseDocument $model): string
134
    {
135
        $proveedor = new Proveedor();
136
        if (empty($model->codproveedor) || false === $proveedor->loadFromCode($model->codproveedor)) {
137
            return '<div class="col-sm-3">'
138
                . '<div class="form-group">' . $i18n->trans('supplier')
139
                . '<input type="hidden" name="codproveedor" />'
140
                . '<a href="#" id="btnFindSupplierModal" class="btn btn-block btn-primary" onclick="$(\'#findSupplierModal\').modal();'
141
                . ' $(\'#findSupplierInput\').focus(); return false;"><i class="fas fa-users fa-fw"></i> '
142
                . $i18n->trans('select') . '</a>'
143
                . '</div>'
144
                . '</div>'
145
                . self::detailModal($i18n, $model);
146
        }
147
148
        $btnProveedor = $model->editable ?
149
            '<button class="btn btn-outline-secondary" type="button" onclick="$(\'#findSupplierModal\').modal();'
150
            . ' $(\'#findSupplierInput\').focus(); return false;"><i class="fas fa-pen"></i></button>' :
151
            '<button class="btn btn-outline-secondary" type="button"><i class="fas fa-lock"></i></button>';
152
153
        $html = '<div class="col-sm-3 col-lg">'
154
            . '<div class="form-group">'
155
            . '<a href="' . $proveedor->url() . '">' . $i18n->trans('supplier') . '</a>'
156
            . '<input type="hidden" name="codproveedor" value="' . $model->codproveedor . '" />'
157
            . '<div class="input-group">'
158
            . '<input type="text" value="' . Tools::noHtml($proveedor->nombre) . '" class="form-control" readonly />'
159
            . '<div class="input-group-append">' . $btnProveedor . '</div>'
160
            . '</div>'
161
            . '</div>'
162
            . '</div>';
163
164
        if (empty($model->primaryColumnValue())) {
165
            $html .= self::detail($i18n, $model, true);
166
        }
167
168
        return $html;
169
    }
170
171
    private static function detail(Translator $i18n, PurchaseDocument $model, bool $new = false): string
172
    {
173
        if (empty($model->primaryColumnValue()) && $new === false) {
174
            // si el modelo es nuevo, ya hemos pintado el modal de detalle
175
            return '';
176
        }
177
178
        $css = $new ? 'col-sm-auto' : 'col-sm';
179
        return '<div class="' . $css . '">'
180
            . '<div class="form-group">'
181
            . '<button class="btn btn-outline-secondary" type="button" data-toggle="modal" data-target="#headerModal">'
182
            . '<i class="fas fa-edit fa-fw" aria-hidden="true"></i> ' . $i18n->trans('detail') . ' </button>'
183
            . '</div>'
184
            . '</div>'
185
            . self::detailModal($i18n, $model);
186
    }
187
188
    private static function detailModal(Translator $i18n, PurchaseDocument $model): string
189
    {
190
        return '<div class="modal fade" id="headerModal" tabindex="-1" aria-labelledby="headerModalLabel" aria-hidden="true">'
191
            . '<div class="modal-dialog modal-dialog-centered">'
192
            . '<div class="modal-content">'
193
            . '<div class="modal-header">'
194
            . '<h5 class="modal-title">' . $i18n->trans('detail') . '</h5>'
195
            . '<button type="button" class="close" data-dismiss="modal" aria-label="Close">'
196
            . '<span aria-hidden="true">&times;</span>'
197
            . '</button>'
198
            . '</div>'
199
            . '<div class="modal-body">'
200
            . '<div class="form-row">'
201
            . self::renderField($i18n, $model, 'nombre')
202
            . self::renderField($i18n, $model, 'cifnif')
203
            . self::renderField($i18n, $model, 'fechadevengo')
204
            . self::renderField($i18n, $model, 'hora')
205
            . self::renderField($i18n, $model, 'operacion')
206
            . self::renderField($i18n, $model, 'femail')
207
            . self::renderField($i18n, $model, 'coddivisa')
208
            . self::renderField($i18n, $model, 'tasaconv')
209
            . self::renderField($i18n, $model, 'user')
210
            . self::renderNewModalFields($i18n, $model)
211
            . '</div>'
212
            . '</div>'
213
            . '<div class="modal-footer">'
214
            . '<button type="button" class="btn btn-secondary" data-dismiss="modal">' . $i18n->trans('close') . '</button>'
215
            . '<button type="button" class="btn btn-primary" data-dismiss="modal">' . $i18n->trans('accept') . '</button>'
216
            . '</div>'
217
            . '</div>'
218
            . '</div>'
219
            . '</div>';
220
    }
221
222
    private static function nombre(Translator $i18n, PurchaseDocument $model): string
223
    {
224
        $attributes = $model->editable ? 'name="nombre" required=""' : 'disabled=""';
225
        return '<div class="col-sm-6">'
226
            . '<div class="form-group">' . $i18n->trans('business-name')
227
            . '<input type="text" ' . $attributes . ' value="' . Tools::noHtml($model->nombre) . '" class="form-control" maxlength="100" autocomplete="off" />'
228
            . '</div>'
229
            . '</div>';
230
    }
231
232
    private static function numproveedor(Translator $i18n, PurchaseDocument $model): string
233
    {
234
        $attributes = $model->editable ? 'name="numproveedor"' : 'disabled=""';
235
        return empty($model->codproveedor) ? '' : '<div class="col-sm-3 col-md-2 col-lg">'
236
            . '<div class="form-group">' . $i18n->trans('numsupplier')
237
            . '<input type="text" ' . $attributes . ' value="' . Tools::noHtml($model->numproveedor) . '" class="form-control" maxlength="50"'
238
            . ' placeholder="' . $i18n->trans('optional') . '" />'
239
            . '</div>'
240
            . '</div>';
241
    }
242
243
    private static function renderField(Translator $i18n, PurchaseDocument $model, string $field): ?string
244
    {
245
        foreach (self::$mods as $mod) {
246
            $html = $mod->renderField($i18n, $model, $field);
247
            if ($html !== null) {
248
                return $html;
249
            }
250
        }
251
252
        switch ($field) {
253
            case '_children':
254
                return self::children($i18n, $model);
255
256
            case '_detail':
257
                return self::detail($i18n, $model);
258
259
            case '_email':
260
                return self::email($i18n, $model);
261
262
            case '_fecha':
263
                return self::fecha($i18n, $model, false);
264
265
            case '_paid':
266
                return self::paid($i18n, $model, 'purchasesFormSave');
267
268
            case '_parents':
269
                return self::parents($i18n, $model);
270
271
            case 'cifnif':
272
                return self::cifnif($i18n, $model);
273
274
            case 'codalmacen':
275
                return self::codalmacen($i18n, $model, 'purchasesFormAction');
276
277
            case 'coddivisa':
278
                return self::coddivisa($i18n, $model);
279
280
            case 'codpago':
281
                return self::codpago($i18n, $model);
282
283
            case 'codproveedor':
284
                return self::codproveedor($i18n, $model);
285
286
            case 'codserie':
287
                return self::codserie($i18n, $model, 'purchasesFormAction');
288
289
            case 'fecha':
290
                return self::fecha($i18n, $model);
291
292
            case 'fechadevengo':
293
                return self::fechadevengo($i18n, $model);
294
295
            case 'femail':
296
                return self::femail($i18n, $model);
297
298
            case 'hora':
299
                return self::hora($i18n, $model);
300
301
            case 'idestado':
302
                return self::idestado($i18n, $model, 'purchasesFormSave');
303
304
            case 'nombre':
305
                return self::nombre($i18n, $model);
306
307
            case 'numproveedor':
308
                return self::numproveedor($i18n, $model);
309
310
            case 'operacion':
311
                return self::operacion($i18n, $model);
312
313
            case 'tasaconv':
314
                return self::tasaconv($i18n, $model);
315
316
            case 'total':
317
                return self::total($i18n, $model, 'purchasesFormSave');
318
319
            case 'user':
320
                return self::user($i18n, $model);
321
        }
322
323
        return null;
324
    }
325
326
    private static function renderNewBtnFields(Translator $i18n, PurchaseDocument $model): string
327
    {
328
        // cargamos los nuevos campos
329
        $newFields = [];
330
        foreach (self::$mods as $mod) {
331
            foreach ($mod->newBtnFields() as $field) {
332
                if (false === in_array($field, $newFields)) {
333
                    $newFields[] = $field;
334
                }
335
            }
336
        }
337
338
        // renderizamos los campos
339
        $html = '';
340
        foreach ($newFields as $field) {
341
            foreach (self::$mods as $mod) {
342
                $fieldHtml = $mod->renderField($i18n, $model, $field);
343
                if ($fieldHtml !== null) {
344
                    $html .= $fieldHtml;
345
                    break;
346
                }
347
            }
348
        }
349
        return $html;
350
    }
351
352
    private static function renderNewFields(Translator $i18n, PurchaseDocument $model): string
353
    {
354
        // cargamos los nuevos campos
355
        $newFields = [];
356
        foreach (self::$mods as $mod) {
357
            foreach ($mod->newFields() as $field) {
358
                if (false === in_array($field, $newFields)) {
359
                    $newFields[] = $field;
360
                }
361
            }
362
        }
363
364
        // renderizamos los campos
365
        $html = '';
366
        foreach ($newFields as $field) {
367
            foreach (self::$mods as $mod) {
368
                $fieldHtml = $mod->renderField($i18n, $model, $field);
369
                if ($fieldHtml !== null) {
370
                    $html .= $fieldHtml;
371
                    break;
372
                }
373
            }
374
        }
375
        return $html;
376
    }
377
378
    private static function renderNewModalFields(Translator $i18n, PurchaseDocument $model): string
379
    {
380
        // cargamos los nuevos campos
381
        $newFields = [];
382
        foreach (self::$mods as $mod) {
383
            foreach ($mod->newModalFields() as $field) {
384
                if (false === in_array($field, $newFields)) {
385
                    $newFields[] = $field;
386
                }
387
            }
388
        }
389
390
        // renderizamos los campos
391
        $html = '';
392
        foreach ($newFields as $field) {
393
            foreach (self::$mods as $mod) {
394
                $fieldHtml = $mod->renderField($i18n, $model, $field);
395
                if ($fieldHtml !== null) {
396
                    $html .= $fieldHtml;
397
                    break;
398
                }
399
            }
400
        }
401
        return $html;
402
    }
403
}
404