akaunting/app/Jobs/Banking/CreateReconciliation.php

59 lines
1.4 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\Reconciliation;
2020-02-18 17:14:06 +03:00
use App\Models\Banking\Transaction;
2019-11-16 10:21:14 +03:00
class CreateReconciliation extends Job
{
2020-06-26 13:40:19 +03:00
protected $reconciliation;
2019-11-16 10:21:14 +03:00
protected $request;
/**
* Create a new job instance.
*
* @param $request
*/
public function __construct($request)
{
$this->request = $this->getRequestInstance($request);
2021-06-17 10:59:07 +03:00
$this->request->merge(['created_by' => user_id()]);
2019-11-16 10:21:14 +03:00
}
/**
* Execute the job.
*
* @return Reconciliation
*/
public function handle()
{
2020-06-26 13:40:19 +03:00
\DB::transaction(function () {
2021-06-17 18:00:26 +03:00
$reconcile = (int) $this->request->get('reconcile');
2020-06-26 13:40:19 +03:00
$transactions = $this->request->get('transactions');
2021-06-17 18:00:26 +03:00
$this->request->merge(['reconciled' => $reconcile]);
$this->reconciliation = Reconciliation::create($this->request->all());
2020-06-26 13:40:19 +03:00
if ($reconcile && $transactions) {
2020-06-26 13:40:19 +03:00
foreach ($transactions as $key => $value) {
if (empty($value)) {
continue;
}
$t = explode('_', $key);
$transaction = Transaction::find($t[1]);
$transaction->reconciled = 1;
$transaction->save();
2020-02-18 17:14:06 +03:00
}
2019-11-16 10:21:14 +03:00
}
2020-06-26 13:40:19 +03:00
});
2019-11-16 10:21:14 +03:00
2020-06-26 13:40:19 +03:00
return $this->reconciliation;
2019-11-16 10:21:14 +03:00
}
}