diff --git a/app/Models/PluginBundle.php b/app/Models/PluginBundle.php index 1426f872..90724e98 100644 --- a/app/Models/PluginBundle.php +++ b/app/Models/PluginBundle.php @@ -188,11 +188,11 @@ protected function formattedPrice(): Attribute /** * Calculate the discount percentage for a given price amount. */ - public function getDiscountPercentFor(int $priceAmount): int + public function getDiscountPercentFor(?int $priceAmount): int { $retailValue = $this->retail_value; - if ($retailValue <= 0) { + if ($priceAmount === null || $retailValue <= 0) { return 0; } @@ -226,8 +226,12 @@ protected function discountPercent(): Attribute /** * Get the savings amount in cents for a given price amount. */ - public function getSavingsFor(int $priceAmount): int + public function getSavingsFor(?int $priceAmount): int { + if ($priceAmount === null) { + return 0; + } + return max(0, $this->retail_value - $priceAmount); } diff --git a/tests/Feature/PluginBundleWithoutPriceTest.php b/tests/Feature/PluginBundleWithoutPriceTest.php new file mode 100644 index 00000000..6e6fc3eb --- /dev/null +++ b/tests/Feature/PluginBundleWithoutPriceTest.php @@ -0,0 +1,23 @@ +create(['price' => null]); + + $this->assertSame(0, $bundle->discount_percent); + $this->assertSame(0, $bundle->savings); + $this->assertSame(0, $bundle->getDiscountPercentForUser(null)); + $this->assertSame(0, $bundle->getSavingsForUser(User::factory()->create())); + } +}