akaunting/app/Jobs/Banking/UpdateTransfer.php

132 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;
use App\Models\Banking\Account;
use App\Models\Banking\Transaction;
use App\Models\Banking\Transfer;
use App\Models\Setting\Category;
use App\Models\Setting\Currency;
2020-07-06 09:54:18 +03:00
use App\Traits\Currencies;
2019-11-16 10:21:14 +03:00
class UpdateTransfer extends Job
{
2020-07-06 09:54:18 +03:00
use Currencies;
2019-11-16 10:21:14 +03:00
protected $transfer;
protected $request;
/**
* Create a new job instance.
*
* @param $transfer
* @param $request
*/
public function __construct($transfer, $request)
{
$this->transfer = $transfer;
$this->request = $this->getRequestInstance($request);
}
/**
* Execute the job.
*
* @return Transfer
*/
public function handle()
{
2020-06-26 13:40:19 +03:00
\DB::transaction(function () {
2021-07-09 23:36:41 +03:00
// Upload attachment
if ($this->request->file('attachment')) {
$this->deleteMediaModel($this->transfer, 'attachment', $this->request);
foreach ($this->request->file('attachment') as $attachment) {
$media = $this->getMedia($attachment, 'transfers');
$this->transfer->attachMedia($media, 'attachment');
}
} elseif (!$this->request->file('attachment') && $this->transfer->attachment) {
$this->deleteMediaModel($this->transfer, 'attachment', $this->request);
}
$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
2020-06-26 13:40:19 +03:00
$expense_transaction = Transaction::findOrFail($this->transfer->expense_transaction_id);
$income_transaction = Transaction::findOrFail($this->transfer->income_transaction_id);
$expense_transaction->update([
'company_id' => $this->request['company_id'],
'type' => 'expense',
'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' => Category::transfer(), // Transfer Category ID
'payment_method' => $this->request->get('payment_method'),
'reference' => $this->request->get('reference'),
]);
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
}
2020-06-26 13:40:19 +03:00
$income_transaction->update([
'company_id' => $this->request['company_id'],
'type' => 'income',
'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' => Category::transfer(), // Transfer Category ID
'payment_method' => $this->request->get('payment_method'),
'reference' => $this->request->get('reference'),
]);
$this->transfer->update([
'company_id' => $this->request['company_id'],
'expense_transaction_id' => $expense_transaction->id,
'income_transaction_id' => $income_transaction->id,
]);
});
2019-11-16 10:21:14 +03:00
return $this->transfer;
}
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)) {
$currency_rate = config('money.' . $this->getCurrencyCode($type) . '.rate');
}
return $currency_rate;
}
2019-11-16 10:21:14 +03:00
}