akaunting/app/Jobs/Document/CreateDocumentItemsAndTotals.php

276 lines
9.6 KiB
PHP
Raw Normal View History

2020-07-06 09:54:18 +03:00
<?php
2020-12-24 01:28:38 +03:00
namespace App\Jobs\Document;
2020-07-06 09:54:18 +03:00
use App\Abstracts\Job;
2021-09-07 10:33:34 +03:00
use App\Interfaces\Job\HasOwner;
use App\Interfaces\Job\HasSource;
2021-09-06 11:53:57 +03:00
use App\Interfaces\Job\ShouldCreate;
use App\Jobs\Common\CreateItem;
2021-09-06 11:53:57 +03:00
use App\Models\Document\Document;
use App\Models\Document\DocumentTotal;
2021-09-06 11:53:57 +03:00
use App\Traits\Currencies;
use App\Traits\DateTime;
use Illuminate\Support\Str;
2020-07-06 09:54:18 +03:00
2021-09-07 10:33:34 +03:00
class CreateDocumentItemsAndTotals extends Job implements HasOwner, HasSource, ShouldCreate
2020-07-06 09:54:18 +03:00
{
use Currencies, DateTime;
2020-12-24 01:28:38 +03:00
protected $document;
2020-07-06 09:54:18 +03:00
2021-09-06 11:53:57 +03:00
public function __construct(Document $document, $request)
2020-07-06 09:54:18 +03:00
{
2020-12-24 01:28:38 +03:00
$this->document = $document;
2021-09-06 11:53:57 +03:00
parent::__construct($request);
2020-07-06 09:54:18 +03:00
}
2021-09-06 11:53:57 +03:00
public function handle(): void
2020-07-06 09:54:18 +03:00
{
2023-07-10 12:53:43 +03:00
$precision = config('money.currencies.' . $this->document->currency_code . '.precision');
2020-07-06 09:54:18 +03:00
list($sub_total, $actual_total, $discount_amount_total, $taxes) = $this->createItems();
2020-07-06 09:54:18 +03:00
$sort_order = 1;
// Add sub total
2020-12-24 01:28:38 +03:00
DocumentTotal::create([
2020-12-25 00:28:36 +03:00
'company_id' => $this->document->company_id,
'type' => $this->document->type,
'document_id' => $this->document->id,
'code' => 'sub_total',
'name' => 'invoices.sub_total',
'amount' => round($sub_total, $precision),
'sort_order' => $sort_order,
2021-09-07 10:33:34 +03:00
'created_from' => $this->request['created_from'],
'created_by' => $this->request['created_by'],
2020-07-06 09:54:18 +03:00
]);
$this->request['amount'] += $actual_total;
2020-07-06 09:54:18 +03:00
$sort_order++;
// Add discount
if ($discount_amount_total > 0) {
2020-12-24 01:28:38 +03:00
DocumentTotal::create([
2020-12-25 00:28:36 +03:00
'company_id' => $this->document->company_id,
'type' => $this->document->type,
'document_id' => $this->document->id,
'code' => 'item_discount',
'name' => 'invoices.item_discount',
'amount' => round($discount_amount_total, $precision),
'sort_order' => $sort_order,
2021-09-07 10:33:34 +03:00
'created_from' => $this->request['created_from'],
'created_by' => $this->request['created_by'],
2020-07-06 09:54:18 +03:00
]);
$sort_order++;
}
2022-09-15 01:16:59 +03:00
if (! empty($this->request['discount'])) {
if ($this->request['discount_type'] === 'percentage') {
$discount_total = ($sub_total - $discount_amount_total) * ($this->request['discount'] / 100);
2021-08-15 17:20:39 +01:00
} else {
$discount_total = $this->request['discount'];
}
2020-07-06 09:54:18 +03:00
2021-09-07 10:33:34 +03:00
DocumentTotal::create([
2020-12-25 00:28:36 +03:00
'company_id' => $this->document->company_id,
'type' => $this->document->type,
'document_id' => $this->document->id,
'code' => 'discount',
'name' => 'invoices.discount',
'amount' => round($discount_total, $precision),
'sort_order' => $sort_order,
2021-09-07 10:33:34 +03:00
'created_from' => $this->request['created_from'],
'created_by' => $this->request['created_by'],
2020-07-06 09:54:18 +03:00
]);
$sort_order++;
}
// Add taxes
2022-09-15 01:16:59 +03:00
if (! empty($taxes)) {
2020-07-06 09:54:18 +03:00
foreach ($taxes as $tax) {
2020-12-24 01:28:38 +03:00
DocumentTotal::create([
2020-12-25 00:28:36 +03:00
'company_id' => $this->document->company_id,
'type' => $this->document->type,
'document_id' => $this->document->id,
'code' => 'tax',
'name' => Str::ucfirst($tax['name']),
2020-12-25 00:28:36 +03:00
'amount' => round(abs($tax['amount']), $precision),
'sort_order' => $sort_order,
2021-09-07 10:33:34 +03:00
'created_from' => $this->request['created_from'],
'created_by' => $this->request['created_by'],
2020-07-06 09:54:18 +03:00
]);
$this->request['amount'] += $tax['amount'];
$sort_order++;
}
}
// Add extra totals, i.e. shipping fee
2022-09-15 01:16:59 +03:00
if (! empty($this->request['totals'])) {
2020-07-06 09:54:18 +03:00
foreach ($this->request['totals'] as $total) {
2020-12-24 01:28:38 +03:00
$total['company_id'] = $this->document->company_id;
$total['type'] = $this->document->type;
$total['document_id'] = $this->document->id;
2020-07-06 09:54:18 +03:00
$total['sort_order'] = $sort_order;
2021-09-07 10:33:34 +03:00
$total['created_from'] = $this->request['created_from'];
$total['created_by'] = $this->request['created_by'];
2020-07-06 09:54:18 +03:00
if (empty($total['code'])) {
$total['code'] = 'extra';
}
2020-07-22 15:11:31 +03:00
$total['amount'] = round(abs($total['amount']), $precision);
2020-07-06 09:54:18 +03:00
2020-12-24 01:28:38 +03:00
DocumentTotal::create($total);
2020-07-06 09:54:18 +03:00
if (empty($total['operator']) || ($total['operator'] == 'addition')) {
$this->request['amount'] += $total['amount'];
} else {
// subtraction
$this->request['amount'] -= $total['amount'];
}
$sort_order++;
}
}
2022-04-16 19:29:32 +03:00
$this->request['amount'] = round($this->request['amount'], $precision);
2020-07-06 09:54:18 +03:00
// Add total
2020-12-24 01:28:38 +03:00
DocumentTotal::create([
2020-12-25 00:28:36 +03:00
'company_id' => $this->document->company_id,
'type' => $this->document->type,
'document_id' => $this->document->id,
'code' => 'total',
'name' => 'invoices.total',
2022-06-01 10:15:55 +03:00
'amount' => $this->request['amount'],
2020-12-25 00:28:36 +03:00
'sort_order' => $sort_order,
2021-09-07 10:33:34 +03:00
'created_from' => $this->request['created_from'],
'created_by' => $this->request['created_by'],
2020-07-06 09:54:18 +03:00
]);
}
2021-09-06 11:53:57 +03:00
protected function createItems(): array
2020-07-06 09:54:18 +03:00
{
$sub_total = $actual_total = $discount_amount = $discount_amount_total = 0;
2020-07-06 09:54:18 +03:00
$taxes = [];
if (empty($this->request['items'])) {
return [$sub_total, $actual_total, $discount_amount_total, $taxes];
2020-07-06 09:54:18 +03:00
}
2022-09-15 01:16:59 +03:00
if (! empty($this->request['discount']) && $this->request['discount_type'] !== 'percentage') {
$for_fixed_discount = $this->fixedDiscountCalculate();
}
foreach ((array) $this->request['items'] as $key => $item) {
2020-07-06 09:54:18 +03:00
$item['global_discount'] = 0;
2022-09-15 01:16:59 +03:00
if (! empty($this->request['discount'])) {
if (isset($for_fixed_discount)) {
$item['global_discount'] = ($for_fixed_discount[$key] / ($for_fixed_discount['total'] / 100)) * ($this->request['discount'] / 100);
$item['global_discount_type'] = '';
} else {
$item['global_discount'] = $this->request['discount'];
$item['global_discount_type'] = $this->request['discount_type'];
}
2020-07-06 09:54:18 +03:00
}
$item['created_from'] = $this->request['created_from'];
$item['created_by'] = $this->request['created_by'];
if (empty($item['item_id'])) {
$new_item_request = [
'company_id' => $this->request['company_id'],
'name' => $item['name'],
'description' => $item['description'],
'sale_price' => $item['price'],
'purchase_price' => $item['price'],
'created_from' => $item['created_from'],
'created_by' => $item['created_by'],
2021-09-07 10:33:34 +03:00
'enabled' => '1',
];
2022-09-15 01:16:59 +03:00
if (! empty($item['tax_ids'])) {
$new_item_request['tax_ids'] = $item['tax_ids'];
}
$new_item = $this->dispatch(new CreateItem($new_item_request));
$item['item_id'] = $new_item->id;
}
2020-12-24 01:28:38 +03:00
$document_item = $this->dispatch(new CreateDocumentItem($this->document, $item));
2020-07-06 09:54:18 +03:00
$item_amount = (double) $item['price'] * (double) $item['quantity'];
$discount_amount = 0;
2022-09-15 01:16:59 +03:00
if (! empty($item['discount'])) {
if ($item['discount_type'] === 'percentage') {
2021-08-15 17:20:39 +01:00
$discount_amount = ($item_amount * ($item['discount'] / 100));
} else {
$discount_amount = $item['discount'];
}
2020-07-06 09:54:18 +03:00
}
// Calculate totals
$sub_total += $item_amount;
$actual_total += $document_item->total;
2020-07-06 09:54:18 +03:00
$discount_amount_total += $discount_amount;
2022-09-15 01:16:59 +03:00
if (! $document_item->item_taxes) {
2020-07-06 09:54:18 +03:00
continue;
}
// Set taxes
2020-12-24 01:28:38 +03:00
foreach ((array) $document_item->item_taxes as $item_tax) {
2020-07-06 09:54:18 +03:00
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, $actual_total, $discount_amount_total, $taxes];
}
public function fixedDiscountCalculate()
{
$total = 0;
foreach ((array) $this->request['items'] as $item) {
$sub = (double) $item['price'] * (double) $item['quantity'];
if (! empty($this->request['discount'])) {
2022-06-01 10:15:55 +03:00
if (isset($item['discount']) && isset($item['discount_type'])) {
if ($item['discount_type'] === 'percentage') {
$sub -= ($sub * ($item['discount'] / 100));
} else {
$sub -= $item['discount'];
}
}
}
$total += $sub;
$item_total[] = $sub;
}
$item_total['total'] = $total;
return $item_total;
2020-07-06 09:54:18 +03:00
}
}