<?php 
	require "connectionDb.php";

$selectDelivery1 = $db->query("SELECT * FROM delivery JOIN delivery_tank ON delivery.tank_group_id=delivery_tank.delivery_id");

// print_r("<thead><tr>
//         <th>Action</th>
//         <th>Message</th>
//         <th>id</th>
//         <th>Tank</th>
//         <th>Status</th>
//         <th>Start</th>
//         <th>End</th>
//         </tr></thead> ");
$json_data=[];
function dateFormatChanger($datetime) {

$getDateTime = $datetime;
$date = explode(' ', $getDateTime);

$newDate = date("d-M-Y", strtotime($date[0]));

$timezone = explode('+', $date[1]);
$time = explode('.', $timezone[0]);
return($newDate." ".$time[0]);
}
while ($row = $selectDelivery1->fetchArray()) {

	print_r($row);
// 	print_r(
//   "<tr>"
//   ."<td>".$row['type']."</td>"
//   ."<td>".$row['type']."</td>"
//   ."<td>".$row['id']."</td>"
//   ."<td>".$row['tank_id']."</td>"
//   ."<td>".$row['status']."</td>"
//   ."<td>".$row['start']."</td>"
//   ."<td>".$row['end']."</td>"
//   ."</tr>"
// );

$details = [
"Action" => $row['type'],
"Message" => $row['type'],
"Id" => $row['id'],
"Tank" => $row['tank_id'],
"Status" => $row['status'],
"Start" => dateFormatChanger($row['start']),
"End" => dateFormatChanger($row['end']),
"Product" => $row['product'],
"SupplierId" => $row['supplier_id'],
"SupplierName" => $row['product'],
"DeliveryCarNumber" => $row['car_number'],
"OrderPrice" => $row['order_price'],
"OrderVolume" => $row['order_vol'],
"DeliverVolume" => $row['deliver_vol'],
"FuelPoints" => $row['fp_group'],
"TankGroup" => $row['tank_group_id'],
];
//here pushing the values in to an array  
array_push($json_data,$details);  
}
$json_data_all = ["data" => $json_data];

$myfile = fopen("api/v1/Deliveries/deliveryDetails.txt", "w+") or die("Unable to open file!");;
fwrite($myfile,json_encode($json_data_all, JSON_PRETTY_PRINT));
fclose($myfile);
 ?>