<?php
header('Content-type: application/json');
include '../db/db.php';
session_start();
$log = htmlspecialchars(trim($_SESSION['user']), ENT_QUOTES, 'UTF-8');
$log_lang = htmlspecialchars(trim($_SESSION['lang']), ENT_QUOTES, 'UTF-8');
$menu = htmlspecialchars(trim($_SESSION['menu']), ENT_QUOTES, 'UTF-8');
$emp_id = htmlspecialchars(trim($_SESSION['emp_id']), ENT_QUOTES, 'UTF-8');

if (empty($log)) {
	$redirectUrl = "index.php";
	echo "<script type=\"text/javascript\">";
	echo "window.location.href = '$redirectUrl'";
	echo "</script>";

} else {

	$sqlu = "SELECT * FROM user_priv_q WHERE emp_id='$emp_id' AND user='$log' AND menu_id='$menu'";
	$resultu = mysqli_query($conn, $sqlu);
	$countu = mysqli_num_rows($resultu);

	if($countu == 1){
		$rowc = mysqli_fetch_assoc($resultu);
		$user_id = $rowc['user_id'];
		
		date_default_timezone_set('Asia/Colombo');
		$date_today = date("Y-m-d");

		$response = array();
	
		if(isset($_POST['project'])) {
			$project = htmlspecialchars(trim($_POST['project']), ENT_QUOTES, 'UTF-8');
			$supplier = htmlspecialchars(trim($_POST['supplier']), ENT_QUOTES, 'UTF-8');
			$pub_supp = htmlspecialchars(trim($_POST['pub_supp']), ENT_QUOTES, 'UTF-8');
			$date = htmlspecialchars(trim($_POST['date']), ENT_QUOTES, 'UTF-8');
			$description = htmlspecialchars(trim($_POST['description']), ENT_QUOTES, 'UTF-8');
			$note = htmlspecialchars(trim($_POST['notes']), ENT_QUOTES, 'UTF-8');
			$progress = htmlspecialchars(trim($_POST['progress']), ENT_QUOTES, 'UTF-8');
			
			$sql_s = mysqli_fetch_array(mysqli_query($conn, "SELECT vat_reg FROM contractor_reg WHERE id='$supplier'"));
			$vat_reg = $sql_s['vat_reg'];
			
			$pay_type = htmlspecialchars(trim($_POST['pay_type']), ENT_QUOTES, 'UTF-8');
			
			if($pay_type == '1') {
				$type = 'Advance Payment';
			
			} else if($pay_type == '2') {
				$type = 'Part Payment';
			
			} else if($pay_type == '3') { // Final Payment
				$type = 'Retention Payment';
			
			} else {
				$type = '';
			}
			
			$con_amount = htmlspecialchars(trim($_POST['con_amount']), ENT_QUOTES, 'UTF-8');
			$pro_sum = htmlspecialchars(trim($_POST['pro_sum']), ENT_QUOTES, 'UTF-8');
			$adv_max = htmlspecialchars(trim($_POST['adv_max']), ENT_QUOTES, 'UTF-8');
			$adv_payment = htmlspecialchars(trim($_POST['adv_payment']), ENT_QUOTES, 'UTF-8');
			$pro_val = htmlspecialchars(trim($_POST['pro_val']), ENT_QUOTES, 'UTF-8');
			
			$gross_amount = htmlspecialchars(trim($_POST['gross_amount']), ENT_QUOTES, 'UTF-8');
			$gross_amount_pay = htmlspecialchars(trim($_POST['gross_amount_pay']), ENT_QUOTES, 'UTF-8');
			$prv_gross_amount = htmlspecialchars(trim($_POST['prv_gross_amount']), ENT_QUOTES, 'UTF-8');
			$less_adv_pay = htmlspecialchars(trim($_POST['less_adv_pay']), ENT_QUOTES, 'UTF-8');
			$balance_1 = htmlspecialchars(trim($_POST['balance_1']), ENT_QUOTES, 'UTF-8');
			$less_reten = htmlspecialchars(trim($_POST['less_reten']), ENT_QUOTES, 'UTF-8');
			$less_reten_val = htmlspecialchars(trim($_POST['less_reten_val']), ENT_QUOTES, 'UTF-8');
			$balance_2 = htmlspecialchars(trim($_POST['balance_2']), ENT_QUOTES, 'UTF-8');
			$less_LD_val = htmlspecialchars(trim($_POST['less_LD_val']), ENT_QUOTES, 'UTF-8');
			$less_LD = htmlspecialchars(trim($_POST['less_LD']), ENT_QUOTES, 'UTF-8');
			$net_amount_pay = htmlspecialchars(trim($_POST['net_amount_pay']), ENT_QUOTES, 'UTF-8');
			$vat = htmlspecialchars(trim($_POST['vat']), ENT_QUOTES, 'UTF-8');
			$vat_val = htmlspecialchars(trim($_POST['vat_val']), ENT_QUOTES, 'UTF-8');
			$less_WHT = ''; //htmlspecialchars(trim($_POST['less_WHT']), ENT_QUOTES, 'UTF-8');
			$paid_amount = htmlspecialchars(trim($_POST['paid_amount']), ENT_QUOTES, 'UTF-8');
			$retention = ''; //htmlspecialchars(trim($_POST['retention']), ENT_QUOTES, 'UTF-8');
			$vat_retention = ''; //htmlspecialchars(trim($_POST['vat_retention']), ENT_QUOTES, 'UTF-8');
			$tot_pay = $paid_amount; //htmlspecialchars(trim($_POST['tot_pay']), ENT_QUOTES, 'UTF-8');
			$adv_vat = htmlspecialchars(trim($_POST['adv_vat']), ENT_QUOTES, 'UTF-8');
			$adv_vat_val = htmlspecialchars(trim($_POST['adv_vat_val']), ENT_QUOTES, 'UTF-8');
			$adv_payment_val = htmlspecialchars(trim($_POST['adv_payment_val']), ENT_QUOTES, 'UTF-8');
			
			$max_reten = htmlspecialchars(trim($_POST['max_reten']), ENT_QUOTES, 'UTF-8');
			$tot_reten_val = htmlspecialchars(trim($_POST['tot_reten_val']), ENT_QUOTES, 'UTF-8');
			$less_LD_max = htmlspecialchars(trim($_POST['less_LD_max']), ENT_QUOTES, 'UTF-8');
			
			$retentiond = htmlspecialchars(trim($_POST['retentiond']), ENT_QUOTES, 'UTF-8');
			$vatfinal = htmlspecialchars(trim($_POST['vatfinal']), ENT_QUOTES, 'UTF-8');
			$vat_retenf = htmlspecialchars(trim($_POST['vat_retenf']), ENT_QUOTES, 'UTF-8');
			$tot_payf = htmlspecialchars(trim($_POST['tot_payf']), ENT_QUOTES, 'UTF-8');
			
			if($pay_type == '1') {
				$sql_p = mysqli_query($conn, "SELECT payment_type FROM project_invoice WHERE supplier='$supplier' AND project='$project' AND payment_type='Advance Payment' AND flag != 2");
				$count = mysqli_num_rows($sql_p);
				
				if($count == 0) { // Advance Payment
					if($adv_payment <= 0 || $adv_payment_val <= 0 || $adv_max <= 0) {
						$response['status'] = 'error2';
					
					} else {
						$sql_inv = mysqli_fetch_assoc(mysqli_query($conn, "SELECT invoice_no FROM group_id_tbl"));
						$inv = $sql_inv['invoice_no'];
						
						$inv2 = $inv+1;
						mysqli_query($conn, "UPDATE group_id_tbl SET invoice_no='$inv2'");
				
						$vt = '';
						if($vat_reg == 1) { 
							$vt = ", vat='$adv_vat', vat_val='$adv_vat_val'";
						}
						
						$sql = "INSERT INTO project_invoice SET currDate=now(), project='$project', supplier='$supplier', pub_supplier='$pub_supp', date='$date', note='$note', description='$description', progress='$progress', payment_type='$type', con_amount='$con_amount', pro_sum='$pro_sum', pro_val='$pro_val', adv_val='$adv_max', advance='$adv_payment', tot_pay='$adv_payment_val', invoice_no='$inv' ".$vt;
						$result = mysqli_query($conn, $sql);

						if ($result) {
							$response['status'] = 'ok';
						
						} else{
							$response['status'] = 'error';
						}
					}
				
				} else {
					$response['status'] = 'error4';
				}
			
			} else if($pay_type == '2') {
				if(empty($gross_amount) || empty($paid_amount)) {
					$response['status'] = 'error3';
				
				} else {
					
					if($vat_reg == 1) {
						if(empty($vat) || empty($vat_val) || empty($paid_amount)) {
							$response['status'] = 'error3';
						
						} else {
							$sql_inv = mysqli_fetch_assoc(mysqli_query($conn, "SELECT invoice_no FROM group_id_tbl"));
							$inv = $sql_inv['invoice_no'];
						
							// Part Payment with VAT
							$sql = "INSERT INTO project_invoice SET currDate=now(), con_amount='$con_amount', pro_sum='$pro_sum', pro_val='$pro_val', project='$project', supplier='$supplier', pub_supplier='$pub_supp', date='$date', note='$note', description='$description', progress='$progress', payment_type='$type', gross_amount='$gross_amount', gross_amount_pay='$gross_amount_pay', prv_gross_amount='$prv_gross_amount', less_adv_pay='$less_adv_pay', balance_1='$balance_1', less_reten='$less_reten', less_reten_val='$less_reten_val', balance_2='$balance_2', less_LD='$less_LD', less_LD_val='$less_LD_val', net_amount_pay='$net_amount_pay', vat='$vat', vat_val='$vat_val', less_WHT='$less_WHT', paid_amount='$paid_amount', retention='$retention', vat_retention='$vat_retention', tot_pay='$tot_pay', invoice_no='$inv', max_LD='$less_LD_max', max_reten='$max_reten', tot_reten_val='$tot_reten_val'"; 

							$result = mysqli_query($conn, $sql);

							if ($result) {
								$inv2 = $inv+1;
								mysqli_query($conn, "UPDATE group_id_tbl SET invoice_no='$inv2'");
								$response['status'] = 'ok';
							
							} else{
								$response['status'] = 'error';
							}							
						}
					
					} else {
						$sql_inv = mysqli_fetch_assoc(mysqli_query($conn, "SELECT invoice_no FROM group_id_tbl"));
						$inv = $sql_inv['invoice_no'];
							
						// Part Payment without VAT
						$sql = "INSERT INTO project_invoice SET currDate=now(), con_amount='$con_amount', pro_sum='$pro_sum', pro_val='$pro_val', project='$project', supplier='$supplier', pub_supplier='$pub_supp', date='$date', note='$note', description='$description', progress='$progress', payment_type='$type', gross_amount='$gross_amount', gross_amount_pay='$gross_amount_pay', prv_gross_amount='$prv_gross_amount', less_adv_pay='$less_adv_pay', balance_1='$balance_1', less_reten='$less_reten', less_reten_val='$less_reten_val', balance_2='$balance_2', less_LD='$less_LD', less_LD_val='$less_LD_val', net_amount_pay='$net_amount_pay', vat='', vat_val='', less_WHT='$less_WHT', paid_amount='$paid_amount', retention='$retention', vat_retention='', tot_pay='$tot_pay', invoice_no='$inv', max_LD='$less_LD_max', max_reten='$max_reten', tot_reten_val='$tot_reten_val'";
						
						$result = mysqli_query($conn, $sql);

						if ($result) {
							$inv2 = $inv+1;
							mysqli_query($conn, "UPDATE group_id_tbl SET invoice_no='$inv2'");
							$response['status'] = 'ok';
						
						} else{
							$response['status'] = 'error';
						}
					}
				}
			
			} else if($pay_type == '3') {
				if($retentiond <= 0 || $tot_payf <= 0) {
					$response['status'] = 'error6';
				
				} else {
					$sql_inv = mysqli_fetch_assoc(mysqli_query($conn, "SELECT invoice_no FROM group_id_tbl"));
					$inv = $sql_inv['invoice_no'];
					
					$inv2 = $inv+1;
					mysqli_query($conn, "UPDATE group_id_tbl SET invoice_no='$inv2'");
			
					$vt = '';
					if($vat_reg == 1) { 
						$vt = ", vat='$vatfinal', vat_retention='$vat_retenf'";
					}
					
					$sql = "INSERT INTO project_invoice SET currDate=now(), project='$project', supplier='$supplier', pub_supplier='$pub_supp', date='$date', note='$note', description='$description', progress='$progress', payment_type='$type', con_amount='$con_amount', pro_sum='$pro_sum', pro_val='$pro_val', retention='$retentiond', tot_pay='$tot_payf', invoice_no='$inv' ".$vt;
					$result = mysqli_query($conn, $sql);

					if ($result) {
						$response['status'] = 'ok';
					
					} else{
						$response['status'] = 'error';
					}
				}
			
			} else {
				$response['status'] = 'error5';
			}

			echo json_encode($response);
		}
		
		
		if(isset($_POST['prosum_up'])){
			$project = htmlspecialchars(trim($_POST['pro_id']), ENT_QUOTES, 'UTF-8');
			$supplier = htmlspecialchars(trim($_POST['sup_id']), ENT_QUOTES, 'UTF-8');
			$prosum = htmlspecialchars(trim($_POST['prosum_up']), ENT_QUOTES, 'UTF-8');
			
			$sql_pr = mysqli_fetch_assoc(mysqli_query($conn, "SELECT c_amount FROM aworded_contractors WHERE project='$project' AND supplier='$supplier'"));
			$con_amt = $sql_pr['c_amount'];
			
			if($con_amt > $prosum && $prosum >= 0) {
				$sql = "UPDATE aworded_contractors SET pro_sum='$prosum' WHERE project='$project' AND supplier='$supplier'";
				$result = mysqli_query($conn, $sql);
	
				if($result) {
					$response['status'] = 'ok';
				
				} else {
					$response['status'] = 'error';
				}				
				
			} else {
				$response['status'] = 'error4';
			}
			echo json_encode($response);
		}

		if(isset($_POST['type_pro'])){

			$type_pro = htmlspecialchars(trim($_POST['type_pro']), ENT_QUOTES, 'UTF-8');

			$sql = "INSERT INTO project_type SET type='$type_pro'";
          	$result = mysqli_query($conn, $sql);

          	if ($result) {
				$response['status'] = 'ok';
			}else{
				$response['status'] = 'error';
			}

			echo json_encode($response);

		}

		if(isset($_POST['id_holder'])){

			$id = htmlspecialchars(trim($_POST['id_holder']), ENT_QUOTES, 'UTF-8');
			$project_up = htmlspecialchars(trim($_POST['project_up']), ENT_QUOTES, 'UTF-8');
			$supplier_up = htmlspecialchars(trim($_POST['supplier_up']), ENT_QUOTES, 'UTF-8');
			$amount_up = htmlspecialchars(trim($_POST['amount_up']), ENT_QUOTES, 'UTF-8');
			$date_up = htmlspecialchars(trim($_POST['date_up']), ENT_QUOTES, 'UTF-8');
			$notes_up = htmlspecialchars(trim($_POST['notes_up']), ENT_QUOTES, 'UTF-8');
			$description_up = htmlspecialchars(trim($_POST['description_up']), ENT_QUOTES, 'UTF-8');
			$progress_up = htmlspecialchars(trim($_POST['progress_up']), ENT_QUOTES, 'UTF-8');

			//$sql = "UPDATE project_invoice SET project='$project_up', supplier='$supplier_up', amount='$amount_up', date='$date_up', note='$notes_up', description='$description_up', progress='$progress_up' WHERE id = $id";
          	$result = mysqli_query($conn, $sql);

          	if ($result) {
				$response['status'] = 'ok';
			}else{
				$response['status'] = 'error';
			}

			echo json_encode($response);

		}

		if (array_key_exists('get_dataset', $_POST)) {

			$reg_id = htmlspecialchars(strip_tags(trim($_POST['reg_id'])), ENT_QUOTES, 'UTF-8');
			
			$data = array();
			$sql = mysqli_query($conn, "SELECT * FROM project_invoice WHERE id=$reg_id");

			while ($row = mysqli_fetch_assoc($sql)) {
				$data[] = $row;
			}
			echo json_encode($data);
		}

		if (array_key_exists('remove_reg', $_POST)) {
			$pid = htmlspecialchars(trim($_POST['pid']), ENT_QUOTES, 'UTF-8');
			$id = htmlspecialchars(trim($_POST['reg_id']), ENT_QUOTES, 'UTF-8');
			
			if(empty($pid) || empty($id)) {
				$response['status'] = 'empty';
			
			} else {
				$sqlinv = mysqli_query($conn, "SELECT * FROM project_invoice WHERE project='$pid' AND flag != 2");
				if(mysqli_num_rows($sqlinv) == 0) {
					$sql2 = mysqli_query($conn, "DELETE FROM project_invoice WHERE id=$id AND flag=0");

					if ($sql2) {
						$response['status'] = 'ok';
					
					} else {
						$response['status'] = 'error';
					}
				
				} else {
					$response['status'] = 'exist';
				}
			}
			
			echo json_encode($response);
		}

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

}
?>