Added script for setting up lat/long in db and showing in admin panels

This commit is contained in:
Tom Bloor 2017-09-22 12:35:12 +01:00
parent bfcacbee32
commit 0486d910ee
2 changed files with 199 additions and 75 deletions

View 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;

View file

@ -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,82 +30,104 @@
<strong>Success!</strong> <%= $success %> <strong>Success!</strong> <%= $success %>
</div> </div>
% } % }
<div class="card mb-3"> <div class="row">
<h3 class="card-header"> <div class="col-sm-12 col-md-6">
%= $valid_org->name <div class="card mb-3">
</h3> <h3 class="card-header">
<div class="card-block"> %= $valid_org->name
<form action="<%= url_for %>" method="post"> </h3>
<div class="form-group row"> <div class="card-block">
<label for="name" class="col-sm-2 col-form-label">Organisation Name</label> <form action="<%= url_for %>" method="post">
<div class="col-sm-10"> <div class="form-group row">
<input id="name" type="text" class="form-control" placeholder="Organisation Name" name="name" value="<%= $valid_org->name %>"> <label for="name" class="col-md-4 col-form-label">Organisation Name</label>
<div class="col-md-8">
<input id="name" type="text" class="form-control" placeholder="Organisation Name" name="name" value="<%= $valid_org->name %>">
</div>
</div>
<div class="form-group row">
<label for="name" class="col-md-4 col-form-label">Street Name</label>
<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 %>">
</div>
</div>
<div class="form-group row">
<label for="name" class="col-md-4 col-form-label">Town/City</label>
<div class="col-md-8">
<input id="town" type="text" class="form-control" placeholder="Town" name="town" value="<%= $valid_org->town %>">
</div>
</div>
<div class="form-group row">
<label for="name" class="col-md-4 col-form-label">Sector</label>
<div class="col-md-8">
<select class="form-control" name="sector">
%= include 'partials/sector_options', selected_sector => $valid_org->sector || '';
</select>
</div>
</div>
<div class="form-group row">
<label for="name" class="col-md-4 col-form-label">Postcode</label>
<div class="col-md-8">
<input id="postcode" type="text" class="form-control" placeholder="Postcode" name="postcode" value="<%= $valid_org->postcode %>">
</div>
</div>
<div class="form-group row">
<label for="name" class="col-md-4 col-form-label">Validated</label>
<div class="col-md-8">
<input id="pending" type="checkbox" name="pending" value="0"<%= $valid_org->pending ? '' : ' checked' %>>
</div>
</div>
<div class="form-group row">
<div class="ml-auto col-md-8">
<button class="btn btn-success btn-block" type="submit">Save Changes To Organisation</button>
</div>
</div>
</form>
</div>
</div>
</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="form-group row"> </div>
<label for="name" class="col-sm-2 col-form-label">Street Name</label> </div>
<div class="col-sm-10"> <div class="col-12">
<input id="street_name" type="text" class="form-control" placeholder="Street Name" name="street_name" value="<%= $valid_org->street_name %>"> <div class="card mb-3">
</div> <h3 class="card-header">
</div> Transactions
<div class="form-group row"> </h3>
<label for="name" class="col-sm-2 col-form-label">Town/City</label> <ul class="list-group list-group-flush">
<div class="col-sm-10"> % for my $transaction ( $transactions->all ) {
<input id="town" type="text" class="form-control" placeholder="Town" name="town" value="<%= $valid_org->town %>"> <li class="list-group-item">
</div> <div class="container">
</div> <a href="<%= url_for '/admin/transactions/' . $transaction->id %>" class="list-group-item list-group-item-action">
<div class="form-group row"> <div class="row text-center">
<label for="name" class="col-sm-2 col-form-label">Sector</label> <div class="col">From: <%= $transaction->buyer->name %></div>
<div class="col-sm-10"> <div class="col">To: <%= $transaction->seller->name %></div>
<select class="form-control" name="sector"> <div class="col">Value: <%= $transaction->value %></div>
%= include 'partials/sector_options', selected_sector => $valid_org->sector || ''; <div class="col">Submitted At: <%= $transaction->submitted_at %></div>
</select> <div class="col">Purchase Time: <%= $transaction->purchase_time %></div>
</div> </div>
</div> </a>
<div class="form-group row"> </div>
<label for="name" class="col-sm-2 col-form-label">Postcode</label> </li>
<div class="col-sm-10"> % }
<input id="postcode" type="text" class="form-control" placeholder="Postcode" name="postcode" value="<%= $valid_org->postcode %>"> <li class="list-group-item">
</div> <div class="container">
</div> %= bootstrap_pagination( $c->param('page') || 1, $transactions->pager->last_page, { class => 'justify-content-center' } );
<div class="form-group row"> </div>
<label for="name" class="col-sm-2 col-form-label">Validated</label> </li>
<div class="col-sm-10"> </ul>
<input id="pending" type="checkbox" name="pending" value="0"<%= $valid_org->pending ? '' : ' checked' %>> </div>
</div>
</div>
<div class="form-group row">
<div class="ml-auto col-sm-10">
<button class="btn btn-success" type="submit">Save Changes To Organisation</button>
</div>
</div>
</form>
</div> </div>
</div> </div>
<div class="card mb-3">
<h3 class="card-header">
Transactions
</h3>
<ul class="list-group list-group-flush">
% for my $transaction ( $transactions->all ) {
<li class="list-group-item">
<div class="container">
<a href="<%= url_for '/admin/transactions/' . $transaction->id %>" class="list-group-item list-group-item-action">
<div class="row text-center">
<div class="col">From: <%= $transaction->buyer->name %></div>
<div class="col">To: <%= $transaction->seller->name %></div>
<div class="col">Value: <%= $transaction->value %></div>
<div class="col">Submitted At: <%= $transaction->submitted_at %></div>
<div class="col">Purchase Time: <%= $transaction->purchase_time %></div>
</div>
</a>
</div>
</li>
% }
<li class="list-group-item">
<div class="container">
%= bootstrap_pagination( $c->param('page') || 1, $transactions->pager->last_page, { class => 'justify-content-center' } );
</div>
</li>
</ul>
</div>