From 081274c78e261e338bf5b22ab7290f4ffcea32f9 Mon Sep 17 00:00:00 2001 From: Finn Date: Tue, 22 May 2018 12:23:17 +0100 Subject: [PATCH 1/6] Added category all time purchase list --- lib/Pear/LocalLoop/Controller/Api/Stats.pm | 33 ++++++++++++++++++---- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/lib/Pear/LocalLoop/Controller/Api/Stats.pm b/lib/Pear/LocalLoop/Controller/Api/Stats.pm index 3a7579d..30c04b5 100644 --- a/lib/Pear/LocalLoop/Controller/Api/Stats.pm +++ b/lib/Pear/LocalLoop/Controller/Api/Stats.pm @@ -63,6 +63,8 @@ sub post_customer { my $entity = $c->stash->{api_user}->entity; + my $purchase_rs = $entity->purchases; + my $duration_weeks = DateTime::Duration->new( weeks => 7 ); my $end = DateTime->today; my $start_weeks = $end->clone->subtract_duration( $duration_weeks ); @@ -108,7 +110,7 @@ sub post_customer { my $sectors = { sectors => [], purchases => [] }; - my $sector_purchase_rs = $entity->purchases->search({}, + my $sector_purchase_rs = $purchase_rs->search({}, { join => { 'seller' => 'organisation' }, columns => { @@ -125,9 +127,32 @@ sub post_customer { push @{ $sectors->{ purchases } }, ($_->get_column('count') || 0); } - my $data = { cat_total => {}, categories => {}, essentials => {} }; + my $data = { cat_total => {}, categories => {}, essentials => {}, cat_list => {} }; - my $purchase_rs = $entity->purchases; + my $category_list = $c->schema->resultset('Category')->as_hash; + + my $category_purchase_rs = $purchase_rs->search({}, + { + join => 'category', + columns => { + category_id => "category.category_id", + value => { sum => 'value' }, + }, + group_by => "category.category_id", + } + ); + + my %cat_total_list; + + for ( $category_purchase_rs->all ) { + my $category = $_->get_column('category_id') || 0; + my $value = ($_->get_column('value') || 0) / 100000; + + $cat_total_list{$category_list->{$category}} += $value; + } + + my @cat_lists = map { { category => $_, value => $cat_total_list{$_} } } sort keys %cat_total_list; + $data->{cat_list} = [ sort { $b->{value} <=> $a->{value} } @cat_lists ]; my $purchase_no_essential_rs = $purchase_rs->search({ "me.essential" => 1, @@ -163,8 +188,6 @@ sub post_customer { } ); - my $category_list = $c->schema->resultset('Category')->as_hash; - for my $cat_trans ( $month_transaction_category_rs->all ) { my $quantised = $c->db_datetime_parser->parse_datetime($cat_trans->get_column('quantised')); my $days = $c->format_iso_date( $quantised ) || 0; From db71cab2d4f1c6530a493d2464235940f0292ccc Mon Sep 17 00:00:00 2001 From: Finn Date: Tue, 22 May 2018 12:47:19 +0100 Subject: [PATCH 2/6] Removed sector data API for customer Should eventually be given to organisation dashboard --- lib/Pear/LocalLoop/Controller/Api/Stats.pm | 20 -------------------- 1 file changed, 20 deletions(-) diff --git a/lib/Pear/LocalLoop/Controller/Api/Stats.pm b/lib/Pear/LocalLoop/Controller/Api/Stats.pm index 30c04b5..a467316 100644 --- a/lib/Pear/LocalLoop/Controller/Api/Stats.pm +++ b/lib/Pear/LocalLoop/Controller/Api/Stats.pm @@ -108,25 +108,6 @@ sub post_customer { count => $count, }; - my $sectors = { sectors => [], purchases => [] }; - - my $sector_purchase_rs = $purchase_rs->search({}, - { - join => { 'seller' => 'organisation' }, - columns => { - sector => "organisation.sector", - count => \"COUNT(*)", - }, - group_by => "organisation.sector", - order_by => { '-desc' => $c->pg_or_sqlite('count',"COUNT(*)",)}, - } - ); - - for ( $sector_purchase_rs->all ) { - push @{ $sectors->{ sectors } }, $_->get_column('sector'); - push @{ $sectors->{ purchases } }, ($_->get_column('count') || 0); - } - my $data = { cat_total => {}, categories => {}, essentials => {}, cat_list => {} }; my $category_list = $c->schema->resultset('Category')->as_hash; @@ -212,7 +193,6 @@ sub post_customer { success => Mojo::JSON->true, data => $data, weeks => $weeks, - sectors => $sectors, }); } From 07a36c22688723655aa86c79da5273bcc8d883b5 Mon Sep 17 00:00:00 2001 From: Thomas Bloor Date: Thu, 24 May 2018 15:16:28 +0100 Subject: [PATCH 3/6] Fix recurring transactions calculator for after-the-fact updates --- .../LocalLoop/Command/recur_transactions.pm | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/lib/Pear/LocalLoop/Command/recur_transactions.pm b/lib/Pear/LocalLoop/Command/recur_transactions.pm index e5bdeb3..7aab7ea 100644 --- a/lib/Pear/LocalLoop/Command/recur_transactions.pm +++ b/lib/Pear/LocalLoop/Command/recur_transactions.pm @@ -87,12 +87,11 @@ sub run { return; } - my $now = DateTime->now(); my $purchase_time = DateTime->new( - year => $now->year, - month => $now->month, - day => $now->day, - hour => $start_time_dt->hour, + year => $datetime->year, + month => $datetime->month, + day => $datetime->day, + hour => $start_time_dt->hour, minute => $start_time_dt->minute, second => $start_time_dt->second, time_zone => 'UTC', @@ -102,12 +101,12 @@ sub run { my $distance = $recur_result->distance; my $new_transaction = $schema->resultset('Transaction')->create({ - buyer_id => $recur_result->buyer_id, - seller_id => $recur_result->seller_id, - value => $recur_result->value, + buyer_id => $recur_result->buyer_id, + seller_id => $recur_result->seller_id, + value => $recur_result->value, purchase_time => $app->format_db_datetime($purchase_time), - distance => $distance, - essential => ( defined $essential ? $essential : 0 ), + distance => $distance, + essential => ( defined $essential ? $essential : 0 ), }); unless ( defined $new_transaction ) { From 2577b95605c7f6c8c075a6f4dfefeb361e838201 Mon Sep 17 00:00:00 2001 From: Finn Date: Mon, 4 Jun 2018 15:48:21 +0100 Subject: [PATCH 4/6] updated stats test --- t/api/stats.t | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/t/api/stats.t b/t/api/stats.t index c7922af..0c35519 100644 --- a/t/api/stats.t +++ b/t/api/stats.t @@ -55,10 +55,6 @@ $t->post_ok('/api/stats/customer' => json => { sum => 118, count => 6, }) - ->json_is('/sectors', { - sectors => ['A'], - purchases => [118], - }) ->json_is('/data', { cat_total => { Uncategorised => 810, @@ -88,6 +84,10 @@ $t->post_ok('/api/stats/customer' => json => { cat_total => { Uncategorised => 810, }, + cat_list => [{ + category => "Uncategorised", + value => 1180, + }], essentials => { purchase_no_essential_total => 0, purchase_no_total => 118, From dd5694b7937139b43e1f3961f8912b5f0091ee9b Mon Sep 17 00:00:00 2001 From: Finn Date: Tue, 5 Jun 2018 14:23:39 +0100 Subject: [PATCH 5/6] updated changelog --- CHANGELOG.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 12fa7ce..1c1895d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,10 @@ # Next Release +# v0.10.9 + +* Removed sector list from dashboard stats and swapped it for category list + # v0.10.8 * Added yearly recurring payments From b7b0076bf64f042f758597b2e4369db52bba0364 Mon Sep 17 00:00:00 2001 From: Finn Date: Tue, 5 Jun 2018 14:47:43 +0100 Subject: [PATCH 6/6] updated changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1c1895d..1490668 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,7 @@ # v0.10.9 * Removed sector list from dashboard stats and swapped it for category list +* Added fix to recurring transaction script # v0.10.8