akaunting/app/Jobs/Purchase/CreateBill.php

62 lines
1.4 KiB
PHP
Raw Normal View History

<?php
2019-12-31 15:49:09 +03:00
namespace App\Jobs\Purchase;
2019-11-16 10:21:14 +03:00
use App\Abstracts\Job;
2019-12-31 15:49:09 +03:00
use App\Events\Purchase\BillCreated;
use App\Events\Purchase\BillCreating;
2020-07-06 09:54:18 +03:00
use App\Jobs\Purchase\CreateBillItemsAndTotals;
2019-12-31 15:49:09 +03:00
use App\Models\Purchase\Bill;
2019-11-16 10:21:14 +03:00
class CreateBill extends Job
{
2019-12-07 12:54:13 +03:00
protected $bill;
2020-06-26 13:40:19 +03:00
protected $request;
/**
* Create a new job instance.
*
* @param $request
*/
public function __construct($request)
{
2019-11-16 10:21:14 +03:00
$this->request = $this->getRequestInstance($request);
}
/**
* Execute the job.
*
2019-12-07 12:54:13 +03:00
* @return Bill
*/
public function handle()
{
2019-12-07 12:54:13 +03:00
if (empty($this->request['amount'])) {
$this->request['amount'] = 0;
}
2019-12-07 12:54:13 +03:00
event(new BillCreating($this->request));
2020-06-26 13:40:19 +03:00
\DB::transaction(function () {
$this->bill = Bill::create($this->request->all());
2020-06-26 13:40:19 +03:00
// Upload attachment
if ($this->request->file('attachment')) {
$media = $this->getMedia($this->request->file('attachment'), 'bills');
2020-06-26 13:40:19 +03:00
$this->bill->attachMedia($media, 'attachment');
}
2020-07-06 09:54:18 +03:00
$this->dispatch(new CreateBillItemsAndTotals($this->bill, $this->request));
2020-07-06 09:54:18 +03:00
$this->bill->update($this->request->all());
2020-06-26 13:40:19 +03:00
$this->bill->createRecurring();
});
2019-12-07 12:54:13 +03:00
event(new BillCreated($this->bill));
2019-12-07 12:54:13 +03:00
return $this->bill;
}
}