2019-07-09 15:25:32 +00:00
|
|
|
package Pear::LocalLoop::Controller::Api::External;
|
|
|
|
use Mojo::Base 'Mojolicious::Controller';
|
|
|
|
use Mojo::JSON;
|
|
|
|
|
|
|
|
sub post_lcc_transactions {
|
|
|
|
my $c = shift;
|
|
|
|
|
|
|
|
my $user = $c->stash->{api_user};
|
|
|
|
|
|
|
|
# TODO Check the user is lancaster city council
|
|
|
|
|
|
|
|
my $validation = $c->validation;
|
2019-07-15 03:45:57 +00:00
|
|
|
$validation->input($c->stash->{api_json});
|
2019-07-09 15:25:32 +00:00
|
|
|
$validation->optional('page')->number;
|
2019-07-15 09:03:44 +00:00
|
|
|
$validation->optional('per_page')->number;
|
2019-07-15 09:11:59 +00:00
|
|
|
$validation->optional('search');
|
2019-07-09 15:25:32 +00:00
|
|
|
|
|
|
|
return $c->api_validation_error if $validation->has_error;
|
|
|
|
|
2019-07-15 11:36:28 +00:00
|
|
|
my $search_ref = { 'me.buyer_id' => $user->entity->id };
|
|
|
|
if ($validation->param('search')) {
|
|
|
|
$search_ref->{"organisation.name"} = { '-like' => join('', '%', $validation->param('search'), '%') };
|
2019-07-15 09:11:59 +00:00
|
|
|
}
|
2019-07-10 16:23:27 +00:00
|
|
|
|
2019-07-15 11:36:28 +00:00
|
|
|
my $lcc_transactions = $c->schema->resultset('Transaction')->search(
|
2019-07-15 09:11:59 +00:00
|
|
|
$search_ref,
|
2019-07-15 03:45:57 +00:00
|
|
|
{
|
|
|
|
page => $validation->param('page') || 1,
|
2019-07-15 09:03:44 +00:00
|
|
|
rows => $validation->param('per_page') || 10,
|
2019-07-15 09:11:59 +00:00
|
|
|
join => [ 'transaction', 'organisation' ],
|
2019-07-15 03:45:57 +00:00
|
|
|
order_by => { -desc => 'transaction.purchase_time' },
|
|
|
|
});
|
2019-07-09 15:25:32 +00:00
|
|
|
|
|
|
|
# purchase_time needs timezone attached to it
|
|
|
|
my @transaction_list = (
|
|
|
|
map {{
|
|
|
|
transaction_external_id => $_->external_id,
|
2019-07-15 03:45:57 +00:00
|
|
|
seller => $_->transaction->seller->name,
|
|
|
|
net_value => $_->transaction->meta->net_value,
|
|
|
|
gross_value => $_->transaction->meta->gross_value,
|
|
|
|
sales_tax_value => $_->transaction->meta->sales_tax_value,
|
|
|
|
purchase_time => $c->format_iso_datetime($_->transaction->purchase_time),
|
2019-07-10 16:23:27 +00:00
|
|
|
}} $lcc_transactions->all
|
2019-07-09 15:25:32 +00:00
|
|
|
);
|
|
|
|
|
2019-07-15 03:45:57 +00:00
|
|
|
return $c->render(json => {
|
|
|
|
success => Mojo::JSON->true,
|
2019-07-09 15:25:32 +00:00
|
|
|
transactions => \@transaction_list,
|
2019-07-15 03:45:57 +00:00
|
|
|
page_no => $lcc_transactions->pager->total_entries,
|
2019-07-09 15:25:32 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
sub post_lcc_suppliers {
|
|
|
|
my $c = shift;
|
|
|
|
|
|
|
|
my $user = $c->stash->{api_user};
|
|
|
|
|
|
|
|
# TODO give an error if user is not of Lancashire County Council
|
|
|
|
|
2019-07-12 17:51:38 +00:00
|
|
|
# my $is_lcc = $user->entity->organisation->count({ name => "Lancashire County Council" });
|
2019-07-09 15:25:32 +00:00
|
|
|
|
2019-07-12 19:04:38 +00:00
|
|
|
my $v = $c->validation;
|
2019-07-15 03:45:57 +00:00
|
|
|
$v->input($c->stash->{api_json});
|
2019-07-12 19:04:38 +00:00
|
|
|
$v->optional('page')->number;
|
|
|
|
$v->optional('sort_by');
|
|
|
|
$v->optional('sort_dir');
|
2019-09-09 17:03:08 +00:00
|
|
|
$v->optional('search');
|
2019-07-09 15:25:32 +00:00
|
|
|
|
2019-07-12 19:04:38 +00:00
|
|
|
my $order_by = [
|
|
|
|
{ -asc => 'organisation.name' },
|
|
|
|
];
|
2019-07-15 03:45:57 +00:00
|
|
|
if ($v->param('sort_by')) {
|
|
|
|
my %dirs = ('asc' => '-asc', 'desc' => '-desc');
|
2019-07-12 19:04:38 +00:00
|
|
|
my $dir = $dirs{$v->param('sort_dir')} // '-asc';
|
|
|
|
my %sorts = (
|
2019-07-15 03:45:57 +00:00
|
|
|
'name' => 'organisation.name',
|
2019-07-12 19:04:38 +00:00
|
|
|
'postcode' => 'organisation.postcode',
|
2019-07-15 03:45:57 +00:00
|
|
|
'spend' => 'total_spend',
|
2019-07-12 19:04:38 +00:00
|
|
|
);
|
|
|
|
my $sort = $sorts{$v->param('sort_by')} || 'organisation.name';
|
|
|
|
$order_by->[0] = { $dir => $sort };
|
|
|
|
}
|
|
|
|
|
|
|
|
return $c->api_validation_error if $v->has_error;
|
2019-07-09 15:25:32 +00:00
|
|
|
|
2019-07-14 18:41:02 +00:00
|
|
|
my $lcc_suppliers = $c->schema->resultset('Entity')->search(
|
|
|
|
{
|
|
|
|
'sales.buyer_id' => $user->entity->id,
|
2019-09-09 18:32:14 +00:00
|
|
|
($v->param('search') ? (
|
2019-09-09 17:03:08 +00:00
|
|
|
'-or' => [
|
|
|
|
{ 'organisation.name' => { 'like' => $v->param('search') . '%' } },
|
|
|
|
{ 'organisation.postcode' => { 'like' => $v->param('search') . '%' } },
|
|
|
|
]
|
2019-09-09 18:32:14 +00:00
|
|
|
) : ()),
|
2019-07-14 18:41:02 +00:00
|
|
|
},
|
|
|
|
{
|
2019-07-15 03:45:57 +00:00
|
|
|
join => [ 'sales', 'organisation' ],
|
|
|
|
group_by => [ 'me.id', 'organisation.id' ],
|
2019-07-14 18:41:02 +00:00
|
|
|
'+select' => [
|
|
|
|
{
|
|
|
|
'sum' => 'sales.value',
|
|
|
|
'-as' => 'total_spend',
|
|
|
|
}
|
|
|
|
],
|
2019-07-15 03:45:57 +00:00
|
|
|
'+as' => [ 'total_spend' ],
|
|
|
|
page => $v->param('page') || 1,
|
|
|
|
rows => 10,
|
|
|
|
order_by => $order_by,
|
2019-07-14 18:41:02 +00:00
|
|
|
}
|
|
|
|
);
|
2019-07-09 15:25:32 +00:00
|
|
|
|
|
|
|
my @supplier_list = (
|
|
|
|
map {{
|
2019-07-12 17:51:38 +00:00
|
|
|
entity_id => $_->id,
|
2019-07-15 03:45:57 +00:00
|
|
|
name => $_->name,
|
|
|
|
street => $_->organisation->street_name,
|
|
|
|
town => $_->organisation->town,
|
|
|
|
postcode => $_->organisation->postcode,
|
|
|
|
country => $_->organisation->country,
|
|
|
|
spend => ($_->get_column('total_spend') / 100000) // 0,
|
2019-07-09 15:25:32 +00:00
|
|
|
}} $lcc_suppliers->all
|
|
|
|
);
|
|
|
|
|
2019-07-15 03:45:57 +00:00
|
|
|
return $c->render(json => {
|
|
|
|
success => Mojo::JSON->true,
|
2019-07-09 15:25:32 +00:00
|
|
|
suppliers => \@supplier_list,
|
2019-07-15 03:45:57 +00:00
|
|
|
page_no => $lcc_suppliers->pager->total_entries,
|
2019-07-09 15:25:32 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-07-15 00:33:07 +00:00
|
|
|
sub post_year_spend {
|
|
|
|
my $c = shift;
|
|
|
|
|
|
|
|
my $user = $c->stash->{api_user};
|
|
|
|
|
2019-09-09 18:32:14 +00:00
|
|
|
my $v = $c->validation;
|
|
|
|
$v->input($c->stash->{api_json});
|
|
|
|
$v->required('from');
|
|
|
|
$v->required('to');
|
|
|
|
|
|
|
|
return $c->api_validation_error if $v->has_error;
|
|
|
|
|
|
|
|
my $last = $c->parse_iso_datetime($v->param('to'));
|
|
|
|
my $first = $c->parse_iso_datetime($v->param('from'));
|
2019-07-15 00:33:07 +00:00
|
|
|
|
|
|
|
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 = (
|
2019-07-15 03:45:57 +00:00
|
|
|
map {{
|
2019-07-15 00:33:07 +00:00
|
|
|
count => $_->get_column('count'),
|
2019-07-15 03:45:57 +00:00
|
|
|
value => ($_->get_column('total_spend') / 100000) // 0,
|
2019-07-15 00:55:02 +00:00
|
|
|
date => $_->get_column('quantised'),
|
2019-07-15 03:45:57 +00:00
|
|
|
}} $spend_rs->all,
|
2019-07-15 00:33:07 +00:00
|
|
|
);
|
|
|
|
|
2019-07-15 03:45:57 +00:00
|
|
|
return $c->render(json => {
|
2019-07-15 00:33:07 +00:00
|
|
|
success => Mojo::JSON->true,
|
|
|
|
data => \@graph_data,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-07-15 01:41:23 +00:00
|
|
|
sub post_supplier_count {
|
|
|
|
my $c = shift;
|
|
|
|
|
|
|
|
my $user = $c->stash->{api_user};
|
|
|
|
|
2019-09-09 18:32:14 +00:00
|
|
|
my $v = $c->validation;
|
|
|
|
$v->input($c->stash->{api_json});
|
|
|
|
$v->required('from');
|
|
|
|
$v->required('to');
|
|
|
|
|
|
|
|
return $c->api_validation_error if $v->has_error;
|
|
|
|
|
|
|
|
my $last = $c->parse_iso_datetime($v->param('to'));
|
|
|
|
my $first = $c->parse_iso_datetime($v->param('from'));
|
2019-07-15 01:41:23 +00:00
|
|
|
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
{
|
2019-09-09 18:32:14 +00:00
|
|
|
join => { 'seller' => 'organisation' },
|
|
|
|
select => [
|
2019-09-09 15:44:05 +00:00
|
|
|
{ count => 'me.value', '-as' => 'count' },
|
2019-09-09 14:37:26 +00:00
|
|
|
{ sum => 'me.value', '-as' => 'total_spend' },
|
|
|
|
'organisation.name',
|
|
|
|
'me.quantised_days',
|
2019-07-15 01:41:23 +00:00
|
|
|
],
|
2019-09-09 18:32:14 +00:00
|
|
|
as => [ qw/count total_spend name quantised_days/ ],
|
|
|
|
group_by => [ qw/me.quantised_days seller.id organisation.id/ ],
|
2019-09-09 14:37:26 +00:00
|
|
|
order_by => { '-asc' => 'me.quantised_days' },
|
2019-07-15 01:41:23 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
my @graph_data = (
|
2019-07-15 03:45:57 +00:00
|
|
|
map {{
|
2019-07-15 01:41:23 +00:00
|
|
|
count => $_->get_column('count'),
|
2019-07-15 03:28:44 +00:00
|
|
|
value => ($_->get_column('total_spend') / 100000) // 0,
|
2019-09-09 14:37:26 +00:00
|
|
|
date => $_->get_column('quantised_days'),
|
2019-09-09 15:42:49 +00:00
|
|
|
seller => $_->get_column('name'),
|
2019-07-15 03:45:57 +00:00
|
|
|
}} $spend_rs->all,
|
2019-07-15 01:41:23 +00:00
|
|
|
);
|
|
|
|
|
2019-07-15 03:45:57 +00:00
|
|
|
return $c->render(json => {
|
2019-07-15 01:41:23 +00:00
|
|
|
success => Mojo::JSON->true,
|
|
|
|
data => \@graph_data,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-07-15 03:45:57 +00:00
|
|
|
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;
|
2019-07-15 04:34:11 +00:00
|
|
|
my $year_rs = $c->schema->resultset('Entity')->search(
|
2019-07-15 03:45:57 +00:00
|
|
|
{
|
2019-07-15 03:54:55 +00:00
|
|
|
'sales.purchase_time' => {
|
2019-07-15 03:45:57 +00:00
|
|
|
-between => [
|
|
|
|
$dtf->format_datetime($first),
|
|
|
|
$dtf->format_datetime($last),
|
|
|
|
],
|
|
|
|
},
|
2019-07-15 03:54:55 +00:00
|
|
|
'sales.buyer_id' => $user->entity->id,
|
2019-07-15 03:45:57 +00:00
|
|
|
},
|
|
|
|
{
|
2019-07-15 03:54:55 +00:00
|
|
|
join => [ 'sales', 'organisation' ],
|
2019-07-15 03:45:57 +00:00
|
|
|
columns => [
|
|
|
|
{
|
2019-07-15 03:54:55 +00:00
|
|
|
id => 'me.id',
|
2019-07-15 03:45:57 +00:00
|
|
|
name => 'organisation.name',
|
|
|
|
count => \"COUNT(*)",
|
2019-07-15 03:54:55 +00:00
|
|
|
total_spend => { sum => 'sales.value' },
|
2019-07-15 03:45:57 +00:00
|
|
|
}
|
|
|
|
],
|
2019-07-15 03:54:55 +00:00
|
|
|
group_by => [ 'me.id', 'organisation.id' ],
|
2019-07-15 03:45:57 +00:00
|
|
|
order_by => { '-asc' => 'organisation.name' },
|
|
|
|
}
|
|
|
|
);
|
2019-07-15 04:34:11 +00:00
|
|
|
my $half_year_rs = $c->schema->resultset('Entity')->search(
|
2019-07-15 03:45:57 +00:00
|
|
|
{
|
2019-07-15 03:54:55 +00:00
|
|
|
'sales.purchase_time' => {
|
2019-07-15 03:45:57 +00:00
|
|
|
-between => [
|
|
|
|
$dtf->format_datetime($second),
|
|
|
|
$dtf->format_datetime($last),
|
|
|
|
],
|
|
|
|
},
|
2019-07-15 03:54:55 +00:00
|
|
|
'sales.buyer_id' => $user->entity->id,
|
2019-07-15 03:45:57 +00:00
|
|
|
},
|
|
|
|
{
|
2019-07-15 03:54:55 +00:00
|
|
|
join => [ 'sales', 'organisation' ],
|
2019-07-15 03:45:57 +00:00
|
|
|
columns => [
|
|
|
|
{
|
2019-07-15 03:54:55 +00:00
|
|
|
id => 'me.id',
|
2019-07-15 03:45:57 +00:00
|
|
|
name => 'organisation.name',
|
|
|
|
count => \"COUNT(*)",
|
2019-07-15 03:54:55 +00:00
|
|
|
total_spend => { sum => 'sales.value' },
|
2019-07-15 03:45:57 +00:00
|
|
|
}
|
|
|
|
],
|
2019-07-15 03:54:55 +00:00
|
|
|
group_by => [ 'me.id', 'organisation.id' ],
|
2019-07-15 03:45:57 +00:00
|
|
|
order_by => { '-asc' => 'organisation.name' },
|
|
|
|
}
|
|
|
|
);
|
2019-07-15 03:50:46 +00:00
|
|
|
my $quarter_year_rs = $c->schema->resultset('Entity')->search(
|
2019-07-15 03:45:57 +00:00
|
|
|
{
|
2019-07-15 03:50:46 +00:00
|
|
|
'sales.purchase_time' => {
|
2019-07-15 03:45:57 +00:00
|
|
|
-between => [
|
|
|
|
$dtf->format_datetime($third),
|
|
|
|
$dtf->format_datetime($last),
|
|
|
|
],
|
|
|
|
},
|
2019-07-15 03:50:46 +00:00
|
|
|
'sales.buyer_id' => $user->entity->id,
|
2019-07-15 03:45:57 +00:00
|
|
|
},
|
|
|
|
{
|
2019-07-15 03:50:46 +00:00
|
|
|
join => [ 'sales', 'organisation' ],
|
2019-07-15 03:45:57 +00:00
|
|
|
columns => [
|
|
|
|
{
|
2019-07-15 03:50:46 +00:00
|
|
|
id => 'me.id',
|
2019-07-15 03:45:57 +00:00
|
|
|
name => 'organisation.name',
|
|
|
|
count => \"COUNT(*)",
|
2019-07-15 03:50:46 +00:00
|
|
|
total_spend => { sum => 'sales.value' },
|
2019-07-15 03:45:57 +00:00
|
|
|
}
|
|
|
|
],
|
2019-07-15 03:52:55 +00:00
|
|
|
group_by => [ 'me.id', 'organisation.id' ],
|
2019-07-15 03:45:57 +00:00
|
|
|
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'),
|
2019-07-15 04:34:11 +00:00
|
|
|
year_total => $row->get_column('total_spend') / 100000,
|
2019-07-15 03:45:57 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
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'),
|
2019-07-15 04:34:11 +00:00
|
|
|
half_total => $row->get_column('total_spend') / 100000,
|
2019-07-15 03:45:57 +00:00
|
|
|
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'),
|
2019-07-15 04:34:11 +00:00
|
|
|
quarter_total => $row->get_column('total_spend') / 100000,
|
2019-07-15 03:45:57 +00:00
|
|
|
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 ],
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-09-06 16:31:09 +00:00
|
|
|
sub post_lcc_table_summary {
|
|
|
|
my $c = shift;
|
|
|
|
|
2019-09-09 18:32:14 +00:00
|
|
|
my $user = $c->stash->{api_user};
|
|
|
|
|
|
|
|
my $v = $c->validation;
|
|
|
|
$v->input($c->stash->{api_json});
|
|
|
|
$v->required('from');
|
|
|
|
$v->required('to');
|
|
|
|
|
|
|
|
return $c->api_validation_error if $v->has_error;
|
|
|
|
|
|
|
|
my $last = $c->parse_iso_datetime($v->param('to'));
|
|
|
|
my $first = $c->parse_iso_datetime($v->param('from'));
|
2019-09-06 16:31:09 +00:00
|
|
|
|
|
|
|
my $transaction_rs = $c->schema->resultset('Transaction');
|
|
|
|
|
2019-09-09 18:32:14 +00:00
|
|
|
my $dtf = $c->schema->storage->datetime_parser;
|
|
|
|
my $ward_transactions_rs = $transaction_rs->search(
|
2019-09-06 16:31:09 +00:00
|
|
|
{
|
2019-09-09 18:32:14 +00:00
|
|
|
purchase_time => {
|
|
|
|
-between => [
|
|
|
|
$dtf->format_datetime($first),
|
|
|
|
$dtf->format_datetime($last),
|
|
|
|
],
|
|
|
|
buyer_id => $user->entity->id,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
join => { seller => { postcode => { gb_postcode => 'ward' } } },
|
2019-09-06 16:31:09 +00:00
|
|
|
group_by => 'ward.id',
|
2019-09-09 18:32:14 +00:00
|
|
|
select => [
|
2019-09-06 16:31:09 +00:00
|
|
|
{ count => 'me.id', '-as' => 'count' },
|
|
|
|
{ sum => 'me.value', '-as' => 'sum' },
|
|
|
|
'ward.ward'
|
|
|
|
],
|
2019-09-09 18:32:14 +00:00
|
|
|
as => [ qw/count sum ward_name/ ],
|
2019-09-06 16:31:09 +00:00
|
|
|
}
|
|
|
|
);
|
|
|
|
|
|
|
|
my $transaction_type_data = {};
|
|
|
|
|
2019-09-09 15:24:07 +00:00
|
|
|
my %meta_names = (
|
2019-09-09 18:32:14 +00:00
|
|
|
local_service => "Local Services",
|
|
|
|
regional_service => "Regional Services",
|
|
|
|
national_service => "National Services",
|
2019-09-09 15:24:07 +00:00
|
|
|
private_household_rebate => "Private Household Rebates etc",
|
2019-09-09 18:32:14 +00:00
|
|
|
business_tax_and_rebate => "Business Tax & Service Rebates",
|
|
|
|
stat_loc_gov => "Statutory Loc Gov",
|
|
|
|
central_loc_gov => "Central Gov HMRC",
|
2019-09-09 15:24:07 +00:00
|
|
|
);
|
|
|
|
|
2019-09-09 18:32:14 +00:00
|
|
|
for my $meta (qw/
|
2019-09-06 16:31:09 +00:00
|
|
|
local_service
|
|
|
|
regional_service
|
|
|
|
national_service
|
|
|
|
private_household_rebate
|
|
|
|
business_tax_and_rebate
|
|
|
|
stat_loc_gov
|
|
|
|
central_loc_gov
|
2019-09-09 18:32:14 +00:00
|
|
|
/) {
|
2019-09-06 16:31:09 +00:00
|
|
|
my $transaction_type_rs = $transaction_rs->search(
|
|
|
|
{
|
2019-09-09 18:32:14 +00:00
|
|
|
'me.purchase_time' => {
|
|
|
|
-between => [
|
|
|
|
$dtf->format_datetime($first),
|
|
|
|
$dtf->format_datetime($last),
|
|
|
|
],
|
|
|
|
},
|
|
|
|
'me.buyer_id' => $user->entity->id,
|
|
|
|
'meta.' . $meta => 1,
|
2019-09-06 16:31:09 +00:00
|
|
|
},
|
|
|
|
{
|
2019-09-09 18:32:14 +00:00
|
|
|
join => 'meta',
|
2019-09-06 16:31:09 +00:00
|
|
|
group_by => 'meta.' . $meta,
|
2019-09-09 18:32:14 +00:00
|
|
|
select => [
|
2019-09-06 16:31:09 +00:00
|
|
|
{ count => 'me.id', '-as' => 'count' },
|
|
|
|
{ sum => 'me.value', '-as' => 'sum' },
|
|
|
|
],
|
2019-09-09 18:32:14 +00:00
|
|
|
as => [ qw/count sum/ ],
|
2019-09-06 16:31:09 +00:00
|
|
|
}
|
|
|
|
)->first;
|
|
|
|
|
|
|
|
$transaction_type_data->{$meta} = {
|
2019-09-09 18:32:14 +00:00
|
|
|
($transaction_type_rs ? (
|
2019-09-06 16:31:09 +00:00
|
|
|
count => $transaction_type_rs->get_column('count'),
|
2019-09-09 18:32:14 +00:00
|
|
|
sum => $transaction_type_rs->get_column('sum'),
|
|
|
|
type => $meta_names{$meta},
|
|
|
|
) : (
|
2019-09-09 15:24:07 +00:00
|
|
|
count => 0,
|
2019-09-09 18:32:14 +00:00
|
|
|
sum => 0,
|
|
|
|
type => $meta_names{$meta},
|
|
|
|
)),
|
2019-09-06 16:31:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
my @ward_transaction_list = (
|
|
|
|
map {{
|
2019-09-09 18:32:14 +00:00
|
|
|
ward => $_->get_column('ward_name') || "N/A",
|
|
|
|
sum => $_->get_column('sum') / 100000,
|
2019-09-06 16:31:09 +00:00
|
|
|
count => $_->get_column('count'),
|
|
|
|
}} $ward_transactions_rs->all
|
|
|
|
);
|
|
|
|
|
2019-09-09 18:32:14 +00:00
|
|
|
return $c->render(json => {
|
2019-09-06 16:31:09 +00:00
|
|
|
success => Mojo::JSON->true,
|
2019-09-09 18:32:14 +00:00
|
|
|
wards => \@ward_transaction_list,
|
|
|
|
types => $transaction_type_data,
|
2019-09-06 16:31:09 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-07-09 15:25:32 +00:00
|
|
|
1;
|