filter everything by date

This commit is contained in:
Thomas Bloor 2019-09-09 19:32:14 +01:00
parent 653f495a70
commit 103cf61ec6
No known key found for this signature in database
GPG key ID: 4657C7EBE42CC5CC

View file

@ -86,12 +86,12 @@ sub post_lcc_suppliers {
my $lcc_suppliers = $c->schema->resultset('Entity')->search( my $lcc_suppliers = $c->schema->resultset('Entity')->search(
{ {
'sales.buyer_id' => $user->entity->id, 'sales.buyer_id' => $user->entity->id,
( $v->param('search') ? ( ($v->param('search') ? (
'-or' => [ '-or' => [
{ 'organisation.name' => { 'like' => $v->param('search') . '%' } }, { 'organisation.name' => { 'like' => $v->param('search') . '%' } },
{ 'organisation.postcode' => { 'like' => $v->param('search') . '%' } }, { 'organisation.postcode' => { 'like' => $v->param('search') . '%' } },
] ]
) : () ), ) : ()),
}, },
{ {
join => [ 'sales', 'organisation' ], join => [ 'sales', 'organisation' ],
@ -133,13 +133,15 @@ sub post_year_spend {
my $user = $c->stash->{api_user}; my $user = $c->stash->{api_user};
# Temporary date lock for dev data my $v = $c->validation;
my $last = DateTime->new( $v->input($c->stash->{api_json});
year => 2019, $v->required('from');
month => 4, $v->required('to');
day => 1
); return $c->api_validation_error if $v->has_error;
my $first = $last->clone->subtract(years => 1);
my $last = $c->parse_iso_datetime($v->param('to'));
my $first = $c->parse_iso_datetime($v->param('from'));
my $dtf = $c->schema->storage->datetime_parser; my $dtf = $c->schema->storage->datetime_parser;
my $driver = $c->schema->storage->dbh->{Driver}->{Name}; my $driver = $c->schema->storage->dbh->{Driver}->{Name};
@ -185,13 +187,15 @@ sub post_supplier_count {
my $user = $c->stash->{api_user}; my $user = $c->stash->{api_user};
# Temporary date lock for dev data my $v = $c->validation;
my $last = DateTime->new( $v->input($c->stash->{api_json});
year => 2019, $v->required('from');
month => 4, $v->required('to');
day => 1
); return $c->api_validation_error if $v->has_error;
my $first = $last->clone->subtract(years => 1);
my $last = $c->parse_iso_datetime($v->param('to'));
my $first = $c->parse_iso_datetime($v->param('from'));
my $dtf = $c->schema->storage->datetime_parser; my $dtf = $c->schema->storage->datetime_parser;
my $driver = $c->schema->storage->dbh->{Driver}->{Name}; my $driver = $c->schema->storage->dbh->{Driver}->{Name};
@ -213,8 +217,8 @@ sub post_supplier_count {
'organisation.name', 'organisation.name',
'me.quantised_days', 'me.quantised_days',
], ],
as => [ qw/ count total_spend name quantised_days / ], as => [ qw/count total_spend name quantised_days/ ],
group_by => [ qw/ me.quantised_days seller.id organisation.id / ], group_by => [ qw/me.quantised_days seller.id organisation.id/ ],
order_by => { '-asc' => 'me.quantised_days' }, order_by => { '-asc' => 'me.quantised_days' },
} }
); );
@ -374,12 +378,31 @@ sub post_supplier_history {
sub post_lcc_table_summary { sub post_lcc_table_summary {
my $c = shift; my $c = shift;
my $validation = $c->validation; my $user = $c->stash->{api_user};
$validation->input($c->stash->{api_json});
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'));
my $transaction_rs = $c->schema->resultset('Transaction'); my $transaction_rs = $c->schema->resultset('Transaction');
my $ward_transactions_rs = $transaction_rs->search({}, my $dtf = $c->schema->storage->datetime_parser;
my $ward_transactions_rs = $transaction_rs->search(
{
purchase_time => {
-between => [
$dtf->format_datetime($first),
$dtf->format_datetime($last),
],
buyer_id => $user->entity->id,
},
},
{ {
join => { seller => { postcode => { gb_postcode => 'ward' } } }, join => { seller => { postcode => { gb_postcode => 'ward' } } },
group_by => 'ward.id', group_by => 'ward.id',
@ -388,7 +411,7 @@ sub post_lcc_table_summary {
{ sum => 'me.value', '-as' => 'sum' }, { sum => 'me.value', '-as' => 'sum' },
'ward.ward' 'ward.ward'
], ],
as => [ qw/ count sum ward_name /], as => [ qw/count sum ward_name/ ],
} }
); );
@ -404,7 +427,7 @@ sub post_lcc_table_summary {
central_loc_gov => "Central Gov HMRC", central_loc_gov => "Central Gov HMRC",
); );
for my $meta ( qw/ for my $meta (qw/
local_service local_service
regional_service regional_service
national_service national_service
@ -412,10 +435,17 @@ sub post_lcc_table_summary {
business_tax_and_rebate business_tax_and_rebate
stat_loc_gov stat_loc_gov
central_loc_gov central_loc_gov
/ ) { /) {
my $transaction_type_rs = $transaction_rs->search( my $transaction_type_rs = $transaction_rs->search(
{ {
'meta.'.$meta => 1, 'me.purchase_time' => {
-between => [
$dtf->format_datetime($first),
$dtf->format_datetime($last),
],
},
'me.buyer_id' => $user->entity->id,
'meta.' . $meta => 1,
}, },
{ {
join => 'meta', join => 'meta',
@ -424,13 +454,12 @@ sub post_lcc_table_summary {
{ count => 'me.id', '-as' => 'count' }, { count => 'me.id', '-as' => 'count' },
{ sum => 'me.value', '-as' => 'sum' }, { sum => 'me.value', '-as' => 'sum' },
], ],
as => [ qw/ count sum /], as => [ qw/count sum/ ],
} }
)->first; )->first;
$transaction_type_data->{$meta} = { $transaction_type_data->{$meta} = {
( $transaction_type_rs ? ( ($transaction_type_rs ? (
count => $transaction_type_rs->get_column('count'), count => $transaction_type_rs->get_column('count'),
sum => $transaction_type_rs->get_column('sum'), sum => $transaction_type_rs->get_column('sum'),
type => $meta_names{$meta}, type => $meta_names{$meta},
@ -438,7 +467,7 @@ sub post_lcc_table_summary {
count => 0, count => 0,
sum => 0, sum => 0,
type => $meta_names{$meta}, type => $meta_names{$meta},
) ), )),
} }
} }
@ -450,7 +479,7 @@ sub post_lcc_table_summary {
}} $ward_transactions_rs->all }} $ward_transactions_rs->all
); );
return $c->render( json => { return $c->render(json => {
success => Mojo::JSON->true, success => Mojo::JSON->true,
wards => \@ward_transaction_list, wards => \@ward_transaction_list,
types => $transaction_type_data, types => $transaction_type_data,