90 lines
3.7 KiB
PHP
90 lines
3.7 KiB
PHP
<?php
|
|
$job_name = pathinfo(__FILE__, PATHINFO_FILENAME);
|
|
echo "[".date("Y-m-d H:i:s")."] ".$job_name." job is starting.\n";
|
|
|
|
set_time_limit(0); // No limit!
|
|
|
|
require('../../backend.php');
|
|
require('../common/QuoteApi.php');
|
|
$httpAuthToken = $savvyext->cfgReadChar('system.oauth2_token');
|
|
$encryptionAlg = $savvyext->cfgReadChar('encryption.algorithm');
|
|
$encryptionKey = $savvyext->cfgReadChar('encryption.key');
|
|
$encryptionIV = $savvyext->cfgReadChar('encryption.iv');
|
|
|
|
$baseURL = $savvyext->cfgReadChar('system.api_url');
|
|
|
|
$db_host = $savvyext->cfgReadChar('database.host');
|
|
$db_name = $savvyext->cfgReadChar('database.name');
|
|
$db_user = $savvyext->cfgReadChar('database.user');
|
|
$db_pass = $savvyext->cfgReadChar('database.pass');
|
|
$db_port = $savvyext->cfgReadLong('database.port');
|
|
$connstr = "host=${db_host} port=${db_port} dbname=${db_name} user=${db_user} password=${db_pass}";
|
|
$conn = pg_connect($connstr);
|
|
|
|
$country = 'SG';
|
|
$member_id = 0;
|
|
|
|
// Grab, ComfortDelGro & Gojek
|
|
$transport_providers = [/*3,*/4,5];
|
|
|
|
// Step 1: Load trips
|
|
$q = "select b.*,c.address AS origin, d.address AS destination ";
|
|
$q.= " from (select root_id,count(*) as count from trip_price_comparison where root_type=1 group by root_id) as a, ";
|
|
$q.= " parsedemail_item b, address c, address d ";
|
|
$q.= " where a.count=1 and b.id=a.root_id and b.private='f' and c.id=b.location_start_id and d.id=b.location_end_id and c.country='${country}' ";
|
|
$q.= " and b.dup_id is null ";
|
|
$q.= " order by b.travel_date desc";
|
|
echo $q."\n";
|
|
//exit();
|
|
$r = pg_query($conn, $q);
|
|
|
|
// Step 3: Schedule quotes
|
|
$checkQuotes = [];
|
|
|
|
QuoteApi::$job_name=$job_name;
|
|
if ($r && pg_num_rows($r)) {
|
|
$n = pg_num_rows($r);
|
|
echo "[".date("Y-m-d H:i:s")."] Processing ".$n." trips.\n";
|
|
$i = 0;
|
|
while ($f = pg_fetch_assoc($r)) {
|
|
$i++;
|
|
echo "[".date("Y-m-d H:i:s")."] Processing ${i}/${n} (".sprintf("%0.02f",100.0*$i/$n).") trip id=".$f["id"].".\n";
|
|
if ($f["location_start_id"]==$f["location_end_id"]) continue;
|
|
$location_start_id=$f["location_start_id"];
|
|
$location_end_id=$f["location_end_id"];
|
|
foreach ($transport_providers as $transport_provider) {
|
|
$q = "SELECT * FROM quotes ";
|
|
$q.= " WHERE location_start_id=${location_start_id} AND location_end_id=${location_end_id} ";
|
|
$q.= " AND transport_provider_id=${transport_provider} AND cost>0 ";
|
|
$q.= " AND completed>(current_date-1) ORDER BY completed DESC LIMIT 1";
|
|
$quotes = pg_query($conn, $q);
|
|
if ($quotes && pg_num_rows($quotes) && $quote=pg_fetch_assoc($quotes)) {
|
|
echo "[".date("Y-m-d H:i:s")."] Quote ".$quote["cost"]." already exists ID #".$quote["id"]."\n";
|
|
continue;
|
|
}
|
|
list($res,$id) = QuoteApi::schedule_quote($f["origin"],$f["destination"],$country,$member_id,$transport_provider);
|
|
if ($res>0) {
|
|
if ($res==2) {
|
|
echo "[".date("Y-m-d H:i:s")."] Quote complete! ID #${id}\n";
|
|
} else if ($res==1) {
|
|
echo "[".date("Y-m-d H:i:s")."] Scheduled quote ID #${id}\n";
|
|
$checkQuotes[] = $id;
|
|
} else {
|
|
echo "[".date("Y-m-d H:i:s")."] Unexpected result for ID #${id} ($res)\n";
|
|
}
|
|
} else {
|
|
echo "[".date("Y-m-d H:i:s")."] schedule_quote failed: ${res}.\n";
|
|
echo "[".date("Y-m-d H:i:s")."] $origin,$destination,$country,$member_id,$transport_provider\n";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Step 4: Check quotes
|
|
foreach ($checkQuotes as $id) {
|
|
list($res,$cost) = QuoteApi::check_quote($id);
|
|
echo "[".date("Y-m-d H:i:s")."] Checking quote ID #${id} ($res) cost=${cost}\n";
|
|
}
|
|
|
|
echo "[".date("Y-m-d H:i:s")."] ".$job_name." job complete.\n";
|