<?php 
include("../config.php");
if(!empty($_POST)){
	
	$product  = $_POST['lrdata'];
	if(!empty($_POST['emp_id'])){
    	$emp_id = mysqli_real_escape_string($conn, $_POST['emp_id']);
	}else{
		$emp_id = '';
	}

	if(!empty($_POST['type'])){
    	$types  = mysqli_real_escape_string($conn, $_POST['type']);
	}else{
		$types = '';
	}

	if(!empty($_POST['vendor_id'])){
    	$vendor_id = mysqli_real_escape_string($conn, $_POST['vendor_id']);
	}else{
		$vendor_id = '';
	}

	$t_date 			= mysqli_real_escape_string($conn, date('Y-m-d', strtotime($_POST['t_date'])));
	
	$tracking_id 		= mysqli_real_escape_string($conn, $_POST['tracking_id']);

	if($_POST["id"] != ''){
        $tracking_id = mysqli_real_escape_string($conn, $_POST['tracking_id']);
    }else{
        $tracking_id = mysqli_real_escape_string($conn, $_POST['tracking_id']);
        $checkpoint = mysqli_query($conn,"SELECT tracking_id FROM  tracking WHERE tracking_id = '".$tracking_id."'");
        
        if(mysqli_num_rows($checkpoint) > 0) {
        	$tracking_id = Tracking($conn);
        }
    }

	$company_id         = $_SESSION['company_id'];
  	$branch_id          = $_SESSION['branch_id'];
	$created_id         = mysqli_real_escape_string($conn, $_SESSION['user_detail']['id']);
	$created_datetime   = mysqli_real_escape_string($conn, date('Y-m-d H:i:s'));
	$updated_id         = mysqli_real_escape_string($conn, $_SESSION['user_detail']['id']);
	$updated_datetime   = mysqli_real_escape_string($conn, date('Y-m-d H:i:s'));
	
	if($_POST["id"] != ''){
		// UPDATE
		$type               = 'tracking_update';
		$user_id            = $_SESSION['user_detail']['id'];
		$data               = serialize($_POST);

		foreach ($product as $product) {
			$update = "UPDATE `tracking` SET `tracking_id`='".$tracking_id."',`t_date`='".$t_date."',`lr_no`='".$product['lr_no']."',`location`='".$product['location']."',`tracking_status`='".$product['tracking_status']."',`truck_no`='".$product['truck_no']."',`follow_up_time`='".$product['time']."',`updated_id`='".$updated_id."',`updated_datetime`='".$updated_datetime."' WHERE `lr_no` = '".$product['lr_no']."' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'";
			$results = mysqli_query($conn,$update);

			$lr_update 		= "UPDATE `lr_mst` SET `tracking_status`='".$product['tracking_status']."' WHERE `lr_no` = '".$product['lr_no']."' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'";
			$lr_update_res 	= mysqli_query($conn,$lr_update);
		}

		$activiti = "INSERT INTO `activities`(`type`, `user_id`, `data`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$type."','".$user_id."','".$data."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
		$acttivity_res = mysqli_query($conn,$activiti);

	}else{
		$type               = 'tracking_add';
		$user_id            = $_SESSION['user_detail']['id'];
		$data               = serialize($_POST);

		foreach ($product as $product) {
			if( isset($product['lr_no']) && !empty($product['lr_no']) && !empty($product['location'])) {
				$insert = "INSERT INTO `tracking`(`tracking_id`, `t_date`, `emp_id`, `type`, `vendor_id`, `lr_no`, `location`, `tracking_status`, `truck_no`, `follow_up_time`, `branch_id`, `company_id`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$tracking_id."','".$t_date."','".$emp_id."','".$types."','".$vendor_id."','".$product['lr_no']."','".$product['location']."','".$product['tracking_status']."','".$product['truck_no']."','".$product['time']."','".$branch_id."','".$company_id."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
				
				$results = mysqli_query($conn,$insert);

				$lr_update 		= "UPDATE `lr_mst` SET `tracking_status`='".$product['tracking_status']."' WHERE `lr_no` = '".$product['lr_no']."' AND branch_id = '".$_SESSION['branch_id']."' AND company_id = '".$_SESSION['company_id']."'";
				$lr_update_res 	= mysqli_query($conn,$lr_update);
			}
		}
		
		$activiti = "INSERT INTO `activities`(`type`, `user_id`, `data`, `created_id`, `created_datetime`, `updated_id`, `updated_datetime`) VALUES ('".$type."','".$user_id."','".$data."','".$created_id."','".$created_datetime."','".$updated_id."','".$updated_datetime."')";
		$acttivity_res = mysqli_query($conn,$activiti);		
	}
	
	echo json_encode( array('response' => 1, 'msg' => 'DATA SAVED SUCCESSFULLY', 'alert' => 1, 'redirect' => 'index.php?page=tracking/list.php') );die();
}else{
	echo json_encode( array('response' => 2, 'msg' => 'Opps', 'alert' => 1, 'redirect' => 'index.php?page=tracking/add.php') );die();
}  
?>