Added transaction distance calculation

This commit is contained in:
Tom Bloor 2017-10-03 18:08:30 +01:00
parent 37e8f0b46a
commit 62881a0eda
5 changed files with 75 additions and 3 deletions

View file

@ -173,6 +173,7 @@ sub post_upload {
my $purchase_time = $c->parse_iso_datetime($validation->param('purchase_time') || '');
$purchase_time ||= DateTime->now();
my $file = defined $upload ? $c->store_file_from_upload( $upload ) : undef;
my $distance = $c->get_distance_from_coords( $user->entity->type_object, $organisation );
my $new_transaction = $organisation->entity->create_related(
'sales',
@ -181,6 +182,7 @@ sub post_upload {
value => $transaction_value * 100000,
( defined $file ? ( proof_image => $file ) : () ),
purchase_time => $c->format_db_datetime($purchase_time),
distance => $distance,
}
);

View file

@ -1,7 +1,8 @@
package Pear::LocalLoop::Plugin::Postcodes;
use Mojo::Base 'Mojolicious::Plugin';
use DateTime::Format::Strptime;
use Geo::UK::Postcode::Regex;
use GIS::Distance;
sub register {
my ( $plugin, $app, $conf ) = @_;
@ -35,6 +36,23 @@ sub register {
}
return $location;
});
$app->helper( get_distance_from_coords => sub {
my ( $c, $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 int( $gis->distance( $buyer_lat, $buyer_long => $seller_lat, $seller_long )->meters );
}
return;
});
}
1;