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