Passed
Push — master ( b8d623...b21d4a )
by Joe Nilson
01:59
created

CommonFunctionsDominicanRepublic::exportTXT606()   B

Complexity

Conditions 6
Paths 17

Size

Total Lines 49
Code Lines 37

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 6
eloc 37
nc 17
nop 5
dl 0
loc 49
rs 8.7057
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\Lib;
19
20
use FacturaScripts\Dinamic\Model\NCFTipo;
0 ignored issues
show
Bug introduced by
The type FacturaScripts\Dinamic\Model\NCFTipo 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\Dinamic\Model\Join\FiscalReport606;
0 ignored issues
show
Bug introduced by
The type FacturaScripts\Dinamic\Model\Join\FiscalReport606 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...
22
use FacturaScripts\Dinamic\Model\Join\FiscalReport607;
0 ignored issues
show
Bug introduced by
The type FacturaScripts\Dinamic\Model\Join\FiscalReport607 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...
23
use FacturaScripts\Dinamic\Model\Join\FiscalReport608;
0 ignored issues
show
Bug introduced by
The type FacturaScripts\Dinamic\Model\Join\FiscalReport608 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\Plugins\fsRepublicaDominicana\Interfaces\CommonFunctionsInterface;
25
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...
26
use FacturaScripts\Dinamic\Model\NCFRango;
0 ignored issues
show
Bug introduced by
The type FacturaScripts\Dinamic\Model\NCFRango 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\Plugins\fsRepublicaDominicana\Model\NCFTipoAnulacion;
28
use FacturaScripts\Plugins\fsRepublicaDominicana\Model\NCFTipoMovimiento;
29
30
class CommonFunctionsDominicanRepublic implements CommonFunctionsInterface
31
{
32
    public static function ncfRango()
33
    {
34
        // TODO: Implement ncfRango() method.
35
    }
36
37
    public static function ncfCorrelativo($tipoComprobante, $idempresa)
38
    {
39
        $tipocomprobante = new NCFRango();
40
        $where = [
41
            new DatabaseWhere('tipocomprobante', $tipoComprobante),
42
            new DatabaseWhere('idempresa', $idempresa),
43
            new DatabaseWhere('estado', 1)
44
        ];
45
        $comprobante = $tipocomprobante->all($where);
46
        if ($comprobante) {
47
            echo json_encode(['existe' => $comprobante], JSON_THROW_ON_ERROR);
48
        } else {
49
            echo json_encode(['existe' => false], JSON_THROW_ON_ERROR);
50
        }
51
    }
52
53
    /**
54
     * @throws \JsonException
55
     */
56
    public static function ncfTipoPago(string $tipoPago)
57
    {
58
        $where = [new DatabaseWhere('tipopago', $tipoPago)];
59
        $tipoPagos = new NCFTipoPago();
0 ignored issues
show
Bug introduced by
The type FacturaScripts\Plugins\f...inicana\Lib\NCFTipoPago 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...
60
        $pagos = $tipoPagos->all($where);
61
        if ($pagos) {
62
            echo \json_encode(['pagos' => $pagos], JSON_THROW_ON_ERROR);
63
        } else {
64
            echo '';
65
        }
66
    }
67
68
    /**
69
     * @throws \JsonException
70
     */
71
    public static function ncfTipoMovimiento(string $tipoMovimiento)
72
    {
73
        $tipomovimiento = new NCFTipoMovimiento();
74
        $where = [new DatabaseWhere('tipomovimiento', $tipoMovimiento)];
75
        $movimientos = $tipomovimiento->all($where);
76
        if ($movimientos) {
77
            echo json_encode(['movimientos' => $movimientos], JSON_THROW_ON_ERROR);
78
        } else {
79
            echo '';
80
        }
81
    }
82
83
    public static function ncfTipoAnulacion(string $tipoAnulacion)
84
    {
85
        $where = [new DatabaseWhere('codigo', $tipoAnulacion)];
86
        $tipoAnulaciones = new NCFTipoAnulacion();
87
        $anulaciones = $tipoAnulaciones->all($where);
88
        if ($anulaciones) {
89
            echo \json_encode(['anulaciones' => $anulaciones], JSON_THROW_ON_ERROR);
90
        } else {
91
            echo '';
92
        }
93
    }
94
95
    /**
96
     * @throws \JsonException
97
     */
98
    public static function ncfTipoComprobante($tipoComprobante)
99
    {
100
        $where = [new DatabaseWhere($tipoComprobante, 'Y')];
101
        $tipoComprobantes = new NCFTipo();
102
        $lista = $tipoComprobantes->all($where);
103
        if ($lista) {
104
            echo json_encode(['tipocomprobantes' => $lista], JSON_THROW_ON_ERROR);
105
        } else {
106
            echo '';
107
        }
108
    }
109
110
    public static function ncfFechaVencimiento()
111
    {
112
        // TODO: Implement ncfFechaVencimiento() method.
113
    }
114
115
    /**
116
     * @throws \JsonException
117
     */
118
    public static function ncfTipoCliente(string $cliente)
119
    {
120
        $NCFTipo = new NCFTipo();
121
        $tipoCliente = $NCFTipo->tipoCliente($cliente);
122
        if ($tipoCliente) {
123
            echo json_encode(['infocliente' => $tipoCliente], JSON_THROW_ON_ERROR);
124
        } else {
125
            echo '';
126
        }
127
    }
128
129
    public function exportTXT(
130
        string $report,
131
        string $fileName,
132
        string $rncCompany,
133
        string $yearReport,
134
        string $monthReport,
135
        array $whereReport
136
    ) {
137
        if (file_exists($fileName)) {
138
            unlink($fileName);
139
        }
140
        $dataCounter = 0;
0 ignored issues
show
Unused Code introduced by
The assignment to $dataCounter is dead and can be removed.
Loading history...
141
        $fp = fopen($fileName, "w");
142
        switch ($report) {
143
            case "606":
144
                $this->exportTXT606(
145
                    $fp,
146
                    $rncCompany,
147
                    $yearReport,
148
                    $monthReport,
149
                    $whereReport
150
                );
151
                break;
152
            case "607":
153
            default:
154
                $this->exportTXT607(
155
                    $fp,
156
                    $rncCompany,
157
                    $yearReport,
158
                    $monthReport,
159
                    $whereReport
160
                );
161
                break;
162
            case "608":
163
                $this->exportTXT608(
164
                    $fp,
165
                    $rncCompany,
166
                    $yearReport,
167
                    $monthReport,
168
                    $whereReport
169
                );
170
                break;
171
        }
172
        fclose($fp);
173
        return true;
174
    }
175
176
    /**
177
     * @param mixed $fp
178
     * @param string $rncCompany
179
     * @param string $yearReport
180
     * @param string $monthReport
181
     * @param array $whereReport
182
     * @return void
183
     */
184
    protected function exportTXT606(
185
        &$fp,
186
        string $rncCompany,
187
        string $yearReport,
188
        string $monthReport,
189
        array $whereReport
190
    ): void
191
    {
192
        $reportData = new FiscalReport606();
193
        $data = $reportData->all($whereReport);
194
        $dataCounter = count($data);
195
        fwrite(
196
            $fp,
197
            sprintf(
198
                "%s|%s|%4s%2s|%s\r\n",
199
                '606',
200
                $rncCompany,
201
                $yearReport,
202
                $monthReport,
203
                $dataCounter
204
            )
205
        );
206
207
        foreach ($data as $line) {
208
            $convertValue = (substr($line->ncf, 1, 2) === '04') ? -1 : 1;
209
            $ncfModifica = ($line->ncfmodifica) ? substr($line->ncfmodifica, -11, 11) : "";
210
            $fechaPago = ($line->fechapago === '') ? $line->fechapago : $line->fecha;
211
            $itbisCosto = ($line->totalservicios > 0)
212
                ? number_format($line->itbis, 2, ".", "")
213
                : 0;
214
            fwrite(
215
                $fp,
216
                sprintf(
217
                    "%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s\r\n",
218
                    $line->cifnif,
219
                    $line->tipoid,
220
                    $line->tipocompra,
221
                    substr($line->ncf, -11, 11),
222
                    $ncfModifica,
223
                    $line->fecha,
224
                    $fechaPago,
225
                    number_format($line->totalservicios * $convertValue, 2, ".", ""),
226
                    number_format($line->totalbienes * $convertValue, 2, ".", ""),
227
                    number_format($line->base, 2, ".", ""),
228
                    number_format($line->itbis, 2, ".", ""),
229
                    "", "", "",
230
                    number_format($itbisCosto, 2, ".", ""),
0 ignored issues
show
Bug introduced by
It seems like $itbisCosto can also be of type string; however, parameter $num of number_format() does only seem to accept double, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

230
                    number_format(/** @scrutinizer ignore-type */ $itbisCosto, 2, ".", ""),
Loading history...
231
                    "", "", "", "", "", "", "", "",
232
                    $line->tipopago
233
                ));
234
        }
235
    }
236
237
    /**
238
     * @param mixed $fp
239
     * @param string $rncCompany
240
     * @param string $yearReport
241
     * @param string $monthReport
242
     * @param array $whereReport
243
     * @return void
244
     */
245
    protected function exportTXT607(
246
        &$fp,
247
        string $rncCompany,
248
        string $yearReport,
249
        string $monthReport,
250
        array $whereReport
251
    ): void
252
    {
253
        $reportData = new FiscalReport607();
254
        $data = $reportData->all($whereReport);
255
        $dataCounter = count($data);
256
        fwrite(
257
            $fp,
258
            sprintf(
259
                "%s|%s|%4s%2s|%s\r\n",
260
                '607',
261
                $rncCompany,
262
                $yearReport,
263
                $monthReport,
264
                $dataCounter
265
            )
266
        );
267
        foreach ($data as $line) {
268
            $convertValue = (substr($line->ncf, 1, 2) === '04') ? -1 : 1;
269
            fwrite(
270
                $fp,
271
                sprintf(
272
                    "%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s|%s\r\n",
273
                    $line->cifnif,
274
                    $line->tipoid,
275
                    substr($line->ncf, -11, 11),
276
                    substr($line->ncfmodifica, -11, 11),
277
                    1,
278
                    $line->fecha,
279
                    "",
280
                    number_format($line->base, 2, ".", ""),
281
                    number_format($line->itbis, 2, ".", ""),
282
                    "", "", "", "", "", "", "",
283
                    number_format($line->totalefectivo * $convertValue, 2, ".", ""),
284
                    number_format($line->totalcheque * $convertValue, 2, ".", ""),
285
                    number_format($line->totaltarjeta * $convertValue, 2, ".", ""),
286
                    number_format($line->totalcredito * $convertValue, 2, ".", ""),
287
                    number_format($line->totalbonos * $convertValue, 2, ".", ""),
288
                    number_format($line->totalpermuta * $convertValue, 2, ".", ""),
289
                    number_format($line->totalotrasformas * $convertValue, 2, ".", "")
290
                ));
291
        }
292
    }
293
294
    /**
295
     * @param mixed $fp
296
     * @param string $rncCompany
297
     * @param string $yearReport
298
     * @param string $monthReport
299
     * @param array $whereReport
300
     * @return void
301
     */
302
    protected function exportTXT608(
303
        &$fp,
304
        string $rncCompany,
305
        string $yearReport,
306
        string $monthReport,
307
        array $whereReport
308
    ): void {
309
        $reportData = new FiscalReport608();
310
        $data = $reportData->all($whereReport);
311
        $dataCounter = count($data);
312
        fwrite(
313
            $fp,
314
            sprintf(
315
                "%s|%s|%4s%2s|%s\r\n",
316
                '608',
317
                $rncCompany,
318
                $yearReport,
319
                $monthReport,
320
                $dataCounter
321
            )
322
        );
323
        foreach ($data as $line) {
324
            fwrite(
325
                $fp,
326
                sprintf(
327
                    "%s|%s|%s\r\n",
328
                    substr($line->ncf, -11, 11),
329
                    $line->fecha,
330
                    $line->tipoanulacion
331
                )
332
            );
333
        }
334
    }
335
336
    public function checkDateFormat($dateValue)
337
    {
338
        $year = (substr($dateValue, 4, 1) === '-')
339
            ? substr($dateValue, 0, 4)
340
            : substr($dateValue, 6, 4);
341
        $month = (substr($dateValue, 4, 1) === '-')
342
            ? substr($dateValue, 5, 2)
343
            : substr($dateValue, 3, 2);
344
345
        return [$year, $month];
346
    }
347
}
348