akaunting/app/Jobs/Purchase/CreateBill.php

196 lines
5.2 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;
use App\Models\Purchase\Bill;
use App\Models\Purchase\BillTotal;
use App\Traits\Currencies;
use App\Traits\DateTime;
2019-11-16 10:21:14 +03:00
class CreateBill extends Job
{
2019-11-16 10:21:14 +03:00
use Currencies, DateTime;
protected $request;
2019-12-07 12:54:13 +03:00
protected $bill;
/**
* 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));
2019-12-07 12:54:13 +03:00
$this->bill = Bill::create($this->request->all());
2019-12-07 12:54:13 +03:00
// Upload attachment
if ($this->request->file('attachment')) {
$media = $this->getMedia($this->request->file('attachment'), 'bills');
2019-12-07 12:54:13 +03:00
$this->bill->attachMedia($media, 'attachment');
}
2019-12-07 12:54:13 +03:00
$this->createItemsAndTotals();
2019-12-07 12:54:13 +03:00
$this->bill->update($this->request->input());
2019-12-07 12:54:13 +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;
}
2019-12-07 12:54:13 +03:00
protected function createItemsAndTotals()
{
2019-12-07 12:54:13 +03:00
// Create items
list($sub_total, $taxes) = $this->createItems();
2018-11-16 11:55:04 +03:00
$sort_order = 1;
2019-12-07 12:54:13 +03:00
// Add sub total
BillTotal::create([
2019-12-07 12:54:13 +03:00
'company_id' => $this->bill->company_id,
'bill_id' => $this->bill->id,
'code' => 'sub_total',
'name' => 'bills.sub_total',
'amount' => $sub_total,
'sort_order' => $sort_order,
]);
2019-12-07 12:54:13 +03:00
$this->request['amount'] += $sub_total;
$sort_order++;
2019-12-07 12:54:13 +03:00
// Add discount
if (!empty($this->request['discount'])) {
$discount_total = $sub_total * ($this->request['discount'] / 100);
BillTotal::create([
2019-12-07 12:54:13 +03:00
'company_id' => $this->bill->company_id,
'bill_id' => $this->bill->id,
'code' => 'discount',
'name' => 'bills.discount',
'amount' => $discount_total,
'sort_order' => $sort_order,
]);
2019-12-07 12:54:13 +03:00
$this->request['amount'] -= $discount_total;
$sort_order++;
}
2019-12-07 12:54:13 +03:00
// Add taxes
if (!empty($taxes)) {
foreach ($taxes as $tax) {
BillTotal::create([
2019-12-07 12:54:13 +03:00
'company_id' => $this->bill->company_id,
'bill_id' => $this->bill->id,
'code' => 'tax',
'name' => $tax['name'],
'amount' => $tax['amount'],
'sort_order' => $sort_order,
]);
2019-12-07 12:54:13 +03:00
$this->request['amount'] += $tax['amount'];
$sort_order++;
}
}
2019-12-07 12:54:13 +03:00
// Add extra totals, i.e. shipping fee
if (!empty($this->request['totals'])) {
foreach ($this->request['totals'] as $total) {
$total['company_id'] = $this->bill->company_id;
$total['bill_id'] = $this->bill->id;
$total['sort_order'] = $sort_order;
if (empty($total['code'])) {
$total['code'] = 'extra';
}
BillTotal::create($total);
if (empty($total['operator']) || ($total['operator'] == 'addition')) {
$this->request['amount'] += $total['amount'];
} else {
// subtraction
$this->request['amount'] -= $total['amount'];
}
$sort_order++;
}
}
// Add total
BillTotal::create([
2019-12-07 12:54:13 +03:00
'company_id' => $this->bill->company_id,
'bill_id' => $this->bill->id,
'code' => 'total',
'name' => 'bills.total',
2019-12-07 12:54:13 +03:00
'amount' => $this->request['amount'],
'sort_order' => $sort_order,
]);
}
2019-12-07 12:54:13 +03:00
protected function createItems()
{
$sub_total = 0;
$taxes = [];
if (empty($this->request['items'])) {
return [$sub_total, $taxes];
}
foreach ((array) $this->request['items'] as $item) {
if (empty($item['discount'])) {
$item['discount'] = !empty($this->request['discount']) ? !empty($this->request['discount']) : 0;
}
$bill_item = $this->dispatch(new CreateBillItem($item, $this->bill));
// Calculate totals
$sub_total += $bill_item->total;
if (!$bill_item->item_taxes) {
continue;
}
// Set taxes
foreach ((array) $bill_item->item_taxes as $item_tax) {
if (array_key_exists($item_tax['tax_id'], $taxes)) {
$taxes[$item_tax['tax_id']]['amount'] += $item_tax['amount'];
} else {
$taxes[$item_tax['tax_id']] = [
'name' => $item_tax['name'],
'amount' => $item_tax['amount']
];
}
}
}
return [$sub_total, $taxes];
}
}