From 06b08cff07c6759a675ef5dbeea4df7c3ff92fbb Mon Sep 17 00:00:00 2001 From: Tom Bloor Date: Mon, 15 Jul 2019 01:33:07 +0100 Subject: [PATCH] Add new year spend graph --- lib/Pear/LocalLoop.pm | 3 ++ lib/Pear/LocalLoop/Controller/Api/External.pm | 47 +++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/lib/Pear/LocalLoop.pm b/lib/Pear/LocalLoop.pm index 07da9fa..eccb0a3 100644 --- a/lib/Pear/LocalLoop.pm +++ b/lib/Pear/LocalLoop.pm @@ -190,8 +190,11 @@ sub startup { $api_v1_org->post('/supplier/add')->to('api-organisation#post_supplier_add'); $api_v1_org->post('/employee')->to('api-organisation#post_employee_read'); $api_v1_org->post('/employee/add')->to('api-organisation#post_employee_add'); + $api_v1_org->post('/external/transactions')->to('api-external#post_lcc_transactions'); $api_v1_org->post('/external/suppliers')->to('api-external#post_lcc_suppliers'); + $api_v1_org->post('/external/year_spend')->to('api-external#post_year_spend'); + $api_v1_org->post('/pies')->to('api-v1-organisation-pies#index'); my $api_v1_cust = $api_v1->under('/customer')->to('api-v1-customer#auth'); diff --git a/lib/Pear/LocalLoop/Controller/Api/External.pm b/lib/Pear/LocalLoop/Controller/Api/External.pm index 3a1cb29..e671201 100644 --- a/lib/Pear/LocalLoop/Controller/Api/External.pm +++ b/lib/Pear/LocalLoop/Controller/Api/External.pm @@ -118,4 +118,51 @@ sub post_lcc_suppliers { }); } +sub post_year_spend { + my $c = shift; + + my $user = $c->stash->{api_user}; + + my $last = DateTime->today; + my $first = $last->clone->subtract( years => 1 ); + + my $dtf = $c->schema->storage->datetime_parser; + my $driver = $c->schema->storage->dbh->{Driver}->{Name}; + my $spend_rs = $c->schema->resultset('ViewQuantisedTransaction' . $driver)->search( + { + purchase_time => { + -between => [ + $dtf->format_datetime($first), + $dtf->format_datetime($last), + ], + }, + buyer_id => $user->entity->id, + }, + { + columns => [ + { + quantised => 'quantised_days', + count => \"COUNT(*)", + total_spend => { sum => 'value' }, + } + ], + group_by => 'quantised_days', + order_by => { '-asc' => 'quantised_days' }, + } + ); + + my @graph_data = ( + map { { + count => $_->get_column('count'), + value => $_->get_column('total_spend'), + date => $_->get_column('quantised_days'), + } } $spend_rs->all, + ); + + return $c->render( json => { + success => Mojo::JSON->true, + data => \@graph_data, + }); +} + 1;