diff --git a/app/Database/migrations/2020_02_12_141152_expenses_add_flight_type.php b/app/Database/migrations/2020_02_12_141152_expenses_add_flight_type.php new file mode 100644 index 00000000..b3a3e98b --- /dev/null +++ b/app/Database/migrations/2020_02_12_141152_expenses_add_flight_type.php @@ -0,0 +1,32 @@ +string('flight_type', 50) + ->nullable() + ->after('type'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('expenses', function (Blueprint $table) { + $table->dropColumn('flight_type'); + }); + } +} diff --git a/app/Http/Controllers/Admin/ExpenseController.php b/app/Http/Controllers/Admin/ExpenseController.php index 79fc218f..a331dd58 100644 --- a/app/Http/Controllers/Admin/ExpenseController.php +++ b/app/Http/Controllers/Admin/ExpenseController.php @@ -5,6 +5,7 @@ namespace App\Http\Controllers\Admin; use App\Contracts\Controller; use App\Http\Controllers\Admin\Traits\Importable; use App\Models\Enums\ExpenseType; +use App\Models\Enums\FlightType; use App\Models\Enums\ImportExportType; use App\Models\Expense; use App\Repositories\AirlineRepository; @@ -69,6 +70,7 @@ class ExpenseController extends Controller return view('admin.expenses.create', [ 'airlines_list' => $this->airlineRepo->selectBoxList(true), 'expense_types' => ExpenseType::select(), + 'flight_types' => FlightType::select(), ]); } @@ -135,6 +137,7 @@ class ExpenseController extends Controller 'expense' => $expense, 'airlines_list' => $this->airlineRepo->selectBoxList(true), 'expense_types' => ExpenseType::select(), + 'flight_types' => FlightType::select(), ]); } @@ -154,14 +157,12 @@ class ExpenseController extends Controller if (empty($expenses)) { Flash::error('Expense not found'); - return redirect(route('admin.expenses.index')); } $this->expenseRepo->update($request->all(), $id); Flash::success('Expense updated successfully.'); - return redirect(route('admin.expenses.index')); } diff --git a/app/Models/Expense.php b/app/Models/Expense.php index 15e0fd85..3a328487 100644 --- a/app/Models/Expense.php +++ b/app/Models/Expense.php @@ -6,13 +6,15 @@ use App\Contracts\Model; use App\Models\Traits\ReferenceTrait; /** - * Class Expense - * * @property int airline_id * @property float amount * @property string name + * @property string type + * @property string flight_type * @property string ref_model * @property string ref_model_id + * + * @mixin \Illuminate\Database\Eloquent\Builder */ class Expense extends Model { @@ -25,6 +27,7 @@ class Expense extends Model 'name', 'amount', 'type', + 'flight_type', 'multiplier', 'charge_to_user', 'ref_model', @@ -40,6 +43,34 @@ class Expense extends Model 'charge_to_user' => 'bool', ]; + /** + * flight_type is stored a comma delimited list in table. Retrieve it as an array + * + * @return array + */ + public function getFlightTypeAttribute() + { + if (empty(trim($this->attributes['flight_type']))) { + return []; + } + + return explode(',', $this->attributes['flight_type']); + } + + /** + * Make sure the flight type is stored a comma-delimited list in the table + * + * @param string $value + */ + public function setFlightTypeAttribute($value) + { + if (is_array($value)) { + $this->attributes['flight_type'] = implode(',', $value); + } else { + $this->attributes['flight_type'] = trim($value); + } + } + /** * Foreign Keys */ diff --git a/app/Models/Pirep.php b/app/Models/Pirep.php index 90d013d3..5b176e69 100644 --- a/app/Models/Pirep.php +++ b/app/Models/Pirep.php @@ -17,6 +17,7 @@ use Illuminate\Support\Collection; * @property string flight_number * @property string route_code * @property string route_leg + * @property string flight_type * @property int airline_id * @property int user_id * @property int aircraft_id diff --git a/app/Models/Traits/ReferenceTrait.php b/app/Models/Traits/ReferenceTrait.php index f7305830..bb6c4bdf 100644 --- a/app/Models/Traits/ReferenceTrait.php +++ b/app/Models/Traits/ReferenceTrait.php @@ -27,7 +27,7 @@ trait ReferenceTrait /** * Return an instance of the object or null * - * @return \App\Contracts\Model|null + * @return \App\Contracts\Model|$this|null */ public function getReferencedObject() { diff --git a/app/Services/Finance/PirepFinanceService.php b/app/Services/Finance/PirepFinanceService.php index 2cd99bbc..e79f0b28 100644 --- a/app/Services/Finance/PirepFinanceService.php +++ b/app/Services/Finance/PirepFinanceService.php @@ -4,10 +4,13 @@ namespace App\Services\Finance; use App\Contracts\Service; use App\Events\Expenses as ExpensesEvent; +use App\Models\Aircraft; +use App\Models\Airport; use App\Models\Enums\ExpenseType; use App\Models\Enums\PirepSource; use App\Models\Expense; use App\Models\Pirep; +use App\Models\Subfleet; use App\Repositories\ExpenseRepository; use App\Repositories\JournalRepository; use App\Services\FareService; @@ -222,13 +225,19 @@ class PirepFinanceService extends Service /* * Go through the expenses and apply a mulitplier if present */ - $expenses->map(function ($expense, $i) use ($pirep) { - /*if ($expense->multiplier) { - # TODO: Modify the amount - }*/ - + $expenses->map(function (/** @var \App\Models\Expense */ $expense, $i) use ($pirep) { Log::info('Finance: PIREP: '.$pirep->id.', expense:', $expense->toArray()); + // Check to see if there is a certain fleet or flight type set on this expense + // if there is and it doesn't match up the flight type for the PIREP, skip it + if ($expense->ref_model === Expense::class) { + if (is_array($expense->flight_type) && count($expense->flight_type) > 0) { + if (!in_array($pirep->flight_type, $expense->flight_type, true)) { + return; + } + } + } + // Get the transaction group name from the ref_model name // This way it can be more dynamic and don't have to add special // tables or specific expense calls to accomodate all of these @@ -239,13 +248,13 @@ class PirepFinanceService extends Service } // Form the memo, with some specific ones depending on the group - if ($klass === 'Airport') { + if ($expense->ref_model === Airport::class) { $memo = "Airport Expense: {$expense->name} ({$expense->ref_model_id})"; $transaction_group = "Airport: {$expense->ref_model_id}"; - } elseif ($klass === 'Subfleet') { + } elseif ($expense->ref_model === Subfleet::class) { $memo = "Subfleet Expense: {$expense->name} ({$pirep->aircraft->subfleet->name})"; $transaction_group = "Subfleet: {$expense->name} ({$pirep->aircraft->subfleet->name})"; - } elseif ($klass === 'Aircraft') { + } elseif ($expense->ref_model === Aircraft::class) { $memo = "Aircraft Expense: {$expense->name} ({$pirep->aircraft->name})"; $transaction_group = "Aircraft: {$expense->name} " ."({$pirep->aircraft->name}-{$pirep->aircraft->registration})"; diff --git a/resources/views/admin/expenses/fields.blade.php b/resources/views/admin/expenses/fields.blade.php index 87072a89..8b8253df 100644 --- a/resources/views/admin/expenses/fields.blade.php +++ b/resources/views/admin/expenses/fields.blade.php @@ -1,6 +1,6 @@
{{ $errors->first('type') }}
{{ $errors->first('type') }}
+ @component('admin.components.info') + If selected and the expense type is "flight", this expense will only apply to the specified flight types + @endcomponent +