<?php
class db_query
{

	function print_header()
	{
		$mdb   = mysqli_connect('127.0.0.1','root', '', 'herp_amch');
		$parentPath = dirname(dirname($_SERVER['PHP_SELF']));
		global $rootpath;
		$loc = 'NOC';
		$msql = mysqli_query($mdb, "SELECT * FROM locations WHERE loc_code = '" . $loc . "'");
		$list = mysqli_fetch_assoc($msql);
		$data = '<div class="company-header" style="display: flex; justify-content: space-between; align-items: center; width: 100%; margin-bottom:0px;"><div style="flex: 0 0 auto;"><img src="' . $parentPath . '/img/logo/' . $list['logo'] . '" alt="Left Logo" style="margin: 5px 20px; height: 100px;" /></div>
        <div style="flex: 1; text-align: center; padding: 0 20px;">
                <h1 class="heading-top">Adesh Medical College & Hospital</h1>
                <span class="f14">NH-1 Village Mohri Teh Shahbad (M)</span><br><span class="f12">Contact Number - 0171-2700000,8222954101</span><br><span class="f12">TollFree-18001802012 Ext 200,233</span><br><span class="txt1">
                www.amch.ac.in, Email-ipd@amch.ac.in, opd@amch.ac.in
                </span>
        </div>
        <div style="flex: 0 0 auto;"><img src="' . $parentPath . '/img/nabh-logo.png" alt="Right Logo" style="margin: 5px 40px 0 0; height: 100px;" /></div></div>';
		return $data;
	}

	function getField($table, $condition, $val, $field)
	{
		global $db;
		//echo "select ".$field." from ".$table." where ".$condition." ='".$val."'";
		$sql = DB_query("select " . $field . " from " . $table . " where " . $condition . " ='" . $val . "'", $db);
		$r = DB_fetch_array($sql);
		//echo "select ".$field." from ".$table." where ".$condition." ='".$val."'"; 
		return ($r[$field]);
	}
	function GetFieldValue($table, $condition, $field)
	{
		global $db;
		//echo "select ".$field." from ".$table." where  $condition ";
		// echo "<br>";
		$sql = DB_query("select " . $field . " from " . $table . " where  $condition  ", $db);
		$r = DB_fetch_array($sql);
		return ($r[$field]);
	}

	function GetNumRecord($table_name,  $condition)
	{
		$sql = DB_query("SELECT * FROM $table_name where  $condition ");
		$r = DB_fetch_array($sql);
		return (DB_num_rows($sql));
	}

	function getFieldByCompanyId($table, $condition, $val, $field, $comp)
	{
		global $db;
		$sql = DB_query("select " . $field . " from " . $table . " where $condition ='" . $val . "' and   $comp", $db);
		$r = DB_fetch_array($sql);
		return ($r[$field]);
	}
	function RecordInsert($table, $dataArray)
	{
		global $db;
		unset($dataArray[0]);
		unset($dataArray[1]);
		$sql = "Insert into " . $table . " set ";
		$i = 0;
		foreach ($dataArray as $key => $val) {
			if ($i == 0)
				$sql .= " " . $key . " = '" . $val . "'";
			else
				$sql .= ", " . $key . " = '" . $val . "'";
			$i++;
		}

		//echo "<br>*************INSERT***************************************************</br>"			;
		//echo $sql;

		$r = DB_query($sql, $db);
		return ($r);
	}

	function RecordEdit($table, $dataArray, $cond)
	{
		global $db;
		unset($dataArray[0]);
		unset($dataArray[1]);
		$sql = "update  " . $table . " set ";
		$i = 0;
		foreach ($dataArray as $key => $val) {
			if ($i == 0)
				$sql .= " " . $key . " = '" . $val . "'";
			else
				$sql .= "," . $key . " = '" . $val . "'";
			$i++;
		}
		$sql .= " where  $cond";
		// echo "<br>******************************************************************</br>";
		//echo $sql;		 
		$r = DB_query($sql, $db);
		return ($r);
	}


	function GetRecordById($table_name,  $condition)
	{

		//echo "SELECT * FROM $table_name where  $condition ";
		global $db;
		$sql = Db_query("SELECT * FROM $table_name where  $condition ", $db);
		return $sql;
	}


	//***********************************************************************
	function insertRecord($table, $dataArray)
	{
		unset($dataArray[0]);
		unset($dataArray[1]);
		$sql = "Insert into " . $table . " set ";
		$i = 0;
		foreach ($dataArray as $key => $val) {
			if ($i == 0)
				$sql .= " " . $key . " = '" . $val . "'";
			else
				$sql .= ", " . $key . " = '" . $val . "'";
			$i++;
		}
		// echo "<br>*************INSERT***************************************************</br>";
		//  echo $sql;
		return ($sql);
	}

	// Function to edit record in Database 
	function EditRecord($table, $dataArray, $cond)
	{
		unset($dataArray[0]);
		unset($dataArray[1]);
		$sql = "update  " . $table . " set ";
		$i = 0;
		foreach ($dataArray as $key => $val) {
			if ($i == 0)
				$sql .= " " . $key . " = '" . $val . "'";
			else
				$sql .= "," . $key . " = '" . $val . "'";
			$i++;
		}
		$sql .= " where  $cond";
		//echo "<br>*************update***************************************************</br>";
		//echo $sql;
		return ($sql);
	}

	function DisplayRecord($table_name,  $condition)
	{
		global $db;
		$sql = DB_query("SELECT * FROM $table_name where  $condition ", $db);
		return ($sql);
	}


	function GetLastRecord($table_name,  $condition, $field)
	{
		global $db;

		//echo "SELECT $field FROM $table_name where  $condition  order by $field desc limit 1";
		$sql = DB_query("SELECT $field FROM $table_name where  $condition  order by $field desc limit 1", $db);
		$r = DB_fetch_array($sql);
		return ($r[$field]);
	}


	function GetRecord($table_name,  $condition)
	{
		$sql = "SELECT * FROM $table_name where  $condition ";
		return $sql;
	}

	function DeleteRecord($table_name,  $condition)
	{
		global $db;
		"<br>******************************************************************</br>";
		$sql =	" delete FROM $table_name where  $condition ";
		$sql = DB_query("delete FROM $table_name where  $condition ", $db);
		return $sql;
	}

	function pageLocation($filename)
	{
		echo "<SCRIPT LANGUAGE='JavaScript'>window.location='$filename'</SCRIPT>";
	}





	function FinedDays($fromdate, $todate)
	{
		$diff = abs(strtotime($fromdate) - strtotime($todate));
		$days = floor(($diff - $years * 365 * 60 * 60 * 24 - $months * 30 * 60 * 60 * 24) / (60 * 60 * 24));
		return  $days + 1;
	}

	function GETPhysicalAddress()
	{
		ob_start(); // Turn on output buffering
		system('ipconfig /all'); //Execute external program to display output 
		$capout = ob_get_contents(); // Capture the output into a variable 
		ob_clean(); // Clean the output buffer
		$findcurrentsystem = "Physical";
		$pmac = strpos($capout, $findcurrentsystem); // Find the position of Physical text 
		$mac = substr($capout, ($pmac + 36), 17); // Get Physical Address
		return $mac;
	}


	function GetRecordByJoin($table_name1, $table_name2,  $condition)
	{   // echo "SELECT * FROM $table_name1 A , $table_name2 B where  $condition ";
		global $db;
		$sql = Db_query("SELECT * FROM $table_name1 A , $table_name2 B where  $condition ", $db);
		return $sql;
	}


	function GetTotalRecordsTalbes($table, $condition)
	{
		global $db;
		$sql = DB_query("SELECT * FROM $table where  $condition", $db);
		$num = DB_num_rows($sql);
		return ($num);
	}



	function GetDeptIdByUser($userid, $CompanyId)
	{
		global $db;
		$sqlDeptid = DB_query("select department from www_users where id='" . $userid . "' and CompanyId='" . $CompanyId . "'", $db);
		$GetDeptId = DB_fetch_array($sqlDeptid);
		return $GetDeptId['department'];
	}


	function GetDeptHead($department, $UserId, $ApprovalType)
	{
		global $db;
		if ($_SESSION['UserType'] == 'SuperAdmin') {
			return $_SESSION['UserID'];
		} else {
			$SQLDeptHead = DB_query("SELECT userid FROM dept_indet_approval where department='" . $department . "' and find_in_set($UserId,userid) and ApprovalType='" . $ApprovalType . "'", $db);
			$GetDeptHead = DB_fetch_array($SQLDeptHead);
			return $GetDeptHead['userid'];
		}
	}




	function GetDeptWiseEmp($deptId, $CompanyId)
	{
		global $db;
		$SQLGetDeptEmp = DB_query("SELECT id FROM empmaster where department='" . $deptId . "' and CompanyId='" . $CompanyId . "'", $db);
		while ($GetDeptEmp = DB_fetch_array($SQLGetDeptEmp)) {
			$EmpIda[] = $GetDeptEmp['id'];
		}
		return $EmpIda;
	}


	//************FUNCTION USED FOR CHANGING THE SEQUENCE***********04-05-2013*************AMIT*******START HERE****************	


	function get_category_sorted_list_selectbox($selectedsortid, $moduleid) //GET SEQUENCE
	{
		global $db;
		$i = 0;
		$query = "SELECT * FROM modules_form where moduleid='" . $moduleid . "'";
		$result = DB_query($query, $db);
		$slbox = '';
		while ($output = DB_fetch_array($result)) {
			if ($selectedsortid == $output['sequence'])
				$selected = "selected";
			else
				$selected = "";

			$slbox .= "<option value='" . $output['id'] . "' $selected>" . $output['sequence'] . "</option>";
		}
		return $slbox;
	}

	function swap_sortid_category($currid, $newid) //UPDATE SEQUENCE
	{
		global $db;
		$sql_currid = "select sequence from modules_form where id=" . $currid;
		$run_currid = DB_query($sql_currid, $db);
		$rs_currid = DB_fetch_array($run_currid);
		$curr_sortid = $rs_currid['sequence'];

		$sql_newid = "select sequence from modules_form where id=" . $newid;
		$run_newid = DB_query($sql_newid, $db);
		$rs_newid = DB_fetch_array($run_newid);
		$new_sortid = $rs_newid['sequence'];

		$update_current_id = DB_query("update modules_form set sequence = '" . $new_sortid . "' where id = '" . $currid . "'", $db);
		$update_new_id = DB_query("update modules_form set sequence = '" . $curr_sortid . "' where id = '" . $newid . "'", $db);
	}

	function convert_number_to_words($number)
	{

		$hyphen      = '-';
		$conjunction = ' and ';
		$separator   = ', ';
		$negative    = 'negative ';
		$decimal     = ' point ';
		$dictionary  = array(
			0                   => 'zero',
			1                   => 'one',
			2                   => 'two',
			3                   => 'three',
			4                   => 'four',
			5                   => 'five',
			6                   => 'six',
			7                   => 'seven',
			8                   => 'eight',
			9                   => 'nine',
			10                  => 'ten',
			11                  => 'eleven',
			12                  => 'twelve',
			13                  => 'thirteen',
			14                  => 'fourteen',
			15                  => 'fifteen',
			16                  => 'sixteen',
			17                  => 'seventeen',
			18                  => 'eighteen',
			19                  => 'nineteen',
			20                  => 'twenty',
			30                  => 'thirty',
			40                  => 'fourty',
			50                  => 'fifty',
			60                  => 'sixty',
			70                  => 'seventy',
			80                  => 'eighty',
			90                  => 'ninety',
			100                 => 'hundred',
			1000                => 'thousand',
			1000000             => 'million',
			1000000000          => 'billion',
			1000000000000       => 'trillion',
			1000000000000000    => 'quadrillion',
			1000000000000000000 => 'quintillion'
		);

		if (!is_numeric($number)) {
			return false;
		}

		if (($number >= 0 && (int) $number < 0) || (int) $number < 0 - PHP_INT_MAX) {
			// overflow
			trigger_error(
				'convert_number_to_words only accepts numbers between -' . PHP_INT_MAX . ' and ' . PHP_INT_MAX,
				E_USER_WARNING
			);
			return false;
		}

		if ($number < 0) {
			return $negative . convert_number_to_words(abs($number));
		}

		$string = $fraction = null;

		if (strpos($number, '.') !== false) {
			list($number, $fraction) = explode('.', $number);
		}

		switch (true) {
			case $number < 21:
				$string = $dictionary[$number];
				break;
			case $number < 100:
				$tens   = ((int) ($number / 10)) * 10;
				$units  = $number % 10;
				$string = $dictionary[$tens];
				if ($units) {
					$string .= $hyphen . $dictionary[$units];
				}
				break;
			case $number < 1000:
				$hundreds  = $number / 100;
				$remainder = $number % 100;
				$string = $dictionary[$hundreds] . ' ' . $dictionary[100];
				if ($remainder) {
					$string .= $conjunction . convert_number_to_words($remainder);
				}
				break;
			default:
				$baseUnit = pow(1000, floor(log($number, 1000)));
				$numBaseUnits = (int) ($number / $baseUnit);
				$remainder = $number % $baseUnit;
				$string = convert_number_to_words($numBaseUnits) . ' ' . $dictionary[$baseUnit];
				if ($remainder) {
					$string .= $remainder < 100 ? $conjunction : $separator;
					$string .= convert_number_to_words($remainder);
				}
				break;
		}

		if (null !== $fraction && is_numeric($fraction)) {
			$string .= $decimal;
			$words = array();
			foreach (str_split((string) $fraction) as $number) {
				$words[] = $dictionary[$number];
			}
			$string .= implode(' ', $words);
		}

		return $string;
	}

	//************FUNCTION USED FOR CHANGING THE SEQUENCE***********04-05-2013*************AMIT*******END HERE****************		



}
$fnLib = new db_query();
$s = new db_query();
