Add new year spend graph

This commit is contained in:
Tom Bloor 2019-07-15 01:33:07 +01:00
parent 0e99a9bf94
commit 41b7c24544
2 changed files with 50 additions and 0 deletions

View file

@ -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');

View file

@ -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;