akaunting/app/Jobs/Banking/UpdateTransfer.php

97 lines
3.5 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 () {
$expense_currency_code = Account::where('id', $this->request->get('from_account_id'))->pluck('currency_code')->first();
$income_currency_code = Account::where('id', $this->request->get('to_account_id'))->pluck('currency_code')->first();
2020-07-06 09:54:18 +03:00
$expense_currency_rate = config('money.' . $expense_currency_code . '.rate');
$income_currency_rate = config('money.' . $income_currency_code . '.rate');
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;
}
}