PHP: dynamic delivery cost according to postcode - php

The task is just like the title says: implement in PHP a feature that dynamically sets the delivery cost according to given postcode for United Kingdom. It would be a part of an e-commerce application (precisely: prestashop). It should work more or less like on this page.
My question is: is there any tool I can use? The only thing I've found is a one-table "database" (basically to be downloaded in CSV). But there are 2 problems:
I'm missing relation between a small-scale region (like Aberdeen) and a big-scale region/county/country (like Scotland). I don't consider doing it manually - possibly there's something that could help.
However, as wikipedia says, Royal Mail has given some special postcodes that are not related to regions but to organisations. Therefore I can't be 100% sure that above CSV will cover all possible postcodes that a customer could submit.
Do you know any tool or webservice or whatever else that deals with such functionalities?

Why not use a postcode database from an official source, such as Ordnance Survey's CodePoint Open - ordnancesurvey.co.uk/oswebsite/products/code-point-open - which is also free, and updated four times a year.
CodePoint Open doesn't include the 'specials', because they're not geographically tide (so the valid "SAN TA1" postcode isn't included), but do you really expect to be delivering to the headquarters of the FA or any of those "own postcode" organisations? If you do need those, then you can always add them manually.

Related

From PHP to XML and then X12 EDI conversion

I have written PHP code to calculate shipping rates for Estes Carrier, now I need to make EDI file to sent to carrier company. I dont have EDI code knowledge. i studied about 204 motor carrier load tender , but didn't help me more. do i need to convert first my PHP code in to XML and then into x12 EDI format? how?
To output data as an EDI, you need to format your data such that it
has this EDI structure:
ISA - Interchange header - Contains general sender and receiver information
GS - Functional group header - Document type information
ST - Transaction set header - Indicates start of transaction set and transaction identifier
...
... Depending on your transaction set , there will be different segements between ST and SE.
...
SE - Transaction set trailer - indicates end of transaction
GE - Functional group trailer
IEA - Interchange trailer
Segments contain multiple elements and elements contain pieces of data.
Here is a EDI 214 example in which segments are separated by ~ and elements are separated by *:
ISA*00* *00* *08*9251750000 *08*1234567890 *030627*1304*U*00401*000001403*0*P*>~
GS*QM*8019721193*1234567890*20030627*1304*1403*X*004010~
ST*214*8101~
B10*5896152*5896152*XX~
N1*SH*BAW PLASTICS INCORPORATED~
N3*CENTURY IBUSINES CTR*2148 CENTURY DRIVE~
N4*JEFERSON HILS*PA*15025~
G62*86*2050812~
N1*CN*FINSH LINE~
N3*308 NMITOEFER OAD*~
N4*INDIANAPOLIS*IN*46235~
G62*17*2050816~
LX*1~
AT7*D1*NS*2050815*132*LT~
MS1*INDIANAPOLIS*IN*USA~
MS2*CWCE*04937~
Q7*DCAS*1~
AT8*GL*41923~
SE*18*48101~
GE*1*1403~
IEA*1*000001403~
You will need to follow the x12 standard to construct or parse EDI's.
Here is a list of standard EDI document types: http://www.edibasics.com/edi-resources/document-standards/ansi/
I just google around to find EDI specifications to understand how to lay out my data, for example, here is one for EDI 214:
http://ffportal.freightforce.com/technology/edi/EDI214.pdf
most edi setups are like this:
- you produce a export file from your application. Eg xml, csv, fixed records, etc. (in edi this is called 'in-house file)
- an edi translator translates this to x12
- communication.
Receiving x12 is reversed: communication->translation->import
Focus upon good imports/exports in your application, not upon x12; that is what translators do.
There are good open source translators eg http://bots.sourceforge.net but there are more (google!). Some can only use xml as in-house format; if you prefer that you will have to write the export as xml.
You do not need to convert it into XML first. That's a wasted step (IMHO). You should be able to write code to go from DB -> EDI.
EDI is simply a text document that adheres to a standard. The standards are published (not for free), but the person requiring the 204 should be able to provide you with an implementation guide (IG). This IG will tell you what you need to send, in the order you need to send it. If an IG is not available, you can Google for a sample and start building off that. When testing the document, the partner will tell you what you need to change.
There will be terminology like Segment, Element, delimiter, terminator. When you create your text file, you will need to create segments (N1, REF) as an example. You will also need to put the proper delimiters in the file, and use the correct segment terminator (usually agreed upon between partners, but common ones are: *,^,~. The IG probably will not have the envelopes (ISA and GS segments). You will need to put those in the file and increment the control counters correctly.
My standard usual answer stil applies: I'm sure there is no $$ in the budget for a translator. That would be the best approach rather than reinventing the wheel with PHP code. Translators have built in syntax checking, drag and drop mapping, data modeling. Takes some of the learning curve out.

Calculating distance between two post codes

I would like some help with how to go about calculating the distance between two post codes. Here is the setup that I wish to have, let me give you a scenario.
A user logs into my website (they have provided their details, their postcode being one of them). That data is stored in the users table.
I have another table with details about locations. I would like to display locations (and the distance) that are closest to the user.
I know that there are certain methods that can be used, for example:
Converting a postcode to a long-lat and using built in methods to calculate the distance, I think Google offer a free service. But I have no idea how I would implement such a thing. All the tutorials I have taken a look at seem too complicated and for the time being I want a very crude and simple solution upon which I can build and more importantly learn!
I am fairly familiar with PHP and MySQL when it comes to basic input/output of data. But I am not sure of the best way to handle the information with regards to user data, and the locations of the 'businesses'.
Any suggestions, comments will be more than appreciated.
If you're US based, this API should meet your needs: https://www.zipcodeapi.com
It's made specifically to find distance between two zip codes, tells you the long-lat for zip codes, find all zips relative to a location, etc.
Get postcode northings eastings data free from ordinance survey (credit them on your webpage to get free)
2 calculate distance from the appropriate csv flat file (M.csv contains all manchester postcodes and north and eastness).
Distance is sqrt((n1-n2)**2 +(e1-e2)**2) - excuse my fortran
If its slow,and you want an approximate distance create a small database of first half postcodes averages so you are looking up M20 not M20 3nb.
5 NB google don't like you using their postcode apis outside google Maps.
I am wondering about an algorithm so that for short distances, if you are looking at places close to a postcode, you just search M postcodes for a M postcode (and maybe nearby post areas like SK, OL, if you have a table of adjacent postcode areas).
7 Any body taken this further?

PHP - Postcode check if it's covered

I am currently developing a website for an electrical company. They would like some sort of postcode check on there. It would somehow work like this:
User enters postcode
See if we cover it
display results.
But I have never worked with postcodes before. How would I be able to check whether they cover it. I obviously need some sort of database listing the postcode or area they cover. But how would I also check if the postcode is valid.
The postcode lookup is obviously to see if the electrical company covers the user's area.
Thanks in advance.
I think instead of using a database to search your results, you would be better of looking at geo location, and using a 3rd party to calculate everything for you.
Google and Sony both provide Geo Location platforms
Sony has: http://www.placeengine.com/en
Google has: http://code.google.com/apis/maps/documentation/distancematrix/
I obviously need some sort of database listing the postcode or area they cover.
Yes. Then do a simple look up.
But how would I also check if the postcode is valid.
You would have to have a database listing all possible post codes and do a similar check.
I don't know which country you are in, but in most countries there is no programmatical way of determining wether a postcode is covered by a service or not.
Also no way of knowing wether it is valid or not without consulting a database that contains coverage.
There are databases available with postal codes, here is a dutch one: http://www.postcode.nl/index/269/1/0/overzicht-producten-en-diensten.html
You can validate the postal code using a regular expression.
The electrical company could surely give you a list of postcodes they do cover? Then it is a simple string matching from there....
I assume you are interested in the UK. To see if a postcode is valid grab the free CodePoint open data set from OS: http://www.ordnancesurvey.co.uk/oswebsite/products/code-point-open/
You could pull that into a DG and cross check user input. Just remember that this data is based on the Royal Mail PAF, so do not assume it is 100% accurate. Build a bit of flexibility/fault tolerance into your code.
If the client wants a specific radius covered, you coudl also use the OS data for distance calculations... and it is all FREE, as in both freedom and free beer :-)
As a first step you could check if the postcode is in a valid format: http://en.wikipedia.org/wiki/Postcodes_in_the_United_Kingdom#Validation - this check will pre-filter input for you.

Post Code Lookup

A client wants a page on their website where the user can search for a stockist of their product range. What they want is the ability to enter a UK PostCode and for a list of their stockists to appear in closest order.
Is there a way to utilise Google Maps to determine the closest supplier?
I have a PHP MySQL database with ALL the suppliers Postcodes and details and I'll have the postcode of the user to use as well.
I think you'll need the Ordnance Survey CodePoint data to map your postcodes to a longitude and latitude. Codepoint is available as a commercial product, or there's also Codepoint Open
Once you have the latitude and longitude, then you can use standard algorithms such as Haversine or Vincenty to calculate distances between the points
EDIT
Structure of the CodePoint Open CSV files can be found here. Note that location is held as Northings and Esatings rather than Longitude and Latitude, so it will need converting. There's a number of articles about this on the web, e.g.
http://www.deepbluesky.com/blog/-/converting-os-coodinates-into-longitude-latitude_7/
http://mediakey.dk/~cc/convert-northing-and-easting-utm-to-longitude-and-latitude/
but you need to be aware that OS Northings/Eastings are based on the Airy 1830 ellipsoid rather than the WGS84 model used by Google maps (and most GSM systems). Failing to allow for this difference can put you out by anything between 70-120m between Cornwall and East Anglia.
You can also find PHP functions to do this conversion at the Movable Type site (essential reading for any PHP developer, working with GeoData. I'd recommend adding a couple off columns to your stockist database for longitude and latitude, and a one-off script to update all existing data using the Codepoint data, then modify your insert/update routines for stockists to keep this information up-to-date.
Using PHP, another solution for this conversion is PHPCoord by Jonathan Stott
For higher performance in your database query, do a lookup of lat/long against a "bounding box" before calculating distances. I've explained how to do this in response to a similar query.
I think you'll need a little more data than that. I'd suggest that you need to store the latitude and longitude of every UK postcode alongside the postcode itself. I think this data is available from the Royal Mail for a cost and I remember reading somewhere that it was going to be available in the public domain too. Have a user enter their postcode, look up their latitude and longitude in the db and then use that to perform another query that calculates the closest supplier to them, perhaps within a certain number of miles. You could perhaps create a stores procedure to do all of this on the db. This post seems to have some details on how to do this.
If you want it to be fast then pre-compute the distances between postcodes within a certain radius of each other - see the following:
Calculate distance between zip codes and users
This should work fine in your case unless you think a user would be willing to travel more than 100 miles to purchase a product ??
Instead of Google Maps, this open postcode geocoding API may be of use to you. As others have mentioned, once you have lat long for both points you can use standard algos to find distances. A previous question contains some info on how to do this directly in SQL.
You may find this website helpful: SQL/CSV Postcode Database
I've previously used this to lookup long/lat from a postcode "outcode".
There's also a php script (which I've not tested) which calculates distance between two postcodes.

Performing bulk language translation in a large database

My application has a 2.7 million records table with the list of all cities and villages in the world (provided by GeoNames.org). Each city has it's name in the native language. This database will be searched in an auto-complete form, but users should be able to type the city name in their own language (primarily Portuguese, since this will be a Brazilian website) and be able to locate the city (at least the most important ones).
For instance: Munich is a well-known German city. However, in the GeoNames database, it is registered as "München", the native German name. GeoNames provides an english representation of the name, but that basically strips the special characters of the city name (in this case, München becomes Munchen, not Munich).
Is there a way or service I can use to translate each of these cities names into Portuguese (or at the very least, English) and cache them in my database? I've looked into the Google Translation API, but in their TOS, automated processes are forbidden.
Thanks in advance.
A solution I found was parsing the WordPress translation files, it includes continents & cities in one of the language files.
Find the language you want here:
http://codex.wordpress.org/WordPress_in_Your_Language
Go to the language you want and download WordPress
Inside the zip goto: /wp-content/languages/
and open continents & cities file eg. "continents-cities-nl_NL.po"
In that file you'll have all the translated city names you can easily parse into your DB
I'm currently parsing the cities in PHP when I'm finished I can post the translated files here if you want.
How about parsing Wikipedia? I often use the "Languages" menu as a dictionary. It would take some time but since this is a one-time operation it doesn't matter that much.

Categories