<?php
$addHeadSpace = (!SableSystem::loggedIn() && ($_SESSION['current_dir'] === 'login'));

function getCheckedText($val){if ($val) echo('checked="checked"');}

function getNullText($val){if ($val==="NULL") echo(""); else echo($val);}

function getClassNameFromPageName()
{
	$tmp	= explode(' ', getTitleFromPageName());
	$new = [];
	foreach($tmp as $t) $new[] = strtolower($t);
	return implode('-', $new);
}

function getTitleFromPageName()
{
	$page		= $_SESSION['current_page'] === 'index.php'?'':$_SESSION['current_page'];
	$file		= '';
	$tmp		= pathinfo($page, PATHINFO_FILENAME);
	$tmp		= str_replace('_', ' ', $tmp);
	$tmp		= str_replace('-', ' ', $tmp);
	$tmparray	= explode(' ', $tmp);
	$on			= false;

	if (!empty($tmp))
	{
		foreach($tmparray as $t) 
		{
			$c		= ucfirst(trim($t)); 
			$file	.= $on?' '.$c:$c;
			$on		= true;
		}
	}
	return empty($file)?ucfirst(trim($_SESSION['current_dir'])):$file;	
}

function printBreadCrumb($title="")
{
	$file 	= empty($title)?getTitleFromPageName():$title;
	$dir 	= ucfirst($_SESSION['current_dir']);
	$page	= $_SESSION['current_page'];
//	SableDebug::debug($dir);
//	SableDebug::debug($file);
//	SableDebug::debug($page);
	if (!empty($dir))
	{
		if ($page === 'index.php')
		{
			$link = $dir;
		}
		else
		{
			//$link = '<a href="'.SableSystem::getThemeLinkPath(strtolower($dir).'/index.php').'">'.$dir.'</a>&nbsp;/&nbsp;'.$file;
			$link = '<a href="/'.strtolower($dir).'/">'.$dir.'</a>&nbsp;/&nbsp;'.$file;
		}
	}
	else
	{
		$link = $file;
	}
//	SableDebug::debug($link);
	$html = '
	<nav class="breadcrumbs">
		<ul>
			<li><a href="'.SABLE_URL_ROOT.'">HOME</a></li>
			<li><span>'.$link.'</span></li>
		</ul>
	</nav>';		
	echo($html);	
}

function isSableHead()	{return intval($_SESSION['company']['company_type_id']) === 1;}
function isSable()		{return intval($_SESSION['company']['company_type_id']) === 1 || 
								intval($_SESSION['company']['company_type_id']) === 2;}
function isSableManager()	{return isSableHead() && ($_SESSION['user']['access_level'] >= SABLE_ACCESS_MANAGER);}
function isSableAdmin()		{return isSableHead() && ($_SESSION['user']['access_level'] >= SABLE_ACCESS_ADMIN);}
function isSableUser()		{return isSableHead() && ($_SESSION['user']['access_level'] < SABLE_ACCESS_MANAGER);}
function isSableMechanic()	{return isSable() && intval($_SESSION['user']['position_id']) === 2;} //isMechanic()
function isSableClient()	{return intval($_SESSION['company']['company_type_id']) === 4;}

function getUserCount()
{
	global $sableDB;
	return $sableDB->getValue('select count(*) from user where user_code > 0 and deleted = 0');
}

function getClientCount()
{
	global $sableDB;
	return $sableDB->getValue('select count(*) from client');
}

function getBuildingCount()
{
	global $sableDB;
	return $sableDB->getValue('select count(*) from building');
}

function getServiceCount()
{
	global $sableDB;
	return $sableDB->getValue('select count(*) from service');
}

function getBookingCount()
{
	global $sableDB;
	return $sableDB->getValue('select count(*) from booking where booking_date = DATE("'.date('Y-m-d').'")');
}

function getPlanningCount()
{
	global $sableDB;
	return $sableDB->getValue('select count(*) from booking_schedule
							where (date(booking_time) = "'.date('Y-m-d').'") and (booking_schedule.team_id > 0)');
}

function getIncomeCount()
{
	global $sableDB;
	$today = new DateTime();
	$first = new DateTime();
	
	$first->setDate($first->format('Y'), $first->format('m'), 1);
	$first->setTime(0,0);
	
	$arr1 = $arr2 = [];
	$sql = 'select concat("R", FORMAT(sum(finance.txn_amount), 2)) as value from finance inner join txn_type on txn_type.txn_type_id = finance.txn_type_id inner join payment_method on payment_method.pm_id = finance.pm_id where (finance.txn_type_id = 1) and (DATE(txn_date) between "'.$first->format('Y-m-d').'" and "'.$today->format('Y-m-d').'")';
	$val = $sableDB->getValue($sql);
	$arr1[] = ['caption' => 'Turnover:', 'value' => $val];
	
	$sql = 'select concat(payment_method.pm_name, ":") as caption, concat("R", FORMAT(sum(txn_amount * -1), 2)) as value 
		  from finance
		  inner join txn_type on txn_type.txn_type_id = finance.txn_type_id
		  inner join payment_method on payment_method.pm_id = finance.pm_id
          where (finance.txn_type_id = 3) and (DATE(txn_date) between "'.$first->format('Y-m-d').'" and "'.$today->format('Y-m-d').'") 
          group by finance.pm_id';
	$arr2 = $sableDB->getRows($sql);	
	
	return array_merge($arr1, $arr2);
}

function getSMSCount()
{
	global $sableDB;
	$today = new DateTime();
	$arr = [];
	
	//outgoing SMSs
	$sql = 'select concat(sms_reply, ":") as caption, count(sms_sent) as value 
			from sms_message 
			where DATE(sms_message.sms_timestamp) = "'.$today->format('Y-m-d').'" 
			group by sms_reply';
	$rows = $sableDB->getRows($sql);
	
	if (empty($rows))
	{
		$arr[] = ['caption' => 'Sent:', 'value' => 0];
	}
	else $arr = $rows;
	
	//incoming SMSs
	$sql = 'select count(sms_sent) as value 
			from sms_message 
			inner join sms_message__reply on sms_message.sms_id = sms_message__reply.sms_id
			where DATE(sms_message.sms_timestamp) = "'.$today->format('Y-m-d').'" 
			group by sms_reply';
	$val = $sableDB->getValue($sql);
	$arr[] = ['caption' => 'Received:', 'value' => $val];
	return $arr;
	
}