/** fleetTruckLinkImporter::__construct()
  * Class constructor
  */
 public function __construct()
 {
     $realPath = realpath(dirname(__FILE__));
     $maxine = substr($realPath, 0, strrpos($realPath, DIRECTORY_SEPARATOR));
     $rootaccess = substr($maxine, 0, strrpos($maxine, DIRECTORY_SEPARATOR) + 1);
     defined("BASE") || define("BASE", $rootaccess);
     include_once BASE . "basefunctions/baseapis/TableManager.php";
     include_once BASE . "basefunctions/baseapis/FileParser/FileParser.php";
     $manager = new TableManager("fleet_truck_count");
     //: Do a test to see if table includes subbie_trucks field
     $fields = $manager->describeTable();
     if (in_array('subbie_count', array_keys($fields)) === FALSE) {
         $sql = (string) "ALTER TABLE `fleet_truck_count` ADD COLUMN `subbie_count` INT NOT NULL DEFAULT 0;";
         if ($manager->runSql($sql) === FALSE) {
             print "Altering table failed" . PHP_EOL;
             return FALSE;
         }
     }
     //: End
     $subbietrucksurl = "https://login.max.bwtsgroup.com/api_request/Report/export?report=153&responseFormat=csv";
     // Max
     $apiurl = "https://login.max.bwtsgroup.com/api_request/Report/export?report=145&responseFormat=csv";
     // Max
     $t24apiurl = "https://t24.max.bwtsgroup.com/api_request/Report/export?report=73&responseFormat=csv";
     // T24
     // $apiurl	= "http://max.mobilize.biz/m4/2/api_request/Report/export?report=141&responseFormat=csv"; // Test
     //: Get the list of subbie trucks
     $fileParser = new FileParser($subbietrucksurl);
     $fileParser->setCurlFile("subbie_trucks.csv");
     $data = $fileParser->parseFile();
     $subbieTrucks = (array) array();
     foreach ($data as $val) {
         if (array_key_exists("Trucks", $val) === FALSE) {
             continue;
         }
         $trucks = preg_split("/\\,/", $val["Trucks"]);
         if (is_array($trucks) === FALSE) {
             continue;
         }
         foreach ($trucks as $trucklist) {
             $subbieTrucks[] = $trucklist;
         }
     }
     //print_r($subbieTrucks);
     unset($data);
     //return FALSE;
     //: End
     $fleetDayHandler = new fleetDayHandler();
     $required_fleets = $fleetDayHandler->getIncomeFleets();
     $rows = (array) array();
     foreach ($required_fleets as $val) {
         if (array_key_exists('fleets', $val)) {
             $count = (int) 0;
             $subbie_count = (int) 0;
             foreach ($val['fleets'] as $subfleets) {
                 $manager->setWhere($manager->quoteString('`fleet_id`=?', $subfleets[0]));
                 $record = $manager->selectSingle();
                 $count += (int) $record['count'];
                 $subbie_count += (int) $record['subbie_count'];
             }
             $rows[] = (array) array('fleet_id' => $val['id'], 'count' => isset($count) && $count ? $count : 0, 'subbie_count' => isset($subbie_count) && $subbie_count ? $subbie_count : 0);
         } else {
             $url = $apiurl . "&Fleet=" . $val["maxid"] . "&Start%20Date=" . date("Y-m-d") . "&Stop%20Date=" . date('Y-m-d', strtotime('+1 day'));
             if (array_key_exists('t24', $val)) {
                 $url = $t24apiurl . "&Fleet=" . $val["maxid"] . "&Start%20Date=" . date("Y-m-d") . "&Stop%20Date=" . date('Y-m-d', strtotime('+1 day'));
             }
             print_r('url: ' . $url . PHP_EOL);
             $fileParser = new FileParser($url);
             $fileParser->setCurlFile("fleet_truck_count_" . $val["id"] . ".csv");
             $data = $fileParser->parseFile();
             // print_r($data);
             /*if ($fleetId == 75) { //: confirm a fleet is correct
             		print("<pre>");
             		print_r($data);
             		print("</pre>");
             		}*/
             $sub_cnt = (int) 0;
             foreach ($data as $row) {
                 if (in_array($row['Truck'], $subbieTrucks)) {
                     $sub_cnt++;
                 }
             }
             //print($sub_cnt.PHP_EOL);
             $record = (array) array('fleet_id' => $val['id'], 'count' => count($data), 'subbie_count' => $sub_cnt);
             if (array_key_exists('t24', $val)) {
                 $record['t24'] = (int) 1;
             }
             $rows[] = $record;
         }
     }
     //: Loop through and update/insert records
     foreach ($rows as $row) {
         $where = (string) $manager->quoteString('`fleet_id`=?', $row['fleet_id']);
         if (array_key_exists('t24', $row) && $row['t24'] === 1) {
             $where .= $manager->quoteString(' AND `t24`=?', $row['t24']);
         }
         $manager->setWhere($where);
         $record = $manager->selectSingle();
         if ($record) {
             print_r($record);
             print_r($row);
             $nDifference = (double) $row['count'] == 0 ? 100 : ($record['count'] - $row['count']) / $row['count'] * 100;
             print_r('diff:  ' . $nDifference . PHP_EOL);
             if ($nDifference > 50 || $nDifference < -50) {
                 continue;
             }
             $manager->setWhere($manager->quoteString('`id`=?', $record['id']));
             $manager->update($row);
         } else {
             $manager->insert($row);
         }
     }
 }
Exemple #2
0
function exportFleetScoreUpdates()
{
    require_once BASE . 'basefunctions/baseapis/PHPExcel/php-excel.class.php';
    $manager = new TableManager("fleet_scores");
    $data = __sanitizeData($_GET);
    $where = (string) "1=1";
    if (isset($data["fleet"]) && $data["fleet"]) {
        $where .= $manager->quoteString(" AND `fleetid`=?", (int) $data["fleet"]);
    }
    if (isset($data["start"]) && $data["start"]) {
        $where .= $manager->quoteString(" AND `date`>=?", (int) unixDate($data["start"]));
    }
    if (isset($data["end"]) && $data["end"]) {
        $where .= $manager->quoteString(" AND `date`<=?", (int) unixDate($data["end"]));
    }
    $manager->setWhere($where);
    $manager->setOrderBy(array("column" => "date"));
    $records = $manager->selectMultiple();
    $rows = returnFleetTruckCount();
    $xlsdata = (array) array();
    $fleetdayobj = new fleetDayHandler();
    $tempfleetlist = $fleetdayobj->getIncomeFleets();
    $fleetlist = array();
    foreach ($tempfleetlist as $tempkey => $tempval) {
        $fleetlist[$tempval["id"]] = $tempval;
    }
    unset($tempfleetlist);
    unset($fleetdayobj);
    $row = (array) array("Date", "Fleet", "Income budget", "Income", "Contribution budget", "Contribution", "Truck count", "Kms", "Ave. Kms per truck", "Budget Ave. Kms per truck");
    $xlsdata[] = $row;
    foreach ($records as $val) {
        $row = (array) array(date("Y-m-d", $val["date"]), $fleetlist[$val["fleetid"]]["name"], $val["budget"], $val["income"], $val["budgetcontrib"], $val["contrib"], $rows[$val["fleetid"]]["count"], $val["kms"], round($val["kms"] / (isset($rows[$val["fleetid"]]) && isset($rows[$val["fleetid"]]["count"]) ? $rows[$val["fleetid"]]["count"] : 1), 2), round($val["budkms"] / (isset($rows[$val["fleetid"]]) && isset($rows[$val["fleetid"]]["count"]) ? $rows[$val["fleetid"]]["count"] : 1), 2));
        $xlsdata[] = $row;
    }
    $xls = new Excel_XML('UTF-8', TRUE);
    $xls->addArray($xlsdata);
    $xls->generateXML('fleet_score_data');
}
}
// }
// Create date strings for query {
$startday = date("d");
$startmonth = date("m");
$startyear = date("Y");
$startstring = $startyear . "-" . $startmonth . "-" . $startday;
$stopdate = mktime(0, 0, 0, $startmonth, date("d") + 1, $startyear);
$stopday = date("d", $stopdate);
$stopmonth = date("m", $stopdate);
$stopyear = date("Y", $stopdate);
$stopstring = $stopyear . "-" . $stopmonth . "-" . $stopday;
$count = 0;
$rowcount = 0;
// }
$fleetlist = $fleetdayobj->getIncomeFleets();
// Pull the details for each fleet {
foreach ($fleetlist as $fleetkey => $fleetval) {
    $fleetdetails = $fleetdayobj->getFleetScoreDay($fleetval["id"]);
    $fleetid = $fleetval["id"];
    $fleetscore[$fleetid]["name"] = $fleetval["name"];
    foreach ($fleetdetails as $daykey => $dayval) {
        $fleetscore[$fleetid]["income"] += $dayval["income"];
        $fleetscore[$fleetid]["kms"] += $dayval["kms"];
        $fleetscore[$fleetid]["budget"] += $dayval["budget"];
    }
    if ($fleetscore[$fleetid]["budget"] > 0) {
        $fleetscore[$fleetid]["score"] = round($fleetscore[$fleetid]["income"] / $fleetscore[$fleetid]["budget"] * 100, 0);
    } else {
        $fleetscore[$fleetid]["score"] = 0;
    }
Exemple #4
0
/** ocdData.php
 * @package ocdData
 * @author Feighen Oosterbroek <*****@*****.**>
 * @copyright 2013 onwards Barloworld Transport Solutions
 * @license GNU GPL
 * @link http://www.gnu.org/licenses/gpl.html
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
function ocdData()
{
    //: Preparation
    $times = substr_count($_SERVER['PHP_SELF'], "/");
    $rootaccess = "";
    $i = 1;
    while ($i < $times) {
        $rootaccess .= "../";
        $i++;
    }
    defined('BASE') || define("BASE", $rootaccess);
    include_once BASE . "/basefunctions/localdefines.php";
    include_once BASE . "/basefunctions/dbcontrols.php";
    include_once BASE . "/basefunctions/baseapis/manapi.php";
    include_once BASE . "Maxine/api/maxineapi.php";
    require_once BASE . "basefunctions/baseapis/fleetDayHandler.php";
    $link = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_SCHEMA) or die(mysqli_error($link));
    $fleetdayobj = new fleetDayHandler();
    $fleetlist = (array) array();
    $tmp = $fleetdayobj->getIncomeFleets();
    foreach ($tmp as $key => $val) {
        $sorted[$val['maxid']] = $val["name"];
    }
    asort($sorted, SORT_STRING);
    foreach ($sorted as $key => $val) {
        $fleetlist[$key] = $sorted[$key];
    }
    unset($sorted);
    unset($tmp);
    $sql = (string) 'SELECT `r`.`fleet_id`, `missing_count`, `open_count`, `total_open_count`, `count`, `litres` FROM `refuels` AS `r` INNER JOIN `unauthorized_refuels` AS `u` ON `r`.`fleet_id`=`u`.`fleet_id`';
    if (isset($_POST) && isset($_POST['conf']) && $_POST['conf']) {
        if (isset($_POST['conf']['fleetid']) && $_POST['conf']['fleetid']) {
            $data = __sanitizeData(array(0 => $_POST['conf']['fleetid']));
            $sql .= ' WHERE `r`.`fleet_id`=' . $data[0];
        }
    }
    $fleetid = (int) (isset($_POST['conf']) && $_POST['conf'] && (isset($_POST['conf']['fleetid']) && $_POST['conf']['fleetid']) ? $_POST['conf']['fleetid'] : 0);
    $records = sqlQuery($sql);
    $data = (array) array();
    foreach ($records as $val) {
        $data[$val['fleet_id']] = $val;
    }
    unset($records);
    //: End
    print '<!DOCTYPE html>';
    print '<head>';
    print '<meta charset="utf-8">';
    print '<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">';
    print '<title>Dashboards - Barloworld Transport</title>';
    print '<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />';
    print '<link href="favicon.ico" rel="shortcut icon" />';
    print '<link rel="stylesheet" href="' . BASE . 'basefunctions/scripts/bootstrap.min.css">';
    print '<link href="' . BASE . 'basefunctions/scripts/font-awesome.min.css" rel="stylesheet">';
    print '<link rel="stylesheet" href="' . BASE . 'Maxine/displaycase/content/site/css/fonts.css">';
    print '<link rel="stylesheet" href="' . BASE . 'Maxine/displaycase/content/site/css/main.css">';
    print '<script src="' . BASE . 'Maxine/displaycase/content/site/js/vendor/modernizr-2.6.2.min.js"></script>';
    print '<script src="' . BASE . 'basefunctions/scripts/jquery.min.js"></script>';
    print '<script src="' . BASE . 'basefunctions/scripts/jquery.ui.touch-punch.min.js"></script>';
    print '<!--[if lt IE 9]>';
    print '<script src="' . BASE . 'basefunctions/scripts/html5shiv.min.js"></script>';
    print '<script src="' . BASE . 'basefunctions/scripts/respond.js"></script>';
    print '<![endif]-->';
    print '<script src="' . BASE . 'Maxine/displaycase/content/site/js/vendor/jcircle.js"></script>';
    print '<script src="' . BASE . 'Maxine/displaycase/content/site/js/vendor/jquery.flot/jquery.flot.js"></script>';
    print '<script src="' . BASE . 'Maxine/displaycase/content/site/js/vendor/jquery.flot/jquery.flot.time.js"></script>';
    print '<script src="' . BASE . 'Maxine/displaycase/content/site/js/vendor/jquery.flot/jquery.flot.tooltip.js"></script>';
    print '</head>' . PHP_EOL;
    print '<body>';
    print '<div id="root"></div>';
    print '<div id="page" style="overflow-y:auto;">';
    print '<header>';
    print '<div class="controlsWrapper">';
    print '<a href="#" class="menu"></a>';
    print '</div><!-- controlsWrapper -->';
    print '</header>';
    print '<nav>';
    print '<ul>';
    print '<li><a href="/?personal">Dashboard</a></li>';
    print '<li><a href="/?mydashdetails">Dashboard Builder</a></li>';
    print '<li><a href="/?importfleetday">Import Day</a></li>';
    print '<li><a href="/?checkfleetscoreupdates">Fleet Scores</a></li>';
    print '<li><a href="/?ocddata">OCD Data</a></li>';
    print '<li><a href="/?logout">Logout</a></li>';
    print '</ul>';
    print '</nav>';
    //: Page Content
    print '<div id="blackouts">';
    //: Form
    print '<div class="fleetWrapper" style="height:10%;">';
    print '<form method="POST">';
    print '<table><tbody><tr>';
    print '<td>';
    //: Col 1
    print '<label for="conf[fleetid]">Fleet:</label>';
    print '<select id="conf[fleetid]" name="conf[fleetid]" value="' . $fleetid . '">';
    print '<option value="0" ' . ($fleetid == 0 ? "selected" : "") . '>All</option>';
    foreach ($fleetlist as $fleetkey => $fleetval) {
        if (!$fleetval) {
            continue;
        }
        print '<option value="' . $fleetkey . '"' . ($fleetid == $fleetkey ? ' selected="selected"' : '') . '>' . $fleetval . '</option>';
    }
    print '</select>';
    print '</td>';
    print '<td>';
    //: Col 2
    print '<input type="Submit" value="Search" />';
    print '</td>';
    print '</tr></tbody></table>';
    print '</form>';
    print '</div>';
    print '<div class="fleetWrapper" style="height:90%;">';
    print '<table style="margin-bottom:20px;">';
    if ($data) {
        $cols = (array) array('fleet_id' => 'Fleet', 'missing_count' => 'Missing count', 'total_open_count' => 'Total open', 'open_count' => 'Open above fleet limit', 'count' => 'Unauthorized count', 'litres' => 'Litres');
        print '<thead><tr>';
        foreach ($cols as $key => $val) {
            print '<td>' . $val . '</td>';
        }
        print '</tr></thead>';
        print '<tbody>';
        foreach ($fleetlist as $key => $val) {
            //: Skip rows that do not have an active fleet
            if (array_key_exists($key, $data) === FALSE) {
                continue;
            }
            $row = $data[$key];
            print '<tr>';
            foreach ($cols as $key1 => $val1) {
                print '<td>';
                print $key1 == 'fleet_id' ? $val : $row[$key1];
                print '</td>';
            }
            print '</tr>';
        }
        print '</tbody>';
    } else {
        print '<tbody><tr><td>No results to show</td></tr></tbody>';
    }
    print '</table>';
    print '</div>';
    //: End
    print '</div>';
    //: End
    //: End Page
    print '<script>window.jQuery || document.write("<script src=\\"' . BASE . 'Maxine/displaycase/content/site/js/vendor/jquery-1.9.1.min.js\\"><\\/script>")</script>';
    print '<script src="' . BASE . 'basefunctions/scripts/jquery.color.min.js"></script>';
    print '<script src="' . BASE . 'Maxine/displaycase/content/site/js/plugins.js"></script>';
    print '<script src="' . BASE . 'Maxine/displaycase/content/site/js/styling.js"></script>';
    print '<script src="' . BASE . 'Maxine/displaycase/content/site/js/main.js"></script>';
    print '<script type="text/javascript" language="javascript" src="' . BASE . '/basefunctions/scripts/manline.js"></script>';
    print '</body>' . PHP_EOL;
    print '</html>';
}