ncfTipoMovimiento()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

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

251
                    number_format(/** @scrutinizer ignore-type */ $itbisCosto, 2, ".", ""),
Loading history...
252
                    "", "", "", "", "", "", "", "",
253
                    $line->tipopago
254
                ));
255
        }
256
    }
257
258
    /**
259
     * @param mixed $fp
260
     * @param string $rncCompany
261
     * @param string $yearReport
262
     * @param string $monthReport
263
     * @param array $whereReport
264
     * @return void
265
     */
266
    protected function exportTXT607(
267
        &$fp,
268
        string $rncCompany,
269
        string $yearReport,
270
        string $monthReport,
271
        array $whereReport
272
    ): void
273
    {
274
        $reportData = new FiscalReport607();
275
        $data = $reportData->all($whereReport);
276
        $dataCounter = count($data);
277
        fwrite(
278
            $fp,
279
            sprintf(
280
                "%s|%s|%4s%2s|%s\r\n",
281
                '607',
282
                $rncCompany,
283
                $yearReport,
284
                $monthReport,
285
                $dataCounter
286
            )
287
        );
288
        foreach ($data as $line) {
289
            $convertValue = (substr($line->ncf, 1, 2) === '04') ? -1 : 1;
290
            fwrite(
291
                $fp,
292
                sprintf(
293
                    "%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",
294
                    $line->cifnif,
295
                    $line->tipoid,
296
                    substr($line->ncf, -11, 11),
297
                    substr($line->ncfmodifica, -11, 11),
298
                    1,
299
                    $line->fecha,
300
                    "",
301
                    number_format($line->base, 2, ".", ""),
302
                    number_format($line->itbis, 2, ".", ""),
303
                    "", "", "", "", "", "", "",
304
                    number_format($line->totalefectivo * $convertValue, 2, ".", ""),
305
                    number_format($line->totalcheque * $convertValue, 2, ".", ""),
306
                    number_format($line->totaltarjeta * $convertValue, 2, ".", ""),
307
                    number_format($line->totalcredito * $convertValue, 2, ".", ""),
308
                    number_format($line->totalbonos * $convertValue, 2, ".", ""),
309
                    number_format($line->totalpermuta * $convertValue, 2, ".", ""),
310
                    number_format($line->totalotrasformas * $convertValue, 2, ".", "")
311
                ));
312
        }
313
    }
314
315
    /**
316
     * @param mixed $fp
317
     * @param string $rncCompany
318
     * @param string $yearReport
319
     * @param string $monthReport
320
     * @param array $whereReport
321
     * @return void
322
     */
323
    protected function exportTXT608(
324
        &$fp,
325
        string $rncCompany,
326
        string $yearReport,
327
        string $monthReport,
328
        array $whereReport
329
    ): void {
330
        $reportData = new FiscalReport608();
331
        $data = $reportData->all($whereReport);
332
        $dataCounter = count($data);
333
        fwrite(
334
            $fp,
335
            sprintf(
336
                "%s|%s|%4s%2s|%s\r\n",
337
                '608',
338
                $rncCompany,
339
                $yearReport,
340
                $monthReport,
341
                $dataCounter
342
            )
343
        );
344
        foreach ($data as $line) {
345
            fwrite(
346
                $fp,
347
                sprintf(
348
                    "%s|%s|%s\r\n",
349
                    substr($line->ncf, -11, 11),
350
                    $line->fecha,
351
                    $line->tipoanulacion
352
                )
353
            );
354
        }
355
    }
356
357
    public function checkDateFormat($dateValue)
358
    {
359
        $year = (substr($dateValue, 4, 1) === '-')
360
            ? substr($dateValue, 0, 4)
361
            : substr($dateValue, 6, 4);
362
        $month = (substr($dateValue, 4, 1) === '-')
363
            ? substr($dateValue, 5, 2)
364
            : substr($dateValue, 3, 2);
365
366
        return [$year, $month];
367
    }
368
}
369
370
371