item total method remove #mvb7xp

This commit is contained in:
Cüneyt Şentürk 2021-07-06 23:20:46 +03:00
parent db9ade340c
commit 551a87fe69
2 changed files with 0 additions and 123 deletions

View File

@ -330,126 +330,4 @@ class Items extends Controller
'data' => ($items->count()) ? $items : null,
]);
}
public function total(TotalRequest $request)
{
$input_items = $request->input('items');
$currency_code = $request->input('currency_code');
$discount = $request->input('discount');
if (empty($currency_code)) {
$currency_code = setting('default.currency');
}
$json = new \stdClass();
$sub_total = 0;
$tax_total = 0;
$items = [];
if ($input_items) {
foreach ($input_items as $key => $item) {
$price = (double) $item['price'];
$quantity = (double) $item['quantity'];
$item_tax_total = 0;
$item_tax_amount = 0;
$item_sub_total = ($price * $quantity);
$item_discounted_total = $item_sub_total;
// Apply discount to amount
if ($discount) {
$item_discounted_total = $item_sub_total - ($item_sub_total * ($discount / 100));
}
if (!empty($item['tax_id'])) {
$inclusives = $compounds = [];
foreach ($item['tax_id'] as $tax_id) {
$tax = Tax::find($tax_id);
switch ($tax->type) {
case 'inclusive':
$inclusives[] = $tax;
break;
case 'compound':
$compounds[] = $tax;
break;
case 'fixed':
$item_tax_total += $tax->rate * $quantity;
break;
case 'withholding':
$item_tax_amount = 0 - $item_discounted_total * ($tax->rate / 100);
$item_tax_total += $item_tax_amount;
break;
default:
$item_tax_amount = ($item_discounted_total / 100) * $tax->rate;
$item_tax_total += $item_tax_amount;
break;
}
}
if ($inclusives) {
$item_sub_and_tax_total = $item_discounted_total + $item_tax_total;
$item_base_rate = $item_sub_and_tax_total / (1 + collect($inclusives)->sum('rate') / 100);
$item_tax_total = $item_sub_and_tax_total - $item_base_rate;
$item_sub_total = $item_base_rate + $discount;
}
if ($compounds) {
foreach ($compounds as $compound) {
$item_tax_total += (($item_discounted_total + $item_tax_total) / 100) * $compound->rate;
}
}
}
$sub_total += $item_sub_total;
$tax_total += $item_tax_total;
$items[$key] = money($item_sub_total, $currency_code, true)->format();
}
}
$json->items = $items;
$json->sub_total = money($sub_total, $currency_code, true)->format();
$json->discount_text = trans('invoices.add_discount');
$json->discount_total = '';
$json->tax_total = money($tax_total, $currency_code, true)->format();
// Apply discount to total
if ($discount) {
$json->discount_text = trans('invoices.show_discount', ['discount' => $discount]);
$json->discount_total = money($sub_total * ($discount / 100), $currency_code, true)->format();
$sub_total = $sub_total - ($sub_total * ($discount / 100));
}
$grand_total = $sub_total + $tax_total;
$json->grand_total = money($grand_total, $currency_code, true)->format();
// Get currency object
$currency = Currency::where('code', $currency_code)->first();
$json->currency_name = $currency->name;
$json->currency_code = $currency_code;
$json->currency_rate = $currency->rate;
$json->thousands_separator = $currency->thousands_separator;
$json->decimal_mark = $currency->decimal_mark;
$json->precision = (int) $currency->precision;
$json->symbol_first = $currency->symbol_first;
$json->symbol = $currency->symbol;
return response()->json($json);
}
}

View File

@ -31,7 +31,6 @@ Route::group(['prefix' => 'common'], function () {
Route::get('import/{group}/{type}/{route?}', 'Common\Import@create')->name('import.create');
Route::get('items/autocomplete', 'Common\Items@autocomplete')->name('items.autocomplete');
Route::post('items/total', 'Common\Items@total')->middleware(['money'])->name('items.total');
Route::get('items/{item}/enable', 'Common\Items@enable')->name('items.enable');
Route::get('items/{item}/disable', 'Common\Items@disable')->name('items.disable');
Route::get('items/{item}/duplicate', 'Common\Items@duplicate')->name('items.duplicate');