Allow setting percent for fare overrides against base fare #125
This commit is contained in:
@@ -36,9 +36,9 @@ class CreateSubfleetTables extends Migration
|
||||
Schema::create('subfleet_fare', function (Blueprint $table) {
|
||||
$table->unsignedInteger('subfleet_id');
|
||||
$table->unsignedInteger('fare_id');
|
||||
$table->unsignedDecimal('price')->nullable();
|
||||
$table->unsignedDecimal('cost')->nullable();
|
||||
$table->unsignedInteger('capacity')->nullable();
|
||||
$table->string('price')->nullable();
|
||||
$table->string('cost')->nullable();
|
||||
$table->string('capacity')->nullable();
|
||||
$table->timestamps();
|
||||
|
||||
$table->primary(['subfleet_id', 'fare_id']);
|
||||
|
||||
@@ -5,9 +5,44 @@ namespace App\Services;
|
||||
use App\Models\Fare;
|
||||
use App\Models\Flight;
|
||||
use App\Models\Subfleet;
|
||||
use App\Support\Math;
|
||||
|
||||
class FareService extends BaseService
|
||||
{
|
||||
/**
|
||||
* Get fares
|
||||
* @param $fare
|
||||
* @return mixed
|
||||
*/
|
||||
protected function getFares($fare)
|
||||
{
|
||||
if (filled($fare->pivot->price)) {
|
||||
if (substr_count($fare->pivot->price, '%', -1)) {
|
||||
$fare->price = Math::addPercent($fare->price, $fare->pivot->price);
|
||||
} else {
|
||||
$fare->price = $fare->pivot->price;
|
||||
}
|
||||
}
|
||||
|
||||
if (filled($fare->pivot->cost)) {
|
||||
if (substr_count($fare->pivot->cost, '%', -1)) {
|
||||
$fare->cost = Math::addPercent($fare->cost, $fare->pivot->cost);
|
||||
} else {
|
||||
$fare->cost = $fare->pivot->cost;
|
||||
}
|
||||
}
|
||||
|
||||
if (filled($fare->pivot->capacity)) {
|
||||
if (substr_count($fare->pivot->capacity, '%', -1)) {
|
||||
$fare->capacity = Math::addPercent($fare->capacity, $fare->pivot->capacity);
|
||||
} else {
|
||||
$fare->capacity = $fare->pivot->capacity;
|
||||
}
|
||||
}
|
||||
|
||||
return $fare;
|
||||
}
|
||||
|
||||
/**
|
||||
* Attach a fare to an flight
|
||||
*
|
||||
@@ -40,19 +75,7 @@ class FareService extends BaseService
|
||||
public function getForFlight(Flight $flight)
|
||||
{
|
||||
$fares = $flight->fares->map(function ($fare) {
|
||||
if (null !== $fare->pivot->price) {
|
||||
$fare->price = $fare->pivot->price;
|
||||
}
|
||||
|
||||
if (null !== $fare->pivot->cost) {
|
||||
$fare->cost = $fare->pivot->cost;
|
||||
}
|
||||
|
||||
if (null !== $fare->pivot->capacity) {
|
||||
$fare->capacity = $fare->pivot->capacity;
|
||||
}
|
||||
|
||||
return $fare;
|
||||
return $this->getFares($fare);
|
||||
});
|
||||
|
||||
return $fares;
|
||||
@@ -102,19 +125,7 @@ class FareService extends BaseService
|
||||
public function getForSubfleet(Subfleet $subfleet)
|
||||
{
|
||||
$fares = $subfleet->fares->map(function($fare) {
|
||||
if(!is_null($fare->pivot->price)) {
|
||||
$fare->price = $fare->pivot->price;
|
||||
}
|
||||
|
||||
if(!is_null($fare->pivot->cost)) {
|
||||
$fare->cost = $fare->pivot->cost;
|
||||
}
|
||||
|
||||
if(!is_null($fare->pivot->capacity)) {
|
||||
$fare->capacity = $fare->pivot->capacity;
|
||||
}
|
||||
|
||||
return $fare;
|
||||
return $this->getFares($fare);
|
||||
});
|
||||
|
||||
return $fares;
|
||||
|
||||
32
app/Support/Math.php
Normal file
32
app/Support/Math.php
Normal file
@@ -0,0 +1,32 @@
|
||||
<?php
|
||||
|
||||
namespace App\Support;
|
||||
|
||||
/**
|
||||
* Helper math
|
||||
* @package App\Support
|
||||
*/
|
||||
class Math
|
||||
{
|
||||
|
||||
/**
|
||||
* Add/subtract a percentage to a number
|
||||
* @param $number
|
||||
* @param $percent
|
||||
* @return float
|
||||
*/
|
||||
public static function addPercent($number, $percent): float
|
||||
{
|
||||
if(!is_numeric($number)) {
|
||||
$number = (float) $number;
|
||||
}
|
||||
|
||||
if(!is_numeric($percent)) {
|
||||
$percent = (float) $percent;
|
||||
}
|
||||
|
||||
|
||||
return $number + ($number * ($percent/100));
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user