added functions and endpoints to calculate an eta based on google maps

This commit is contained in:
Devin Smith 2014-11-24 11:23:58 -08:00
parent d028908ee6
commit ddcba3780d
2 changed files with 75 additions and 4 deletions

View File

@ -59,12 +59,25 @@ class Controller_api_order extends Crunchbutton_Controller_RestAccount {
header('HTTP/1.1 401 Unauthorized');
exit;
}
switch (c::getPagePiece(3)) {
case 'eta':
echo json_encode(['time' => $order->eta()]);
break;
$out = $order->exports();
$out['user'] = $order->user()->id_user ? $order->user()->exports() : null;
$out['restaurant'] = $order->restaurant()->id_restaurant ? $order->restaurant()->exports() : null;
case 'status':
echo json_encode($order->status()->last());
break;
default:
$out = $order->exports();
$out['user'] = $order->user()->id_user ? $order->user()->exports() : null;
$out['restaurant'] = $order->restaurant()->id_restaurant ? $order->restaurant()->exports() : null;
echo json_encode($out);
break;
}
echo json_encode($out);;
break;
}
break;

View File

@ -179,5 +179,63 @@ class Cockpit_Order extends Crunchbutton_Order {
return $out;
}
public function eta() {
if (!isset($this->_eta)) {
$status = $this->status()->last()['status'];
if (!$status) {
$this->_eta = $this->restaurant()->delivery_estimated_time;
} elseif ($status == 'delivered') {
$this->_eta = 0;
} else {
$this->_eta = $this->calculateGoogleEta();
if ($status == 'accepted' || $status == 'transfered') {
if ($this->restaurant()->formal_relationship == 1 || $this->restaurant()->order_notifications_sent) {
$this->_eta += 5;
} else {
$this->_eta += 15;
}
}
}
}
return $this->_eta;
}
public function calculateGoogleEta() {
$status = $this->status()->last()['status'];
$driver = $this->driver()->location()->lat.','.$this->driver()->location()->lon;
$customer = urlencode($this->address);
$restaurant = $this->restaurant()->loc_lat.','.$this->restaurant()->loc_long;
$url = 'https://maps.googleapis.com/maps/api/directions/json?';
if ($status == 'pickedup') {
$url .= 'origin='.$driver.'&destination='.$customer;
} elseif ($status == 'accepted' || $status == 'transfered') {
$url .= 'origin='.$driver.'&destination='.$customer.'&waypoints='.$restaurant;
}
$url = 'https://maps.googleapis.com/maps/api/directions/json?origin=33.9848,-118.446&destination=1120%20princeton,%20marina%20del%20rey%20ca%2090292&waypoints=33.1751,-96.6778';
$res = @json_decode(@file_get_contents($url));
$eta = 0;
if ($res) {
foreach ($res->routes[0]->legs as $leg) {
$eta += $leg->duration->value/60;
}
}
return $eta;
//&key=API_KEY
}
}