akaunting/app/Jobs/Banking/CreateReconciliation.php

50 lines
1.5 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;
2019-11-16 10:21:14 +03:00
use App\Models\Banking\Reconciliation;
2020-02-18 17:14:06 +03:00
use App\Models\Banking\Transaction;
use App\Utilities\Date;
2019-11-16 10:21:14 +03:00
2021-09-07 10:33:34 +03:00
class CreateReconciliation extends Job implements HasOwner, HasSource, ShouldCreate
2019-11-16 10:21:14 +03:00
{
2021-09-06 11:53:57 +03:00
public function handle(): Reconciliation
2019-11-16 10:21:14 +03:00
{
2020-06-26 13:40:19 +03:00
\DB::transaction(function () {
$started_at = Date::parse($this->request->get('started_at'))->startOfDay();
$ended_at = Date::parse($this->request->get('ended_at'))->endOfDay();
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');
$this->request->merge([
'started_at' => $started_at,
'ended_at' => $ended_at,
'reconciled' => $reconcile,
]);
2021-06-17 18:00:26 +03:00
2021-09-06 11:53:57 +03:00
$this->model = 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
2021-09-06 11:53:57 +03:00
return $this->model;
2019-11-16 10:21:14 +03:00
}
}