Added script for setting up lat/long in db and showing in admin panels
This commit is contained in:
parent
bfcacbee32
commit
0486d910ee
2 changed files with 199 additions and 75 deletions
83
lib/Pear/LocalLoop/Command/latlong_setup.pm
Normal file
83
lib/Pear/LocalLoop/Command/latlong_setup.pm
Normal file
|
@ -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;
|
|
@ -1,6 +1,25 @@
|
||||||
% layout 'admin';
|
% layout 'admin';
|
||||||
% title 'Organisations';
|
% title 'Organisations';
|
||||||
% content_for javascript => begin
|
% content_for javascript => begin
|
||||||
|
<script>
|
||||||
|
function initMap() {
|
||||||
|
var org_latlng = {
|
||||||
|
lat: <%= $valid_org->latitude %>,
|
||||||
|
lng: <%= $valid_org->longitude %>
|
||||||
|
};
|
||||||
|
var map = new google.maps.Map(document.getElementById('map'), {
|
||||||
|
zoom: 18,
|
||||||
|
center: org_latlng
|
||||||
|
});
|
||||||
|
var marker = new google.maps.Marker({
|
||||||
|
position: org_latlng,
|
||||||
|
map: map
|
||||||
|
});
|
||||||
|
}
|
||||||
|
</script>
|
||||||
|
<script async defer
|
||||||
|
src="https://maps.googleapis.com/maps/api/js?key=<%= $c->config->{api_keys}->{google_maps_js_api} %>&callback=initMap">
|
||||||
|
</script>
|
||||||
% end
|
% end
|
||||||
% if ( my $error = flash 'error' ) {
|
% if ( my $error = flash 'error' ) {
|
||||||
<div class="alert alert-danger" role="alert">
|
<div class="alert alert-danger" role="alert">
|
||||||
|
@ -11,59 +30,79 @@
|
||||||
<strong>Success!</strong> <%= $success %>
|
<strong>Success!</strong> <%= $success %>
|
||||||
</div>
|
</div>
|
||||||
% }
|
% }
|
||||||
<div class="card mb-3">
|
<div class="row">
|
||||||
|
<div class="col-sm-12 col-md-6">
|
||||||
|
<div class="card mb-3">
|
||||||
<h3 class="card-header">
|
<h3 class="card-header">
|
||||||
%= $valid_org->name
|
%= $valid_org->name
|
||||||
</h3>
|
</h3>
|
||||||
<div class="card-block">
|
<div class="card-block">
|
||||||
<form action="<%= url_for %>" method="post">
|
<form action="<%= url_for %>" method="post">
|
||||||
<div class="form-group row">
|
<div class="form-group row">
|
||||||
<label for="name" class="col-sm-2 col-form-label">Organisation Name</label>
|
<label for="name" class="col-md-4 col-form-label">Organisation Name</label>
|
||||||
<div class="col-sm-10">
|
<div class="col-md-8">
|
||||||
<input id="name" type="text" class="form-control" placeholder="Organisation Name" name="name" value="<%= $valid_org->name %>">
|
<input id="name" type="text" class="form-control" placeholder="Organisation Name" name="name" value="<%= $valid_org->name %>">
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="form-group row">
|
<div class="form-group row">
|
||||||
<label for="name" class="col-sm-2 col-form-label">Street Name</label>
|
<label for="name" class="col-md-4 col-form-label">Street Name</label>
|
||||||
<div class="col-sm-10">
|
<div class="col-md-8">
|
||||||
<input id="street_name" type="text" class="form-control" placeholder="Street Name" name="street_name" value="<%= $valid_org->street_name %>">
|
<input id="street_name" type="text" class="form-control" placeholder="Street Name" name="street_name" value="<%= $valid_org->street_name %>">
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="form-group row">
|
<div class="form-group row">
|
||||||
<label for="name" class="col-sm-2 col-form-label">Town/City</label>
|
<label for="name" class="col-md-4 col-form-label">Town/City</label>
|
||||||
<div class="col-sm-10">
|
<div class="col-md-8">
|
||||||
<input id="town" type="text" class="form-control" placeholder="Town" name="town" value="<%= $valid_org->town %>">
|
<input id="town" type="text" class="form-control" placeholder="Town" name="town" value="<%= $valid_org->town %>">
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="form-group row">
|
<div class="form-group row">
|
||||||
<label for="name" class="col-sm-2 col-form-label">Sector</label>
|
<label for="name" class="col-md-4 col-form-label">Sector</label>
|
||||||
<div class="col-sm-10">
|
<div class="col-md-8">
|
||||||
<select class="form-control" name="sector">
|
<select class="form-control" name="sector">
|
||||||
%= include 'partials/sector_options', selected_sector => $valid_org->sector || '';
|
%= include 'partials/sector_options', selected_sector => $valid_org->sector || '';
|
||||||
</select>
|
</select>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="form-group row">
|
<div class="form-group row">
|
||||||
<label for="name" class="col-sm-2 col-form-label">Postcode</label>
|
<label for="name" class="col-md-4 col-form-label">Postcode</label>
|
||||||
<div class="col-sm-10">
|
<div class="col-md-8">
|
||||||
<input id="postcode" type="text" class="form-control" placeholder="Postcode" name="postcode" value="<%= $valid_org->postcode %>">
|
<input id="postcode" type="text" class="form-control" placeholder="Postcode" name="postcode" value="<%= $valid_org->postcode %>">
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="form-group row">
|
<div class="form-group row">
|
||||||
<label for="name" class="col-sm-2 col-form-label">Validated</label>
|
<label for="name" class="col-md-4 col-form-label">Validated</label>
|
||||||
<div class="col-sm-10">
|
<div class="col-md-8">
|
||||||
<input id="pending" type="checkbox" name="pending" value="0"<%= $valid_org->pending ? '' : ' checked' %>>
|
<input id="pending" type="checkbox" name="pending" value="0"<%= $valid_org->pending ? '' : ' checked' %>>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="form-group row">
|
<div class="form-group row">
|
||||||
<div class="ml-auto col-sm-10">
|
<div class="ml-auto col-md-8">
|
||||||
<button class="btn btn-success" type="submit">Save Changes To Organisation</button>
|
<button class="btn btn-success btn-block" type="submit">Save Changes To Organisation</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</form>
|
</form>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="card mb-3">
|
</div>
|
||||||
|
<div class="col-sm-12 col-md-6">
|
||||||
|
<div class="card mb-3">
|
||||||
|
<div class="card-header" role="tab" id="mapHeader">
|
||||||
|
<h3 class="mb-0">
|
||||||
|
Location
|
||||||
|
</h3>
|
||||||
|
</div>
|
||||||
|
<div id="mapBody" role="tabpanel">
|
||||||
|
<div class="card-block">
|
||||||
|
<!-- Yes this is nasty. no i dont care. --!>
|
||||||
|
<style> #map { width: 100%; height: 400px; background-color: grey; } </style>
|
||||||
|
<div id="map"></div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
|
<div class="col-12">
|
||||||
|
<div class="card mb-3">
|
||||||
<h3 class="card-header">
|
<h3 class="card-header">
|
||||||
Transactions
|
Transactions
|
||||||
</h3>
|
</h3>
|
||||||
|
@ -89,4 +128,6 @@
|
||||||
</div>
|
</div>
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
Reference in a new issue