akaunting/app/Imports/Banking/Transfers.php

147 lines
5.0 KiB
PHP
Raw Permalink Normal View History

2020-10-01 18:20:32 +03:00
<?php
namespace App\Imports\Banking;
use App\Abstracts\Import;
2021-09-06 12:15:56 +03:00
use App\Jobs\Banking\CreateTransaction;
use App\Models\Banking\Transaction;
2020-10-01 18:20:32 +03:00
use App\Models\Banking\Transfer as Model;
use App\Traits\Categories;
2020-10-01 18:20:32 +03:00
use App\Traits\Currencies;
2021-09-06 12:15:56 +03:00
use App\Traits\Jobs;
use App\Traits\Transactions;
use App\Utilities\Date;
2020-10-01 18:20:32 +03:00
class Transfers extends Import
{
use Categories, Currencies, Jobs, Transactions;
2020-10-01 18:20:32 +03:00
public function model(array $row)
{
return new Model($row);
2020-10-01 18:20:32 +03:00
}
public function map($row): array
{
$row = parent::map($row);
2021-02-11 13:46:14 +03:00
$row['transferred_at'] = Date::parse($row['transferred_at'])->format('Y-m-d');
$row['from_currency_code'] = $this->getFromCurrencyCode($row);
$row['to_currency_code'] = $this->getToCurrencyCode($row);
2023-03-07 15:18:14 +03:00
$row['from_account_id'] = $this->getFromAccountId($row);
$row['to_account_id'] = $this->getToAccountId($row);
2020-10-01 18:20:32 +03:00
$row['expense_transaction_id'] = $this->getExpenseTransactionId($row);
$row['income_transaction_id'] = $this->getIncomeTransactionId($row);
2020-10-01 18:20:32 +03:00
return $row;
}
public function rules(): array
{
return [
'from_account_id' => 'required|integer',
'from_currency_code' => 'required|string|currency',
'from_currency_rate' => 'required|gt:0',
2020-10-01 18:20:32 +03:00
'to_account_id' => 'required|integer',
'to_currency_code' => 'required|string|currency',
'to_currency_rate' => 'required|gt:0',
2020-10-01 18:20:32 +03:00
'amount' => 'required|amount',
'transferred_at' => 'required|date_format:Y-m-d',
'payment_method' => 'required|string',
];
}
private function getExpenseTransactionId($row)
{
2021-09-06 12:15:56 +03:00
$expense_transaction = $this->dispatch(new CreateTransaction([
'company_id' => $row['company_id'],
'number' => $this->getNextTransactionNumber(),
'type' => Transaction::EXPENSE_TRANSFER_TYPE,
'account_id' => $row['from_account_id'],
'paid_at' => $row['transferred_at'],
'currency_code' => $row['from_currency_code'],
'currency_rate' => $row['from_currency_rate'],
'amount' => $row['amount'],
'contact_id' => 0,
'description' => $row['description'],
'category_id' => $this->getTransferCategoryId(),
'payment_method' => $row['payment_method'],
'reference' => $row['reference'],
2021-09-06 12:15:56 +03:00
'created_by' => $row['created_by'],
]));
2020-10-01 18:20:32 +03:00
return $expense_transaction->id;
}
private function getIncomeTransactionId($row)
{
$amount = $row['amount'];
2021-09-06 12:15:56 +03:00
2020-10-01 18:20:32 +03:00
// Convert amount if not same currency
if ($row['from_currency_code'] !== $row['to_currency_code']) {
$amount = $this->convertBetween(
$amount,
$row['from_currency_code'],
$row['from_currency_rate'],
$row['to_currency_code'],
$row['to_currency_rate']
);
}
2021-09-06 12:15:56 +03:00
$income_transaction = $this->dispatch(new CreateTransaction([
'company_id' => $row['company_id'],
'number' => $this->getNextTransactionNumber(),
'type' => Transaction::INCOME_TRANSFER_TYPE,
'account_id' => $row['to_account_id'],
'paid_at' => $row['transferred_at'],
'currency_code' => $row['to_currency_code'],
'currency_rate' => $row['to_currency_rate'],
'amount' => $amount,
'contact_id' => 0,
'description' => $row['description'],
'category_id' => $this->getTransferCategoryId(),
'payment_method' => $row['payment_method'],
'reference' => $row['reference'],
2021-09-06 12:15:56 +03:00
'created_by' => $row['created_by'],
]));
2020-10-01 18:20:32 +03:00
return $income_transaction->id;
}
private function getFromAccountId($row)
{
$row['account_id'] = $row['from_account_id'] ?? null;
$row['account_name'] = $row['from_account_name'] ?? null;
$row['account_number'] = $row['from_account_number'] ?? null;
$row['currency_code'] = $row['from_currency_code'] ?? null;
return $this->getAccountId($row);
}
private function getToAccountId($row)
{
$row['account_id'] = $row['to_account_id'] ?? null;
$row['account_name'] = $row['to_account_name'] ?? null;
$row['account_number'] = $row['to_account_number'] ?? null;
$row['currency_code'] = $row['to_currency_code'] ?? null;
return $this->getAccountId($row);
}
private function getFromCurrencyCode($row)
{
$row['currency_code'] = $row['from_currency_code'] ?? null;
$row['currency_rate'] = $row['from_currency_rate'] ?? null;
return $this->getCurrencyCode($row);
}
private function getToCurrencyCode($row)
{
$row['currency_code'] = $row['to_currency_code'] ?? null;
$row['currency_rate'] = $row['to_currency_rate'] ?? null;
return $this->getCurrencyCode($row);
}
2020-10-01 18:20:32 +03:00
}