Passed
Push — master ( f22301...8e49de )
by Joe Nilson
01:58
created

EditFacturaCliente::newRefundAction()   B

Complexity

Conditions 7
Paths 13

Size

Total Lines 45
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 31
c 0
b 0
f 0
nc 13
nop 0
dl 0
loc 45
rs 8.4906
1
<?php
2
3
/**
4
 * Copyright (C) 2020 joenilson.
5
 *
6
 * This library is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 3 of the License, or (at your option) any later version.
10
 *
11
 * This library 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 GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with this library; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
19
 * MA 02110-1301  USA
20
 */
21
22
namespace FacturaScripts\Plugins\fsRepublicaDominicana\Controller;
23
24
use FacturaScripts\Core\Base\DataBase\DataBaseWhere;
0 ignored issues
show
Bug introduced by
The type FacturaScripts\Core\Base\DataBase\DataBaseWhere was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
25
use FacturaScripts\Core\Model\Base\ModelOnChangeClass;
0 ignored issues
show
Bug introduced by
The type FacturaScripts\Core\Model\Base\ModelOnChangeClass was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
26
use FacturaScripts\Dinamic\Lib\AssetManager;
0 ignored issues
show
Bug introduced by
The type FacturaScripts\Dinamic\Lib\AssetManager was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
27
use FacturaScripts\Dinamic\Lib\BusinessDocumentGenerator;
0 ignored issues
show
Bug introduced by
The type FacturaScripts\Dinamic\L...sinessDocumentGenerator was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
28
use FacturaScripts\Dinamic\Model\Cliente;
0 ignored issues
show
Bug introduced by
The type FacturaScripts\Dinamic\Model\Cliente was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
29
use FacturaScripts\Dinamic\Model\FacturaCliente;
0 ignored issues
show
Bug introduced by
The type FacturaScripts\Dinamic\Model\FacturaCliente was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
30
use FacturaScripts\Core\Controller\EditFacturaCliente as ParentClass;
0 ignored issues
show
Bug introduced by
The type FacturaScripts\Core\Controller\EditFacturaCliente was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
31
use FacturaScripts\Plugins\fsRepublicaDominicana\Model\NCFRango;
32
33
/**
34
 * Description of EditFacturaCliente
35
 *
36
 * @author joenilson
37
 */
38
class EditFacturaCliente extends ParentClass
39
{
40
    /**
41
     * Shows the document opertation type selector.
42
     *
43
     * @var bool
44
     */
45
    public $showDocOperation = false;
46
    /**
47
     * Shows the document sub-type selector.
48
     *
49
     * @var bool
50
     */
51
    public $showDocSubType = true;
52
    
53
    protected function createViews()
54
    {
55
        parent::createViews();
56
        $this->customWidgetValues();
57
    }
58
    
59
    public function customWidgetValues()
60
    {
61
        //TODO
62
    }
63
64
    protected function subjectChangedAction()
65
    {
66
        $this->setTemplate(false);
67
68
        //Client data
69
        $cliente0 = new Cliente();
70
71
        /// loads model
72
        $data = $this->getBusinessFormData();
73
        $cliente = $cliente0->get($data['subject']['codcliente']);
74
        $data['form']['codsubtipodoc'] = (isset($data['form']['codsubtipodoc'])) ? $cliente->codsubtipodoc : "02";
75
        $data['form']['codoperaciondoc'] = (isset($data['form']['codoperaciondoc'])) ? "01" : "LIMPIO";
76
        $data['form']['ncftipopago'] = (!isset($data['form']['ncftipopago'])) ? $cliente->ncftipopago : "";
77
78
        $merged = array_merge($data['custom'], $data['final'], $data['form'], $data['subject']);
79
        $this->views[$this->active]->loadFromData($merged);
80
81
        /// update subject data?
82
        if (!$this->views[$this->active]->model->exists()) {
83
            $this->views[$this->active]->model->updateSubject();
84
        }
85
86
        $this->response->setContent(json_encode($this->views[$this->active]->model));
87
        return false;
88
    }
89
90
    /**
91
     *
92
     * @return bool
93
     */
94
    protected function newRefundAction()
95
    {
96
        $invoice = new FacturaCliente();
97
        if (false === $invoice->loadFromCode($this->request->request->get('idfactura'))) {
98
            $this->toolBox()->i18nLog()->warning('record-not-found');
99
            return false;
100
        }
101
102
        $lines = [];
103
        $quantities = [];
104
        foreach ($invoice->getLines() as $line) {
105
            $quantity = (float) $this->request->request->get('refund_' . $line->primaryColumnValue(), '0');
106
            if (empty($quantity)) {
107
                continue;
108
            }
109
110
            $quantities[$line->primaryColumnValue()] = 0 - $quantity;
111
            $lines[] = $line;
112
        }
113
114
        if (empty($quantities)) {
115
            $this->toolBox()->i18nLog()->warning('no-selected-item');
116
            return false;
117
        }
118
119
        $generator = new BusinessDocumentGenerator();
120
        $properties = [
121
            'codigorect' => $invoice->codigo,
122
            'codserie' => $this->request->request->get('codserie'),
123
            'fecha' => $this->request->request->get('fecha'),
124
            'idfacturarect' => $invoice->idfactura,
125
            'observaciones' => $this->request->request->get('observaciones'),
126
            'ncftipoanulacion' => $this->request->request->get('ncf-cancellation-type'),
127
            'codsubtipodoc' => '04'
128
        ];
129
        if ($generator->generate($invoice, $invoice->modelClassName(), $lines, $quantities, $properties)) {
130
            foreach ($generator->getLastDocs() as $doc) {
131
                $this->toolBox()->i18nLog()->notice('record-updated-correctly');
132
                $this->redirect($doc->url() . '&action=save-ok');
133
                return true;
134
            }
135
        }
136
137
        $this->toolBox()->i18nLog()->error('record-save-error');
138
        return false;
139
    }
140
}
141