Added extra data showing

This commit is contained in:
Finn 2019-09-06 17:31:09 +01:00
parent af9069b17a
commit 3b8b5b97f4
5 changed files with 104 additions and 35 deletions

View file

@ -196,6 +196,7 @@ sub startup {
$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_history')->to('api-external#post_supplier_history');
$api_v1_org->post('/external/lcc_tables')->to('api-external#post_lcc_table_summary');
$api_v1_org->post('/pies')->to('api-v1-organisation-pies#index');

View file

@ -9,7 +9,7 @@ sub index {
$c->stash->{org_entities} = [
map {
{ id => $_->entity_id, name => $_->name }
} $c->schema->resultset('Organisation')->all
} $c->schema->resultset('Organisation')->search({ name => { like => '%lancashire%' }}, { columns => [qw/ entity_id name / ]})
];
$c->app->max_request_size(104857600);
@ -37,9 +37,7 @@ sub post_suppliers {
my $job_id = $c->minion->enqueue('csv_supplier_import' => [ $filename ]);
my $job_url = $c->url_for("/admin/minion/jobs?id=$job_id")->to_abs;
$c->flash(success => "CSV import started, see status of minion job at: $job_url");
$c->flash(success => "CSV import started, see status of minion job at: " . $c->link_to( 'Minion Job', "/admin/minion/jobs?id=$job_id"));
return $c->redirect_to('/admin/import_from');
}
@ -65,9 +63,7 @@ sub post_postcodes {
my $job_id = $c->minion->enqueue('csv_postcode_import' => [ $filename ]);
my $job_url = $c->url_for("/admin/minion/jobs?id=$job_id")->to_abs;
$c->flash(success => "CSV import started, see status of minion job at: $job_url");
$c->flash(success => "CSV import started, see status of minion job at: " . $c->link_to( 'Minion Job', "/admin/minion/jobs?id=$job_id"));
return $c->redirect_to('/admin/import_from');
}
@ -98,9 +94,7 @@ sub post_transactions {
my $job_id = $c->minion->enqueue('csv_transaction_import' => [ $filename, $c->param('entity_id') ]);
my $job_url = $c->url_for("/admin/minion/jobs?id=$job_id")->to_abs;
$c->flash(success => "CSV import started, see status of minion job at: $job_url");
$c->flash(success => "CSV import started, see status of minion job at: " . $c->link_to( 'Minion Job', "/admin/minion/jobs?id=$job_id"));
return $c->redirect_to('/admin/import_from');
}

View file

@ -379,4 +379,75 @@ sub post_supplier_history {
});
}
sub post_lcc_table_summary {
my $c = shift;
my $validation = $c->validation;
$validation->input($c->stash->{api_json});
my $transaction_rs = $c->schema->resultset('Transaction');
my $ward_transactions_rs = $transaction_rs->search({},
{
join => { seller => { postcode => { gb_postcode => 'ward' } } },
group_by => 'ward.id',
select => [
{ count => 'me.id', '-as' => 'count' },
{ sum => 'me.value', '-as' => 'sum' },
'ward.ward'
],
as => [ qw/ count sum ward_name /],
}
);
my $transaction_type_data = {};
for my $meta ( qw/
local_service
regional_service
national_service
private_household_rebate
business_tax_and_rebate
stat_loc_gov
central_loc_gov
/ ) {
my $transaction_type_rs = $transaction_rs->search(
{
'meta.'.$meta => 1,
},
{
join => 'meta',
group_by => 'meta.' . $meta,
select => [
{ count => 'me.id', '-as' => 'count' },
{ sum => 'me.value', '-as' => 'sum' },
],
as => [ qw/ count sum /],
}
)->first;
$transaction_type_data->{$meta} = {
( $transaction_type_rs ? (
count => $transaction_type_rs->get_column('count'),
sum => $transaction_type_rs->get_column('sum'),
) : () ),
}
}
my @ward_transaction_list = (
map {{
ward => $_->get_column('ward_name') || "N/A",
sum => $_->get_column('sum') / 100000,
count => $_->get_column('count'),
}} $ward_transactions_rs->all
);
return $c->render( json => {
success => Mojo::JSON->true,
wards => \@ward_transaction_list,
types => $transaction_type_data,
});
}
1;

View file

@ -22,7 +22,6 @@ sub import_csv {
}
}
use Devel::Dwarn;
sub _row_to_result {
my ( $self, $row ) = @_;

View file

@ -55,6 +55,8 @@ sub _row_to_result {
unless ($organisation) {
# Pear::LocalLoop::Error->throw("Cannot find an organisation with supplier_id $supplier_id");
return unless $row->{'Company Name (WHO)'};
my $town = $row->{post_town};
unless ($town) {
@ -68,7 +70,7 @@ sub _row_to_result {
$organisation = $self->schema->resultset('Entity')->create({
type => 'organisation',
organisation => {
name => $row->{name},
name => $row->{'Company Name (WHO)'},
street_name => $row->{"address line 1"},
town => $town,
postcode => $row->{post_code},
@ -86,9 +88,9 @@ sub _row_to_result {
time_zone => 'Europe/London'
);
my $paid_date = ( $row->{paid_date} ?
$date_formatter->parse_datetime($row->{paid_date}) :
$date_formatter->parse_datetime($row->{invoice_date}) );
my $paid_date = ( $row->{paid_date} ?
$date_formatter->parse_datetime($row->{paid_date}) :
$date_formatter->parse_datetime($row->{invoice_date}) );
my $gross_value = $row->{gross_amount};
$gross_value =~ s/,//g;
@ -98,26 +100,28 @@ sub _row_to_result {
$net_value =~ s/,//g;
# TODO negative values are sometimes present
$self->external_result->find_or_create_related('transactions', {
my $external_transaction = $self->external_result->update_or_create_related('transactions', { # This is a TransactionExternal result
external_id => $row->{transaction_id},
transaction => {
seller => $organisation->entity,
buyer => $lcc_org,
purchase_time => $paid_date,
value => $gross_value * 100000,
meta => {
gross_value => $gross_value * 100000,
sales_tax_value => $sales_tax_value * 100000,
net_value => $net_value * 100000,
($row->{"local service"} ? (local_service => $row->{"local service"}) : ()),
($row->{"regional service"} ? (regional_service => $row->{"regional service"}) : ()),
($row->{"national service"} ? (national_service => $row->{"national service"}) : ()),
($row->{"private household rebate"} ? (private_household_rebate => $row->{"private household rebate"}) : ()),
($row->{"business tax and rebate"} ? (business_tax_and_rebate => $row->{"business tax and rebate"}) : ()),
($row->{"stat loc gov"} ? (stat_loc_gov => $row->{"stat loc gov"}) : ()),
($row->{"central loc gov"} ? (central_loc_gov => $row->{"central loc gov"}) : ()),
},
}
});
my $transaction_result = $external_transaction->update_or_create_related( 'transaction', {
seller => $organisation->entity,
buyer => $lcc_org,
purchase_time => $paid_date,
value => $gross_value * 100000,
});
my $meta_result = $transaction_result->update_or_create_related('meta', {
gross_value => $gross_value * 100000,
sales_tax_value => $sales_tax_value * 100000,
net_value => $net_value * 100000,
($row->{"local service"} ? (local_service => $row->{"local service"}) : ()),
($row->{"regional service"} ? (regional_service => $row->{"regional service"}) : ()),
($row->{"national service"} ? (national_service => $row->{"national service"}) : ()),
($row->{"private household rebate"} ? (private_household_rebate => $row->{"private household rebate"}) : ()),
($row->{"business tax and rebate"} ? (business_tax_and_rebate => $row->{"business tax and rebate"}) : ()),
($row->{"stat loc gov"} ? (stat_loc_gov => $row->{"stat loc gov"}) : ()),
($row->{"central loc gov"} ? (central_loc_gov => $row->{"central loc gov"}) : ()),
});
}