diff --git a/app/Http/Controllers/Common/Items.php b/app/Http/Controllers/Common/Items.php index 4b29ebac3..cad690c4d 100644 --- a/app/Http/Controllers/Common/Items.php +++ b/app/Http/Controllers/Common/Items.php @@ -323,44 +323,31 @@ class Items extends Controller $price = (double) $item['price']; $quantity = (double) $item['quantity']; - $item_tax_total= 0; + $item_tax_total = 0; $item_sub_total = ($price * $quantity); if (!empty($item['tax_id'])) { - $tax = Tax::find($item['tax_id']); + foreach ($item['tax_id'] as $tax_id) { + $tax = Tax::find($tax_id); - $item_tax_total = (($price * $quantity) / 100) * $tax->rate; + $item_tax = (($price * $quantity) / 100) * $tax->rate; + + // Apply discount to tax + if ($discount) { + $item_tax = $item_tax - ($item_tax * ($discount / 100)); + } + + $item_tax_total += $item_tax; + } } $sub_total += $item_sub_total; - - // Apply discount to tax - if ($discount) { - $item_tax_total = $item_tax_total - ($item_tax_total * ($discount / 100)); - } - $tax_total += $item_tax_total; $items[$key] = money($item_sub_total, $currency_code, true)->format(); } } - if ($request['multiple_tax']) { - foreach ($request['multiple_tax'] as $tax_row => $multiple_tax) { - if ($multiple_tax['position'] != 'GST') { - continue; - } - - $multible_tax_object = Tax::find($multiple_tax['tax_id']); - - $multiple_tax_amount = ($sub_total / 100) * $multible_tax_object->rate; - - $sub_total += $multiple_tax_amount; - - $taxes['multiple-tax-' . $tax_row . '-total'] = money($multiple_tax_amount, $currency_code, true)->format(); - } - } - $json->items = $items; $json->sub_total = money($sub_total, $currency_code, true)->format(); @@ -378,32 +365,10 @@ class Items extends Controller $sub_total = $sub_total - ($sub_total * ($discount / 100)); } - if ($request['multiple_tax']) { - foreach ($request['multiple_tax'] as $tax_row => $multiple_tax) { - if ($multiple_tax['position'] != 'PST') { - continue; - } - - $multible_tax_object = Tax::find($multiple_tax['tax_id']); - - $multiple_tax_amount = ($sub_total / 100) * $multible_tax_object->rate; - - $tax_total += $multiple_tax_amount; - - $taxes['multiple-tax-' . $tax_row . '-total'] = money($multiple_tax_amount, $currency_code, true)->format(); - } - } - $grand_total = $sub_total + $tax_total; $json->grand_total = money($grand_total, $currency_code, true)->format(); - $json->multible_taxes = false; - - if (!empty($taxes)) { - $json->multible_taxes = $taxes; - } - // Get currency object $currency = Currency::where('code', $currency_code)->first(); diff --git a/app/Http/Controllers/Expenses/Bills.php b/app/Http/Controllers/Expenses/Bills.php index 7cdfcf4d5..fba2db439 100644 --- a/app/Http/Controllers/Expenses/Bills.php +++ b/app/Http/Controllers/Expenses/Bills.php @@ -169,7 +169,6 @@ class Bills extends Controller if ($request['item']) { foreach ($request['item'] as $item) { - unset($tax_object); $item_sku = ''; if (!empty($item['item_id'])) { @@ -183,18 +182,32 @@ class Bills extends Controller $item_object->save(); } - $tax = $tax_id = 0; + $item_tax = 0; + $item_taxes = []; + $bill_item_taxes = []; if (!empty($item['tax_id'])) { - $tax_object = Tax::find($item['tax_id']); + foreach ($item['tax_id'] as $tax_id) { + $tax_object = Tax::find($tax_id); - $tax_id = $item['tax_id']; + $item_taxes[] = $tax_id; - $tax = (((double) $item['price'] * (double) $item['quantity']) / 100) * $tax_object->rate; + $tax = (((double) $item['price'] * (double) $item['quantity']) / 100) * $tax_object->rate; - // Apply discount to tax - if ($discount) { - $tax = $tax - ($tax * ($discount / 100)); + // Apply discount to tax + if ($discount) { + $tax = $tax - ($tax * ($discount / 100)); + } + + $bill_item_taxes[] = [ + 'company_id' => $request['company_id'], + 'bill_id' => $bill->id, + 'tax_id' => $tax_id, + 'name' => $tax_object->name, + 'amount' => $tax, + ]; + + $item_tax += $tax; } } @@ -203,53 +216,33 @@ class Bills extends Controller $bill_item['sku'] = $item_sku; $bill_item['quantity'] = (double) $item['quantity']; $bill_item['price'] = (double) $item['price']; - $bill_item['tax'] = $tax; - $bill_item['tax_id'] = $tax_id; + $bill_item['tax'] = $item_tax; + $bill_item['tax_id'] = 0;//$tax_id; $bill_item['total'] = (double) $item['price'] * (double) $item['quantity']; - BillItem::create($bill_item); + $bill_item_created = BillItem::create($bill_item); - // Set taxes - if (isset($tax_object)) { - if (isset($taxes['VAT']) && array_key_exists($tax_object->id, $taxes['VAT'])) { - $taxes['VAT'][$tax_object->id]['amount'] += $tax; - } else { - $taxes['VAT'][$tax_object->id] = [ - 'name' => $tax_object->name, - 'amount' => $tax - ]; + if ($bill_item_taxes) { + foreach ($bill_item_taxes as $bill_item_tax) { + $bill_item_tax['invoice_item_id'] = $bill_item_created->id; + + BillItemTax::create($bill_item_tax); + + // Set taxes + if (isset($taxes) && array_key_exists($bill_item_tax['tax_id'], $taxes)) { + $taxes[$bill_item_tax['tax_id']]['amount'] += $bill_item_tax['amount']; + } else { + $taxes[$bill_item_tax['tax_id']] = [ + 'name' => $bill_item_tax['name'], + 'amount' => $bill_item_tax['amount'] + ]; + } } } // Calculate totals - $tax_total += $tax; + $tax_total += $item_tax; $sub_total += $bill_item['total']; - - unset($tax_object); - } - } - - if ($request['multiple_tax']) { - foreach ($request['multiple_tax'] as $multiple_tax) { - if ($multiple_tax['position'] != 'GST') { - continue; - } - - $multible_tax_object = Tax::find($multiple_tax['tax_id']); - - $multiple_tax_amount = ($sub_total / 100) * $multible_tax_object->rate; - - if (isset($taxes['GST']) && array_key_exists($multible_tax_object->id, $taxes['GST'])) { - $taxes['GST'][$multible_tax_object->id]['amount'] += $multiple_tax_amount; - } else { - $taxes['GST'][$multible_tax_object->id] = [ - 'id' => $multible_tax_object->id, - 'name' => $multible_tax_object->name, - 'amount' => $multiple_tax_amount - ]; - } - - $sub_total += $multiple_tax_amount; } } @@ -262,30 +255,6 @@ class Bills extends Controller $s_total = $s_total - $s_discount; } - if ($request['multiple_tax']) { - foreach ($request['multiple_tax'] as $multiple_tax) { - if ($multiple_tax['position'] != 'PST') { - continue; - } - - $multible_tax_object = Tax::find($multiple_tax['tax_id']); - - $multiple_tax_amount = ($s_total / 100) * $multible_tax_object->rate; - - if (isset($taxes['PST']) && array_key_exists($multible_tax_object->id, $taxes['PST'])) { - $taxes['PST'][$multible_tax_object->id]['amount'] += $multiple_tax_amount; - } else { - $taxes['PST'][$multible_tax_object->id] = [ - 'id' => $multible_tax_object->id, - 'name' => $multible_tax_object->name, - 'amount' => $multiple_tax_amount - ]; - } - - $tax_total += $multiple_tax_amount; - } - } - $amount = $s_total + $tax_total; $request['amount'] = money($amount, $request['currency_code'])->getAmount(); @@ -422,6 +391,7 @@ class Bills extends Controller public function update(Bill $bill, Request $request) { $taxes = []; + $tax_total = 0; $sub_total = 0; $discount_total = 0; @@ -445,18 +415,32 @@ class Bills extends Controller $item_sku = $item_object->sku; } - $tax = $tax_id = 0; + $item_tax = 0; + $item_taxes = []; + $bill_item_taxes = []; if (!empty($item['tax_id'])) { - $tax_object = Tax::find($item['tax_id']); + foreach ($item['tax_id'] as $tax_id) { + $tax_object = Tax::find($tax_id); - $tax_id = $item['tax_id']; + $item_taxes[] = $tax_id; - $tax = (((double) $item['price'] * (double) $item['quantity']) / 100) * $tax_object->rate; + $tax = (((double) $item['price'] * (double) $item['quantity']) / 100) * $tax_object->rate; - // Apply discount to tax - if ($discount) { - $tax = $tax - ($tax * ($discount / 100)); + // Apply discount to tax + if ($discount) { + $tax = $tax - ($tax * ($discount / 100)); + } + + $bill_item_taxes[] = [ + 'company_id' => $request['company_id'], + 'bill_id' => $bill->id, + 'tax_id' => $tax_id, + 'name' => $tax_object->name, + 'amount' => $tax, + ]; + + $item_tax += $tax; } } @@ -465,49 +449,32 @@ class Bills extends Controller $bill_item['sku'] = $item_sku; $bill_item['quantity'] = (double) $item['quantity']; $bill_item['price'] = (double) $item['price']; - $bill_item['tax'] = $tax; - $bill_item['tax_id'] = $tax_id; + $bill_item['tax'] = $item_tax; + $bill_item['tax_id'] = 0;//$tax_id; $bill_item['total'] = (double) $item['price'] * (double) $item['quantity']; - if (isset($tax_object)) { - if (isset($taxes['VAT']) && array_key_exists($tax_object->id, $taxes['VAT'])) { - $taxes['VAT'][$tax_object->id]['amount'] += $tax; - } else { - $taxes['VAT'][$tax_object->id] = [ - 'name' => $tax_object->name, - 'amount' => $tax - ]; - } - } - - $tax_total += $tax; + $tax_total += $item_tax; $sub_total += $bill_item['total']; - BillItem::create($bill_item); - } - } + $bill_item_created = BillItem::create($bill_item); - if ($request['multiple_tax']) { - foreach ($request['multiple_tax'] as $multiple_tax) { - if ($multiple_tax['position'] != 'GST') { - continue; + if ($bill_item_taxes) { + foreach ($bill_item_taxes as $bill_item_tax) { + $bill_item_tax['invoice_item_id'] = $bill_item_created->id; + + BillItemTax::create($bill_item_tax); + + // Set taxes + if (isset($taxes) && array_key_exists($bill_item_tax['tax_id'], $taxes)) { + $taxes[$bill_item_tax['tax_id']]['amount'] += $bill_item_tax['amount']; + } else { + $taxes[$bill_item_tax['tax_id']] = [ + 'name' => $bill_item_tax['name'], + 'amount' => $bill_item_tax['amount'] + ]; + } + } } - - $multible_tax_object = Tax::find($multiple_tax['tax_id']); - - $multiple_tax_amount = ($sub_total / 100) * $multible_tax_object->rate; - - if (isset($taxes['GST']) && array_key_exists($multible_tax_object->id, $taxes['GST'])) { - $taxes['GST'][$multible_tax_object->id]['amount'] += $multiple_tax_amount; - } else { - $taxes['GST'][$multible_tax_object->id] = [ - 'id' => $multible_tax_object->id, - 'name' => $multible_tax_object->name, - 'amount' => $multiple_tax_amount - ]; - } - - $sub_total += $multiple_tax_amount; } } @@ -520,30 +487,6 @@ class Bills extends Controller $s_total = $s_total - $s_discount; } - if ($request['multiple_tax']) { - foreach ($request['multiple_tax'] as $multiple_tax) { - if ($multiple_tax['position'] != 'PST') { - continue; - } - - $multible_tax_object = Tax::find($multiple_tax['tax_id']); - - $multiple_tax_amount = ($s_total / 100) * $multible_tax_object->rate; - - if (isset($taxes['PST']) && array_key_exists($multible_tax_object->id, $taxes['PST'])) { - $taxes['PST'][$multible_tax_object->id]['amount'] += $multiple_tax_amount; - } else { - $taxes['PST'][$multible_tax_object->id] = [ - 'id' => $multible_tax_object->id, - 'name' => $multible_tax_object->name, - 'amount' => $multiple_tax_amount - ]; - } - - $tax_total += $multiple_tax_amount; - } - } - $amount = $s_total + $tax_total; $request['amount'] = money($amount, $request['currency_code'])->getAmount(); @@ -892,21 +835,6 @@ class Bills extends Controller { $sort_order = 1; - if (isset($taxes['GST'])) { - foreach ($taxes['GST'] as $tax) { - InvoiceTotal::create([ - 'company_id' => $request['company_id'], - 'invoice_id' => $invoice->id, - 'code' => 'gst-' . $tax['id'], - 'name' => $tax['name'], - 'amount' => $tax['amount'], - 'sort_order' => $sort_order, - ]); - - $sort_order++; - } - } - // Added bill sub total BillTotal::create([ 'company_id' => $request['company_id'], @@ -936,24 +864,9 @@ class Bills extends Controller $sort_order++; } - if (isset($taxes['PST'])) { - foreach ($taxes['PST'] as $tax) { - InvoiceTotal::create([ - 'company_id' => $request['company_id'], - 'invoice_id' => $invoice->id, - 'code' => 'pst-' . $tax['id'], - 'name' => $tax['name'], - 'amount' => $tax['amount'], - 'sort_order' => $sort_order, - ]); - - $sort_order++; - } - } - // Added bill taxes - if (isset($taxes['VAT'])) { - foreach ($taxes['VAT'] as $tax) { + if (isset($taxes)) { + foreach ($taxes as $tax) { BillTotal::create([ 'company_id' => $request['company_id'], 'bill_id' => $bill->id, diff --git a/app/Http/Controllers/Incomes/Invoices.php b/app/Http/Controllers/Incomes/Invoices.php index 123a2c621..2b5b27b2c 100644 --- a/app/Http/Controllers/Incomes/Invoices.php +++ b/app/Http/Controllers/Incomes/Invoices.php @@ -14,6 +14,7 @@ use App\Models\Income\Customer; use App\Models\Income\Invoice; use App\Models\Income\InvoiceHistory; use App\Models\Income\InvoiceItem; +use App\Models\Income\InvoiceItemTax; use App\Models\Income\InvoiceTotal; use App\Models\Income\InvoicePayment; use App\Models\Income\InvoiceStatus; @@ -218,18 +219,32 @@ class Invoices extends Controller } } - $tax = $tax_id = 0; + $item_tax = 0; + $item_taxes = []; + $invoice_item_taxes = []; if (!empty($item['tax_id'])) { - $tax_object = Tax::find($item['tax_id']); + foreach ($item['tax_id'] as $tax_id) { + $tax_object = Tax::find($tax_id); - $tax_id = $item['tax_id']; + $item_taxes[] = $tax_id; - $tax = (((double) $item['price'] * (double) $item['quantity']) / 100) * $tax_object->rate; + $tax = (((double) $item['price'] * (double) $item['quantity']) / 100) * $tax_object->rate; - // Apply discount to tax - if ($discount) { - $tax = $tax - ($tax * ($discount / 100)); + // Apply discount to tax + if ($discount) { + $tax = $tax - ($tax * ($discount / 100)); + } + + $invoice_item_taxes[] = [ + 'company_id' => $request['company_id'], + 'invoice_id' => $invoice->id, + 'tax_id' => $tax_id, + 'name' => $tax_object->name, + 'amount' => $tax, + ]; + + $item_tax += $tax; } } @@ -238,53 +253,33 @@ class Invoices extends Controller $invoice_item['sku'] = $item_sku; $invoice_item['quantity'] = (double) $item['quantity']; $invoice_item['price'] = (double) $item['price']; - $invoice_item['tax'] = $tax; - $invoice_item['tax_id'] = $tax_id; + $invoice_item['tax'] = $item_tax; + $invoice_item['tax_id'] = 0;//(int) $item_taxes; $invoice_item['total'] = (double) $item['price'] * (double) $item['quantity']; - InvoiceItem::create($invoice_item); + $invoice_item_created = InvoiceItem::create($invoice_item); - // Set taxes - if (isset($tax_object)) { - if (isset($taxes['VAT']) && array_key_exists($tax_object->id, $taxes['VAT'])) { - $taxes['VAT'][$tax_object->id]['amount'] += $tax; - } else { - $taxes['VAT'][$tax_object->id] = [ - 'name' => $tax_object->name, - 'amount' => $tax - ]; + if ($invoice_item_taxes) { + foreach ($invoice_item_taxes as $invoice_item_tax) { + $invoice_item_tax['invoice_item_id'] = $invoice_item_created->id; + + InvoiceItemTax::create($invoice_item_tax); + + // Set taxes + if (isset($taxes) && array_key_exists($invoice_item_tax['tax_id'], $taxes)) { + $taxes[$invoice_item_tax['tax_id']]['amount'] += $invoice_item_tax['amount']; + } else { + $taxes[$invoice_item_tax['tax_id']] = [ + 'name' => $invoice_item_tax['name'], + 'amount' => $invoice_item_tax['amount'] + ]; + } } } // Calculate totals - $tax_total += $tax; + $tax_total += $item_tax; $sub_total += $invoice_item['total']; - - unset($tax_object); - } - } - - if ($request['multiple_tax']) { - foreach ($request['multiple_tax'] as $multiple_tax) { - if ($multiple_tax['position'] != 'GST') { - continue; - } - - $multible_tax_object = Tax::find($multiple_tax['tax_id']); - - $multiple_tax_amount = ($sub_total / 100) * $multible_tax_object->rate; - - if (isset($taxes['GST']) && array_key_exists($multible_tax_object->id, $taxes['GST'])) { - $taxes['GST'][$multible_tax_object->id]['amount'] += $multiple_tax_amount; - } else { - $taxes['GST'][$multible_tax_object->id] = [ - 'id' => $multible_tax_object->id, - 'name' => $multible_tax_object->name, - 'amount' => $multiple_tax_amount - ]; - } - - $sub_total += $multiple_tax_amount; } } @@ -297,30 +292,6 @@ class Invoices extends Controller $s_total = $s_total - $s_discount; } - if ($request['multiple_tax']) { - foreach ($request['multiple_tax'] as $multiple_tax) { - if ($multiple_tax['position'] != 'PST') { - continue; - } - - $multible_tax_object = Tax::find($multiple_tax['tax_id']); - - $multiple_tax_amount = ($s_total / 100) * $multible_tax_object->rate; - - if (isset($taxes['PST']) && array_key_exists($multible_tax_object->id, $taxes['PST'])) { - $taxes['PST'][$multible_tax_object->id]['amount'] += $multiple_tax_amount; - } else { - $taxes['PST'][$multible_tax_object->id] = [ - 'id' => $multible_tax_object->id, - 'name' => $multible_tax_object->name, - 'amount' => $multiple_tax_amount - ]; - } - - $tax_total += $multiple_tax_amount; - } - } - $amount = $s_total + $tax_total; $request['amount'] = money($amount, $request['currency_code'])->getAmount(); @@ -463,6 +434,7 @@ class Invoices extends Controller public function update(Invoice $invoice, Request $request) { $taxes = []; + $tax_total = 0; $sub_total = 0; $discount_total = 0; @@ -486,18 +458,32 @@ class Invoices extends Controller $item_sku = $item_object->sku; } - $tax = $tax_id = 0; + $item_tax = 0; + $item_taxes = []; + $invoice_item_taxes = []; if (!empty($item['tax_id'])) { - $tax_object = Tax::find($item['tax_id']); + foreach ($item['tax_id'] as $tax_id) { + $tax_object = Tax::find($tax_id); - $tax_id = $item['tax_id']; + $item_taxes[] = $tax_id; - $tax = (((double) $item['price'] * (double) $item['quantity']) / 100) * $tax_object->rate; + $tax = (((double) $item['price'] * (double) $item['quantity']) / 100) * $tax_object->rate; - // Apply discount to tax - if ($discount) { - $tax = $tax - ($tax * ($discount / 100)); + // Apply discount to tax + if ($discount) { + $tax = $tax - ($tax * ($discount / 100)); + } + + $invoice_item_taxes[] = [ + 'company_id' => $request['company_id'], + 'invoice_id' => $invoice->id, + 'tax_id' => $tax_id, + 'name' => $tax_object->name, + 'amount' => $tax, + ]; + + $item_tax += $tax; } } @@ -507,48 +493,31 @@ class Invoices extends Controller $invoice_item['quantity'] = (double) $item['quantity']; $invoice_item['price'] = (double) $item['price']; $invoice_item['tax'] = $tax; - $invoice_item['tax_id'] = $tax_id; + $invoice_item['tax_id'] = 0;//$tax_id; $invoice_item['total'] = (double) $item['price'] * (double) $item['quantity']; - if (isset($tax_object)) { - if (isset($taxes['VAT']) && array_key_exists($tax_object->id, $taxes['VAT'])) { - $taxes['VAT'][$tax_object->id]['amount'] += $tax; - } else { - $taxes['VAT'][$tax_object->id] = [ - 'name' => $tax_object->name, - 'amount' => $tax - ]; + $invoice_item_created = InvoiceItem::create($invoice_item); + + if ($invoice_item_taxes) { + foreach ($invoice_item_taxes as $invoice_item_tax) { + $invoice_item_tax['invoice_item_id'] = $invoice_item_created->id; + + InvoiceItemTax::create($invoice_item_tax); + + // Set taxes + if (isset($taxes) && array_key_exists($invoice_item_tax['tax_id'], $taxes)) { + $taxes[$invoice_item_tax['tax_id']]['amount'] += $invoice_item_tax['amount']; + } else { + $taxes[$invoice_item_tax['tax_id']] = [ + 'name' => $invoice_item_tax['name'], + 'amount' => $invoice_item_tax['amount'] + ]; + } } } - $tax_total += $tax; + $tax_total += $item_tax; $sub_total += $invoice_item['total']; - - InvoiceItem::create($invoice_item); - } - } - - if ($request['multiple_tax']) { - foreach ($request['multiple_tax'] as $multiple_tax) { - if ($multiple_tax['position'] != 'GST') { - continue; - } - - $multible_tax_object = Tax::find($multiple_tax['tax_id']); - - $multiple_tax_amount = ($sub_total / 100) * $multible_tax_object->rate; - - if (isset($taxes['GST']) && array_key_exists($multible_tax_object->id, $taxes['GST'])) { - $taxes['GST'][$multible_tax_object->id]['amount'] += $multiple_tax_amount; - } else { - $taxes['GST'][$multible_tax_object->id] = [ - 'id' => $multible_tax_object->id, - 'name' => $multible_tax_object->name, - 'amount' => $multiple_tax_amount - ]; - } - - $sub_total += $multiple_tax_amount; } } @@ -561,30 +530,6 @@ class Invoices extends Controller $s_total = $s_total - $s_discount; } - if ($request['multiple_tax']) { - foreach ($request['multiple_tax'] as $multiple_tax) { - if ($multiple_tax['position'] != 'PST') { - continue; - } - - $multible_tax_object = Tax::find($multiple_tax['tax_id']); - - $multiple_tax_amount = ($s_total / 100) * $multible_tax_object->rate; - - if (isset($taxes['PST']) && array_key_exists($multible_tax_object->id, $taxes['PST'])) { - $taxes['PST'][$multible_tax_object->id]['amount'] += $multiple_tax_amount; - } else { - $taxes['PST'][$multible_tax_object->id] = [ - 'id' => $multible_tax_object->id, - 'name' => $multible_tax_object->name, - 'amount' => $multiple_tax_amount - ]; - } - - $tax_total += $multiple_tax_amount; - } - } - $amount = $s_total + $tax_total; $request['amount'] = money($amount, $request['currency_code'])->getAmount(); @@ -1049,21 +994,6 @@ class Invoices extends Controller { $sort_order = 1; - if (isset($taxes['GST'])) { - foreach ($taxes['GST'] as $tax) { - InvoiceTotal::create([ - 'company_id' => $request['company_id'], - 'invoice_id' => $invoice->id, - 'code' => 'gst-' . $tax['id'], - 'name' => $tax['name'], - 'amount' => $tax['amount'], - 'sort_order' => $sort_order, - ]); - - $sort_order++; - } - } - // Added invoice sub total InvoiceTotal::create([ 'company_id' => $request['company_id'], @@ -1093,24 +1023,9 @@ class Invoices extends Controller $sort_order++; } - if (isset($taxes['PST'])) { - foreach ($taxes['PST'] as $tax) { - InvoiceTotal::create([ - 'company_id' => $request['company_id'], - 'invoice_id' => $invoice->id, - 'code' => 'pst-' . $tax['id'], - 'name' => $tax['name'], - 'amount' => $tax['amount'], - 'sort_order' => $sort_order, - ]); - - $sort_order++; - } - } - // Added invoice taxes - if (isset($taxes['VAT'])) { - foreach ($taxes['VAT'] as $tax) { + if (isset($taxes)) { + foreach ($taxes as $tax) { InvoiceTotal::create([ 'company_id' => $request['company_id'], 'invoice_id' => $invoice->id, diff --git a/app/Http/Controllers/Modals/Taxes.php b/app/Http/Controllers/Modals/Taxes.php new file mode 100644 index 000000000..9e909c3bf --- /dev/null +++ b/app/Http/Controllers/Modals/Taxes.php @@ -0,0 +1,63 @@ +middleware('permission:create-settings-taxes')->only(['create', 'store']); + $this->middleware('permission:read-settings-taxes')->only(['index', 'edit']); + $this->middleware('permission:update-settings-taxes')->only(['update', 'enable', 'disable']); + $this->middleware('permission:delete-settings-taxes')->only('destroy'); + } + + /** + * Show the form for creating a new resource. + * + * @return Response + */ + public function create() + { + $html = view('modals.taxes.create')->render(); + + return response()->json([ + 'success' => true, + 'error' => false, + 'message' => 'null', + 'html' => $html, + ]); + } + + /** + * Store a newly created resource in storage. + * + * @param Request $request + * + * @return Response + */ + public function store(Request $request) + { + $request['enabled'] = 1; + + $tax = Tax::create($request->all()); + + $message = trans('messages.success.added', ['type' => trans_choice('general.taxes', 1)]); + + return response()->json([ + 'success' => true, + 'error' => false, + 'data' => $tax, + 'message' => $message, + 'html' => 'null', + ]); + } +} diff --git a/app/Models/Expense/Bill.php b/app/Models/Expense/Bill.php index 3ae017282..3258163e1 100644 --- a/app/Models/Expense/Bill.php +++ b/app/Models/Expense/Bill.php @@ -82,6 +82,11 @@ class Bill extends Model return $this->hasMany('App\Models\Expense\BillItem'); } + public function itemTaxes() + { + return $this->hasMany('App\Models\Expense\BillItemTax'); + } + public function payments() { return $this->hasMany('App\Models\Expense\BillPayment'); diff --git a/app/Models/Expense/BillItem.php b/app/Models/Expense/BillItem.php index ae2c026c0..b40f6fdf5 100644 --- a/app/Models/Expense/BillItem.php +++ b/app/Models/Expense/BillItem.php @@ -29,6 +29,11 @@ class BillItem extends Model return $this->belongsTo('App\Models\Common\Item'); } + public function itemTaxes() + { + return $this->hasMany('App\Models\Expense\BillItemTax', 'bill_item_id', 'id'); + } + public function tax() { return $this->belongsTo('App\Models\Setting\Tax'); @@ -66,4 +71,23 @@ class BillItem extends Model { $this->attributes['tax'] = (double) $value; } + + /** + * Convert tax to double. + * + * @param string $value + * @return void + */ + public function getTaxIdAttribute($value) + { + $tax_ids = []; + + if (!empty($value)) { + $tax_ids[] = $value; + + return $tax_ids; + } + + return $this->itemTaxes->pluck('tax_id'); + } } diff --git a/app/Models/Expense/BillItemTax.php b/app/Models/Expense/BillItemTax.php new file mode 100644 index 000000000..8f7e2862a --- /dev/null +++ b/app/Models/Expense/BillItemTax.php @@ -0,0 +1,47 @@ +belongsTo('App\Models\Expense\Bill'); + } + + public function item() + { + return $this->belongsTo('App\Models\Common\Item'); + } + + public function tax() + { + return $this->belongsTo('App\Models\Setting\Tax'); + } + + /** + * Convert amount to double. + * + * @param string $value + * @return void + */ + public function setAmountAttribute($value) + { + $this->attributes['amount'] = (double) $value; + } +} diff --git a/app/Models/Income/Invoice.php b/app/Models/Income/Invoice.php index f07e31eb1..1e765293e 100644 --- a/app/Models/Income/Invoice.php +++ b/app/Models/Income/Invoice.php @@ -83,6 +83,11 @@ class Invoice extends Model return $this->hasMany('App\Models\Income\InvoiceItem'); } + public function itemTaxes() + { + return $this->hasMany('App\Models\Income\InvoiceItemTax'); + } + public function histories() { return $this->hasMany('App\Models\Income\InvoiceHistory'); diff --git a/app/Models/Income/InvoiceItem.php b/app/Models/Income/InvoiceItem.php index c6e50c9e8..dff192aec 100644 --- a/app/Models/Income/InvoiceItem.php +++ b/app/Models/Income/InvoiceItem.php @@ -29,6 +29,11 @@ class InvoiceItem extends Model return $this->belongsTo('App\Models\Common\Item'); } + public function itemTaxes() + { + return $this->hasMany('App\Models\Income\InvoiceItemTax', 'invoice_item_id', 'id'); + } + public function tax() { return $this->belongsTo('App\Models\Setting\Tax'); @@ -66,4 +71,23 @@ class InvoiceItem extends Model { $this->attributes['tax'] = (double) $value; } + + /** + * Convert tax to double. + * + * @param string $value + * @return void + */ + public function getTaxIdAttribute($value) + { + $tax_ids = []; + + if (!empty($value)) { + $tax_ids[] = $value; + + return $tax_ids; + } + + return $this->itemTaxes->pluck('tax_id'); + } } diff --git a/app/Models/Income/InvoiceItemTax.php b/app/Models/Income/InvoiceItemTax.php new file mode 100644 index 000000000..ddaea16ad --- /dev/null +++ b/app/Models/Income/InvoiceItemTax.php @@ -0,0 +1,47 @@ +belongsTo('App\Models\Income\Invoice'); + } + + public function item() + { + return $this->belongsTo('App\Models\Common\Item'); + } + + public function tax() + { + return $this->belongsTo('App\Models\Setting\Tax'); + } + + /** + * Convert amount to double. + * + * @param string $value + * @return void + */ + public function setAmountAttribute($value) + { + $this->attributes['amount'] = (double) $value; + } +} diff --git a/database/migrations/2018_10_22_000000_create_bill_item_taxes_table.php b/database/migrations/2018_10_22_000000_create_bill_item_taxes_table.php new file mode 100644 index 000000000..7338aca0d --- /dev/null +++ b/database/migrations/2018_10_22_000000_create_bill_item_taxes_table.php @@ -0,0 +1,39 @@ +increments('id'); + $table->integer('company_id'); + $table->integer('bill_id'); + $table->integer('bill_item_id'); + $table->integer('tax_id'); + $table->string('name'); + $table->double('amount', 15, 4)->default('0.0000'); + $table->timestamps(); + $table->softDeletes(); + + $table->index('company_id'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::drop('bill_item_taxes'); + } +} diff --git a/database/migrations/2018_10_22_000000_create_invoice_item_taxes_table.php b/database/migrations/2018_10_22_000000_create_invoice_item_taxes_table.php new file mode 100644 index 000000000..db24d8471 --- /dev/null +++ b/database/migrations/2018_10_22_000000_create_invoice_item_taxes_table.php @@ -0,0 +1,39 @@ +increments('id'); + $table->integer('company_id'); + $table->integer('invoice_id'); + $table->integer('invoice_item_id'); + $table->integer('tax_id'); + $table->string('name'); + $table->double('amount', 15, 4)->default('0.0000'); + $table->timestamps(); + $table->softDeletes(); + + $table->index('company_id'); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::drop('invoice_item_taxes'); + } +} diff --git a/public/css/app.css b/public/css/app.css index b910f65c8..0058aecdf 100644 --- a/public/css/app.css +++ b/public/css/app.css @@ -744,4 +744,32 @@ input[type="number"] { .col-md-6.input-group-invoice-text { padding-left: 0; padding-right: 0; +} + +#items .select2-container--default .select2-selection--multiple .select2-selection__choice { + background-color: #6da252; + border: 1px solid #6da252; + margin-bottom: 5px; +} + +#items .select2-container--default .select2-selection--multiple .select2-selection__choice__remove { + color: #fffdfd; +} + +#items span.select2.select2-container.select2-container--default .select2-selection.select2-selection--multiple { + border-color: #d2d6de; + border-radius: 0; +} + +#items .select2-container--default.select2-container--focus .select2-selection--multiple { + border-color: #6da252; + border-radius: 0; +} + +#tax-add-new { + padding: 6px 12px; +} + +#items .select2-search__field { + padding-left: 15px; } \ No newline at end of file diff --git a/resources/lang/en-GB/bills.php b/resources/lang/en-GB/bills.php index 2f8effef0..b8832c93d 100644 --- a/resources/lang/en-GB/bills.php +++ b/resources/lang/en-GB/bills.php @@ -39,14 +39,6 @@ return [ 'paid' => 'Paid', ], - 'taxes' => [ - 'position' => 'Position', - 'positions' => [ - 'before' => 'Before Subtotal (GST)', - 'after' => 'After Subtotal (PST)', - ], - ], - 'messages' => [ 'received' => 'Bill marked as received successfully!', ], diff --git a/resources/lang/en-GB/invoices.php b/resources/lang/en-GB/invoices.php index e662b7aba..512220c9b 100644 --- a/resources/lang/en-GB/invoices.php +++ b/resources/lang/en-GB/invoices.php @@ -42,14 +42,6 @@ return [ 'paid' => 'Paid', ], - 'taxes' => [ - 'position' => 'Position', - 'positions' => [ - 'before' => 'Before Subtotal (GST)', - 'after' => 'After Subtotal (PST)', - ], - ], - 'messages' => [ 'email_sent' => 'Invoice email has been sent successfully!', 'marked_sent' => 'Invoice marked as sent successfully!', diff --git a/resources/views/expenses/bills/create.blade.php b/resources/views/expenses/bills/create.blade.php index 7d1c7dc59..be9bb813f 100644 --- a/resources/views/expenses/bills/create.blade.php +++ b/resources/views/expenses/bills/create.blade.php @@ -96,7 +96,6 @@ @stack('tax_total_td_start') - {{ trans_choice('general.taxes', 1) }} 0 @@ -131,7 +130,7 @@ {{ Form::recurring('create') }} - {{ Form::fileGroup('attachment', trans('general.attachment'),[]) }} + {{ Form::fileGroup('attachment', trans('general.attachment')) }} {{ Form::hidden('vendor_name', old('vendor_name'), ['id' => 'vendor_name']) }} {{ Form::hidden('vendor_email', old('vendor_email'), ['id' => 'vendor_email']) }} @@ -170,7 +169,6 @@ @push('scripts') \ No newline at end of file diff --git a/routes/web.php b/routes/web.php index d084481cd..127316e8c 100644 --- a/routes/web.php +++ b/routes/web.php @@ -202,6 +202,15 @@ Route::group(['middleware' => 'language'], function () { ]]); Route::resource('invoices/{invoice}/payment', 'Modals\InvoicePayments', ['middleware' => ['dateformat', 'money']]); Route::resource('bills/{bill}/payment', 'Modals\BillPayments', ['middleware' => ['dateformat', 'money']]); + Route::resource('taxes', 'Modals\Taxes', ['names' => [ + 'index' => 'modals.taxes.index', + 'create' => 'modals.taxes.create', + 'store' => 'modals.taxes.store', + 'show' => 'modals.taxes.show', + 'edit' => 'modals.taxes.edit', + 'update' => 'modals.taxes.update', + 'destroy' => 'modals.taxes.destroy', + ]]); }); /* @deprecated */