Issues (257)

Entity/LogSystem/LegacyInstockChangedLogEntry.php (1 issue)

1
<?php
2
/**
3
 * This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
4
 *
5
 * Copyright (C) 2019 - 2022 Jan Böhmer (https://github.com/jbtronics)
6
 *
7
 * This program is free software: you can redistribute it and/or modify
8
 * it under the terms of the GNU Affero General Public License as published
9
 * by the Free Software Foundation, either version 3 of the License, or
10
 * (at your option) any later version.
11
 *
12
 * This program is distributed in the hope that it will be useful,
13
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15
 * GNU Affero General Public License for more details.
16
 *
17
 * You should have received a copy of the GNU Affero General Public License
18
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
19
 */
20
21
declare(strict_types=1);
22
23
namespace App\Entity\LogSystem;
24
25
use Doctrine\ORM\Mapping as ORM;
26
27
/**
28
 * @ORM\Entity()
29
 */
30
class LegacyInstockChangedLogEntry extends AbstractLogEntry
31
{
32
    protected string $typeString = 'instock_changed';
33
34
    /**
35
     * Get the old instock.
36
     */
37
    public function getOldInstock(): int
38
    {
39
        return $this->extra['o'];
40
    }
41
42
    /**
43
     * Get the new instock.
44
     */
45
    public function getNewInstock(): int
46
    {
47
        return $this->extra['n'];
48
    }
49
50
    /**
51
     * Gets the comment associated with the instock change.
52
     */
53
    public function getComment(): string
54
    {
55
        return $this->extra['c'];
56
    }
57
58
    /**
59
     * Returns the price that has to be payed for the change (in the base currency).
60
     *
61
     * @param bool $absolute Set this to true, if you want only get the absolute value of the price (without minus)
62
     */
63
    public function getPrice(bool $absolute = false): float
64
    {
65
        if ($absolute) {
66
            return abs($this->extra['p']);
67
        }
68
69
        return $this->extra['p'];
70
    }
71
72
    /**
73
     * Returns the difference value of the change ($new_instock - $old_instock).
74
     *
75
     * @param bool $absolute set this to true if you want only the absolute value of the difference
76
     *
77
     * @return int difference is positive if instock has increased, negative if decreased
78
     */
79
    public function getDifference(bool $absolute = false): int
80
    {
81
        // Check if one of the instock values is unknown
82
        if (-2 === $this->getNewInstock() || -2 === $this->getOldInstock()) {
83
            return 0;
84
        }
85
86
        $difference = $this->getNewInstock() - $this->getOldInstock();
87
        if ($absolute) {
88
            return abs($difference);
0 ignored issues
show
Bug Best Practice introduced by
The expression return abs($difference) could return the type double which is incompatible with the type-hinted return integer. Consider adding an additional type-check to rule them out.
Loading history...
89
        }
90
91
        return $difference;
92
    }
93
94
    /**
95
     * Checks if the Change was an withdrawal of parts.
96
     *
97
     * @return bool true if the change was an withdrawal, false if not
98
     */
99
    public function isWithdrawal(): bool
100
    {
101
        return $this->getNewInstock() < $this->getOldInstock();
102
    }
103
}
104