refactored calculations

This commit is contained in:
Denis Duliçi
2020-07-06 09:54:18 +03:00
parent acacacbe62
commit 630fdb6e85
20 changed files with 549 additions and 871 deletions

View File

@@ -5,15 +5,11 @@ namespace App\Jobs\Purchase;
use App\Abstracts\Job;
use App\Events\Purchase\BillCreated;
use App\Events\Purchase\BillCreating;
use App\Jobs\Purchase\CreateBillItemsAndTotals;
use App\Models\Purchase\Bill;
use App\Models\Purchase\BillTotal;
use App\Traits\Currencies;
use App\Traits\DateTime;
class CreateBill extends Job
{
use Currencies, DateTime;
protected $bill;
protected $request;
@@ -51,9 +47,9 @@ class CreateBill extends Job
$this->bill->attachMedia($media, 'attachment');
}
$this->createItemsAndTotals();
$this->dispatch(new CreateBillItemsAndTotals($this->bill, $this->request));
$this->bill->update($this->request->input());
$this->bill->update($this->request->all());
$this->bill->createRecurring();
});
@@ -62,163 +58,4 @@ class CreateBill extends Job
return $this->bill;
}
protected function createItemsAndTotals()
{
// Create items
list($sub_total, $discount_amount_total, $taxes) = $this->createItems();
$sort_order = 1;
// Add sub total
BillTotal::create([
'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,
]);
$this->request['amount'] += $sub_total;
$sort_order++;
// Add discount
if ($discount_amount_total > 0) {
BillTotal::create([
'company_id' => $this->bill->company_id,
'bill_id' => $this->bill->id,
'code' => 'item_discount',
'name' => 'bills.item_discount',
'amount' => $discount_amount_total,
'sort_order' => $sort_order,
]);
$this->request['amount'] -= $discount_amount_total;
$sort_order++;
}
if (!empty($this->request['discount'])) {
$discount_total = ($sub_total - $discount_amount_total) * ($this->request['discount'] / 100);
BillTotal::create([
'company_id' => $this->bill->company_id,
'bill_id' => $this->bill->id,
'code' => 'discount',
'name' => 'bills.discount',
'amount' => $discount_total,
'sort_order' => $sort_order,
]);
$this->request['amount'] -= $discount_total;
$sort_order++;
}
// Add taxes
if (!empty($taxes)) {
foreach ($taxes as $tax) {
BillTotal::create([
'company_id' => $this->bill->company_id,
'bill_id' => $this->bill->id,
'code' => 'tax',
'name' => $tax['name'],
'amount' => $tax['amount'],
'sort_order' => $sort_order,
]);
$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->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([
'company_id' => $this->bill->company_id,
'bill_id' => $this->bill->id,
'code' => 'total',
'name' => 'bills.total',
'amount' => $this->request['amount'],
'sort_order' => $sort_order,
]);
}
protected function createItems()
{
$sub_total = $discount_amount = $discount_amount_total = 0;
$taxes = [];
if (empty($this->request['items'])) {
return [$sub_total, $discount_amount_total, $taxes];
}
foreach ((array) $this->request['items'] as $item) {
$item['global_discount'] = 0;
if (!empty($this->request['discount'])) {
$item['global_discount'] = $this->request['discount'];
}
$bill_item = $this->dispatch(new CreateBillItem($item, $this->bill));
$item_amount = (double) $item['price'] * (double) $item['quantity'];
$discount_amount = 0;
if (!empty($item['discount'])) {
$discount_amount = ($item_amount * ($item['discount'] / 100));
}
// Calculate totals
$sub_total += $bill_item->total + $discount_amount;
$discount_amount_total += $discount_amount;
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, $discount_amount_total, $taxes];
}
}

View File

@@ -10,20 +10,20 @@ use Illuminate\Support\Str;
class CreateBillItem extends Job
{
protected $request;
protected $bill;
protected $request;
/**
* Create a new job instance.
*
* @param $request
* @param $bill
* @param $request
*/
public function __construct($request, $bill)
public function __construct($bill, $request)
{
$this->request = $request;
$this->bill = $bill;
$this->request = $request;
}
/**
@@ -34,10 +34,11 @@ class CreateBillItem extends Job
public function handle()
{
$item_id = !empty($this->request['item_id']) ? $this->request['item_id'] : 0;
$precision = config('money.' . $this->bill->currency_code . '.precision');
$item_amount = (double) $this->request['price'] * (double) $this->request['quantity'];
$discount = 0;
$item_discounted_amount = $item_amount;
// Apply line discount to amount
@@ -110,7 +111,7 @@ class CreateBillItem extends Job
$item_base_rate = $item_amount / (1 + collect($inclusives)->sum('rate') / 100);
foreach ($inclusives as $inclusive) {
$item_tax_total += $tax_amount = $item_base_rate * ($inclusive->rate / 100);
$tax_amount = $item_base_rate * ($inclusive->rate / 100);
$item_taxes[] = [
'company_id' => $this->bill->company_id,
@@ -119,6 +120,8 @@ class CreateBillItem extends Job
'name' => $inclusive->name,
'amount' => $tax_amount,
];
$item_tax_total += $tax_amount;
}
$item_amount = ($item_amount - $item_tax_total) / (1 - $discount / 100);
@@ -128,8 +131,6 @@ class CreateBillItem extends Job
foreach ($compounds as $compound) {
$tax_amount = (($item_discounted_amount + $item_tax_total) / 100) * $compound->rate;
$item_tax_total += $tax_amount;
$item_taxes[] = [
'company_id' => $this->bill->company_id,
'bill_id' => $this->bill->id,
@@ -137,6 +138,8 @@ class CreateBillItem extends Job
'name' => $compound->name,
'amount' => $tax_amount,
];
$item_tax_total += $tax_amount;
}
}
}
@@ -147,10 +150,10 @@ class CreateBillItem extends Job
'item_id' => $item_id,
'name' => Str::limit($this->request['name'], 180, ''),
'quantity' => (double) $this->request['quantity'],
'price' => (double) $this->request['price'],
'tax' => $item_tax_total,
'price' => round($this->request['price'], $precision),
'tax' => round($item_tax_total, $precision),
'discount_rate' => !empty($this->request['discount']) ? $this->request['discount'] : 0,
'total' => $item_amount,
'total' => round($item_amount, $precision),
]);
$bill_item->item_taxes = false;
@@ -164,6 +167,7 @@ class CreateBillItem extends Job
foreach ($item_taxes as $item_tax) {
$item_tax['bill_item_id'] = $bill_item->id;
$item_tax['amount'] = round($item_tax['amount'], $precision);
BillItemTax::create($item_tax);
}

View File

@@ -0,0 +1,199 @@
<?php
namespace App\Jobs\Purchase;
use App\Abstracts\Job;
use App\Models\Purchase\Bill;
use App\Models\Purchase\BillTotal;
use App\Traits\Currencies;
use App\Traits\DateTime;
class CreateBillItemsAndTotals extends Job
{
use Currencies, DateTime;
protected $bill;
protected $request;
/**
* Create a new job instance.
*
* @param $bill
* @param $request
*/
public function __construct($bill, $request)
{
$this->bill = $bill;
$this->request = $this->getRequestInstance($request);
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
$precision = config('money.' . $this->bill->currency_code . '.precision');
list($sub_total, $discount_amount_total, $taxes) = $this->createItems();
$sort_order = 1;
// Add sub total
BillTotal::create([
'company_id' => $this->bill->company_id,
'bill_id' => $this->bill->id,
'code' => 'sub_total',
'name' => 'bills.sub_total',
'amount' => round($sub_total, $precision),
'sort_order' => $sort_order,
]);
$this->request['amount'] += $sub_total;
$sort_order++;
// Add discount
if ($discount_amount_total > 0) {
BillTotal::create([
'company_id' => $this->bill->company_id,
'bill_id' => $this->bill->id,
'code' => 'item_discount',
'name' => 'bills.item_discount',
'amount' => round($discount_amount_total, $precision),
'sort_order' => $sort_order,
]);
$this->request['amount'] -= $discount_amount_total;
$sort_order++;
}
if (!empty($this->request['discount'])) {
$discount_total = ($sub_total - $discount_amount_total) * ($this->request['discount'] / 100);
BillTotal::create([
'company_id' => $this->bill->company_id,
'bill_id' => $this->bill->id,
'code' => 'discount',
'name' => 'bills.discount',
'amount' => round($discount_total, $precision),
'sort_order' => $sort_order,
]);
$this->request['amount'] -= $discount_total;
$sort_order++;
}
// Add taxes
if (!empty($taxes)) {
foreach ($taxes as $tax) {
BillTotal::create([
'company_id' => $this->bill->company_id,
'bill_id' => $this->bill->id,
'code' => 'tax',
'name' => $tax['name'],
'amount' => round($tax['amount'], $precision),
'sort_order' => $sort_order,
]);
$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->bill->company_id;
$total['bill_id'] = $this->bill->id;
$total['sort_order'] = $sort_order;
if (empty($total['code'])) {
$total['code'] = 'extra';
}
$total['amount'] = round($total['amount'], $precision);
BillTotal::create($total);
if (empty($total['operator']) || ($total['operator'] == 'addition')) {
$this->request['amount'] += $total['amount'];
} else {
// subtraction
$this->request['amount'] -= $total['amount'];
}
$sort_order++;
}
}
$this->request['amount'] = round($this->request['amount'], $precision);
// Add total
BillTotal::create([
'company_id' => $this->bill->company_id,
'bill_id' => $this->bill->id,
'code' => 'total',
'name' => 'bills.total',
'amount' => $this->request['amount'],
'sort_order' => $sort_order,
]);
}
protected function createItems()
{
$sub_total = $discount_amount = $discount_amount_total = 0;
$taxes = [];
if (empty($this->request['items'])) {
return [$sub_total, $discount_amount_total, $taxes];
}
foreach ((array) $this->request['items'] as $item) {
$item['global_discount'] = 0;
if (!empty($this->request['discount'])) {
$item['global_discount'] = $this->request['discount'];
}
$bill_item = $this->dispatch(new CreateBillItem($this->bill, $item));
$item_amount = (double) $item['price'] * (double) $item['quantity'];
$discount_amount = 0;
if (!empty($item['discount'])) {
$discount_amount = ($item_amount * ($item['discount'] / 100));
}
// Calculate totals
$sub_total += $bill_item->total + $discount_amount;
$discount_amount_total += $discount_amount;
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, $discount_amount_total, $taxes];
}
}

View File

@@ -5,15 +5,13 @@ namespace App\Jobs\Purchase;
use App\Abstracts\Job;
use App\Events\Purchase\BillUpdated;
use App\Events\Purchase\BillUpdating;
use App\Jobs\Purchase\CreateBillItemsAndTotals;
use App\Models\Purchase\Bill;
use App\Models\Purchase\BillTotal;
use App\Traits\Currencies;
use App\Traits\DateTime;
use App\Traits\Relationships;
class UpdateBill extends Job
{
use Currencies, DateTime, Relationships;
use Relationships;
protected $bill;
@@ -51,7 +49,9 @@ class UpdateBill extends Job
$this->bill->attachMedia($media, 'attachment');
}
$this->createItemsAndTotals();
$this->deleteRelationships($this->bill, ['items', 'item_taxes', 'totals']);
$this->dispatch(new CreateBillItemsAndTotals($this->bill, $this->request));
$bill_paid = $this->bill->paid;
@@ -70,169 +70,4 @@ class UpdateBill extends Job
return $this->bill;
}
protected function createItemsAndTotals()
{
// Create items
list($sub_total, $discount_amount_total, $taxes) = $this->createItems();
// Delete current totals
$this->deleteRelationships($this->bill, 'totals');
$sort_order = 1;
// Add sub total
BillTotal::create([
'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,
]);
$this->request['amount'] += $sub_total;
$sort_order++;
// Add discount
if ($discount_amount_total > 0) {
BillTotal::create([
'company_id' => $this->bill->company_id,
'bill_id' => $this->bill->id,
'code' => 'item_discount',
'name' => 'bills.item_discount',
'amount' => $discount_amount_total,
'sort_order' => $sort_order,
]);
$this->request['amount'] -= $discount_amount_total;
$sort_order++;
}
if (!empty($this->request['discount'])) {
$discount_total = ($sub_total - $discount_amount_total) * ($this->request['discount'] / 100);
BillTotal::create([
'company_id' => $this->bill->company_id,
'bill_id' => $this->bill->id,
'code' => 'discount',
'name' => 'bills.discount',
'amount' => $discount_total,
'sort_order' => $sort_order,
]);
$this->request['amount'] -= $discount_total;
$sort_order++;
}
// Add taxes
if (!empty($taxes)) {
foreach ($taxes as $tax) {
BillTotal::create([
'company_id' => $this->bill->company_id,
'bill_id' => $this->bill->id,
'code' => 'tax',
'name' => $tax['name'],
'amount' => $tax['amount'],
'sort_order' => $sort_order,
]);
$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->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([
'company_id' => $this->bill->company_id,
'bill_id' => $this->bill->id,
'code' => 'total',
'name' => 'bills.total',
'amount' => $this->request['amount'],
'sort_order' => $sort_order,
]);
}
protected function createItems()
{
$sub_total = $discount_amount = $discount_amount_total = 0;
$taxes = [];
if (empty($this->request['items'])) {
return [$sub_total, $discount_amount_total, $taxes];
}
// Delete current items
$this->deleteRelationships($this->bill, ['items', 'item_taxes']);
foreach ((array) $this->request['items'] as $item) {
$item['global_discount'] = 0;
if (!empty($this->request['discount'])) {
$item['global_discount'] = $this->request['discount'];
}
$bill_item = $this->dispatch(new CreateBillItem($item, $this->bill));
$item_amount = (double) $item['price'] * (double) $item['quantity'];
$discount_amount = 0;
if (!empty($item['discount'])) {
$discount_amount = ($item_amount * ($item['discount'] / 100));
}
// Calculate totals
$sub_total += $bill_item->total + $discount_amount;
$discount_amount_total += $discount_amount;
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, $discount_amount_total, $taxes];
}
}