From 69d89511beb733aaa04157bb1e8594ef1d79e622 Mon Sep 17 00:00:00 2001 From: Nabeel S Date: Sat, 10 Oct 2020 15:24:10 -0400 Subject: [PATCH] Inherited value can't be removed #811 (#863) Inherited value can't be removed #811 --- app/Services/FareService.php | 30 ++++++++++++--------------- tests/FinanceTest.php | 40 ++++++++++++++++++++++++++++++++++++ 2 files changed, 53 insertions(+), 17 deletions(-) diff --git a/app/Services/FareService.php b/app/Services/FareService.php index 34f28c54..944e8aa8 100644 --- a/app/Services/FareService.php +++ b/app/Services/FareService.php @@ -9,6 +9,7 @@ use App\Models\Pirep; use App\Models\PirepFare; use App\Models\Subfleet; use App\Support\Math; +use function count; use Illuminate\Support\Collection; class FareService extends Service @@ -66,27 +67,28 @@ class FareService extends Service */ protected function getFares($fare) { - if (filled($fare->pivot->price)) { + $pivot = $fare->pivot; + if (filled($pivot->price)) { if (strpos($fare->pivot->price, '%', -1) !== false) { - $fare->price = Math::addPercent($fare->price, $fare->pivot->price); + $fare->price = Math::addPercent($fare->price, $pivot->price); } else { $fare->price = $fare->pivot->price; } } - if (filled($fare->pivot->cost)) { - if (strpos($fare->pivot->cost, '%', -1) !== false) { - $fare->cost = Math::addPercent($fare->cost, $fare->pivot->cost); + if (filled($pivot->cost)) { + if (strpos($pivot->cost, '%', -1) !== false) { + $fare->cost = Math::addPercent($fare->cost, $pivot->cost); } else { $fare->cost = $fare->pivot->cost; } } - if (filled($fare->pivot->capacity)) { - if (strpos($fare->pivot->capacity, '%', -1) !== false) { - $fare->capacity = Math::addPercent($fare->capacity, $fare->pivot->capacity); + if (filled($pivot->capacity)) { + if (strpos($pivot->capacity, '%', -1) !== false) { + $fare->capacity = Math::addPercent($fare->capacity, $pivot->capacity); } else { - $fare->capacity = $fare->pivot->capacity; + $fare->capacity = $pivot->capacity; } } @@ -106,14 +108,8 @@ class FareService extends Service { $flight->fares()->syncWithoutDetaching([$fare->id]); - foreach ($override as $key => $item) { - if (!$item) { - unset($override[$key]); - } - } - // modify any pivot values? - if (\count($override) > 0) { + if (count($override) > 0) { $flight->fares()->updateExistingPivot($fare->id, $override); } @@ -169,7 +165,7 @@ class FareService extends Service $subfleet->fares()->syncWithoutDetaching([$fare->id]); // modify any pivot values? - if (\count($override) > 0) { + if (count($override) > 0) { $subfleet->fares()->updateExistingPivot($fare->id, $override); } diff --git a/tests/FinanceTest.php b/tests/FinanceTest.php index 890e4580..a969a504 100644 --- a/tests/FinanceTest.php +++ b/tests/FinanceTest.php @@ -195,6 +195,46 @@ class FinanceTest extends TestCase $this->assertCount(0, $this->fareSvc->getForFlight($flight)); } + public function testFlightFaresSetToNull() + { + /** @var Flight $flight */ + $flight = factory(Flight::class)->create(); + + /** @var Fare $fare */ + $fare = factory(Fare::class)->create(); + + $this->fareSvc->setForFlight($flight, $fare); + $subfleet_fares = $this->fareSvc->getForFlight($flight); + + $this->assertCount(1, $subfleet_fares); + $this->assertEquals($fare->price, $subfleet_fares->get(0)->price); + $this->assertEquals($fare->capacity, $subfleet_fares->get(0)->capacity); + + // + // set an override now + // + $this->fareSvc->setForFlight($flight, $fare, [ + 'price' => 50, 'capacity' => 400, + ]); + + // look for them again + $subfleet_fares = $this->fareSvc->getForFlight($flight); + + $this->assertCount(1, $subfleet_fares); + $this->assertEquals(50, $subfleet_fares[0]->price); + $this->assertEquals(400, $subfleet_fares[0]->capacity); + + // Set back to null + $this->fareSvc->setForFlight($flight, $fare, [ + 'price' => null, 'capacity' => null, + ]); + + // Get the original and check that it's being used + $subfleet_fares = $this->fareSvc->getForFlight($flight); + $this->assertEquals($fare->price, $subfleet_fares->get(0)->price); + $this->assertEquals($fare->capacity, $subfleet_fares->get(0)->capacity); + } + /** * Assign percentage values and make sure they're valid */