FiscalReport606::getSQLFrom()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 11
nc 1
nop 0
dl 0
loc 13
rs 9.9
c 1
b 0
f 0
1
<?php
2
/*
3
 * Copyright (C) 2022 Joe Nilson <[email protected]>
4
 *
5
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU Lesser General Public License as
7
 * published by the Free Software Foundation, either version 3 of the
8
 * License, or (at your option) any later version.
9
 *
10
 * This program 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
13
 * GNU Lesser General Public License for more details.
14
 * You should have received a copy of the GNU Lesser General Public License
15
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16
 */
17
18
namespace FacturaScripts\Plugins\fsRepublicaDominicana\Model\Join;
19
20
use FacturaScripts\Core\Model\Base\JoinModel;
0 ignored issues
show
Bug introduced by
The type FacturaScripts\Core\Model\Base\JoinModel 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...
21
use FacturaScripts\Core\Tools;
22
23
24
class FiscalReport606 extends JoinModel
25
{
26
    const MAIN_TABLE = 'facturasprov';
27
    const LINES_TABLE = 'lineasfacturasprov';
28
    const SECONDARY_TABLE = 'facturasprov AS f2';
29
30
    const RECIBOSPROV_TABLE = 'recibospagosprov';
31
    const SECONDARY_TABLE_ALIAS = 'f2';
32
    const ESTADOSDOC_TABLE = 'estados_documentos';
33
    const PRODS_TABLE = 'productos';
34
35
    /**
36
     *
37
     * @return array
38
     */
39
    protected function getFields(): array
40
    {
41
        $dateFormat = (FS_DB_TYPE === 'postgresql') ? "to_char" : "date_format";
0 ignored issues
show
Bug introduced by
The constant FacturaScripts\Plugins\f...a\Model\Join\FS_DB_TYPE was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
42
        $dateFormatString = (FS_DB_TYPE === 'postgresql') ? "YYYYMMDD" : "%Y%m%d";
43
44
        $data = [
45
            'itemrow' => static::MAIN_TABLE.'.idfactura',
46
            'idempresa' => static::MAIN_TABLE.'.idempresa',
47
            'codalmacen' => static::MAIN_TABLE.'.codalmacen',
48
            'cifnif' => 'CASE WHEN length('.static::MAIN_TABLE.'.cifnif)=9 THEN '.static::MAIN_TABLE.'.cifnif WHEN length('.static::MAIN_TABLE.'.cifnif)=11 THEN '.static::MAIN_TABLE.'.cifnif ELSE NULL END',
49
            'tipoid' => 'CASE WHEN length('.static::MAIN_TABLE.'.cifnif)=9 THEN 1 WHEN length('.static::MAIN_TABLE.'.cifnif)=11 THEN 2 ELSE 3 END',
50
            'tipocompra' => static::MAIN_TABLE.'.ncftipomovimiento',
51
            'ncf' => static::MAIN_TABLE.'.numeroncf',
52
            'ncfmodifica' => static::SECONDARY_TABLE_ALIAS.'.numeroncf',
53
            'fecha' => $dateFormat.'('.static::MAIN_TABLE.'.fecha,\''.$dateFormatString.'\')',
54
            'fechapago' => $dateFormat.'('.static::RECIBOSPROV_TABLE.'.fechapago,\''.$dateFormatString.'\')',
55
            'totalservicios' => 'SUM(CASE WHEN '.static::PRODS_TABLE.'.esservicio = true THEN '.static::LINES_TABLE.'.pvptotal ELSE 0 END)',
56
            'totalbienes' => 'SUM(CASE WHEN ('.static::PRODS_TABLE.'.esservicio = false OR '.static::PRODS_TABLE.'.esservicio IS NULL) THEN '.static::LINES_TABLE.'.pvptotal ELSE 0 END)',
57
            'base' => 'CASE WHEN '.static::ESTADOSDOC_TABLE.'.nombre = \'Anulada\' THEN 0 WHEN '.static::ESTADOSDOC_TABLE.'.nombre = \'Emitida\' AND '.static::MAIN_TABLE.'.neto < 0 THEN '.static::MAIN_TABLE.'.neto*-1 ELSE '.static::MAIN_TABLE.'.neto END',
58
            'itbis' => 'CASE WHEN '.static::ESTADOSDOC_TABLE.'.nombre = \'Anulada\' THEN 0 WHEN '.static::ESTADOSDOC_TABLE.'.nombre = \'Emitida\' AND '.static::MAIN_TABLE.'.totaliva < 0 THEN '.static::MAIN_TABLE.'.totaliva*-1 ELSE '.static::MAIN_TABLE.'.totaliva END',
59
//            'itbisretenido' => '0',
60
//            'itbissujeto' => '0',
61
            'itbisalcosto' => 'CASE WHEN SUM(CASE WHEN '.static::PRODS_TABLE.'.esservicio = true THEN '.static::LINES_TABLE.'.pvptotal ELSE 0 END) = 0 THEN 0 ELSE CASE WHEN '.static::ESTADOSDOC_TABLE.'.nombre = \'Anulada\' THEN 0 WHEN '.static::ESTADOSDOC_TABLE.'.nombre = \'Emitida\' AND '.static::MAIN_TABLE.'.totaliva < 0 THEN '.static::MAIN_TABLE.'.totaliva*-1 ELSE '.static::MAIN_TABLE.'.totaliva END END',
62
//            'itbisporadelantar' => '0',
63
//            'itbispercibido' => '0',
64
            //'tiporetencionrenta' => '\'\'',
65
//            'rentaretenido' => '0',
66
//            'rentapercibido' => '0',
67
//            'isc' => '0',
68
//            'otrosimpuestos' => '0',
69
//            'propinalegal' => '0',
70
            'tipopago' => 'CASE WHEN '.static::MAIN_TABLE.'.ncftipomovimiento is null THEN \'02\' ELSE '.static::MAIN_TABLE.'.ncftipomovimiento END',
71
            'estado' => 'CASE WHEN '.static::ESTADOSDOC_TABLE.'.nombre = \'Anulada\' THEN \'Anulado\' ELSE \'Activo\' END',
72
        ];
73
        return $data;
74
    }
75
76
    protected function getGroupFields(): string
77
    {
78
        //return parent::getGroupFields(); // TODO: Change the autogenerated stub
79
        return  static::MAIN_TABLE.'.idfactura, ' .
80
                static::MAIN_TABLE.'.idempresa, ' .
81
                static::MAIN_TABLE.'.codalmacen, ' .
82
                static::MAIN_TABLE.'.cifnif, ' .
83
                static::MAIN_TABLE.'.ncftipomovimiento, ' .
84
                static::MAIN_TABLE.'.numeroncf, ' .
85
                static::SECONDARY_TABLE_ALIAS.'.numeroncf, ' .
86
                static::MAIN_TABLE.'.fecha, ' .
87
                static::RECIBOSPROV_TABLE.'.fechapago, ' .
88
                static::ESTADOSDOC_TABLE.'.nombre, ' .
89
                static::MAIN_TABLE.'.neto, ' .
90
                static::MAIN_TABLE.'.totaliva' .
91
            ' ';
92
    }
93
94
    /**
95
     *
96
     * @return string
97
     */
98
    protected function getSQLFrom(): string
99
    {
100
        return static::MAIN_TABLE
101
            . ' LEFT JOIN '. static::SECONDARY_TABLE . ' ON ('
102
            . static::MAIN_TABLE . '.idfacturarect = ' . static::SECONDARY_TABLE_ALIAS . '.idfactura)'
103
            . ' LEFT JOIN '. static::RECIBOSPROV_TABLE . ' ON ('
104
            . static::MAIN_TABLE . '.idfactura = ' . static::RECIBOSPROV_TABLE . '.idfactura)'
105
            . ' LEFT JOIN '. static::LINES_TABLE . ' ON ('
106
            . static::MAIN_TABLE . '.idfactura = ' . static::LINES_TABLE . '.idfactura)'
107
            . ' LEFT JOIN '. static::PRODS_TABLE . ' ON ('
108
            . static::LINES_TABLE . '.referencia = ' . static::PRODS_TABLE . '.referencia)'
109
            . ' LEFT JOIN ' . static::ESTADOSDOC_TABLE . ' ON ('
110
            . static::MAIN_TABLE . '.idestado = ' . static::ESTADOSDOC_TABLE . '.idestado)';
111
    }
112
113
    /**
114
     *
115
     * @return array
116
     */
117
    protected function getTables(): array
118
    {
119
        return [
120
            static::MAIN_TABLE,
121
            static::RECIBOSPROV_TABLE,
122
            static::LINES_TABLE,
123
            static::ESTADOSDOC_TABLE,
124
            static::PRODS_TABLE
125
        ];
126
    }
127
}
128