Passed
Push — master ( a1d016...c39af1 )
by Carlos
04:29 queued 11s
created

EditFamilia::removeProductAction()   A

Complexity

Conditions 5
Paths 5

Size

Total Lines 19
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 11
dl 0
loc 19
rs 9.6111
c 0
b 0
f 0
cc 5
nc 5
nop 0
1
<?php
2
/**
3
 * This file is part of FacturaScripts
4
 * Copyright (C) 2017-2019 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
namespace FacturaScripts\Core\Controller;
20
21
use FacturaScripts\Core\Base\DataBase\DataBaseWhere;
22
use FacturaScripts\Core\Lib\ExtendedController\BaseView;
23
use FacturaScripts\Core\Lib\ExtendedController\EditController;
24
use FacturaScripts\Dinamic\Model\Producto;
25
26
/**
27
 * Controller to edit a single item from the Familia model
28
 *
29
 * @author Carlos García Gómez          <[email protected]>
30
 * @author Artex Trading sa             <[email protected]>
31
 * @author Fco. Antonio Moreno Pérez    <[email protected]>
32
 */
33
class EditFamilia extends EditController
34
{
35
36
    /**
37
     * 
38
     * @return string
39
     */
40
    public function getModelClassName()
41
    {
42
        return 'Familia';
43
    }
44
45
    /**
46
     * Returns basic page attributes
47
     *
48
     * @return array
49
     */
50
    public function getPageData()
51
    {
52
        $data = parent::getPageData();
53
        $data['menu'] = 'warehouse';
54
        $data['title'] = 'family';
55
        $data['icon'] = 'fas fa-sitemap';
56
        return $data;
57
    }
58
59
    protected function addProductAction()
60
    {
61
        $codes = $this->request->request->get('code', []);
62
        if (!is_array($codes)) {
63
            return;
64
        }
65
66
        $num = 0;
67
        foreach ($codes as $code) {
68
            $product = new Producto();
69
            if ($product->loadFromCode($code)) {
70
                $product->codfamilia = $this->request->query->get('code');
71
                if ($product->save()) {
72
                    $num++;
73
                }
74
            }
75
        }
76
77
        $this->toolBox()->i18nLog()->notice('items-added-correctly', ['%num%' => $num]);
78
    }
79
80
    /**
81
     * Load views
82
     */
83
    protected function createViews()
84
    {
85
        parent::createViews();
86
        $this->setTabsPosition('bottom');
87
88
        /// more tabs
89
        $this->createViewProducts();
90
        $this->createViewNewProducts();
91
        $this->createViewFamilies();
92
    }
93
94
    /**
95
     * 
96
     * @param string $viewName
97
     */
98
    protected function createViewFamilies(string $viewName = 'ListFamilia')
99
    {
100
        $this->addListView($viewName, 'Familia', 'subfamilies', 'fas fa-sitemap');
101
        $this->views[$viewName]->addOrderBy(['codfamilia'], 'code');
102
103
        /// disable column
104
        $this->views[$viewName]->disableColumn('parent');
105
106
        /// disable button
107
        $this->setSettings($viewName, 'btnDelete', false);
108
    }
109
110
    /**
111
     * 
112
     * @param string $viewName
113
     */
114
    protected function createViewNewProducts(string $viewName = 'ListProducto-new')
115
    {
116
        $this->addListView($viewName, 'Producto', 'add', 'fas fa-folder-plus');
117
        $this->createViewProductsCommon($viewName);
118
119
        /// add action button
120
        $newButton = [
121
            'action' => 'add-product',
122
            'color' => 'success',
123
            'icon' => 'fas fa-folder-plus',
124
            'label' => 'add',
125
        ];
126
        $this->addButton($viewName, $newButton);
127
    }
128
129
    /**
130
     * 
131
     * @param string $viewName
132
     */
133
    protected function createViewProducts(string $viewName = 'ListProducto')
134
    {
135
        $this->addListView($viewName, 'Producto', 'products', 'fas fa-cubes');
136
        $this->createViewProductsCommon($viewName);
137
138
        /// add action button
139
        $newButton = [
140
            'action' => 'remove-product',
141
            'color' => 'danger',
142
            'confirm' => true,
143
            'icon' => 'fas fa-folder-minus',
144
            'label' => 'remove-from-list',
145
        ];
146
        $this->addButton($viewName, $newButton);
147
    }
148
149
    /**
150
     * 
151
     * @param string $viewName
152
     */
153
    protected function createViewProductsCommon(string $viewName)
154
    {
155
        $this->views[$viewName]->addOrderBy(['referencia'], 'reference', 1);
156
        $this->views[$viewName]->addOrderBy(['precio'], 'price');
157
        $this->views[$viewName]->addOrderBy(['stock'], 'stock');
158
        $this->views[$viewName]->searchFields = ['referencia', 'descripcion'];
159
160
        /// disable columns and buttons
161
        $this->views[$viewName]->disableColumn('family');
162
        $this->setSettings($viewName, 'btnNew', false);
163
        $this->setSettings($viewName, 'btnDelete', false);
164
    }
165
166
    /**
167
     * 
168
     * @param string $action
169
     *
170
     * @return bool
171
     */
172
    protected function execPreviousAction($action)
173
    {
174
        switch ($action) {
175
            case 'add-product':
176
                $this->addProductAction();
177
                return true;
178
179
            case 'remove-product':
180
                $this->removeProductAction();
181
                return true;
182
183
            default:
184
                return parent::execPreviousAction($action);
185
        }
186
    }
187
188
    /**
189
     * Load view data procedure
190
     *
191
     * @param string   $viewName
192
     * @param BaseView $view
193
     */
194
    protected function loadData($viewName, $view)
195
    {
196
        $codfamilia = $this->getViewModelValue($this->getMainViewName(), 'codfamilia');
197
        switch ($viewName) {
198
            case 'ListProducto':
199
                $where = [new DataBaseWhere('codfamilia', $codfamilia)];
200
                $view->loadData('', $where);
201
                break;
202
203
            case 'ListProducto-new':
204
                $where = [new DataBaseWhere('codfamilia', null, 'IS')];
205
                $view->loadData('', $where);
206
                break;
207
208
            case 'ListFamilia':
209
                $where = [new DataBaseWhere('madre', $codfamilia)];
210
                $view->loadData('', $where);
211
                break;
212
213
            default:
214
                parent::loadData($viewName, $view);
215
                break;
216
        }
217
    }
218
219
    protected function removeProductAction()
220
    {
221
        $codes = $this->request->request->get('code', []);
222
        if (!is_array($codes)) {
223
            return;
224
        }
225
226
        $num = 0;
227
        foreach ($codes as $code) {
228
            $product = new Producto();
229
            if ($product->loadFromCode($code)) {
230
                $product->codfamilia = null;
231
                if ($product->save()) {
232
                    $num++;
233
                }
234
            }
235
        }
236
237
        $this->toolBox()->i18nLog()->notice('items-removed-correctly', ['%num%' => $num]);
238
    }
239
}
240