2018-01-07 23:19:46 +08:00
|
|
|
<?php
|
|
|
|
|
2018-02-24 05:12:09 +08:00
|
|
|
use App\Services\FareService;
|
|
|
|
use App\Support\Math;
|
|
|
|
|
2018-01-07 23:19:46 +08:00
|
|
|
class FinanceTest extends TestCase
|
|
|
|
{
|
|
|
|
protected $ac_svc,
|
2018-02-24 05:12:09 +08:00
|
|
|
$ICAO = 'B777',
|
|
|
|
$fareSvc;
|
2018-01-07 23:19:46 +08:00
|
|
|
|
|
|
|
public function setUp()
|
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
$this->addData('base');
|
2018-02-24 05:12:09 +08:00
|
|
|
$this->fareSvc = app(FareService::class);
|
2018-01-07 23:19:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testFlightFaresNoOverride()
|
|
|
|
{
|
|
|
|
$flight = factory(App\Models\Flight::class)->create();
|
|
|
|
$fare = factory(App\Models\Fare::class)->create();
|
|
|
|
|
2018-02-24 05:12:09 +08:00
|
|
|
$this->fareSvc->setForFlight($flight, $fare);
|
|
|
|
$subfleet_fares = $this->fareSvc->getForFlight($flight);
|
2018-01-07 23:19:46 +08:00
|
|
|
|
|
|
|
$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
|
|
|
|
#
|
2018-02-24 05:12:09 +08:00
|
|
|
$this->fareSvc->setForFlight($flight, $fare, [
|
2018-01-07 23:19:46 +08:00
|
|
|
'price' => 50, 'capacity' => 400
|
|
|
|
]);
|
|
|
|
|
|
|
|
# look for them again
|
2018-02-24 05:12:09 +08:00
|
|
|
$subfleet_fares = $this->fareSvc->getForFlight($flight);
|
2018-01-07 23:19:46 +08:00
|
|
|
|
|
|
|
$this->assertCount(1, $subfleet_fares);
|
|
|
|
$this->assertEquals(50, $subfleet_fares[0]->price);
|
|
|
|
$this->assertEquals(400, $subfleet_fares[0]->capacity);
|
|
|
|
|
|
|
|
# delete
|
2018-02-24 05:12:09 +08:00
|
|
|
$this->fareSvc->delFareFromFlight($flight, $fare);
|
|
|
|
$this->assertCount(0, $this->fareSvc->getForFlight($flight));
|
2018-01-07 23:19:46 +08:00
|
|
|
}
|
|
|
|
|
2018-02-24 05:12:09 +08:00
|
|
|
/**
|
|
|
|
* Assign percentage values and make sure they're valid
|
|
|
|
*/
|
|
|
|
public function testFlightFareOverrideAsPercent()
|
2018-01-07 23:19:46 +08:00
|
|
|
{
|
2018-02-24 05:12:09 +08:00
|
|
|
$flight = factory(App\Models\Flight::class)->create();
|
|
|
|
$fare = factory(App\Models\Fare::class)->create();
|
|
|
|
|
|
|
|
$percent_incr = '20%';
|
|
|
|
$percent_decr = '-20%';
|
|
|
|
$percent_200 = '200%';
|
|
|
|
|
|
|
|
$new_price = Math::addPercent($fare->price, $percent_incr);
|
|
|
|
$new_cost = Math::addPercent($fare->cost, $percent_decr);
|
|
|
|
$new_capacity = Math::addPercent($fare->capacity, $percent_200);
|
2018-01-07 23:19:46 +08:00
|
|
|
|
2018-02-24 05:12:09 +08:00
|
|
|
$this->fareSvc->setForFlight($flight, $fare, [
|
|
|
|
'price' => $percent_incr,
|
|
|
|
'cost' => $percent_decr,
|
|
|
|
'capacity' => $percent_200,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$ac_fares = $this->fareSvc->getForFlight($flight);
|
|
|
|
|
|
|
|
$this->assertCount(1, $ac_fares);
|
|
|
|
$this->assertEquals($new_price, $ac_fares[0]->price);
|
|
|
|
$this->assertEquals($new_cost, $ac_fares[0]->cost);
|
|
|
|
$this->assertEquals($new_capacity, $ac_fares[0]->capacity);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSubfleetFaresNoOverride()
|
|
|
|
{
|
2018-01-07 23:19:46 +08:00
|
|
|
$subfleet = factory(App\Models\Subfleet::class)->create();
|
|
|
|
$fare = factory(App\Models\Fare::class)->create();
|
|
|
|
|
2018-02-24 05:12:09 +08:00
|
|
|
$this->fareSvc->setForSubfleet($subfleet, $fare);
|
|
|
|
$subfleet_fares = $this->fareSvc->getForSubfleet($subfleet);
|
2018-01-07 23:19:46 +08:00
|
|
|
|
|
|
|
$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
|
|
|
|
#
|
2018-02-24 05:12:09 +08:00
|
|
|
$this->fareSvc->setForSubfleet($subfleet, $fare, [
|
2018-01-07 23:19:46 +08:00
|
|
|
'price' => 50, 'capacity' => 400
|
|
|
|
]);
|
|
|
|
|
|
|
|
# look for them again
|
2018-02-24 05:12:09 +08:00
|
|
|
$subfleet_fares = $this->fareSvc->getForSubfleet($subfleet);
|
2018-01-07 23:19:46 +08:00
|
|
|
|
|
|
|
$this->assertCount(1, $subfleet_fares);
|
|
|
|
$this->assertEquals(50, $subfleet_fares[0]->price);
|
|
|
|
$this->assertEquals(400, $subfleet_fares[0]->capacity);
|
|
|
|
|
|
|
|
# delete
|
2018-02-24 05:12:09 +08:00
|
|
|
$this->fareSvc->delFareFromSubfleet($subfleet, $fare);
|
|
|
|
$this->assertCount(0, $this->fareSvc->getForSubfleet($subfleet));
|
2018-01-07 23:19:46 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testSubfleetFaresOverride()
|
|
|
|
{
|
|
|
|
$subfleet = factory(App\Models\Subfleet::class)->create();
|
|
|
|
$fare = factory(App\Models\Fare::class)->create();
|
|
|
|
|
2018-02-24 05:12:09 +08:00
|
|
|
$this->fareSvc->setForSubfleet($subfleet, $fare, [
|
2018-01-07 23:19:46 +08:00
|
|
|
'price' => 50, 'capacity' => 400
|
|
|
|
]);
|
|
|
|
|
2018-02-24 05:12:09 +08:00
|
|
|
$ac_fares = $this->fareSvc->getForSubfleet($subfleet);
|
2018-01-07 23:19:46 +08:00
|
|
|
|
|
|
|
$this->assertCount(1, $ac_fares);
|
|
|
|
$this->assertEquals(50, $ac_fares[0]->price);
|
|
|
|
$this->assertEquals(400, $ac_fares[0]->capacity);
|
|
|
|
|
|
|
|
#
|
|
|
|
# update the override to a different amount and make sure it updates
|
|
|
|
#
|
|
|
|
|
2018-02-24 05:12:09 +08:00
|
|
|
$this->fareSvc->setForSubfleet($subfleet, $fare, [
|
2018-01-07 23:19:46 +08:00
|
|
|
'price' => 150, 'capacity' => 50
|
|
|
|
]);
|
|
|
|
|
2018-02-24 05:12:09 +08:00
|
|
|
$ac_fares = $this->fareSvc->getForSubfleet($subfleet);
|
2018-01-07 23:19:46 +08:00
|
|
|
|
|
|
|
$this->assertCount(1, $ac_fares);
|
|
|
|
$this->assertEquals(150, $ac_fares[0]->price);
|
|
|
|
$this->assertEquals(50, $ac_fares[0]->capacity);
|
|
|
|
|
|
|
|
# delete
|
2018-02-24 05:12:09 +08:00
|
|
|
$this->fareSvc->delFareFromSubfleet($subfleet, $fare);
|
|
|
|
$this->assertCount(0, $this->fareSvc->getForSubfleet($subfleet));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Assign percentage values and make sure they're valid
|
|
|
|
*/
|
|
|
|
public function testSubfleetFareOverrideAsPercent()
|
|
|
|
{
|
|
|
|
$subfleet = factory(App\Models\Subfleet::class)->create();
|
|
|
|
$fare = factory(App\Models\Fare::class)->create();
|
|
|
|
|
|
|
|
$percent_incr = '20%';
|
|
|
|
$percent_decr = '-20%';
|
|
|
|
$percent_200 = '200%';
|
|
|
|
|
|
|
|
$new_price = Math::addPercent($fare->price, $percent_incr);
|
|
|
|
$new_cost = Math::addPercent($fare->cost, $percent_decr);
|
|
|
|
$new_capacity = Math::addPercent($fare->capacity, $percent_200);
|
|
|
|
|
|
|
|
$this->fareSvc->setForSubfleet($subfleet, $fare, [
|
|
|
|
'price' => $percent_incr,
|
|
|
|
'cost' => $percent_decr,
|
|
|
|
'capacity' => $percent_200,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$ac_fares = $this->fareSvc->getForSubfleet($subfleet);
|
|
|
|
|
|
|
|
$this->assertCount(1, $ac_fares);
|
|
|
|
$this->assertEquals($new_price, $ac_fares[0]->price);
|
|
|
|
$this->assertEquals($new_cost, $ac_fares[0]->cost);
|
|
|
|
$this->assertEquals($new_capacity, $ac_fares[0]->capacity);
|
2018-01-07 23:19:46 +08:00
|
|
|
}
|
2018-02-25 05:38:25 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Test getting the fares from the flight svc. Have a few base fares
|
|
|
|
* and then override some of them
|
|
|
|
*/
|
|
|
|
public function testGetFaresWithOverrides()
|
|
|
|
{
|
|
|
|
$flight = factory(App\Models\Flight::class)->create();
|
|
|
|
$subfleet = factory(App\Models\Subfleet::class)->create();
|
|
|
|
[$fare1, $fare2, $fare3, $fare4] = factory(App\Models\Fare::class, 4)->create();
|
|
|
|
|
|
|
|
# add to the subfleet, and just override one of them
|
|
|
|
$this->fareSvc->setForSubfleet($subfleet, $fare1);
|
|
|
|
$this->fareSvc->setForSubfleet($subfleet, $fare2, [
|
|
|
|
'price' => 100,
|
|
|
|
'cost' => 50,
|
|
|
|
'capacity' => 25,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->fareSvc->setForSubfleet($subfleet, $fare3);
|
|
|
|
|
|
|
|
# Now set the last one to the flight and then override stuff
|
|
|
|
$this->fareSvc->setForFlight($flight, $fare3, [
|
|
|
|
'price' => '300%',
|
|
|
|
'cost' => 250,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$fare3_price = Math::addPercent($fare3->price, 300);
|
|
|
|
|
|
|
|
# Assign another one to the flight, that's not on the subfleet
|
|
|
|
# This one should NOT be returned in the list of fares
|
|
|
|
$this->fareSvc->setForFlight($flight, $fare4);
|
|
|
|
|
|
|
|
$fares = $this->fareSvc->getAllFares($flight, $subfleet);
|
|
|
|
$this->assertCount(3, $fares);
|
|
|
|
|
|
|
|
foreach($fares as $fare) {
|
|
|
|
switch($fare->id) {
|
|
|
|
case $fare1->id:
|
|
|
|
$this->assertEquals($fare->price, $fare1->price);
|
|
|
|
$this->assertEquals($fare->cost, $fare1->cost);
|
|
|
|
$this->assertEquals($fare->capacity, $fare1->capacity);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case $fare2->id:
|
|
|
|
$this->assertEquals($fare->price, 100);
|
|
|
|
$this->assertEquals($fare->cost, 50);
|
|
|
|
$this->assertEquals($fare->capacity, 25);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case $fare3->id:
|
|
|
|
$this->assertEquals($fare->price, $fare3_price);
|
|
|
|
$this->assertEquals($fare->cost, 250);
|
|
|
|
$this->assertEquals($fare->capacity, $fare3->capacity);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetFaresNoFlightOverrides()
|
|
|
|
{
|
|
|
|
$subfleet = factory(App\Models\Subfleet::class)->create();
|
|
|
|
[$fare1, $fare2, $fare3] = factory(App\Models\Fare::class, 3)->create();
|
|
|
|
|
|
|
|
# add to the subfleet, and just override one of them
|
|
|
|
$this->fareSvc->setForSubfleet($subfleet, $fare1);
|
|
|
|
$this->fareSvc->setForSubfleet($subfleet, $fare2, [
|
|
|
|
'price' => 100,
|
|
|
|
'cost' => 50,
|
|
|
|
'capacity' => 25,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->fareSvc->setForSubfleet($subfleet, $fare3);
|
|
|
|
|
|
|
|
$fares = $this->fareSvc->getAllFares(null, $subfleet);
|
|
|
|
$this->assertCount(3, $fares);
|
|
|
|
|
|
|
|
foreach ($fares as $fare) {
|
|
|
|
switch ($fare->id) {
|
|
|
|
case $fare1->id:
|
|
|
|
$this->assertEquals($fare->price, $fare1->price);
|
|
|
|
$this->assertEquals($fare->cost, $fare1->cost);
|
|
|
|
$this->assertEquals($fare->capacity, $fare1->capacity);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case $fare2->id:
|
|
|
|
$this->assertEquals($fare->price, 100);
|
|
|
|
$this->assertEquals($fare->cost, 50);
|
|
|
|
$this->assertEquals($fare->capacity, 25);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case $fare3->id:
|
|
|
|
$this->assertEquals($fare->price, $fare3->price);
|
|
|
|
$this->assertEquals($fare->cost, $fare3->cost);
|
|
|
|
$this->assertEquals($fare->capacity, $fare3->capacity);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-07 23:19:46 +08:00
|
|
|
}
|