Inherited value can't be removed #811 (#863)

Inherited value can't be removed #811
This commit is contained in:
Nabeel S 2020-10-10 15:24:10 -04:00 committed by GitHub
parent 48087fb05f
commit 69d89511be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 53 additions and 17 deletions

View File

@ -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);
}

View File

@ -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
*/