<?php

include "../../root.class.php";
$db = new db_safeguard();

$date = date("Y-m-d H:i");
$user_id = $_SESSION['user_id'];
$client_id = $_POST['client_id'];
$tank_id = $_POST['barcode'];   

// get company_details
$company_res = $db->query("companies","SELECT * FROM companies WHERE record_id = {$_SESSION['company_id']}");
$company = $company_res->fetch_assoc();


// get the tank record_id
$tank_record_id = $db->query("tanks","SELECT * FROM `tanks` WHERE `unit_code` = '$tank_id' LIMIT 1");
$tank_id = $tank_record_id->fetch_assoc()['record_id'];

// get last order_id
$results = $db->query("fuel_movement","SELECT * FROM `fuel_movement` WHERE `company_id` = {$_SESSION['company_id']} ORDER BY `record_id` DESC LIMIT 1");
$order_id = $results->fetch_assoc()['order_id'] + 1;

// insert the movement
$transaction_id = $db->query("fuel_movement","INSERT INTO `fuel_movement`(`tank_id`, `amount`, `status`, `date_time_opened`, `date_time_closed`, `user_id`, `flow_rate`, `pulses`, `client_id`,`order_id`,`company_id`,`fuel_price`) VALUES ($tank_id,0,'AUTHORISED','$date',0,'$user_id',0,0,$client_id,$order_id,{$_SESSION['company_id']},{$company['fuel_price']})");


echo  "diesel_transaction.php?transaction_number=$transaction_id&order_id=$order_id";