Completed
Push — master ( d95b40...8e7417 )
by Carlos
05:47
created

EditAgente::editAction()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 15
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 9
c 1
b 0
f 0
dl 0
loc 15
rs 9.9666
cc 4
nc 3
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\ComercialContactController;
24
use FacturaScripts\Dinamic\Model\Agente;
25
use FacturaScripts\Dinamic\Model\TotalModel;
26
27
/**
28
 * Controller to edit a single item from the Agente model
29
 *
30
 * @author Carlos Garcia Gomez <[email protected]>
31
 * @author Artex Trading sa    <[email protected]>
32
 * @author Raul
33
 */
34
class EditAgente extends ComercialContactController
35
{
36
37
    /**
38
     * Returns the sum of the agent's total outstanding invoices.
39
     *
40
     * @return string
41
     */
42
    public function calcAgentInvoicePending()
43
    {
44
        $where = [
45
            new DataBaseWhere('codagente', $this->getViewModelValue($this->getMainViewName(), 'codagente')),
46
            new DataBaseWhere('pagada', false)
47
        ];
48
49
        $totalModel = TotalModel::all('facturascli', $where, ['total' => 'SUM(total)'], '')[0];
50
        return $this->toolBox()->coins()->format($totalModel->totals['total'], 2);
51
    }
52
53
    /**
54
     * Returns the class name of the model to use.
55
     *
56
     * @return string
57
     */
58
    public function getModelClassName()
59
    {
60
        return 'Agente';
61
    }
62
63
    /**
64
     * Returns basic page attributes.
65
     *
66
     * @return array
67
     */
68
    public function getPageData()
69
    {
70
        $data = parent::getPageData();
71
        $data['menu'] = 'sales';
72
        $data['title'] = 'agent';
73
        $data['icon'] = 'fas fa-user-tie';
74
        return $data;
75
    }
76
77
    /**
78
     * 
79
     * @param string $viewName
80
     */
81
    protected function createCommissionsView($viewName = 'ListComision')
82
    {
83
        $this->addListView($viewName, 'Comision', 'commissions', 'fas fa-percentage');
84
        $this->views[$viewName]->addOrderBy(['prioridad'], 'priority', 2);
85
86
        /// disable columns
87
        $this->views[$viewName]->disableColumn('agent', true);
88
    }
89
90
    /**
91
     * 
92
     * @param string $viewName
93
     */
94
    protected function createContactView($viewName = 'EditContacto')
95
    {
96
        $this->addEditView($viewName, 'Contacto', 'contact', 'fa fa-address-book');
97
98
        /// disable columns
99
        $this->views[$viewName]->disableColumn('agent', true);
100
        $this->views[$viewName]->disableColumn('company', true);
101
        $this->views[$viewName]->disableColumn('fiscal-id', true);
102
        $this->views[$viewName]->disableColumn('fiscal-number', true);
103
        $this->views[$viewName]->disableColumn('position', true);
104
    }
105
106
    /**
107
     * 
108
     * @param string $viewName
109
     */
110
    protected function createSettlementView($viewName = 'ListLiquidacionComision')
111
    {
112
        $this->addListView($viewName, 'LiquidacionComision', 'settlements', 'fas fa-chalkboard-teacher');
113
    }
114
115
    /**
116
     * Load Views
117
     */
118
    protected function createViews()
119
    {
120
        parent::createViews();
121
        $this->createContactView();
122
        $this->createCommissionsView();
123
        $this->createSettlementView();
124
        $this->createCustomerListView('ListFacturaCliente', 'FacturaCliente', 'invoices');
125
        $this->createCustomerListView('ListAlbaranCliente', 'AlbaranCliente', 'delivery-notes');
126
        $this->createCustomerListView('ListPedidoCliente', 'PedidoCliente', 'orders');
127
        $this->createCustomerListView('ListPresupuestoCliente', 'PresupuestoCliente', 'estimations');
128
    }
129
130
    /**
131
     * 
132
     * @return bool
133
     */
134
    protected function editAction()
135
    {
136
        $return = parent::editAction();
137
        if ($return && $this->active == 'EditContacto') {
138
            /// update agent data when contact data is updated
139
            $agente = new Agente();
140
            if ($agente->loadFromCode($this->views[$this->active]->model->codagente)) {
141
                $agente->email = $this->views[$this->active]->model->email;
142
                $agente->telefono1 = $this->views[$this->active]->model->telefono1;
143
                $agente->telefono2 = $this->views[$this->active]->model->telefono2;
144
                $agente->save();
145
            }
146
        }
147
148
        return $return;
149
    }
150
151
    /**
152
     * Load view data procedure
153
     *
154
     * @param string   $viewName
155
     * @param BaseView $view
156
     */
157
    protected function loadData($viewName, $view)
158
    {
159
        switch ($viewName) {
160
            case 'EditContacto':
161
            case 'ListAlbaranCliente':
162
            case 'ListComision':
163
            case 'ListFacturaCliente':
164
            case 'ListLiquidacionComision':
165
            case 'ListPedidoCliente':
166
            case 'ListPresupuestoCliente':
167
                $codagente = $this->getViewModelValue('EditAgente', 'codagente');
168
                $where = [new DataBaseWhere('codagente', $codagente)];
169
                $view->loadData('', $where);
170
                break;
171
172
            default:
173
                parent::loadData($viewName, $view);
174
                if (!$view->model->exists()) {
175
                    $view->disableColumn('contact');
176
                }
177
                break;
178
        }
179
    }
180
181
    /**
182
     *
183
     * @param string $viewName
184
     */
185
    protected function setCustomWidgetValues($viewName)
186
    {
187
        ;
188
    }
189
}
190