From 80e0ebdbfbe44d30fff76fb4862b7776c368f381 Mon Sep 17 00:00:00 2001 From: cmorenov1402 Date: Mon, 10 Mar 2025 09:57:58 +0100 Subject: [PATCH] Update BudgetItem Entity --- src/Entity/Project/BudgetItem.php | 31 ++++++++-------- .../Transformer/BudgetMapTransformer.php | 36 +++++++++---------- 2 files changed, 30 insertions(+), 37 deletions(-) diff --git a/src/Entity/Project/BudgetItem.php b/src/Entity/Project/BudgetItem.php index 09778b6..8041312 100644 --- a/src/Entity/Project/BudgetItem.php +++ b/src/Entity/Project/BudgetItem.php @@ -43,7 +43,7 @@ class BudgetItem implements LocalizedEntityInterface private ?Money $money = null; #[ORM\Column(enumType: ProjectDeadline::class)] - private ?ProjectDeadline $category = null; + private ?ProjectDeadline $deadline = null; public function getId(): ?int { @@ -98,22 +98,8 @@ class BudgetItem implements LocalizedEntityInterface return $this; } - // Quitar - private function getMoneyByCategory(string $category): ?Money - { - $nullValue = 0; - - return $this->category->value == $category ? $this->money : $nullValue; - } - - public function getMinimum(): ?Money - { - return $this->getMoneyByCategory(ProjectDeadline::Minimum->value); - } - - public function getOptimum(): ?Money - { - return $this->getMoneyByCategory(ProjectDeadline::Optimum->value); + public function getMoney(): ?Money{ + return $this->money; } public function setMoney(Money $money): static @@ -122,4 +108,15 @@ class BudgetItem implements LocalizedEntityInterface return $this; } + + public function getDeadline(): ?ProjectDeadline{ + return $this->deadline; + } + + public function setDeadline(ProjectDeadline $deadline): static + { + $this->deadline = $deadline; + + return $this; + } } diff --git a/src/Mapping/Transformer/BudgetMapTransformer.php b/src/Mapping/Transformer/BudgetMapTransformer.php index 0551243..9976913 100644 --- a/src/Mapping/Transformer/BudgetMapTransformer.php +++ b/src/Mapping/Transformer/BudgetMapTransformer.php @@ -8,12 +8,15 @@ use App\Entity\Money; use App\Entity\Project\BudgetItem; use App\Entity\Project\BudgetItemType; use App\Entity\Project\Project; +use App\Entity\Project\ProjectDeadline; use App\Library\Economy\MoneyService; use AutoMapper\Transformer\PropertyTransformer\PropertyTransformerInterface; class BudgetMapTransformer implements PropertyTransformerInterface { - public const VALID_SUMMARY_TYPES = ['minimum', 'optimum']; + public const MINIMUM = ProjectDeadline::Minimum->value; + public const OPTIMUM = ProjectDeadline::Optimum->value; + public const VALID_SUMMARY_TYPES = [self::MINIMUM, self::OPTIMUM]; public function __construct( private MoneyService $moneyService, @@ -28,8 +31,8 @@ class BudgetMapTransformer implements PropertyTransformerInterface $currency = $source->getAccounting()->getCurrency(); $budget = new Budget(); - $budget->minimum = $this->getItemsSummary($items, $currency, 'minimum'); - $budget->optimum = $this->getItemsSummary($items, $currency, 'optimum'); + $budget->minimum = $this->getItemsSummary($items, $currency, self::MINIMUM); + $budget->optimum = $this->getItemsSummary($items, $currency, self::OPTIMUM); return $budget; } @@ -48,31 +51,24 @@ class BudgetMapTransformer implements PropertyTransformerInterface */ private function calcItemsTotal(array $items, string $currency): array { - $totalMinimum = new Money(0, $currency); - $totalOptimum = new Money(0, $currency); + $totals = [ + self::MINIMUM => new Money(0, $currency), + self::OPTIMUM => new Money(0, $currency), + ]; foreach ($items as $item) { - $itemMinimum = $item->getMinimum(); + $itemMoney = $item->getMoney(); + $itemDeadlineValue = $item->getDeadline()->value; - if ($itemMinimum === null || !isset($itemMinimum->amount)) { - $itemMinimum = new Money(0, $currency); + if (isset($totals[$itemDeadlineValue])) { + $totals[$itemDeadlineValue] = $this->moneyService->add($itemMoney, $totals[$itemDeadlineValue]); } - - $totalMinimum = $this->moneyService->add($itemMinimum, $totalMinimum); - - $itemOptimum = $item->getOptimum(); - - if ($itemOptimum === null || !isset($itemOptimum->amount)) { - $itemOptimum = new Money(0, $currency); - } - - $itemOptimum = $this->moneyService->add($itemMinimum, $itemOptimum); - $totalOptimum = $this->moneyService->add($itemOptimum, $totalOptimum); } - return ['minimum' => $totalMinimum, 'optimum' => $totalOptimum]; + return $totals; } + private function getItemsSummary(array $items, string $currency, string $type) { if (!\in_array($type, self::VALID_SUMMARY_TYPES)) {