From 3b8b5b97f4c6dcc50ce1e667cdaf0e3f6eb0939c Mon Sep 17 00:00:00 2001 From: Finn Date: Fri, 6 Sep 2019 17:31:09 +0100 Subject: [PATCH] Added extra data showing --- lib/Pear/LocalLoop.pm | 1 + .../LocalLoop/Controller/Admin/ImportFrom.pm | 14 ++-- lib/Pear/LocalLoop/Controller/Api/External.pm | 71 +++++++++++++++++++ lib/Pear/LocalLoop/Import/LCCCsv/Postcodes.pm | 3 +- .../LocalLoop/Import/LCCCsv/Transactions.pm | 50 +++++++------ 5 files changed, 104 insertions(+), 35 deletions(-) diff --git a/lib/Pear/LocalLoop.pm b/lib/Pear/LocalLoop.pm index 9cdb14a..6697cd7 100644 --- a/lib/Pear/LocalLoop.pm +++ b/lib/Pear/LocalLoop.pm @@ -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'); diff --git a/lib/Pear/LocalLoop/Controller/Admin/ImportFrom.pm b/lib/Pear/LocalLoop/Controller/Admin/ImportFrom.pm index f860aef..a01924e 100644 --- a/lib/Pear/LocalLoop/Controller/Admin/ImportFrom.pm +++ b/lib/Pear/LocalLoop/Controller/Admin/ImportFrom.pm @@ -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'); } diff --git a/lib/Pear/LocalLoop/Controller/Api/External.pm b/lib/Pear/LocalLoop/Controller/Api/External.pm index 6482af8..43a3087 100644 --- a/lib/Pear/LocalLoop/Controller/Api/External.pm +++ b/lib/Pear/LocalLoop/Controller/Api/External.pm @@ -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; diff --git a/lib/Pear/LocalLoop/Import/LCCCsv/Postcodes.pm b/lib/Pear/LocalLoop/Import/LCCCsv/Postcodes.pm index ad2de50..c1efb5f 100644 --- a/lib/Pear/LocalLoop/Import/LCCCsv/Postcodes.pm +++ b/lib/Pear/LocalLoop/Import/LCCCsv/Postcodes.pm @@ -22,7 +22,6 @@ sub import_csv { } } -use Devel::Dwarn; sub _row_to_result { my ( $self, $row ) = @_; @@ -37,7 +36,7 @@ sub _row_to_result { return unless $postcode_r; return if $postcode_r->ward; - + $postcode_r->update({ ward_id => $ward->id }); } diff --git a/lib/Pear/LocalLoop/Import/LCCCsv/Transactions.pm b/lib/Pear/LocalLoop/Import/LCCCsv/Transactions.pm index 590c248..026806e 100644 --- a/lib/Pear/LocalLoop/Import/LCCCsv/Transactions.pm +++ b/lib/Pear/LocalLoop/Import/LCCCsv/Transactions.pm @@ -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"}) : ()), }); }