From 47fce2d163845601763fa03ea58f2aa24bd90222 Mon Sep 17 00:00:00 2001 From: Hillel Coren Date: Wed, 26 Apr 2017 19:50:43 +0300 Subject: [PATCH] Report sorting is broken with comma decimal separator #1438 --- app/Ninja/Reports/AbstractReport.php | 35 +++++++++++++++++++ .../views/reports/chart_builder.blade.php | 18 ++++++++-- 2 files changed, 51 insertions(+), 2 deletions(-) diff --git a/app/Ninja/Reports/AbstractReport.php b/app/Ninja/Reports/AbstractReport.php index 4b6ffa080c6e..e327c60b7170 100644 --- a/app/Ninja/Reports/AbstractReport.php +++ b/app/Ninja/Reports/AbstractReport.php @@ -80,4 +80,39 @@ class AbstractReport return $str; } + + // convert the date format to one supported by tablesorter + public function convertDateFormat() + { + $account = Auth::user()->account; + $format = $account->getMomentDateFormat(); + $format = strtolower($format); + $format = str_replace('do', '', $format); + + $orignalFormat = $format; + $format = preg_replace("/[^mdy]/", '', $format); + + $lastLetter = false; + $reportParts = []; + $phpParts = []; + + foreach (str_split($format) as $letter) { + if ($lastLetter && $letter == $lastLetter) { + continue; + } + $lastLetter = $letter; + if ($letter == 'm') { + $reportParts[] = 'mm'; + $phpParts[] = 'm'; + } elseif ($letter == 'd') { + $reportParts[] = 'dd'; + $phpParts[] = 'd'; + } elseif ($letter == 'y') { + $reportParts[] = 'yyyy'; + $phpParts[] = 'Y'; + } + } + + return join('', $reportParts); + } } diff --git a/resources/views/reports/chart_builder.blade.php b/resources/views/reports/chart_builder.blade.php index 8a282d71d334..8146617819f3 100644 --- a/resources/views/reports/chart_builder.blade.php +++ b/resources/views/reports/chart_builder.blade.php @@ -288,6 +288,14 @@ } }); + // parse 1,000.00 or 1.000,00 + function convertStringToNumber(str) { + str = str + '' || ''; + var number = Number(str.replace(/[^0-9]+/g,"")); + return number / 100; + } + + $(function(){ $(".tablesorter-data").tablesorter({ @if (! request()->group_when_sorted) @@ -296,6 +304,12 @@ theme: 'bootstrap', widgets: ['zebra', 'uitheme', 'filter'{!! request()->group_when_sorted ? ", 'group'" : "" !!}, 'columnSelector'], headerTemplate : '{content} {icon}', + dateFormat: '{{ $report->convertDateFormat() }}', + numberSorter: function(a, b, direction) { + var a = convertStringToNumber(a); + var b = convertStringToNumber(b); + return direction ? a - b : b - a; + }, widgetOptions : { columnSelector_container : $('#columnSelector'), filter_cssFilter: 'form-control', @@ -310,8 +324,8 @@ } var subtotal = 0; $rows.each(function() { - var txt = $(this).find("td").eq(i).text().replace(/[$,]/g, ''); - subtotal += parseFloat(txt || 0); + var txt = $(this).find("td").eq(i).text(); + subtotal += convertStringToNumber(txt); }); $cell.find(".group-count").append(' - ' + label + ': ' + roundToTwo(subtotal)); }