akaunting/app/Jobs/Banking/CreateTransfer.php

116 lines
4.6 KiB
PHP
Raw Normal View History

2019-11-16 10:21:14 +03:00
<?php
namespace App\Jobs\Banking;
use App\Abstracts\Job;
2021-09-06 11:53:57 +03:00
use App\Interfaces\Job\HasOwner;
2021-09-07 10:33:34 +03:00
use App\Interfaces\Job\HasSource;
2021-09-06 11:53:57 +03:00
use App\Interfaces\Job\ShouldCreate;
use App\Jobs\Banking\CreateTransaction;
2019-11-16 10:21:14 +03:00
use App\Models\Banking\Account;
use App\Models\Banking\Transfer;
use App\Models\Banking\Transaction;
use App\Traits\Categories;
2020-07-06 09:54:18 +03:00
use App\Traits\Currencies;
2022-06-01 10:15:55 +03:00
use App\Traits\Transactions;
2019-11-16 10:21:14 +03:00
2021-09-07 10:33:34 +03:00
class CreateTransfer extends Job implements HasOwner, HasSource, ShouldCreate
2019-11-16 10:21:14 +03:00
{
use Categories, Currencies, Transactions;
2020-07-06 09:54:18 +03:00
2021-09-06 11:53:57 +03:00
public function handle(): Transfer
2019-11-16 10:21:14 +03:00
{
2020-06-26 13:40:19 +03:00
\DB::transaction(function () {
$expense_currency_code = $this->getCurrencyCode('from');
$income_currency_code = $this->getCurrencyCode('to');
2020-06-26 13:40:19 +03:00
$expense_currency_rate = $this->getCurrencyRate('from');
$income_currency_rate = $this->getCurrencyRate('to');
2020-07-06 09:54:18 +03:00
2021-09-06 11:53:57 +03:00
$expense_transaction = $this->dispatch(new CreateTransaction([
2020-06-26 13:40:19 +03:00
'company_id' => $this->request['company_id'],
'type' => Transaction::EXPENSE_TRANSFER_TYPE,
2022-06-01 10:15:55 +03:00
'number' => $this->getNextTransactionNumber(),
2020-06-26 13:40:19 +03:00
'account_id' => $this->request->get('from_account_id'),
'paid_at' => $this->request->get('transferred_at'),
'currency_code' => $expense_currency_code,
2020-07-06 09:54:18 +03:00
'currency_rate' => $expense_currency_rate,
2020-06-26 13:40:19 +03:00
'amount' => $this->request->get('amount'),
'contact_id' => 0,
'description' => $this->request->get('description'),
'category_id' => $this->getTransferCategoryId(),
2020-06-26 13:40:19 +03:00
'payment_method' => $this->request->get('payment_method'),
'reference' => $this->request->get('reference'),
2022-10-04 00:23:40 +03:00
'created_from' => $this->request->get('created_from'),
'created_by' => $this->request->get('created_by'),
2021-09-06 11:53:57 +03:00
]));
2020-06-26 13:40:19 +03:00
2020-07-06 09:54:18 +03:00
$amount = $this->request->get('amount');
2020-06-26 13:40:19 +03:00
// Convert amount if not same currency
if ($expense_currency_code != $income_currency_code) {
2020-07-06 09:54:18 +03:00
$amount = $this->convertBetween($amount, $expense_currency_code, $expense_currency_rate, $income_currency_code, $income_currency_rate);
2019-11-16 10:21:14 +03:00
}
2021-09-06 11:53:57 +03:00
$income_transaction = $this->dispatch(new CreateTransaction([
2020-06-26 13:40:19 +03:00
'company_id' => $this->request['company_id'],
'type' => Transaction::INCOME_TRANSFER_TYPE,
2022-06-01 10:15:55 +03:00
'number' => $this->getNextTransactionNumber(),
2020-06-26 13:40:19 +03:00
'account_id' => $this->request->get('to_account_id'),
'paid_at' => $this->request->get('transferred_at'),
'currency_code' => $income_currency_code,
2020-07-06 09:54:18 +03:00
'currency_rate' => $income_currency_rate,
2020-06-26 13:40:19 +03:00
'amount' => $amount,
'contact_id' => 0,
'description' => $this->request->get('description'),
'category_id' => $this->getTransferCategoryId(),
2020-06-26 13:40:19 +03:00
'payment_method' => $this->request->get('payment_method'),
'reference' => $this->request->get('reference'),
2022-10-04 00:23:40 +03:00
'created_from' => $this->request->get('created_from'),
'created_by' => $this->request->get('created_by'),
2021-09-06 11:53:57 +03:00
]));
2020-06-26 13:40:19 +03:00
2021-09-06 11:53:57 +03:00
$this->model = Transfer::create([
2020-06-26 13:40:19 +03:00
'company_id' => $this->request['company_id'],
'expense_transaction_id' => $expense_transaction->id,
'income_transaction_id' => $income_transaction->id,
2022-10-04 00:23:40 +03:00
'created_from' => $this->request->get('created_from'),
'created_by' => $this->request->get('created_by'),
2020-06-26 13:40:19 +03:00
]);
2021-07-09 23:36:41 +03:00
// Upload attachment
if ($this->request->file('attachment')) {
foreach ($this->request->file('attachment') as $attachment) {
$media = $this->getMedia($attachment, 'transfers');
2021-09-06 11:53:57 +03:00
$this->model->attachMedia($media, 'attachment');
2021-07-09 23:36:41 +03:00
}
}
2020-06-26 13:40:19 +03:00
});
2021-09-06 11:53:57 +03:00
return $this->model;
2019-11-16 10:21:14 +03:00
}
protected function getCurrencyCode($type)
{
$currency_code = $this->request->get($type . '_account_currency_code');
if (empty($currency_code)) {
$currency_code = Account::where('id', $this->request->get($type . '_account_id'))->pluck('currency_code')->first();
}
return $currency_code;
}
protected function getCurrencyRate($type)
{
$currency_rate = $this->request->get($type . '_account_rate');
if (empty($currency_rate)) {
2023-07-10 12:53:43 +03:00
$currency_rate = config('money.currencies.' . $this->getCurrencyCode($type) . '.rate');
}
return $currency_rate;
}
2019-11-16 10:21:14 +03:00
}