Passed
Push — master ( 3a17ef...a7ff16 )
by Joe Nilson
03:12
created

FiscalReports::loadData()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 28
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 23
nc 10
nop 2
dl 0
loc 28
rs 8.9297
c 0
b 0
f 0
1
<?php
2
/*
3
 * Copyright (C) 2019 joenilson.
4
 *
5
 * This library is free software; you can redistribute it and/or
6
 * modify it under the terms of the GNU Lesser General Public
7
 * License as published by the Free Software Foundation; either
8
 * version 3 of the License, or (at your option) any later version.
9
 *
10
 * This library is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13
 * Lesser General Public License for more details.
14
 *
15
 * You should have received a copy of the GNU Lesser General Public
16
 * License along with this library; if not, write to the Free Software
17
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18
 * MA 02110-1301  USA
19
 */
20
21
namespace FacturaScripts\Plugins\fsRepublicaDominicana\Controller;
22
23
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...
24
use FacturaScripts\Core\DataSrc\Almacenes;
0 ignored issues
show
Bug introduced by
The type FacturaScripts\Core\DataSrc\Almacenes 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\Lib\ExtendedController\BaseView;
0 ignored issues
show
Bug introduced by
The type FacturaScripts\Core\Lib\...ndedController\BaseView 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\Core\Lib\ExtendedController\ListController;
0 ignored issues
show
Bug introduced by
The type FacturaScripts\Core\Lib\...ntroller\ListController 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\Core\Lib\ListFilter\PeriodTools;
0 ignored issues
show
Bug introduced by
The type FacturaScripts\Core\Lib\ListFilter\PeriodTools 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\LineaFacturaCliente;
0 ignored issues
show
Bug introduced by
The type FacturaScripts\Dinamic\Model\LineaFacturaCliente 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\Plugins\fsRepublicaDominicana\Lib\CommonFunctionsDominicanRepublic;
30
31
/**
32
 * Description of FiscalReports
33
 *
34
 * @author joenilson
35
 */
36
class FiscalReports extends ListController
37
{
38
    /**
39
     * @return array
40
     */
41
    public function getPageData(): array
42
    {
43
        $data = parent::getPageData();
44
        $data['menu'] = 'reports';
45
        $data['submenu'] = 'dominican-republic';
46
        $data['icon'] = 'fas fa-hand-holding-usd';
47
        $data['title'] = 'rd-fiscal-reports';
48
        return $data;
49
    }
50
51
    protected function createViews(): void
52
    {
53
        // needed dependencies
54
        new LineaFacturaCliente();
55
        $this->createViewsFiscalReportsConsolidated();
56
        $this->createViewsFiscalReports606();
57
        $this->createViewsFiscalReports607();
58
        $this->createViewsFiscalReports608();
59
        $this->exportManager->addOption('dgii', 'txt-export', 'fas fa-file-alt');
60
    }
61
62
    public function execAfterAction($action)
63
    {
64
        parent::execAfterAction($action);
65
        $periodStartDate = \date('01-m-Y');
66
        $periodEndDate = \date('d-m-Y');
67
        if ($this->request->request->get('filterfecha') !== null) {
68
            PeriodTools::applyPeriod($this->request->request->get('filterfecha'), $periodStartDate, $periodEndDate);
69
        }
70
        $startDate = ($this->request->request->get('filterstartfecha') !== null)
71
            ? $this->request->request->get('filterstartfecha')
72
            : $periodStartDate;
73
        $endDate = ($this->request->request->get('filterendfecha') !== null)
74
            ? $this->request->request->get('filterendfecha')
75
            : $periodEndDate;
76
        $year = substr($startDate, 6, 4);
77
        $month = substr($startDate, 3, 2);
78
        $commonFunctions = new CommonFunctionsDominicanRepublic();
79
        $option = $this->request->get('option');
80
        $actualTab = $this->request->get('activetab');
81
        switch ($actualTab) {
82
            case "FiscalReport606":
83
                $whereReport = [
84
                    new DataBaseWhere('facturasprov.fecha', $startDate, '>='),
85
                    new DataBaseWhere('facturasprov.fecha', $endDate, '<='),
86
                ];
87
                if ($action === 'export' and $option === 'dgii') {
88
                    $this->setTemplate(false);
89
                    $fileName = 'DGII_606_'.$this->empresa->cifnif.'_'.$year.'_'.$month.'.txt';
90
                    $commonFunctions->exportTXT('606', $fileName, $this->empresa->cifnif, $year, $month, $whereReport);
91
                    $this->response->headers->set('Content-type', 'text/plain');
92
                    $this->response->headers->set('Content-Disposition', 'attachment;filename=' . $fileName);
93
                    $this->response->setContent(file_get_contents(\FS_FOLDER . DIRECTORY_SEPARATOR . $fileName));
0 ignored issues
show
Bug introduced by
The constant FS_FOLDER was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
94
                }
95
                break;
96
            case "FiscalReport607":
97
                $whereReport = [
98
                    new DataBaseWhere('facturascli.fecha', $startDate, '>='),
99
                    new DataBaseWhere('facturascli.fecha', $endDate, '<='),
100
                ];
101
                if ($action === 'export' and $option === 'dgii') {
102
                    $fileName = 'DGII_607_'.$this->empresa->cifnif.'_'.$year.'_'.$month.'.txt';
103
                    $commonFunctions->exportTXT('607', $fileName, $this->empresa->cifnif, $year, $month, $whereReport);
104
                    $this->response->headers->set('Content-type', 'text/plain');
105
                    $this->response->headers->set('Content-Disposition', 'attachment;filename=' . $fileName);
106
                    $this->response->setContent(file_get_contents(\FS_FOLDER . DIRECTORY_SEPARATOR . $fileName));
107
                    $this->setTemplate(false);
108
                }
109
                break;
110
            case "FiscalReport608":
111
                $whereReport = [
112
                    new DataBaseWhere('facturascli.fecha', $startDate, '>='),
113
                    new DataBaseWhere('facturascli.fecha', $endDate, '<='),
114
                ];
115
                if ($action === 'export' and $option === 'dgii') {
116
                    $this->setTemplate(false);
117
                    $fileName = 'DGII_608_'.$this->empresa->cifnif.'_'.$year.'_'.$month.'.txt';
118
                    $commonFunctions->exportTXT('608', $fileName, $this->empresa->cifnif, $year, $month, $whereReport);
119
                    $this->response->headers->set('Content-type', 'text/plain');
120
                    $this->response->headers->set('Content-Disposition', 'attachment;filename=' . $fileName);
121
                    $this->response->setContent(file_get_contents(\FS_FOLDER . DIRECTORY_SEPARATOR . $fileName));
122
                }
123
                break;
124
            default:
125
                $whereReport = [
0 ignored issues
show
Unused Code introduced by
The assignment to $whereReport is dead and can be removed.
Loading history...
126
                    new DataBaseWhere('facturascli.fecha', $startDate, '>='),
127
                    new DataBaseWhere('facturascli.fecha', $endDate, '<='),
128
                ];
129
                break;
130
        }
131
        //$this->views[$actualTab]->loadData('', $whereReport);
132
//        }
133
    }
134
135
    /**
136
     * @param string $viewName
137
     */
138
    protected function createViewsFiscalReportsConsolidated(string $viewName = 'FiscalReports-consolidated')
139
    {
140
        $this->addView(
141
            $viewName,
142
            'Join\FiscalReports',
143
            'rd-fiscal-reports-consolidated',
144
            'fas fa-shipping-fast'
145
        );
146
        $this->addOrderBy($viewName, ['ncf'], 'ncf');
147
        $this->addFilterPeriod($viewName, 'fecha', 'date', 'facturascli.fecha');
148
        $this->addCommonSearchFields($viewName);
149
        $this->disableButtons($viewName);
150
    }
151
152
    /**
153
     * @param string $viewName
154
     */
155
    protected function createViewsFiscalReports608(string $viewName = 'FiscalReport608')
156
    {
157
        $this->addView($viewName,
158
            'Join\FiscalReport608',
159
            'rd-fiscal-reports-608',
160
            'fas fa-shopping-cart');
161
        $this->addFilterPeriod($viewName, 'fecha', 'date', 'facturascli.fecha');
162
        $this->addCommonSearchFields($viewName);
163
        $this->disableButtons($viewName);
164
    }
165
166
    /**
167
     * @param string $viewName
168
     */
169
    protected function createViewsFiscalReports607(string $viewName = 'FiscalReport607')
170
    {
171
        $this->addView($viewName, 'Join\FiscalReport607', 'rd-fiscal-reports-607', 'fas fa-copy');
172
        $this->addFilterPeriod($viewName, 'fecha', 'date', 'facturascli.fecha');
173
        $this->addCommonSearchFields($viewName);
174
        $this->disableButtons($viewName);
175
    }
176
177
    /**
178
     * @param string $viewName
179
     */
180
    protected function createViewsFiscalReports606(string $viewName = 'FiscalReport606')
181
    {
182
        $this->addView($viewName, 'Join\FiscalReport606', 'rd-fiscal-reports-606', 'fas fa-copy');
183
        $this->addFilterPeriod($viewName, 'fecha', 'date', 'facturasprov.fecha');
184
        $this->addSearchFields($viewName, ['numero2', 'cifnif', 'fecha', 'estado'], 'fecha');
185
        $this->addOrderBy($viewName, ['facturasprov.fecha'], 'fecha');
186
        $this->addOrderBy($viewName, ['facturasprov.numproveedor'], 'ncf');
187
        $this->addOrderBy($viewName, ['cifnif'], 'cifnif');
188
        $this->disableButtons($viewName);
189
    }
190
191
    /**
192
     * @param string $viewName
193
     */
194
    private function addCommonSearchFields(string $viewName)
195
    {
196
        $this->addSearchFields($viewName, ['numero2', 'cifnif', 'fecha', 'estado'], 'fecha');
197
        $this->addOrderBy($viewName, ['facturascli.fecha'], 'fecha');
198
        $this->addOrderBy($viewName, ['facturascli.numero2'], 'ncf');
199
        $this->addOrderBy($viewName, ['cifnif'], 'cifnif');
200
    }
201
202
    /**
203
     * @param string $viewName
204
     */
205
    private function disableButtons(string $viewName)
206
    {
207
        $this->setSettings($viewName, 'btnDelete', false);
208
        $this->setSettings($viewName, 'btnNew', false);
209
        $this->setSettings($viewName, 'checkBoxes', false);
210
        $this->setSettings($viewName, 'clickable', false);
211
    }
212
213
    /**
214
     *
215
     * @param string   $viewName
216
     * @param BaseView $view
217
     */
218
    protected function loadData($viewName, $view)
219
    {
220
        $periodStartDate = \date('Y-m-01');
221
        $periodEndDate = \date('Y-m-d');
222
        PeriodTools::applyPeriod('last-month', $periodStartDate, $periodEndDate);
223
        if ($this->request->request->get('filterfecha') !== null) {
224
            PeriodTools::applyPeriod($this->request->request->get('filterfecha'), $periodStartDate, $periodEndDate);
225
        }
226
        switch ($viewName) {
227
            case 'FiscalReport606':
228
                $where = [
229
                    new DataBaseWhere('facturasprov.fecha', $periodStartDate, '>='),
230
                    new DataBaseWhere('facturasprov.fecha', $periodEndDate, '<='),
231
                ];
232
                $view->loadData('', $where);
233
                break;
234
            case 'FiscalReport607':
235
            case 'FiscalReport608':
236
            case 'FiscalReports-consolidated':
237
                $where = [
238
                    new DataBaseWhere('facturascli.fecha', $periodStartDate, '>='),
239
                    new DataBaseWhere('facturascli.fecha', $periodEndDate, '<='),
240
                ];
241
                $view->loadData('', $where);
242
                break;
243
            default:
244
                parent::loadData($viewName, $view);
245
                break;
246
        }
247
    }
248
}
249