<?php
include("../dbconfig.php");
include "../dbfunction.php";
/*
echo '<pre>';
print_r($_FILES);
print_r($_POST);
echo '</pre>';
exit;
*/
//var_dump($_FILES);
//exit;
$first_name="";
$phone="";
$dob="";
$email="";
$address="";
$post_code="";
$pco_number="";
$badge_number ="";
$insurance_date ="";
$pco_expiry ="";
$tfl_expiry = "";
$badge_expiry ="";
$make ="";
$passengers ="";
$mot_date ="";
$color ="";
$reg="";
$balance=0;
$mobile="";
$n_i_number="";
$reg_plate="";

if (isset($_POST['first_name'])) {
   // include Database connection file 
   $condition=$_POST;
   // get values 
   
   $first_name = $_POST['first_name'];
   if(isset($_POST['phone']))
   {
   $phone = $_POST['phone'];
   }
   if(isset($_POST['dob']))
   {
   $dob= date("Y-m-d", strtotime(str_replace('/', '-', $_POST['dob'])));
   }
   if(isset($_POST['email']))
   {
   $email = $_POST['email'];
   }
   if(isset($_POST['address']))
   {
   $address = $_POST['address'];
   }
   if(isset($_POST['post_code']))
   {
   $post_code = $_POST['post_code'];
   }
   if(isset($_POST['pco_number']))
   {
   $pco_number = $_POST['pco_number'];
   }
   if(isset($_POST['badge_number']))
   {
   $badge_number = $_POST['badge_number'];
   }
   if(isset($_POST['insurance_date']))
   {
   $insurance_date = $_POST['insurance_date'];
   $insurance_date = date("Y-m-d", strtotime(str_replace('/', '-', $insurance_date)));
   }
   
   if(isset($_POST['pco_expiry']))
   { 
    $pco_expiry = date("Y-m-d", strtotime(str_replace('/', '-', $_POST['pco_expiry'])));
    }
	if(isset($_POST['tfl_expiry']))
	{
	$tfl_expiry = date("Y-m-d", strtotime(str_replace('/', '-', $_POST['tfl_expiry'])));
	}
	if(isset($_POST['badge_expiry']))
	{
	$badge_expiry = date("Y-m-d", strtotime(str_replace('/', '-', $_POST['badge_expiry'])));
     }
	 if(isset($_POST['balance']))
	 {
	 $balance=$_POST['balance'];
	 }
	 if(isset($_POST['reg_plate']))
	 {
	 $reg_plate=$_POST['reg_plate'];
	 }
	 if(isset($_POST['n_i_number']))
	 {
	 $n_i_number=$_POST['n_i_number'];
	 }
	 
}
if (!empty($_FILES['driver']['name'][0])) {
	
$driver_photo=$_FILES['driver']['name'][0];
$driver_photo=$first_name."-".$driver_photo;
}else{$driver_photo='';

}


$sql="INSERT INTO `employees`(first_name, phone,dob, email, address, post_code, pco_number,badge_number,tfl_expiry,driver_photo,pco_expiry,badge_expiry,reg_plate,n_i_number) values ('$first_name', '$phone','$dob', '$email', '$address', '$post_code', '$pco_number','$badge_number','$tfl_expiry','$driver_photo','$pco_expiry','$badge_expiry','$reg_plate','$n_i_number')";
 
if (!mysqli_query($con,$sql)) {echo $sql;
        die('Insert 02 -Error : ' . mysqli_error($con));
		exit;
         echo $sql;
}
//$driver_id = $con->insert_id;
$driver_id=mysqli_insert_id($con);



$address1="";
$address2="";
$city="";
$payment_terms="";

if(isset($_POST['phone']))
{
$phone=$_POST['phone'];
}
if(isset($_POST['mobile']))
{
$mobile=$_POST['mobile'];
}
if(isset($_POST['email']))
{
$email=$_POST['email'];
}
if(isset($_POST['address1']))
{
$address1=$_POST['address1'];
}
if(isset($_POST['address2']))
{
$address2=$_POST['address2'];
}
if(isset($_POST['city']))
{
$city=$_POST['city'];
}
if(isset($_POST['post_code']))
{
$post_code=$_POST['post_code'];
}
if(isset($_POST['payment_terms']))
{
$payment_terms=$_POST['payment_terms'];
}
mysqli_query($con,"INSERT INTO suppliers (name,phone,mobile,email,address1,address2,city,post_code,payment_terms) 
VALUES ('$first_name','$phone','$mobile','$email','$address','$address2','$city','$post_code','$payment_terms')");

$source_id=mysqli_insert_id($con);
$balance=0;
if(isset($_POST['balance']))
{
$balance=$_POST['balance'];
}
else
{
$balance=0;
}
$details="opening balance";
date_default_timezone_set ("Europe/London");
 $date = date('Y-m-d');
  $created = date('Y-m-d H:i:s');
  $sql = "INSERT INTO `debtors` (date, source_id,balance,details,created,name) VALUES('$date','$driver_id','$balance','$details','$created','$first_name')";
		 
				//	  echo $sql;

		  if (!mysqli_query($con,$sql)) {
			  echo $sql;
			  die('Create Transaction 3 INTO ORDER_Details -Error: ' . mysqli_error($con));
		  }
		  else
		  {
		 // echo 'success';
		  }


// Now update car database
if(isset($_POST['make']))
{
   $make = $_POST['make'];
 }
 if(isset($_POST['passengers']))
 {
   $passengers = $_POST['passengers'];
  }
  if(isset($_POST['mot_date']))
  {
   $mot_date = $_POST['mot_date'];
   $mot_date = date("Y-m-d", strtotime(str_replace('/', '-', $mot_date)));
   }
   if(isset($_POST['insurance_date']))
   {
   $insurance_date = $_POST['insurance_date'];
   $insurance_date = date("Y-m-d", strtotime(str_replace('/', '-', $insurance_date)));
   }
   if(isset($_POST['color']))
   {
   $color = $_POST['color'];
   }
   if(isset($_POST['reg']))
   {
   $reg = $_POST['reg'];
   }
$sql_car="INSERT INTO `vehicles`(`driver_id`, `make`, `passengers`, `mot_date`, `color`,insurance_date, `reg`) VALUES ($driver_id,'$make','$passengers','$mot_date','$color','$insurance_date','$reg')";
//echo $sql_car;
//exit;
if(!mysqli_query($con,$sql_car)){
	echo $sql_car;
echo '<script>alert("Driver Record was not created");</script>';
die('Insert 03 -Error : ' . mysqli_error($con));
exit;	
}


// Now upload photos in the folder
if(is_array($_FILES)) {
	//$count=count($_FILES['driver']['name']);
	
foreach ($_FILES['driver']['name'] as $name => $value){
if(is_uploaded_file($_FILES['driver']['tmp_name'][$name])) {
	
$sourcePath = $_FILES['driver']['tmp_name'][$name];
   if (!$result = mysqli_query($con, $sql)) {
      }
$targetPath = "../drivers/".$first_name."-".$_FILES['driver']['name'][$name];
//echo $sourcePath;
if(move_uploaded_file($sourcePath,$targetPath)) {
}}}}
echo "success";
//header('Location: ../list_drivers.php');
?>
