From 0486d910ee978435268a09f99e37affd9ee57a2f Mon Sep 17 00:00:00 2001 From: Tom Bloor Date: Fri, 22 Sep 2017 12:35:12 +0100 Subject: [PATCH] Added script for setting up lat/long in db and showing in admin panels --- lib/Pear/LocalLoop/Command/latlong_setup.pm | 83 ++++++++ .../admin/organisations/valid_read.html.ep | 191 +++++++++++------- 2 files changed, 199 insertions(+), 75 deletions(-) create mode 100644 lib/Pear/LocalLoop/Command/latlong_setup.pm diff --git a/lib/Pear/LocalLoop/Command/latlong_setup.pm b/lib/Pear/LocalLoop/Command/latlong_setup.pm new file mode 100644 index 0000000..1bf796a --- /dev/null +++ b/lib/Pear/LocalLoop/Command/latlong_setup.pm @@ -0,0 +1,83 @@ +package Pear::LocalLoop::Command::latlong_setup; +use Mojo::Base 'Mojolicious::Command'; + +use Mojo::Util 'getopt'; + +use Geo::UK::Postcode::Regex; +use GIS::Distance; + +has description => 'Set lat/long data on customers and orgs'; + +has usage => sub { shift->extract_usage }; + +sub run { + my ( $self, @args ) = @_; + + my $customer_rs = $self->app->schema->resultset('Customer'); + my $org_rs = $self->app->schema->resultset('Organisation'); + + for my $result ( $customer_rs->all, $org_rs->all ) { + $self->_set_lat_long_for_result( $result ); + } + + my $transaction_rs = $self->app->schema->resultset('Transaction'); + + for my $result ( $transaction_rs->all ) { + my $distance = $self->_calculate_distance( + $result->buyer->${\$result->buyer->type}, + $result->seller->${\$result->seller->type}, + ); + $result->update({ distance => $distance }) if defined $distance; + } +} + +sub _set_lat_long_for_result { + my ( $self, $result ) = @_; + + my $parsed_postcode = Geo::UK::Postcode::Regex->parse($result->postcode); + my $pc_rs = $self->app->schema->resultset('GbPostcode'); + + if ( $parsed_postcode->{valid} && !$parsed_postcode->{non_geographical} ) { + my $gb_pc = $pc_rs->find({ + outcode => $parsed_postcode->{outcode}, + incode => $parsed_postcode->{incode}, + }); + if ( $gb_pc ) { + $result->update({ + latitude => $gb_pc->latitude, + longitude => $gb_pc->longitude, + }); + } + } +} + +sub _calculate_distance { + my ( $self, $buyer, $seller ) = @_; + + my $gis = GIS::Distance->new(); + + my $buyer_lat = $buyer->latitude; + my $buyer_long = $buyer->longitude; + my $seller_lat = $seller->latitude; + my $seller_long = $seller->longitude; + + if ( $buyer_lat && $buyer_long + && $seller_lat && $seller_long ) { + return $gis->distance( $buyer_lat, $buyer_long => $seller_lat, $seller_long )->meters; + } else { + print STDERR "missing lat-long for: " . $buyer->name . " or " . $seller->name . "\n"; + } + return; +} + +=head1 SYNOPSIS + + Usage: APPLICATION latlong_setup [OPTIONS] + + Options: + + none for now + +=cut + +1; diff --git a/templates/admin/organisations/valid_read.html.ep b/templates/admin/organisations/valid_read.html.ep index e390e5d..8a0f0e2 100644 --- a/templates/admin/organisations/valid_read.html.ep +++ b/templates/admin/organisations/valid_read.html.ep @@ -1,6 +1,25 @@ % layout 'admin'; % title 'Organisations'; % content_for javascript => begin + + % end % if ( my $error = flash 'error' ) { % } -
-

- %= $valid_org->name -

-
-
-
- -
- +
+
+
+

+ %= $valid_org->name +

+
+ +
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ +
+
+
+ +
+ pending ? '' : ' checked' %>> +
+
+
+
+ +
+
+ +
+
+
+
+
+ +
+
+ + +
-
- -
- -
-
-
- -
- -
-
-
- -
- -
-
-
- -
- -
-
-
- -
- pending ? '' : ' checked' %>> -
-
-
-
- -
-
- +
+
+
+
+

+ Transactions +

+ +
-
-

- Transactions -

- -