From 795a502b76aad39c777cf03024b6ef21b8d88101 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=2E=20Nagy=20Gerg=C5=91?= Date: Fri, 29 Dec 2023 18:07:18 +0100 Subject: [PATCH] fix --- src/Fields/Repeater.php | 2 +- src/Widgets/Value.php | 20 +++++++++++++++++--- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/Fields/Repeater.php b/src/Fields/Repeater.php index 3a7e6d49..4f298574 100644 --- a/src/Fields/Repeater.php +++ b/src/Fields/Repeater.php @@ -121,7 +121,7 @@ public function withFields(Closure $callback): static { $this->optionFieldsResolver = function (Request $request, Model $model, Model $tmpModel) use ($callback): Fields { $fields = new Fields([ - Hidden::make(__('Key'), '_key') + Hidden::make(__('Key'), '_key'), ]); $fields->register(call_user_func_array($callback, [$request, $model, $tmpModel])); diff --git a/src/Widgets/Value.php b/src/Widgets/Value.php index 1f183b21..4c73b6ed 100644 --- a/src/Widgets/Value.php +++ b/src/Widgets/Value.php @@ -67,15 +67,29 @@ function (Builder $query) use ($period, $dateColumn): Builder { */ public function result(Builder $query): array { - $result = array_merge(['current' => 0, 'pervious' => 0], parent::result($query)); + $result = array_merge(['current' => 0, 'previous' => 0], parent::result($query)); return [ 'current' => round($result['current'], 2), - 'pervious' => round($result['previous'], 2), - 'trend' => round(($result['current'] - $result['previous']) / (($result['current'] + $result['previous']) / 2) * 100, 1), + 'previous' => round($result['previous'], 2), + 'trend' => $this->trend($result), ]; } + /** + * Calculate the trend value. + */ + protected function trend(array $result): float + { + $divider = ($result['current'] + $result['previous']); + + if ($divider == 0) { + return 0; + } + + return round(($result['current'] - $result['previous']) / ($divider / 2) * 100, 1); + } + /** * Convert the widget to an array. */