Refactor and add importer to Installer module #443 (#444)

* Refactor and add importer to Installer module #443

* Refactor for finances to use in import

* Import groups into roles

* Formatting

* Formatting

* Add interface in installer for import

* Notes about importing

* Check for installer folder

* Formatting

* Fix pirep->user mapping

* Unused import

* Formatting
This commit is contained in:
Nabeel S
2019-11-27 09:19:20 -05:00
committed by GitHub
parent f95a3f336b
commit 50dc79bc8d
76 changed files with 2753 additions and 1431 deletions

View File

@@ -11,17 +11,16 @@ use App\Models\Pirep;
use App\Repositories\ExpenseRepository;
use App\Repositories\JournalRepository;
use App\Services\FareService;
use App\Services\FinanceService;
use App\Support\Math;
use App\Support\Money;
use Illuminate\Support\Facades\Log;
/**
* Class FinanceService
*/
class PirepFinanceService extends Service
{
private $expenseRepo;
private $fareSvc;
private $financeSvc;
private $journalRepo;
/**
@@ -30,15 +29,18 @@ class PirepFinanceService extends Service
* @param ExpenseRepository $expenseRepo
* @param FareService $fareSvc
* @param JournalRepository $journalRepo
* @param FinanceService $financeSvc
*/
public function __construct(
ExpenseRepository $expenseRepo,
FareService $fareSvc,
FinanceService $financeSvc,
JournalRepository $journalRepo
) {
$this->expenseRepo = $expenseRepo;
$this->fareSvc = $fareSvc;
$this->journalRepo = $journalRepo;
$this->financeSvc = $financeSvc;
}
/**
@@ -149,13 +151,11 @@ class PirepFinanceService extends Service
Log::info('Finance: Fuel cost, (fuel='.$fuel_used.', cost='.$ap->fuel_jeta_cost.') D='
.$debit->getAmount());
$this->journalRepo->post(
$this->financeSvc->debitFromJournal(
$pirep->airline->journal,
null,
$debit,
$pirep,
'Fuel Cost ('.$ap->fuel_jeta_cost.'/'.config('phpvms.internal_units.fuel').')',
null,
'Fuel',
'fuel'
);
@@ -194,13 +194,11 @@ class PirepFinanceService extends Service
$debit = Money::createFromAmount($cost_per_min * $block_time);
Log::info('Finance: Subfleet Block Hourly, D='.$debit->getAmount());
$this->journalRepo->post(
$this->financeSvc->debitFromJournal(
$pirep->airline->journal,
null,
$debit,
$pirep,
'Subfleet '.$sf->type.': Block Time Cost',
null,
'Subfleet '.$sf->type,
'subfleet'
);
@@ -265,13 +263,11 @@ class PirepFinanceService extends Service
$journal = $pirep->user->journal;
}
$this->journalRepo->post(
$this->financeSvc->debitFromJournal(
$journal,
null,
$debit,
$pirep,
$memo,
null,
$transaction_group,
strtolower($klass)
);
@@ -320,13 +316,11 @@ class PirepFinanceService extends Service
$debit = Money::createFromAmount($expense->amount);
$this->journalRepo->post(
$this->financeSvc->debitFromJournal(
$pirep->airline->journal,
null,
$debit,
$pirep,
'Expense: '.$expense->name,
null,
$expense->transaction_group ?? 'Expenses',
'expense'
);
@@ -347,13 +341,12 @@ class PirepFinanceService extends Service
{
$ground_handling_cost = $this->getGroundHandlingCost($pirep);
Log::info('Finance: PIREP: '.$pirep->id.'; ground handling: '.$ground_handling_cost);
$this->journalRepo->post(
$this->financeSvc->debitFromJournal(
$pirep->airline->journal,
null,
Money::createFromAmount($ground_handling_cost),
$pirep,
'Ground Handling',
null,
'Ground Handling',
'ground_handling'
);
@@ -378,24 +371,20 @@ class PirepFinanceService extends Service
Log::info('Finance: PIREP: '.$pirep->id
.'; pilot pay: '.$pilot_pay_rate.', total: '.$pilot_pay);
$this->journalRepo->post(
$this->financeSvc->debitFromJournal(
$pirep->airline->journal,
null,
$pilot_pay,
$pirep,
$memo,
null,
'Pilot Pay',
'pilot_pay'
);
$this->journalRepo->post(
$this->financeSvc->creditToJournal(
$pirep->user->journal,
$pilot_pay,
null,
$pirep,
$memo,
null,
'Pilot Pay',
'pilot_pay'
);

View File

@@ -8,23 +8,22 @@ use App\Models\Enums\ExpenseType;
use App\Models\Expense;
use App\Models\JournalTransaction;
use App\Repositories\JournalRepository;
use App\Services\FinanceService;
use App\Support\Money;
use Log;
use Carbon\Carbon;
use Illuminate\Support\Facades\Log;
/**
* Process all of the daily expenses and charge them
*/
class RecurringFinanceService extends Service
{
private $financeSvc;
private $journalRepo;
/**
* RecurringFinanceService constructor.
*
* @param JournalRepository $journalRepo
*/
public function __construct(JournalRepository $journalRepo)
public function __construct(JournalRepository $journalRepo, FinanceService $financeSvc)
{
$this->financeSvc = $financeSvc;
$this->journalRepo = $journalRepo;
}
@@ -87,10 +86,8 @@ class RecurringFinanceService extends Service
/**
* Run all of the daily expense/financials
*
* @param int $type
* @param string $type
*
* @throws \UnexpectedValueException
* @throws \InvalidArgumentException
* @throws \Prettus\Validator\Exceptions\ValidatorException
*/
public function processExpenses($type = ExpenseType::DAILY): void
@@ -101,7 +98,7 @@ class RecurringFinanceService extends Service
if ($type === ExpenseType::DAILY) {
$tag = 'expenses_daily';
} elseif ($type === ExpenseType::MONTHLY) {
$tag === 'expenses_monthly';
$tag = 'expenses_monthly';
}
/**
@@ -122,7 +119,7 @@ class RecurringFinanceService extends Service
];
$found = JournalTransaction::where($w)
->whereDate('post_date', '=', \Carbon::now('UTC')->toDateString())
->whereDate('post_date', '=', Carbon::now('UTC')->toDateString())
->count(['id']);
if ($found > 0) {
@@ -132,13 +129,11 @@ class RecurringFinanceService extends Service
[$memo, $ta_group] = $this->getMemoAndGroup($expense);
$this->journalRepo->post(
$this->financeSvc->debitFromJournal(
$journal,
null,
Money::createFromAmount($amount),
$expense,
$memo,
null,
$ta_group,
$tag
);

View File

@@ -4,11 +4,93 @@ namespace App\Services;
use App\Contracts\Service;
use App\Models\Airline;
use App\Models\Journal;
use App\Models\JournalTransaction;
use App\Repositories\JournalRepository;
use App\Support\Money;
class FinanceService extends Service
{
private $journalRepo;
public function __construct(JournalRepository $journalRepo)
{
$this->journalRepo = $journalRepo;
}
/**
* Credit some amount to a given journal
* E.g, some amount for expenses or ground handling fees, etc. Example, to pay a user a dollar
* for a pirep:
*
* creditToJournal($user->journal, new Money(1000), $pirep, 'Payment', 'pirep', 'payment');
*
* @param \App\Models\Journal $journal
* @param Money $amount
* @param \Illuminate\Database\Eloquent\Model $reference
* @param string $memo
* @param string $transaction_group
* @param string|array $tag
*
* @throws \Prettus\Validator\Exceptions\ValidatorException
*
* @return mixed
*/
public function creditToJournal(
Journal $journal,
Money $amount,
$reference,
$memo,
$transaction_group,
$tag
) {
return $this->journalRepo->post(
$journal,
$amount,
null,
$reference,
$memo,
null,
$transaction_group,
$tag
);
}
/**
* Charge some expense for a given PIREP to the airline its file against
* E.g, some amount for expenses or ground handling fees, etc.
*
* @param \App\Models\Journal $journal
* @param Money $amount
* @param \Illuminate\Database\Eloquent\Model $reference
* @param string $memo
* @param string $transaction_group
* @param string|array $tag
*
* @throws \Prettus\Validator\Exceptions\ValidatorException
*
* @return mixed
*/
public function debitFromJournal(
Journal $journal,
Money $amount,
$reference,
$memo,
$transaction_group,
$tag
) {
return $this->journalRepo->post(
$journal,
null,
$amount,
$reference,
$memo,
null,
$transaction_group,
$tag
);
}
/**
* Get all of the transactions for an airline between two given dates. Returns an array
* with `credits`, `debits` and `transactions` fields, where transactions contains the

View File

@@ -236,6 +236,15 @@ class SeederService extends Service
return true;
}
// See if any of these column values have changed
foreach (['name', 'description'] as $column) {
$currVal = $row->{$column};
$newVal = $setting[$column];
if ($currVal !== $newVal) {
return true;
}
}
// See if any of the options have changed
if ($row->type === 'select') {
if ($row->options !== $setting['options']) {
@@ -259,10 +268,20 @@ class SeederService extends Service
$yml = Yaml::parse($data);
foreach ($yml as $perm) {
$count = DB::table('permissions')->where('name', $perm['name'])->count('name');
if ($count === 0) {
$row = DB::table('permissions')
->where('name', $perm['name'])
->first();
if (!$row) {
return true;
}
// See if any of these column values have changed
foreach (['display_name', 'description'] as $column) {
if ($row->{$column} !== $perm[$column]) {
return true;
}
}
}
return false;

View File

@@ -0,0 +1,47 @@
<?php
namespace App\Services;
use App\Contracts\Service;
use App\Models\Role;
use App\Repositories\RoleRepository;
class RoleService extends Service
{
private $roleRepo;
public function __construct(RoleRepository $roleRepo)
{
$this->roleRepo = $roleRepo;
}
/**
* Update a role with the given attributes
*
* @param Role $role
* @param array $attrs
*
* @return Role
*/
public function updateRole(Role $role, array $attrs)
{
$role->update($attrs);
$role->save();
return $role;
}
/**
* @param Role $role
* @param array $permissions
*/
public function setPermissionsForRole(Role $role, array $permissions)
{
// Update the permissions, filter out null/invalid values
$perms = collect($permissions)->filter(static function ($v, $k) {
return $v;
});
$role->permissions()->sync($perms);
}
}

View File

@@ -48,14 +48,14 @@ class UserService extends Service
* Register a pilot. Also attaches the initial roles
* required, and then triggers the UserRegistered event
*
* @param User $user User model
* @param array $groups Additional groups to assign
* @param User $user User model
* @param array $roles List of "display_name" of groups to assign
*
* @throws \Exception
*
* @return mixed
*/
public function createUser(User $user, array $groups = null)
public function createUser(User $user, array $roles = null)
{
// Determine if we want to auto accept
if (setting('pilots.auto_accept') === true) {
@@ -66,15 +66,10 @@ class UserService extends Service
$user->save();
// Attach the user roles
// $role = Role::where('name', 'user')->first();
// $user->attachRole($role);
// Attach any additional roles
if (!empty($groups) && is_array($groups)) {
foreach ($groups as $group) {
$role = Role::where('name', $group)->first();
$user->attachRole($role);
if (!empty($roles) && is_array($roles)) {
foreach ($roles as $role) {
$this->addUserToRole($user, $role);
}
}
@@ -87,6 +82,32 @@ class UserService extends Service
return $user;
}
/**
* Add a user to a given role
*
* @param User $user
* @param string $roleName
*
* @return User
*/
public function addUserToRole(User $user, $roleName): User
{
$role = Role::where('name', $roleName)->first();
$user->attachRole($role);
return $user;
}
/**
* Find and return the next available pilot ID (usually just the max+1)
*
* @return int
*/
public function getNextAvailablePilotId(): int
{
return (int) User::max('pilot_id') + 1;
}
/**
* Find the next available pilot ID and set the current user's pilot_id to that +1
* Called from UserObserver right now after a record is created
@@ -101,8 +122,7 @@ class UserService extends Service
return $user;
}
$max = (int) User::max('pilot_id');
$user->pilot_id = $max + 1;
$user->pilot_id = $this->getNextAvailablePilotId();
$user->save();
Log::info('Set pilot ID for user '.$user->id.' to '.$user->pilot_id);
@@ -348,8 +368,8 @@ class UserService extends Service
'state' => PirepState::ACCEPTED,
];
$flight_count = Pirep::where($w)->count();
$user->flights = $flight_count;
$pirep_count = Pirep::where($w)->count();
$user->flights = $pirep_count;
$flight_time = Pirep::where($w)->sum('flight_time');
$user->flight_time = $flight_time;
@@ -359,7 +379,7 @@ class UserService extends Service
// Recalc the rank
$this->calculatePilotRank($user);
Log::info('User '.$user->ident.' updated; flight count='.$flight_count
Log::info('User '.$user->ident.' updated; pirep count='.$pirep_count
.', rank='.$user->rank->name
.', flight_time='.$user->flight_time.' minutes');