<?php
include "../../../backend/db/db.php";
session_start();
if(isset($_SESSION['user'])) {
	$log = htmlspecialchars(trim($_SESSION['user']), ENT_QUOTES, 'UTF-8');
}
if (empty($log)) {
	$redirectUrl = "../../../home";
	echo "<script type=\"text/javascript\">";
	echo "window.location.href = '$redirectUrl'";
	echo "</script>";

} else {

	$sqlu = "SELECT id FROM contractor_reg WHERE SIDA_no='$log' and del='0'";
	$resultu = mysqli_query($conn, $sqlu);
	$countu = mysqli_num_rows($resultu);

	if($countu == 1){
		$rowc = mysqli_fetch_assoc($resultu);
		$user_id = $rowc['id'];

		/****************************** Get Invoice *************************************/
		
		if (array_key_exists('get_invoice', $_POST)) {
			$pid = htmlspecialchars(strip_tags(trim($_POST['pid'])), ENT_QUOTES, 'UTF-8');
			
			$sql = "SELECT invoice_no FROM project_invoice WHERE supplier = '$user_id' AND project='$pid'";
			
			$result = mysqli_query($conn, $sql);
			echo'<option value="">Select Invoice...</option>';
			while ($row = mysqli_fetch_assoc($result)) {
				$data[] = $row;
			}
		
			if (!empty($data)) {
				foreach ($data AS $data) {
					echo'<option value="' . $data['invoice_no'] . '">' . $data['invoice_no'] . '</option>';
				}
			} else {
				//echo'<option value="">No Data Found</option>';
			}
			echo json_encode($data);
		}

		/******************************************************************/		

	} else{
		$redirectUrl = "../../../home";
		echo "<script type=\"text/javascript\">";
		echo "window.location.href = '$redirectUrl'";
		echo "</script>";	
	}
}
?>