Test Failed
Push — master ( d1c4da...e7f98d )
by Carlos
08:34
created

Migrations::fixFormasPago()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 3
eloc 11
c 1
b 0
f 0
nc 3
nop 0
dl 0
loc 18
rs 9.9
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
    }
44
45
    private static function clearLogs(): void
46
    {
47
        $logModel = new LogMessage();
48
        $where = [new DataBaseWhere('channel', 'master')];
49
        if ($logModel->count($where) < 20000) {
50
            return;
51
        }
52
53
        // cuando hay miles de registros en el canal master, eliminamos los antiguos para evitar problemas de rendimiento
54
        $date = date("Y-m-d H:i:s", strtotime("-1 month"));
55
        $sql = "DELETE FROM logs WHERE channel = 'master' AND time < '" . $date . "';";
56
        self::db()->exec($sql);
57
    }
58
59
    private static function db(): DataBase
60
    {
61
        if (self::$database === null) {
62
            self::$database = new DataBase();
63
        }
64
65
        return self::$database;
66
    }
67
68
    // versión 2024.5, fecha 15-04-2024
69
    private static function fixFormasPago(): void
70
    {
71
        // forzamos la comprobación de la tabla formas_pago
72
        new FormaPago();
73
74
        // recorremos las tablas de documentos de compra o venta
75
        $tables = [
76
            'albaranescli', 'albaranesprov', 'facturascli', 'facturasprov', 'pedidoscli', 'pedidosprov',
77
            'presupuestoscli', 'presupuestosprov'
78
        ];
79
        foreach ($tables as $table) {
80
            // buscamos aquellos códigos de pago que no estén en la tabla formaspago
81
            $sql = "SELECT DISTINCT codpago FROM " . $table . " WHERE codpago NOT IN (SELECT codpago FROM formaspago);";
82
            foreach (self::db()->select($sql) as $row) {
83
                $formaPago = new FormaPago();
84
                $formaPago->codpago = $row['codpago'];
85
                $formaPago->descripcion = Tools::lang()->trans('deleted');
86
                $formaPago->save();
87
            }
88
        }
89
    }
90
91
    // version 2023.06, fecha 07-10-2023
92
    private static function fixSeries(): void
93
    {
94
        // forzamos la comprobación de la tabla series
95
        new Serie();
96
97
        // actualizamos con el tipo R la serie marcada como rectificativa en el panel de control
98
        $serieRectifying = Tools::settings('default', 'codserierec', '');
99
        if (empty($serieRectifying)) {
100
            return;
101
        }
102
103
        $sqlUpdate = "UPDATE series SET tipo = 'R' WHERE codserie = " . self::db()->var2str($serieRectifying) . ";";
104
        self::db()->exec($sqlUpdate);
105
    }
106
}
107