akaunting/app/Jobs/Sale/UpdateInvoice.php

231 lines
6.7 KiB
PHP
Raw Normal View History

<?php
2019-12-31 15:49:09 +03:00
namespace App\Jobs\Sale;
2019-11-16 10:21:14 +03:00
use App\Abstracts\Job;
2019-12-31 15:49:09 +03:00
use App\Events\Sale\InvoiceUpdated;
use App\Events\Sale\InvoiceUpdating;
use App\Models\Sale\Invoice;
use App\Models\Sale\InvoiceTotal;
use App\Traits\Currencies;
use App\Traits\DateTime;
2019-12-07 12:54:13 +03:00
use App\Traits\Relationships;
2019-11-16 10:21:14 +03:00
class UpdateInvoice extends Job
{
2019-12-07 12:54:13 +03:00
use Currencies, DateTime, Relationships;
protected $invoice;
protected $request;
/**
* Create a new job instance.
*
* @param $request
*/
public function __construct($invoice, $request)
{
$this->invoice = $invoice;
2019-11-16 10:21:14 +03:00
$this->request = $this->getRequestInstance($request);
}
/**
* Execute the job.
*
* @return Invoice
*/
public function handle()
{
2019-12-07 12:54:13 +03:00
if (empty($this->request['amount'])) {
$this->request['amount'] = 0;
}
event(new InvoiceUpdating($this->invoice, $this->request));
2019-11-16 10:21:14 +03:00
// Upload attachment
if ($this->request->file('attachment')) {
$media = $this->getMedia($this->request->file('attachment'), 'invoices');
$this->invoice->attachMedia($media, 'attachment');
}
2019-12-07 12:54:13 +03:00
$this->createItemsAndTotals();
$invoice_paid = $this->invoice->paid;
unset($this->invoice->reconciled);
if (($invoice_paid) && $this->request['amount'] > $invoice_paid) {
2020-01-11 16:57:32 +03:00
$this->request['status'] = 'partial';
}
2019-12-07 12:54:13 +03:00
$this->invoice->update($this->request->all());
$this->invoice->updateRecurring();
2019-12-07 12:54:13 +03:00
event(new InvoiceUpdated($this->invoice, $this->request));
return $this->invoice;
}
2019-12-07 12:54:13 +03:00
protected function createItemsAndTotals()
{
2019-12-07 12:54:13 +03:00
// Create items
2020-03-24 21:37:08 +03:00
list($sub_total, $discount_amount_total, $taxes) = $this->createItems();
2018-11-16 11:55:04 +03:00
2019-12-07 12:54:13 +03:00
// Delete current totals
$this->deleteRelationships($this->invoice, 'totals');
2018-11-16 11:55:04 +03:00
$sort_order = 1;
2019-12-07 12:54:13 +03:00
// Add sub total
InvoiceTotal::create([
2019-12-07 12:54:13 +03:00
'company_id' => $this->invoice->company_id,
'invoice_id' => $this->invoice->id,
'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;
$sort_order++;
2019-12-07 12:54:13 +03:00
// Add discount
2020-03-24 21:37:08 +03:00
if ($discount_amount_total > 0) {
InvoiceTotal::create([
'company_id' => $this->invoice->company_id,
'invoice_id' => $this->invoice->id,
'code' => 'item_discount',
'name' => 'invoices.item_discount',
'amount' => $discount_amount_total,
'sort_order' => $sort_order,
]);
$this->request['amount'] -= $discount_amount_total;
$sort_order++;
}
2019-12-07 12:54:13 +03:00
if (!empty($this->request['discount'])) {
2020-03-24 21:37:08 +03:00
$discount_total = ($sub_total - $discount_amount_total) * ($this->request['discount'] / 100);
2019-12-07 12:54:13 +03:00
InvoiceTotal::create([
2019-12-07 12:54:13 +03:00
'company_id' => $this->invoice->company_id,
'invoice_id' => $this->invoice->id,
'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;
$sort_order++;
}
2019-12-07 12:54:13 +03:00
// Add taxes
if (!empty($taxes)) {
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,
'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'];
}
$sort_order++;
}
}
2019-12-07 12:54:13 +03:00
// Add total
InvoiceTotal::create([
2019-12-07 12:54:13 +03:00
'company_id' => $this->invoice->company_id,
'invoice_id' => $this->invoice->id,
'code' => 'total',
'name' => 'invoices.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()
{
2020-03-24 21:37:08 +03:00
$sub_total = $discount_amount = $discount_amount_total = 0;
2019-12-07 12:54:13 +03:00
$taxes = [];
if (empty($this->request['items'])) {
return [$sub_total, $discount_amount_total, $taxes];
2019-12-07 12:54:13 +03:00
}
// Delete current items
$this->deleteRelationships($this->invoice, ['items', 'item_taxes']);
foreach ((array) $this->request['items'] as $item) {
if (empty($item['discount'])) {
$item['discount'] = !empty($this->request['discount']) ? !empty($this->request['discount']) : 0;
}
2019-12-07 12:54:13 +03:00
$invoice_item = $this->dispatch(new CreateInvoiceItem($item, $this->invoice));
2020-03-24 21:37:08 +03:00
$item_amount = (double) $item['price'] * (double) $item['quantity'];
$discount_amount = ($item_amount * ($item['discount'] / 100));
2019-12-07 12:54:13 +03:00
// Calculate totals
2020-03-24 21:37:08 +03:00
$sub_total += $invoice_item->total + $discount_amount;
$discount_amount_total += $discount_amount;
2019-12-07 12:54:13 +03:00
if (!$invoice_item->item_taxes) {
continue;
}
2019-12-07 12:54:13 +03:00
// 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']
];
}
}
}
2019-12-07 12:54:13 +03:00
2020-03-24 21:37:08 +03:00
return [$sub_total, $discount_amount_total, $taxes];
}
}