fixed api #46
This commit is contained in:
parent
7278e9a061
commit
fc9e62dc8d
@ -11,7 +11,7 @@ use App\Models\Income\Invoice;
|
||||
use App\Models\Income\InvoiceHistory;
|
||||
use App\Models\Income\InvoiceItem;
|
||||
use App\Models\Income\InvoicePayment;
|
||||
use App\Models\Income\InvoiceStatus;
|
||||
use App\Models\Income\InvoiceTotal;
|
||||
use App\Models\Item\Item;
|
||||
use App\Models\Setting\Tax;
|
||||
use Dingo\Api\Routing\Helpers;
|
||||
@ -53,6 +53,10 @@ class Invoices extends ApiController
|
||||
{
|
||||
$invoice = Invoice::create($request->all());
|
||||
|
||||
$taxes = [];
|
||||
$tax_total = 0;
|
||||
$sub_total = 0;
|
||||
|
||||
$invoice_item = array();
|
||||
$invoice_item['company_id'] = $request['company_id'];
|
||||
$invoice_item['invoice_id'] = $invoice->id;
|
||||
@ -91,16 +95,38 @@ class Invoices extends ApiController
|
||||
$invoice_item['price'] = $item['price'];
|
||||
$invoice_item['tax'] = $tax;
|
||||
$invoice_item['tax_id'] = $tax_id;
|
||||
$invoice_item['total'] = ($item['price'] + $invoice_item['tax']) * $item['quantity'];
|
||||
|
||||
$request['amount'] += $invoice_item['total'];
|
||||
$invoice_item['total'] = $item['price'] * $item['quantity'];
|
||||
|
||||
InvoiceItem::create($invoice_item);
|
||||
|
||||
if (isset($tax_object)) {
|
||||
if (array_key_exists($tax_object->id, $taxes)) {
|
||||
$taxes[$tax_object->id]['amount'] += $tax;
|
||||
} else {
|
||||
$taxes[$tax_object->id] = [
|
||||
'name' => $tax_object->name,
|
||||
'amount' => $tax
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
$tax_total += $tax;
|
||||
$sub_total += $invoice_item['total'];
|
||||
|
||||
unset($item_object);
|
||||
unset($tax_object);
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($request['amount'])) {
|
||||
$request['amount'] += $sub_total + $tax_total;
|
||||
}
|
||||
|
||||
$invoice->update($request->input());
|
||||
|
||||
// Add invoice totals
|
||||
$this->addTotals($invoice, $request, $taxes, $sub_total, $tax_total);
|
||||
|
||||
$request['invoice_id'] = $invoice->id;
|
||||
$request['status_code'] = $request['invoice_status_code'];
|
||||
$request['notify'] = 0;
|
||||
@ -123,6 +149,10 @@ class Invoices extends ApiController
|
||||
*/
|
||||
public function update(Invoice $invoice, Request $request)
|
||||
{
|
||||
$taxes = [];
|
||||
$tax_total = 0;
|
||||
$sub_total = 0;
|
||||
|
||||
$invoice_item = array();
|
||||
$invoice_item['company_id'] = $request['company_id'];
|
||||
$invoice_item['invoice_id'] = $invoice->id;
|
||||
@ -163,16 +193,42 @@ class Invoices extends ApiController
|
||||
$invoice_item['price'] = $item['price'];
|
||||
$invoice_item['tax'] = $tax;
|
||||
$invoice_item['tax_id'] = $tax_id;
|
||||
$invoice_item['total'] = ($item['price'] + $invoice_item['tax']) * $item['quantity'];
|
||||
$invoice_item['total'] = $item['price'] * $item['quantity'];
|
||||
|
||||
$request['amount'] += $invoice_item['total'];
|
||||
|
||||
InvoiceItem::create($invoice_item);
|
||||
|
||||
if (isset($tax_object)) {
|
||||
if (array_key_exists($tax_object->id, $taxes)) {
|
||||
$taxes[$tax_object->id]['amount'] += $tax;
|
||||
} else {
|
||||
$taxes[$tax_object->id] = [
|
||||
'name' => $tax_object->name,
|
||||
'amount' => $tax
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
$tax_total += $tax;
|
||||
$sub_total += $invoice_item['total'];
|
||||
|
||||
unset($item_object);
|
||||
unset($tax_object);
|
||||
}
|
||||
}
|
||||
|
||||
if (empty($request['amount'])) {
|
||||
$request['amount'] += $sub_total + $tax_total;
|
||||
}
|
||||
|
||||
$invoice->update($request->input());
|
||||
|
||||
// Delete previous invoice totals
|
||||
InvoiceTotal::where('invoice_id', $invoice->id)->delete();
|
||||
|
||||
$this->addTotals($invoice, $request, $taxes, $sub_total, $tax_total);
|
||||
|
||||
// Fire the event to make it extendible
|
||||
event(new InvoiceUpdated($invoice));
|
||||
|
||||
@ -195,4 +251,76 @@ class Invoices extends ApiController
|
||||
|
||||
return $this->response->noContent();
|
||||
}
|
||||
|
||||
protected function addTotals($invoice, $request, $taxes, $sub_total, $tax_total) {
|
||||
// Add invoice total taxes
|
||||
if ($request['totals']) {
|
||||
$sort_order = 1;
|
||||
|
||||
foreach ($request['totals'] as $total) {
|
||||
if (!empty($total['sort_order'])) {
|
||||
$sort_order = $total['sort_order'];
|
||||
}
|
||||
|
||||
$invoice_total = [
|
||||
'company_id' => $request['company_id'],
|
||||
'invoice_id' => $invoice->id,
|
||||
'code' => $total['code'],
|
||||
'name' => $total['name'],
|
||||
'amount' => $total['amount'],
|
||||
'sort_order' => $sort_order,
|
||||
];
|
||||
|
||||
InvoiceTotal::create($invoice_total);
|
||||
|
||||
if (empty($total['sort_order'])) {
|
||||
$sort_order++;
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Added invoice total sub total
|
||||
$invoice_sub_total = [
|
||||
'company_id' => $request['company_id'],
|
||||
'invoice_id' => $invoice->id,
|
||||
'code' => 'sub_total',
|
||||
'name' => 'invoices.sub_total',
|
||||
'amount' => $sub_total,
|
||||
'sort_order' => 1,
|
||||
];
|
||||
|
||||
InvoiceTotal::create($invoice_sub_total);
|
||||
|
||||
$sort_order = 2;
|
||||
|
||||
// Added invoice total taxes
|
||||
if ($taxes) {
|
||||
foreach ($taxes as $tax) {
|
||||
$invoice_tax_total = [
|
||||
'company_id' => $request['company_id'],
|
||||
'invoice_id' => $invoice->id,
|
||||
'code' => 'tax',
|
||||
'name' => $tax['name'],
|
||||
'amount' => $tax['amount'],
|
||||
'sort_order' => $sort_order,
|
||||
];
|
||||
|
||||
InvoiceTotal::create($invoice_tax_total);
|
||||
|
||||
$sort_order++;
|
||||
}
|
||||
}
|
||||
|
||||
// Added invoice total total
|
||||
$invoice_total = [
|
||||
'company_id' => $request['company_id'],
|
||||
'invoice_id' => $invoice->id,
|
||||
'code' => 'total',
|
||||
'name' => 'invoices.total',
|
||||
'amount' => $sub_total + $tax_total,
|
||||
'sort_order' => $sort_order,
|
||||
];
|
||||
|
||||
InvoiceTotal::create($invoice_total);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -282,7 +282,6 @@ class Invoices extends Controller
|
||||
|
||||
if ($request['item']) {
|
||||
foreach ($request['item'] as $item) {
|
||||
unset($tax_object);
|
||||
$item_sku = '';
|
||||
|
||||
if (!empty($item['item_id'])) {
|
||||
@ -310,6 +309,9 @@ class Invoices extends Controller
|
||||
$invoice_item['tax_id'] = $tax_id;
|
||||
$invoice_item['total'] = $item['price'] * $item['quantity'];
|
||||
|
||||
InvoiceItem::create($invoice_item);
|
||||
|
||||
// Set taxes
|
||||
if (isset($tax_object)) {
|
||||
if (array_key_exists($tax_object->id, $taxes)) {
|
||||
$taxes[$tax_object->id]['amount'] += $tax;
|
||||
@ -321,10 +323,11 @@ class Invoices extends Controller
|
||||
}
|
||||
}
|
||||
|
||||
// Calculate totals
|
||||
$tax_total += $tax;
|
||||
$sub_total += $invoice_item['total'];
|
||||
|
||||
InvoiceItem::create($invoice_item);
|
||||
unset($tax_object);
|
||||
}
|
||||
}
|
||||
|
||||
@ -332,49 +335,8 @@ class Invoices extends Controller
|
||||
|
||||
$invoice->update($request->input());
|
||||
|
||||
// Added invoice total sub total
|
||||
$invoice_sub_total = [
|
||||
'company_id' => $request['company_id'],
|
||||
'invoice_id' => $invoice->id,
|
||||
'code' => 'sub_total',
|
||||
'name' => 'invoices.sub_total',
|
||||
'amount' => $sub_total,
|
||||
'sort_order' => 1,
|
||||
];
|
||||
|
||||
InvoiceTotal::create($invoice_sub_total);
|
||||
|
||||
$sort_order = 2;
|
||||
|
||||
// Added invoice total taxes
|
||||
if ($taxes) {
|
||||
foreach ($taxes as $tax) {
|
||||
$invoice_tax_total = [
|
||||
'company_id' => $request['company_id'],
|
||||
'invoice_id' => $invoice->id,
|
||||
'code' => 'tax',
|
||||
'name' => $tax['name'],
|
||||
'amount' => $tax['amount'],
|
||||
'sort_order' => $sort_order,
|
||||
];
|
||||
|
||||
InvoiceTotal::create($invoice_tax_total);
|
||||
|
||||
$sort_order++;
|
||||
}
|
||||
}
|
||||
|
||||
// Added invoice total total
|
||||
$invoice_total = [
|
||||
'company_id' => $request['company_id'],
|
||||
'invoice_id' => $invoice->id,
|
||||
'code' => 'total',
|
||||
'name' => 'invoices.total',
|
||||
'amount' => $sub_total + $tax_total,
|
||||
'sort_order' => $sort_order,
|
||||
];
|
||||
|
||||
InvoiceTotal::create($invoice_total);
|
||||
// Add invoice totals
|
||||
$this->addTotals($invoice, $request, $taxes, $sub_total, $tax_total);
|
||||
|
||||
$request['invoice_id'] = $invoice->id;
|
||||
$request['status_code'] = 'draft';
|
||||
@ -507,55 +469,17 @@ class Invoices extends Controller
|
||||
}
|
||||
}
|
||||
|
||||
$request['amount'] += $sub_total + $tax_total;
|
||||
if (empty($request['amount'])) {
|
||||
$request['amount'] += $sub_total + $tax_total;
|
||||
}
|
||||
|
||||
$invoice->update($request->input());
|
||||
|
||||
// Delete previous invoice totals
|
||||
InvoiceTotal::where('invoice_id', $invoice->id)->delete();
|
||||
|
||||
// Added invoice total sub total
|
||||
$invoice_sub_total = [
|
||||
'company_id' => $request['company_id'],
|
||||
'invoice_id' => $invoice->id,
|
||||
'code' => 'sub_total',
|
||||
'name' => 'invoices.sub_total',
|
||||
'amount' => $sub_total,
|
||||
'sort_order' => 1,
|
||||
];
|
||||
|
||||
InvoiceTotal::create($invoice_sub_total);
|
||||
|
||||
$sort_order = 2;
|
||||
|
||||
// Added invoice total taxes
|
||||
if ($taxes) {
|
||||
foreach ($taxes as $tax) {
|
||||
$invoice_tax_total = [
|
||||
'company_id' => $request['company_id'],
|
||||
'invoice_id' => $invoice->id,
|
||||
'code' => 'tax',
|
||||
'name' => $tax['name'],
|
||||
'amount' => $tax['amount'],
|
||||
'sort_order' => $sort_order,
|
||||
];
|
||||
|
||||
InvoiceTotal::create($invoice_tax_total);
|
||||
|
||||
$sort_order++;
|
||||
}
|
||||
}
|
||||
|
||||
// Added invoice total total
|
||||
$invoice_total = [
|
||||
'company_id' => $request['company_id'],
|
||||
'invoice_id' => $invoice->id,
|
||||
'code' => 'total',
|
||||
'name' => 'invoices.total',
|
||||
'amount' => $sub_total + $tax_total,
|
||||
'sort_order' => $sort_order,
|
||||
];
|
||||
|
||||
InvoiceTotal::create($invoice_total);
|
||||
// Add invoice totals
|
||||
$this->addTotals($invoice, $request, $taxes, $sub_total, $tax_total);
|
||||
|
||||
// Fire the event to make it extendible
|
||||
event(new InvoiceUpdated($invoice));
|
||||
@ -613,4 +537,53 @@ class Invoices extends Controller
|
||||
|
||||
return redirect('incomes/invoices');
|
||||
}
|
||||
|
||||
protected function addTotals($invoice, $request, $taxes, $sub_total, $tax_total)
|
||||
{
|
||||
$sort_order = 1;
|
||||
|
||||
// Added invoice total sub total
|
||||
$invoice_sub_total = [
|
||||
'company_id' => $request['company_id'],
|
||||
'invoice_id' => $invoice->id,
|
||||
'code' => 'sub_total',
|
||||
'name' => 'invoices.sub_total',
|
||||
'amount' => $sub_total,
|
||||
'sort_order' => $sort_order,
|
||||
];
|
||||
|
||||
InvoiceTotal::create($invoice_sub_total);
|
||||
|
||||
$sort_order++;
|
||||
|
||||
// Added invoice total taxes
|
||||
if ($taxes) {
|
||||
foreach ($taxes as $tax) {
|
||||
$invoice_tax_total = [
|
||||
'company_id' => $request['company_id'],
|
||||
'invoice_id' => $invoice->id,
|
||||
'code' => 'tax',
|
||||
'name' => $tax['name'],
|
||||
'amount' => $tax['amount'],
|
||||
'sort_order' => $sort_order,
|
||||
];
|
||||
|
||||
InvoiceTotal::create($invoice_tax_total);
|
||||
|
||||
$sort_order++;
|
||||
}
|
||||
}
|
||||
|
||||
// Added invoice total total
|
||||
$invoice_total = [
|
||||
'company_id' => $request['company_id'],
|
||||
'invoice_id' => $invoice->id,
|
||||
'code' => 'total',
|
||||
'name' => 'invoices.total',
|
||||
'amount' => $sub_total + $tax_total,
|
||||
'sort_order' => $sort_order,
|
||||
];
|
||||
|
||||
InvoiceTotal::create($invoice_total);
|
||||
}
|
||||
}
|
||||
|
@ -11,7 +11,7 @@ return [
|
||||
'currencies_gbp' => 'British Pound',
|
||||
'currencies_try' => 'Turkish Lira',
|
||||
'taxes_exempt' => 'Tax Exempt',
|
||||
'taxes_normal' => 'Normal',
|
||||
'taxes_normal' => 'Normal Tax',
|
||||
'taxes_sales' => 'Sales Tax',
|
||||
|
||||
];
|
||||
|
@ -92,7 +92,7 @@
|
||||
</td>
|
||||
<td class="text-center">{{ $item->quantity }}</td>
|
||||
<td class="text-right">@money($item->price, $bill->currency_code, true)</td>
|
||||
<td class="text-right">@money($item->total - $item->tax, $bill->currency_code, true)</td>
|
||||
<td class="text-right">@money($item->total, $bill->currency_code, true)</td>
|
||||
</tr>
|
||||
@endforeach
|
||||
</tbody>
|
||||
|
@ -92,7 +92,7 @@
|
||||
</td>
|
||||
<td class="text-center">{{ $item->quantity }}</td>
|
||||
<td class="text-right">@money($item->price, $invoice->currency_code, true)</td>
|
||||
<td class="text-right">@money($item->total - $item->tax, $invoice->currency_code, true)</td>
|
||||
<td class="text-right">@money($item->total, $invoice->currency_code, true)</td>
|
||||
</tr>
|
||||
@endforeach
|
||||
</tbody>
|
||||
|
Loading…
x
Reference in New Issue
Block a user