Issues (232)

Lib/CommonFunctionsDominicanRepublic.php (1 issue)

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;
21
use FacturaScripts\Dinamic\Model\NCFTipo;
22
use FacturaScripts\Dinamic\Model\Join\FiscalReport606;
23
use FacturaScripts\Dinamic\Model\Join\FiscalReport607;
24
use FacturaScripts\Dinamic\Model\Join\FiscalReport608;
25
use FacturaScripts\Plugins\fsRepublicaDominicana\Interfaces\CommonFunctionsInterface;
26
use FacturaScripts\Core\Base\DataBase\DataBaseWhere;
27
use FacturaScripts\Dinamic\Model\NCFRango;
28
use FacturaScripts\Plugins\fsRepublicaDominicana\Model\NCFTipoAnulacion;
29
use FacturaScripts\Plugins\fsRepublicaDominicana\Model\NCFTipoMovimiento;
30
31
class CommonFunctionsDominicanRepublic implements CommonFunctionsInterface
32
{
33
    public static function ncfRango()
34
    {
35
        // TODO: Implement ncfRango() method.
36
    }
37
38
    public static function ncfCorrelativo($tipoComprobante, $idempresa)
39
    {
40
        $tipocomprobante = new NCFRango();
41
        $where = [
42
            new DatabaseWhere('tipocomprobante', $tipoComprobante),
43
            new DatabaseWhere('idempresa', $idempresa),
44
            new DatabaseWhere('estado', 1)
45
        ];
46
        $comprobante = $tipocomprobante->all($where);
47
        if ($comprobante) {
48
            echo json_encode(['existe' => $comprobante], JSON_THROW_ON_ERROR);
49
        } else {
50
            echo json_encode(['existe' => false], JSON_THROW_ON_ERROR);
51
        }
52
    }
53
54
    /**
55
     * @throws \JsonException
56
     */
57
    public static function ncfTipoPago(string $tipoPago)
58
    {
59
        $where = [new DatabaseWhere('tipopago', $tipoPago)];
60
        $tipoPagos = new NCFTipoPago();
61
        $pagos = $tipoPagos->all($where);
62
        if ($pagos) {
63
            echo \json_encode(['pagos' => $pagos], JSON_THROW_ON_ERROR);
64
        } else {
65
            echo '';
66
        }
67
    }
68
69
    /**
70
     * @throws \JsonException
71
     */
72
    public static function ncfTipoMovimiento(string $tipoMovimiento)
73
    {
74
        $tipomovimiento = new NCFTipoMovimiento();
75
        $where = [new DatabaseWhere('tipomovimiento', $tipoMovimiento)];
76
        $movimientos = $tipomovimiento->all($where);
77
        if ($movimientos) {
78
            echo json_encode(['movimientos' => $movimientos], JSON_THROW_ON_ERROR);
79
        } else {
80
            echo '';
81
        }
82
    }
83
84
    public static function ncfTipoAnulacion(string $tipoAnulacion)
85
    {
86
        $where = [new DatabaseWhere('codigo', $tipoAnulacion)];
87
        $tipoAnulaciones = new NCFTipoAnulacion();
88
        $anulaciones = $tipoAnulaciones->all($where);
89
        if ($anulaciones) {
90
            echo \json_encode(['anulaciones' => $anulaciones], JSON_THROW_ON_ERROR);
91
        } else {
92
            echo '';
93
        }
94
    }
95
96
    /**
97
     * @throws \JsonException
98
     */
99
    public static function ncfTipoComprobante($tipoComprobante)
100
    {
101
        $where = [new DatabaseWhere($tipoComprobante, 'Y')];
102
        $tipoComprobantes = new NCFTipo();
103
        $lista = $tipoComprobantes->all($where);
104
        if ($lista) {
105
            echo json_encode(['tipocomprobantes' => $lista], JSON_THROW_ON_ERROR);
106
        } else {
107
            echo '';
108
        }
109
    }
110
111
    public static function ncfFechaVencimiento()
112
    {
113
        // TODO: Implement ncfFechaVencimiento() method.
114
    }
115
116
    /**
117
     * @throws \JsonException
118
     */
119
    public static function ncfTipoCliente(string $cliente)
120
    {
121
        $NCFTipo = new NCFTipo();
122
        $tipoCliente = $NCFTipo->tipoCliente($cliente);
123
        if ($tipoCliente) {
124
            echo json_encode(['infocliente' => $tipoCliente], JSON_THROW_ON_ERROR);
125
        } else {
126
            echo '';
127
        }
128
    }
129
130
    public static function verifyDocument($ncf, $proveedor): void
131
    {
132
        if ($ncf !== "") {
133
            $facturasProveedores = new FacturaProveedor();
134
            $where = [
135
                new DataBaseWhere('numeroncf', $ncf),
136
                new DataBaseWhere('codproveedor', $proveedor)
137
            ];
138
            $verificacion = $facturasProveedores->all($where);
139
            if (!$verificacion) {
140
                echo json_encode(['success' => true], JSON_THROW_ON_ERROR);
141
            } else {
142
                $message = "Factura: " . $verificacion[0]->idfactura . " Fecha: " . $verificacion[0]->fecha;
143
                echo json_encode(['error' => true, 'message' => $message], JSON_THROW_ON_ERROR);
144
            }
145
        }
146
    }
147
148
    public function exportTXT(
149
        string $report,
150
        string $fileName,
151
        string $rncCompany,
152
        string $yearReport,
153
        string $monthReport,
154
        array $whereReport
155
    ) {
156
        if (file_exists($fileName)) {
157
            unlink($fileName);
158
        }
159
        $dataCounter = 0;
0 ignored issues
show
The assignment to $dataCounter is dead and can be removed.
Loading history...
160
        $fp = fopen($fileName, "w");
161
        switch ($report) {
162
            case "606":
163
                $this->exportTXT606(
164
                    $fp,
165
                    $rncCompany,
166
                    $yearReport,
167
                    $monthReport,
168
                    $whereReport
169
                );
170
                break;
171
            case "607":
172
            default:
173
                $this->exportTXT607(
174
                    $fp,
175
                    $rncCompany,
176
                    $yearReport,
177
                    $monthReport,
178
                    $whereReport
179
                );
180
                break;
181
            case "608":
182
                $this->exportTXT608(
183
                    $fp,
184
                    $rncCompany,
185
                    $yearReport,
186
                    $monthReport,
187
                    $whereReport
188
                );
189
                break;
190
        }
191
        fclose($fp);
192
        return true;
193
    }
194
195
    /**
196
     * @param mixed $fp
197
     * @param string $rncCompany
198
     * @param string $yearReport
199
     * @param string $monthReport
200
     * @param array $whereReport
201
     * @return void
202
     */
203
    protected function exportTXT606(
204
        &$fp,
205
        string $rncCompany,
206
        string $yearReport,
207
        string $monthReport,
208
        array $whereReport
209
    ): void
210
    {
211
        $reportData = new FiscalReport606();
212
        $data = $reportData->all($whereReport);
213
        $dataCounter = count($data);
214
        fwrite(
215
            $fp,
216
            sprintf(
217
                "%s|%s|%4s%2s|%s\r\n",
218
                '606',
219
                $rncCompany,
220
                $yearReport,
221
                $monthReport,
222
                $dataCounter
223
            )
224
        );
225
226
        foreach ($data as $line) {
227
            $convertValue = (substr($line->ncf, 1, 2) === '04') ? -1 : 1;
228
            $ncfModifica = ($line->ncfmodifica) ? substr($line->ncfmodifica, -11, 11) : "";
229
            $fechaPago = ($line->fechapago === '') ? $line->fechapago : $line->fecha;
230
            $itbisCosto = ($line->totalservicios > 0)
231
                ? number_format($line->itbis, 2, ".", "")
232
                : 0;
233
            fwrite(
234
                $fp,
235
                sprintf(
236
                    "%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",
237
                    $line->cifnif,
238
                    $line->tipoid,
239
                    $line->tipocompra,
240
                    substr($line->ncf, -11, 11),
241
                    $ncfModifica,
242
                    $line->fecha,
243
                    $fechaPago,
244
                    number_format($line->totalservicios * $convertValue, 2, ".", ""),
245
                    number_format($line->totalbienes * $convertValue, 2, ".", ""),
246
                    number_format($line->base, 2, ".", ""),
247
                    number_format($line->itbis, 2, ".", ""),
248
                    "", "", "",
249
                    number_format($itbisCosto, 2, ".", ""),
250
                    "", "", "", "", "", "", "", "",
251
                    $line->tipopago
252
                ));
253
        }
254
    }
255
256
    /**
257
     * @param mixed $fp
258
     * @param string $rncCompany
259
     * @param string $yearReport
260
     * @param string $monthReport
261
     * @param array $whereReport
262
     * @return void
263
     */
264
    protected function exportTXT607(
265
        &$fp,
266
        string $rncCompany,
267
        string $yearReport,
268
        string $monthReport,
269
        array $whereReport
270
    ): void
271
    {
272
        $reportData = new FiscalReport607();
273
        $data = $reportData->all($whereReport);
274
        $dataCounter = count($data);
275
        fwrite(
276
            $fp,
277
            sprintf(
278
                "%s|%s|%4s%2s|%s\r\n",
279
                '607',
280
                $rncCompany,
281
                $yearReport,
282
                $monthReport,
283
                $dataCounter
284
            )
285
        );
286
        foreach ($data as $line) {
287
            $convertValue = (substr($line->ncf, 1, 2) === '04') ? -1 : 1;
288
            fwrite(
289
                $fp,
290
                sprintf(
291
                    "%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",
292
                    $line->cifnif,
293
                    $line->tipoid,
294
                    substr($line->ncf, -11, 11),
295
                    substr($line->ncfmodifica, -11, 11),
296
                    1,
297
                    $line->fecha,
298
                    "",
299
                    number_format($line->base, 2, ".", ""),
300
                    number_format($line->itbis, 2, ".", ""),
301
                    "", "", "", "", "", "", "",
302
                    number_format($line->totalefectivo * $convertValue, 2, ".", ""),
303
                    number_format($line->totalcheque * $convertValue, 2, ".", ""),
304
                    number_format($line->totaltarjeta * $convertValue, 2, ".", ""),
305
                    number_format($line->totalcredito * $convertValue, 2, ".", ""),
306
                    number_format($line->totalbonos * $convertValue, 2, ".", ""),
307
                    number_format($line->totalpermuta * $convertValue, 2, ".", ""),
308
                    number_format($line->totalotrasformas * $convertValue, 2, ".", "")
309
                ));
310
        }
311
    }
312
313
    /**
314
     * @param mixed $fp
315
     * @param string $rncCompany
316
     * @param string $yearReport
317
     * @param string $monthReport
318
     * @param array $whereReport
319
     * @return void
320
     */
321
    protected function exportTXT608(
322
        &$fp,
323
        string $rncCompany,
324
        string $yearReport,
325
        string $monthReport,
326
        array $whereReport
327
    ): void {
328
        $reportData = new FiscalReport608();
329
        $data = $reportData->all($whereReport);
330
        $dataCounter = count($data);
331
        fwrite(
332
            $fp,
333
            sprintf(
334
                "%s|%s|%4s%2s|%s\r\n",
335
                '608',
336
                $rncCompany,
337
                $yearReport,
338
                $monthReport,
339
                $dataCounter
340
            )
341
        );
342
        foreach ($data as $line) {
343
            fwrite(
344
                $fp,
345
                sprintf(
346
                    "%s|%s|%s\r\n",
347
                    substr($line->ncf, -11, 11),
348
                    $line->fecha,
349
                    $line->tipoanulacion
350
                )
351
            );
352
        }
353
    }
354
355
    public function checkDateFormat($dateValue)
356
    {
357
        $year = (substr($dateValue, 4, 1) === '-')
358
            ? substr($dateValue, 0, 4)
359
            : substr($dateValue, 6, 4);
360
        $month = (substr($dateValue, 4, 1) === '-')
361
            ? substr($dateValue, 5, 2)
362
            : substr($dateValue, 3, 2);
363
364
        return [$year, $month];
365
    }
366
}
367
368
369