mirror of
https://github.com/Part-DB/Part-DB-server.git
synced 2025-06-22 09:53:35 +02:00
Use an enum for the part stock change type
This commit is contained in:
parent
9adfcc7aec
commit
7d99607919
5 changed files with 65 additions and 43 deletions
|
@ -43,7 +43,7 @@ class EnumColumn extends AbstractColumn
|
||||||
return ($this->getEnumClass())::from($value);
|
return ($this->getEnumClass())::from($value);
|
||||||
}
|
}
|
||||||
|
|
||||||
protected function configureOptions(OptionsResolver $resolver)
|
protected function configureOptions(OptionsResolver $resolver): static
|
||||||
{
|
{
|
||||||
parent::configureOptions($resolver);
|
parent::configureOptions($resolver);
|
||||||
|
|
||||||
|
|
|
@ -139,7 +139,7 @@ class LogDataTable implements DataTableTypeInterface
|
||||||
if ($context instanceof PartStockChangedLogEntry) {
|
if ($context instanceof PartStockChangedLogEntry) {
|
||||||
$text .= sprintf(
|
$text .= sprintf(
|
||||||
' (<i>%s</i>)',
|
' (<i>%s</i>)',
|
||||||
$this->translator->trans('log.part_stock_changed.' . $context->getInstockChangeType())
|
$this->translator->trans('log.part_stock_changed.' . $context->getInstockChangeType()->toExtraShortType())
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
51
src/Entity/LogSystem/PartStockChangeType.php
Normal file
51
src/Entity/LogSystem/PartStockChangeType.php
Normal file
|
@ -0,0 +1,51 @@
|
||||||
|
<?php
|
||||||
|
/*
|
||||||
|
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
|
||||||
|
*
|
||||||
|
* Copyright (C) 2019 - 2023 Jan Böhmer (https://github.com/jbtronics)
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU Affero General Public License as published
|
||||||
|
* by the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU Affero General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU Affero General Public License
|
||||||
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
namespace App\Entity\LogSystem;
|
||||||
|
|
||||||
|
enum PartStockChangeType: string
|
||||||
|
{
|
||||||
|
case ADD = "add";
|
||||||
|
case WITHDRAW = "withdraw";
|
||||||
|
case MOVE = "move";
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Converts the type to a short representation usable in the extra field of the log entry.
|
||||||
|
* @return string
|
||||||
|
*/
|
||||||
|
public function toExtraShortType(): string
|
||||||
|
{
|
||||||
|
return match ($this) {
|
||||||
|
self::ADD => 'a',
|
||||||
|
self::WITHDRAW => 'w',
|
||||||
|
self::MOVE => 'm',
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function fromExtraShortType(string $value): self
|
||||||
|
{
|
||||||
|
return match ($value) {
|
||||||
|
'a' => self::ADD,
|
||||||
|
'w' => self::WITHDRAW,
|
||||||
|
'm' => self::MOVE,
|
||||||
|
default => throw new \InvalidArgumentException("Invalid short type: $value"),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
|
@ -38,7 +38,7 @@ class PartStockChangedLogEntry extends AbstractLogEntry
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new part stock changed log entry.
|
* Creates a new part stock changed log entry.
|
||||||
* @param string $type The type of the log entry. One of the TYPE_* constants.
|
* @param PartStockChangeType $type The type of the log entry.
|
||||||
* @param PartLot $lot The part lot which has been changed.
|
* @param PartLot $lot The part lot which has been changed.
|
||||||
* @param float $old_stock The old stock of the lot.
|
* @param float $old_stock The old stock of the lot.
|
||||||
* @param float $new_stock The new stock of the lot.
|
* @param float $new_stock The new stock of the lot.
|
||||||
|
@ -46,14 +46,10 @@ class PartStockChangedLogEntry extends AbstractLogEntry
|
||||||
* @param string $comment The comment associated with the change.
|
* @param string $comment The comment associated with the change.
|
||||||
* @param PartLot|null $move_to_target The target lot if the type is TYPE_MOVE.
|
* @param PartLot|null $move_to_target The target lot if the type is TYPE_MOVE.
|
||||||
*/
|
*/
|
||||||
protected function __construct(string $type, PartLot $lot, float $old_stock, float $new_stock, float $new_total_part_instock, string $comment, ?PartLot $move_to_target = null)
|
protected function __construct(PartStockChangeType $type, PartLot $lot, float $old_stock, float $new_stock, float $new_total_part_instock, string $comment, ?PartLot $move_to_target = null)
|
||||||
{
|
{
|
||||||
parent::__construct();
|
parent::__construct();
|
||||||
|
|
||||||
if (!in_array($type, [self::TYPE_ADD, self::TYPE_WITHDRAW, self::TYPE_MOVE], true)) {
|
|
||||||
throw new \InvalidArgumentException('Invalid type for PartStockChangedLogEntry!');
|
|
||||||
}
|
|
||||||
|
|
||||||
//Same as every other element change log entry
|
//Same as every other element change log entry
|
||||||
$this->level = LogLevel::INFO;
|
$this->level = LogLevel::INFO;
|
||||||
|
|
||||||
|
@ -61,7 +57,7 @@ class PartStockChangedLogEntry extends AbstractLogEntry
|
||||||
|
|
||||||
$this->typeString = 'part_stock_changed';
|
$this->typeString = 'part_stock_changed';
|
||||||
$this->extra = array_merge($this->extra, [
|
$this->extra = array_merge($this->extra, [
|
||||||
't' => $this->typeToShortType($type),
|
't' => $type->toExtraShortType(),
|
||||||
'o' => $old_stock,
|
'o' => $old_stock,
|
||||||
'n' => $new_stock,
|
'n' => $new_stock,
|
||||||
'p' => $new_total_part_instock,
|
'p' => $new_total_part_instock,
|
||||||
|
@ -71,7 +67,7 @@ class PartStockChangedLogEntry extends AbstractLogEntry
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($move_to_target instanceof PartLot) {
|
if ($move_to_target instanceof PartLot) {
|
||||||
if ($type !== self::TYPE_MOVE) {
|
if ($type !== PartStockChangeType::MOVE) {
|
||||||
throw new \InvalidArgumentException('The move_to_target parameter can only be set if the type is "move"!');
|
throw new \InvalidArgumentException('The move_to_target parameter can only be set if the type is "move"!');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -90,7 +86,7 @@ class PartStockChangedLogEntry extends AbstractLogEntry
|
||||||
*/
|
*/
|
||||||
public static function add(PartLot $lot, float $old_stock, float $new_stock, float $new_total_part_instock, string $comment): self
|
public static function add(PartLot $lot, float $old_stock, float $new_stock, float $new_total_part_instock, string $comment): self
|
||||||
{
|
{
|
||||||
return new self(self::TYPE_ADD, $lot, $old_stock, $new_stock, $new_total_part_instock, $comment);
|
return new self(PartStockChangeType::ADD, $lot, $old_stock, $new_stock, $new_total_part_instock, $comment);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -104,7 +100,7 @@ class PartStockChangedLogEntry extends AbstractLogEntry
|
||||||
*/
|
*/
|
||||||
public static function withdraw(PartLot $lot, float $old_stock, float $new_stock, float $new_total_part_instock, string $comment): self
|
public static function withdraw(PartLot $lot, float $old_stock, float $new_stock, float $new_total_part_instock, string $comment): self
|
||||||
{
|
{
|
||||||
return new self(self::TYPE_WITHDRAW, $lot, $old_stock, $new_stock, $new_total_part_instock, $comment);
|
return new self(PartStockChangeType::WITHDRAW, $lot, $old_stock, $new_stock, $new_total_part_instock, $comment);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -118,16 +114,16 @@ class PartStockChangedLogEntry extends AbstractLogEntry
|
||||||
*/
|
*/
|
||||||
public static function move(PartLot $lot, float $old_stock, float $new_stock, float $new_total_part_instock, string $comment, PartLot $move_to_target): self
|
public static function move(PartLot $lot, float $old_stock, float $new_stock, float $new_total_part_instock, string $comment, PartLot $move_to_target): self
|
||||||
{
|
{
|
||||||
return new self(self::TYPE_MOVE, $lot, $old_stock, $new_stock, $new_total_part_instock, $comment, $move_to_target);
|
return new self(PartStockChangeType::MOVE, $lot, $old_stock, $new_stock, $new_total_part_instock, $comment, $move_to_target);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Returns the instock change type of this entry
|
* Returns the instock change type of this entry
|
||||||
* @return string One of the TYPE_* constants.
|
* @return PartStockChangeType
|
||||||
*/
|
*/
|
||||||
public function getInstockChangeType(): string
|
public function getInstockChangeType(): PartStockChangeType
|
||||||
{
|
{
|
||||||
return $this->shortTypeToType($this->extra['t']);
|
return PartStockChangeType::fromExtraShortType($this->extra['t']);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -177,30 +173,4 @@ class PartStockChangedLogEntry extends AbstractLogEntry
|
||||||
{
|
{
|
||||||
return $this->extra['m'] ?? null;
|
return $this->extra['m'] ?? null;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Converts the human-readable type (TYPE_* consts) to the version stored in DB
|
|
||||||
*/
|
|
||||||
protected function typeToShortType(string $type): string
|
|
||||||
{
|
|
||||||
return match ($type) {
|
|
||||||
self::TYPE_ADD => 'a',
|
|
||||||
self::TYPE_WITHDRAW => 'w',
|
|
||||||
self::TYPE_MOVE => 'm',
|
|
||||||
default => throw new \InvalidArgumentException('Invalid type: '.$type),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Converts the short type stored in DB to the human-readable type (TYPE_* consts).
|
|
||||||
*/
|
|
||||||
protected function shortTypeToType(string $short_type): string
|
|
||||||
{
|
|
||||||
return match ($short_type) {
|
|
||||||
'a' => self::TYPE_ADD,
|
|
||||||
'w' => self::TYPE_WITHDRAW,
|
|
||||||
'm' => self::TYPE_MOVE,
|
|
||||||
default => throw new \InvalidArgumentException('Invalid short type: '.$short_type),
|
|
||||||
};
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -33,6 +33,7 @@ use App\Entity\LogSystem\ElementEditedLogEntry;
|
||||||
use App\Entity\LogSystem\ExceptionLogEntry;
|
use App\Entity\LogSystem\ExceptionLogEntry;
|
||||||
use App\Entity\LogSystem\LegacyInstockChangedLogEntry;
|
use App\Entity\LogSystem\LegacyInstockChangedLogEntry;
|
||||||
use App\Entity\LogSystem\PartStockChangedLogEntry;
|
use App\Entity\LogSystem\PartStockChangedLogEntry;
|
||||||
|
use App\Entity\LogSystem\PartStockChangeType;
|
||||||
use App\Entity\LogSystem\SecurityEventLogEntry;
|
use App\Entity\LogSystem\SecurityEventLogEntry;
|
||||||
use App\Entity\LogSystem\UserLoginLogEntry;
|
use App\Entity\LogSystem\UserLoginLogEntry;
|
||||||
use App\Entity\LogSystem\UserLogoutLogEntry;
|
use App\Entity\LogSystem\UserLogoutLogEntry;
|
||||||
|
@ -187,7 +188,7 @@ class LogEntryExtraFormatter
|
||||||
if ($context->getComment() !== '') {
|
if ($context->getComment() !== '') {
|
||||||
$array['log.part_stock_changed.comment'] = htmlspecialchars($context->getComment());
|
$array['log.part_stock_changed.comment'] = htmlspecialchars($context->getComment());
|
||||||
}
|
}
|
||||||
if ($context->getInstockChangeType() === PartStockChangedLogEntry::TYPE_MOVE) {
|
if ($context->getInstockChangeType() === PartStockChangeType::MOVE) {
|
||||||
$array['log.part_stock_changed.move_target'] =
|
$array['log.part_stock_changed.move_target'] =
|
||||||
htmlspecialchars($this->elementTypeNameGenerator->getLocalizedTypeLabel(PartLot::class))
|
htmlspecialchars($this->elementTypeNameGenerator->getLocalizedTypeLabel(PartLot::class))
|
||||||
.' ' . $context->getMoveToTargetID();
|
.' ' . $context->getMoveToTargetID();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue