-
Notifications
You must be signed in to change notification settings - Fork 246
/
PurchasesHeaderHTML.php
336 lines (290 loc) · 12.9 KB
/
PurchasesHeaderHTML.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
<?php
/**
* This file is part of FacturaScripts
* Copyright (C) 2021-2022 Carlos Garcia Gomez <carlos@facturascripts.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
namespace FacturaScripts\Core\Base\AjaxForms;
use FacturaScripts\Core\Base\Contract\PurchasesModInterface;
use FacturaScripts\Core\Base\Translator;
use FacturaScripts\Core\Model\Base\PurchaseDocument;
use FacturaScripts\Core\Model\User;
use FacturaScripts\Dinamic\Model\Proveedor;
/**
* Description of PurchasesHeaderHTML
*
* @author Carlos Garcia Gomez <carlos@facturascripts.com>
* @author Jose Antonio Cuello Principal <yopli2000@gmail.com>
*/
class PurchasesHeaderHTML
{
use CommonSalesPurchases;
/** @var PurchasesModInterface[] */
private static $mods = [];
public static function addMod(PurchasesModInterface $mod)
{
self::$mods[] = $mod;
}
public static function apply(PurchaseDocument &$model, array $formData, User $user)
{
// mods
foreach (self::$mods as $mod) {
$mod->applyBefore($model, $formData, $user);
}
$proveedor = new Proveedor();
if (empty($model->primaryColumnValue())) {
// new record. Sets user and supplier
$model->setAuthor($user);
if (isset($formData['codproveedor']) && $formData['codproveedor'] && $proveedor->loadFromCode($formData['codproveedor'])) {
$model->setSubject($proveedor);
if (empty($formData['action']) || $formData['action'] === 'set-supplier') {
return;
}
}
} elseif (isset($formData['action'], $formData['codproveedor']) &&
$formData['action'] === 'set-supplier' &&
$proveedor->loadFromCode($formData['codproveedor'])) {
// existing record and change supplier
$model->setSubject($proveedor);
return;
}
$model->cifnif = $formData['cifnif'] ?? $model->cifnif;
$model->codalmacen = $formData['codalmacen'] ?? $model->codalmacen;
$model->coddivisa = $formData['coddivisa'] ?? $model->coddivisa;
$model->codpago = $formData['codpago'] ?? $model->codpago;
$model->codproveedor = $formData['codproveedor'] ?? $model->codproveedor;
$model->codserie = $formData['codserie'] ?? $model->codserie;
$model->fecha = $formData['fecha'] ?? $model->fecha;
$model->femail = isset($formData['femail']) && !empty($formData['femail']) ? $formData['femail'] : $model->femail;
$model->hora = $formData['hora'] ?? $model->hora;
$model->nombre = $formData['nombre'] ?? $model->nombre;
$model->numproveedor = $formData['numproveedor'] ?? $model->numproveedor;
$model->tasaconv = (float)($formData['tasaconv'] ?? $model->tasaconv);
// mods
foreach (self::$mods as $mod) {
$mod->apply($model, $formData, $user);
}
}
public static function assets()
{
// mods
foreach (self::$mods as $mod) {
$mod->assets();
}
}
public static function render(PurchaseDocument $model): string
{
$i18n = new Translator();
return '<div class="container-fluid">'
. '<div class="form-row align-items-end">'
. self::renderField($i18n, $model, 'codproveedor')
. self::renderField($i18n, $model, 'codalmacen')
. self::renderField($i18n, $model, 'codserie')
. self::renderField($i18n, $model, 'fecha')
. self::renderField($i18n, $model, 'numproveedor')
. self::renderField($i18n, $model, 'codpago')
. self::renderField($i18n, $model, 'total')
. '</div>'
. '<div class="form-row align-items-end">'
. self::renderField($i18n, $model, '_detail')
. self::renderField($i18n, $model, '_parents')
. self::renderField($i18n, $model, '_children')
. self::renderField($i18n, $model, '_email')
. self::renderField($i18n, $model, '_paid')
. self::renderField($i18n, $model, 'idestado')
. '</div>'
. '</div>';
}
private static function codproveedor(Translator $i18n, PurchaseDocument $model): string
{
$proveedor = new Proveedor();
if (empty($model->codproveedor) || false === $proveedor->loadFromCode($model->codproveedor)) {
return '<div class="col-sm-3">'
. '<div class="form-group">' . $i18n->trans('supplier')
. '<input type="hidden" name="codproveedor" />'
. '<a href="#" id="btnFindSupplierModal" class="btn btn-block btn-primary" onclick="$(\'#findSupplierModal\').modal();'
. ' $(\'#findSupplierInput\').focus(); return false;"><i class="fas fa-users fa-fw"></i> '
. $i18n->trans('select') . '</a>'
. '</div>'
. '</div>';
}
$btnProveedor = $model->editable ?
'<button class="btn btn-outline-secondary" type="button" onclick="$(\'#findSupplierModal\').modal();'
. ' $(\'#findSupplierInput\').focus(); return false;"><i class="fas fa-pen"></i></button>' :
'<button class="btn btn-outline-secondary" type="button"><i class="fas fa-lock"></i></button>';
$html = '<div class="col-sm-3 col-lg">'
. '<div class="form-group">'
. '<a href="' . $proveedor->url() . '">' . $i18n->trans('supplier') . '</a>'
. '<input type="hidden" name="codproveedor" value="' . $model->codproveedor . '" />'
. '<div class="input-group">'
. '<input type="text" value="' . $proveedor->nombre . '" class="form-control" readonly />'
. '<div class="input-group-append">' . $btnProveedor . '</div>'
. '</div>'
. '</div>'
. '</div>';
if (empty($model->primaryColumnValue())) {
$html .= self::detail($i18n, $model, true);
}
return $html;
}
private static function detail(Translator $i18n, PurchaseDocument $model, bool $new = false): string
{
if (empty($model->primaryColumnValue()) && $new === false) {
// si el modelo es nuevo, ya hemos pintado el modal de detalle
return '';
}
$css = $new ? 'col-sm-auto' : 'col-sm';
return '<div class="' . $css . '">'
. '<div class="form-group">'
. '<button class="btn btn-outline-secondary" type="button" data-toggle="modal" data-target="#headerModal">'
. '<i class="fas fa-edit fa-fw" aria-hidden="true"></i> ' . $i18n->trans('detail') . ' </button>'
. '</div>'
. '</div>'
. self::detailModal($i18n, $model);
}
private static function detailModal(Translator $i18n, PurchaseDocument $model): string
{
return '<div class="modal fade" id="headerModal" tabindex="-1" aria-labelledby="headerModalLabel" aria-hidden="true">'
. '<div class="modal-dialog modal-dialog-centered">'
. '<div class="modal-content">'
. '<div class="modal-header">'
. '<h5 class="modal-title">' . $i18n->trans('detail') . '</h5>'
. '<button type="button" class="close" data-dismiss="modal" aria-label="Close">'
. '<span aria-hidden="true">×</span>'
. '</button>'
. '</div>'
. '<div class="modal-body">'
. '<div class="form-row">'
. self::renderField($i18n, $model, 'nombreproveedor')
. self::renderField($i18n, $model, 'cifnif')
. self::renderField($i18n, $model, 'coddivisa')
. self::renderField($i18n, $model, 'tasaconv')
. self::renderField($i18n, $model, '_fecha')
. self::renderField($i18n, $model, 'hora')
. self::renderField($i18n, $model, 'femail')
. self::renderField($i18n, $model, 'user')
. self::renderNewFields($i18n, $model)
. '</div>'
. '</div>'
. '<div class="modal-footer">'
. '<button type="button" class="btn btn-secondary" data-dismiss="modal">'
. $i18n->trans('close')
. '</button>'
. '<button type="button" class="btn btn-primary" data-dismiss="modal">'
. $i18n->trans('accept')
. '</button>'
. '</div>'
. '</div>'
. '</div>'
. '</div>';
}
private static function nombreproveedor(Translator $i18n, PurchaseDocument $model): string
{
$attributes = $model->editable ? 'name="nombre" required=""' : 'disabled=""';
return '<div class="col-sm-12">'
. '<div class="form-group">' . $i18n->trans('business-name')
. '<input type="text" ' . $attributes . ' value="' . $model->nombre . '" class="form-control" maxlength="100" autocomplete="off" />'
. '</div>'
. '</div>';
}
private static function numproveedor(Translator $i18n, PurchaseDocument $model): string
{
$attributes = $model->editable ? 'name="numproveedor"' : 'disabled=""';
return empty($model->codproveedor) ? '' : '<div class="col-sm-3 col-md-2 col-lg">'
. '<div class="form-group">' . $i18n->trans('numsupplier')
. '<input type="text" ' . $attributes . ' value="' . $model->numproveedor . '" class="form-control" maxlength="50"'
. ' placeholder="' . $i18n->trans('optional') . '" />'
. '</div>'
. '</div>';
}
private static function renderField(Translator $i18n, PurchaseDocument $model, string $field): ?string
{
foreach (self::$mods as $mod) {
$html = $mod->renderField($i18n, $model, $field);
if ($html !== null) {
return $html;
}
}
switch ($field) {
case '_children':
return self::children($i18n, $model);
case '_detail':
return self::detail($i18n, $model);
case '_email':
return self::email($i18n, $model);
case '_fecha':
return self::fecha($i18n, $model, false);
case '_paid':
return self::paid($i18n, $model, 'purchasesFormSave');
case '_parents':
return self::parents($i18n, $model);
case 'cifnif':
return self::cifnif($i18n, $model);
case 'codalmacen':
return self::codalmacen($i18n, $model);
case 'coddivisa':
return self::coddivisa($i18n, $model);
case 'codpago':
return self::codpago($i18n, $model);
case 'codproveedor':
return self::codproveedor($i18n, $model);
case 'codserie':
return self::codserie($i18n, $model, 'purchasesFormAction');
case 'fecha':
return self::fecha($i18n, $model);
case 'femail':
return self::femail($i18n, $model);
case 'hora':
return self::hora($i18n, $model);
case 'idestado':
return self::idestado($i18n, $model, 'purchasesFormSave');
case 'nombreproveedor':
return self::nombreproveedor($i18n, $model);
case 'numproveedor':
return self::numproveedor($i18n, $model);
case 'tasaconv':
return self::tasaconv($i18n, $model);
case 'total':
return self::total($i18n, $model, 'purchasesFormSave');
case 'user':
return self::user($i18n, $model);
}
return null;
}
private static function renderNewFields(Translator $i18n, PurchaseDocument $model): string
{
// cargamos los nuevos campos
$newFields = [];
foreach (self::$mods as $mod) {
foreach ($mod->newFields() as $field) {
if (false === in_array($field, $newFields)) {
$newFields[] = $field;
}
}
}
// renderizamos los campos
$html = '';
foreach ($newFields as $field) {
foreach (self::$mods as $mod) {
$fieldHtml = $mod->renderField($i18n, $model, $field);
if ($fieldHtml !== null) {
$html .= $fieldHtml;
break;
}
}
}
return $html;
}
}