2020-02-26 03:45:23 +08:00
|
|
|
<?php
|
|
|
|
|
2020-10-19 22:10:28 +08:00
|
|
|
namespace App\Services\Importers;
|
2020-02-26 03:45:23 +08:00
|
|
|
|
|
|
|
use App\Models\Airline;
|
|
|
|
use App\Models\Expense;
|
|
|
|
use App\Services\FinanceService;
|
|
|
|
use App\Support\Money;
|
2020-10-19 22:10:28 +08:00
|
|
|
use Prettus\Validator\Exceptions\ValidatorException;
|
2020-02-26 03:45:23 +08:00
|
|
|
|
|
|
|
class ExpenseLogImporter extends BaseImporter
|
|
|
|
{
|
|
|
|
protected $table = 'expenselog';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* {@inheritdoc}
|
|
|
|
*
|
2020-10-19 22:10:28 +08:00
|
|
|
* @throws ValidatorException
|
2020-02-26 03:45:23 +08:00
|
|
|
*/
|
|
|
|
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');
|
|
|
|
}
|
|
|
|
}
|