<?php
include '../db/db.php';
session_start();

if(isset($_SESSION['userID'])) {
	$log = htmlspecialchars(strip_tags(trim($_SESSION['userID'])), ENT_QUOTES, 'UTF-8');
} else {
	$log = '';
}

if (empty($log)) {     
	$redirectUrl = "../";
	echo "<script type=\"text/javascript\">";
	echo "window.location.href = '$redirectUrl'";
	echo "</script>";
	 
} else {
	$sqlu = "SELECT id FROM user_acc_tbl WHERE userID='$log' and access=1 and val=1";
	$stmtu = mysqli_query($conn, $sqlu);
	$countu = mysqli_num_rows($stmtu);
	
	if($countu == 1) {
		
		date_default_timezone_set('Asia/Colombo');
		
		/*************************** Insert ***************************/
		
		if(isset($_POST['sup_name'])) {
			$sup_name = htmlspecialchars(strip_tags(trim($_POST['sup_name'])), ENT_QUOTES, 'UTF-8');
			$address = htmlspecialchars(strip_tags(trim($_POST['address'])), ENT_QUOTES, 'UTF-8');
			$nic_no = htmlspecialchars(strip_tags(trim($_POST['nic_no'])), ENT_QUOTES, 'UTF-8');
			$mob_no = htmlspecialchars(strip_tags(trim($_POST['mob_no'])), ENT_QUOTES, 'UTF-8');
			$tel_no = htmlspecialchars(strip_tags(trim($_POST['tel_no'])), ENT_QUOTES, 'UTF-8');
			$vcode = htmlspecialchars(strip_tags(trim($_POST['vcode'])), ENT_QUOTES, 'UTF-8');
			$note = htmlspecialchars(strip_tags(trim($_POST['note'])), ENT_QUOTES, 'UTF-8');
		
			$stmt1 = mysqli_query($conn, "SELECT name FROM supplier WHERE name='$sup_name'");
			$row_count1 = mysqli_num_rows($stmt1);
			
			$stmt2 = mysqli_query($conn, "SELECT PName FROM account_posting WHERE PName='$sup_name'");
			$row_count2 = mysqli_num_rows($stmt2);
			
			$stmt3 = mysqli_query($conn, "SELECT nic FROM supplier WHERE nic='$nic_no'");
			$row_count3 = mysqli_num_rows($stmt3);
		
			if($row_count1 == 0 && $row_count2 == 0) {
				if($row_count3 == 0) {
					$sql_pr = mysqli_fetch_assoc(mysqli_query($conn, "SELECT PR_Code FROM account_numbers"));
					$PR_Code = $sql_pr['PR_Code'];
					mysqli_query($conn, "UPDATE account_numbers SET PR_Code=PR_Code+1");
					
					$L_Code = "6-8-11-".$PR_Code;
					
					$stmt4 = mysqli_query($conn, "SELECT L_Code FROM account_posting WHERE L_Code='$L_Code'");
					$row_count4 = mysqli_num_rows($stmt4);
					
					$stmt5 = mysqli_query($conn, "SELECT acc_no FROM supplier WHERE acc_no='$L_Code'");
					$row_count5 = mysqli_num_rows($stmt5);
					
					if($row_count4 == 0 && $row_count5 == 0) {
						$sql = "INSERT INTO supplier SET name='$sup_name', address='$address', nic='$nic_no', mobile='$mob_no', tp='$tel_no', note='$note', acc_no='$L_Code', vender_code='$vcode', reg_date=now()";
						$stmt_log = mysqli_query($conn, $sql);
						
						if($stmt_log === false) {
							echo 'error';
						
						} else {
							mysqli_query($conn, "INSERT INTO account_posting SET PName='$sup_name', B_Code='6', T_Code='8', H_Code='11', P_Code='$PR_Code', L_Code='$L_Code'");
							
							echo 'success';
						}
					
					} else {
						//mysqli_query($conn, "UPDATE account_numbers SET PR_Code=PR_Code+1");
					}
				
				} else {
					echo 'data exist1'; // code exist
				}
			
			} else {
				echo 'data exist'; // name exist
			}
		}
		
		/************************ Load Form Data ************************/
	    
		if (array_key_exists('get_dataset', $_POST)) {
			$id = htmlspecialchars(strip_tags(trim($_POST['id'])), ENT_QUOTES, 'UTF-8');

			$sql = mysqli_query($conn, "SELECT * FROM supplier WHERE id=$id"); 
			$row = mysqli_fetch_array($sql);

			echo json_encode($row);
	    }
		
		/*************************** Update ***************************/
		
		if(isset($_POST['id_up'])) {
			$id = htmlspecialchars(strip_tags(trim($_POST['id_up'])), ENT_QUOTES, 'UTF-8');
			$sup_name = htmlspecialchars(strip_tags(trim($_POST['sup_name_up'])), ENT_QUOTES, 'UTF-8');
			$address = htmlspecialchars(strip_tags(trim($_POST['address_up'])), ENT_QUOTES, 'UTF-8');
			$nic_no = htmlspecialchars(strip_tags(trim($_POST['nic_no_up'])), ENT_QUOTES, 'UTF-8');
			$mob_no = htmlspecialchars(strip_tags(trim($_POST['mob_no_up'])), ENT_QUOTES, 'UTF-8');
			$tel_no = htmlspecialchars(strip_tags(trim($_POST['tel_no_up'])), ENT_QUOTES, 'UTF-8');
			$vcode = htmlspecialchars(strip_tags(trim($_POST['vcode_up'])), ENT_QUOTES, 'UTF-8');
			$note = htmlspecialchars(strip_tags(trim($_POST['note_up'])), ENT_QUOTES, 'UTF-8');
			$acc_no = htmlspecialchars(strip_tags(trim($_POST['acc_no_up'])), ENT_QUOTES, 'UTF-8');
			
			$stmt1 = mysqli_query($conn, "SELECT name FROM supplier WHERE name='$sup_name' AND id!=$id");
			$row_count1 = mysqli_num_rows($stmt1);
			
			$stmt2 = mysqli_query($conn, "SELECT PName FROM account_posting WHERE PName='$sup_name' AND L_Code!='$acc_no'");
			$row_count2 = mysqli_num_rows($stmt2);
			
			$stmt3 = mysqli_query($conn, "SELECT nic FROM supplier WHERE nic='$nic_no' AND id!=$id");
			$row_count3 = mysqli_num_rows($stmt3);
		
			if($row_count1 == 0 && $row_count2 == 0) {
				if($row_count3 == 0) {
					$sql = "UPDATE supplier SET name='$sup_name', address='$address', nic='$nic_no', mobile='$mob_no', tp='$tel_no', note='$note', vender_code='$vcode' WHERE id=$id";
					$stmt_log = mysqli_query($conn, $sql);
					
					if($stmt_log === false) {
						echo 'error';
					
					} else {
						mysqli_query($conn, "UPDATE account_posting SET PName='$sup_name' WHERE L_Code='$acc_no'");
						echo 'success';
					}
				
				} else {
					echo 'data exist1'; // code exist
				}
			
			} else {
				echo 'data exist'; // name exist
			}
			
		}
		
		/************************ Remove *************************/
		
	    if (array_key_exists('remove_data', $_POST)) {
			$id = htmlspecialchars(strip_tags(trim($_POST['id'])), ENT_QUOTES, 'UTF-8');
			
			$stmt = mysqli_query( $conn, "SELECT sup_id FROM grn_tkn_order WHERE sup_id=$id");
			$row_count = mysqli_num_rows( $stmt );
			
			$stmt1 = mysqli_query( $conn, "SELECT supp_id FROM stock_orders WHERE supp_id=$id");
			$row_count1 = mysqli_num_rows( $stmt1 );
		  
			if($row_count == 0 && $row_count1 == 0) {
				$sql = "DELETE FROM supplier WHERE id=$id";
				$stmt_log = mysqli_query($conn, $sql);
				
				if( $stmt_log === false ) {
					echo json_encode(array("msgType" => 2, "msg" => "Sorry ! not deleted"));
				
				} else {
					echo json_encode(array("msgType" => 1, "msg" => "Successfully Removed"));
				}
			
			} else {
				echo json_encode(array("msgType" => 3, "msg" => "data exist"));
			}
	    }
		
	} else {
		$redirectUrl = "../";
		echo "<script type=\"text/javascript\">";
		echo "window.location.href = '$redirectUrl'";
		echo "</script>";
	}
}
?>