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;
|
2022-07-21 01:07:55 +03:00
|
|
|
use App\Models\Banking\Transaction;
|
2020-10-01 18:20:32 +03:00
|
|
|
use App\Models\Banking\Transfer as Model;
|
2022-07-21 01:07:55 +03:00
|
|
|
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;
|
2022-06-09 18:16:29 +03:00
|
|
|
use App\Traits\Transactions;
|
2020-10-08 15:22:13 +03:00
|
|
|
use App\Utilities\Date;
|
2020-10-01 18:20:32 +03:00
|
|
|
|
|
|
|
class Transfers extends Import
|
|
|
|
{
|
2022-07-21 01:07:55 +03:00
|
|
|
use Categories, Currencies, Jobs, Transactions;
|
2020-10-01 18:20:32 +03:00
|
|
|
|
|
|
|
public function model(array $row)
|
|
|
|
{
|
2021-02-26 17:16:48 +03:00
|
|
|
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');
|
2022-07-19 12:09:04 +03:00
|
|
|
$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);
|
2022-07-21 01:07:55 +03:00
|
|
|
|
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',
|
2023-06-19 17:46:57 +03:00
|
|
|
'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',
|
2023-06-19 17:46:57 +03:00
|
|
|
'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'],
|
2022-06-09 18:16:29 +03:00
|
|
|
'number' => $this->getNextTransactionNumber(),
|
2022-07-21 01:07:55 +03:00
|
|
|
'type' => Transaction::EXPENSE_TRANSFER_TYPE,
|
2021-02-26 15:38:45 +03:00
|
|
|
'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'],
|
2022-07-21 01:07:55 +03:00
|
|
|
'category_id' => $this->getTransferCategoryId(),
|
2021-02-26 15:38:45 +03:00
|
|
|
'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'],
|
2022-06-09 18:16:29 +03:00
|
|
|
'number' => $this->getNextTransactionNumber(),
|
2022-07-21 01:07:55 +03:00
|
|
|
'type' => Transaction::INCOME_TRANSFER_TYPE,
|
2021-02-26 15:38:45 +03:00
|
|
|
'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'],
|
2022-07-21 01:07:55 +03:00
|
|
|
'category_id' => $this->getTransferCategoryId(),
|
2021-02-26 15:38:45 +03:00
|
|
|
'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);
|
|
|
|
}
|
2022-07-19 12:09:04 +03:00
|
|
|
|
|
|
|
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
|
|
|
}
|