From 14aacdfb7586fce08c95c8fba27575aafe5d4db7 Mon Sep 17 00:00:00 2001 From: Nabeel S Date: Tue, 25 Feb 2020 14:45:23 -0500 Subject: [PATCH] Import expense and ledger entries #443 (#588) * Fix rank importing with PIREP pay #443 * Import ledger information #443 * Uncomment out testing importers * Import expense log and settings #443 * Formatting --- app/Services/FinanceService.php | 10 ++- app/Support/Money.php | 6 +- modules/Importer/Services/BaseImporter.php | 6 ++ modules/Importer/Services/ImporterService.php | 6 ++ .../Services/Importers/ClearDatabase.php | 2 + .../Services/Importers/ExpenseImporter.php | 48 ++++++++++++ .../Services/Importers/ExpenseLogImporter.php | 50 +++++++++++++ .../Services/Importers/LedgerImporter.php | 75 +++++++++++++++++++ .../Services/Importers/RankImport.php | 2 +- .../Services/Importers/SettingsImporter.php | 31 ++++++++ modules/Importer/Utils/ImporterDB.php | 21 ++++++ 11 files changed, 251 insertions(+), 6 deletions(-) create mode 100644 modules/Importer/Services/Importers/ExpenseImporter.php create mode 100644 modules/Importer/Services/Importers/ExpenseLogImporter.php create mode 100644 modules/Importer/Services/Importers/LedgerImporter.php create mode 100644 modules/Importer/Services/Importers/SettingsImporter.php diff --git a/app/Services/FinanceService.php b/app/Services/FinanceService.php index 13372431..489b8eba 100644 --- a/app/Services/FinanceService.php +++ b/app/Services/FinanceService.php @@ -37,6 +37,7 @@ class FinanceService extends Service * @param string $memo * @param string $transaction_group * @param string|array $tag + * @param string $post_date * * @throws \Prettus\Validator\Exceptions\ValidatorException * @@ -48,7 +49,8 @@ class FinanceService extends Service $reference, $memo, $transaction_group, - $tag + $tag, + $post_date = null ) { return $this->journalRepo->post( $journal, @@ -72,6 +74,7 @@ class FinanceService extends Service * @param string $memo * @param string $transaction_group * @param string|array $tag + * @param string $post_date * * @throws \Prettus\Validator\Exceptions\ValidatorException * @@ -83,7 +86,8 @@ class FinanceService extends Service $reference, $memo, $transaction_group, - $tag + $tag, + $post_date = null ) { return $this->journalRepo->post( $journal, @@ -91,7 +95,7 @@ class FinanceService extends Service $amount, $reference, $memo, - null, + $post_date, $transaction_group, $tag ); diff --git a/app/Support/Money.php b/app/Support/Money.php index d01d3e8d..d243521f 100644 --- a/app/Support/Money.php +++ b/app/Support/Money.php @@ -16,7 +16,9 @@ class Money public static $subunit_multiplier; /** - * @param mixed $amount + * Create a new Money instance, passing in the amount in pennies + * + * @param mixed $amount The amount, in pennies * * @throws \UnexpectedValueException * @throws \InvalidArgumentException @@ -31,7 +33,7 @@ class Money /** * Create from a dollar amount * - * @param mixed $amount + * @param mixed $amount The amount in pennies * * @throws \UnexpectedValueException * @throws \InvalidArgumentException diff --git a/modules/Importer/Services/BaseImporter.php b/modules/Importer/Services/BaseImporter.php index 8d97e380..eace47c5 100644 --- a/modules/Importer/Services/BaseImporter.php +++ b/modules/Importer/Services/BaseImporter.php @@ -72,6 +72,12 @@ abstract class BaseImporter implements ShouldQueue { $manifest = []; + // Ensure that the table exists; if it doesn't skip it from the manifest + if (!$this->db->tableExists($this->table)) { + Log::info('Table '.$this->table.' doesn\'t exist'); + return []; + } + $start = 0; $total_rows = $this->db->getTotalRows($this->table); Log::info('Found '.$total_rows.' rows for '.$this->table); diff --git a/modules/Importer/Services/ImporterService.php b/modules/Importer/Services/ImporterService.php index f7e79371..82571156 100644 --- a/modules/Importer/Services/ImporterService.php +++ b/modules/Importer/Services/ImporterService.php @@ -11,11 +11,14 @@ use Modules\Importer\Services\Importers\AircraftImporter; use Modules\Importer\Services\Importers\AirlineImporter; use Modules\Importer\Services\Importers\AirportImporter; use Modules\Importer\Services\Importers\ClearDatabase; +use Modules\Importer\Services\Importers\ExpenseImporter; use Modules\Importer\Services\Importers\FinalizeImporter; use Modules\Importer\Services\Importers\FlightImporter; use Modules\Importer\Services\Importers\GroupImporter; +use Modules\Importer\Services\Importers\LedgerImporter; use Modules\Importer\Services\Importers\PirepImporter; use Modules\Importer\Services\Importers\RankImport; +use Modules\Importer\Services\Importers\SettingsImporter; use Modules\Importer\Services\Importers\UserImport; class ImporterService extends Service @@ -40,6 +43,9 @@ class ImporterService extends Service FlightImporter::class, UserImport::class, PirepImporter::class, + ExpenseImporter::class, + LedgerImporter::class, + SettingsImporter::class, FinalizeImporter::class, ]; diff --git a/modules/Importer/Services/Importers/ClearDatabase.php b/modules/Importer/Services/Importers/ClearDatabase.php index e637e4ca..dc4a86cf 100644 --- a/modules/Importer/Services/Importers/ClearDatabase.php +++ b/modules/Importer/Services/Importers/ClearDatabase.php @@ -14,6 +14,7 @@ use App\Models\FlightField; use App\Models\FlightFieldValue; use App\Models\Journal; use App\Models\JournalTransaction; +use App\Models\Ledger; use App\Models\News; use App\Models\Pirep; use App\Models\Role; @@ -62,6 +63,7 @@ class ClearDatabase extends BaseImporter Expense::truncate(); JournalTransaction::truncate(); Journal::truncate(); + Ledger::truncate(); // Clear flights DB::table('flight_fare')->truncate(); diff --git a/modules/Importer/Services/Importers/ExpenseImporter.php b/modules/Importer/Services/Importers/ExpenseImporter.php new file mode 100644 index 00000000..dad95dd2 --- /dev/null +++ b/modules/Importer/Services/Importers/ExpenseImporter.php @@ -0,0 +1,48 @@ + ExpenseType::MONTHLY, + 'F' => ExpenseType::FLIGHT, + 'P' => ExpenseType::MONTHLY, // percent, monthly + 'G' => ExpenseType::FLIGHT, // percent, per-flight + ]; + + /** + * {@inheritdoc} + */ + public function run($start = 0) + { + $this->comment('--- EXPENSES IMPORT ---'); + + $count = 0; + $rows = $this->db->readRows($this->table, $this->idField, $start); + foreach ($rows as $row) { + $attrs = [ + 'airline_id' => null, + 'name' => $row->name, + 'amount' => $row->amount, + 'type' => $this->expense_types[$row->type], + 'active' => 1, + 'ref_model' => Expense::class, + ]; + + $expense = Expense::updateOrCreate(['name' => $row->name], $attrs); + $this->idMapper->addMapping('expenses', $row->id, $expense->id); + $this->idMapper->addMapping('expenses', $row->name, $expense->id); + + $count++; + } + + $this->info('Imported '.$count.' expenses'); + } +} diff --git a/modules/Importer/Services/Importers/ExpenseLogImporter.php b/modules/Importer/Services/Importers/ExpenseLogImporter.php new file mode 100644 index 00000000..9d05b00d --- /dev/null +++ b/modules/Importer/Services/Importers/ExpenseLogImporter.php @@ -0,0 +1,50 @@ +comment('--- EXPENSE LOG IMPORT ---'); + + /** @var FinanceService $financeSvc */ + $financeSvc = app(FinanceService::class); + $airline = Airline::first(); + + $count = 0; + $rows = $this->db->readRows($this->table, $this->idField, $start); + foreach ($rows as $row) { + $expense_id = $this->idMapper->getMapping('expenses', $row->name); + $expense = Expense::find($expense_id); + + $debit = Money::createFromAmount($expense->amount); + + $financeSvc->debitFromJournal( + $airline->journal, + $debit, + $airline, + 'Expense: '.$expense->name, + $expense->transaction_group ?? 'Expenses', + 'expense' + ); + + $count++; + } + + $this->info('Imported '.$count.' expense logs'); + } +} diff --git a/modules/Importer/Services/Importers/LedgerImporter.php b/modules/Importer/Services/Importers/LedgerImporter.php new file mode 100644 index 00000000..eb4fd58e --- /dev/null +++ b/modules/Importer/Services/Importers/LedgerImporter.php @@ -0,0 +1,75 @@ + Pirep::class, + ]; + + /** + * {@inheritdoc} + * + * @throws \Prettus\Validator\Exceptions\ValidatorException + */ + public function run($start = 0) + { + if (!$this->db->tableExists('ledger')) { + return; + } + + $this->comment('--- LEDGER IMPORT ---'); + + /** @var FinanceService $financeSvc */ + $financeSvc = app(FinanceService::class); + + $count = 0; + $rows = $this->db->readRows($this->table, $this->idField, $start); + foreach ($rows as $row) { + $pirep = Pirep::find($this->idMapper->getMapping('pireps', $row->pirepid)); + if (!$pirep) { + continue; + } + + $pilot_pay = Money::createFromAmount($row->amount * 100); + $memo = 'Pilot payment'; + + $financeSvc->debitFromJournal( + $pirep->airline->journal, + $pilot_pay, + $pirep, + $memo, + 'Pilot Pay', + 'pilot_pay', + $row->submitdate + ); + + $financeSvc->creditToJournal( + $pirep->user->journal, + $pilot_pay, + $pirep, + $memo, + 'Pilot Pay', + 'pilot_pay', + $row->submitdate + ); + + $count++; + } + + $this->info('Imported '.$count.' ledger entries'); + } +} diff --git a/modules/Importer/Services/Importers/RankImport.php b/modules/Importer/Services/Importers/RankImport.php index c587a069..209a6af7 100644 --- a/modules/Importer/Services/Importers/RankImport.php +++ b/modules/Importer/Services/Importers/RankImport.php @@ -17,7 +17,7 @@ class RankImport extends BaseImporter $count = 0; $rows = $this->db->readRows($this->table, $this->idField, $start); foreach ($rows as $row) { - $rank = Rank::firstOrCreate(['name' => $row->rank], [ + $rank = Rank::updateOrCreate(['name' => $row->rank], [ 'image_url' => $row->rankimage, 'hours' => $row->minhours, 'acars_base_payrate' => $row->payrate, diff --git a/modules/Importer/Services/Importers/SettingsImporter.php b/modules/Importer/Services/Importers/SettingsImporter.php new file mode 100644 index 00000000..34da3a99 --- /dev/null +++ b/modules/Importer/Services/Importers/SettingsImporter.php @@ -0,0 +1,31 @@ +comment('--- SETTINGS IMPORT ---'); + + /** @var SettingRepository $settingsRepo */ + $settingsRepo = app(SettingRepository::class); + + $count = 0; + $rows = $this->db->readRows($this->table, $this->idField, $start); + foreach ($rows as $row) { + switch ($row->name) { + case 'ADMIN_EMAIL': + $settingsRepo->store('general.admin_email', $row->value); + break; + } + } + + $this->info('Imported '.$count.' settings'); + } +} diff --git a/modules/Importer/Utils/ImporterDB.php b/modules/Importer/Utils/ImporterDB.php index 3716966c..fa9211b9 100644 --- a/modules/Importer/Utils/ImporterDB.php +++ b/modules/Importer/Utils/ImporterDB.php @@ -85,6 +85,27 @@ class ImporterDB return $table; } + /** + * Does a table exist? Try to get the column information on it. + * The result will be 'false' if that table isn't there + * + * @param $table + * + * @return bool + */ + public function tableExists($table): bool + { + $this->connect(); + + $sql = 'SHOW COLUMNS FROM '.$this->tableName($table); + $result = $this->conn->query($sql); + if (!$result) { + return false; + } + + return true; + } + /** * Get the names of the columns for a particular table *