Issues (459)

Core/Controller/EditContacto.php (1 issue)

1
<?php
2
/**
3
 * This file is part of FacturaScripts
4
 * Copyright (C) 2018-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\DocFilesTrait;
25
use FacturaScripts\Core\Lib\ExtendedController\EditController;
26
use FacturaScripts\Core\Tools;
27
use FacturaScripts\Dinamic\Model\Contacto;
28
use FacturaScripts\Dinamic\Model\RoleAccess;
29
30
/**
31
 * Controller to edit a single item from the Contacto model
32
 *
33
 * @author Carlos García Gómez <[email protected]>
34
 */
35
class EditContacto extends EditController
36
{
37
    use DocFilesTrait;
0 ignored issues
show
The trait FacturaScripts\Core\Lib\...ontroller\DocFilesTrait requires some properties which are not provided by FacturaScripts\Core\Controller\EditContacto: $allowUpdate, $logLevels, $query, $files, $allowDelete
Loading history...
38
39
    public function getImageUrl(): string
40
    {
41
        $mvn = $this->getMainViewName();
42
        return $this->views[$mvn]->model->gravatar();
43
    }
44
45
    public function getModelClassName(): string
46
    {
47
        return 'Contacto';
48
    }
49
50
    public function getPageData(): array
51
    {
52
        $data = parent::getPageData();
53
        $data['menu'] = 'sales';
54
        $data['title'] = 'contact';
55
        $data['icon'] = 'fas fa-address-book';
56
        return $data;
57
    }
58
59
    protected function addConversionButtons(string $viewName, BaseView $view)
60
    {
61
        $accessClient = $this->getRolePermissions('EditCliente');
62
        if (empty($view->model->codcliente) && $accessClient['allowupdate']) {
63
            $this->addButton($viewName, [
64
                'action' => 'convert-into-customer',
65
                'color' => 'success',
66
                'icon' => 'fas fa-user-check',
67
                'label' => 'convert-into-customer'
68
            ]);
69
        }
70
71
        $accessSupplier = $this->getRolePermissions('EditProveedor');
72
        if (empty($view->model->codproveedor) && $accessSupplier['allowupdate']) {
73
            $this->addButton($viewName, [
74
                'action' => 'convert-into-supplier',
75
                'color' => 'success',
76
                'icon' => 'fas fa-user-cog',
77
                'label' => 'convert-into-supplier'
78
            ]);
79
        }
80
    }
81
82
    protected function checkViesAction(): bool
83
    {
84
        $model = $this->getModel();
85
        if (false === $model->loadFromCode($this->request->get('code'))) {
86
            return true;
87
        }
88
89
        $model->checkVies();
90
        return true;
91
    }
92
93
    protected function createCustomerAction()
94
    {
95
        $access = $this->getRolePermissions('EditCliente');
96
        if (false === $access['allowupdate']) {
97
            Tools::log()->warning('not-allowed-update');
98
            return;
99
        }
100
101
        $mvn = $this->getMainViewName();
102
        $customer = $this->views[$mvn]->model->getCustomer();
103
        if ($customer->exists()) {
104
            Tools::log()->notice('record-updated-correctly');
105
            $this->redirect($customer->url() . '&action=save-ok');
106
            return;
107
        }
108
109
        Tools::log()->error('record-save-error');
110
    }
111
112
    protected function createEmailsView(string $viewName = 'ListEmailSent')
113
    {
114
        $this->addListView($viewName, 'EmailSent', 'emails-sent', 'fas fa-envelope');
115
        $this->views[$viewName]->addOrderBy(['date'], 'date', 2);
116
        $this->views[$viewName]->addSearchFields(['addressee', 'body', 'subject']);
117
118
        // desactivamos la columna de destinatario
119
        $this->views[$viewName]->disableColumn('to');
120
121
        // desactivamos el botón de nuevo
122
        $this->setSettings($viewName, 'btnNew', false);
123
    }
124
125
    protected function createSupplierAction()
126
    {
127
        $access = $this->getRolePermissions('EditProveedor');
128
        if (false === $access['allowupdate']) {
129
            Tools::log()->warning('not-allowed-update');
130
            return;
131
        }
132
133
        $mvn = $this->getMainViewName();
134
        $supplier = $this->views[$mvn]->model->getSupplier();
135
        if ($supplier->exists()) {
136
            Tools::log()->notice('record-updated-correctly');
137
            $this->redirect($supplier->url() . '&action=save-ok');
138
            return;
139
        }
140
141
        Tools::log()->error('record-save-error');
142
    }
143
144
    /**
145
     * Create views.
146
     */
147
    protected function createViews()
148
    {
149
        parent::createViews();
150
        $this->createEmailsView();
151
        $this->createViewDocFiles();
152
    }
153
154
    /**
155
     * @return bool
156
     */
157
    protected function editAction()
158
    {
159
        $return = parent::editAction();
160
        if ($return && $this->active === $this->getMainViewName()) {
161
            $this->updateRelations($this->views[$this->active]->model);
162
        }
163
164
        return $return;
165
    }
166
167
    /**
168
     * Run the controller after actions
169
     *
170
     * @param string $action
171
     */
172
    protected function execAfterAction($action)
173
    {
174
        switch ($action) {
175
            case 'convert-into-customer':
176
                $this->createCustomerAction();
177
                break;
178
179
            case 'convert-into-supplier':
180
                $this->createSupplierAction();
181
                break;
182
183
            default:
184
                parent::execAfterAction($action);
185
        }
186
    }
187
188
    /**
189
     * @param string $action
190
     *
191
     * @return bool
192
     */
193
    protected function execPreviousAction($action): bool
194
    {
195
        switch ($action) {
196
            case 'add-file':
197
                return $this->addFileAction();
198
199
            case 'check-vies':
200
                return $this->checkViesAction();
201
202
            case 'delete-file':
203
                return $this->deleteFileAction();
204
205
            case 'edit-file':
206
                return $this->editFileAction();
207
208
            case 'unlink-file':
209
                return $this->unlinkFileAction();
210
        }
211
212
        return parent::execPreviousAction($action);
213
    }
214
215
    protected function getRolePermissions(string $pageName): array
216
    {
217
        $access = [
218
            'allowdelete' => $this->user->admin,
219
            'allowupdate' => $this->user->admin,
220
            'onlyownerdata' => $this->user->admin
221
        ];
222
        foreach (RoleAccess::allFromUser($this->user->nick, $pageName) as $rolesPageUser) {
223
            if ($rolesPageUser->allowdelete) {
224
                $access['allowdelete'] = true;
225
            }
226
            if ($rolesPageUser->allowupdate) {
227
                $access['allowupdate'] = true;
228
            }
229
            if ($rolesPageUser->onlyownerdata) {
230
                $access['onlyownerdata'] = true;
231
            }
232
        }
233
        return $access;
234
    }
235
236
    /**
237
     * @param string $viewName
238
     * @param BaseView $view
239
     */
240
    protected function loadData($viewName, $view)
241
    {
242
        $mvn = $this->getMainViewName();
243
244
        switch ($viewName) {
245
            case 'docfiles':
246
                $this->loadDataDocFiles($view, $this->getModelClassName(), $this->getModel()->primaryColumnValue());
247
                break;
248
249
            case 'ListEmailSent':
250
                $email = $this->getViewModelValue($mvn, 'email');
251
                if (empty($email)) {
252
                    $this->setSettings($viewName, 'active', false);
253
                    break;
254
                }
255
256
                $where = [new DataBaseWhere('addressee', $email)];
257
                $view->loadData('', $where);
258
259
                // añadimos un botón para enviar un nuevo email
260
                $this->addButton($viewName, [
261
                    'action' => 'SendMail?email=' . $email,
262
                    'color' => 'success',
263
                    'icon' => 'fas fa-envelope',
264
                    'label' => 'send',
265
                    'type' => 'link'
266
                ]);
267
                break;
268
269
            case $mvn:
270
                parent::loadData($viewName, $view);
271
                $this->loadLanguageValues($viewName);
272
                if (false === $view->model->exists()) {
273
                    break;
274
                }
275
                if ($this->permissions->allowUpdate) {
276
                    $this->addConversionButtons($viewName, $view);
277
                }
278
                $this->addButton($viewName, [
279
                    'action' => 'check-vies',
280
                    'color' => 'info',
281
                    'icon' => 'fas fa-check-double',
282
                    'label' => 'check-vies'
283
                ]);
284
                break;
285
        }
286
    }
287
288
    /**
289
     * Load the available language values from translator.
290
     */
291
    protected function loadLanguageValues(string $viewName)
292
    {
293
        $columnLangCode = $this->views[$viewName]->columnForName('language');
294
        if ($columnLangCode && $columnLangCode->widget->getType() === 'select') {
295
            $langs = [];
296
            foreach (Tools::lang()->getAvailableLanguages() as $key => $value) {
297
                $langs[] = ['value' => $key, 'title' => $value];
298
            }
299
300
            $columnLangCode->widget->setValuesFromArray($langs, false, true);
301
        }
302
    }
303
304
    /**
305
     * @param Contacto $contact
306
     */
307
    protected function updateRelations($contact)
308
    {
309
        $customer = $contact->getCustomer(false);
310
        if ($customer->idcontactofact == $contact->idcontacto && $customer->exists()) {
311
            $customer->email = $contact->email;
312
            $customer->fax = $contact->fax;
313
            $customer->telefono1 = $contact->telefono1;
314
            $customer->telefono2 = $contact->telefono2;
315
            $customer->save();
316
        }
317
318
        $supplier = $contact->getSupplier(false);
319
        if ($supplier->idcontacto == $contact->idcontacto && $supplier->exists()) {
320
            $supplier->email = $contact->email;
321
            $supplier->fax = $contact->fax;
322
            $supplier->telefono1 = $contact->telefono1;
323
            $supplier->telefono2 = $contact->telefono2;
324
            $supplier->save();
325
        }
326
    }
327
}
328