<?

// include "../classes/db.class.php";

// $db = new db();

// $asset_res = $db->exec_query('assets', ['*']);

// while ($asset_info = $asset_res->fetch_assoc()) {
//     echo $asset_info['fleet_no'] . "<br>";
//     echo "CURRENT ODO: ";
//     $asset_odo_res = $db->exec_query('asset_odo', ['*'], '', '', '', '', "asset_record_id = {$asset_info['record_id']}", "ORDER BY record_id DESC");
//     $odo_info = $asset_odo_res->fetch_assoc();
//     $latest_odo = $odo_info['odo'];
//     echo $latest_odo . "<br>";
//     echo "LAST SERVICE: ";
//     $last_service = $db->exec_query('job_cards', ['*'], '', '', '', '', "(job_card_type = 'SERICE SETUP' OR job_card_type = 'SERVICE') AND asset_id = {$asset_info['record_id']} AND job_card_status = 'CLOSED'", "ORDER BY record_id DESC");
//     if ($last_service->num_rows < 1) {
//         echo "NO SERVICE RECORD <br>";
//     } else {
//         $laset_service_info = $last_service->fetch_assoc();
//         $last_service = $laset_service_info['odo'];
//         echo $last_service . '<br>';
//     }

//     echo "SERVICE INTERVAL: ";

//     $asset_type = $db->exec_query('asset_types', ['*'], '', '', '', '', "record_id = {$asset_info['asset_type_id']}");
//     $asset_type_info = $asset_type->fetch_assoc();
//     $service_type_res = $db->exec_query('service_types', ['*'], '', '', '', '', "record_id = {$asset_type_info['service_type_id']}");
//     $service_type_info = $service_type_res->fetch_assoc();
//     echo $service_type_info['service_interval'] . '<br>';

//     $due = (($last_service + $service_type_info['service_interval']) - $latest_odo);

//     echo "SERVICE DUE IN: " . $due . '<br>';
//     $date = date('Y-m-d H:i');
//     if (strlen($latest_odo) > 1) {
//         if ($due <= 0) {
//             $db->insert("INSERT INTO `job_cards`(`job_card_status`, `asset_id`, `parts`, `notes`, `user`, `date_time_created`, `date_time_opened`, `date_time_closed`, `job_card_type`, `odo`, `warning_questions`, `images`, `user_id_opened`, `user_id_closed`, `farm_id`) VALUES ('OPEN','{$asset_info['record_id']}','','','0','$date','','','SERVICE','','','','','','{$asset_info['farm_id']}')");
//         }
//     }
//     echo "<br><br>";
// } 
