diff --git a/app/Database/migrations/2018_02_26_185121_create_expenses_table.php b/app/Database/migrations/2018_02_26_185121_create_expenses_table.php index 8ce03d65..2d25d07b 100644 --- a/app/Database/migrations/2018_02_26_185121_create_expenses_table.php +++ b/app/Database/migrations/2018_02_26_185121_create_expenses_table.php @@ -14,7 +14,7 @@ class CreateExpensesTable extends Migration $table->string('name'); $table->unsignedInteger('amount'); - $table->unsignedTinyInteger('type'); + $table->char('type'); $table->boolean('charge_to_user')->nullable()->default(false); $table->boolean('multiplier')->nullable()->default(0); $table->boolean('active')->nullable()->default(true); diff --git a/app/Database/seeds/sample.yml b/app/Database/seeds/sample.yml index 6d7d9ffa..79032bdc 100644 --- a/app/Database/seeds/sample.yml +++ b/app/Database/seeds/sample.yml @@ -192,35 +192,35 @@ aircraft: expenses: - name: Per-Flight (no muliplier) amount: 100 - type: 0 + type: F active: 1 ref_model: App\Models\Expense - name: Per-Flight (multiplier) amount: 100 - type: 0 + type: F multiplier: 1 active: 1 ref_model: App\Models\Expense - name: Per-Flight (multiplier, on airline) airline_id: 1 amount: 200 - type: 0 + type: F multiplier: 1 active: 1 ref_model: App\Models\Expense - name: A daily fee amount: 800 - type: 1 + type: D active: 1 ref_model: App\Models\Expense - name: A monthly fee amount: 5000 - type: 2 + type: M active: 1 ref_model: App\Models\Expense - name: Catering amount: 1000 - type: 0 + type: F active: 1 ref_model: App\Models\Subfleet ref_model_id: 1 @@ -228,7 +228,7 @@ expenses: updated_at: now - name: Catering Staff amount: 1000 - type: 1 + type: D active: 1 ref_model: App\Models\Subfleet ref_model_id: 1 @@ -236,7 +236,7 @@ expenses: updated_at: now - name: Maintenance amount: 1000 - type: 1 + type: D active: 1 ref_model: App\Models\Aircraft ref_model_id: 1 @@ -518,7 +518,7 @@ journal_transactions: debit: null currency: USD memo: 'Pilot Payment @ 50' - tags: '"pilot_pay"' + tags: "pilot_pay" ref_model: App\Models\Pirep ref_model_id: pirepid_1 created_at: '2018-03-06 12:34:15' @@ -532,7 +532,7 @@ journal_transactions: debit: 10000 currency: USD memo: 'Expense: Per-Flight (no muliplier)' - tags: '"expense"' + tags: "expense" ref_model: App\Models\Pirep ref_model_id: pirepid_1 created_at: '2018-03-06 12:34:15' @@ -546,7 +546,7 @@ journal_transactions: debit: 0 currency: USD memo: 'Fares Y300; price: 200, cost: 0' - tags: '"fare"' + tags: "fare" ref_model: App\Models\Pirep ref_model_id: pirepid_1 created_at: '2018-03-06 12:34:15' @@ -560,7 +560,7 @@ journal_transactions: debit: 20000 currency: USD memo: 'Expense: Per-Flight (multiplier, on airline)' - tags: '"expense"' + tags: "expense" ref_model: App\Models\Pirep ref_model_id: pirepid_1 created_at: '2018-03-06 12:34:15' @@ -574,7 +574,7 @@ journal_transactions: debit: 10000 currency: USD memo: 'Expense: Per-Flight (multiplier)' - tags: '"expense"' + tags: "expense" ref_model: App\Models\Pirep ref_model_id: pirepid_1 created_at: '2018-03-06 12:34:15' @@ -588,7 +588,7 @@ journal_transactions: debit: 100000 currency: USD memo: 'Subfleet Expense: Catering (747-43X RB211-524G)' - tags: '"subfleet"' + tags: "subfleet" ref_model: App\Models\Pirep ref_model_id: pirepid_1 created_at: '2018-03-06 12:34:15' @@ -602,7 +602,7 @@ journal_transactions: debit: 0 currency: USD memo: 'Fares F10; price: 1000, cost: 0' - tags: '"fare"' + tags: "fare" ref_model: App\Models\Pirep ref_model_id: pirepid_1 created_at: '2018-03-06 12:34:15' @@ -616,7 +616,7 @@ journal_transactions: debit: 15000 currency: USD memo: 'Pilot Payment @ 50' - tags: '"pilot_pay"' + tags: "pilot_pay" ref_model: App\Models\Pirep ref_model_id: pirepid_1 created_at: now @@ -630,7 +630,7 @@ journal_transactions: debit: 0 currency: USD memo: 'Fares B10; price: 1100, cost: 0' - tags: '"fare"' + tags: "fare" ref_model: App\Models\Pirep ref_model_id: pirepid_1 created_at: now @@ -644,7 +644,7 @@ journal_transactions: debit: 75000 currency: USD memo: 'Ground Handling' - tags: '"ground_handling"' + tags: "ground_handling" ref_model: App\Models\Pirep ref_model_id: pirepid_1 created_at: now @@ -659,7 +659,7 @@ journal_transactions: debit: 300000 currency: USD memo: 'Subfleet 744-3X-RB211: Block Time Cost' - tags: '"subfleet"' + tags: "subfleet" ref_model: App\Models\Pirep ref_model_id: pirepid_1 created_at: now diff --git a/app/Models/Enums/ActiveState.php b/app/Models/Enums/ActiveState.php index d20fc413..bfe5df17 100644 --- a/app/Models/Enums/ActiveState.php +++ b/app/Models/Enums/ActiveState.php @@ -14,7 +14,7 @@ class ActiveState extends Enum public const ACTIVE = 1; public static $labels = [ - ActiveState::INACTIVE => 'Inactive', - ActiveState::ACTIVE => 'Active', + ActiveState::ACTIVE => 'system.global.active', + ActiveState::INACTIVE => 'system.global.inactive', ]; } diff --git a/app/Models/Enums/ExpenseType.php b/app/Models/Enums/ExpenseType.php index 0330fd0a..1e744e6b 100644 --- a/app/Models/Enums/ExpenseType.php +++ b/app/Models/Enums/ExpenseType.php @@ -10,14 +10,14 @@ use App\Interfaces\Enum; */ class ExpenseType extends Enum { - public const FLIGHT = 0; - public const DAILY = 1; - public const MONTHLY = 2; + public const FLIGHT = 'F'; + public const DAILY = 'D'; + public const MONTHLY = 'M'; protected static $labels = [ - ExpenseType::FLIGHT => 'Flight', - ExpenseType::DAILY => 'Daily', - ExpenseType::MONTHLY => 'Monthly', + ExpenseType::FLIGHT => 'system.expenses.type.flight', + ExpenseType::DAILY => 'system.expenses.type.daily', + ExpenseType::MONTHLY => 'system.expenses.type.monthly', ]; protected static $codes = [ diff --git a/app/Models/Expense.php b/app/Models/Expense.php index 0a682ab8..e282a42e 100644 --- a/app/Models/Expense.php +++ b/app/Models/Expense.php @@ -2,6 +2,7 @@ namespace App\Models; use App\Interfaces\Model; +use App\Models\Traits\ReferenceTrait; /** * Class Expense @@ -14,6 +15,8 @@ use App\Interfaces\Model; */ class Expense extends Model { + use ReferenceTrait; + public $table = 'expenses'; protected $fillable = [ @@ -32,33 +35,10 @@ class Expense extends Model 'active' => 'boolean', 'airline_id' => 'integer', 'amount' => 'float', - 'type' => 'integer', 'multiplier' => 'bool', 'charge_to_user' => 'bool', ]; - /** - * Get the referring object - */ - public function getReference() - { - if (!$this->ref_model || !$this->ref_model_id) { - return null; - } - - if ($this->ref_model === __CLASS__) { - return $this; - } - - try { - $klass = new $this->ref_model; - - return $klass->find($this->ref_model_id); - } catch (\Exception $e) { - return null; - } - } - /** * Foreign Keys */ diff --git a/app/Models/Traits/ReferenceTrait.php b/app/Models/Traits/ReferenceTrait.php index 27129cf9..f36349d7 100644 --- a/app/Models/Traits/ReferenceTrait.php +++ b/app/Models/Traits/ReferenceTrait.php @@ -5,7 +5,7 @@ namespace App\Models\Traits; /** * Trait ReferenceTrait * @property \App\Interfaces\Model $ref_model - * @property mixed $ref_model_id + * @property mixed $ref_model_id * @package App\Models\Traits */ trait ReferenceTrait @@ -29,12 +29,20 @@ trait ReferenceTrait */ public function getReferencedObject() { - if ($classname = $this->ref_model) { - $klass = new $this->ref_model; - - return $klass->find($this->ref_model_id); + if (!$this->ref_model || !$this->ref_model_id) { + return null; } - return null; + if ($this->ref_model === __CLASS__) { + return $this; + } + + try { + $klass = new $this->ref_model; + $obj = $klass->find($this->ref_model_id); + return $obj; + } catch (\Exception $e) { + return null; + } } } diff --git a/app/Repositories/ExpenseRepository.php b/app/Repositories/ExpenseRepository.php index f9c96e75..dfb842b9 100644 --- a/app/Repositories/ExpenseRepository.php +++ b/app/Repositories/ExpenseRepository.php @@ -37,7 +37,15 @@ class ExpenseRepository extends Repository implements CacheableInterface ]; if ($ref_model) { - $where['ref_model'] = $ref_model; + if (\is_object($ref_model)) { + $ref_model_type = \get_class($ref_model); + } else { + $ref_model_type = $ref_model; + } + + if ($ref_model) { + $where['ref_model'] = $ref_model_type; + } } $expenses = $this->findWhere($where); @@ -49,7 +57,7 @@ class ExpenseRepository extends Repository implements CacheableInterface ]; if ($ref_model) { - $where['ref_model'] = $ref_model; + $where['ref_model'] = $ref_model_type; } $airline_expenses = $this->findWhere($where); diff --git a/app/Services/Finance/RecurringFinanceService.php b/app/Services/Finance/RecurringFinanceService.php index 9f7d2d60..9da71f37 100644 --- a/app/Services/Finance/RecurringFinanceService.php +++ b/app/Services/Finance/RecurringFinanceService.php @@ -60,7 +60,7 @@ class RecurringFinanceService extends Service if ($expense->ref_model) { $ref = explode('\\', $expense->ref_model); $klass = end($ref); - $obj = $expense->getReference(); + $obj = $expense->getReferencedObject(); } if ($klass === 'Airport') { diff --git a/app/Services/ImportExport/ExpenseExporter.php b/app/Services/ImportExport/ExpenseExporter.php index c2368861..1a99e96a 100644 --- a/app/Services/ImportExport/ExpenseExporter.php +++ b/app/Services/ImportExport/ExpenseExporter.php @@ -44,15 +44,13 @@ class ExpenseExporter extends ImportExport $ret['airline'] = $expense->airline->icao; } - $ret['type'] = ExpenseType::convertToCode($ret['type']); - // For the different expense types, instead of exporting // the ID, export a specific column if ($expense->ref_model === Expense::class) { $ret['ref_model'] = ''; $ret['ref_model_id'] = ''; } else { - $obj = $expense->getReference(); + $obj = $expense->getReferencedObject(); if(!$obj) { // bail out return $ret; } diff --git a/app/Services/ImportExport/ExpenseImporter.php b/app/Services/ImportExport/ExpenseImporter.php index 34bc024c..2f84ed34 100644 --- a/app/Services/ImportExport/ExpenseImporter.php +++ b/app/Services/ImportExport/ExpenseImporter.php @@ -49,7 +49,6 @@ class ExpenseImporter extends ImportExport # Figure out what this is referring to $row = $this->getRefClassInfo($row); - $row['type'] = ExpenseType::getFromCode($row['type']); if(!$row['active']) { $row['active'] = true; } diff --git a/resources/lang/en/system.php b/resources/lang/en/system.php index b62c0e1e..f5c29799 100644 --- a/resources/lang/en/system.php +++ b/resources/lang/en/system.php @@ -2,6 +2,11 @@ return [ + 'global' => [ + 'active' => 'Active', + 'inactive' => 'Inactive' + ], + 'aircraft' => [ 'status' => [ 'active' => 'Active', @@ -22,6 +27,14 @@ return [ 'sun' => 'Sunday', ], + 'expenses' => [ + 'type' => [ + 'flight' => 'Flight', + 'daily' => 'Daily', + 'monthly' => 'Monthly', + ], + ], + 'flights' => [ 'type' => [ 'pass_scheduled' => 'Passenger - Scheduled', diff --git a/tests/FinanceTest.php b/tests/FinanceTest.php index e9777d95..197a0465 100644 --- a/tests/FinanceTest.php +++ b/tests/FinanceTest.php @@ -603,18 +603,25 @@ class FinanceTest extends TestCase * Test the subfleet class */ + $subfleet = factory(App\Models\Subfleet::class)->create(); factory(App\Models\Expense::class)->create([ 'airline_id' => null, 'ref_model' => \App\Models\Subfleet::class, + 'ref_model_id' => $subfleet->id, ]); $expenses = $this->expenseRepo->getAllForType( ExpenseType::FLIGHT, $airline->id, - \App\Models\Subfleet::class + $subfleet ); $this->assertCount(1, $expenses); + + $expense = $expenses->random(); + $this->assertEquals(\App\Models\Subfleet::class, $expense->ref_model); + $obj = $expense->getReferencedObject(); + $this->assertEquals($obj->id, $expense->ref_model_id); } /**