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

EditFabricante::addProductAction()   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 Fabricante model
28
 *
29
 * @author Carlos García Gómez  <[email protected]>
30
 * @author Artex Trading sa     <[email protected]>
31
 */
32
class EditFabricante extends EditController
33
{
34
35
    /**
36
     * 
37
     * @return string
38
     */
39
    public function getModelClassName()
40
    {
41
        return 'Fabricante';
42
    }
43
44
    /**
45
     * Returns basic page attributes
46
     *
47
     * @return array
48
     */
49
    public function getPageData()
50
    {
51
        $data = parent::getPageData();
52
        $data['menu'] = 'warehouse';
53
        $data['title'] = 'manufacturer';
54
        $data['icon'] = 'fas fa-industry';
55
        return $data;
56
    }
57
58
    protected function addProductAction()
59
    {
60
        $codes = $this->request->request->get('code', []);
61
        if (!is_array($codes)) {
62
            return;
63
        }
64
65
        $num = 0;
66
        foreach ($codes as $code) {
67
            $product = new Producto();
68
            if ($product->loadFromCode($code)) {
69
                $product->codfabricante = $this->request->query->get('code');
70
                if ($product->save()) {
71
                    $num++;
72
                }
73
            }
74
        }
75
76
        $this->toolBox()->i18nLog()->notice('items-added-correctly', ['%num%' => $num]);
77
    }
78
79
    /**
80
     * Load views.
81
     */
82
    protected function createViews()
83
    {
84
        parent::createViews();
85
        $this->setTabsPosition('bottom');
86
87
        $this->createViewProducts();
88
        $this->createViewNewProducts();
89
    }
90
91
    /**
92
     * 
93
     * @param string $viewName
94
     */
95
    protected function createViewNewProducts(string $viewName = 'ListProducto-new')
96
    {
97
        $this->addListView($viewName, 'Producto', 'add', 'fas fa-folder-plus');
98
        $this->createViewProductsCommon($viewName);
99
100
        /// add action button
101
        $newButton = [
102
            'action' => 'add-product',
103
            'color' => 'success',
104
            'icon' => 'fas fa-folder-plus',
105
            'label' => 'add',
106
        ];
107
        $this->addButton($viewName, $newButton);
108
    }
109
110
    /**
111
     * 
112
     * @param string $viewName
113
     */
114
    protected function createViewProducts(string $viewName = 'ListProducto')
115
    {
116
        $this->addListView($viewName, 'Producto', 'products', 'fas fa-cubes');
117
        $this->createViewProductsCommon($viewName);
118
119
        /// add action button
120
        $newButton = [
121
            'action' => 'remove-product',
122
            'color' => 'danger',
123
            'confirm' => true,
124
            'icon' => 'fas fa-folder-minus',
125
            'label' => 'remove-from-list',
126
        ];
127
        $this->addButton($viewName, $newButton);
128
    }
129
130
    /**
131
     * 
132
     * @param string $viewName
133
     */
134
    protected function createViewProductsCommon(string $viewName)
135
    {
136
        $this->views[$viewName]->addOrderBy(['referencia'], 'reference', 1);
137
        $this->views[$viewName]->addOrderBy(['precio'], 'price');
138
        $this->views[$viewName]->addOrderBy(['stock'], 'stock');
139
        $this->views[$viewName]->searchFields = ['referencia', 'descripcion'];
140
141
        /// disable columns and buttons
142
        $this->views[$viewName]->disableColumn('manufacturer');
143
        $this->setSettings($viewName, 'btnNew', false);
144
        $this->setSettings($viewName, 'btnDelete', false);
145
    }
146
147
    /**
148
     * 
149
     * @param string $action
150
     *
151
     * @return bool
152
     */
153
    protected function execPreviousAction($action)
154
    {
155
        switch ($action) {
156
            case 'add-product':
157
                $this->addProductAction();
158
                return true;
159
160
            case 'remove-product':
161
                $this->removeProductAction();
162
                return true;
163
164
            default:
165
                return parent::execPreviousAction($action);
166
        }
167
    }
168
169
    /**
170
     * Load data view procedure
171
     *
172
     * @param string   $viewName
173
     * @param BaseView $view
174
     */
175
    protected function loadData($viewName, $view)
176
    {
177
        switch ($viewName) {
178
            case 'ListProducto':
179
                $codfabricante = $this->getViewModelValue($this->getMainViewName(), 'codfabricante');
180
                $where = [new DataBaseWhere('codfabricante', $codfabricante)];
181
                $view->loadData('', $where);
182
                break;
183
184
            case 'ListProducto-new':
185
                $where = [new DataBaseWhere('codfabricante', null, 'IS')];
186
                $view->loadData('', $where);
187
                break;
188
189
            default:
190
                parent::loadData($viewName, $view);
191
                break;
192
        }
193
    }
194
195
    protected function removeProductAction()
196
    {
197
        $codes = $this->request->request->get('code', []);
198
        if (!is_array($codes)) {
199
            return;
200
        }
201
202
        $num = 0;
203
        foreach ($codes as $code) {
204
            $product = new Producto();
205
            if ($product->loadFromCode($code)) {
206
                $product->codfabricante = null;
207
                if ($product->save()) {
208
                    $num++;
209
                }
210
            }
211
        }
212
213
        $this->toolBox()->i18nLog()->notice('items-removed-correctly', ['%num%' => $num]);
214
    }
215
}
216