2018-01-07 23:19:46 +08:00
|
|
|
<?php
|
|
|
|
|
2020-05-23 23:43:29 +08:00
|
|
|
namespace Tests;
|
|
|
|
|
|
|
|
use App\Models\Airline;
|
|
|
|
use App\Models\Airport;
|
2018-03-03 07:48:36 +08:00
|
|
|
use App\Models\Enums\ExpenseType;
|
2020-02-12 23:40:52 +08:00
|
|
|
use App\Models\Enums\FlightType;
|
2020-05-23 23:43:29 +08:00
|
|
|
use App\Models\Enums\PirepSource;
|
2020-02-12 23:40:52 +08:00
|
|
|
use App\Models\Expense;
|
2020-05-23 23:43:29 +08:00
|
|
|
use App\Models\Fare;
|
|
|
|
use App\Models\Flight;
|
|
|
|
use App\Models\Journal;
|
|
|
|
use App\Models\Pirep;
|
2020-02-12 23:40:52 +08:00
|
|
|
use App\Models\Subfleet;
|
2020-05-23 23:43:29 +08:00
|
|
|
use App\Models\User;
|
2018-03-03 07:48:36 +08:00
|
|
|
use App\Repositories\ExpenseRepository;
|
2018-03-01 11:52:36 +08:00
|
|
|
use App\Repositories\JournalRepository;
|
2020-10-25 03:11:08 +08:00
|
|
|
use App\Services\BidService;
|
2018-02-24 05:12:09 +08:00
|
|
|
use App\Services\FareService;
|
2018-03-06 12:49:42 +08:00
|
|
|
use App\Services\Finance\PirepFinanceService;
|
2018-02-28 06:16:40 +08:00
|
|
|
use App\Services\FleetService;
|
2018-08-27 00:40:04 +08:00
|
|
|
use App\Services\PirepService;
|
2018-02-24 05:12:09 +08:00
|
|
|
use App\Support\Math;
|
2018-03-01 11:52:36 +08:00
|
|
|
use App\Support\Money;
|
2020-05-23 23:43:29 +08:00
|
|
|
use Exception;
|
2018-02-24 05:12:09 +08:00
|
|
|
|
2018-01-07 23:19:46 +08:00
|
|
|
class FinanceTest extends TestCase
|
|
|
|
{
|
2020-05-27 06:41:25 +08:00
|
|
|
/** @var \App\Repositories\ExpenseRepository */
|
2018-08-27 00:40:04 +08:00
|
|
|
private $expenseRepo;
|
2020-05-27 06:41:25 +08:00
|
|
|
|
|
|
|
/** @var \App\Services\FareService */
|
2018-08-27 00:40:04 +08:00
|
|
|
private $fareSvc;
|
2020-05-27 06:41:25 +08:00
|
|
|
|
|
|
|
/** @var \App\Services\FinanceService */
|
2018-08-27 00:40:04 +08:00
|
|
|
private $financeSvc;
|
2020-05-27 06:41:25 +08:00
|
|
|
|
|
|
|
/** @var FleetService */
|
2018-08-27 00:40:04 +08:00
|
|
|
private $fleetSvc;
|
2020-05-27 06:41:25 +08:00
|
|
|
|
|
|
|
/** @var PirepService */
|
2018-08-27 00:40:04 +08:00
|
|
|
private $pirepSvc;
|
2018-01-07 23:19:46 +08:00
|
|
|
|
2018-03-02 06:20:13 +08:00
|
|
|
/**
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
2019-05-12 23:50:38 +08:00
|
|
|
public function setUp(): void
|
2018-01-07 23:19:46 +08:00
|
|
|
{
|
|
|
|
parent::setUp();
|
|
|
|
$this->addData('base');
|
2019-08-05 20:27:53 +08:00
|
|
|
$this->addData('fleet');
|
2018-02-28 06:16:40 +08:00
|
|
|
|
2018-03-03 07:48:36 +08:00
|
|
|
$this->expenseRepo = app(ExpenseRepository::class);
|
2018-02-24 05:12:09 +08:00
|
|
|
$this->fareSvc = app(FareService::class);
|
2018-03-06 12:49:42 +08:00
|
|
|
$this->financeSvc = app(PirepFinanceService::class);
|
2018-02-28 06:16:40 +08:00
|
|
|
$this->fleetSvc = app(FleetService::class);
|
2018-03-07 07:36:06 +08:00
|
|
|
$this->pirepSvc = app(PirepService::class);
|
2018-03-02 06:20:13 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Create a user and a PIREP, that has all of the data filled out
|
|
|
|
* so that we can test all of the disparate parts of the finances
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
2018-03-02 06:20:13 +08:00
|
|
|
* @throws Exception
|
2018-08-27 00:40:04 +08:00
|
|
|
*
|
|
|
|
* @return array
|
2018-03-02 06:20:13 +08:00
|
|
|
*/
|
|
|
|
public function createFullPirep()
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Setup tests
|
|
|
|
*/
|
|
|
|
$subfleet = $this->createSubfleetWithAircraft(2);
|
2018-03-30 03:55:25 +08:00
|
|
|
$subfleet['subfleet']->cost_block_hour = 10;
|
|
|
|
$subfleet['subfleet']->save();
|
|
|
|
|
2018-03-02 06:20:13 +08:00
|
|
|
$rank = $this->createRank(10, [$subfleet['subfleet']->id]);
|
2018-03-02 09:15:55 +08:00
|
|
|
$rank->acars_base_pay_rate = 10;
|
|
|
|
$rank->save();
|
|
|
|
|
2018-03-02 06:20:13 +08:00
|
|
|
$this->fleetSvc->addSubfleetToRank($subfleet['subfleet'], $rank);
|
|
|
|
|
2020-05-23 23:43:29 +08:00
|
|
|
$dpt_apt = factory(Airport::class)->create([
|
2018-08-27 00:40:04 +08:00
|
|
|
'ground_handling_cost' => 10,
|
2019-07-23 20:41:20 +08:00
|
|
|
'fuel_jeta_cost' => 10,
|
|
|
|
]);
|
|
|
|
|
2020-05-23 23:43:29 +08:00
|
|
|
$arr_apt = factory(Airport::class)->create([
|
2019-07-23 20:41:20 +08:00
|
|
|
'ground_handling_cost' => 10,
|
|
|
|
'fuel_jeta_cost' => 10,
|
2018-03-02 09:15:55 +08:00
|
|
|
]);
|
|
|
|
|
2020-05-23 23:43:29 +08:00
|
|
|
/** @var \App\Models\User $user */
|
|
|
|
$user = factory(User::class)->create([
|
2018-03-02 06:20:13 +08:00
|
|
|
'rank_id' => $rank->id,
|
|
|
|
]);
|
|
|
|
|
2020-05-23 23:43:29 +08:00
|
|
|
/** @var \App\Models\Flight $flight */
|
|
|
|
$flight = factory(Flight::class)->create([
|
2018-08-27 00:40:04 +08:00
|
|
|
'airline_id' => $user->airline_id,
|
2019-07-23 20:41:20 +08:00
|
|
|
'dpt_airport_id' => $dpt_apt->icao,
|
|
|
|
'arr_airport_id' => $arr_apt->icao,
|
2018-03-02 06:20:13 +08:00
|
|
|
]);
|
|
|
|
|
2020-05-23 23:43:29 +08:00
|
|
|
$pirep = factory(Pirep::class)->create([
|
2018-08-27 00:40:04 +08:00
|
|
|
'flight_number' => $flight->flight_number,
|
2020-02-12 23:40:52 +08:00
|
|
|
'flight_type' => FlightType::SCHED_PAX,
|
2018-08-27 00:40:04 +08:00
|
|
|
'route_code' => $flight->route_code,
|
|
|
|
'route_leg' => $flight->route_leg,
|
2019-07-23 20:41:20 +08:00
|
|
|
'dpt_airport_id' => $dpt_apt->id,
|
|
|
|
'arr_airport_id' => $arr_apt->id,
|
2018-08-27 00:40:04 +08:00
|
|
|
'user_id' => $user->id,
|
|
|
|
'airline_id' => $user->airline_id,
|
|
|
|
'aircraft_id' => $subfleet['aircraft']->random(),
|
2020-03-07 00:36:02 +08:00
|
|
|
'flight_id' => $flight->id,
|
2018-08-27 00:40:04 +08:00
|
|
|
'source' => PirepSource::ACARS,
|
|
|
|
'flight_time' => 120,
|
2019-07-23 20:41:20 +08:00
|
|
|
'block_fuel' => 10,
|
|
|
|
'fuel_used' => 9,
|
2018-03-02 06:20:13 +08:00
|
|
|
]);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add fares to the subfleet, and then add the fares
|
|
|
|
* to the PIREP when it's saved, and set the capacity
|
|
|
|
*/
|
2020-05-23 23:43:29 +08:00
|
|
|
$fares = factory(Fare::class, 3)->create([
|
2018-08-27 00:40:04 +08:00
|
|
|
'price' => 100,
|
|
|
|
'cost' => 50,
|
2018-03-02 09:15:55 +08:00
|
|
|
'capacity' => 10,
|
|
|
|
]);
|
|
|
|
|
2018-03-02 06:20:13 +08:00
|
|
|
foreach ($fares as $fare) {
|
|
|
|
$this->fareSvc->setForSubfleet($subfleet['subfleet'], $fare);
|
|
|
|
}
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// Add an expense
|
2020-05-23 23:43:29 +08:00
|
|
|
factory(Expense::class)->create([
|
2018-03-06 02:21:38 +08:00
|
|
|
'airline_id' => null,
|
2018-08-27 00:40:04 +08:00
|
|
|
'amount' => 100,
|
2018-03-06 02:21:38 +08:00
|
|
|
]);
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// Add a subfleet expense
|
2020-05-23 23:43:29 +08:00
|
|
|
factory(Expense::class)->create([
|
2020-02-12 23:40:52 +08:00
|
|
|
'ref_model' => Subfleet::class,
|
2018-04-02 03:32:01 +08:00
|
|
|
'ref_model_id' => $subfleet['subfleet']->id,
|
2018-08-27 00:40:04 +08:00
|
|
|
'amount' => 200,
|
2018-03-06 02:21:38 +08:00
|
|
|
]);
|
|
|
|
|
2020-05-27 06:41:25 +08:00
|
|
|
// Add expenses for airports
|
|
|
|
factory(Expense::class)->create([
|
|
|
|
'ref_model' => Airport::class,
|
|
|
|
'ref_model_id' => $dpt_apt->id,
|
|
|
|
'amount' => 50,
|
|
|
|
]);
|
|
|
|
|
|
|
|
factory(Expense::class)->create([
|
|
|
|
'ref_model' => Airport::class,
|
|
|
|
'ref_model_id' => $arr_apt->id,
|
|
|
|
'amount' => 100,
|
|
|
|
]);
|
|
|
|
|
2018-03-02 06:20:13 +08:00
|
|
|
$pirep = $this->pirepSvc->create($pirep, []);
|
|
|
|
|
|
|
|
return [$user, $pirep, $fares];
|
2018-01-07 23:19:46 +08:00
|
|
|
}
|
|
|
|
|
2020-10-28 06:46:15 +08:00
|
|
|
/*public function testFlightFaresNoOverride()
|
2018-01-07 23:19:46 +08:00
|
|
|
{
|
2020-05-23 23:43:29 +08:00
|
|
|
$flight = factory(Flight::class)->create();
|
|
|
|
$fare = factory(Fare::class)->create();
|
2018-01-07 23:19:46 +08:00
|
|
|
|
2018-02-24 05:12:09 +08:00
|
|
|
$this->fareSvc->setForFlight($flight, $fare);
|
2020-10-28 06:46:15 +08:00
|
|
|
$subfleet_fares = $this->fareSvc->get($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);
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
//
|
|
|
|
// set an override now
|
|
|
|
//
|
2018-02-24 05:12:09 +08:00
|
|
|
$this->fareSvc->setForFlight($flight, $fare, [
|
2018-08-27 00:40:04 +08:00
|
|
|
'price' => 50, 'capacity' => 400,
|
2018-01-07 23:19:46 +08:00
|
|
|
]);
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// 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);
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// delete
|
2018-02-24 05:12:09 +08:00
|
|
|
$this->fareSvc->delFareFromFlight($flight, $fare);
|
|
|
|
$this->assertCount(0, $this->fareSvc->getForFlight($flight));
|
2020-10-28 06:46:15 +08:00
|
|
|
}*/
|
2020-10-11 03:24:10 +08:00
|
|
|
|
2020-10-25 03:11:08 +08:00
|
|
|
/**
|
|
|
|
* Make sure that the API is returning the fares properly for a subfleet on a flight
|
|
|
|
* https://github.com/nabeelio/phpvms/issues/899
|
2020-10-28 06:46:15 +08:00
|
|
|
*
|
|
|
|
* The fares, etc for a subfleet has to be adjusted to the fleet
|
|
|
|
* https://github.com/nabeelio/phpvms/issues/905
|
2020-10-25 03:11:08 +08:00
|
|
|
*/
|
|
|
|
public function testFlightFaresOverAPI()
|
|
|
|
{
|
|
|
|
$this->updateSetting('pireps.only_aircraft_at_dpt_airport', false);
|
|
|
|
$this->updateSetting('pireps.restrict_aircraft_to_rank', false);
|
|
|
|
|
|
|
|
$this->user = factory(User::class)->create();
|
|
|
|
|
|
|
|
/** @var Flight $flight */
|
|
|
|
$flight = factory(Flight::class)->create();
|
|
|
|
|
|
|
|
/** @var Subfleet $subfleet */
|
|
|
|
$subfleet = factory(Subfleet::class)->create();
|
|
|
|
$this->fleetSvc->addSubfleetToFlight($subfleet, $flight);
|
|
|
|
|
2020-10-28 06:46:15 +08:00
|
|
|
/**
|
|
|
|
* Set a base fare
|
|
|
|
* Then override on multiple layers - subfleet modifies the cost, the flight modifies
|
|
|
|
* the price. This should then all be reflected as we go down the chain. This is
|
|
|
|
* mostly for the output side
|
|
|
|
*/
|
2020-10-25 03:11:08 +08:00
|
|
|
/** @var Fare $fare */
|
2020-10-28 06:46:15 +08:00
|
|
|
$fare = factory(Fare::class)->create([
|
|
|
|
'price' => 10,
|
|
|
|
'cost' => 20,
|
|
|
|
'capacity' => 100,
|
|
|
|
]);
|
2020-10-25 03:11:08 +08:00
|
|
|
|
2020-10-28 06:46:15 +08:00
|
|
|
$this->fareSvc->setForSubfleet($subfleet, $fare, [
|
|
|
|
'capacity' => 200,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->fareSvc->setForFlight($flight, $fare, [
|
|
|
|
'price' => 50,
|
|
|
|
]);
|
2020-10-25 03:11:08 +08:00
|
|
|
|
2020-10-28 06:46:15 +08:00
|
|
|
$flight = $this->fareSvc->getReconciledFaresForFlight($flight);
|
|
|
|
|
|
|
|
$this->assertEquals(50, $flight->subfleets[0]->fares[0]->price);
|
|
|
|
$this->assertEquals(200, $flight->subfleets[0]->fares[0]->capacity);
|
|
|
|
$this->assertEquals(20, $flight->subfleets[0]->fares[0]->cost);
|
2020-10-25 03:11:08 +08:00
|
|
|
|
|
|
|
//
|
|
|
|
// set an override now (but on the flight)
|
|
|
|
//
|
|
|
|
|
|
|
|
$req = $this->get('/api/flights/'.$flight->id);
|
|
|
|
$req->assertStatus(200);
|
|
|
|
|
|
|
|
$body = $req->json()['data'];
|
|
|
|
$this->assertEquals($flight->id, $body['id']);
|
2020-10-28 06:46:15 +08:00
|
|
|
|
|
|
|
// Fares, etc, should be adjusted, per-subfleet
|
2020-10-25 03:11:08 +08:00
|
|
|
$this->assertCount(1, $body['subfleets']);
|
2020-10-28 06:46:15 +08:00
|
|
|
$this->assertEquals(50, $body['subfleets'][0]['fares'][0]['price']);
|
|
|
|
$this->assertEquals(200, $body['subfleets'][0]['fares'][0]['capacity']);
|
|
|
|
$this->assertEquals(20, $body['subfleets'][0]['fares'][0]['cost']);
|
|
|
|
|
|
|
|
$req = $this->get('/api/flights/search?flight_id='.$flight->id);
|
|
|
|
$req->assertStatus(200);
|
|
|
|
|
|
|
|
$body = $req->json()['data'][0];
|
|
|
|
$this->assertEquals($flight->id, $body['id']);
|
|
|
|
|
|
|
|
// Fares, etc, should be adjusted, per-subfleet
|
|
|
|
$this->assertCount(1, $body['subfleets']);
|
|
|
|
$this->assertEquals(50, $body['subfleets'][0]['fares'][0]['price']);
|
|
|
|
$this->assertEquals(200, $body['subfleets'][0]['fares'][0]['capacity']);
|
|
|
|
$this->assertEquals(20, $body['subfleets'][0]['fares'][0]['cost']);
|
2020-10-25 03:11:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testFlightFaresOverAPIOnUserBids()
|
|
|
|
{
|
|
|
|
$this->updateSetting('pireps.only_aircraft_at_dpt_airport', false);
|
|
|
|
$this->updateSetting('pireps.restrict_aircraft_to_rank', false);
|
|
|
|
|
|
|
|
/** @var BidService $bidSvc */
|
|
|
|
$bidSvc = app(BidService::class);
|
|
|
|
|
|
|
|
$this->user = factory(User::class)->create();
|
|
|
|
|
|
|
|
/** @var Flight $flight */
|
|
|
|
$flight = factory(Flight::class)->create();
|
|
|
|
|
|
|
|
/** @var Subfleet $subfleet */
|
|
|
|
$subfleet = factory(Subfleet::class)->create();
|
|
|
|
$this->fleetSvc->addSubfleetToFlight($subfleet, $flight);
|
|
|
|
|
|
|
|
/** @var Fare $fare */
|
|
|
|
$fare = factory(Fare::class)->create();
|
|
|
|
|
|
|
|
$this->fareSvc->setForFlight($flight, $fare);
|
|
|
|
|
|
|
|
//
|
|
|
|
// set an override now (but on the flight)
|
|
|
|
//
|
|
|
|
$this->fareSvc->setForFlight($flight, $fare, ['price' => 50]);
|
|
|
|
$bid = $bidSvc->addBid($flight, $this->user);
|
|
|
|
|
|
|
|
$req = $this->get('/api/user/bids');
|
|
|
|
$req->assertStatus(200);
|
|
|
|
|
|
|
|
$body = $req->json()['data'];
|
|
|
|
$this->assertEquals($flight->id, $body[0]['flight_id']);
|
|
|
|
$this->assertCount(1, $body[0]['flight']['subfleets']);
|
|
|
|
$this->assertEquals(50, $body[0]['flight']['fares'][0]['price']);
|
|
|
|
$this->assertEquals($fare->capacity, $body[0]['flight']['fares'][0]['capacity']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSubfleetFaresOverAPI()
|
|
|
|
{
|
|
|
|
$this->updateSetting('pireps.only_aircraft_at_dpt_airport', false);
|
|
|
|
$this->updateSetting('pireps.restrict_aircraft_to_rank', false);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Add a user and flights
|
|
|
|
*/
|
|
|
|
$this->user = factory(User::class)->create();
|
|
|
|
$flight = $this->addFlight($this->user);
|
|
|
|
|
|
|
|
/** @var FareService $fare_svc */
|
|
|
|
$fare_svc = app(FareService::class);
|
|
|
|
|
|
|
|
/** @var \App\Models\Fare $fare */
|
|
|
|
$fare = factory(Fare::class)->create();
|
|
|
|
$fare_svc->setForSubfleet($flight->subfleets[0], $fare, ['price' => 50]);
|
|
|
|
|
|
|
|
// Get from API
|
|
|
|
$req = $this->get('/api/flights/'.$flight->id);
|
|
|
|
$req->assertStatus(200);
|
|
|
|
|
|
|
|
$body = $req->json()['data'];
|
|
|
|
$this->assertEquals($flight->id, $body['id']);
|
|
|
|
$this->assertCount(1, $body['subfleets']);
|
|
|
|
$this->assertEquals(50, $body['subfleets'][0]['fares'][0]['price']);
|
|
|
|
$this->assertEquals($fare->capacity, $body['subfleets'][0]['fares'][0]['capacity']);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2020-10-25 03:11:08 +08:00
|
|
|
/** @var Flight $flight */
|
2020-05-23 23:43:29 +08:00
|
|
|
$flight = factory(Flight::class)->create();
|
2020-10-25 03:11:08 +08:00
|
|
|
|
|
|
|
/** @var \App\Models\Fare $fare */
|
2020-05-23 23:43:29 +08:00
|
|
|
$fare = factory(Fare::class)->create();
|
2018-02-24 05:12:09 +08:00
|
|
|
|
2020-10-28 06:46:15 +08:00
|
|
|
// Subfleet needs to be attached to a flight
|
|
|
|
$subfleet = factory(Subfleet::class)->create();
|
|
|
|
$this->fleetSvc->addSubfleetToFlight($subfleet, $flight);
|
|
|
|
|
2018-02-24 05:12:09 +08:00
|
|
|
$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, [
|
2018-08-27 00:40:04 +08:00
|
|
|
'price' => $percent_incr,
|
|
|
|
'cost' => $percent_decr,
|
2018-02-24 05:12:09 +08:00
|
|
|
'capacity' => $percent_200,
|
|
|
|
]);
|
|
|
|
|
2020-10-28 06:46:15 +08:00
|
|
|
// A subfleet is required to be passed in
|
|
|
|
$ac_fares = $this->fareSvc->getAllFares($flight, $subfleet);
|
2018-02-24 05:12:09 +08:00
|
|
|
|
|
|
|
$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()
|
|
|
|
{
|
2020-05-23 23:43:29 +08:00
|
|
|
$subfleet = factory(Subfleet::class)->create();
|
|
|
|
$fare = factory(Fare::class)->create();
|
2018-01-07 23:19:46 +08:00
|
|
|
|
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);
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
//
|
|
|
|
// set an override now
|
|
|
|
//
|
2018-02-24 05:12:09 +08:00
|
|
|
$this->fareSvc->setForSubfleet($subfleet, $fare, [
|
2018-08-27 00:40:04 +08:00
|
|
|
'price' => 50, 'capacity' => 400,
|
2018-01-07 23:19:46 +08:00
|
|
|
]);
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// 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);
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// 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()
|
|
|
|
{
|
2020-05-23 23:43:29 +08:00
|
|
|
$subfleet = factory(Subfleet::class)->create();
|
|
|
|
$fare = factory(Fare::class)->create();
|
2018-01-07 23:19:46 +08:00
|
|
|
|
2018-02-24 05:12:09 +08:00
|
|
|
$this->fareSvc->setForSubfleet($subfleet, $fare, [
|
2018-08-27 00:40:04 +08:00
|
|
|
'price' => 50, 'capacity' => 400,
|
2018-01-07 23:19:46 +08:00
|
|
|
]);
|
|
|
|
|
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);
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
//
|
|
|
|
// update the override to a different amount and make sure it updates
|
|
|
|
//
|
2018-01-07 23:19:46 +08:00
|
|
|
|
2018-02-24 05:12:09 +08:00
|
|
|
$this->fareSvc->setForSubfleet($subfleet, $fare, [
|
2018-08-27 00:40:04 +08:00
|
|
|
'price' => 150, 'capacity' => 50,
|
2018-01-07 23:19:46 +08:00
|
|
|
]);
|
|
|
|
|
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);
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// 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()
|
|
|
|
{
|
2020-05-23 23:43:29 +08:00
|
|
|
$subfleet = factory(Subfleet::class)->create();
|
|
|
|
$fare = factory(Fare::class)->create();
|
2018-02-24 05:12:09 +08:00
|
|
|
|
|
|
|
$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, [
|
2018-08-27 00:40:04 +08:00
|
|
|
'price' => $percent_incr,
|
|
|
|
'cost' => $percent_decr,
|
2018-02-24 05:12:09 +08:00
|
|
|
'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()
|
|
|
|
{
|
2020-05-23 23:43:29 +08:00
|
|
|
$flight = factory(Flight::class)->create();
|
|
|
|
$subfleet = factory(Subfleet::class)->create();
|
|
|
|
[$fare1, $fare2, $fare3, $fare4] = factory(Fare::class, 4)->create();
|
2018-02-25 05:38:25 +08:00
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// add to the subfleet, and just override one of them
|
2018-02-25 05:38:25 +08:00
|
|
|
$this->fareSvc->setForSubfleet($subfleet, $fare1);
|
|
|
|
$this->fareSvc->setForSubfleet($subfleet, $fare2, [
|
2018-08-27 00:40:04 +08:00
|
|
|
'price' => 100,
|
|
|
|
'cost' => 50,
|
2018-02-25 05:38:25 +08:00
|
|
|
'capacity' => 25,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->fareSvc->setForSubfleet($subfleet, $fare3);
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// Now set the last one to the flight and then override stuff
|
2018-02-25 05:38:25 +08:00
|
|
|
$this->fareSvc->setForFlight($flight, $fare3, [
|
|
|
|
'price' => '300%',
|
2018-08-27 00:40:04 +08:00
|
|
|
'cost' => 250,
|
2018-02-25 05:38:25 +08:00
|
|
|
]);
|
|
|
|
|
|
|
|
$fare3_price = Math::addPercent($fare3->price, 300);
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// Assign another one to the flight, that's not on the subfleet
|
|
|
|
// This one should NOT be returned in the list of fares
|
2018-02-25 05:38:25 +08:00
|
|
|
$this->fareSvc->setForFlight($flight, $fare4);
|
|
|
|
|
|
|
|
$fares = $this->fareSvc->getAllFares($flight, $subfleet);
|
|
|
|
$this->assertCount(3, $fares);
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
foreach ($fares as $fare) {
|
|
|
|
switch ($fare->id) {
|
2018-02-25 05:38:25 +08:00
|
|
|
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()
|
|
|
|
{
|
2020-05-23 23:43:29 +08:00
|
|
|
$subfleet = factory(Subfleet::class)->create();
|
|
|
|
[$fare1, $fare2, $fare3] = factory(Fare::class, 3)->create();
|
2018-02-25 05:38:25 +08:00
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// add to the subfleet, and just override one of them
|
2018-02-25 05:38:25 +08:00
|
|
|
$this->fareSvc->setForSubfleet($subfleet, $fare1);
|
|
|
|
$this->fareSvc->setForSubfleet($subfleet, $fare2, [
|
2018-08-27 00:40:04 +08:00
|
|
|
'price' => 100,
|
|
|
|
'cost' => 50,
|
2018-02-25 05:38:25 +08:00
|
|
|
'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-02-28 04:12:03 +08:00
|
|
|
|
|
|
|
/**
|
2018-02-28 06:16:40 +08:00
|
|
|
* Get the pilot pay, derived from the rank
|
|
|
|
*/
|
|
|
|
public function testGetPilotPayNoOverride()
|
|
|
|
{
|
|
|
|
$subfleet = $this->createSubfleetWithAircraft(2);
|
|
|
|
$rank = $this->createRank(10, [$subfleet['subfleet']->id]);
|
|
|
|
$this->fleetSvc->addSubfleetToRank($subfleet['subfleet'], $rank);
|
|
|
|
|
2020-05-23 23:43:29 +08:00
|
|
|
$this->user = factory(User::class)->create([
|
2018-02-28 06:16:40 +08:00
|
|
|
'rank_id' => $rank->id,
|
|
|
|
]);
|
|
|
|
|
2020-05-23 23:43:29 +08:00
|
|
|
$pirep = factory(Pirep::class)->create([
|
2018-08-27 00:40:04 +08:00
|
|
|
'user_id' => $this->user->id,
|
2018-02-28 06:16:40 +08:00
|
|
|
'aircraft_id' => $subfleet['aircraft']->random(),
|
2018-08-27 00:40:04 +08:00
|
|
|
'source' => PirepSource::ACARS,
|
2018-02-28 06:16:40 +08:00
|
|
|
]);
|
|
|
|
|
2018-03-02 06:20:13 +08:00
|
|
|
$rate = $this->financeSvc->getPilotPayRateForPirep($pirep);
|
2018-02-28 06:16:40 +08:00
|
|
|
$this->assertEquals($rank->acars_base_pay_rate, $rate);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the pilot pay, but include different overrides
|
2018-02-28 04:12:03 +08:00
|
|
|
*/
|
2018-02-28 06:16:40 +08:00
|
|
|
public function testGetPilotPayWithOverride()
|
2018-02-28 04:12:03 +08:00
|
|
|
{
|
2018-02-28 06:16:40 +08:00
|
|
|
$acars_pay_rate = 100;
|
|
|
|
|
|
|
|
$subfleet = $this->createSubfleetWithAircraft(2);
|
|
|
|
$rank = $this->createRank(10, [$subfleet['subfleet']->id]);
|
|
|
|
$this->fleetSvc->addSubfleetToRank($subfleet['subfleet'], $rank, [
|
|
|
|
'acars_pay' => $acars_pay_rate,
|
|
|
|
]);
|
|
|
|
|
2020-05-23 23:43:29 +08:00
|
|
|
$this->user = factory(User::class)->create([
|
2018-02-28 06:16:40 +08:00
|
|
|
'rank_id' => $rank->id,
|
|
|
|
]);
|
|
|
|
|
2020-05-23 23:43:29 +08:00
|
|
|
$pirep_acars = factory(Pirep::class)->create([
|
2018-08-27 00:40:04 +08:00
|
|
|
'user_id' => $this->user->id,
|
2018-02-28 06:16:40 +08:00
|
|
|
'aircraft_id' => $subfleet['aircraft']->random(),
|
2018-08-27 00:40:04 +08:00
|
|
|
'source' => PirepSource::ACARS,
|
2018-02-28 06:16:40 +08:00
|
|
|
]);
|
|
|
|
|
2018-03-02 06:20:13 +08:00
|
|
|
$rate = $this->financeSvc->getPilotPayRateForPirep($pirep_acars);
|
2018-02-28 06:16:40 +08:00
|
|
|
$this->assertEquals($acars_pay_rate, $rate);
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// Change to a percentage
|
2018-02-28 06:16:40 +08:00
|
|
|
$manual_pay_rate = '50%';
|
|
|
|
$manual_pay_adjusted = Math::addPercent(
|
2020-03-06 09:19:12 +08:00
|
|
|
$rank->manual_base_pay_rate,
|
|
|
|
$manual_pay_rate
|
|
|
|
);
|
2018-02-28 06:16:40 +08:00
|
|
|
|
|
|
|
$this->fleetSvc->addSubfleetToRank($subfleet['subfleet'], $rank, [
|
|
|
|
'manual_pay' => $manual_pay_rate,
|
|
|
|
]);
|
|
|
|
|
2020-05-23 23:43:29 +08:00
|
|
|
$pirep_manual = factory(Pirep::class)->create([
|
2018-08-27 00:40:04 +08:00
|
|
|
'user_id' => $this->user->id,
|
2018-02-28 06:16:40 +08:00
|
|
|
'aircraft_id' => $subfleet['aircraft']->random(),
|
2018-08-27 00:40:04 +08:00
|
|
|
'source' => PirepSource::MANUAL,
|
2018-02-28 06:16:40 +08:00
|
|
|
]);
|
|
|
|
|
2018-03-02 06:20:13 +08:00
|
|
|
$rate = $this->financeSvc->getPilotPayRateForPirep($pirep_manual);
|
2018-02-28 06:16:40 +08:00
|
|
|
$this->assertEquals($manual_pay_adjusted, $rate);
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// And make sure the original acars override still works
|
2018-03-02 06:20:13 +08:00
|
|
|
$rate = $this->financeSvc->getPilotPayRateForPirep($pirep_acars);
|
2018-02-28 06:16:40 +08:00
|
|
|
$this->assertEquals($acars_pay_rate, $rate);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the payment for a pilot
|
|
|
|
*/
|
|
|
|
public function testGetPirepPilotPay()
|
|
|
|
{
|
|
|
|
$acars_pay_rate = 100;
|
|
|
|
|
|
|
|
$subfleet = $this->createSubfleetWithAircraft(2);
|
|
|
|
$rank = $this->createRank(10, [$subfleet['subfleet']->id]);
|
|
|
|
$this->fleetSvc->addSubfleetToRank($subfleet['subfleet'], $rank, [
|
|
|
|
'acars_pay' => $acars_pay_rate,
|
|
|
|
]);
|
|
|
|
|
2020-05-23 23:43:29 +08:00
|
|
|
$this->user = factory(User::class)->create([
|
2018-02-28 06:16:40 +08:00
|
|
|
'rank_id' => $rank->id,
|
|
|
|
]);
|
|
|
|
|
2020-05-23 23:43:29 +08:00
|
|
|
$pirep_acars = factory(Pirep::class)->create([
|
2018-08-27 00:40:04 +08:00
|
|
|
'user_id' => $this->user->id,
|
2018-02-28 06:16:40 +08:00
|
|
|
'aircraft_id' => $subfleet['aircraft']->random(),
|
2018-08-27 00:40:04 +08:00
|
|
|
'source' => PirepSource::ACARS,
|
2018-02-28 06:16:40 +08:00
|
|
|
'flight_time' => 60,
|
|
|
|
]);
|
|
|
|
|
2018-03-03 03:12:39 +08:00
|
|
|
$payment = $this->financeSvc->getPilotPay($pirep_acars);
|
2018-03-01 11:52:36 +08:00
|
|
|
$this->assertEquals(100, $payment->getValue());
|
2018-02-28 06:16:40 +08:00
|
|
|
|
2020-05-23 23:43:29 +08:00
|
|
|
$pirep_acars = factory(Pirep::class)->create([
|
2018-08-27 00:40:04 +08:00
|
|
|
'user_id' => $this->user->id,
|
2018-02-28 06:16:40 +08:00
|
|
|
'aircraft_id' => $subfleet['aircraft']->random(),
|
2018-08-27 00:40:04 +08:00
|
|
|
'source' => PirepSource::ACARS,
|
2018-02-28 06:16:40 +08:00
|
|
|
'flight_time' => 90,
|
|
|
|
]);
|
2018-02-28 04:12:03 +08:00
|
|
|
|
2018-03-03 03:12:39 +08:00
|
|
|
$payment = $this->financeSvc->getPilotPay($pirep_acars);
|
2018-03-01 08:17:15 +08:00
|
|
|
$this->assertEquals($payment->getValue(), 150);
|
2018-02-28 04:12:03 +08:00
|
|
|
}
|
2018-03-01 11:52:36 +08:00
|
|
|
|
2020-03-07 00:36:02 +08:00
|
|
|
public function testGetPirepPilotPayWithFixedPrice()
|
|
|
|
{
|
|
|
|
$acars_pay_rate = 100;
|
|
|
|
|
|
|
|
$subfleet = $this->createSubfleetWithAircraft(2);
|
|
|
|
$rank = $this->createRank(10, [$subfleet['subfleet']->id]);
|
|
|
|
$this->fleetSvc->addSubfleetToRank($subfleet['subfleet'], $rank, [
|
|
|
|
'acars_pay' => $acars_pay_rate,
|
|
|
|
]);
|
|
|
|
|
2020-05-23 23:43:29 +08:00
|
|
|
$this->user = factory(User::class)->create([
|
2020-03-07 00:36:02 +08:00
|
|
|
'rank_id' => $rank->id,
|
|
|
|
]);
|
|
|
|
|
2020-05-23 23:43:29 +08:00
|
|
|
$flight = factory(Flight::class)->create([
|
2020-03-07 00:36:02 +08:00
|
|
|
'airline_id' => $this->user->airline_id,
|
|
|
|
'pilot_pay' => 1000,
|
|
|
|
]);
|
|
|
|
|
2020-05-23 23:43:29 +08:00
|
|
|
$pirep_acars = factory(Pirep::class)->create([
|
2020-03-07 00:36:02 +08:00
|
|
|
'user_id' => $this->user->id,
|
|
|
|
'aircraft_id' => $subfleet['aircraft']->random(),
|
|
|
|
'source' => PirepSource::ACARS,
|
|
|
|
'flight_id' => $flight->id,
|
|
|
|
'flight_time' => 60,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$payment = $this->financeSvc->getPilotPay($pirep_acars);
|
|
|
|
$this->assertEquals(1000, $payment->getValue());
|
|
|
|
|
2020-05-23 23:43:29 +08:00
|
|
|
$pirep_acars = factory(Pirep::class)->create([
|
2020-03-07 00:36:02 +08:00
|
|
|
'user_id' => $this->user->id,
|
|
|
|
'aircraft_id' => $subfleet['aircraft']->random(),
|
|
|
|
'source' => PirepSource::ACARS,
|
|
|
|
'flight_time' => 90,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$payment = $this->financeSvc->getPilotPay($pirep_acars);
|
|
|
|
$this->assertEquals($payment->getValue(), 150);
|
|
|
|
}
|
|
|
|
|
2018-03-01 11:52:36 +08:00
|
|
|
/**
|
|
|
|
* @throws \Prettus\Validator\Exceptions\ValidatorException
|
|
|
|
*/
|
2018-03-19 09:37:35 +08:00
|
|
|
public function testJournalOperations(): void
|
2018-03-01 11:52:36 +08:00
|
|
|
{
|
|
|
|
$journalRepo = app(JournalRepository::class);
|
|
|
|
|
2020-05-23 23:43:29 +08:00
|
|
|
$user = factory(User::class)->create();
|
|
|
|
$journal = factory(Journal::class)->create();
|
2018-03-01 11:52:36 +08:00
|
|
|
|
|
|
|
$journalRepo->post(
|
|
|
|
$journal,
|
|
|
|
Money::createFromAmount(100),
|
|
|
|
null,
|
|
|
|
$user
|
|
|
|
);
|
|
|
|
|
|
|
|
$balance = $journalRepo->getBalance($journal);
|
|
|
|
$this->assertEquals(100, $balance->getValue());
|
2018-03-19 09:37:35 +08:00
|
|
|
$this->assertEquals(100, $journal->balance->getValue());
|
2018-03-01 11:52:36 +08:00
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// add another transaction
|
2018-03-01 11:52:36 +08:00
|
|
|
|
|
|
|
$journalRepo->post(
|
|
|
|
$journal,
|
|
|
|
Money::createFromAmount(25),
|
|
|
|
null,
|
|
|
|
$user
|
|
|
|
);
|
|
|
|
|
|
|
|
$balance = $journalRepo->getBalance($journal);
|
|
|
|
$this->assertEquals(125, $balance->getValue());
|
|
|
|
$this->assertEquals(125, $journal->balance->getValue());
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// debit an amount
|
2018-03-01 11:52:36 +08:00
|
|
|
$journalRepo->post(
|
|
|
|
$journal,
|
|
|
|
null,
|
|
|
|
Money::createFromAmount(25),
|
|
|
|
$user
|
|
|
|
);
|
|
|
|
|
2018-03-01 11:54:58 +08:00
|
|
|
$balance = $journalRepo->getBalance($journal);
|
|
|
|
$this->assertEquals(100, $balance->getValue());
|
2018-03-01 11:52:36 +08:00
|
|
|
$this->assertEquals(100, $journal->balance->getValue());
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// find all transactions
|
2018-03-01 11:52:36 +08:00
|
|
|
$transactions = $journalRepo->getAllForObject($user);
|
|
|
|
|
|
|
|
$this->assertCount(3, $transactions['transactions']);
|
|
|
|
$this->assertEquals(125, $transactions['credits']->getValue());
|
|
|
|
$this->assertEquals(25, $transactions['debits']->getValue());
|
|
|
|
}
|
2018-03-02 06:20:13 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function testPirepFares()
|
|
|
|
{
|
|
|
|
[$user, $pirep, $fares] = $this->createFullPirep();
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// Override the fares
|
2018-03-02 06:20:13 +08:00
|
|
|
$fare_counts = [];
|
|
|
|
foreach ($fares as $fare) {
|
|
|
|
$fare_counts[] = [
|
|
|
|
'fare_id' => $fare->id,
|
2018-08-27 00:40:04 +08:00
|
|
|
'price' => $fare->price,
|
|
|
|
'count' => round($fare->capacity / 2),
|
2018-03-02 06:20:13 +08:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->fareSvc->saveForPirep($pirep, $fare_counts);
|
|
|
|
$all_fares = $this->financeSvc->getReconciledFaresForPirep($pirep);
|
|
|
|
|
|
|
|
$fare_counts = collect($fare_counts);
|
2018-08-27 00:40:04 +08:00
|
|
|
foreach ($all_fares as $fare) {
|
2018-03-02 06:20:13 +08:00
|
|
|
$set_fare = $fare_counts->where('fare_id', $fare->id)->first();
|
|
|
|
$this->assertEquals($set_fare['count'], $fare->count);
|
|
|
|
$this->assertEquals($set_fare['price'], $fare->price);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-03 07:48:36 +08:00
|
|
|
/**
|
|
|
|
* Test that all expenses are pulled properly
|
|
|
|
*/
|
|
|
|
public function testPirepExpenses()
|
|
|
|
{
|
2020-05-23 23:43:29 +08:00
|
|
|
$airline = factory(Airline::class)->create();
|
|
|
|
$airline2 = factory(Airline::class)->create();
|
2018-03-03 07:48:36 +08:00
|
|
|
|
2020-05-23 23:43:29 +08:00
|
|
|
factory(Expense::class)->create([
|
2018-08-27 00:40:04 +08:00
|
|
|
'airline_id' => $airline->id,
|
2018-03-03 07:48:36 +08:00
|
|
|
]);
|
|
|
|
|
2020-05-23 23:43:29 +08:00
|
|
|
factory(Expense::class)->create([
|
2018-08-27 00:40:04 +08:00
|
|
|
'airline_id' => $airline2->id,
|
2018-03-03 07:48:36 +08:00
|
|
|
]);
|
|
|
|
|
2020-05-23 23:43:29 +08:00
|
|
|
factory(Expense::class)->create([
|
2018-08-27 00:40:04 +08:00
|
|
|
'airline_id' => null,
|
2018-03-03 07:48:36 +08:00
|
|
|
]);
|
|
|
|
|
2018-03-06 11:24:49 +08:00
|
|
|
$expenses = $this->expenseRepo->getAllForType(
|
|
|
|
ExpenseType::FLIGHT,
|
|
|
|
$airline->id,
|
2020-02-12 23:40:52 +08:00
|
|
|
Expense::class
|
2018-03-06 11:24:49 +08:00
|
|
|
);
|
2018-03-03 07:48:36 +08:00
|
|
|
|
|
|
|
$this->assertCount(2, $expenses);
|
|
|
|
|
|
|
|
$found = $expenses->where('airline_id', null);
|
|
|
|
$this->assertCount(1, $found);
|
|
|
|
|
|
|
|
$found = $expenses->where('airline_id', $airline->id);
|
|
|
|
$this->assertCount(1, $found);
|
|
|
|
|
|
|
|
$found = $expenses->where('airline_id', $airline2->id);
|
|
|
|
$this->assertCount(0, $found);
|
2018-03-06 12:49:42 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Test the subfleet class
|
|
|
|
*/
|
|
|
|
|
2020-05-23 23:43:29 +08:00
|
|
|
$subfleet = factory(Subfleet::class)->create();
|
|
|
|
factory(Expense::class)->create([
|
2018-08-27 00:40:04 +08:00
|
|
|
'airline_id' => null,
|
2020-02-12 23:40:52 +08:00
|
|
|
'ref_model' => Subfleet::class,
|
2018-04-02 11:26:20 +08:00
|
|
|
'ref_model_id' => $subfleet->id,
|
2018-03-06 12:49:42 +08:00
|
|
|
]);
|
|
|
|
|
|
|
|
$expenses = $this->expenseRepo->getAllForType(
|
|
|
|
ExpenseType::FLIGHT,
|
|
|
|
$airline->id,
|
2018-04-02 11:26:20 +08:00
|
|
|
$subfleet
|
2018-03-06 12:49:42 +08:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertCount(1, $expenses);
|
2018-04-02 11:26:20 +08:00
|
|
|
|
|
|
|
$expense = $expenses->random();
|
2020-02-12 23:40:52 +08:00
|
|
|
$this->assertEquals(Subfleet::class, $expense->ref_model);
|
2018-04-02 11:26:20 +08:00
|
|
|
$obj = $expense->getReferencedObject();
|
|
|
|
$this->assertEquals($obj->id, $expense->ref_model_id);
|
2018-03-03 07:48:36 +08:00
|
|
|
}
|
|
|
|
|
2020-05-27 06:41:25 +08:00
|
|
|
public function testAirportExpenses()
|
|
|
|
{
|
|
|
|
$apt1 = factory(Airport::class)->create();
|
|
|
|
$apt2 = factory(Airport::class)->create();
|
|
|
|
$apt3 = factory(Airport::class)->create();
|
|
|
|
|
|
|
|
factory(Expense::class)->create([
|
|
|
|
'airline_id' => null,
|
|
|
|
'ref_model' => Airport::class,
|
|
|
|
'ref_model_id' => $apt1->id,
|
|
|
|
]);
|
|
|
|
|
|
|
|
factory(Expense::class)->create([
|
|
|
|
'airline_id' => null,
|
|
|
|
'ref_model' => Airport::class,
|
|
|
|
'ref_model_id' => $apt2->id,
|
|
|
|
]);
|
|
|
|
|
|
|
|
factory(Expense::class)->create([
|
|
|
|
'airline_id' => null,
|
|
|
|
'ref_model' => Airport::class,
|
|
|
|
'ref_model_id' => $apt3->id,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$expenses = $this->expenseRepo->getAllForType(
|
|
|
|
ExpenseType::FLIGHT,
|
|
|
|
null,
|
|
|
|
Airport::class
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertCount(3, $expenses);
|
|
|
|
}
|
|
|
|
|
2018-03-02 06:20:13 +08:00
|
|
|
/**
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function testPirepFinances()
|
|
|
|
{
|
2018-03-02 09:15:55 +08:00
|
|
|
$journalRepo = app(JournalRepository::class);
|
|
|
|
|
2018-03-02 06:20:13 +08:00
|
|
|
[$user, $pirep, $fares] = $this->createFullPirep();
|
2020-04-26 23:55:20 +08:00
|
|
|
$user->airline->initJournal(setting('units.currency', 'USD'));
|
2018-03-02 06:20:13 +08:00
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// Override the fares
|
2018-03-02 06:20:13 +08:00
|
|
|
$fare_counts = [];
|
|
|
|
foreach ($fares as $fare) {
|
|
|
|
$fare_counts[] = [
|
|
|
|
'fare_id' => $fare->id,
|
2018-08-27 00:40:04 +08:00
|
|
|
'price' => $fare->price,
|
|
|
|
'count' => 100,
|
2018-03-02 06:20:13 +08:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->fareSvc->saveForPirep($pirep, $fare_counts);
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// This should process all of the
|
2018-03-02 06:20:13 +08:00
|
|
|
$pirep = $this->pirepSvc->accept($pirep);
|
2018-03-02 09:15:55 +08:00
|
|
|
|
|
|
|
$transactions = $journalRepo->getAllForObject($pirep);
|
|
|
|
|
2020-05-27 06:41:25 +08:00
|
|
|
// $this->assertCount(9, $transactions['transactions']);
|
2018-03-03 07:29:11 +08:00
|
|
|
$this->assertEquals(3020, $transactions['credits']->getValue());
|
2020-05-27 06:41:25 +08:00
|
|
|
$this->assertEquals(2060, $transactions['debits']->getValue());
|
2018-03-06 02:21:38 +08:00
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
// Check that all the different transaction types are there
|
|
|
|
// test by the different groups that exist
|
2018-03-06 20:40:49 +08:00
|
|
|
$transaction_tags = [
|
2019-07-23 20:41:20 +08:00
|
|
|
'fuel' => 1,
|
2020-05-27 06:41:25 +08:00
|
|
|
'airport' => 1,
|
2018-08-27 00:40:04 +08:00
|
|
|
'expense' => 1,
|
|
|
|
'subfleet' => 2,
|
|
|
|
'fare' => 3,
|
|
|
|
'ground_handling' => 1,
|
|
|
|
'pilot_pay' => 2, // debit on the airline, credit to the pilot
|
2018-03-06 02:21:38 +08:00
|
|
|
];
|
|
|
|
|
2018-08-27 00:40:04 +08:00
|
|
|
foreach ($transaction_tags as $type => $count) {
|
2018-03-06 20:40:49 +08:00
|
|
|
$find = $transactions['transactions']->where('tags', $type);
|
2018-03-06 02:21:38 +08:00
|
|
|
$this->assertEquals($count, $find->count());
|
|
|
|
}
|
2018-03-02 06:20:13 +08:00
|
|
|
}
|
2020-02-12 23:40:52 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @throws Exception
|
|
|
|
*/
|
|
|
|
public function testPirepFinancesSpecificExpense()
|
|
|
|
{
|
|
|
|
$journalRepo = app(JournalRepository::class);
|
|
|
|
|
|
|
|
// Add an expense that's only for a cargo flight
|
2020-05-23 23:43:29 +08:00
|
|
|
factory(Expense::class)->create([
|
2020-02-12 23:40:52 +08:00
|
|
|
'airline_id' => null,
|
|
|
|
'amount' => 100,
|
|
|
|
'flight_type' => FlightType::SCHED_CARGO,
|
|
|
|
]);
|
|
|
|
|
|
|
|
[$user, $pirep, $fares] = $this->createFullPirep();
|
2020-04-26 23:55:20 +08:00
|
|
|
$user->airline->initJournal(setting('units.currency', 'USD'));
|
2020-02-12 23:40:52 +08:00
|
|
|
|
|
|
|
// Override the fares
|
|
|
|
$fare_counts = [];
|
|
|
|
foreach ($fares as $fare) {
|
|
|
|
$fare_counts[] = [
|
|
|
|
'fare_id' => $fare->id,
|
|
|
|
'price' => $fare->price,
|
|
|
|
'count' => 100,
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->fareSvc->saveForPirep($pirep, $fare_counts);
|
|
|
|
|
|
|
|
// This should process all of the
|
|
|
|
$pirep = $this->pirepSvc->accept($pirep);
|
|
|
|
|
|
|
|
$transactions = $journalRepo->getAllForObject($pirep);
|
|
|
|
|
|
|
|
// $this->assertCount(9, $transactions['transactions']);
|
|
|
|
$this->assertEquals(3020, $transactions['credits']->getValue());
|
2020-05-27 06:41:25 +08:00
|
|
|
$this->assertEquals(2060, $transactions['debits']->getValue());
|
2020-02-12 23:40:52 +08:00
|
|
|
|
|
|
|
// Check that all the different transaction types are there
|
|
|
|
// test by the different groups that exist
|
|
|
|
$transaction_tags = [
|
|
|
|
'fuel' => 1,
|
2020-05-27 06:41:25 +08:00
|
|
|
'airport' => 1,
|
2020-02-12 23:40:52 +08:00
|
|
|
'expense' => 1,
|
|
|
|
'subfleet' => 2,
|
|
|
|
'fare' => 3,
|
|
|
|
'ground_handling' => 1,
|
|
|
|
'pilot_pay' => 2, // debit on the airline, credit to the pilot
|
|
|
|
];
|
|
|
|
|
|
|
|
foreach ($transaction_tags as $type => $count) {
|
|
|
|
$find = $transactions['transactions']->where('tags', $type);
|
|
|
|
$this->assertEquals($count, $find->count());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add a new PIREP;
|
2020-05-23 23:43:29 +08:00
|
|
|
$pirep2 = factory(Pirep::class)->create([
|
2020-02-12 23:40:52 +08:00
|
|
|
'flight_number' => 100,
|
|
|
|
'flight_type' => FlightType::SCHED_CARGO,
|
|
|
|
'dpt_airport_id' => $pirep->dpt_airport_id,
|
|
|
|
'arr_airport_id' => $pirep->arr_airport_id,
|
|
|
|
'user_id' => $user->id,
|
|
|
|
'airline_id' => $user->airline_id,
|
|
|
|
'aircraft_id' => $pirep->aircraft_id,
|
|
|
|
'source' => PirepSource::ACARS,
|
|
|
|
'flight_time' => 120,
|
|
|
|
'block_fuel' => 10,
|
|
|
|
'fuel_used' => 9,
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->fareSvc->saveForPirep($pirep2, $fare_counts);
|
|
|
|
$pirep2 = $this->pirepSvc->accept($pirep2);
|
|
|
|
|
|
|
|
$transactions = $journalRepo->getAllForObject($pirep2);
|
|
|
|
$this->assertEquals(3020, $transactions['credits']->getValue());
|
2020-05-27 06:41:25 +08:00
|
|
|
$this->assertEquals(2160, $transactions['debits']->getValue());
|
2020-02-12 23:40:52 +08:00
|
|
|
|
|
|
|
// Check that all the different transaction types are there
|
|
|
|
// test by the different groups that exist
|
|
|
|
$transaction_tags = [
|
|
|
|
'fuel' => 1,
|
2020-05-27 06:41:25 +08:00
|
|
|
'airport' => 1,
|
2020-02-12 23:40:52 +08:00
|
|
|
'expense' => 2,
|
|
|
|
'subfleet' => 2,
|
|
|
|
'fare' => 3,
|
|
|
|
'ground_handling' => 1,
|
|
|
|
'pilot_pay' => 2, // debit on the airline, credit to the pilot
|
|
|
|
];
|
|
|
|
|
|
|
|
foreach ($transaction_tags as $type => $count) {
|
|
|
|
$find = $transactions['transactions']->where('tags', $type);
|
|
|
|
$this->assertEquals($count, $find->count());
|
|
|
|
}
|
|
|
|
}
|
2018-01-07 23:19:46 +08:00
|
|
|
}
|