Test Failed
Push — master ( 3d762a...0faba9 )
by Carlos
08:30
created

Migrations::fixRectifiedInvoices()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 5
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 9
rs 10
1
<?php
2
/**
3
 * This file is part of FacturaScripts
4
 * Copyright (C) 2020-2024 Carlos Garcia Gomez <[email protected]>
5
 *
6
 * This program is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU Lesser General Public License as
8
 * published by the Free Software Foundation, either version 3 of the
9
 * License, or (at your option) any later version.
10
 *
11
 * This program is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14
 * GNU Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public License
17
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18
 */
19
20
namespace FacturaScripts\Core\Base;
21
22
use FacturaScripts\Core\Base\DataBase\DataBaseWhere;
23
use FacturaScripts\Core\Tools;
24
use FacturaScripts\Dinamic\Model\FormaPago;
25
use FacturaScripts\Dinamic\Model\LogMessage;
26
use FacturaScripts\Dinamic\Model\Serie;
27
28
/**
29
 * Description of Migrations
30
 *
31
 * @author Carlos Garcia Gomez <[email protected]>
32
 */
33
final class Migrations
34
{
35
    /** @var DataBase */
36
    private static $database;
37
38
    public static function run(): void
39
    {
40
        self::clearLogs();
41
        self::fixSeries();
42
        self::fixFormasPago();
43
        self::fixRectifiedInvoices();
44
    }
45
46
    private static function clearLogs(): void
47
    {
48
        $logModel = new LogMessage();
49
        $where = [new DataBaseWhere('channel', 'master')];
50
        if ($logModel->count($where) < 20000) {
51
            return;
52
        }
53
54
        // cuando hay miles de registros en el canal master, eliminamos los antiguos para evitar problemas de rendimiento
55
        $date = date("Y-m-d H:i:s", strtotime("-1 month"));
56
        $sql = "DELETE FROM logs WHERE channel = 'master' AND time < '" . $date . "';";
57
        self::db()->exec($sql);
58
    }
59
60
    private static function db(): DataBase
61
    {
62
        if (self::$database === null) {
63
            self::$database = new DataBase();
64
        }
65
66
        return self::$database;
67
    }
68
69
    // versión 2024.5, fecha 15-04-2024
70
    private static function fixFormasPago(): void
71
    {
72
        // forzamos la comprobación de la tabla formas_pago
73
        new FormaPago();
74
75
        // recorremos las tablas de documentos de compra o venta
76
        $tables = [
77
            'albaranescli', 'albaranesprov', 'facturascli', 'facturasprov', 'pedidoscli', 'pedidosprov',
78
            'presupuestoscli', 'presupuestosprov'
79
        ];
80
        foreach ($tables as $table) {
81
            // buscamos aquellos códigos de pago que no estén en la tabla formaspago
82
            $sql = "SELECT DISTINCT codpago FROM " . $table . " WHERE codpago NOT IN (SELECT codpago FROM formaspago);";
83
            foreach (self::db()->select($sql) as $row) {
84
                $formaPago = new FormaPago();
85
                $formaPago->codpago = $row['codpago'];
86
                $formaPago->descripcion = Tools::lang()->trans('deleted');
87
                $formaPago->save();
88
            }
89
        }
90
    }
91
92
    // versión 2024.5, fecha 16-04-2024
93
    private static function fixRectifiedInvoices(): void
94
    {
95
        // ponemos a null el idfacturarect de las facturas que rectifiquen a una factura que no existe
96
        foreach (['facturascli', 'facturasprov'] as $table) {
97
            $sql = "UPDATE " . $table . " SET idfacturarect = NULL"
98
                . " WHERE idfacturarect IS NOT NULL"
99
                . " AND idfacturarect NOT IN (SELECT idfactura FROM (SELECT idfactura FROM " . $table . ") AS subquery);";
100
101
            self::db()->exec($sql);
102
        }
103
    }
104
105
    // version 2023.06, fecha 07-10-2023
106
    private static function fixSeries(): void
107
    {
108
        // forzamos la comprobación de la tabla series
109
        new Serie();
110
111
        // actualizamos con el tipo R la serie marcada como rectificativa en el panel de control
112
        $serieRectifying = Tools::settings('default', 'codserierec', '');
113
        if (empty($serieRectifying)) {
114
            return;
115
        }
116
117
        $sqlUpdate = "UPDATE series SET tipo = 'R' WHERE codserie = " . self::db()->var2str($serieRectifying) . ";";
118
        self::db()->exec($sqlUpdate);
119
    }
120
}
121