Updated to allow for user updates to change location by postcode
This commit is contained in:
parent
202deb9178
commit
37e8f0b46a
6 changed files with 105 additions and 26 deletions
40
lib/Pear/LocalLoop/Plugin/Postcodes.pm
Normal file
40
lib/Pear/LocalLoop/Plugin/Postcodes.pm
Normal file
|
@ -0,0 +1,40 @@
|
|||
package Pear::LocalLoop::Plugin::Postcodes;
|
||||
use Mojo::Base 'Mojolicious::Plugin';
|
||||
|
||||
use DateTime::Format::Strptime;
|
||||
|
||||
sub register {
|
||||
my ( $plugin, $app, $conf ) = @_;
|
||||
|
||||
$app->helper( get_location_from_postcode => sub {
|
||||
my ( $c, $postcode, $usertype ) = @_;
|
||||
my $postcode_obj = Geo::UK::Postcode::Regex->parse( $postcode );
|
||||
|
||||
my $location;
|
||||
|
||||
unless ( defined $postcode_obj && $postcode_obj->{non_geographical} ) {
|
||||
my $pc_result = $c->schema->resultset('GbPostcode')->find({
|
||||
incode => $postcode_obj->{incode},
|
||||
outcode => $postcode_obj->{outcode},
|
||||
});
|
||||
if ( defined $pc_result ) {
|
||||
# Force truncation here as SQLite is stupid
|
||||
$location = {
|
||||
latitude => (
|
||||
$usertype eq 'customer'
|
||||
? int($pc_result->latitude * 100 ) / 100
|
||||
: $pc_result->latitude
|
||||
),
|
||||
longitude => (
|
||||
$usertype eq 'customer'
|
||||
? int($pc_result->longitude * 100 ) / 100
|
||||
: $pc_result->longitude
|
||||
),
|
||||
};
|
||||
}
|
||||
}
|
||||
return $location;
|
||||
});
|
||||
}
|
||||
|
||||
1;
|
Reference in a new issue