Issues (460)

Core/Controller/EditCliente.php (2 issues)

1
<?php
2
/**
3
 * This file is part of FacturaScripts
4
 * Copyright (C) 2017-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\Controller;
21
22
use FacturaScripts\Core\Base\DataBase\DataBaseWhere;
23
use FacturaScripts\Core\Lib\ExtendedController\BaseView;
24
use FacturaScripts\Core\Lib\ExtendedController\ComercialContactController;
25
use FacturaScripts\Core\Tools;
26
use FacturaScripts\Dinamic\Lib\CustomerRiskTools;
27
use FacturaScripts\Dinamic\Lib\RegimenIVA;
28
29
/**
30
 * Controller to edit a single item from the Cliente model
31
 *
32
 * @author       Carlos García Gómez           <[email protected]>
33
 * @author       Jose Antonio Cuello Principal <[email protected]>
34
 * @author       Fco. Antonio Moreno Pérez     <[email protected]>
35
 * @collaborator Daniel Fernández Giménez      <[email protected]>
36
 */
37
class EditCliente extends ComercialContactController
38
{
39
    /**
40
     * Returns the customer's risk on pending delivery notes.
41
     *
42
     * @return string
43
     */
44
    public function getDeliveryNotesRisk(): string
45
    {
46
        $codcliente = $this->getViewModelValue('EditCliente', 'codcliente');
47
        $total = empty($codcliente) ? 0 : CustomerRiskTools::getDeliveryNotesRisk($codcliente);
48
        return Tools::money($total);
49
    }
50
51
    public function getImageUrl(): string
52
    {
53
        $mvn = $this->getMainViewName();
54
        return $this->views[$mvn]->model->gravatar();
55
    }
56
57
    /**
58
     * Returns the customer's risk on unpaid invoices.
59
     *
60
     * @return string
61
     */
62
    public function getInvoicesRisk(): string
63
    {
64
        $codcliente = $this->getViewModelValue('EditCliente', 'codcliente');
65
        $total = empty($codcliente) ? 0 : CustomerRiskTools::getInvoicesRisk($codcliente);
66
        return Tools::money($total);
67
    }
68
69
    public function getModelClassName(): string
70
    {
71
        return 'Cliente';
72
    }
73
74
    /**
75
     * Returns the customer's risk on pending orders.
76
     *
77
     * @return string
78
     */
79
    public function getOrdersRisk(): string
80
    {
81
        $codcliente = $this->getViewModelValue('EditCliente', 'codcliente');
82
        $total = empty($codcliente) ? 0 : CustomerRiskTools::getOrdersRisk($codcliente);
83
        return Tools::money($total);
84
    }
85
86
    public function getPageData(): array
87
    {
88
        $data = parent::getPageData();
89
        $data['menu'] = 'sales';
90
        $data['title'] = 'customer';
91
        $data['icon'] = 'fas fa-users';
92
        return $data;
93
    }
94
95
    protected function createDocumentView(string $viewName, string $model, string $label)
96
    {
97
        $this->createCustomerListView($viewName, $model, $label);
98
99
        // botones
100
        $this->setSettings($viewName, 'btnPrint', true);
101
        $this->addButtonGroupDocument($viewName);
102
        $this->addButtonApproveDocument($viewName);
103
    }
104
105
    protected function createInvoiceView(string $viewName)
106
    {
107
        $this->createCustomerListView($viewName, 'FacturaCliente', 'invoices');
108
109
        // botones
110
        $this->setSettings($viewName, 'btnPrint', true);
111
        $this->addButtonLockInvoice($viewName);
112
    }
113
114
    /**
115
     * Create views
116
     */
117
    protected function createViews()
118
    {
119
        parent::createViews();
120
        $this->createContactsView();
121
        $this->addEditListView('EditCuentaBancoCliente', 'CuentaBancoCliente', 'customer-banking-accounts', 'fas fa-piggy-bank');
122
123
        if ($this->user->can('EditSubcuenta')) {
124
            $this->createSubaccountsView();
125
        }
126
127
        $this->createEmailsView();
128
        $this->createViewDocFiles();
129
130
        if ($this->user->can('EditFacturaCliente')) {
131
            $this->createInvoiceView('ListFacturaCliente');
132
            $this->createLineView('ListLineaFacturaCliente', 'LineaFacturaCliente');
133
        }
134
        if ($this->user->can('EditAlbaranCliente')) {
135
            $this->createDocumentView('ListAlbaranCliente', 'AlbaranCliente', 'delivery-notes');
136
        }
137
        if ($this->user->can('EditPedidoCliente')) {
138
            $this->createDocumentView('ListPedidoCliente', 'PedidoCliente', 'orders');
139
        }
140
        if ($this->user->can('EditPresupuestoCliente')) {
141
            $this->createDocumentView('ListPresupuestoCliente', 'PresupuestoCliente', 'estimations');
142
        }
143
        if ($this->user->can('EditReciboCliente')) {
144
            $this->createReceiptView('ListReciboCliente', 'ReciboCliente');
145
        }
146
    }
147
148
    /**
149
     * @return bool
150
     */
151
    protected function editAction()
152
    {
153
        $return = parent::editAction();
154
        if ($return && $this->active === $this->getMainViewName()) {
155
            $this->checkSubaccountLength($this->getModel()->codsubcuenta);
156
157
            // update contact email and phones when customer email or phones are updated
158
            $this->updateContact($this->views[$this->active]->model);
159
        }
160
161
        return $return;
162
    }
163
164
    /**
165
     * @return bool
166
     */
167
    protected function insertAction()
168
    {
169
        if (false === parent::insertAction()) {
170
            return false;
171
        }
172
173
        // redirect to returnUrl if return is defined
174
        $returnUrl = $this->request->query->get('return');
175
        if (!empty($returnUrl)) {
176
            $model = $this->views[$this->active]->model;
177
            $this->redirect($returnUrl . '?' . $model->primaryColumn() . '=' . $model->primaryColumnValue());
178
        }
179
180
        return true;
181
    }
182
183
    /**
184
     * Load view data procedure
185
     *
186
     * @param string $viewName
187
     * @param BaseView $view
188
     */
189
    protected function loadData($viewName, $view)
190
    {
191
        $mainViewName = $this->getMainViewName();
192
        $codcliente = $this->getViewModelValue($mainViewName, 'codcliente');
193
        $where = [new DataBaseWhere('codcliente', $codcliente)];
194
195
        switch ($viewName) {
196
            case 'EditCuentaBancoCliente':
197
                $view->loadData('', $where, ['codcuenta' => 'DESC']);
198
                break;
199
200
            case 'EditDireccionContacto':
201
                $view->loadData('', $where, ['idcontacto' => 'DESC']);
202
                break;
203
204
            case 'ListFacturaCliente':
205
                $view->loadData('', $where);
206
                $this->addButtonGenerateAccountingInvoices($viewName, $codcliente);
207
                break;
208
209
            case 'ListAlbaranCliente':
210
            case 'ListPedidoCliente':
211
            case 'ListPresupuestoCliente':
212
            case 'ListReciboCliente':
213
                $view->loadData('', $where);
214
                break;
215
216
            case 'ListLineaFacturaCliente':
217
                $inSQL = 'SELECT idfactura FROM facturascli WHERE codcliente = ' . $this->dataBase->var2str($codcliente);
218
                $where = [new DataBaseWhere('idfactura', $inSQL, 'IN')];
219
                $view->loadData('', $where);
220
                break;
221
222
            case $mainViewName:
223
                parent::loadData($viewName, $view);
224
                $this->loadLanguageValues($viewName);
225
                break;
226
227
            default:
228
                parent::loadData($viewName, $view);
229
                break;
230
        }
231
    }
232
233
    /**
234
     * Load the available language values from translator.
235
     */
236
    protected function loadLanguageValues(string $viewName)
237
    {
238
        $columnLangCode = $this->views[$viewName]->columnForName('language');
239
        if ($columnLangCode && $columnLangCode->widget->getType() === 'select') {
240
            $langs = [];
241
            foreach (Tools::lang()->getAvailableLanguages() as $key => $value) {
242
                $langs[] = ['value' => $key, 'title' => $value];
243
            }
244
245
            $columnLangCode->widget->setValuesFromArray($langs, false, true);
246
        }
247
    }
248
249
    protected function setCustomWidgetValues(string $viewName)
250
    {
251
        // Load values option to VAT Type select input
252
        $columnVATType = $this->views[$viewName]->columnForName('vat-regime');
253
        if ($columnVATType && $columnVATType->widget->getType() === 'select') {
254
            $columnVATType->widget->setValuesFromArrayKeys(RegimenIVA::all(), true);
0 ignored issues
show
The method setValuesFromArrayKeys() does not exist on FacturaScripts\Core\Lib\Widget\BaseWidget. Did you maybe mean setValue()? ( Ignorable by Annotation )

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

254
            $columnVATType->widget->/** @scrutinizer ignore-call */ 
255
                                    setValuesFromArrayKeys(RegimenIVA::all(), true);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
255
        }
256
257
        // Model exists?
258
        if (false === $this->views[$viewName]->model->exists()) {
259
            $this->views[$viewName]->disableColumn('billing-address');
260
            $this->views[$viewName]->disableColumn('shipping-address');
261
            return;
262
        }
263
264
        // Search for client contacts
265
        $codcliente = $this->getViewModelValue($viewName, 'codcliente');
266
        $where = [new DataBaseWhere('codcliente', $codcliente)];
267
        $contacts = $this->codeModel->all('contactos', 'idcontacto', 'descripcion', false, $where);
268
269
        // Load values option to default billing address from client contacts list
270
        $columnBilling = $this->views[$viewName]->columnForName('billing-address');
271
        if ($columnBilling && $columnBilling->widget->getType() === 'select') {
272
            $columnBilling->widget->setValuesFromCodeModel($contacts);
0 ignored issues
show
The method setValuesFromCodeModel() does not exist on FacturaScripts\Core\Lib\Widget\BaseWidget. Did you maybe mean setValue()? ( Ignorable by Annotation )

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

272
            $columnBilling->widget->/** @scrutinizer ignore-call */ 
273
                                    setValuesFromCodeModel($contacts);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
273
        }
274
275
        // Load values option to default shipping address from client contacts list
276
        $columnShipping = $this->views[$viewName]->columnForName('shipping-address');
277
        if ($columnShipping && $columnShipping->widget->getType() === 'select') {
278
            $contacts2 = $this->codeModel->all('contactos', 'idcontacto', 'descripcion', true, $where);
279
            $columnShipping->widget->setValuesFromCodeModel($contacts2);
280
        }
281
    }
282
}
283