From 588b56d71e28aa8a78832d67314f3a2cacccc9ce Mon Sep 17 00:00:00 2001 From: chris Date: Mon, 3 Feb 2025 11:55:52 +0100 Subject: [PATCH] Better information in dashboard tiles and prevent division by zero in Excel calculations. --- app/Http/Controllers/Api/ExcelController.php | 19 +++++++++++-------- resources/views/dashboard.blade.php | 4 ++-- 2 files changed, 13 insertions(+), 10 deletions(-) diff --git a/app/Http/Controllers/Api/ExcelController.php b/app/Http/Controllers/Api/ExcelController.php index 3d8d36d..80ea831 100644 --- a/app/Http/Controllers/Api/ExcelController.php +++ b/app/Http/Controllers/Api/ExcelController.php @@ -93,15 +93,17 @@ class ExcelController extends Controller $row = 2; foreach ($invoices as $invoice) { - foreach ($invoice->items as $item) { - if (!isset($net[$item->tax])) { - $net[$item->tax] = 0; - $tax[$item->tax] = 0; - $gross[$item->tax] = 0; + if ($invoice->sum != 0) { + foreach ($invoice->items as $item) { + if (!isset($net[$item->tax])) { + $net[$item->tax] = 0; + $tax[$item->tax] = 0; + $gross[$item->tax] = 0; + } + $net[$item->tax] += $item->amount * $item->price; + $tax[$item->tax] += $item->amount * $item->price * $item->tax / 100; + $gross[$item->tax] += $item->total; } - $net[$item->tax] += $item->amount * $item->price; - $tax[$item->tax] += $item->amount * $item->price * $item->tax / 100; - $gross[$item->tax] += $item->total; } $worksheet->setCellValue('A' . $row, $invoice->number); $worksheet->setCellValue('B' . $row, $invoice->customer->name); @@ -114,6 +116,7 @@ class ExcelController extends Controller $worksheet->setCellValue('F' . $row, $invoice->sum); $worksheet->setCellValue('G' . $row, Date::PHPToExcel(\DateTime::createFromFormat('!d.m.Y', $invoice->created))); $worksheet->getCell('G' . $row)->getStyle()->getNumberFormat()->setFormatCode('dd.mm.yyyy'); + $row++; } diff --git a/resources/views/dashboard.blade.php b/resources/views/dashboard.blade.php index 56730a4..9e421d8 100644 --- a/resources/views/dashboard.blade.php +++ b/resources/views/dashboard.blade.php @@ -23,7 +23,7 @@
{{ $invoice->number }}
-
{{ $invoice->address->name }}
+
{{ $invoice->customer->name }}
{{ \Illuminate\Support\Number::currency($invoice->sum) }}
{{ $invoice->created }}
@@ -39,7 +39,7 @@
{{ $invoice->number }}
-
{{ $invoice->address->name }}
+
{{ $invoice->customer->name }}
@endforeach