filter everything by date

This commit is contained in:
Thomas Bloor 2019-09-09 19:32:14 +01:00
parent f7b4fd251e
commit e3a3c1cc91

View file

@ -86,12 +86,12 @@ sub post_lcc_suppliers {
my $lcc_suppliers = $c->schema->resultset('Entity')->search(
{
'sales.buyer_id' => $user->entity->id,
( $v->param('search') ? (
($v->param('search') ? (
'-or' => [
{ 'organisation.name' => { 'like' => $v->param('search') . '%' } },
{ 'organisation.postcode' => { 'like' => $v->param('search') . '%' } },
]
) : () ),
) : ()),
},
{
join => [ 'sales', 'organisation' ],
@ -133,13 +133,15 @@ sub post_year_spend {
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 $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 $dtf = $c->schema->storage->datetime_parser;
my $driver = $c->schema->storage->dbh->{Driver}->{Name};
@ -185,13 +187,15 @@ sub post_supplier_count {
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 $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 $dtf = $c->schema->storage->datetime_parser;
my $driver = $c->schema->storage->dbh->{Driver}->{Name};
@ -213,8 +217,8 @@ sub post_supplier_count {
'organisation.name',
'me.quantised_days',
],
as => [ qw/ count total_spend name quantised_days / ],
group_by => [ qw/ me.quantised_days seller.id organisation.id / ],
as => [ qw/count total_spend name quantised_days/ ],
group_by => [ qw/me.quantised_days seller.id organisation.id/ ],
order_by => { '-asc' => 'me.quantised_days' },
}
);
@ -374,12 +378,31 @@ sub post_supplier_history {
sub post_lcc_table_summary {
my $c = shift;
my $validation = $c->validation;
$validation->input($c->stash->{api_json});
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'));
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' } } },
group_by => 'ward.id',
@ -388,7 +411,7 @@ sub post_lcc_table_summary {
{ sum => 'me.value', '-as' => 'sum' },
'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",
);
for my $meta ( qw/
for my $meta (qw/
local_service
regional_service
national_service
@ -412,10 +435,17 @@ sub post_lcc_table_summary {
business_tax_and_rebate
stat_loc_gov
central_loc_gov
/ ) {
/) {
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',
@ -424,13 +454,12 @@ sub post_lcc_table_summary {
{ count => 'me.id', '-as' => 'count' },
{ sum => 'me.value', '-as' => 'sum' },
],
as => [ qw/ count sum /],
as => [ qw/count sum/ ],
}
)->first;
$transaction_type_data->{$meta} = {
( $transaction_type_rs ? (
($transaction_type_rs ? (
count => $transaction_type_rs->get_column('count'),
sum => $transaction_type_rs->get_column('sum'),
type => $meta_names{$meta},
@ -438,7 +467,7 @@ sub post_lcc_table_summary {
count => 0,
sum => 0,
type => $meta_names{$meta},
) ),
)),
}
}
@ -450,7 +479,7 @@ sub post_lcc_table_summary {
}} $ward_transactions_rs->all
);
return $c->render( json => {
return $c->render(json => {
success => Mojo::JSON->true,
wards => \@ward_transaction_list,
types => $transaction_type_data,