<?php
/*******************************************************************************
 *                    _ _ _ _
 *                   / _ _  /
 *            _ _   / /__/ /_ _   ____ _ _   _ _ _    __
 *           / _ ` /   _ _// _ ` / __// _ ` / _ \\ \ / /
 *          / _ _// /\ \  / _ _/ \_ \/ _ _// / \/ \ ` /
 *          \_ , /_/  \_\ \_ ,  /__ /\_ , /_/      \_/
 *
 *******************************************************************************
 *    Application:  eReserv
 *    Version:      6
 *    Author:       David Di Certo
 *    Website:      http://www.cogites.com
 *    Email:        contact@cogites.com
 *    Copyright:    2013 - David Di Certo
 *******************************************************************************
 *    == FR ==      La revente et la redistribution de l'application sont
 *                  strictement interdites.
 *
 *                  Vous êtes tenu d'avoir pris connaissance des
 *                  Conditions Générales de Vente disponibles sur Cogites.com
 *                  (y compris pour la version Free).
 *
 *    == EN ==      Resale and redistribution of the application are
 *                  Strictly prohibited.
 *
 *                  You are required to have read the Terms and Conditions
 *                  available on Cogites.com (including the Free version).
 *
 *******************************************************************************
*/

	function getPost( $param) {
		if( isset($_POST[$param] ))
			return $_POST[$param];
		else 
			return "";
	}
	function getGet( $param) {
		if( isset($_GET[$param] ))
			return $_GET[$param];
		else 
			return "";
	}
	function getTextSummary($str, $limit=30, $strip = false) {
		$str = ($strip == true)?strip_tags($str):$str;
		if (strlen ($str) > $limit) {
			$str = substr ($str, 0, $limit - 3);
			return (substr ($str, 0, strrpos ($str, ' ')).'...');
		}
		return trim($str);
	}
	function getExtras($postExtra) {
		$extras = json_decode($postExtra, true);
		$result = array();
		if (!empty($extras)) {
			foreach($extras as $v){
				$data = array();
				$arrExtras = array_values($v);
				foreach($arrExtras as $i => $j){
					// if (!empty($j)) {
						switch ($i) {  // Table td ID
							case 0:
							$data['name'] = $j;
							break;
						case 1:
							$dom = new DOMDocument();
							$dom->loadHTML($j);
							$xpath = new DOMXPath($dom);
							$tags = $xpath->query('//a');
							foreach ($tags as $tag) {
								$data['qty'] = trim($tag->nodeValue);
								break;
							}
							break;
						case 2:
							$dom = new DOMDocument();
							$dom->loadHTML($j);
							$xpath = new DOMXPath($dom);
							$tags = $xpath->query('//a');
							foreach ($tags as $tag) {
								$data['price'] = trim($tag->nodeValue);
								break;
							}
							break;
						case 3:
							$data['pricetype'] = $j;
							break;
						case 4:
							$data['total'] = $j;
							break;
						case 5:
							$data['vat'] = $j;
							break;
						}
					// }
				}
				$result[] = $data;
			}
		}
		return $result;
	}

	function getVatTotal($arrReserv, $arrExtra, $config) {
		$result = 0;
		$arrVat = array();
		foreach (explode(',', $config['vat_list']) as $val) {
			if ($val > 0) {
				$arrVat[number_format((float)$val, 2, '.', '')] = "0";
			}
		}
		// This is to avoid error if the VAT point is deleted from the VAT list
		$arrVat[number_format((float)$arrReserv[0]->vat, 2, '.', '')] = 0;
		
		if ($arrReserv[0]->vat > 0) {
			if ($config['tax_rate_included']) {
				$ttotal = getTaxTotal($arrReserv, $config);
				$arrVat[number_format((float)$arrReserv[0]->vat, 2, '.', '')] += ($arrReserv[0]->price - $ttotal);
			}else{
				$arrVat[number_format((float)$arrReserv[0]->vat, 2, '.', '')] += $arrReserv[0]->price;
			}
		}

		foreach ($arrExtra as $extra) {
			if ($extra->vat > 0) {
				$arrVat[number_format((float)$extra->vat, 2, '.', '')] += $extra->total;
			}
		}
		foreach ($arrVat as $k => $v) {
			if (!($v == 0 || $k == 0)) {
				$val = $v;
				if ($config['price_vat'] == 1) {
					$result += ($val - ($val * 100 / (100+$k)));
				}else{
					$result += ($val * $k / 100);
				}
			}
		}
		$result = number_format($result, 2, '.', '');
		return $result;
	}

	function getTaxTotal($arrReserv, $config) {
		$result = 0;
		$tax_adults = 0;
		$tax_children = 0;
		$nbNights = round((strtotime($arrReserv[0]->departure_date) - strtotime($arrReserv[0]->arrival_date))/86400);
		if (!empty($config['tax_rate'])) {
			$tax_adults = $config['tax_rate'] * (int)$nbNights * (int)$arrReserv[0]->adults_nb;
		}
		if (!empty($config['tax_rate_children'])) {
			$tax_children = $config['tax_rate_children'] * (int)$nbNights * (int)$arrReserv[0]->childs_nb;
		}
		$result = $tax_adults + $tax_children;
		$result = number_format($result, 2, '.', '');
		return $result;
	}
	function getBalance($arrReserv, $arrExtra, $arrPayment, $config) {
		$result = 0;
		foreach ($arrReserv as $reserv) {
			$result += number_format($reserv->price, 2, '.', '');
		}
		foreach ($arrExtra as $extra) {
			$result += $extra->total;
		}
		foreach ($arrPayment as $payment) {
			$result = $result - $payment->amount;
		}
		if (!$config['tax_rate_included']) $result += getTaxTotal($arrReserv, $config);
		if (!$config['price_vat'] == 1) $result += getVatTotal($arrReserv, $arrExtra, $config);
		$result = number_format($result, 2, '.', '');
		return $result;
	}
	function getPriceHT($arrReserv, $arrExtra, $config) {
		$result = 0;
		foreach ($arrReserv as $reserv) {
			$result += number_format($reserv->price, 2, '.', '');
		}
		foreach ($arrExtra as $extra) {
			$result += $extra->total;
		}
		if ($config['tax_rate_included']) $result -= getTaxTotal($arrReserv, $config);
		if ($config['price_vat'] == 1) $result -= getVatTotal($arrReserv, $arrExtra, $config);
		return $result;
	}
	function getPriceTTC($arrReserv, $arrExtra, $config) {
		$result = 0;
		foreach ($arrReserv as $reserv) {
			$result += number_format($reserv->price, 2, '.', '');
		}
		foreach ($arrExtra as $extra) {
			$result += $extra->total;
		}
		if ($config['price_vat'] == 0) $result += getVatTotal($arrReserv, $arrExtra, $config);
		if (!$config['tax_rate_included']) $result += getTaxTotal($arrReserv, $config);
		return $result;
	}
?>