phpvms/app/Services/Finance/RecurringFinanceService.php

151 lines
4.5 KiB
PHP
Raw Normal View History

<?php
namespace App\Services\Finance;
use App\Contracts\Service;
use App\Models\Airline;
use App\Models\Enums\ExpenseType;
use App\Models\Expense;
use App\Models\JournalTransaction;
use App\Repositories\JournalRepository;
use App\Support\Money;
use Log;
/**
* Process all of the daily expenses and charge them
*/
class RecurringFinanceService extends Service
{
private $journalRepo;
/**
* RecurringFinanceService constructor.
2018-08-27 00:40:04 +08:00
*
* @param JournalRepository $journalRepo
*/
public function __construct(JournalRepository $journalRepo)
{
$this->journalRepo = $journalRepo;
}
/**
* Determine the journal to charge to, otherwise, it's charged
* to every airline journal
2018-08-27 00:40:04 +08:00
*
* @param Expense $expense
2018-08-27 00:40:04 +08:00
*
* @return \Generator
*/
protected function findJournals(Expense $expense)
{
if ($expense->airline_id) {
$airline = Airline::find($expense->airline_id)->first(['id', 'icao']);
Log::info('Charging to '.$airline->icao);
yield $airline->journal;
} else {
$airlines = Airline::all(['id', 'icao']);
foreach ($airlines as $airline) {
Log::info('Charging to '.$airline->icao);
yield $airline->journal;
}
}
}
/**
* Get the name of the transaction group from the expense
2018-08-27 00:40:04 +08:00
*
* @param Expense $expense
2018-08-27 00:40:04 +08:00
*
* @return array
*/
protected function getMemoAndGroup(Expense $expense): array
{
$klass = 'Expense';
2018-04-02 03:32:01 +08:00
if ($expense->ref_model) {
$ref = explode('\\', $expense->ref_model);
$klass = end($ref);
$obj = $expense->getReferencedObject();
}
if ($klass === 'Airport') {
2018-04-02 03:32:01 +08:00
$memo = "Airport Expense: {$expense->name} ({$expense->ref_model_id})";
$transaction_group = "Airport: {$expense->ref_model_id}";
} elseif ($klass === 'Subfleet') {
$memo = "Subfleet Expense: {$expense->name}";
$transaction_group = "Subfleet: {$expense->name}";
} elseif ($klass === 'Aircraft') {
$memo = "Aircraft Expense: {$expense->name} ({$obj->name})";
$transaction_group = "Aircraft: {$expense->name} ({$obj->name}-{$obj->registration})";
} else {
$memo = "Expense: {$expense->name}";
$transaction_group = "Expense: {$expense->name}";
}
return [$memo, $transaction_group];
}
/**
* Run all of the daily expense/financials
2018-08-27 00:40:04 +08:00
*
* @param int $type
2018-08-27 00:40:04 +08:00
*
* @throws \UnexpectedValueException
* @throws \InvalidArgumentException
* @throws \Prettus\Validator\Exceptions\ValidatorException
*/
public function processExpenses($type = ExpenseType::DAILY): void
{
$expenses = Expense::where(['type' => $type])->get();
$tag = 'expense_recurring';
if ($type === ExpenseType::DAILY) {
$tag = 'expenses_daily';
} elseif ($type === ExpenseType::MONTHLY) {
$tag === 'expenses_monthly';
}
/**
* @var $expenses Expense[]
*/
foreach ($expenses as $expense) {
2018-08-27 00:40:04 +08:00
// Apply the expenses to the appropriate journals
$journals = $this->findJournals($expense);
foreach ($journals as $journal) {
$amount = $expense->amount;
2018-08-27 00:40:04 +08:00
// Has this expense already been charged? Check
// against this specific journal, on today
$w = [
'journal_id' => $journal->id,
2018-04-02 03:32:01 +08:00
'ref_model' => Expense::class,
'ref_model_id' => $expense->id,
];
$found = JournalTransaction::where($w)
->whereDate('post_date', '=', \Carbon::now('UTC')->toDateString())
->count(['id']);
if ($found > 0) {
Log::info('Expense "'.$expense->name.'" already charged for today, skipping');
continue;
}
[$memo, $ta_group] = $this->getMemoAndGroup($expense);
$this->journalRepo->post(
$journal,
null,
Money::createFromAmount($amount),
$expense,
$memo,
null,
$ta_group,
$tag
);
Log::info('Expense memo: "'.$memo.'"; group: "'.$ta_group.'" charged!');
}
}
}
}