From bfb5e06e28dd8ec50f5039ebf24c5335a00ad59c Mon Sep 17 00:00:00 2001 From: Nikita Tychuk Date: Sun, 22 Aug 2021 12:14:08 +0300 Subject: [PATCH] Allow to ignore auction sell in calculations --- migrations/Version20210822090029.php | 31 +++++++++++++ src/Controller/CalculatorController.php | 12 ++++-- src/Entity/Calculator/UserCalculation.php | 17 ++++++++ .../Calculator/CalculatorParamsFactory.php | 43 +++++++++++++------ src/ViewModel/Calculator/Inventory.php | 25 +++++++++++ src/ViewModel/Calculator/Mining.php | 25 +++++++++++ src/ViewModel/Calculator/Trade.php | 25 +++++++++++ .../calculator/_auction_sell_form.html.twig | 6 +++ templates/calculator/inventory.html.twig | 1 + templates/calculator/mining.html.twig | 1 + templates/calculator/trade.html.twig | 1 + 11 files changed, 170 insertions(+), 17 deletions(-) create mode 100644 migrations/Version20210822090029.php create mode 100644 templates/calculator/_auction_sell_form.html.twig diff --git a/migrations/Version20210822090029.php b/migrations/Version20210822090029.php new file mode 100644 index 00000000..795dacdd --- /dev/null +++ b/migrations/Version20210822090029.php @@ -0,0 +1,31 @@ +addSql('ALTER TABLE user_calculation ADD is_auction_sell_allowed TINYINT(1) NOT NULL'); + } + + public function down(Schema $schema): void + { + // this down() migration is auto-generated, please modify it to your needs + $this->addSql('ALTER TABLE user_calculation DROP is_auction_sell_allowed'); + } +} diff --git a/src/Controller/CalculatorController.php b/src/Controller/CalculatorController.php index 0384eef3..ba0b7dbf 100644 --- a/src/Controller/CalculatorController.php +++ b/src/Controller/CalculatorController.php @@ -163,7 +163,8 @@ public function inventory(): Response $calculatorParams = $this->calculatorParamsFactory->createParamsForInventory( $materials, $devices, - $userInventory->getMaterialQtys() + $userInventory->getMaterialQtys(), + $userCalculation->getIsAuctionSellAllowed() ); $deals = $this->calculatorService->calculateForInventory( @@ -216,7 +217,8 @@ public function mining(): Response $calculatorParams = $this->calculatorParamsFactory->createParamsForMining( $materials, $devices, - $userMining->getAcceptableMaterialIds() + $userMining->getAcceptableMaterialIds(), + $userCalculation->getIsAuctionSellAllowed() ); $deals = $this->calculatorService->calculateForMining( @@ -264,7 +266,11 @@ public function trade(): Response if (!$viewModel->hasErrors()) { $this->getDoctrine()->getManager()->flush(); - $calculatorParams = $this->calculatorParamsFactory->createParamsForTrade($materials, $devices); + $calculatorParams = $this->calculatorParamsFactory->createParamsForTrade( + $materials, + $devices, + $userCalculation->getIsAuctionSellAllowed() + ); $deals = $this->calculatorService->calculateForTrade( $calculatorParams, diff --git a/src/Entity/Calculator/UserCalculation.php b/src/Entity/Calculator/UserCalculation.php index 451fc3b6..ff3c29d9 100644 --- a/src/Entity/Calculator/UserCalculation.php +++ b/src/Entity/Calculator/UserCalculation.php @@ -31,6 +31,11 @@ class UserCalculation */ private $maximizationParamCode; + /** + * @ORM\Column(type="boolean") + */ + private $isAuctionSellAllowed; + public function __construct() { $this->maximizationParamCode = UserCalculation::CREDIT_CODE; @@ -64,4 +69,16 @@ public function setMaximizationParamCode(?string $maximizationParamCode): self return $this; } + + public function getIsAuctionSellAllowed(): ?bool + { + return $this->isAuctionSellAllowed; + } + + public function setIsAuctionSellAllowed(bool $isAuctionSellAllowed): self + { + $this->isAuctionSellAllowed = $isAuctionSellAllowed; + + return $this; + } } diff --git a/src/Service/Calculator/CalculatorParamsFactory.php b/src/Service/Calculator/CalculatorParamsFactory.php index f0e968ba..b56d1d04 100644 --- a/src/Service/Calculator/CalculatorParamsFactory.php +++ b/src/Service/Calculator/CalculatorParamsFactory.php @@ -18,40 +18,46 @@ class CalculatorParamsFactory * @param Material[] $materials * @param Device[] $devices * @param int[] $inventoryQtys + * @param bool $isAuctionSellAllowed * @return CalculatorParams */ public function createParamsForInventory( array $materials, array $devices, - array $inventoryQtys + array $inventoryQtys, + bool $isAuctionSellAllowed = true ): CalculatorParams { - return $this->createParams($materials, $devices, $inventoryQtys); + return $this->createParams($materials, $devices, $inventoryQtys, [], $isAuctionSellAllowed); } /** * @param Material[] $materials * @param Device[] $devices * @param int[] $miningAcceptableIds + * @param bool $isAuctionSellAllowed * @return CalculatorParams */ public function createParamsForMining( array $materials, array $devices, - array $miningAcceptableIds + array $miningAcceptableIds, + bool $isAuctionSellAllowed = true ): CalculatorParams { - return $this->createParams($materials, $devices, [], $miningAcceptableIds); + return $this->createParams($materials, $devices, [], $miningAcceptableIds, $isAuctionSellAllowed); } /** * @param Material[] $materials * @param Device[] $devices + * @param bool $isAuctionSellAllowed * @return CalculatorParams */ public function createParamsForTrade( array $materials, - array $devices + array $devices, + bool $isAuctionSellAllowed = true ): CalculatorParams { - return $this->createParams($materials, $devices); + return $this->createParams($materials, $devices, [], [], $isAuctionSellAllowed); } /** @@ -59,23 +65,26 @@ public function createParamsForTrade( * @param Device[] $devices * @param int[] $inventoryQtys * @param int[] $miningAcceptableIds + * @param bool $isAuctionSellAllowed * @return CalculatorParams */ public function createParams( array $materials, array $devices, array $inventoryQtys = [], - array $miningAcceptableIds = [] + array $miningAcceptableIds = [], + bool $isAuctionSellAllowed = true ): CalculatorParams { $params = new CalculatorParams(); $params->setMaterialStockItems($this->createMaterialStockItems( $materials, $inventoryQtys, - $miningAcceptableIds + $miningAcceptableIds, + $isAuctionSellAllowed )); - $params->setDeviceStockItems($this->createDeviceStockItems($devices)); + $params->setDeviceStockItems($this->createDeviceStockItems($devices, $isAuctionSellAllowed)); return $params; } @@ -84,10 +93,15 @@ public function createParams( * @param Material[] $materials * @param int[] $inventoryQtys * @param int[] $miningAcceptableIds + * @param bool $isAuctionSellAllowed * @return MaterialStockItem[] */ - private function createMaterialStockItems(array $materials, array $inventoryQtys, array $miningAcceptableIds): array - { + private function createMaterialStockItems( + array $materials, + array $inventoryQtys, + array $miningAcceptableIds, + bool $isAuctionSellAllowed + ): array { /** @var MaterialStockItem[] $materialItems */ $materialItems = []; @@ -121,7 +135,7 @@ private function createMaterialStockItems(array $materials, array $inventoryQtys $destinations[] = $this->createMarketplaceDestination($marketplacePrice); } - if ($auctionPrice !== null) { + if ($auctionPrice !== null && $isAuctionSellAllowed) { $destinations[] = $this->createAuctionDestination($auctionPrice); } @@ -136,9 +150,10 @@ private function createMaterialStockItems(array $materials, array $inventoryQtys /** * @param Device[] $devices + * @param bool $isAuctionSellAllowed * @return DeviceStockItem[] */ - private function createDeviceStockItems(array $devices): array + private function createDeviceStockItems(array $devices, bool $isAuctionSellAllowed): array { /** @var DeviceStockItem[] $deviceItems */ $deviceItems = []; @@ -169,7 +184,7 @@ private function createDeviceStockItems(array $devices): array $destinations[] = $this->createMarketplaceDestination($marketplacePrice); } - if ($auctionPrice !== null) { + if ($auctionPrice !== null && $isAuctionSellAllowed) { $destinations[] = $this->createAuctionDestination($auctionPrice); } diff --git a/src/ViewModel/Calculator/Inventory.php b/src/ViewModel/Calculator/Inventory.php index c0915470..2aa35e32 100644 --- a/src/ViewModel/Calculator/Inventory.php +++ b/src/ViewModel/Calculator/Inventory.php @@ -20,6 +20,11 @@ class Inventory extends AbstractViewModel */ private $maximizationParamList; + /** + * @var bool + */ + private $isAuctionSellAllowed = false; + /** * @var DealGrid */ @@ -61,6 +66,8 @@ public function fillFromRequest(Request $request): void $this->maximizationParamList->fillFromRequest($request); $this->addErrors($this->maximizationParamList->getErrors()); + $this->isAuctionSellAllowed = (bool)$request->request->get('auction-sell-allowed'); + $this->inventoryMaterialGrid->fillFromRequest($request); } @@ -71,6 +78,7 @@ public function fillFromRequest(Request $request): void public function fillFromUser(UserCalculation $userCalculation, UserInventory $userInventory): void { $this->maximizationParamList->fillFromUserCalculation($userCalculation); + $this->isAuctionSellAllowed = $userCalculation->getIsAuctionSellAllowed(); $this->inventoryMaterialGrid->fillFromModels($userInventory->getMaterials()->toArray()); } @@ -90,6 +98,7 @@ public function fillFromDeals(array $deals): void public function fillUser(UserCalculation $userCalculation, UserInventory $userInventory): void { $this->maximizationParamList->fillUserCalculation($userCalculation); + $userCalculation->setIsAuctionSellAllowed($this->isAuctionSellAllowed); $this->inventoryMaterialGrid->fillModels($userInventory->getMaterials()->toArray(), $userInventory); } @@ -156,4 +165,20 @@ public function setHasCalculationResult(bool $hasCalculationResult): void { $this->hasCalculationResult = $hasCalculationResult; } + + /** + * @return bool + */ + public function isAuctionSellAllowed(): bool + { + return $this->isAuctionSellAllowed; + } + + /** + * @param bool $isAuctionSellAllowed + */ + public function setIsAuctionSellAllowed(bool $isAuctionSellAllowed): void + { + $this->isAuctionSellAllowed = $isAuctionSellAllowed; + } } diff --git a/src/ViewModel/Calculator/Mining.php b/src/ViewModel/Calculator/Mining.php index b08c2c9a..498b1efd 100644 --- a/src/ViewModel/Calculator/Mining.php +++ b/src/ViewModel/Calculator/Mining.php @@ -20,6 +20,11 @@ class Mining extends AbstractViewModel */ private $maximizationParamList; + /** + * @var bool + */ + private $isAuctionSellAllowed = false; + /** * @var DealGrid */ @@ -61,6 +66,8 @@ public function fillFromRequest(Request $request): void $this->maximizationParamList->fillFromRequest($request); $this->addErrors($this->maximizationParamList->getErrors()); + $this->isAuctionSellAllowed = (bool)$request->request->get('auction-sell-allowed'); + $this->miningMaterialGrid->fillFromRequest($request); } @@ -71,6 +78,7 @@ public function fillFromRequest(Request $request): void public function fillFromUser(UserCalculation $userCalculation, UserMining $userMining): void { $this->maximizationParamList->fillFromUserCalculation($userCalculation); + $this->isAuctionSellAllowed = $userCalculation->getIsAuctionSellAllowed(); $this->miningMaterialGrid->fillFromModels($userMining->getMaterials()->toArray()); } @@ -90,6 +98,7 @@ public function fillFromDeals(array $deals): void public function fillUser(UserCalculation $userCalculation, UserMining $userMining): void { $this->maximizationParamList->fillUserCalculation($userCalculation); + $userCalculation->setIsAuctionSellAllowed($this->isAuctionSellAllowed); $this->miningMaterialGrid->fillModels($userMining->getMaterials()->toArray(), $userMining); } @@ -156,4 +165,20 @@ public function setHasCalculationResult(bool $hasCalculationResult): void { $this->hasCalculationResult = $hasCalculationResult; } + + /** + * @return bool + */ + public function isAuctionSellAllowed(): bool + { + return $this->isAuctionSellAllowed; + } + + /** + * @param bool $isAuctionSellAllowed + */ + public function setIsAuctionSellAllowed(bool $isAuctionSellAllowed): void + { + $this->isAuctionSellAllowed = $isAuctionSellAllowed; + } } diff --git a/src/ViewModel/Calculator/Trade.php b/src/ViewModel/Calculator/Trade.php index fb922362..b8b649bb 100644 --- a/src/ViewModel/Calculator/Trade.php +++ b/src/ViewModel/Calculator/Trade.php @@ -17,6 +17,11 @@ class Trade extends AbstractViewModel */ private $maximizationParamList; + /** + * @var bool + */ + private $isAuctionSellAllowed = false; + /** * @var DealGrid */ @@ -46,6 +51,8 @@ public function fillFromRequest(Request $request): void { $this->maximizationParamList->fillFromRequest($request); $this->addErrors($this->maximizationParamList->getErrors()); + + $this->isAuctionSellAllowed = (bool)$request->request->get('auction-sell-allowed'); } /** @@ -54,6 +61,7 @@ public function fillFromRequest(Request $request): void public function fillFromUser(UserCalculation $userCalculation): void { $this->maximizationParamList->fillFromUserCalculation($userCalculation); + $this->isAuctionSellAllowed = $userCalculation->getIsAuctionSellAllowed(); } /** @@ -71,6 +79,7 @@ public function fillFromDeals(array $deals): void public function fillUser(UserCalculation $userCalculation): void { $this->maximizationParamList->fillUserCalculation($userCalculation); + $userCalculation->setIsAuctionSellAllowed($this->isAuctionSellAllowed); } /** @@ -120,4 +129,20 @@ public function setHasCalculationResult(bool $hasCalculationResult): void { $this->hasCalculationResult = $hasCalculationResult; } + + /** + * @return bool + */ + public function isAuctionSellAllowed(): bool + { + return $this->isAuctionSellAllowed; + } + + /** + * @param bool $isAuctionSellAllowed + */ + public function setIsAuctionSellAllowed(bool $isAuctionSellAllowed): void + { + $this->isAuctionSellAllowed = $isAuctionSellAllowed; + } } diff --git a/templates/calculator/_auction_sell_form.html.twig b/templates/calculator/_auction_sell_form.html.twig new file mode 100644 index 00000000..6d6d710d --- /dev/null +++ b/templates/calculator/_auction_sell_form.html.twig @@ -0,0 +1,6 @@ +
+ +
diff --git a/templates/calculator/inventory.html.twig b/templates/calculator/inventory.html.twig index 02156d76..cf72b04f 100644 --- a/templates/calculator/inventory.html.twig +++ b/templates/calculator/inventory.html.twig @@ -11,6 +11,7 @@
{{ include('calculator/_maximization_param_list.html.twig', {list: viewModel.maximizationParamList}) }} + {{ include('calculator/_auction_sell_form.html.twig', {allowed: viewModel.auctionSellAllowed}) }}
diff --git a/templates/calculator/mining.html.twig b/templates/calculator/mining.html.twig index 5a58dd04..af7e5096 100644 --- a/templates/calculator/mining.html.twig +++ b/templates/calculator/mining.html.twig @@ -11,6 +11,7 @@
{{ include('calculator/_maximization_param_list.html.twig', {list: viewModel.maximizationParamList}) }} + {{ include('calculator/_auction_sell_form.html.twig', {allowed: viewModel.auctionSellAllowed}) }}
diff --git a/templates/calculator/trade.html.twig b/templates/calculator/trade.html.twig index 608ce89f..263ad0df 100644 --- a/templates/calculator/trade.html.twig +++ b/templates/calculator/trade.html.twig @@ -11,6 +11,7 @@
{{ include('calculator/_maximization_param_list.html.twig', {list: viewModel.maximizationParamList}) }} + {{ include('calculator/_auction_sell_form.html.twig', {allowed: viewModel.auctionSellAllowed}) }}