1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-09-19 16:01:34 +02:00

Zip Code Table Lookup Request #1942

This commit is contained in:
Hillel Coren 2018-04-09 21:05:05 +03:00
parent c31221149b
commit bb127743da
6 changed files with 145 additions and 6 deletions

View File

@ -760,6 +760,14 @@ class Account extends Eloquent
return $this->currency_id ?: DEFAULT_CURRENCY;
}
/**
* @return mixed
*/
public function getCountryId()
{
return $this->country_id ?: DEFAULT_COUNTRY;
}
/**
* @param $date
*

View File

@ -26,4 +26,8 @@ return [
// privacy policy
'privacy_policy_url' => env('PRIVACY_POLICY_URL', ''),
// Google maps
'google_maps_enabled' => env('GOOGLE_MAPS_ENABLED', true),
'google_maps_api_key' => env('GOOGLE_MAPS_API_KEY', ''),
];

View File

@ -2823,6 +2823,8 @@ $LANG = array(
'paid_invoice' => 'Paid Invoice',
'unapproved_quote' => 'Unapproved Quote',
'unapproved_proposal' => 'Unapproved Proposal',
'autofills_city_state' => 'Auto-fills city/state',
'no_match_found' => 'No match found',
);

View File

@ -4,6 +4,12 @@
$('input#name').focus();
@stop
@section('head')
@if (config('ninja.google_maps_enabled'))
@include('partials.google_geocode')
@endif
@stop
@section('content')
@if ($errors->first('contacts'))
@ -87,7 +93,8 @@
{!! Former::text('address2') !!}
{!! Former::text('city') !!}
{!! Former::text('state') !!}
{!! Former::text('postal_code') !!}
{!! Former::text('postal_code')
->onchange(config('ninja.google_maps_enabled') ? 'lookupPostalCode()' : '') !!}
{!! Former::select('country_id')->addOption('','')
->fromQuery($countries, 'name', 'id') !!}
@ -104,7 +111,9 @@
{!! Former::text('shipping_address2')->label('address2') !!}
{!! Former::text('shipping_city')->label('city') !!}
{!! Former::text('shipping_state')->label('state') !!}
{!! Former::text('shipping_postal_code')->label('postal_code') !!}
{!! Former::text('shipping_postal_code')
->onchange(config('ninja.google_maps_enabled') ? 'lookupPostalCode(true)' : '')
->label('postal_code') !!}
{!! Former::select('shipping_country_id')->addOption('','')
->fromQuery($countries, 'name', 'id')->label('country_id') !!}
@ -239,7 +248,7 @@
@foreach (App\Models\Account::$customMessageTypes as $type)
{!! Former::textarea('custom_messages[' . $type . ']')
->label($type) !!}
@endforeach
@endforeach
</div>
<div role="tabpanel" class="tab-pane" id="classify">
{!! Former::select('size_id')->addOption('','')
@ -327,9 +336,11 @@
// button handles to copy the address
$('#copyBillingDiv button').click(function() {
copyAddress();
$('#copyBillingDiv').hide();
});
$('#copyShippingDiv button').click(function() {
copyAddress(true);
$('#copyShippingDiv').hide();
});
// show/hide buttons based on loaded values

View File

@ -0,0 +1,107 @@
<script src="https://maps.googleapis.com/maps/api/js?key={{ env('GOOGLE_MAPS_API_KEY') }}"></script>
<script type="text/javascript">
var countries = {!! \Cache::get('countries') !!};
var countryMap = {};
for (var i=0; i<countries.length; i++) {
var country = countries[i];
countryMap[country.id] = country;
}
$(function() {
showGeocodePlaceholder();
showGeocodePlaceholder(true);
$('#billing_address').change(function() {
showGeocodePlaceholder();
});
$('#shipping_address').change(function() {
showGeocodePlaceholder(true);
});
})
function showGeocodePlaceholder(isShipping) {
var postalCodeField = 'postal_code';
if (isShipping) {
postalCodeField = 'shipping_' + postalCodeField;
}
var placeholder = hasCityOrState(isShipping) ? '' : {!! json_encode(trans('texts.autofills_city_state')) !!};
$('#' + postalCodeField).attr('placeholder', placeholder);
}
function hasCityOrState(isShipping) {
var cityField = 'city';
var stateField = 'state';
if (isShipping) {
cityField = 'shipping_' + cityField;
stateField = 'shipping_' + stateField;
}
if ($('#' + cityField).val() || $('#' + stateField).val()) {
return true;
}
}
function lookupPostalCode(isShipping) {
var cityField = 'city';
var stateField = 'state';
var postalCodeField = 'postal_code';
var countryField = 'country_id';
if (isShipping) {
cityField = 'shipping_' + cityField;
stateField = 'shipping_' + stateField;
postalCodeField = 'shipping_' + postalCodeField;
countryField = 'shipping_' + countryField;
}
if (hasCityOrState(isShipping)) {
return;
}
var postalCode = $('#' + postalCodeField).val();
var countryId = $('#' + countryField).val() || {{ $account->getCountryId() }};
var countryCode = countryMap[countryId].iso_3166_2;
if (! postalCode) {
return;
}
var geocoder = new google.maps.Geocoder;
geocoder.geocode({
componentRestrictions: {
country: countryCode,
postalCode: postalCode,
}
}, function(results, status) {
if (status == 'OK') {
if (! results.length) {
return;
}
//console.log('Address: ' + results[0].formatted_address);
var components = results[0].address_components;
for (var i=0; i<components.length; i++) {
//console.log(component);
var component = components[i];
if (component.types.indexOf('locality') >= 0
|| component.types.indexOf('neighborhood') >= 0) {
if (! $('#' + cityField).val()) {
$('#' + cityField).val(component.long_name);
}
} else if (component.types.indexOf('administrative_area_level_1') >= 0
|| component.types.indexOf('postal_town') >= 0) {
if (! $('#' + stateField).val()) {
$('#' + stateField).val(component.short_name);
}
}
}
} else {
$('#' + cityField).attr("placeholder", {!! json_encode(trans('texts.no_match_found')) !!});
}
showGeocodePlaceholder(isShipping);
});
}
</script>

View File

@ -1,10 +1,15 @@
@extends('header')
@section('onReady')
$('input#name').focus();
@stop
@section('head')
@if (config('ninja.google_maps_enabled'))
@include('partials.google_geocode')
@endif
@stop
@section('content')
@if ($errors->first('vendor_contacts'))
@ -52,13 +57,15 @@
<div class="panel-heading">
<h3 class="panel-title">{!! trans('texts.address') !!}</h3>
</div>
<div class="panel-body">
<div class="panel-body" id="billing_address">
{!! Former::text('address1') !!}
{!! Former::text('address2') !!}
{!! Former::text('city') !!}
{!! Former::text('state') !!}
{!! Former::text('postal_code') !!}
{!! Former::text('postal_code')
->onchange(config('ninja.google_maps_enabled') ? 'lookupPostalCode()' : '') !!}
{!! Former::select('country_id')->addOption('','')
->fromQuery($countries, 'name', 'id') !!}