2018-11-05 01:25:43 +03:00
|
|
|
<?php
|
|
|
|
|
2019-12-31 15:49:09 +03:00
|
|
|
namespace App\Jobs\Sale;
|
2018-11-05 01:25:43 +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\Sale\InvoiceCreated;
|
|
|
|
use App\Events\Sale\InvoiceCreating;
|
|
|
|
use App\Models\Sale\Invoice;
|
|
|
|
use App\Models\Sale\InvoiceTotal;
|
2018-11-05 01:25:43 +03:00
|
|
|
use App\Traits\Currencies;
|
|
|
|
use App\Traits\DateTime;
|
|
|
|
|
2019-11-16 10:21:14 +03:00
|
|
|
class CreateInvoice extends Job
|
2018-11-05 01:25:43 +03:00
|
|
|
{
|
2019-12-07 12:54:13 +03:00
|
|
|
use Currencies, DateTime;
|
2018-11-05 01:25:43 +03:00
|
|
|
|
|
|
|
protected $request;
|
|
|
|
|
2019-12-07 12:54:13 +03:00
|
|
|
protected $invoice;
|
|
|
|
|
2018-11-05 01:25:43 +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-05 01:25:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Execute the job.
|
|
|
|
*
|
|
|
|
* @return Invoice
|
|
|
|
*/
|
|
|
|
public function handle()
|
|
|
|
{
|
2019-11-16 10:21:14 +03:00
|
|
|
if (empty($this->request['amount'])) {
|
|
|
|
$this->request['amount'] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
event(new InvoiceCreating($this->request));
|
|
|
|
|
2019-12-07 12:54:13 +03:00
|
|
|
$this->invoice = Invoice::create($this->request->all());
|
2018-11-05 01:25:43 +03:00
|
|
|
|
|
|
|
// Upload attachment
|
|
|
|
if ($this->request->file('attachment')) {
|
|
|
|
$media = $this->getMedia($this->request->file('attachment'), 'invoices');
|
|
|
|
|
2019-12-07 12:54:13 +03:00
|
|
|
$this->invoice->attachMedia($media, 'attachment');
|
2018-11-05 01:25:43 +03:00
|
|
|
}
|
|
|
|
|
2019-12-07 12:54:13 +03:00
|
|
|
$this->createItemsAndTotals();
|
2018-11-05 01:25:43 +03:00
|
|
|
|
2019-12-07 12:54:13 +03:00
|
|
|
$this->invoice->update($this->request->all());
|
2018-11-05 01:25:43 +03:00
|
|
|
|
2019-12-07 12:54:13 +03:00
|
|
|
$this->invoice->createRecurring();
|
2018-11-05 01:25:43 +03:00
|
|
|
|
2019-12-07 12:54:13 +03:00
|
|
|
event(new InvoiceCreated($this->invoice));
|
2018-11-05 01:25:43 +03:00
|
|
|
|
2019-12-07 12:54:13 +03:00
|
|
|
return $this->invoice;
|
2018-11-05 01:25:43 +03:00
|
|
|
}
|
|
|
|
|
2019-12-07 12:54:13 +03:00
|
|
|
protected function createItemsAndTotals()
|
2018-11-05 01:25:43 +03:00
|
|
|
{
|
2019-12-07 12:54:13 +03:00
|
|
|
// Create items
|
|
|
|
list($sub_total, $taxes) = $this->createItems();
|
2018-11-16 11:55:04 +03:00
|
|
|
|
2018-11-05 01:25:43 +03:00
|
|
|
$sort_order = 1;
|
|
|
|
|
2019-12-07 12:54:13 +03:00
|
|
|
// Add sub total
|
2018-11-05 01:25:43 +03:00
|
|
|
InvoiceTotal::create([
|
2019-12-07 12:54:13 +03:00
|
|
|
'company_id' => $this->invoice->company_id,
|
|
|
|
'invoice_id' => $this->invoice->id,
|
2018-11-05 01:25:43 +03:00
|
|
|
'code' => 'sub_total',
|
|
|
|
'name' => 'invoices.sub_total',
|
|
|
|
'amount' => $sub_total,
|
|
|
|
'sort_order' => $sort_order,
|
|
|
|
]);
|
|
|
|
|
2019-12-07 12:54:13 +03:00
|
|
|
$this->request['amount'] += $sub_total;
|
|
|
|
|
2018-11-05 01:25:43 +03:00
|
|
|
$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);
|
|
|
|
|
2018-11-05 01:25:43 +03:00
|
|
|
InvoiceTotal::create([
|
2019-12-07 12:54:13 +03:00
|
|
|
'company_id' => $this->invoice->company_id,
|
|
|
|
'invoice_id' => $this->invoice->id,
|
2018-11-05 01:25:43 +03:00
|
|
|
'code' => 'discount',
|
|
|
|
'name' => 'invoices.discount',
|
|
|
|
'amount' => $discount_total,
|
|
|
|
'sort_order' => $sort_order,
|
|
|
|
]);
|
|
|
|
|
2019-12-07 12:54:13 +03:00
|
|
|
$this->request['amount'] -= $discount_total;
|
2018-11-05 01:25:43 +03:00
|
|
|
|
|
|
|
$sort_order++;
|
|
|
|
}
|
|
|
|
|
2019-12-07 12:54:13 +03:00
|
|
|
// Add taxes
|
|
|
|
if (!empty($taxes)) {
|
2018-11-05 01:25:43 +03:00
|
|
|
foreach ($taxes as $tax) {
|
|
|
|
InvoiceTotal::create([
|
2019-12-07 12:54:13 +03:00
|
|
|
'company_id' => $this->invoice->company_id,
|
|
|
|
'invoice_id' => $this->invoice->id,
|
2018-11-05 01:25:43 +03:00
|
|
|
'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++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add extra totals, i.e. shipping fee
|
|
|
|
if (!empty($this->request['totals'])) {
|
|
|
|
foreach ($this->request['totals'] as $total) {
|
|
|
|
$total['company_id'] = $this->invoice->company_id;
|
|
|
|
$total['invoice_id'] = $this->invoice->id;
|
|
|
|
$total['sort_order'] = $sort_order;
|
|
|
|
|
|
|
|
if (empty($total['code'])) {
|
|
|
|
$total['code'] = 'extra';
|
|
|
|
}
|
|
|
|
|
|
|
|
InvoiceTotal::create($total);
|
|
|
|
|
|
|
|
if (empty($total['operator']) || ($total['operator'] == 'addition')) {
|
|
|
|
$this->request['amount'] += $total['amount'];
|
|
|
|
} else {
|
|
|
|
// subtraction
|
|
|
|
$this->request['amount'] -= $total['amount'];
|
|
|
|
}
|
|
|
|
|
2018-11-05 01:25:43 +03:00
|
|
|
$sort_order++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-07 12:54:13 +03:00
|
|
|
// Add total
|
2018-11-05 01:25:43 +03:00
|
|
|
InvoiceTotal::create([
|
2019-12-07 12:54:13 +03:00
|
|
|
'company_id' => $this->invoice->company_id,
|
|
|
|
'invoice_id' => $this->invoice->id,
|
2018-11-05 01:25:43 +03:00
|
|
|
'code' => 'total',
|
|
|
|
'name' => 'invoices.total',
|
2019-12-07 12:54:13 +03:00
|
|
|
'amount' => $this->request['amount'],
|
2018-11-05 01:25:43 +03:00
|
|
|
'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;
|
|
|
|
}
|
|
|
|
|
|
|
|
$invoice_item = $this->dispatch(new CreateInvoiceItem($item, $this->invoice));
|
|
|
|
|
|
|
|
// Calculate totals
|
|
|
|
$sub_total += $invoice_item->total;
|
|
|
|
|
|
|
|
if (!$invoice_item->item_taxes) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set taxes
|
|
|
|
foreach ((array) $invoice_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];
|
|
|
|
}
|
2018-11-06 17:55:31 +03:00
|
|
|
}
|