From 3fa8534431359fd2d24e90c56282c92b59066c9d Mon Sep 17 00:00:00 2001 From: Finn Date: Fri, 24 Aug 2018 15:22:59 +0100 Subject: [PATCH] fixed week display --- lib/Pear/LocalLoop/Controller/Api/Stats.pm | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/lib/Pear/LocalLoop/Controller/Api/Stats.pm b/lib/Pear/LocalLoop/Controller/Api/Stats.pm index db46fd0..9eea160 100644 --- a/lib/Pear/LocalLoop/Controller/Api/Stats.pm +++ b/lib/Pear/LocalLoop/Controller/Api/Stats.pm @@ -93,9 +93,23 @@ sub post_customer { } ); + use Devel::Dwarn; + + my $date_formatter = DateTime::Format::Strptime->new( + pattern => '%Y-%m-%d %H-%M-%S' + ); + my @all_weeks = $week_transaction_rs->all; - my $first = defined $all_weeks[0] ? $all_weeks[0]->get_column('count') || 0 : 0; - my $second = defined $all_weeks[1] ? $all_weeks[1]->get_column('count') || 0 : 0; + my ( $first, $second ) = (0,0); + my $this_week_object = $end->truncate( to => 'week' ); + my $this_week = $date_formatter->parse_datetime($this_week_object); + if ($all_weeks[0]->get_column('quantised') eq $this_week) { + $first = defined $all_weeks[0] ? $all_weeks[0]->get_column('count') || 0 : 0; + } + my $last_week = $date_formatter->parse_datetime($this_week_object->subtract( weeks => 1 )); + if (defined $all_weeks[1] && $all_weeks[1]->get_column('quantised') eq $last_week) { + $second = defined $all_weeks[1] ? $all_weeks[1]->get_column('count') || 0 : 0; + } my $max = max( map { $_->get_column('count') } @all_weeks ); my $sum = sum( map { $_->get_column('count') } @all_weeks ); my $count = $week_transaction_rs->count;