Supplier history view

This commit is contained in:
Tom Bloor 2019-07-15 04:45:57 +01:00
parent 9357405445
commit ed2b6970f4
No known key found for this signature in database
GPG key ID: 4657C7EBE42CC5CC
2 changed files with 183 additions and 45 deletions

View file

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

View file

@ -239,4 +239,141 @@ sub post_supplier_count {
}); });
} }
sub post_supplier_history {
my $c = shift;
my $user = $c->stash->{api_user};
# Temporary date lock for dev data
my $last = DateTime->new(
year => 2019,
month => 4,
day => 1
);
my $first = $last->clone->subtract(years => 1);
my $second = $last->clone->subtract(months => 6);
my $third = $last->clone->subtract(months => 3);
my $dtf = $c->schema->storage->datetime_parser;
my $year_rs = $c->schema->resultset('Transaction')->search(
{
'me.purchase_time' => {
-between => [
$dtf->format_datetime($first),
$dtf->format_datetime($last),
],
},
'me.buyer_id' => $user->entity->id,
},
{
join => { seller => 'organisation' },
columns => [
{
id => 'me.seller_id',
name => 'organisation.name',
count => \"COUNT(*)",
total_spend => { sum => 'me.value' },
}
],
group_by => 'me.seller_id',
order_by => { '-asc' => 'organisation.name' },
}
);
my $half_year_rs = $c->schema->resultset('Transaction')->search(
{
'me.purchase_time' => {
-between => [
$dtf->format_datetime($second),
$dtf->format_datetime($last),
],
},
'me.buyer_id' => $user->entity->id,
},
{
join => { seller => 'organisation' },
columns => [
{
id => 'me.seller_id',
name => 'organisation.name',
count => \"COUNT(*)",
total_spend => { sum => 'me.value' },
}
],
group_by => 'me.seller_id',
order_by => { '-asc' => 'organisation.name' },
}
);
my $quarter_year_rs = $c->schema->resultset('Transaction')->search(
{
'me.purchase_time' => {
-between => [
$dtf->format_datetime($third),
$dtf->format_datetime($last),
],
},
'me.buyer_id' => $user->entity->id,
},
{
join => { seller => 'organisation' },
columns => [
{
id => 'me.seller_id',
name => 'organisation.name',
count => \"COUNT(*)",
total_spend => { sum => 'me.value' },
}
],
group_by => 'me.seller_id',
order_by => { '-asc' => 'organisation.name' },
}
);
my %data;
for my $row ($year_rs->all) {
$data{$row->get_column('id')} = {
id => $row->get_column('id'),
name => $row->get_column('name'),
quarter_count => 0,
quarter_total => 0,
half_count => 0,
half_total => 0,
year_count => $row->get_column('count'),
year_total => $row->get_column('total_spend'),
};
}
for my $row ($half_year_rs->all) {
$data{$row->get_column('id')} = {
id => $row->get_column('id'),
name => $row->get_column('name'),
quarter_count => 0,
quarter_total => 0,
half_count => $row->get_column('count'),
half_total => $row->get_column('total_spend'),
year_count => 0,
year_total => 0,
%{$data{$row->get_column('id')}},
};
}
for my $row ($quarter_year_rs->all) {
$data{$row->get_column('id')} = {
id => $row->get_column('id'),
name => $row->get_column('name'),
quarter_count => $row->get_column('count'),
quarter_total => $row->get_column('total_spend'),
half_count => 0,
half_total => 0,
year_count => 0,
year_total => 0,
%{$data{$row->get_column('id')}},
};
}
return $c->render(json => {
success => Mojo::JSON->true,
data => [ values %data ],
});
}
1; 1;