Passed
Push — master ( e32a8b...a90170 )
by Joe Nilson
02:16
created

CommonFunctionsDominicanRepublic::exportTXT606()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 43
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 29
c 1
b 0
f 0
nc 2
nop 5
dl 0
loc 43
rs 9.456
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
        //array('RNC/Cédula','Tipo Id','Tipo Compra','NCF','NCF Modifica','Fecha Documento','Fecha Pago','Total Servicios','Total Bienes',
207
        //'Total Facturado','ITBIS Facturado',
208
        //'ITBIS Retenido','ITBIS sujeto a Proporcionalidad (Art. 349)','ITBIS llevado al Costo','ITBIS por Adelantar','ITBIS percibido en compras',
209
        //'Tipo de Retención en ISR','Monto Retencion Renta','ISR Percibido en compras','Impuesto Selectivo al Consumo','Otros Impuestos/Tasas','Monto Propina Legal','Forma de Pago'),
210
        foreach ($data as $line) {
211
            fwrite(
212
                $fp,
213
                sprintf(
214
                    "%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",
215
                    $line->cifnif,
216
                    $line->tipoid,
217
                    $line->tipocompra,
218
                    substr($line->ncf, -11, 11),
219
                    substr($line->ncfmodifica, -11, 11),
220
                    $line->fecha,
221
                    "",
222
                    number_format($line->totalservicios, 2, ".", ""),
223
                    number_format($line->totalbienes, 2, ".", ""),
224
                    number_format($line->base, 2, ".", ""),
225
                    number_format($line->itbis, 2, ".", ""),
226
                    "", "", "", "", "", "", "", "", "", "", "", "", ""
227
                ));
228
        }
229
    }
230
231
    /**
232
     * @param mixed $fp
233
     * @param string $rncCompany
234
     * @param string $yearReport
235
     * @param string $monthReport
236
     * @param array $whereReport
237
     * @return void
238
     */
239
    protected function exportTXT607(
240
        &$fp,
241
        string $rncCompany,
242
        string $yearReport,
243
        string $monthReport,
244
        array $whereReport
245
    ): void
246
    {
247
        $reportData = new FiscalReport607();
248
        $data = $reportData->all($whereReport);
249
        $dataCounter = count($data);
250
        fwrite(
251
            $fp,
252
            sprintf(
253
                "%s|%s|%4s%2s|%s\r\n",
254
                '607',
255
                $rncCompany,
256
                $yearReport,
257
                $monthReport,
258
                $dataCounter
259
            )
260
        );
261
        foreach ($data as $line) {
262
            fwrite(
263
                $fp,
264
                sprintf(
265
                    "%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",
266
                    $line->cifnif,
267
                    $line->tipoid,
268
                    substr($line->ncf, -11, 11),
269
                    substr($line->ncfmodifica, -11, 11),
270
                    1,
271
                    $line->fecha,
272
                    "",
273
                    number_format($line->base, 2, ".", ""),
274
                    number_format($line->itbis, 2, ".", ""),
275
                    "", "", "", "", "", "", "",
276
                    number_format($line->totalefectivo, 2, ".", ""),
277
                    number_format($line->totalcheque, 2, ".", ""),
278
                    number_format($line->totaltarjeta, 2, ".", ""),
279
                    number_format($line->totalcredito, 2, ".", ""),
280
                    number_format($line->totalbonos, 2, ".", ""),
281
                    number_format($line->totalpermuta, 2, ".", ""),
282
                    number_format($line->totalotrasformas, 2, ".", "")
283
                ));
284
        }
285
    }
286
287
    /**
288
     * @param mixed $fp
289
     * @param string $rncCompany
290
     * @param string $yearReport
291
     * @param string $monthReport
292
     * @param array $whereReport
293
     * @return void
294
     */
295
    protected function exportTXT608(
296
        &$fp,
297
        string $rncCompany,
298
        string $yearReport,
299
        string $monthReport,
300
        array $whereReport
301
    ): void {
302
        $reportData = new FiscalReport608();
303
        $data = $reportData->all($whereReport);
304
        $dataCounter = count($data);
305
        fwrite(
306
            $fp,
307
            sprintf(
308
                "%s|%s|%4s%2s|%s\r\n",
309
                '608',
310
                $rncCompany,
311
                $yearReport,
312
                $monthReport,
313
                $dataCounter
314
            )
315
        );
316
        foreach ($data as $line) {
317
            fwrite(
318
                $fp,
319
                sprintf(
320
                    "%s|%s|%s\r\n",
321
                    substr($line->ncf, -11, 11),
322
                    $line->fecha,
323
                    $line->tipoanulacion
324
                )
325
            );
326
        }
327
    }
328
329
    public function checkDateFormat($dateValue)
330
    {
331
        $year = (substr($dateValue, 4, 1) === '-')
332
            ? substr($dateValue, 0, 4)
333
            : substr($dateValue, 6, 4);
334
        $month = (substr($dateValue, 4, 1) === '-')
335
            ? substr($dateValue, 5, 2)
336
            : substr($dateValue, 3, 2);
337
338
        return [$year, $month];
339
    }
340
}
341