Files
dev-chiefworks f76abffdcd first commit
2022-05-31 16:21:53 -04:00

150 lines
6.7 KiB
PHP

<?php
function getDuration($duration,$startTime,$endTime) {
$ret = $duration;
if ($startTime!="" && $endTime!="") {
$end_t = strtotime($endTime);
$start_t = strtotime($startTime);
$ret = (int)round(($end_t-$start_t)/60,2);
if ($ret<0) $ret = -$ret;
return $ret > 240 ? ($ret-240) : $ret;
}
if ($ret>$duration) $ret = $duration;
return $ret;
}
function getEndDateTime($startTime,$endTime,$duration) {
if ($endTime!="") return $endTime;
if ($duration>0) return $duration;
}
function showDirections($f,$noRecurse=true, $htmlHeader="") {
if ($f["routes"]>0 && $f["gid"]>0) {
$q0 = "SELECT * FROM google_directions_legs WHERE parsedemail_item_advice_google_id=".$f["gid"];
$r0 = pg_query($q0);
if ($htmlHeader!="") {
echo "<table width='100%' style='color:#cc5555;font-weight:bold;'>${htmlHeader}</table>\n";
}
echo "<table width='100%' bgcolor='#ccc'><tr><th>Departure</th><th>Arrival</th><th>Distance</th><th>Duration</th><th>Steps</th><th>Cost</th></tr>\n";
while ($f0=pg_fetch_assoc($r0)) {
echo "<tr align=center bgcolor='#ffc'>";
echo "<td>".($f0["departure_time"]==0?'N/A':date("Y-m-d H:i:s",$f0["departure_time"]))."</td>" ;
echo "<td>".($f0["arrival_time"]==0?'N/A':date("Y-m-d H:i:s",$f0["arrival_time"]))."</td>";
echo "<td>".sprintf("%0.01f",$f0["distance"]/1000.0)."km</td>";
echo "<td>".gmdate("H:i:s", $f0["duration"])."</td>";
echo "<td>".$f0["steps"]."</td>";
echo "<td>$".sprintf("%0.02f",0.01*$f0["fare_raw"])."</td>";
echo "</tr>\n";
echo "<tr><td colspan=8>";
$q1 = "SELECT a.id AS sid,a.*,b.* FROM google_directions_leg_steps a LEFT JOIN google_directions_leg_step_details b ON a.id=b.google_directions_leg_step_id ";
$q1.= " WHERE a.google_directions_leg_id=".$f0["id"];
$r1 = pg_query($q1); $i=1;
echo "<table width='100%'><tr><th>N</th><th>Travel Mode</th><th>Distance</th><th>Duration</th><th>Instructions</th>";
echo "<th>Stops</th><th>Line</th><th>Vehicle</th><th>Departure Stop</th><th>Arrival Stop</th><th>Quote</th><th align=right>Departure / Arrival</th>";
echo "</tr>";
$fare = 0;
while ($f1=pg_fetch_assoc($r1)) {
echo "<tr bgcolor='#".($i%2==0?"fff'":"eee")."'><td>".$i."</td>";
echo "<td>".$f1["travel_mode"]."</td>";
echo "<td align=right>".($f1["distance"]<1000 ? $f1["distance"] : sprintf("%0.01fk",$f1["distance"]/1000.0))."m</td>";
echo "<td align=center>".gmdate("H:i:s", $f1["duration"])."</td>";
echo "<td>".$f1["html_instructions"]."</td>";
echo "<td>".$f1["num_stops"]."</td>";
echo "<td>".$f1["line"]."</td>";
echo "<td>".$f1["vehicle"]."</td>";
echo "<td>".$f1["departure_stop"]."</td>";
echo "<td>".$f1["arrival_stop"]."</td>";
$q2 = "SELECT * FROM leg_step_quote WHERE google_directions_leg_step_id=".$f1["sid"];
$r2 = pg_query($q2);
$f2 = pg_fetch_assoc($r2);
if (is_array($f2) && isset($f2["fare"]) && $f2["fare"]!="") {
echo "<td align=right><div id=\"quote_".$f["id"]."_".$f1["sid"]."\"><a href='#' title=\"Get quote\" onclick=\"return getQuote(".$f["id"].",".$f1["sid"].")\">".$f2["fare"]."</a></div></td>";
echo "<td align=right>".sprintf("%0.03f",$f1["location_start_lat"]).",".sprintf("%0.03f",$f1["location_start_lng"]);
echo "<br/>".sprintf("%0.03f",$f1["location_end_lat"]).",".sprintf("%0.03f",$f1["location_end_lng"])."</td>";
$fare += $f2["fare_raw"];
} else if (strtolower($f1["vehicle"])=='bus' || strtolower($f1["vehicle"])=='subway' || strtolower($f1["vehicle"])=='scooter') {
echo "<td align=right><div id=\"quote_".$f["id"]."_".$f1["sid"]."\"><a href='#' onclick=\"return getQuote(".$f["id"].",".$f1["sid"].")\">Get quote</a></div></td>";
echo "<td align=right>".sprintf("%0.03f",$f1["location_start_lat"]).",".sprintf("%0.03f",$f1["location_start_lng"]);
echo "<br/>".sprintf("%0.03f",$f1["location_end_lat"]).",".sprintf("%0.03f",$f1["location_end_lng"])."</td>";
} else {
echo "<td align=center>N/A</td><td align=center>N/A</th>";
}
echo "</tr>\n";
$i++;
}
if ($fare==0) $fare = $f0["fare_raw"];
echo "<tr align=right><td colspan=10><b>Total:</b></td><td>$".sprintf("%0.02f",$fare/100.0)."</td></tr>\n";
echo "</table>\n";
echo "</td></tr>\n";
}
echo "</table>";
} else {
echo " <a href='#' onclick=\"return loadRoutes(".$f["id"].");\">Get google directions routes</a>";
}
echo "<div id=\"taxi_".$f["id"]."\">";
showTaxi($f,$noRecurse);
echo "</div>";
}
function showMenu($id) {
echo "<a href=\"#\" onclick=\"return loadTaxi(${id});\">Get taxi advice</a>";
echo " | <a href=\"#\" onclick=\"return quoteTaxi(${id});\">Regular taxi quote</a>";
}
function showTaxi($f, $noRecurse=true, $force=false) {
$q0 = "SELECT * FROM parsedemail_item_advice WHERE parsedemail_item_id=".$f["id"];
$r0 = pg_query($q0);
//echo $q0;
if ($noRecurse) {
showMenu($f["id"]);
}
if ($r0 && pg_num_rows($r0) && !$force) {
ob_start();
echo "<table width=100%><tr align=right><th align=center>Time</th><th>Provider</th><th align=left>Product</th><th>High</th><th>Low</th><th>Distance</th><th align=center>Duration</th></tr>\n";
while ($f0=pg_fetch_assoc($r0)) {
echo "<tr align=right>";
echo "<td align=center>".strtok($f0["advice_time"],".")."</td>";
echo "<td>".getTransportProvider($f0["transport_provider_id"])."</td>";
echo "<td align=left>".$f0["display_name"]."<!-- ".$f0["transport_product"]." --></td>";
echo "<td>$".sprintf("%0.02f",$f0["high_estimate"]/100.0)."</td>";
echo "<td>$".sprintf("%0.02f",$f0["low_estimate"]/100.0)."</td>";
echo "<td>".$f0["distance_estimate"]."</td>";
echo "<td align=center>".gmdate("H:i:s", $f0["duration_estimate"])."</td>";
echo "</tr>";
}
echo "</table>\n";
if ($force) {
showTaxi($f,true,false);
} else {
ob_end_flush();
}
} if ($noRecurse) {
//echo "No recurse!";
} else {
// Get Advice
ob_start();
$result = local_v1_service_call('GET','advice',$f["id"]);
ob_end_clean();
//ob_end_flush();
if ($result["code"]==0) {
$data = $result["data"];
if (isset($data["error_uber"])) echo $data["error_uber"]."\n";
if (isset($data["error_lyft"])) echo $data["error_lyft"]."\n";
showTaxi($f, true);
} else {
echo "Invalid service response code!\n";
}
}
}
function getTransportProvider($id) {
global $providers;
if (!is_array($providers) || !isset($providers[$id])) {
$q = "SELECT id,name FROM transport_providers";
$r = pg_query($q);
while ($f=pg_fetch_row($r)) $providers[$f[0]] = $f[1];
}
return $providers[$id]."(".$id.")";
}