<?php

///////////////////////////////////////////////////////////////////////////////////////////////////
// GETS SOME BASIC DETAILS ABOUT THE BALANCE //////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////
function getBalanceBasicDetails($idBalance) {

	global $db, $DBTInvoiceBalances, $_LANG;

	if (!$idBalance) return 0;

	// the table month
	$thisTm = \App\Utils\Db\TablesUtils::getInstance()->getYmFromId($idBalance);

	if ($thisTm<200001) return 0;

	// get blance details
	$balanceDetailsSQL="SELECT balanceType, balanceStatus, balanceNumber, balanceDate, balanceCode, balanceName, addTime, idAddUser, confirmTime, idConfirmUser FROM `$DBTInvoiceBalances$thisTm` WHERE id='".$idBalance."'";
	$balanceDetailsArr=$db->GetRow($balanceDetailsSQL);
	if (!is_array($balanceDetailsArr)) {
		writeLog("Could not get balance details. SQL: ".$balanceDetailsSQL, 'events', 10);
		return 0;
	}

	// replace some values
	$balanceDetailsArr['balanceTypeName']=$_LANG['balanceInvoices']['balanceDetails']['type'][$balanceDetailsArr['balanceType']];
	$balanceDetailsArr['balanceStatusName']=$_LANG['balanceInvoices']['balanceDetails']['status'][$balanceDetailsArr['balanceStatus']];

	return $balanceDetailsArr;

}

///////////////////////////////////////////////////////////////////////////////////////////////////
// GETS ID, CODE AND NUMBER OF THE RESERVATION BASED ON INPUT (id reservation and id type). ///////
// IdType can be 'id' (normal/converted), 'code', 'number' (idRes may be any of the three types). /
///////////////////////////////////////////////////////////////////////////////////////////////////
function getReservationIdentifiers($idRes, $idType) {

	// the return result
	$return=array('id'=>'', 'code'=>'', 'number'=>'');

	// CHECK INPUT --------------------------------------------------------------------------------
	if (!$idRes) return $return;
	if ($idType!='id' and $idType!='code' and $idType!='number') return $return;

	// GET INFO FROM DATABASE /////////////////////////////////////////////////////////////////////
	global $db, $DBTReservations;

	// ID TYPE = id -------------------------------------------------------------------------------
	if ($idType=="id") {
		$idr=\App\Utils\Db\TablesUtils::getInstance()->switchIdReservationConversion($idRes, 'normal');
		$tt=\App\Utils\Db\TablesUtils::getInstance()->getYmFromId($idr);
		$resDetSQL="SELECT reservationCode, reservationNumber, convertedId FROM `$DBTReservations$tt` WHERE id='$idr'";
		$resDet=$db->GetRow($resDetSQL);
		$return['id']=\App\Utils\Db\TablesUtils::getInstance()->switchIdReservationConversion($idRes, 'converted');
		$return['number']=$resDet['reservationNumber'];
		$return['code']=$resDet['reservationCode'];
		if ($db->ErrorNo()) writeLog("Could not get reservation id details.", 'events', 9);
	}

	// ID TYPE = code -----------------------------------------------------------------------------
	if ($idType=="code") {
		$return['code']=$idRes;
	}

	// ID TYPE = number ---------------------------------------------------------------------------
	if ($idType=="number") {
		$return['number']=$idRes;
	}

	// return result
	return $return;

}



#########################################################################################
## RETURNS CITY AND COUNTRY DETAILS FOR A SPECIAL CODE (countryCode|cityId) #############
#########################################################################################
function getLocationDetails($locationCode) {

	$codes=preg_split('#[ |*,:]#', $locationCode);

	$countryCode=$codes[0];
	$cityId=$codes[1];

	if (!$countryCode and !is_numeric($cityId))
		return;

	$return['countryCode']=$countryCode;
	$return['countryName']=\Entity\Country::select($countryCode)->name;
	$return['cityId']=$cityId;
	$return['cityName']=\Entity\City::select($cityId)->name;

	return $return;

}


