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
This commit is contained in:
Nabeel S 2020-02-25 14:45:23 -05:00 committed by GitHub
parent 07a75de0bf
commit 14aacdfb75
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
11 changed files with 251 additions and 6 deletions

View File

@ -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
);

View File

@ -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

View File

@ -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);

View File

@ -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,
];

View File

@ -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();

View File

@ -0,0 +1,48 @@
<?php
namespace Modules\Importer\Services\Importers;
use App\Models\Enums\ExpenseType;
use App\Models\Expense;
use Modules\Importer\Services\BaseImporter;
class ExpenseImporter extends BaseImporter
{
protected $table = 'expenses';
private $expense_types = [
'M' => 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');
}
}

View File

@ -0,0 +1,50 @@
<?php
namespace Modules\Importer\Services\Importers;
use App\Models\Airline;
use App\Models\Expense;
use App\Services\FinanceService;
use App\Support\Money;
use Modules\Importer\Services\BaseImporter;
class ExpenseLogImporter extends BaseImporter
{
protected $table = 'expenselog';
/**
* {@inheritdoc}
*
* @throws \Prettus\Validator\Exceptions\ValidatorException
*/
public function run($start = 0)
{
$this->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');
}
}

View File

@ -0,0 +1,75 @@
<?php
namespace Modules\Importer\Services\Importers;
use App\Models\Pirep;
use App\Services\FinanceService;
use App\Support\Money;
use Modules\Importer\Services\BaseImporter;
class LedgerImporter extends BaseImporter
{
protected $table = 'ledger';
/**
* Legacy ID to the current ledger ref_model mapping
*
* @var array
*/
private static $legacy_paysource = [
1 => 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');
}
}

View File

@ -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,

View File

@ -0,0 +1,31 @@
<?php
namespace Modules\Importer\Services\Importers;
use App\Repositories\SettingRepository;
use Modules\Importer\Services\BaseImporter;
class SettingsImporter extends BaseImporter
{
protected $table = 'settings';
public function run($start = 0)
{
$this->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');
}
}

View File

@ -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
*