More graph niceness

This commit is contained in:
Tom Bloor 2019-07-15 02:41:23 +01:00
parent 932af83ca5
commit d740846f48
3 changed files with 63 additions and 0 deletions

View file

@ -194,6 +194,7 @@ sub startup {
$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('/external/supplier_count')->to('api-external#post_supplier_count');
$api_v1_org->post('/pies')->to('api-v1-organisation-pies#index');

View file

@ -165,4 +165,52 @@ sub post_year_spend {
});
}
sub post_supplier_count {
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', 'seller_id' ],
order_by => { '-asc' => 'quantised_days' },
}
);
my @graph_data = (
map { {
count => $_->get_column('count'),
value => $_->get_column('total_spend'),
date => $_->get_column('quantised'),
seller => $_->seller->name,
} } $spend_rs->all,
);
return $c->render( json => {
success => Mojo::JSON->true,
data => \@graph_data,
});
}
1;

View file

@ -22,4 +22,18 @@ SELECT "value",
FROM "transactions"
/);
__PACKAGE__->belongs_to(
"buyer",
"Pear::LocalLoop::Schema::Result::Entity",
{ id => "buyer_id" },
{ is_deferrable => 0, on_delete => "NO ACTION", on_update => "NO ACTION" },
);
__PACKAGE__->belongs_to(
"seller",
"Pear::LocalLoop::Schema::Result::Entity",
{ id => "seller_id" },
{ is_deferrable => 0, on_delete => "NO ACTION", on_update => "NO ACTION" },
);
1;