<?php
require_once('../config.inc');
require_once('common/mysql/default.php');

define('INF_ICON_SRC',URL.'/images/icons/inf_icon.gif');
define('INF_ODECET_DANE',"<img class='inf_icon' src='".INF_ICON_SRC."' onmouseover=\"return overlib('Pokud spl�c�te hypot�ku nebo �v�r od stavebn� spo�itelny, m��ete si sn��it da�ov� z�klad o ��stku zaplacen�ch �rok�.');\" onmouseout=\"return nd();\" />");


class Hypoteka {
	var $server_id,$partner_id,$page,$cena,$akontace,$hypoteka,$sazba,$splatnost,$splatka;
	var $charset = 'iso-8859-2';
	var $title = 'Hypote�n� kalkula�ka | Hypot�ka s e-Finance, a.s.';
	
	var $fixace_arr = array ();
		
	function __construct() {
		
		
		if($charset) {
			$this->charset = $charset;
		}
			
		$this->prepare_fix();
		if(!$fixace) { $fixace = 5; }
		if(!$this->sazba) {
			$this->sazba = $this->fixace_arr[$fixace]['sazba'];
			$this->sazba_fix = $fixace; //default 5 rocni
		}
		$this->calc();	
		
		/*
		if($_POST['calc'] || $_GET['calc']) {
			$this->calc();
		}
		*/
	}
        
        private function initialize_calc($server_id,$partner_id,$page,$cena,$akontace
				,$hypoteka,$sazba,$splatnost,$splatka,$charset='',$fixace=5) {
            $this->server_id = $server_id;
            $this->partner_id = $partner_id;
            $this->page = $page;

            $this->cena = $cena;
            $this->akontace = $akontace;
            $this->hypoteka = $hypoteka;
            $this->sazba = $sazba;
            $this->splatnost = $splatnost;
            //$this->splatka = $splatka;
        }
	
	
	public function render() {
		
		$cesta = 'hypoteka_tpl/basic.php';
		require_once $cesta;  	
			
	}
	
	public function render_page() {
		
		
		$cesta = 'hypoteka_tpl/page.php';
		require_once $cesta;  	
			
	}
	
	
	public function calc() {		
		if($this->cena && isset($this->akontace)) {		
			$this->hypoteka = $this->cena;
			if($this->akontace > 0) {
				$this->hypoteka -= ($this->cena * ($this->akontace / 100));
			} 
			$this->hypoteka = (int)$this->hypoteka;
		}
	
		if($this->cena && isset($this->akontace) && $this->hypoteka 
			&& $this->sazba && $this->splatnost) {
		
			$this->calcHU();
			
			$this->sumaUr = 0;
			for($r=0;$r < 12;$r++) {
				$this->calcUr($r);
			}
		
			//var_export($this->Ur);
		}
	}
	
	public function calcHU() {
		$i = $this->sazba / 100;
		$n = $this->splatnost;
		$HU = $this->hypoteka;
		
		$part1_1 = $HU * ($i / 12);
		$part1_2 = pow((1 + ($i / 12)),($n * 12));
		$part1 = $part1_1 * $part1_2;
		
		$part2_1 = 1 + ($i / 12) ;
		$part2_2 = ($n * 12);
		$part2 = pow($part2_1,$part2_2) - 1;

		$this->splatka = ($part1 / $part2);
		$this->splatka = round($this->splatka);				
	}
	
	//$r ... doba v mesicich od poskytnuti uveru do terminu Tr
	public function calcUr($r) {
		if(!$this->splatka) return false;
		
		$a = $this->splatka;
		$i = $this->sazba / 100 / 12;
		$n = $this->splatnost;
		
		$part1 = (1 / (1 + $i));
		$part2 = ($n * 12 - $r);		
		$temp = $a * ( 1 - pow( $part1 , $part2 ));
		$this->Ur[$r+1] = round($temp);
		$this->sumaUr += $this->Ur[$r+1];
	}
	
	
	function prepare_fix() {
		global $db;
		$arr = array(1,3,5,10,15,20,25,30);
		foreach($arr as $k=>$v) {
			$sql = "SELECT MIN(fix_".$v.") as sazba FROM gpdb_manager.hypo_sazby 
					WHERE fix_".$v." > 0";
			$res = mysqli_query($db,$sql);
			$row = mysqli_fetch_array($res);
			if($v == 1) { $word = 'rok'; }
			elseif($v > 1 & $v < 5) { $word = 'roky'; }
			elseif($v > 5) { $word = 'let'; }
			$this->fixace_arr[$v]['fixace'] = $v." ".$word;
			$this->fixace_arr[$v]['sazba'] = $row['sazba'];
		}	
	}
	
}
?>