<?php
//header('Content-type: application/pdf');
$path = $_SERVER['DOCUMENT_ROOT'] . "/sys/libs/";
define('FPDF_FONTPATH',$path . 'pdf/font/');
include_once('pdf/fpdf.php');
if($_GET['id']){
//require('lib_format.php');
require('../config.php');
require('../init.php');

}

class Billing {
	
	var $lang;
	
	function Billing(){
		
		global $db;	
		
		global $lang_pdf;
		
		$this->lang = $lang_pdf;
		
		$this->db = $db;
		$this->datacontainer = array();
		$this->path = $_SERVER['DOCUMENT_ROOT'] . "/sys/libs/";
	}    
	
	function Paypal_Izdaj($id){ 
		$result = $this->db->query("SELECT * FROM `ord_transactions` WHERE id='" . $id . "'");
		$fetch = $result->fetch();
		
		//Izdaj predračun
		$pid = $this->Predracun_Izdaj($fetch['id']);
		
		$result_p = $this->db->query("SELECT skupaj FROM `fin_predracuni` WHERE id='" . $pid . "'");
		$fetch_p = $result_p->fetch();
		
		//Dodaj v paypal tabelo
		$result = $this->db->query("INSERT INTO `ord_paypal` (`user_id`,`transaction_id`,`predracun_id`) VALUES ('".$fetch['user_id']."','".$fetch['id']."','".$pid."')");
		$return['id'] = $result->insertID();
		$return['id_predracun'] = $result_p->insertID();
		$return['pdf'] = $this->Predracun_Shrani($pid);
		$return['skupaj']= $fetch_p['skupaj'];
			
		
		return $return;
	}
	
	function Predracun_Placilo_Izdaj($id){ 
		$result = $this->db->query("SELECT * FROM `ord_transactions` WHERE id='" . $id . "'");
		$fetch = $result->fetch();
		
		//Izdaj predračun
		$id = $this->Predracun_Izdaj($fetch['id']);
		//Dodaj v banka tabelo
		$result = $this->db->query("INSERT INTO `ord_banka` (`user_id`,`transaction_id`,`predracun_id`) VALUES ('".$fetch['user_id']."','".$fetch['id']."','".$id."')");
		$return['id'] = $id;
		$return['pdf']= $this->Predracun_Shrani($id);
	
		
		return $return;
	}

	function Kartice_Izdaj($id){ 
		$result = $this->db->query("SELECT * FROM `ord_transactions` WHERE id='" . $id . "'");
		$fetch = $result->fetch();
		
		//Izdaj predračun
		$pid = $this->Predracun_Izdaj($fetch['id']);
		
		$result_p = $this->db->query("SELECT skupaj FROM `fin_predracuni` WHERE id='" . $pid . "'");
		$fetch_p = $result_p->fetch();
		
		//Dodaj v kartice tabelo
		$result = $this->db->query("INSERT INTO `ord_kartice` (`user_id`,`transaction_id`,`predracun_id`) VALUES ('".$fetch['user_id']."','".$fetch['id']."','".$pid."')");
		$return['id'] = $result->insertID();
		$return['id_predracun'] = $result_p->insertID();
		$return['pdf'] = $this->Predracun_Shrani($pid);
		$return['skupaj']= $fetch_p['skupaj'];
			
		return $return;
	}
	
	function AM_Izdaj($id,$am_id){ 
		$result = $this->db->query("SELECT * FROM `ord_transactions` WHERE id='" . $id . "'");
		$fetch = $result->fetch();
		
		//Izdaj predračun
		$pid = $this->Predracun_Izdaj($fetch['id']);
		
		$result_p = $this->db->query("SELECT skupaj,stevilka FROM `fin_predracuni` WHERE id='" . $pid . "'");
		$fetch_p = $result_p->fetch();
		

		//Dodaj v am tabelo
		$result = $this->db->query("UPDATE `ord_american` SET predracun_id='".$pid."' WHERE id='".$am_id."'");
		$return['id'] = $result->insertID();
		
		$return['pdf']= $this->Predracun_Shrani($pid);
		$return['skupaj']= $fetch_p['skupaj'];
		$return['predracun']= substr($fetch_p['stevilka'],1);
		$return['id_predracun'] = $result_p->insertID();
		
		return $return;
	}

	function Abanet_Izdaj($id){ 
		$result = $this->db->query("SELECT * FROM `ord_transactions` WHERE id='" . $id . "'");
		$fetch = $result->fetch();
		
		//Izdaj predračun
		$pid = $this->Predracun_Izdaj($fetch['id']);
		
		$result_p = $this->db->query("SELECT skupaj,stevilka FROM `fin_predracuni` WHERE id='" . $pid . "'");
		$fetch_p = $result_p->fetch();
		
		//Dodaj v abanet tabelo
		$result = $this->db->query("INSERT INTO `ord_abanet` (`user_id`,`transaction_id`,`predracun_id`) VALUES ('".$fetch['user_id']."','".$fetch['id']."','".$pid."')");
		$return['id'] = $result->insertID();
		$return['pdf']= $this->Predracun_Shrani($pid);
		$return['skupaj']= $fetch_p['skupaj'];
		$return['predracun']= substr($fetch_p['stevilka'],1);
		$return['id_predracun'] = $result_p->insertID();
		
		return $return;
	}
	
	function Moneta_Izdaj($id){ 
		$result = $this->db->query("SELECT * FROM `ord_transactions` WHERE id='" . $id . "'");
		$fetch = $result->fetch();
		
		//Izdaj predračun
		$id = $this->Predracun_Izdaj($fetch['id']);
		//Dodaj v moneta tabelo
		$result = $this->db->query("INSERT INTO `ord_moneta` (`user_id`,`transaction_id`,`predracun_id`) VALUES ('".$fetch['user_id']."','".$fetch['id']."','".$id."')");
		$return['id'] = $result->insertID();
		$return['pdf']= $this->Predracun_Shrani($id);
		$return['id_predracun'] = $id;
		
		return $return;
	}
	
	function Paysafecard_Izdaj($id){ 
		$result = $this->db->query("SELECT * FROM `ord_transactions` WHERE id='" . $id . "'");
		$fetch = $result->fetch();
		
		//Izdaj predračun
		$id = $this->Predracun_Izdaj($fetch['id']);
		//Dodaj v moneta tabelo
		$result = $this->db->query("INSERT INTO `ord_paysafecard` (`user_id`,`transaction_id`,`predracun_id`) VALUES ('".$fetch['user_id']."','".$fetch['id']."','".$id."')");
		$return['id'] = $result->insertID();
		$return['pdf']= $this->Predracun_Shrani($id);
		
		$resPredrac = $this->db->query("SELECT skupaj FROM `fin_predracuni` WHERE id='".$id."' LIMIT 0, 1;");
		$r = $resPredrac->fetch(MYSQL_ASSOC);
		$return['cena'] = $r['skupaj'];
		
		return $return;
	}
	
	//Predračuni / Računi / Avansi
	function Predracun_Izdaj($id){
		//Dobi transakcijo
		$result = $this->db->query("SELECT * FROM `ord_transactions` WHERE id='" . $id . "'");
		$fetch = $result->fetch();
		$fetch_t = $fetch;
		
		$set = array();
		$set['user_id'] = $fetch['user_id'];
		$set['stranka'] = $fetch['stranka'];
		$set['stranka_dostava'] = $fetch['stranka_dostava'];
		$set['datum'] = $fetch['datum'];
		$set['narocilo_id'] = $fetch['id'];
		$set['rok'] = $fetch['rok'];
		$set['bill'] = array();
		
		//Dobi stranko
		$result = $this->db->query("SELECT ime,priimek,naslov,posta,kraj FROM `ord_stranke` WHERE id='". $set['stranka'] ."' ORDER BY id DESC");
		$fetchs = $result->fetch();	
		
		//Dobi zadnji predračun
		$result = $this->db->query("SELECT stevilka FROM `fin_predracuni` ORDER BY id DESC");
		$fetch = $result->fetch();
		if(substr($fetch['stevilka'],1,4) == date("Y")){
			$rid = substr($fetch['stevilka'],6,5) + 1;
			$set['stevilka'] = "O" . date("Y") . "-" . sprintf("%05d", $rid);
		}else{
			$set['stevilka'] = "O" . date("Y") . "-00001";
		}
	
		//Dobi dostavo
		$result = $this->db->query("SELECT ime,priimek,naslov,posta,kraj,tel FROM `ord_stranke` WHERE id='". $set['stranka_dostava'] ."' ORDER BY id DESC");
		$fetchd = $result->fetch();	
		
		//Kreiraj predračun
		$result = $this->db->query("INSERT INTO `fin_predracuni` (`podjetje`,`ime`,`naslov`,`posta`,`kraj`,`d_ime`,`d_naslov`,`d_posta`,`d_kraj`,`d_telefon`,`davcna`,`datum`,`rok`,`dobava`,`stevilka`,`narocilo_id`,`user_id`,`create_user`) VALUES ('" . $fetchs['podjetje'] . "','" . $fetchs['ime'] . " " . $fetchs['priimek'] . "','" . $fetchs['naslov'] . "','" . $fetchs['posta'] . "','" . $fetchs['kraj'] . "','" . $fetchd['ime'] . " " . $fetchd['priimek'] . "','" . $fetchd['naslov'] . "','" . $fetchd['posta'] . "','" . $fetchd['kraj'] . "','" . $fetchd['tel'] . "','" . $fetchs['davcna'] . "','" . $set['datum'] . "','" . $set['rok'] . "','" . $clean['dobava'] . "','" . $set['stevilka'] . "','" . $set['narocilo_id'] . "','" . $set['user_id'] . "','" . $clean['create_user'] . "')");
		if(!$result){
			exit("Napaka pri kreiranju predračuna");
		}
		$set['rac_id'] = $result->insertID();
		
		//$result = $this->db->query("SELECT t.dodatek,t.kom,t.popust,s.naziv,s.sifra,s.cena,s.ddv,s.em FROM `ord_transactions_orders` t,`sifrant` s WHERE s.sifra=t.sifra AND t.transakcija_id='" . $set['user_id'] . "'");
		$result = $this->db->query("SELECT * FROM `ord_transactions_orders` WHERE transakcija_id='" . $id . "'");
		while($fetch = $result->fetch()){
			
			$results = $this->db->query("SELECT i.ime,p.ime AS proizvajalec,i.cena,i.ddv,i.enota,i.id AS id_izdelek,z.id FROM `modul_izdelki_zaloga` z, `modul_izdelki` i, `modul_izdelki_proizvajalci` p WHERE p.id=i.proizvajalec AND i.id=z.item_id AND z.koda='" . $fetch['sifra'] . "'");
			$fetchs = $results->fetch();
			
			if($fetchs['id']){
				if($fetch['komplet_id']){
					/*$sql = "SELECT * FROM modul_kompleti WHERE id='".$fetch['komplet_id']."'";
					$result_kp=$db->query($sql);
					$r_kp=$result_kp->fetch();*/
					$naziv = "Komplet #" . $fetch['komplet_id'] . ": " . $fetchs['proizvajalec'] . " - " . $fetchs['ime'];
				}else{
					$naziv = $fetchs['proizvajalec'] . " - " . $fetchs['ime'];
				}
				
				//Atributi
				$result_at = $this->db->query("SELECT at1,at2,zaloga FROM modul_izdelki_zaloga WHERE koda='".$fetch['sifra']."'");
				$r_at = $result_at->fetch();
				
				$sql = "SELECT DISTINCT k.ime AS atr,a.ime,a.ime_char FROM modul_sub_kat k, modul_izdelki_atributi a WHERE a.sub_kat=k.id AND (a.id='" . $r_at['at1']. "' OR a.id='" . $r_at['at2']. "') AND a.parent='".$fetchs['id_izdelek']."'";
				//print $sql;
				$result_at1 = $this->db->query($sql);
				while($r_at1 = $result_at1->fetch()){
					if($r_at1['ime']){
						if($atribut){
							$atribut .= ", " . $r_at1['atr'] . " - ";
						}else{
							$atribut .= $r_at1['atr'] . " - ";
						}
						if($r_at1['ime'] == "-1"){
							$atribut .= $r_at1['ime_char'];
						}else{
							$atribut .= $r_at1['ime'];
						}
					}
				}
				
				$clean['cena'] = $fetchs['cena'];
				$clean['ddv'] = $fetchs['ddv'];
				$clean['em'] = $fetchs['enota'];
				
			}else{
				$naziv = $fetch['dodatek'];
				$fetch['sifra'] = $fetch['sifra'];
				
				$clean['cena'] = $fetch['cena'];
				$clean['ddv'] = $fetch['ddv'];
				$clean['em'] = $fetch['enota'];
			}
		
			
			$resulti = $this->db->query("INSERT INTO `fin_predracuni_izdelki` (`sifra`,`naziv`,`atributi`,`cena`,`popust`,`ddv`,`kom`,`em`,`id_racuna`) VALUES
			 ('" . $fetch['sifra'] . "','" . $naziv . "','" . $atribut . "','" . round($clean['cena'],2) . "','" . $fetch['popust'] . "','" . $clean['ddv'] . "','" . $fetch['kom'] . "','" . $clean['em'] . "','" . $set['rac_id'] . "')");
			
			
			$current['popust'] = round(Popust_znesek($clean['cena']*$fetch['kom'],$fetch['popust']),2);
			$current['cena'] = round(($clean['cena']*$fetch['kom']),2);
			$current['ddv'] =  round(DDV_Znesek($current['cena']-$current['popust'],$clean['ddv']),2);
			
			$set['bill']['popust'] = $set['bill']['popust'] + $current['popust'];
			$set['bill']['ddv'] = $set['bill']['ddv'] + $current['ddv'];
			$set['bill']['vrednost'] = $set['bill']['vrednost'] + ($clean['cena']*$fetch['kom']);
			
			
			/*
			$setr = var_export($set,true);
			$curr = var_export($current,true);
			mail("dc@zabec.net","ovca",$setr . "\n\n\n\n" . $curr);*/
			unset($current);
			unset($atribut);
		}
		
		//Bonusi
		$sql = "SELECT id,datum,znesek FROM ord_bonusi WHERE user_id='".$fetch_t['user_id']."' and status='0' AND smer='1' AND veljavnost>='" . date("Y-m-d") . "'";		
		$result = $this->db->query($sql);
		$size = $result->size();
		
		if($size > 0){
			
			$bonuski = explode("-",$fetch_t['bonusi']);
			for($x = 0;$bonuski[$x];$x++){
				
				$sql = "SELECT id,datum,znesek FROM ord_bonusi WHERE user_id='".$fetch_t['user_id']."' and status='0' AND smer='1' AND veljavnost>='" . date("Y-m-d") . "' AND id='" . $bonuski[$x] . "'";
				
				$result = $this->db->query($sql);
				$r_b = $result->fetch();
				$bonus = $bonus + $r_b['znesek'];
				//Nuliraj bonus
				$sql = "UPDATE ord_bonusi SET status='1',porabljeno_id='".$set['narocilo_id']."' WHERE user_id='".$fetch_t['user_id']."' AND id='" . $bonuski[$x] . "'";
				$result = $this->db->query($sql);
				
			}
		}
		//OVCA
		//Še update skupnih zadev
		$bonusbddv = $bonus / 1.2;
		$bonusddv = $bonus - $bonusbddv;
		
		$set['bill']['ddv'] = $set['bill']['ddv'] - $bonusddv;
		$set['bill']['skupaj'] = round($set['bill']['ddv'] + $set['bill']['vrednost'] - $set['bill']['popust'] - $bonusbddv,2);
		if($set['bill']['skupaj'] < 0){
			$set['bill']['skupaj'] = 0;
		}
		if($set['bill']['ddv'] < 0){
			$set['bill']['ddv'] = 0;
		}
		if($set['bill']['vrednost'] < 0){
			$set['bill']['vrednost'] = 0;
		}
		

		$resulti = $this->db->query("UPDATE `fin_predracuni` SET ddv='" . $set['bill']['ddv'] . "',vrednost='" . $set['bill']['vrednost'] . "',popust='" . $set['bill']['popust'] . "',skupaj='" . $set['bill']['skupaj'] . "',bonus='" . $bonus . "' WHERE id='".$set['rac_id']."'");
		
		return $set['rac_id'];
		
	}
	
	function Predracun_Izpisi($id){
		$this->pdf = new FPDF('P','pt','A4');
		$this->Predracun_GenerirajPodatke($id);
		
		//Dodatno
		$this->datacontainer['settings'] = array();
		#$this->datacontainer['settings']['type'] = "Predračun";
		$this->datacontainer['settings']['type'] = $this;
		$this->datacontainer['settings']['sklic'] = "11";
		
		$this->GenerirajPDF($this->datacontainer);
		//$pdf->Output("pdf/tmp/" . $array['predracun'] . ".pdf");
		$this->pdf->Output();
	}
	
	function Predracun_Shrani($id){
		$this->pdf = new FPDF('P','pt','A4');
		$this->Predracun_GenerirajPodatke($id);
		
		//Dodatno
		$this->datacontainer['settings'] = array();
		$this->datacontainer['settings']['type'] = "Predračun";
		$this->datacontainer['settings']['sklic'] = "21";
		
		$this->GenerirajPDF($this->datacontainer);
		$number = "ool_predracun_" . time();
		$this->pdf->Output($path = $_SERVER['DOCUMENT_ROOT'] . "/sys/libs/../../files/tmp/" . $number . ".pdf");
		return $number . ".pdf";
		//$this->pdf->Output();
	}

	function Racun_Izpisi($id){
		$this->pdf = new FPDF('P','pt','A4');
		$this->Racun_GenerirajPodatke($id);
	
		//Dodatno
		$this->datacontainer['settings'] = array();
		$this->datacontainer['settings']['type'] = "Račun";
		$this->datacontainer['settings']['sklic'] = "22";
		
		$this->GenerirajPDF($this->datacontainer);
		//$pdf->Output("pdf/tmp/" . $array['predracun'] . ".pdf");
		$this->pdf->Output();
	}
	
	function AvansniRacun_Izpisi($id){
		$this->pdf = new FPDF('P','pt','A4');
		$this->Racun_GenerirajPodatke($id);
	
		//Dodatno
		$this->datacontainer['settings'] = array();
		$this->datacontainer['settings']['type'] = "Avansni Račun";
		$this->datacontainer['settings']['sklic'] = "23";
		
		$this->GenerirajPDF($this->datacontainer);
		//$pdf->Output("pdf/tmp/" . $array['predracun'] . ".pdf");
		$this->pdf->Output();
	}
	
	function GenerirajPDF($array){	
	#die("Payment.class.php > GenerirajPDF");
		$this->pdf->AddFont('Museo','','museo300.php');
		$this->pdf->AddFont('Museo','B','museo700.php');
		$this->pdf->AddFont('Museo','I','museo300.php');
		$this->pdf->SetFont('Museo','',10);
		$this->pdf->SetMargins(11,16,10);
		$this->pdf->SetAutoPageBreak(auto, 4.93);
		$this->pdf->SetCreator('Zabec.net');
		$this->pdf->SetSubject('Izpis');
		$this->pdf->SetDisplayMode(real, continuous);
		$this->pdf->AddPage();
		
		//Header 
		$this->pdf->Image($this->path . 'pdf/img/predracun_logo.jpg',410,50);
		
		//Naziv
		$this->pdf->SetFont('Museo','',14);

		//$this->pdf->Text(50,140,iconv('UTF-8', 'ISO-8859-2','PLAČNIK'));
		$this->pdf->Text(50,154,iconv('UTF-8', 'ISO-8859-2',$array['ime'] . " " . $array['priimek']));
		$this->pdf->Text(50,168,iconv('UTF-8', 'ISO-8859-2',$array['naslov']));
		$this->pdf->Text(50,190,iconv('UTF-8', 'ISO-8859-2',$array['posta'] . " " . $array['kraj']));	
		
		//Prvi rounded
		$this->pdf->SetLineWidth(0.1);
		$this->pdf->SetFillColor(255);
		$this->pdf->RoundedRect(24, 222, 363, 24, 3.5, 'DF');
		
		//Prvi ikonce
		$this->pdf->SetFont('Museo','',6);
		$this->pdf->RoundedRect(30, 225, 10, 10, 3.5, 'DF');
		$this->pdf->Text(33,233,'#');
		$this->pdf->SetFont('Museo','B',10);
		$this->pdf->Text(45,234,$this->lang[1]);
		
		$this->pdf->SetFont('Museo','',10);
		$this->pdf->Text(313,234,$this->lang[2]);

		$this->pdf->SetFont('Museo','',8);
		$this->pdf->Text(101,242,iconv('UTF-8', 'ISO-8859-2', $this->lang[3]));
		$this->pdf->Text(147,242,$this->lang[4]);
		$this->pdf->Text(216,242,$this->lang[5]);
		$this->pdf->Text(276,242,$this->lang[6]);
		$this->pdf->Text(300,242,$this->lang[7]);
		
		//Ok gremo listat produkte
		$h = 252;
		$skupaj = "";
		for($x = 0;$x < count($array['racuni']);$x++){
			$this->pdf->SetFont('Museo','',6);
			$this->pdf->RoundedRect(30, $h, 10, 10, 3.5, 'DF');
			$this->pdf->Text(33,$h + 8,$x + 1);
			$this->pdf->SetFont('Museo','B',10);
			$this->pdf->Text(45,$h + 9,iconv('UTF-8', 'ISO-8859-2',$array['racuni'][$x]['naziv']));
			$this->pdf->SetFont('Museo','B',7);
			$this->pdf->Text(45,$h + 25,iconv('UTF-8', 'ISO-8859-2',$array['racuni'][$x]['atributi']));
		
			$this->pdf->SetFont('Museo','',10);
			$this->pdf->Text(313,$h + 9,Format_Cena(round(DDV_Skupaj($array['racuni'][$x]['vrednost'],$array['racuni'][$x]['ddv']),2)) . " " . $array['valuta']);
			$this->pdf->SetFont('Museo','',8);
			$this->pdf->Text(101,$h + 17,$array['racuni'][$x]['kolicina'] . " " . $array['racuni'][$x]['enota']);
			$this->pdf->Text(147,$h + 17,Format_Cena(round($array['racuni'][$x]['cena'],2)) . " " . $array['valuta']);
			$this->pdf->Text(216,$h + 17,Format_Cena(round($array['racuni'][$x]['cena'],2)) . " " . $array['valuta']);
			$this->pdf->Text(276,$h + 17,$array['racuni'][$x]['ddv'] . " %");
			$this->pdf->Text(300,$h + 17,$array['racuni'][$x]['popust'] . " %");

			$h = $h + 35;
		}
		$skupajddv = $ddv - $skupaj;

		//Podpis
		$this->pdf->Text(60,$h + 50,$this->lang[8]);
		$this->pdf->Text(60,$h + 60,"Alba 2000 d.o.o.");
		$this->pdf->Image($this->path . 'pdf/img/podpis.jpg',60,$h + 70);
		
		if($array['settings']['type'] == "Predračun"){
			$this->pdf->Text(30,780,iconv('UTF-8', 'ISO-8859-2',$this->lang[14]));
		}
		
		//Skupaj
		$bonusbddv = $array['skupaj']['bonus'] / 1.2;
		$bonusddv = $array['skupaj']['bonus'] - $bonusbddv;
		
		$this->pdf->RoundedRect(210,$h + 30, 177, 102, 3.5, 'DF');
		$this->pdf->SetFont('Museo','',10);
		$this->pdf->Text(220,$h + 50,$this->lang[9]);
		
		$this->pdf->Text(220,$h + 62,$this->lang[10]);
		$this->pdf->Text(220,$h + 74,$this->lang[11]);
		$this->pdf->Text(220,$h + 86,$this->lang[12]);
		$this->pdf->Text(220,$h + 98,$this->lang[13]);
		
		$this->pdf->Text(320,$h + 50,Format_Cena(round($array['skupaj']['skupaj'],2)) . " " . $array['valuta']);
		$this->pdf->Text(320,$h + 62,Format_Cena(round($array['skupaj']['bonus'] - $bonusddv,2)) . " " . $array['valuta']);
		$this->pdf->Text(320,$h + 74,Format_Cena(round($array['skupaj']['popust'],2)) . " " . $array['valuta']);
		$this->pdf->Text(320,$h + 86,Format_Cena(round($array['skupaj']['rcpopust'],2)) . " " . $array['valuta'] . "");
		$this->pdf->Text(320,$h + 98,Format_Cena(round($array['skupaj']['ddv'],2)) . " " . $array['valuta']);

		
		//$this->pdf->Text(320,$h + 112,Format_Cena(round(DDV_Znesek($koncna,20),2)) . " " . $array['valuta']);
		
		$this->pdf->SetFont('Museo','B',10);
		if($array['settings']['type'] == "Avansni Račun"){
			$this->pdf->Text(220,$h + 122,iconv('UTF-8', 'ISO-8859-2', $this->lang[15]));
		}else{
			$this->pdf->Text(220,$h + 122,iconv('UTF-8', 'ISO-8859-2', $this->lang[16]));
		}
		
		$this->pdf->Text(320,$h + 122,Format_Cena(round($array['skupaj']['rcskupaj'],2)) . " " . $array['valuta']);
		//$this->pdf->Text(320,$h + 122,Format_Cena(round($koncna + DDV_Znesek($koncna,20),2)) . " " . $array['valuta']);
		
		//Še generalije za račun
		if($array['settings']['type'] == "Račun"){
			if($array['avans']){
				$this->pdf->RoundedRect(210, $h + 30, 177, 80, 3.5, 'DF');
				$this->pdf->SetFont('Museo','',10);
				$this->pdf->Text(220,$h + 50,iconv('UTF-8', 'ISO-8859-2', $this->lang[17]));
				$this->pdf->Text(220,$h + 62,iconv('UTF-8', 'ISO-8859-2', $this->lang[18]));
				$this->pdf->Text(220,$h + 74,$this->lang[54]);
			
				$this->pdf->Text(220,$h + 98,$this->lang[54]);
		
				$this->pdf->Text(320,$h + 62,Format_Cena(round($array['avans']['skupaj'],2)) . " " . $array['valuta']);
				$this->pdf->Text(320,$h + 74,Format_Cena(round($array['avans']['ddv'],2)) . " " . $array['valuta']);
		
				$this->pdf->Text(320,$h + 98,Format_Cena(round(($array['skupaj']['ddv'] - $array['avans']['ddv']),2)) . " " . $array['valuta']);
		
				$this->pdf->SetFont('Museo','B',10);
				$this->pdf->Text(220,$h + 86,iconv('UTF-8', 'ISO-8859-2', $this->lang[19]));
				$this->pdf->Text(320,$h + 86,Format_Cena(round(($array['skupaj']['rcskupaj'] - $array['avans']['skupaj']),2)) . " " . $array['valuta'])	;	
			}
			
			if($array['predracun']){
				$this->pdf->RoundedRect(210, $h + 160, 177, 30, 3.5, 'DF');
				$this->pdf->SetFont('Museo','B',10);
				$this->pdf->Text(220,$h + 180,iconv('UTF-8', 'ISO-8859-2', $this->lang[20]." " . $array['predracun'] . ""));
			}
		}	
		
		if($bonus){
			$this->pdf->SetFont('Museo','B',10);
			$this->pdf->Text(30,760,iconv('UTF-8', 'ISO-8859-2', $this->lang[21].": 110, 120"));
		}
		//No pa e podatki
		$this->pdf->SetLineWidth(0.1);
		$this->pdf->SetFillColor(255);
		$this->pdf->RoundedRect(410, 222, 170, 24, 3.5, 'DF');
		
		$this->pdf->SetFont('Museo','B',16);
		$this->pdf->Text(420,240,iconv('UTF-8', 'ISO-8859-2', $array['settings']['type']));
		
		$this->pdf->SetFont('Museo','B',10);
		$this->pdf->Text(420,270,iconv('UTF-8', 'ISO-8859-2', $this->lang[22]));
		$this->pdf->Text(420,282,$this->lang[52]);
		$this->pdf->Text(420,294,$this->lang[53]);
		$this->pdf->Text(420,318,$this->lang[54]);
		
		$this->pdf->SetFont('Museo','',10);
		$this->pdf->Text(500,270,$array['stevilka']);
		$this->pdf->Text(500,282,'Ljubljana');
		$this->pdf->Text(500,294,$array['datum']);
		$this->pdf->Text(500,318,"00 " . $array['settings']['sklic'] . "-" . substr($array['stevilka'],1));

		//Switch
		switch($array['settings']['type']){
			case "Predračun":
				$this->pdf->SetFont('Museo','B',10);
				$this->pdf->Text(420,306,$this->lang[23]);
				$this->pdf->SetFont('Museo','',10);
				$this->pdf->Text(500,306,$array['veljavnost']);
			break;
			case "Račun":
				$this->pdf->SetFont('Museo','B',10);
				$this->pdf->Text(420,306,iconv('UTF-8', 'ISO-8859-2', $this->lang[24]));
				$this->pdf->SetFont('Museo','',10);
				$this->pdf->Text(500,306,$array['rok']);
			break;
			case "Avansni Račun":
				$this->pdf->SetFont('Museo','B',10);
				$this->pdf->Text(420,306,iconv('UTF-8', 'ISO-8859-2', $this->lang[24]));
				$this->pdf->SetFont('Museo','',10);
				$this->pdf->Text(500,306,iconv('UTF-8', 'ISO-8859-2', $this->lang[25]));
			break;
		}		
		
		//Storitev opravljena		
		$this->pdf->SetLineWidth(0.1);
		$this->pdf->SetFillColor(255);
		$this->pdf->RoundedRect(410, 332, 170, 24, 3.5, 'DF');
		
		$this->pdf->SetFont('Museo','B',16);
		$this->pdf->Text(420,350,iconv('UTF-8', 'ISO-8859-2', $this->lang[26]));
		//Predračun
		switch($array['settings']['type']){
			case "Predračun":		
				$this->pdf->SetFont('Museo','B',10);
				$this->pdf->Text(420,380,$this->lang[27]);
				$this->pdf->Text(420,392,iconv('UTF-8', 'ISO-8859-2', $this->lang[28]));
				$this->pdf->Text(420,404,iconv('UTF-8', 'ISO-8859-2', $this->lang[29]));

				$this->pdf->SetFont('Museo','',10);
				$this->pdf->Text(500,380,$array['rok']);
				$this->pdf->Text(500,392,$array['nstev']);
				$this->pdf->Text(500,404,$array['ncas']);
			break;
			case "Račun":
				$this->pdf->SetFont('Museo','B',10);
				$this->pdf->Text(420,380,$this->lang[51]);
				$this->pdf->Text(420,392,iconv('UTF-8', 'ISO-8859-2', $this->lang[30]));
				$this->pdf->Text(420,404,iconv('UTF-8', 'ISO-8859-2', $this->lang[31]));
		
				$this->pdf->SetFont('Museo','',10);
				$this->pdf->Text(500,380,$array['opravljena']);
				$this->pdf->Text(500,392,$array['nstev']);
				
				if($array['predracun']){
					$this->pdf->Text(500,404,iconv('UTF-8', 'ISO-8859-2', $this->lang[32]));
				}else{
					$this->pdf->Text(500,404,iconv('UTF-8', 'ISO-8859-2', $this->lang[33]));
				}
			break;
			case "Avansni Račun":
				$this->pdf->SetFont('Museo','B',10);
				$this->pdf->Text(420,380,$this->lang[34]);
				$this->pdf->SetFont('Museo','',10);
				$this->pdf->Text(420,400,iconv('UTF-8', 'ISO-8859-2', $array['namen']));
			break;
		}
		
		//Dostava	
		$this->pdf->SetLineWidth(0.1);
		$this->pdf->SetFillColor(255);
		$this->pdf->RoundedRect(410, 420, 170, 24, 3.5, 'DF');
		
		$this->pdf->SetFont('Museo','B',16);
		$this->pdf->Text(420,438,iconv('UTF-8', 'ISO-8859-2', $this->lang[35]));
		
		$this->pdf->SetFont('Museo','B',10);
		$this->pdf->Text(420,468,$this->lang[49]);
		$this->pdf->Text(420,480,$this->lang[50]);
		$this->pdf->Text(420,492,iconv('UTF-8', 'ISO-8859-2', $this->lang[36]));
		$this->pdf->Text(420,504,'Tel:');
				
		$this->pdf->SetFont('Museo','',10);
		$this->pdf->Text(500,468,iconv('UTF-8', 'ISO-8859-2', $array['d_ime']));
		$this->pdf->Text(500,480,iconv('UTF-8', 'ISO-8859-2', $array['d_naslov']));
		$this->pdf->Text(500,492,iconv('UTF-8', 'ISO-8859-2', $array['d_posta'] . " " . $array['d_kraj']));
		$this->pdf->Text(500,504,iconv('UTF-8', 'ISO-8859-2', $array['d_telefon']));
		
		//O Nas		
		$this->pdf->SetLineWidth(0.1);
		$this->pdf->SetFillColor(255);
		$this->pdf->RoundedRect(410, 520, 170, 24, 3.5, 'DF');
		
		$this->pdf->SetFont('Museo','B',16);
		$this->pdf->Text(420,538,iconv('UTF-8', 'ISO-8859-2', $this->lang[37]));
		
		$this->pdf->SetFont('Museo','B',10);
		$this->pdf->Text(420,568,$this->lang[38]);
		$this->pdf->Text(420,580,$this->lang[39]);
		$this->pdf->Text(420,604,$this->lang[40]);
		$this->pdf->Text(420,616,$this->lang[41]);
		$this->pdf->Text(420,628,$this->lang[42]);
		$this->pdf->Text(420,640,$this->lang[43]);
		$this->pdf->Text(420,652,'www:');
		
		$this->pdf->SetFont('Museo','',10);
		$this->pdf->Text(480,568,'Alba 2000 d.o.o.');
		$this->pdf->Text(480,580,'Detelova 3');
		$this->pdf->Text(480,592,'1000 Ljubljana');
		$this->pdf->Text(480,604,'SI57295719');
		$this->pdf->Text(480,616,'SI56 0510 0801 3063 661');
		$this->pdf->Text(480,628,'0591 66 980');
		$this->pdf->Text(480,640,'info@outoflimitz.com');
		$this->pdf->Text(480,652,'www.outoflimitz.com');
		

		
		//Tisk
		$this->pdf->SetFont('Museo','',6);
		$this->pdf->Text(500,815,$this->lang[44].' ' . $array['user']);
		$this->pdf->Text(500,823,$this->lang[45].' ' . date("d.m.Y H:i"));
		//Footer
	    //Go to 1.5 cm from bottom
	    //Select Museo italic 8
	    $this->pdf->SetFont('Museo','I',6);
	    //Print centered page number
		$this->pdf->SetY(-44);
		$this->pdf->Cell(0,10,'Alba 2000 d.o.o., Detelova 3, 1000 Ljubljana',0,0,'C');
		$this->pdf->SetY(-34);
		$this->pdf->Cell(0,10,iconv('UTF-8', 'ISO-8859-2', $this->lang[46]." 1468103 - DŠ: SI57295719 - ".$this->lang[41]." 05100-8012201040"),0,0,'C');
		$this->pdf->SetY(-24);
		$this->pdf->Cell(0,10,iconv('UTF-8', 'ISO-8859-2', $this->lang[47]),0,0,'C');
	}

	function AvansiRacun_GenerirajPodatke($id){
		$query = "SELECT * FROM `fin_avansni` WHERE id='" . $id . "'";
		$result = $this->db->query($query);
		$fetch = $result->fetch();

		$queryp = "SELECT * FROM `poste` WHERE stevilka='" . $fetch['posta'] . "'";
		$resultp = $this->db->query($queryp);
		$fetchp = $resultp->fetch();

		$this->datacontainer = array();
		$this->datacontainer['racun'] = $fetch['racun'];
		$this->datacontainer['ime'] = $fetch['ime'];
		$this->datacontainer['priimek'] = $fetch['priimek'];
		$this->datacontainer['podjetje'] = $fetch['podjetje'];
		$this->datacontainer['naslov'] = $fetch['naslov'];
		$this->datacontainer['posta'] = $fetch['posta'];
		$this->datacontainer['kraj'] = $fetchp['naziv'];
		
		
		$this->datacontainer['ddvid'] = $fetch['davcna'];
		$this->datacontainer['datum'] = formatDateFromDB($fetch['datum']);
		$this->datacontainer['rok'] = formatDateFromDB($fetch['rok']);
		$this->datacontainer['opravljena'] = $fetch['opravljena'];
		$this->datacontainer['predracun'] = $fetch['predracun'];
		$this->datacontainer['prevzem'] = "ePo1ta";
		$this->datacontainer['veljavnost'] = formatDateFromDB($fetch['rok']);
		$this->datacontainer['namen'] = $fetch['namen'];
		if($fetch['valuta'] == 1){ 
			$this->datacontainer['valuta'] = "SIT";
			$this->datacontainer['valuta2'] = "EUR";
		}else{
			$this->datacontainer['valuta'] = "EUR";
			$this->datacontainer['valuta2'] = "SIT";
		}
		$this->datacontainer['racuni'] = array();
		$queryi = "SELECT * FROM `fin_avansni_izdelki` WHERE id_racuna='" . $fetch['id'] . "' ORDER BY id ASC";
		$resulti = $this->db->query($queryi);
		//$max = $db->Lines_Num($resulti);
		for($x = 0;$fetchi = $resulti->fetch();$x++){
			$this->datacontainer['racuni'][$x] = array();
			$this->datacontainer['racuni'][$x]['sifra'] = $fetchi['sifra'];
			$this->datacontainer['racuni'][$x]['naziv'] = $fetchi['naziv'];
			$this->datacontainer['racuni'][$x]['kolicina'] = $fetchi['kom'];
			$this->datacontainer['racuni'][$x]['enota'] = $fetchi['em'];
			$this->datacontainer['racuni'][$x]['cena'] = $fetchi['cena'];
			$this->datacontainer['racuni'][$x]['ddv'] = $fetchi['ddv'];
			$this->datacontainer['racuni'][$x]['popust'] = $fetchi['popust'];
			$this->datacontainer['racuni'][$x]['vrednost'] = $fetchi['cena'] * $fetchi['kom'];
		}
		$this->datacontainer['skupaj'] = array();
		$this->datacontainer['skupaj']['skupaj'] = $fetch['vrednost'];
		$this->datacontainer['skupaj']['popust'] = $fetch['popust'];
		$this->datacontainer['skupaj']['rcpopust'] = $fetch['vrednost'] - $fetch['popust'];
		$this->datacontainer['skupaj']['ddv'] = $fetch['ddv'];
		$this->datacontainer['skupaj']['rcskupaj'] = $fetch['skupaj'];
		if($fetch['valuta'] == 1){
			$this->datacontainer['skupaj']['rcskupajeur'] = $fetch['skupaj']/239.64;
		}else{
			$this->datacontainer['skupaj']['rcskupajeur'] = $fetch['skupaj']*239.64;
		}
	}	
	
	function Racun_GenerirajPodatke($id){
		$query = "SELECT * FROM `fin_racuni` WHERE id='" . $id . "'";
		$result = $this->db->query($query);
		$fetch = $result->fetch();

		$queryp = "SELECT * FROM `poste` WHERE stevilka='" . $fetch['posta'] . "'";
		$resultp = $this->db->query($queryp);
		$fetchp = $resultp->fetch();

		$this->datacontainer = array();
		$this->datacontainer['stevilka'] = $fetch['stevilka'];
		$this->datacontainer['ime'] = $fetch['ime'];
		$this->datacontainer['priimek'] = $fetch['priimek'];
		$this->datacontainer['podjetje'] = $fetch['podjetje'];
		$this->datacontainer['naslov'] = $fetch['naslov'];
		$this->datacontainer['posta'] = $fetch['posta'];
		$this->datacontainer['kraj'] = $fetchp['naziv'];
		$this->datacontainer['ddvid'] = $fetch['davcna'];
		$this->datacontainer['datum'] = formatDateFromDB($fetch['datum']);
		$this->datacontainer['rok'] = formatDateFromDB($fetch['rok']);
		$this->datacontainer['opravljena'] = $fetch['opravljena'];
		$this->datacontainer['predracun'] = $fetch['predracun'];
		$this->datacontainer['prevzem'] = "ePošta";
		$this->datacontainer['veljavnost'] = formatDateFromDB($fetch['rok']);
		if($fetch['valuta'] == 1){ 
			$this->datacontainer['valuta'] = "SIT";
			$this->datacontainer['valuta2'] = "EUR";
		}else{
			$this->datacontainer['valuta'] = "EUR";
			$this->datacontainer['valuta2'] = "SIT";
		}
		if($fetch['avans_id']){
			$querya = "SELECT skupaj,ddv,racun FROM `fin_avansni` WHERE id='" . $fetch['avans_id'] . "'";
			$resulta = $this->db->query($querya);
			$fetcha = $db->Fetch_Array_Name($resulta);
			$this->datacontainer['avans'] = array();
			$this->datacontainer['avans']['skupaj'] = $fetcha['skupaj'];
			$this->datacontainer['avans']['ddv'] = $fetcha['ddv'];
			$this->datacontainer['avans']['racun'] = $fetcha['racun'];
		}
		$this->datacontainer['racuni'] = array();
		$queryi = "SELECT * FROM `fin_racuni_izdelki` WHERE id_racuna='" . $fetch['id'] . "' ORDER BY id ASC";
		$resulti = $this->db->query($queryi);
		//$max = $db->Lines_Num($resulti);
		for($x = 0;$fetchi = $resulti->fetch();$x++){
			$this->datacontainer['racuni'][$x] = array();
			$this->datacontainer['racuni'][$x]['sifra'] = $fetchi['sifra'];
			$this->datacontainer['racuni'][$x]['naziv'] = $fetchi['naziv'];
			$this->datacontainer['racuni'][$x]['kolicina'] = $fetchi['kom'];
			$this->datacontainer['racuni'][$x]['enota'] = $fetchi['em'];
			$this->datacontainer['racuni'][$x]['cena'] = $fetchi['cena'];
			$this->datacontainer['racuni'][$x]['ddv'] = $fetchi['ddv'];
			$this->datacontainer['racuni'][$x]['popust'] = $fetchi['popust'];
			$this->datacontainer['racuni'][$x]['vrednost'] = $fetchi['cena'] * $fetchi['kom'];
		}
		$this->datacontainer['skupaj'] = array();
		$this->datacontainer['skupaj']['skupaj'] = $fetch['vrednost'];
		$this->datacontainer['skupaj']['popust'] = $fetch['popust'];
		$rcpopust = $fetch['vrednost'] - $fetch['popust'];
		if($rcpopust < 0){
			$this->datacontainer['skupaj']['rcpopust'] = 0;
		}else{
			$this->datacontainer['skupaj']['rcpopust'] = $fetch['vrednost'] - $fetch['popust'];
		}
		$this->datacontainer['skupaj']['ddv'] = $fetch['ddv'];
		$this->datacontainer['skupaj']['rcskupaj'] = $fetch['skupaj'];
		if($fetch['valuta'] == 1){
			$this->datacontainer['skupaj']['rcskupajeur'] = $fetch['skupaj']/239.64;
		}else{
			$this->datacontainer['skupaj']['rcskupajeur'] = $fetch['skupaj']*239.64;
		}
	}
			
	function Predracun_GenerirajPodatke($id){
		$query = "SELECT * FROM `fin_predracuni` WHERE id='" . $id . "'";
		$result = $this->db->query($query);
		$fetch = $result->fetch();

		$this->datacontainer = array();
		if($_SESSION['user']){
			$this->datacontainer['user'] = $_SESSION['user'];
		}else{
			$this->datacontainer['user'] = "Zabec.net ".$this->lang[48];
		}
		
		//Še čas naročila
		$query = "SELECT * FROM `ord_transactions` WHERE id='" . $fetch['narocilo_id'] . "'";
		$resultn = $this->db->query($query);
		$fetchn = $resultn->fetch();
		$this->datacontainer['ncas'] = formatDateFromDB($fetchn['datum'] . " " . $fetchn['cas'],"dateshorttime");
		$this->datacontainer['stevilka'] = $fetch['stevilka'];
		$this->datacontainer['ime'] = $fetch['ime'];
		$this->datacontainer['priimek'] = $fetch['priimek'];
		$this->datacontainer['podjetje'] = $fetch['podjetje'];
		$this->datacontainer['naslov'] = $fetch['naslov'];
		$this->datacontainer['posta'] = $fetch['posta'];
		$this->datacontainer['kraj'] = $fetch['kraj'];
		
		$this->datacontainer['d_ime'] = $fetch['d_ime'];
		$this->datacontainer['d_priimek'] = $fetch['d_priimek'];
		$this->datacontainer['d_telefon'] = $fetch['d_telefon'];
		$this->datacontainer['d_naslov'] = $fetch['d_naslov'];
		$this->datacontainer['d_posta'] = $fetch['d_posta'];
		$this->datacontainer['d_kraj'] = $fetch['d_kraj'];
		
		$this->datacontainer['ddvid'] = $fetch['davcna'];
		$this->datacontainer['stnarocnika'] = $fetch['user_id'];
		$this->datacontainer['nstev'] = $fetch['narocilo_id'];
		$this->datacontainer['datum'] = formatDateFromDB($fetch['datum']);
		if(is_numeric($fetch['rok']{0})){
			$this->datacontainer['rok'] = formatDateFromDB($fetch['rok']);
		}else{
			$this->datacontainer['rok'] = $fetch['rok'];
		}

		$this->datacontainer['prevzem'] = "e-pošta";
		$this->datacontainer['veljavnost'] = formatDateFromDB($fetch['rok']);
		$this->datacontainer['valuta'] = "EUR";
		$this->datacontainer['racuni'] = array();
		$queryi = "SELECT * FROM `fin_predracuni_izdelki` WHERE id_racuna='" . $fetch['id'] . "' ORDER BY id ASC";
		$resulti = $this->db->query($queryi);
//		$max = $db->Lines_Num($resulti);
		for($x = 0;$fetchi = $resulti->fetch();$x++){
			$this->datacontainer['racuni'][$x] = array();
			$this->datacontainer['racuni'][$x]['sifra'] = $fetchi['sifra'];
			$this->datacontainer['racuni'][$x]['naziv'] = $fetchi['naziv'];
			$this->datacontainer['racuni'][$x]['atributi'] = $fetchi['atributi'];
			$this->datacontainer['racuni'][$x]['kolicina'] = $fetchi['kom'];
			$this->datacontainer['racuni'][$x]['enota'] = $fetchi['em'];
			$this->datacontainer['racuni'][$x]['cena'] = $fetchi['cena'];
			$this->datacontainer['racuni'][$x]['ddv'] = $fetchi['ddv'];
			$this->datacontainer['racuni'][$x]['popust'] = $fetchi['popust'];
			$this->datacontainer['racuni'][$x]['vrednost'] = $fetchi['cena'] * $fetchi['kom'];
		}
		//OVCA2
		
		$bonusbddv = $fetch['bonus'] / 1.2;
		$bonusddv = $fetch['bonus'] - $bonusbddv;
		
		$this->datacontainer['skupaj'] = array();
		$this->datacontainer['skupaj']['skupaj'] = $fetch['vrednost'];
		$this->datacontainer['skupaj']['popust'] = $fetch['popust'];
		$this->datacontainer['skupaj']['bonus'] = $fetch['bonus'];
		
		$rcpopust = $fetch['vrednost'] - $fetch['popust'] - $bonusbddv;
		if($rcpopust < 0){
			$this->datacontainer['skupaj']['rcpopust'] = 0;
		}else{
			$this->datacontainer['skupaj']['rcpopust'] = $rcpopust;
		}
		$this->datacontainer['skupaj']['ddv'] = $fetch['ddv'];
		$this->datacontainer['skupaj']['rcskupaj'] = round($fetch['skupaj'],2);
		$this->datacontainer['skupaj']['rcskupajeur'] = round($fetch['skupaj']*239.64,2);
	}

}

if($_GET['id']){
//Demo
$pay = new Billing();
$id = $pay->Predracun_Izdaj($_GET['id']);
//mail("dc@zabec.net","TEST",$id);
//print $pay->Predracun_Shrani($id);
$pay->Predracun_Izpisi($id);
}

?>