<?php
	
	
	function all_print_receiving($a_param = array(), $id = null){
		
		$g_path = get_item('select value from settings where name = "putanja.abs" ');
			
		if(!defined('FPDF_FONTPATH')){
			define('FPDF_FONTPATH', 'font/');
		}
		
		//require_once($g_path.'app/pdf/fpdf.php');
		require_once($g_path.'app/libs/fpdf/fpdf.php');
		require_once($g_path.'app/libs/fpdf/fpdi.php');
		
		$doc =& new FPDI('L', 'mm', 'A4');
		
		$doc->AddFont("ArialCE", "B", "6488f5d4657b269b9f5770508e80e299_arialbd.php");
		$doc->AddFont("ArialCE", "", "332cc83ebee36613c19564e6e776c81b_arial.php");
		$doc->AddFont("Helvetica", "B", "6488f5d4657b269b9f5770508e80e299_arialbd.php");
		$doc->AddFont("Helvetica", "", "332cc83ebee36613c19564e6e776c81b_arial.php");
		$doc->AddFont("Courier", "", "cour.php");
		
		//$doc = new Document;
		
		$l_tip_prog = get_item('select value from settings where name = "tipprog" ');
		
		if($l_tip_prog == 'u'){
			$doc->AddPage('P');
		}else{
			$doc->AddPage('L');
		}
		
		$doc->SetFont('Helvetica','B',12);
		$doc->Cell(100);
		$doc->Cell(0,5,_convert($a_param['restName']["Setting"]["value"]),0,2);
		$doc->SetFont('Helvetica','',8);
		
		$doc->Cell(0,5,_convert($a_param['restpost']["Setting"]["value"].' '.$a_param['restComm']["Setting"]["value"].','.
			$a_param['restStreet']["Setting"]["value"].' '.$a_param['restStrNum']["Setting"]["value"].$a_param['restStrNumMark']["Setting"]["value"]),0,2);
		
		//$restPlaceid=;
		$doc->SetFont('Helvetica','',8);
		$doc->Cell(0,5,_convert($a_param['restPlaceid']["Setting"]["value"]),0,0);
		
		$rec = $a_param['rec'];
		
		$doc->Ln();
		$doc->SetFont('Helvetica','B',12);  
		$doc->Cell(80,8,_convert($rec["Supplier"]["name"]),'LTR',0);
	    $doc->Cell(20);
		$doc->SetFont('Helvetica','B',10);
    //$doc->Cell(80,8, 'Primka-Kalkulacija *'.$rec["Receiving"]["number"],'LTRB',0,'C');
    $doc->Cell(80,8, 'Primka-Kalkulacija '.$rec["Receiving"]["broj_primka"],'LTRB',0,'C');

		$dt = date('d.m.Y', strtotime($rec["Receiving"]["created"]));

		$doc->Ln();
		$doc->SetFont('Helvetica','B',8);  
		$doc->Cell(80,5,'  ','LR',0);
	    $doc->Cell(20);
	    $doc->SetFont('Courier','',8);
		$doc->Cell(80,5,"Broj dokumenta:    "._convert($rec["Receiving"]["number"]),'LR',0);
		
		$doc->Ln();
		$doc->SetFont('Helvetica','B',8);  
		$doc->Cell(80,5,_convert($rec["Supplier"]["address"].' '.$rec["Supplier"]["city"]),'LR',0);
	    $doc->Cell(20);
	    $doc->SetFont('Courier','',8);
		$doc->Cell(80,5,"Datum dokumenta:   ".$dt,'LBR',0);
		$doc->Ln();

		/*
		$doc->SetFont('Helvetica','B',8);  
		$doc->Cell(80,5,'  ','LR',0);
	    $doc->Cell(20);
		$doc->SetFont('Courier','',8);
		$doc->Cell(80,5,"Racun:             ".'','LBR',0);
		$doc->Ln();
		*/
		
		$doc->SetFont('Helvetica','',8);  
		$doc->Cell(80,5,"OIB: ".$rec["Supplier"]["oib"],'LRB',0,'R');
		$doc->Ln(10);
	    
		$doc->SetFont('Helvetica','B',7);
		
		// Column headings
		
		// Colors, line width and bold font
		$doc->SetFillColor(255,255,255); // slova  u headeru
		
		$l_pdv = get_item('select value from settings where name = "duty.free" ');
		if($l_pdv == 'no'){
			$l_pdv = 'false';
		}
		
		if($l_tip_prog == 'u'){
			if($l_pdv == 'false'){
				$header = array('Red.br','Šifra','Naziv artikla/usluge', 'Kol', 'Nab. Cj.', 'Nab.Iznos', 'Rabat%', 'Iz.Rabat', 'Fakt.Cj.', 'Fakt. Vrijed.', 'MPC' ,'Uk cijena');
				$w = array(7,19,37,9,14,14,9,14,14,17,17,19);
			}else{
				$header = array('Red.br','Šifra','Naziv artikla/usluge', 'Kol', 'Nab. Cj.', 'Nab.Iznos', 'Rabat%', 'Iz.Rabat', 'Fakt.Cj.', 'Fakt. Vrijed.', 'MPC' ,'Uk cijena');
				$w = array(7,19,37,9,14,14,9,14,14,17,17,19);
			}
		}else{
			if($l_pdv == 'false'){
				$header = array('Red.br','Šifra','Naziv artikla/usluge', 'Kol', 'Nab. Cj.', 'Nab.Iznos', 'Rabat%', 'Iz.Rabat', 'Fakt.Cj.', 'Fakt. Vrijed.', 'Marža%', 'Iz. MpMarža', 'MPC' ,'Uk cijena');
				$w = array(10,20,50,10,15,15,10,15,15,18,18,18,18,20);
			}else{
				$header = array('Red.br','Šifra','Naziv artikla/usluge', 'Kol', 'Nab. Cj.', 'Nab.Iznos', 'Rabat%', 'Iz.Rabat', 'Fakt.Cj.', 'Fakt. Vrijed.', 'Marža%', 'Iz. MpMarža', 'PDV%', 'Iz.PDV', 'MPC' ,'Uk cijena');
				$w = array(10,20,50,10,15,15,10,15,15,18,18,18,10,18,18,20);
			}
		}

		
		for($i=0;$i<count($header);$i++)
			$doc->Cell($w[$i],7,_convert($header[$i]),0,0,'C',true);
		$doc->Ln();
	
		$fill = false;
		
		if($id == 0){
			// resetiranje, odnosno, kad licenca nije valjana, onda se u izvjesce stavi samo zaglavlje, a kad dodju pravi podaci, onda se ti pravi podaci ne ispisuju
			$a_param['items'] = array();
		}
		
		
		$doc->SetFont('Helvetica','',8);
		$priceBuy = 0;
		$priceSell= 0;
		$fakt =0;
		$price = 0;
		$marza = 0;
		$pdv = 0;
		$discount = 0;
		$redbr = 1;
		foreach($a_param['items'] as $item){
			/*
			$iid = $item["Article"]["tax_group_id"];
			$taxGroup=$this->Receiving->ReceivingItem->Article->TaxGroup->find('first', array(
			'conditions' => array('TaxGroup.id' => $iid)
			));
			*/
			
			$iid = $item["Article"]["tax_group_id"];
			$taxGroup = array();
			$taxGroup["TaxGroup"] = get_data('select id, name, tax_1, tax_2 from tax_groups where id = '.$iid, 0);
			
			// mora uzeti vrijednosti koje se pamte unutar receiving_items, a ne unutar articles... jer se vrijednost u articles moze mijenjati, a ovdje treba uvijek ista vrijednost
			$taxGroup["TaxGroup"]["tax_1"] = $item["ReceivingItem"]["tax_1"];
			$taxGroup["TaxGroup"]["tax_2"] = $item["ReceivingItem"]["tax_2"];
			
			
			
			$doc->Cell($w[0],6,$redbr,0,0,'C',$fill);
			$doc->Cell($w[1],6,_convert($item["Article"]["barcode"]),0,0,'L',$fill);
			
			$posY = $doc->GetY();
			$posX = $doc->GetX();
			$nl = 0;
			if(strlen($item["Article"]["name"])>40)
			{
				$temptext = str_split($item["Article"]["name"],40);
				foreach($temptext as $newline) {
					$doc->Cell($w[2],3,_convert($newline),0,2,'L');
					$nl += 1;
				}
				$posX += $w[2];
				$doc->SetY($posY);
				$posY +=  $nl*3; // pozicija sledeceg reda
			}
			else
			{
				$doc->Cell($w[2],6,_convert($item["Article"]["name"]),0,0,'L',$fill);	
				$posX = $doc->GetX();
				$posY +=  6;
			}
			
			$doc->SetX($posX);
			
		
			$doc->Cell($w[3],6,number_format($item["ReceivingItem"]["amount"],2, '.', ' '),0,0,'R',$fill);
			$doc->Cell($w[4],6,number_format($item["ReceivingItem"]["price_buy"],2, '.', ' '),0,0,'R',$fill);
			$doc->Cell($w[5],6,number_format($item["ReceivingItem"]["price_buy"]*$item["ReceivingItem"]["amount"],2, '.', ' '),0,0,'R',$fill);
			$doc->Cell($w[6],6,number_format($item["ReceivingItem"]["discount"]),0,0,'R',$fill); 
			$doc->Cell($w[7],6,number_format(($item["ReceivingItem"]["discount"])*$item["ReceivingItem"]["price_buy"]*$item["ReceivingItem"]["amount"]/100,2, '.', ' '),0,0,'R',$fill);
			$doc->Cell($w[8],6,number_format($item["ReceivingItem"]["price_buy"]*(1- ($item["ReceivingItem"]["discount"]/100)),2, '.', ' '),0,0,'R',$fill);
			$doc->Cell($w[9],6,number_format($item["ReceivingItem"]["price_buy"]*(1- ($item["ReceivingItem"]["discount"]/100))*$item["ReceivingItem"]["amount"],2, '.', ' '),0,0,'R',$fill);
			
			if($l_tip_prog == 'u'){
				if($l_pdv == 'false'){
					$doc->Cell($w[10],6,number_format($item["ReceivingItem"]["price_sell"],2, '.', ' '),0,0,'R',$fill);
					$doc->Cell($w[11],6,number_format($item["ReceivingItem"]["amount"]*$item["ReceivingItem"]["price_sell"],2, '.', ' '),0,0,'R',$fill);
				}else{
					$doc->Cell($w[10],6,number_format($item["ReceivingItem"]["price_sell"],2, '.', ' '),0,0,'R',$fill);
					$doc->Cell($w[11],6,number_format($item["ReceivingItem"]["amount"]*$item["ReceivingItem"]["price_sell"],2, '.', ' '),0,0,'R',$fill);
				}
			}else{
				if($l_pdv == 'false'){
					$doc->Cell($w[10],6,number_format(($item["ReceivingItem"]["price_sell"]/(1+0/100)/($item["ReceivingItem"]["price_buy"]*(1- ($item["ReceivingItem"]["discount"]/100))) -1)*100,2, '.', ' '),0,0,'R',$fill);
					$doc->Cell($w[11],6,number_format(($item["ReceivingItem"]["price_sell"]/(1+0/100)- ($item["ReceivingItem"]["price_buy"]*(1- ($item["ReceivingItem"]["discount"]/100)))) *$item["ReceivingItem"]["amount"],2, '.', ' '),0,0,'R',$fill);
					$doc->Cell($w[12],6,number_format($item["ReceivingItem"]["price_sell"],2, '.', ' '),0,0,'R',$fill);
					$doc->Cell($w[13],6,number_format($item["ReceivingItem"]["amount"]*$item["ReceivingItem"]["price_sell"],2, '.', ' '),0,0,'R',$fill);
				}else{
					$doc->Cell($w[10],6,number_format(($item["ReceivingItem"]["price_sell"]/(1+$taxGroup["TaxGroup"]["tax_1"]/100)/($item["ReceivingItem"]["price_buy"]*(1- ($item["ReceivingItem"]["discount"]/100))) -1)*100,2, '.', ' '),0,0,'R',$fill);
					$doc->Cell($w[11],6,number_format(($item["ReceivingItem"]["price_sell"]/(1+$taxGroup["TaxGroup"]["tax_1"]/100)- ($item["ReceivingItem"]["price_buy"]*(1- ($item["ReceivingItem"]["discount"]/100)))) *$item["ReceivingItem"]["amount"],2, '.', ' '),0,0,'R',$fill);
					$doc->Cell($w[12],6,number_format($taxGroup["TaxGroup"]["tax_1"]),0,0,'R',$fill);
					$doc->Cell($w[13],6,number_format($item["ReceivingItem"]["price_sell"]/(1 + $taxGroup["TaxGroup"]["tax_1"]/100)* $taxGroup["TaxGroup"]["tax_1"] /100*$item["ReceivingItem"]["amount"] ,2, '.', ' '),0,0,'R',$fill);
					$doc->Cell($w[14],6,number_format($item["ReceivingItem"]["price_sell"],2, '.', ' '),0,0,'R',$fill);
					$doc->Cell($w[15],6,number_format($item["ReceivingItem"]["amount"]*$item["ReceivingItem"]["price_sell"],2, '.', ' '),0,0,'R',$fill);
				}
			}
			
			
			$doc->Ln();
			
			$priceBuy = $priceBuy + $item["ReceivingItem"]["price_buy"]*$item["ReceivingItem"]["amount"];
			$discount = $discount + (($item["ReceivingItem"]["discount"])*$item["ReceivingItem"]["price_buy"]*$item["ReceivingItem"]["amount"]/100);
			$fakt = $fakt + $item["ReceivingItem"]["price_buy"]*(1- ($item["ReceivingItem"]["discount"]/100))*$item["ReceivingItem"]["amount"];
			if($l_pdv == 'false'){
				$marza = $marza + ($item["ReceivingItem"]["price_sell"]/(1+0/100)- ($item["ReceivingItem"]["price_buy"]*(1- ($item["ReceivingItem"]["discount"]/100)))) *$item["ReceivingItem"]["amount"];
				$pdv = $pdv + 0;
			}else{
				$marza = $marza + ($item["ReceivingItem"]["price_sell"]/(1+$taxGroup["TaxGroup"]["tax_1"]/100)- ($item["ReceivingItem"]["price_buy"]*(1- ($item["ReceivingItem"]["discount"]/100)))) *$item["ReceivingItem"]["amount"];
				$pdv = $pdv + $item["ReceivingItem"]["price_sell"]/(1 + $taxGroup["TaxGroup"]["tax_1"]/100)* $taxGroup["TaxGroup"]["tax_1"] /100*$item["ReceivingItem"]["amount"];
			}
			$price = $price + $item["ReceivingItem"]["price_sell"]*$item["ReceivingItem"]["amount"];
			
			
			$redbr = $redbr+1;
			$doc->SetY($posY);
		}
		
		// Closing line
		$doc->Cell(array_sum($w),0,'','T');
		$doc->Ln(1);
		$doc->SetFont('Helvetica','B',8);
		$doc->Cell($w[0]+$w[1]+$w[2]+$w[3]+$w[4],6,'',0,0,'L',$fill);
		$doc->Cell($w[5],6,number_format($priceBuy,2, '.', ' '),0,0,'R',$fill);
		$doc->Cell($w[6],6,'',0,0,'R',$fill);
		$doc->Cell($w[7],6,number_format($discount,2, '.', ' '),0,0,'R',$fill);
		$doc->Cell($w[8],6,'',0,0,'R',$fill);
		$doc->Cell($w[9],6,number_format($fakt,2, '.', ' '),0,0,'R',$fill);
		
		if($l_tip_prog == 'u'){
			if($l_pdv == 'false'){
				$doc->Cell($w[10],6,'',0,0,'R',$fill);
				$doc->Cell($w[11],6,number_format($price,2, '.', ' '),0,0,'R',$fill);
			}else{
				$doc->Cell($w[10],6,'',0,0,'R',$fill);
				$doc->Cell($w[11],6,number_format($price,2, '.', ' '),0,0,'R',$fill);
			}
		}else{
			$doc->Cell($w[10],6,'',0,0,'R',$fill);
			$doc->Cell($w[11],6,number_format($marza,2, '.', ' '),0,0,'R',$fill);
			
			if($l_pdv == 'false'){
				$doc->Cell($w[12],6,'',0,0,'R',$fill);
				$doc->Cell($w[13],6,number_format($price,2, '.', ' '),0,0,'R',$fill);
			}else{
				$doc->Cell($w[12],6,'',0,0,'R',$fill);
				$doc->Cell($w[13],6,number_format($pdv,2, '.', ' '),0,0,'R',$fill);
				$doc->Cell($w[14],6,'',0,0,'R',$fill);
				$doc->Cell($w[15],6,number_format($price,2, '.', ' '),0,0,'R',$fill);
			}
		}
		$doc->SetFont('Helvetica','',8);
		
		
		$doc->Ln();
    $doc->Cell(array_sum($w),0,'','T');
		$doc->Ln(6);
		
		
		
		
		$la_sum = array(0, 0);
		$la_sum_osnovica = array(0, 0);
		
		/*
		$l_sql = 'select tax_groups.tax_1, '.
			' sum(receiving_items.amount * receiving_items.price_buy) as s1, '.
			' sum(receiving_items.amount * receiving_items.price_buy * (1 - receiving_items.discount / 100)) as s1b, '.
			' sum(receiving_items.amount * receiving_items.price_sell) as s2, '.
			' sum(receiving_items.amount * receiving_items.feedback_compensation) as s3 '.
			' from receivings, receiving_items, articles, tax_groups '.
			' where receivings.id = receiving_items.receiving_id '.
			' and receiving_items.article_id = articles.id '.
			' and articles.tax_group_id = tax_groups.id '.
			' and receivings.id = '.$id.
			' group by 1 '.
			' order by 1';
		*/
		
		$l_sql = 'select receiving_items.tax_1, '.
			' sum(receiving_items.amount * receiving_items.price_buy) as s1, '.
			' sum(receiving_items.amount * receiving_items.price_buy * (1 - receiving_items.discount / 100)) as s1b, '.
			' sum(receiving_items.amount * receiving_items.price_sell) as s2, '.
			' sum(receiving_items.amount * receiving_items.feedback_compensation) as s3 '.
			' from receivings, receiving_items '.
			' where receivings.id = receiving_items.receiving_id '.
			' and receivings.id = '.$id.
			' group by 1 '.
			' order by 1';
		
		$x = get_data($l_sql, 1);
		
		$l_povratna_naknada = 0;
		
		
		
		
		
		
		if($l_pdv == 'false'){
			
			
			
			for($i = 0; isset($x['tax_1']) && $i < sizeof($x['tax_1']); $i++){
				
				$la_sum_osnovica[0] += $x['s1b'][$i];
				$la_sum_osnovica[1] += $x['s2'][$i];
				
				$l_povratna_naknada += $x['s3'][$i];
			}
			
			
			
		}else{
			
			$y_pos = $doc->GetY();
			
			if($l_tip_prog == 'u'){
				if($y_pos > 215){
					// add a page
					$doc->AddPage('P');
					$y_pos = 10;
				}
			}else{
				if($y_pos > 160){
					// add a page
					$doc->AddPage('L');
					$y_pos = 10;
				}
			}
			
			
			if($l_tip_prog == 'u'){
				$doc->SetXY(10, $y_pos);
				$doc->MultiCell(210, 4, _convert('R E K A P I T U L A C I J A:'), 0, "L");
				$doc->Ln(2);
				
				$y_pos = $doc->GetY();
				$doc->MultiCell(60, 4, _convert('Nabavna vrijednost:'), 0, "L");
				$doc->Ln(1);
				
				$y_pos = $doc->GetY();
				$doc->SetXY(10, $y_pos);
				$doc->MultiCell(20, 4, _convert('PDV'), 0, "L");
				$doc->SetXY(20, $y_pos);
				$doc->MultiCell(19, 4, _convert('OSNOVICA'), 0, "R");
				$doc->SetXY(40, $y_pos);
				$doc->MultiCell(15, 4, _convert('IZNOS'), 0, "R");
				$doc->SetXY(60, $y_pos);
				$doc->MultiCell(15, 4, _convert('NC'), 0, "R");
				$doc->Ln(1);
			}else{
				$doc->SetXY(10, $y_pos);
				$doc->MultiCell(210, 4, _convert('R E K A P I T U L A C I J A:'), 0, "L");
				$doc->Ln(2);
				
				$y_pos = $doc->GetY();
				$doc->MultiCell(60, 4, _convert('Nabavna vrijednost:'), 0, "L");
				$doc->SetXY(110, $y_pos);
				$doc->MultiCell(60, 4, _convert('Prodajna vrijednost:'), 0, "L");
				$doc->Ln(1);
				
				$y_pos = $doc->GetY();
				$doc->SetXY(10, $y_pos);
				$doc->MultiCell(20, 4, _convert('PDV'), 0, "L");
				$doc->SetXY(20, $y_pos);
				$doc->MultiCell(19, 4, _convert('OSNOVICA'), 0, "R");
				$doc->SetXY(40, $y_pos);
				$doc->MultiCell(15, 4, _convert('IZNOS'), 0, "R");
				$doc->SetXY(60, $y_pos);
				$doc->MultiCell(15, 4, _convert('NC'), 0, "R");
				
				$doc->SetXY(110, $y_pos);
				$doc->MultiCell(20, 4, _convert('PDV'), 0, "L");
				$doc->SetXY(120, $y_pos);
				$doc->MultiCell(19, 4, _convert('OSNOVICA'), 0, "R");
				$doc->SetXY(140, $y_pos);
				$doc->MultiCell(15, 4, _convert('IZNOS'), 0, "R");
				$doc->SetXY(160, $y_pos);
				$doc->MultiCell(15, 4, _convert('PC'), 0, "R");
				
				$doc->Ln(1);
			}
			
			
			
			
			for($i = 0; isset($x['tax_1']) && $i < sizeof($x['tax_1']); $i++){
				$y_pos = $doc->GetY();
				$doc->SetXY(10, $y_pos);
				$doc->MultiCell(20, 4, _convert(round($x['tax_1'][$i]).' %'), 0, "L");
				$doc->SetXY(20, $y_pos);
				$doc->MultiCell(19, 4, _convert(sprintf('%01.2f', $x['s1b'][$i])), 0, "R");
				$doc->SetXY(40, $y_pos);
				$doc->MultiCell(15, 4, _convert(sprintf('%01.2f', $x['s1b'][$i] * $x['tax_1'][$i] / 100)), 0, "R");
				$doc->SetXY(60, $y_pos);
				$doc->MultiCell(15, 4, _convert(sprintf('%01.2f', $x['s1b'][$i] * (1 + $x['tax_1'][$i] / 100))), 0, "R");
				
				if($l_tip_prog == 'u'){
				}else{
					$doc->SetXY(110, $y_pos);
					$doc->MultiCell(20, 4, _convert(round($x['tax_1'][$i]).' %'), 0, "L");
					$doc->SetXY(120, $y_pos);
					$doc->MultiCell(19, 4, _convert(sprintf('%01.2f', $x['s2'][$i] / (1 + $x['tax_1'][$i] / 100))), 0, "R");
					$doc->SetXY(140, $y_pos);
					$doc->MultiCell(15, 4, _convert(sprintf('%01.2f', $x['s2'][$i] - ($x['s2'][$i] / (1 + $x['tax_1'][$i] / 100)))), 0, "R");
					$doc->SetXY(160, $y_pos);
					$doc->MultiCell(15, 4, _convert(sprintf('%01.2f', $x['s2'][$i])), 0, "R");
				}
				
				$doc->Ln(1);
				
				$la_sum[0] += $x['s1b'][$i] * $x['tax_1'][$i];
				$la_sum[1] += $x['s2'][$i] - ($x['s2'][$i] / (1 + $x['tax_1'][$i] / 100));
				
				$la_sum_osnovica[0] += $x['s1b'][$i];
				
				$l_povratna_naknada += $x['s3'][$i];
			}
			
			$y_pos = $doc->GetY();
			$doc->SetXY(10, $y_pos);
			$doc->MultiCell(30, 4, _convert('Sveukupno PDV:'), 0, "L");
			$doc->SetXY(40, $y_pos);
			$doc->MultiCell(15, 4, _convert(sprintf('%01.2f', $la_sum[0] / 100)), 0, "R");
			
			if($l_tip_prog == 'u'){
			}else{
				$doc->SetXY(110, $y_pos);
				$doc->MultiCell(30, 4, _convert('Sveukupno PDV:'), 0, "L");
				$doc->SetXY(140, $y_pos);
				$doc->MultiCell(15, 4, _convert(sprintf('%01.2f', $la_sum[1])), 0, "R");
			}
		}
		
		
		$l_sql = 'select trosak1, trosak2, trosak3, uk_trosak, uk_nabavna_cijena, koef_troska '.
			' from receivings '.
			' where receivings.id = '.$id;
		$tr_data = get_data($l_sql);
		
		$doc->Ln(5);
		$y_pos = $doc->GetY();
		$doc->SetXY(10, $y_pos);
		$doc->MultiCell(260, 4, _convert('Trošak 1: '.$tr_data['trosak1'].' EUR       Trošak 2: '.$tr_data['trosak2'].' EUR       '.
			'Trošak 3: '.$tr_data['trosak3'].' EUR       UK Trošak: '.$tr_data['uk_trosak'].' EUR       '.
			'Nabavna cijena: '.$tr_data['uk_nabavna_cijena'].' EUR       Koef. Troška: '.$tr_data['koef_troska'].''), 0, "L");
		$doc->Ln(1);
		
		
		if($l_pdv == 'false'){
		}else{
			$doc->Ln(5);
		}
		
		$doc->SetFont('Helvetica','B',8);
		
		if($l_povratna_naknada){
			$y_pos = $doc->GetY();
			$doc->SetXY(10, $y_pos);
			$doc->MultiCell(210, 4, _convert('Povratna naknada: '.sprintf('%01.2f', $l_povratna_naknada)), 0, "L");
		}
		
		$y_pos = $doc->GetY();
		$doc->SetXY(10, $y_pos);
		$doc->MultiCell(210, 4, _convert('SVEUKUPNO VRIJEDNOST PRIMKE: '.sprintf('%01.2f', $la_sum_osnovica[0] + $la_sum[0] / 100 + $l_povratna_naknada)), 0, "L");
		if($l_pdv == 'false'){
			if($l_tip_prog == 'u'){
				$doc->SetXY(164, $y_pos - 5);
			}else{
				$doc->SetXY(221, $y_pos - 5);
			}
			$doc->MultiCell(110, 4, _convert('SVEUKUPNO PC kn: '.sprintf('%01.2f', $la_sum_osnovica[1])), 0, "L");
		}
		$doc->SetFont('Helvetica','',8);
		
		
		
		
		
		
		if($l_tip_prog == 'u'){
			$doc->Cell(130);
		}else{
			$doc->Cell(220);
		}
		$doc->Cell(50,20,"Obradio",'B',0,'C');
		$doc->Ln();
	
		
		
		
		if($a_param['destination'] == 'screen'){
			
			$doc->output();
			die();
			
		}else{
			
			$l_name = 'primka.pdf';
			$l_dest = $g_path.'app/webroot/res/files/'.$l_name;
			
			$doc->output($l_dest, 'F');
			
			return array('path' => $g_path.'app/webroot/res/files/', 'name' => $l_name);
		}
		
	}
	
	
	function all_print_writeOff($a_param = array(), $id = null){
		
		$g_path = get_item('select value from settings where name = "putanja.abs" ');
			
		if(!defined('FPDF_FONTPATH')){
			define('FPDF_FONTPATH', 'font/');
		}
		
		//require_once($g_path.'app/pdf/fpdf.php');
		require_once($g_path.'app/libs/fpdf/fpdf.php');
		require_once($g_path.'app/libs/fpdf/fpdi.php');
		
		$doc =& new FPDI('L', 'mm', 'A4');
		
		$doc->AddFont("ArialCE", "B", "6488f5d4657b269b9f5770508e80e299_arialbd.php");
		$doc->AddFont("ArialCE", "", "332cc83ebee36613c19564e6e776c81b_arial.php");
		
		//$doc = new Document;
		
		$doc->AddPage('L');
	    
		$doc->SetFont('Helvetica','B',12);
		$doc->Cell(100);
		$doc->Cell(0,5,_convert($a_param['restName']["Setting"]["value"]),0,2);
		$doc->SetFont('Helvetica','',8);
		
		$doc->Cell(0,5,_convert($a_param['restpost']["Setting"]["value"].' '.$a_param['restComm']["Setting"]["value"].','.
			$a_param['restStreet']["Setting"]["value"].' '.$a_param['restStrNum']["Setting"]["value"].$a_param['restStrNumMark']["Setting"]["value"]),0,2);
		
		$doc->SetFont('Helvetica','',8);
		$doc->Cell(0,5,_convert($a_param['restPlaceid']["Setting"]["value"]),0,0);
		
		$rec = $a_param['rec'];
		
		$doc->Ln();
		$doc->SetFont('Helvetica','B',12);  
		$doc->Cell(80,8,$rec["Supplier"]["name"],'LTR',0);
	    $doc->Cell(20);
		$doc->SetFont('Helvetica','B',10);
        $doc->Cell(80,8, 'Otpis '.$rec["Receiving"]["broj_otpis"],'LTRB',0,'C');

		$dt = date('d.m.Y', strtotime($rec["Receiving"]["created"]));

		$doc->Ln();
		$doc->SetFont('Helvetica','B',8);  
		$doc->Cell(80,5,'  ','LR',0);
	    $doc->Cell(20);
	    $doc->SetFont('Courier','',8);
		$doc->Cell(80,5,"Broj dokumenta:    ".$rec["Receiving"]["number"],'LR',0);
		
		$doc->Ln();
		$doc->SetFont('Helvetica','B',8);  
		$doc->Cell(80,5,$rec["Supplier"]["address"].' '.$rec["Supplier"]["city"],'LR',0);
	    $doc->Cell(20);
	    $doc->SetFont('Courier','',8);
		$doc->Cell(80,5,"Datum dokumenta:   ".$dt,'LBR',0);
		$doc->Ln();

		/*
		$doc->SetFont('Helvetica','B',8);  
		$doc->Cell(80,5,'  ','LR',0);
	    $doc->Cell(20);
		$doc->SetFont('Courier','',8);
		$doc->Cell(80,5,"Racun:             ".'','LBR',0);
		$doc->Ln();
		*/
		
		$doc->SetFont('Helvetica','',8);  
		$doc->Cell(80,5,"OIB: ".$rec["Supplier"]["oib"],'LRB',0,'R');
		$doc->Ln(10);
		
		
		//print_r($rec);
		//exit;
	    
		$doc->SetFont('Helvetica','B',8);
		
		// Column headings
		
		// Colors, line width and bold font
		 $doc->SetFillColor(255,255,255); // slova  u headeru
	

		$l_pdv = get_item('select value from settings where name = "duty.free" ');
		if($l_pdv == 'no'){
			$l_pdv = 'false';
		}
		
		if($l_pdv == 'false'){
			$header = array('Red.br','Sifra','Naziv artikla/usluge', 'Kol', 'Nab. Cj.', 'Nab.Vrijed.', 'Rabat%', 'Iz.Rabat', 'Fakt.Cj.', 'Fakt. Vrijed.', 'MPC' ,'Uk cijena');
			$w = array(10,20,50,10,15,15,10,15,15,18,18,18,18,20);
		}else{
			$header = array('Red.br','Sifra','Naziv artikla/usluge', 'Kol', 'Nab. Cj.', 'Nab.Vrijed.', 'Rabat%', 'Iz.Rabat', 'Fakt.Cj.', 'Fakt. Vrijed.', 'PDV%', 'Iz.PDV', 'MPC' ,'Uk cijena');
			$w = array(10,20,50,10,15,15,10,15,15,18,18,18,10,18,18,20);
		}
		
		for($i=0;$i<count($header);$i++)
			$doc->Cell($w[$i],7,$header[$i],0,0,'C',true);
		$doc->Ln();
	
		$fill = false;
		
		if($id == 0){
			// resetiranje, odnosno, kad licenca nije valjana, onda se u izvjesce stavi samo zaglavlje, a kad dodju pravi podaci, onda se ti pravi podaci ne ispisuju
			$a_param['items'] = array();
		}
		
		
		$doc->SetFont('Helvetica','',8);
		$priceBuy = 0;
		$priceSell= 0;
		$fakt =0;
		$price = 0;
		$marza = 0;
		$pdv = 0;
		$discount = 0;
		$redbr = 1;
		foreach($a_param['items'] as $item){
			
			$iid = $item["Article"]["tax_group_id"];
			$taxGroup = array();
			$taxGroup["TaxGroup"] = get_data('select id, name, tax_1, tax_2 from tax_groups where id = '.$iid, 0);
			
			// zasad ove dvije linije ispod zanemari
			// mora uzeti vrijednosti koje se pamte unutar receiving_items, a ne unutar articles... jer se vrijednost u articles moze mijenjati, a ovdje treba uvijek ista vrijednost
			//$taxGroup["TaxGroup"]["tax_1"] = $item["ReceivingItem"]["tax_1"];
			//$taxGroup["TaxGroup"]["tax_2"] = $item["ReceivingItem"]["tax_2"];
			
			
			
			
			$doc->Cell($w[0],6,$redbr,0,0,'C',$fill);
			$doc->Cell($w[1],6,$item["Article"]["barcode"],0,0,'L',$fill);
			
			$posY = $doc->GetY();
			$posX = $doc->GetX();
			$nl = 0;
			if(strlen($item["Article"]["name"])>40)
			{
				$temptext = str_split($item["Article"]["name"],40);
				foreach($temptext as $newline) {
					$doc->Cell($w[2],3,$newline,0,2,'L');
					$nl += 1;
				}
				$posX += $w[2];
				$doc->SetY($posY);
				$posY +=  $nl*3; // pozicija sledeceg reda
			}
			else
			{
				$doc->Cell($w[2],6,$item["Article"]["name"],0,0,'L',$fill);	
				$posX = $doc->GetX();
				$posY +=  6;
			}
			
			$doc->SetX($posX);
			
		
			$doc->Cell($w[3],6,number_format($item["ReceivingItem"]["amount"],2, '.', ' '),0,0,'R',$fill);
			$doc->Cell($w[4],6,number_format($item["ReceivingItem"]["price_buy"],2, '.', ' '),0,0,'R',$fill);
			$doc->Cell($w[5],6,number_format($item["ReceivingItem"]["price_buy"]*$item["ReceivingItem"]["amount"],2, '.', ' '),0,0,'R',$fill);
			$doc->Cell($w[6],6,number_format($item["ReceivingItem"]["discount"]),0,0,'R',$fill); 
			$doc->Cell($w[7],6,number_format(($item["ReceivingItem"]["discount"])*$item["ReceivingItem"]["price_buy"]*$item["ReceivingItem"]["amount"]/100,2, '.', ' '),0,0,'R',$fill);
			$doc->Cell($w[8],6,number_format($item["ReceivingItem"]["price_buy"]*(1- ($item["ReceivingItem"]["discount"]/100))),0,0,'R',$fill);
			$doc->Cell($w[9],6,number_format($item["ReceivingItem"]["price_buy"]*(1- ($item["ReceivingItem"]["discount"]/100))*$item["ReceivingItem"]["amount"],2, '.', ' '),0,0,'R',$fill);
		//print_r($item);
		//exit;
			//$doc->Cell($w[10],6,number_format(($item["ReceivingItem"]["price_sell"]/(1+$taxGroup["TaxGroup"]["tax_1"]/100)/($item["ReceivingItem"]["price_buy"]*(1- ($item["ReceivingItem"]["discount"]/100))) -1)*100,2, '.', ' '),0,0,'R',$fill);
			//$doc->Cell($w[11],6,number_format(($item["ReceivingItem"]["price_sell"]/(1+$taxGroup["TaxGroup"]["tax_1"]/100)- ($item["ReceivingItem"]["price_buy"]*(1- ($item["ReceivingItem"]["discount"]/100)))) *$item["ReceivingItem"]["amount"],2, '.', ' '),0,0,'R',$fill);
			//$doc->Cell($w[10],6,'-     ',0,0,'R',$fill);
			//$doc->Cell($w[10],6,'-     ',0,0,'R',$fill);
			
			
			if($l_pdv == 'false'){
				$doc->Cell($w[12],6,number_format($item["ReceivingItem"]["price_sell"],2, '.', ' '),0,0,'R',$fill);
				$doc->Cell($w[13],6,number_format($item["ReceivingItem"]["amount"]*$item["ReceivingItem"]["price_sell"],2, '.', ' '),0,0,'R',$fill);
			}else{
				$doc->Cell($w[12],6,number_format($taxGroup["TaxGroup"]["tax_1"]),0,0,'R',$fill);
				$doc->Cell($w[13],6,number_format($item["ReceivingItem"]["price_sell"]/(1 + $taxGroup["TaxGroup"]["tax_1"]/100)* $taxGroup["TaxGroup"]["tax_1"] /100*$item["ReceivingItem"]["amount"] ,2, '.', ' '),0,0,'R',$fill);
				$doc->Cell($w[14],6,number_format($item["ReceivingItem"]["price_sell"],2, '.', ' '),0,0,'R',$fill);
				$doc->Cell($w[15],6,number_format($item["ReceivingItem"]["amount"]*$item["ReceivingItem"]["price_sell"],2, '.', ' '),0,0,'R',$fill);
			}
			
			$doc->Ln();
			
			$priceBuy = $priceBuy + $item["ReceivingItem"]["price_buy"]*$item["ReceivingItem"]["amount"];
			$discount = $discount + (($item["ReceivingItem"]["discount"])*$item["ReceivingItem"]["price_buy"]*$item["ReceivingItem"]["amount"]/100);
			$fakt = $fakt + $item["ReceivingItem"]["price_buy"]*(1- ($item["ReceivingItem"]["discount"]/100))*$item["ReceivingItem"]["amount"];
			$marza = $marza + ($item["ReceivingItem"]["price_sell"]/(1+$taxGroup["TaxGroup"]["tax_1"]/100)- ($item["ReceivingItem"]["price_buy"]*(1- ($item["ReceivingItem"]["discount"]/100)))) *$item["ReceivingItem"]["amount"];
			$pdv = $pdv + $item["ReceivingItem"]["price_sell"]/(1 + $taxGroup["TaxGroup"]["tax_1"]/100)* $taxGroup["TaxGroup"]["tax_1"] /100*$item["ReceivingItem"]["amount"];
			$price = $price + $item["ReceivingItem"]["price_sell"]*$item["ReceivingItem"]["amount"];
			
			
			$redbr = $redbr+1;
			$doc->SetY($posY);
		}
		
		// Closing line
		$doc->Cell(array_sum($w) - 36,0,'','T');
		$doc->Ln();
		$doc->Cell($w[0]+$w[1]+$w[2]+$w[3]+$w[4],6,'',0,0,'L',$fill);
		$doc->Cell($w[5],6,number_format($priceBuy,2, '.', ' '),0,0,'R',$fill);
		$doc->Cell($w[6],6,'',0,0,'R',$fill);
		$doc->Cell($w[7],6,number_format($discount,2, '.', ' '),0,0,'R',$fill);
		$doc->Cell($w[8],6,'',0,0,'R',$fill);
		$doc->Cell($w[9],6,number_format($fakt,2, '.', ' '),0,0,'R',$fill);
		//$doc->Cell($w[10],6,'',0,0,'R',$fill);
		//$doc->Cell($w[11],6,number_format($marza,2, '.', ' '),0,0,'R',$fill);
		if($l_pdv == 'false'){
			$doc->Cell($w[12],6,'',0,0,'R',$fill);
			$doc->Cell($w[13],6,number_format($price,2, '.', ' '),0,0,'R',$fill);
		}else{
			$doc->Cell($w[12],6,'',0,0,'R',$fill);
			$doc->Cell($w[13],6,number_format($pdv,2, '.', ' '),0,0,'R',$fill);
			$doc->Cell($w[14],6,'',0,0,'R',$fill);
			$doc->Cell($w[15],6,number_format($price,2, '.', ' '),0,0,'R',$fill);
		}
		
		$doc->Ln();
    $doc->Cell(array_sum($w) - 36,0,'','T');
		$doc->Ln(6);
		
		if($l_pdv == 'false'){
		}else{
			$doc->MultiCell(210, 4, _convert('R E K A P I T U L A C I J A:'), 0, "L");
			$doc->Ln(2);
			
			$y_pos = $doc->GetY();
			$doc->MultiCell(60, 4, _convert('Nabavna vrijednost:'), 0, "L");
			$doc->SetXY(70, $y_pos);
			$doc->MultiCell(60, 4, _convert('Prodajna vrijednost:'), 0, "L");
			$doc->Ln(1);
			
			$y_pos = $doc->GetY();
			$doc->SetXY(10, $y_pos);
			$doc->MultiCell(20, 4, _convert('PDV'), 0, "L");
			$doc->SetXY(20, $y_pos);
			$doc->MultiCell(19, 4, _convert('OSNOVICA'), 0, "R");
			$doc->SetXY(40, $y_pos);
			$doc->MultiCell(15, 4, _convert('IZNOS'), 0, "R");
			
			$doc->SetXY(70, $y_pos);
			$doc->MultiCell(20, 4, _convert('PDV'), 0, "L");
			$doc->SetXY(80, $y_pos);
			$doc->MultiCell(19, 4, _convert('OSNOVICA'), 0, "R");
			$doc->SetXY(100, $y_pos);
			$doc->MultiCell(15, 4, _convert('IZNOS'), 0, "R");
			
			$doc->Ln(1);
			
			$la_sum = array(0, 0);
			
			/*
			// nekad kasnije za implementirati
			$l_sql = 'select receiving_items.tax_1, sum(receiving_items.amount * receiving_items.price_buy) as s1, sum(receiving_items.amount * receiving_items.price_sell) as s2 '.
				' from receivings, receiving_items '.
				' where receivings.id = receiving_items.receiving_id '.
				' and receivings.id = '.$id.
				' group by 1 '.
				' order by 1';
			*/
				
				
			$l_sql = 'select tax_groups.tax_1, sum(receiving_items.amount * receiving_items.price_buy) as s1, sum(receiving_items.amount * receiving_items.price_sell) as s2 '.
				' from receivings, receiving_items, articles, tax_groups '.
				' where receivings.id = receiving_items.receiving_id '.
				' and receiving_items.article_id = articles.id '.
				' and articles.tax_group_id = tax_groups.id '.
				' and receivings.id = '.$id.
				' group by 1 '.
				' order by 1';
				
				
				
				
			$x = get_data($l_sql, 1);
			for($i = 0; isset($x['tax_1']) && $i < sizeof($x['tax_1']); $i++){
				$y_pos = $doc->GetY();
				$doc->SetXY(10, $y_pos);
				$doc->MultiCell(20, 4, _convert(round($x['tax_1'][$i]).' %'), 0, "L");
				$doc->SetXY(20, $y_pos);
				$doc->MultiCell(19, 4, _convert(sprintf('%01.2f', $x['s1'][$i])), 0, "R");
				$doc->SetXY(40, $y_pos);
				$doc->MultiCell(15, 4, _convert(sprintf('%01.2f', $x['s1'][$i] * $x['tax_1'][$i] / 100)), 0, "R");
				
				$doc->SetXY(70, $y_pos);
				$doc->MultiCell(20, 4, _convert(round($x['tax_1'][$i]).' %'), 0, "L");
				$doc->SetXY(80, $y_pos);
				$doc->MultiCell(19, 4, _convert(sprintf('%01.2f', $x['s2'][$i] / (1 + $x['tax_1'][$i] / 100))), 0, "R");
				$doc->SetXY(100, $y_pos);
				$doc->MultiCell(15, 4, _convert(sprintf('%01.2f', $x['s2'][$i] - ($x['s2'][$i] / (1 + $x['tax_1'][$i] / 100)))), 0, "R");
				
				$doc->Ln(1);
				
				$la_sum[0] += $x['s1'][$i] * $x['tax_1'][$i];
				$la_sum[1] += $x['s2'][$i] - ($x['s2'][$i] / (1 + $x['tax_1'][$i] / 100));
			}
			
			$y_pos = $doc->GetY();
			$doc->SetXY(10, $y_pos);
			$doc->MultiCell(30, 4, _convert('Sveukupno PDV:'), 0, "L");
			$doc->SetXY(40, $y_pos);
			$doc->MultiCell(15, 4, _convert(sprintf('%01.2f', $la_sum[0] / 100)), 0, "R");
			
			$doc->SetXY(70, $y_pos);
			$doc->MultiCell(30, 4, _convert('Sveukupno PDV:'), 0, "L");
			$doc->SetXY(100, $y_pos);
			$doc->MultiCell(15, 4, _convert(sprintf('%01.2f', $la_sum[1])), 0, "R");
		}
		
		
		
		$l_sql = 'select trosak1, trosak2, trosak3, uk_trosak, uk_nabavna_cijena, koef_troska '.
			' from receivings '.
			' where receivings.id = '.$id;
		$tr_data = get_data($l_sql);
		
		$doc->Ln(5);
		$y_pos = $doc->GetY();
		$doc->SetXY(10, $y_pos);
		$doc->MultiCell(260, 4, _convert('Trosak 1: '.$tr_data['trosak1'].' EUR       Trosak 2: '.$tr_data['trosak2'].' EUR       '.
			'Trosak 3: '.$tr_data['trosak3'].' EUR       UK Trosak: '.$tr_data['uk_trosak'].' EUR       '.
			'Nabavna cijena: '.$tr_data['uk_nabavna_cijena'].' EUR       Koef. Troska: '.$tr_data['koef_troska'].''), 0, "L");
		$doc->Ln(1);
		
		
		
		$doc->Cell(220);
		$doc->Cell(50,20,"Obradio",'B',0,'C');
		$doc->Ln();
	
		
		if($a_param['destination'] == 'screen'){
			
			$doc->output();
			die();
			
		}else{
			
			$l_name = 'otpis.pdf';
			$l_dest = $g_path.'app/webroot/res/files/'.$l_name;
			
			$doc->output($l_dest, 'F');
			
			return array('path' => $g_path.'app/webroot/res/files/', 'name' => $l_name);
		}
		
	}
	
	function all_print_returnee($a_param = array(), $id = null){
		
		$g_path = get_item('select value from settings where name = "putanja.abs" ');
			
		if(!defined('FPDF_FONTPATH')){
			define('FPDF_FONTPATH', 'font/');
		}
		
		//require_once($g_path.'app/pdf/fpdf.php');
		require_once($g_path.'app/libs/fpdf/fpdf.php');
		require_once($g_path.'app/libs/fpdf/fpdi.php');
		
		$doc =& new FPDI('L', 'mm', 'A4');
		
		$doc->AddFont("ArialCE", "B", "6488f5d4657b269b9f5770508e80e299_arialbd.php");
		$doc->AddFont("ArialCE", "", "332cc83ebee36613c19564e6e776c81b_arial.php");
		
		//$doc = new Document;
		
		$doc->AddPage('L');
	    
		$doc->SetFont('Helvetica','B',12);
		$doc->Cell(100);
		$doc->Cell(0,5,_convert($a_param['restName']["Setting"]["value"]),0,2);
		$doc->SetFont('Helvetica','',8);
		
		$doc->Cell(0,5,_convert($a_param['restpost']["Setting"]["value"].' '.$a_param['restComm']["Setting"]["value"].','.
			$a_param['restStreet']["Setting"]["value"].' '.$a_param['restStrNum']["Setting"]["value"].$a_param['restStrNumMark']["Setting"]["value"]),0,2);
		
		$doc->SetFont('Helvetica','',8);
		$doc->Cell(0,5,_convert($a_param['restPlaceid']["Setting"]["value"]),0,0);
		
		$rec = $a_param['rec'];
		
		$doc->Ln();
		$doc->SetFont('Helvetica','B',12);  
		$doc->Cell(80,8,$rec["Supplier"]["name"],'LTR',0);
	    $doc->Cell(20);
		$doc->SetFont('Helvetica','B',10);
        $doc->Cell(80,8, 'Povratnica '.$rec["Receiving"]["broj_povratnica"],'LTRB',0,'C');

		$dt = date('d.m.Y', strtotime($rec["Receiving"]["created"]));

		$doc->Ln();
		$doc->SetFont('Helvetica','B',8);  
		$doc->Cell(80,5,'  ','LR',0);
	    $doc->Cell(20);
	    $doc->SetFont('Courier','',8);
		$doc->Cell(80,5,"Broj dokumenta:    ".$rec["Receiving"]["number"],'LR',0);
		
		$doc->Ln();
		$doc->SetFont('Helvetica','B',8);  
		$doc->Cell(80,5,$rec["Supplier"]["address"].' '.$rec["Supplier"]["city"],'LR',0);
	    $doc->Cell(20);
	    $doc->SetFont('Courier','',8);
		$doc->Cell(80,5,"Datum dokumenta:   ".$dt,'LBR',0);
		$doc->Ln();

		/*
		$doc->SetFont('Helvetica','B',8);  
		$doc->Cell(80,5,'  ','LR',0);
	    $doc->Cell(20);
		$doc->SetFont('Courier','',8);
		$doc->Cell(80,5,"Racun:             ".'','LBR',0);
		$doc->Ln();
		*/
		
		$doc->SetFont('Helvetica','',8);  
		$doc->Cell(80,5,"OIB: ".$rec["Supplier"]["oib"],'LRB',0,'R');
		$doc->Ln(10);
		
		
		//print_r($rec);
		//exit;
	    
		$doc->SetFont('Helvetica','B',8);
		
		// Column headings
		
		// Colors, line width and bold font
		 $doc->SetFillColor(255,255,255); // slova  u headeru
	

		$l_pdv = get_item('select value from settings where name = "duty.free" ');
		if($l_pdv == 'no'){
			$l_pdv = 'false';
		}
		
		if($l_pdv == 'false'){
			$header = array('Red.br','Sifra','Naziv artikla/usluge', 'Kol', 'Nab. Cj.', 'Nab.Vrijed.', 'Rabat%', 'Iz.Rabat', 'Fakt.Cj.', 'Fakt. Vrijed.','MPC' ,'Uk cijena');
			$w = array(10,20,50,10,15,15,10,15,15,18,18,18,18,20);
		}else{
			$header = array('Red.br','Sifra','Naziv artikla/usluge', 'Kol', 'Nab. Cj.', 'Nab.Vrijed.', 'Rabat%', 'Iz.Rabat', 'Fakt.Cj.', 'Fakt. Vrijed.', 'PDV%', 'Iz.PDV', 'MPC' ,'Uk cijena');
			$w = array(10,20,50,10,15,15,10,15,15,18,18,18,10,18,18,20);
		}
		
		for($i=0;$i<count($header);$i++)
			$doc->Cell($w[$i],7,$header[$i],0,0,'C',true);
		$doc->Ln();
	
		$fill = false;
		
		if($id == 0){
			// resetiranje, odnosno, kad licenca nije valjana, onda se u izvjesce stavi samo zaglavlje, a kad dodju pravi podaci, onda se ti pravi podaci ne ispisuju
			$a_param['items'] = array();
		}
		
		
		$doc->SetFont('Helvetica','',8);
		$priceBuy = 0;
		$priceSell= 0;
		$fakt =0;
		$price = 0;
		$marza = 0;
		$pdv = 0;
		$discount = 0;
		$redbr = 1;
		foreach($a_param['items'] as $item){
			
			$iid = $item["Article"]["tax_group_id"];
			$taxGroup = array();
			$taxGroup["TaxGroup"] = get_data('select id, name, tax_1, tax_2 from tax_groups where id = '.$iid, 0);
			
			// zasad ove dvije linije ispod zanemari
			// mora uzeti vrijednosti koje se pamte unutar receiving_items, a ne unutar articles... jer se vrijednost u articles moze mijenjati, a ovdje treba uvijek ista vrijednost
			//$taxGroup["TaxGroup"]["tax_1"] = $item["ReceivingItem"]["tax_1"];
			//$taxGroup["TaxGroup"]["tax_2"] = $item["ReceivingItem"]["tax_2"];
			
			
			
			$doc->Cell($w[0],6,$redbr,0,0,'C',$fill);
			$doc->Cell($w[1],6,$item["Article"]["barcode"],0,0,'L',$fill);
			
			$posY = $doc->GetY();
			$posX = $doc->GetX();
			$nl = 0;
			if(strlen($item["Article"]["name"])>40)
			{
				$temptext = str_split($item["Article"]["name"],40);
				foreach($temptext as $newline) {
					$doc->Cell($w[2],3,$newline,0,2,'L');
					$nl += 1;
				}
				$posX += $w[2];
				$doc->SetY($posY);
				$posY +=  $nl*3; // pozicija sledeceg reda
			}
			else
			{
				$doc->Cell($w[2],6,$item["Article"]["name"],0,0,'L',$fill);	
				$posX = $doc->GetX();
				$posY +=  6;
			}
			
			$doc->SetX($posX);
			
		
			$doc->Cell($w[3],6,number_format($item["ReceivingItem"]["amount"],2, '.', ' '),0,0,'R',$fill);
			$doc->Cell($w[4],6,number_format($item["ReceivingItem"]["price_buy"],2, '.', ' '),0,0,'R',$fill);
			$doc->Cell($w[5],6,number_format($item["ReceivingItem"]["price_buy"]*$item["ReceivingItem"]["amount"],2, '.', ' '),0,0,'R',$fill);
			$doc->Cell($w[6],6,number_format($item["ReceivingItem"]["discount"]),0,0,'R',$fill); 
			$doc->Cell($w[7],6,number_format(($item["ReceivingItem"]["discount"])*$item["ReceivingItem"]["price_buy"]*$item["ReceivingItem"]["amount"]/100,2, '.', ' '),0,0,'R',$fill);
			$doc->Cell($w[8],6,number_format($item["ReceivingItem"]["price_buy"]*(1- ($item["ReceivingItem"]["discount"]/100))),0,0,'R',$fill);
			$doc->Cell($w[9],6,number_format($item["ReceivingItem"]["price_buy"]*(1- ($item["ReceivingItem"]["discount"]/100))*$item["ReceivingItem"]["amount"],2, '.', ' '),0,0,'R',$fill);
		//print_r($item);
		//exit;
			//$doc->Cell($w[10],6,number_format(($item["ReceivingItem"]["price_sell"]/(1+$taxGroup["TaxGroup"]["tax_1"]/100)/($item["ReceivingItem"]["price_buy"]*(1- ($item["ReceivingItem"]["discount"]/100))) -1)*100,2, '.', ' '),0,0,'R',$fill);
			//$doc->Cell($w[11],6,number_format(($item["ReceivingItem"]["price_sell"]/(1+$taxGroup["TaxGroup"]["tax_1"]/100)- ($item["ReceivingItem"]["price_buy"]*(1- ($item["ReceivingItem"]["discount"]/100)))) *$item["ReceivingItem"]["amount"],2, '.', ' '),0,0,'R',$fill);
			//$doc->Cell($w[10],6,'-     ',0,0,'R',$fill);
			//$doc->Cell($w[10],6,'-     ',0,0,'R',$fill);
			
			if($l_pdv == 'false'){
				$doc->Cell($w[12],6,number_format($item["ReceivingItem"]["price_sell"],2, '.', ' '),0,0,'R',$fill);
				$doc->Cell($w[13],6,number_format($item["ReceivingItem"]["amount"]*$item["ReceivingItem"]["price_sell"],2, '.', ' '),0,0,'R',$fill);
			}else{
				$doc->Cell($w[12],6,number_format($taxGroup["TaxGroup"]["tax_1"]),0,0,'R',$fill);
				$doc->Cell($w[13],6,number_format($item["ReceivingItem"]["price_sell"]/(1 + $taxGroup["TaxGroup"]["tax_1"]/100)* $taxGroup["TaxGroup"]["tax_1"] /100*$item["ReceivingItem"]["amount"] ,2, '.', ' '),0,0,'R',$fill);
				$doc->Cell($w[14],6,number_format($item["ReceivingItem"]["price_sell"],2, '.', ' '),0,0,'R',$fill);
				$doc->Cell($w[15],6,number_format($item["ReceivingItem"]["amount"]*$item["ReceivingItem"]["price_sell"],2, '.', ' '),0,0,'R',$fill);
			}
			
			$doc->Ln();
			
			$priceBuy = $priceBuy + $item["ReceivingItem"]["price_buy"]*$item["ReceivingItem"]["amount"];
			$discount = $discount + (($item["ReceivingItem"]["discount"])*$item["ReceivingItem"]["price_buy"]*$item["ReceivingItem"]["amount"]/100);
			$fakt = $fakt + $item["ReceivingItem"]["price_buy"]*(1- ($item["ReceivingItem"]["discount"]/100))*$item["ReceivingItem"]["amount"];
			$marza = $marza + ($item["ReceivingItem"]["price_sell"]/(1+$taxGroup["TaxGroup"]["tax_1"]/100)- ($item["ReceivingItem"]["price_buy"]*(1- ($item["ReceivingItem"]["discount"]/100)))) *$item["ReceivingItem"]["amount"];
			$pdv = $pdv + $item["ReceivingItem"]["price_sell"]/(1 + $taxGroup["TaxGroup"]["tax_1"]/100)* $taxGroup["TaxGroup"]["tax_1"] /100*$item["ReceivingItem"]["amount"];
			$price = $price + $item["ReceivingItem"]["price_sell"]*$item["ReceivingItem"]["amount"];
			
			
			$redbr = $redbr+1;
			$doc->SetY($posY);
		}
		
		// Closing line
		$doc->Cell(array_sum($w) - 36,0,'','T');
		$doc->Ln();
		$doc->Cell($w[0]+$w[1]+$w[2]+$w[3]+$w[4],6,'',0,0,'L',$fill);
		$doc->Cell($w[5],6,number_format($priceBuy,2, '.', ' '),0,0,'R',$fill);
		$doc->Cell($w[6],6,'',0,0,'R',$fill);
		$doc->Cell($w[7],6,number_format($discount,2, '.', ' '),0,0,'R',$fill);
		$doc->Cell($w[8],6,'',0,0,'R',$fill);
		$doc->Cell($w[9],6,number_format($fakt,2, '.', ' '),0,0,'R',$fill);
		//$doc->Cell($w[10],6,'',0,0,'R',$fill);
		//$doc->Cell($w[11],6,number_format($marza,2, '.', ' '),0,0,'R',$fill);
		if($l_pdv == 'false'){
			$doc->Cell($w[12],6,'',0,0,'R',$fill);
			$doc->Cell($w[13],6,number_format($price,2, '.', ' '),0,0,'R',$fill);
		}else{
			$doc->Cell($w[12],6,'',0,0,'R',$fill);
			$doc->Cell($w[13],6,number_format($pdv,2, '.', ' '),0,0,'R',$fill);
			$doc->Cell($w[14],6,'',0,0,'R',$fill);
			$doc->Cell($w[15],6,number_format($price,2, '.', ' '),0,0,'R',$fill);
		}
		
		$doc->Ln();
    $doc->Cell(array_sum($w) - 36,0,'','T');
		$doc->Ln(6);
		
		if($l_pdv == 'false'){
		}else{
			$doc->MultiCell(210, 4, _convert('R E K A P I T U L A C I J A:'), 0, "L");
			$doc->Ln(2);
			
			$y_pos = $doc->GetY();
			$doc->MultiCell(60, 4, _convert('Nabavna vrijednost:'), 0, "L");
			$doc->SetXY(70, $y_pos);
			$doc->MultiCell(60, 4, _convert('Prodajna vrijednost:'), 0, "L");
			$doc->Ln(1);
			
			$y_pos = $doc->GetY();
			$doc->SetXY(10, $y_pos);
			$doc->MultiCell(20, 4, _convert('PDV'), 0, "L");
			$doc->SetXY(20, $y_pos);
			$doc->MultiCell(19, 4, _convert('OSNOVICA'), 0, "R");
			$doc->SetXY(40, $y_pos);
			$doc->MultiCell(15, 4, _convert('IZNOS'), 0, "R");
			
			$doc->SetXY(70, $y_pos);
			$doc->MultiCell(20, 4, _convert('PDV'), 0, "L");
			$doc->SetXY(80, $y_pos);
			$doc->MultiCell(19, 4, _convert('OSNOVICA'), 0, "R");
			$doc->SetXY(100, $y_pos);
			$doc->MultiCell(15, 4, _convert('IZNOS'), 0, "R");
			
			$doc->Ln(1);
			
			$la_sum = array(0, 0);
			
			/*
			// nekad kasnije za implementirati
			$l_sql = 'select receiving_items.tax_1, sum(receiving_items.amount * receiving_items.price_buy) as s1, sum(receiving_items.amount * receiving_items.price_sell) as s2 '.
				' from receivings, receiving_items '.
				' where receivings.id = receiving_items.receiving_id '.
				' and receivings.id = '.$id.
				' group by 1 '.
				' order by 1';
			*/
			$l_sql = 'select tax_groups.tax_1, sum(receiving_items.amount * receiving_items.price_buy) as s1, sum(receiving_items.amount * receiving_items.price_sell) as s2 '.
				' from receivings, receiving_items, articles, tax_groups '.
				' where receivings.id = receiving_items.receiving_id '.
				' and receiving_items.article_id = articles.id '.
				' and articles.tax_group_id = tax_groups.id '.
				' and receivings.id = '.$id.
				' group by 1 '.
				' order by 1';
				
			$x = get_data($l_sql, 1);
			for($i = 0; isset($x['tax_1']) && $i < sizeof($x['tax_1']); $i++){
				$y_pos = $doc->GetY();
				$doc->SetXY(10, $y_pos);
				$doc->MultiCell(20, 4, _convert(round($x['tax_1'][$i]).' %'), 0, "L");
				$doc->SetXY(20, $y_pos);
				$doc->MultiCell(19, 4, _convert(sprintf('%01.2f', $x['s1'][$i])), 0, "R");
				$doc->SetXY(40, $y_pos);
				$doc->MultiCell(15, 4, _convert(sprintf('%01.2f', $x['s1'][$i] * $x['tax_1'][$i] / 100)), 0, "R");
				
				$doc->SetXY(70, $y_pos);
				$doc->MultiCell(20, 4, _convert(round($x['tax_1'][$i]).' %'), 0, "L");
				$doc->SetXY(80, $y_pos);
				$doc->MultiCell(19, 4, _convert(sprintf('%01.2f', $x['s2'][$i] / (1 + $x['tax_1'][$i] / 100))), 0, "R");
				$doc->SetXY(100, $y_pos);
				$doc->MultiCell(15, 4, _convert(sprintf('%01.2f', $x['s2'][$i] - ($x['s2'][$i] / (1 + $x['tax_1'][$i] / 100)))), 0, "R");
				
				$doc->Ln(1);
				
				$la_sum[0] += $x['s1'][$i] * $x['tax_1'][$i];
				$la_sum[1] += $x['s2'][$i] - ($x['s2'][$i] / (1 + $x['tax_1'][$i] / 100));
				
			}
			
			$y_pos = $doc->GetY();
			$doc->SetXY(10, $y_pos);
			$doc->MultiCell(30, 4, _convert('Sveukupno PDV:'), 0, "L");
			$doc->SetXY(40, $y_pos);
			$doc->MultiCell(15, 4, _convert(sprintf('%01.2f', $la_sum[0] / 100)), 0, "R");
			
			$doc->SetXY(70, $y_pos);
			$doc->MultiCell(30, 4, _convert('Sveukupno PDV:'), 0, "L");
			$doc->SetXY(100, $y_pos);
			$doc->MultiCell(15, 4, _convert(sprintf('%01.2f', $la_sum[1])), 0, "R");
		}
		


		$l_sql = 'select trosak1, trosak2, trosak3, uk_trosak, uk_nabavna_cijena, koef_troska '.
			' from receivings '.
			' where receivings.id = '.$id;
		$tr_data = get_data($l_sql);
		
		$doc->Ln(5);
		$y_pos = $doc->GetY();
		$doc->SetXY(10, $y_pos);
		$doc->MultiCell(260, 4, _convert('Trosak 1: '.$tr_data['trosak1'].' EUR       Trosak 2: '.$tr_data['trosak2'].' EUR       '.
			'Trosak 3: '.$tr_data['trosak3'].' EUR       UK Trosak: '.$tr_data['uk_trosak'].' EUR       '.
			'Nabavna cijena: '.$tr_data['uk_nabavna_cijena'].' EUR       Koef. Troska: '.$tr_data['koef_troska'].''), 0, "L");
		$doc->Ln(1);
		


		$doc->Cell(220);
		$doc->Cell(50,20,"Obradio",'B',0,'C');
		$doc->Ln();
	
		if($a_param['destination'] == 'screen'){
			
			$doc->output();
			die();
			
		}else{
			
			$l_name = 'povratnica.pdf';
			$l_dest = $g_path.'app/webroot/res/files/'.$l_name;
			
			$doc->output($l_dest, 'F');
			
			return array('path' => $g_path.'app/webroot/res/files/', 'name' => $l_name);
		}
		
	}
	
	
	function all_print_transfer_note($a_param = array(), $id = null){
		
		$g_path = get_item('select value from settings where name = "putanja.abs" ');
			
		if(!defined('FPDF_FONTPATH')){
			define('FPDF_FONTPATH', 'font/');
		}
		
		//require_once($g_path.'app/pdf/fpdf.php');
		require_once($g_path.'app/libs/fpdf/fpdf.php');
		require_once($g_path.'app/libs/fpdf/fpdi.php');
		
		$doc =& new FPDI('L', 'mm', 'A4');
		
		$doc->AddFont("ArialCE", "B", "6488f5d4657b269b9f5770508e80e299_arialbd.php");
		$doc->AddFont("ArialCE", "", "332cc83ebee36613c19564e6e776c81b_arial.php");
		
		//$doc = new Document;
		
		$doc->AddPage('L');
	    
		$doc->SetFont('Helvetica','B',12);
		$doc->Cell(100);
		$doc->Cell(0,5,_convert($a_param['restName']["Setting"]["value"]),0,2);
		$doc->SetFont('Helvetica','',8);
		
		$doc->Cell(0,5,_convert($a_param['restpost']["Setting"]["value"].' '.$a_param['restComm']["Setting"]["value"].','.
			$a_param['restStreet']["Setting"]["value"].' '.$a_param['restStrNum']["Setting"]["value"].$a_param['restStrNumMark']["Setting"]["value"]),0,2);
		
		$doc->SetFont('Helvetica','',8);
		$doc->Cell(0,5,_convert($a_param['restPlaceid']["Setting"]["value"]),0,0);
		
		$rec = $a_param['rec'];
		
		$doc->Ln();
		$doc->SetFont('Helvetica','B',12);  
		$doc->Cell(80,8,$rec["Supplier"]["name"],'LTR',0);
	    $doc->Cell(20);
		$doc->SetFont('Helvetica','B',10);
        $doc->Cell(80,8, 'Medjuskladisnica '.$rec["Receiving"]["broj_medjuskladisnica"],'LTRB',0,'C');

		$dt = date('d.m.Y', strtotime($rec["Receiving"]["created"]));

		$doc->Ln();
		$doc->SetFont('Helvetica','B',8);  
		$doc->Cell(80,5,'  ','LR',0);
	    $doc->Cell(20);
	    $doc->SetFont('Courier','',8);
		$doc->Cell(80,5,"Broj dokumenta:    ".$rec["Receiving"]["number"],'LR',0);
		
		$doc->Ln();
		$doc->SetFont('Helvetica','B',8);  
		$doc->Cell(80,5,$rec["Supplier"]["address"].' '.$rec["Supplier"]["city"],'LR',0);
	    $doc->Cell(20);
	    $doc->SetFont('Courier','',8);
		$doc->Cell(80,5,"Datum dokumenta:   ".$dt,'LBR',0);
		$doc->Ln();

		/*
		$doc->SetFont('Helvetica','B',8);  
		$doc->Cell(80,5,'  ','LR',0);
	    $doc->Cell(20);
		$doc->SetFont('Courier','',8);
		$doc->Cell(80,5,"Racun:             ".'','LBR',0);
		$doc->Ln();
		*/
		
		$doc->SetFont('Helvetica','',8);  
		$doc->Cell(80,5,"OIB: ".$rec["Supplier"]["oib"],'LRB',0,'R');
		$doc->Ln(10);
		
		
		//print_r($rec);
		//exit;
	    
		$doc->SetFont('Helvetica','B',8);
		
		// Column headings
		
		// Colors, line width and bold font
		 $doc->SetFillColor(255,255,255); // slova  u headeru
	

		$l_pdv = get_item('select value from settings where name = "duty.free" ');
		if($l_pdv == 'no'){
			$l_pdv = 'false';
		}
		
		if($l_pdv == 'false'){
			$header = array('Red.br','Sifra','Naziv artikla/usluge', 'Kol', 'Nab. Cj.', 'Nab.Vrijed.', 'Rabat%', 'Iz.Rabat', 'Fakt.Cj.', 'Fakt. Vrijed.', 'MPC' ,'Uk cijena');
			$w = array(10,20,50,10,15,15,10,15,15,18,18,18,18,20);
		}else{
			$header = array('Red.br','Sifra','Naziv artikla/usluge', 'Kol', 'Nab. Cj.', 'Nab.Vrijed.', 'Rabat%', 'Iz.Rabat', 'Fakt.Cj.', 'Fakt. Vrijed.', 'PDV%', 'Iz.PDV', 'MPC' ,'Uk cijena');
			$w = array(10,20,50,10,15,15,10,15,15,18,18,18,10,18,18,20);
		}
		
		for($i=0;$i<count($header);$i++)
			$doc->Cell($w[$i],7,$header[$i],0,0,'C',true);
		$doc->Ln();
	
		$fill = false;
		
		if($id == 0){
			// resetiranje, odnosno, kad licenca nije valjana, onda se u izvjesce stavi samo zaglavlje, a kad dodju pravi podaci, onda se ti pravi podaci ne ispisuju
			$a_param['items'] = array();
		}
		
		
		$doc->SetFont('Helvetica','',8);
		$priceBuy = 0;
		$priceSell= 0;
		$fakt =0;
		$price = 0;
		$marza = 0;
		$pdv = 0;
		$discount = 0;
		$redbr = 1;
		foreach($a_param['items'] as $item){
			
			$iid = $item["Article"]["tax_group_id"];
			$taxGroup = array();
			$taxGroup["TaxGroup"] = get_data('select id, name, tax_1, tax_2 from tax_groups where id = '.$iid, 0);
			
			// zasad ove dvije linije ispod zanemari
			// mora uzeti vrijednosti koje se pamte unutar receiving_items, a ne unutar articles... jer se vrijednost u articles moze mijenjati, a ovdje treba uvijek ista vrijednost
			//$taxGroup["TaxGroup"]["tax_1"] = $item["ReceivingItem"]["tax_1"];
			//$taxGroup["TaxGroup"]["tax_2"] = $item["ReceivingItem"]["tax_2"];
			
			
			
			
			$doc->Cell($w[0],6,$redbr,0,0,'C',$fill);
			$doc->Cell($w[1],6,$item["Article"]["barcode"],0,0,'L',$fill);
			
			$posY = $doc->GetY();
			$posX = $doc->GetX();
			$nl = 0;
			if(strlen($item["Article"]["name"])>40)
			{
				$temptext = str_split($item["Article"]["name"],40);
				foreach($temptext as $newline) {
					$doc->Cell($w[2],3,$newline,0,2,'L');
					$nl += 1;
				}
				$posX += $w[2];
				$doc->SetY($posY);
				$posY +=  $nl*3; // pozicija sledeceg reda
			}
			else
			{
				$doc->Cell($w[2],6,$item["Article"]["name"],0,0,'L',$fill);	
				$posX = $doc->GetX();
				$posY +=  6;
			}
			
			$doc->SetX($posX);
			
		
			$doc->Cell($w[3],6,number_format($item["ReceivingItem"]["amount"],2, '.', ' '),0,0,'R',$fill);
			$doc->Cell($w[4],6,number_format($item["ReceivingItem"]["price_buy"],2, '.', ' '),0,0,'R',$fill);
			$doc->Cell($w[5],6,number_format($item["ReceivingItem"]["price_buy"]*$item["ReceivingItem"]["amount"],2, '.', ' '),0,0,'R',$fill);
			$doc->Cell($w[6],6,number_format($item["ReceivingItem"]["discount"]),0,0,'R',$fill); 
			$doc->Cell($w[7],6,number_format(($item["ReceivingItem"]["discount"])*$item["ReceivingItem"]["price_buy"]*$item["ReceivingItem"]["amount"]/100,2, '.', ' '),0,0,'R',$fill);
			$doc->Cell($w[8],6,number_format($item["ReceivingItem"]["price_buy"]*(1- ($item["ReceivingItem"]["discount"]/100))),0,0,'R',$fill);
			$doc->Cell($w[9],6,number_format($item["ReceivingItem"]["price_buy"]*(1- ($item["ReceivingItem"]["discount"]/100))*$item["ReceivingItem"]["amount"],2, '.', ' '),0,0,'R',$fill);
		//print_r($item);
		//exit;
			//$doc->Cell($w[10],6,number_format(($item["ReceivingItem"]["price_sell"]/(1+$taxGroup["TaxGroup"]["tax_1"]/100)/($item["ReceivingItem"]["price_buy"]*(1- ($item["ReceivingItem"]["discount"]/100))) -1)*100,2, '.', ' '),0,0,'R',$fill);
			//$doc->Cell($w[11],6,number_format(($item["ReceivingItem"]["price_sell"]/(1+$taxGroup["TaxGroup"]["tax_1"]/100)- ($item["ReceivingItem"]["price_buy"]*(1- ($item["ReceivingItem"]["discount"]/100)))) *$item["ReceivingItem"]["amount"],2, '.', ' '),0,0,'R',$fill);
			//$doc->Cell($w[10],6,'-     ',0,0,'R',$fill);
			//$doc->Cell($w[10],6,'-     ',0,0,'R',$fill);
			
			if($l_pdv == 'false'){
				$doc->Cell($w[12],6,number_format($item["ReceivingItem"]["price_sell"],2, '.', ' '),0,0,'R',$fill);
				$doc->Cell($w[13],6,number_format($item["ReceivingItem"]["amount"]*$item["ReceivingItem"]["price_sell"],2, '.', ' '),0,0,'R',$fill);
			}else{
				$doc->Cell($w[12],6,number_format($taxGroup["TaxGroup"]["tax_1"]),0,0,'R',$fill);
				$doc->Cell($w[13],6,number_format($item["ReceivingItem"]["price_sell"]/(1 + $taxGroup["TaxGroup"]["tax_1"]/100)* $taxGroup["TaxGroup"]["tax_1"] /100*$item["ReceivingItem"]["amount"] ,2, '.', ' '),0,0,'R',$fill);
				$doc->Cell($w[14],6,number_format($item["ReceivingItem"]["price_sell"],2, '.', ' '),0,0,'R',$fill);
				$doc->Cell($w[15],6,number_format($item["ReceivingItem"]["amount"]*$item["ReceivingItem"]["price_sell"],2, '.', ' '),0,0,'R',$fill);
			}
			
			
			$doc->Ln();
			
			$priceBuy = $priceBuy + $item["ReceivingItem"]["price_buy"]*$item["ReceivingItem"]["amount"];
			$discount = $discount + (($item["ReceivingItem"]["discount"])*$item["ReceivingItem"]["price_buy"]*$item["ReceivingItem"]["amount"]/100);
			$fakt = $fakt + $item["ReceivingItem"]["price_buy"]*(1- ($item["ReceivingItem"]["discount"]/100))*$item["ReceivingItem"]["amount"];
			$marza = $marza + ($item["ReceivingItem"]["price_sell"]/(1+$taxGroup["TaxGroup"]["tax_1"]/100)- ($item["ReceivingItem"]["price_buy"]*(1- ($item["ReceivingItem"]["discount"]/100)))) *$item["ReceivingItem"]["amount"];
			$pdv = $pdv + $item["ReceivingItem"]["price_sell"]/(1 + $taxGroup["TaxGroup"]["tax_1"]/100)* $taxGroup["TaxGroup"]["tax_1"] /100*$item["ReceivingItem"]["amount"];
			$price = $price + $item["ReceivingItem"]["price_sell"]*$item["ReceivingItem"]["amount"];
			
			
			$redbr = $redbr+1;
			$doc->SetY($posY);
		}
		
		// Closing line
		$doc->Cell(array_sum($w) - 36,0,'','T');
		$doc->Ln();
		$doc->Cell($w[0]+$w[1]+$w[2]+$w[3]+$w[4],6,'',0,0,'L',$fill);
		$doc->Cell($w[5],6,number_format($priceBuy,2, '.', ' '),0,0,'R',$fill);
		$doc->Cell($w[6],6,'',0,0,'R',$fill);
		$doc->Cell($w[7],6,number_format($discount,2, '.', ' '),0,0,'R',$fill);
		$doc->Cell($w[8],6,'',0,0,'R',$fill);
		$doc->Cell($w[9],6,number_format($fakt,2, '.', ' '),0,0,'R',$fill);
		//$doc->Cell($w[10],6,'',0,0,'R',$fill);
		//$doc->Cell($w[11],6,number_format($marza,2, '.', ' '),0,0,'R',$fill);
		
		if($l_pdv == 'false'){
			$doc->Cell($w[12],6,'',0,0,'R',$fill);
			$doc->Cell($w[13],6,number_format($price,2, '.', ' '),0,0,'R',$fill);
		}else{
			$doc->Cell($w[12],6,'',0,0,'R',$fill);
			$doc->Cell($w[13],6,number_format($pdv,2, '.', ' '),0,0,'R',$fill);
			$doc->Cell($w[14],6,'',0,0,'R',$fill);
			$doc->Cell($w[15],6,number_format($price,2, '.', ' '),0,0,'R',$fill);
		}
			
		
		$doc->Ln();
    $doc->Cell(array_sum($w) - 36,0,'','T');
		$doc->Ln(6);
		
		if($l_pdv == 'false'){
		}else{
			$doc->MultiCell(210, 4, _convert('R E K A P I T U L A C I J A:'), 0, "L");
			$doc->Ln(2);
			
			$y_pos = $doc->GetY();
			$doc->MultiCell(60, 4, _convert('Nabavna vrijednost:'), 0, "L");
			$doc->SetXY(70, $y_pos);
			$doc->MultiCell(60, 4, _convert('Prodajna vrijednost:'), 0, "L");
			$doc->Ln(1);
			
			$y_pos = $doc->GetY();
			$doc->SetXY(10, $y_pos);
			$doc->MultiCell(20, 4, _convert('PDV'), 0, "L");
			$doc->SetXY(20, $y_pos);
			$doc->MultiCell(19, 4, _convert('OSNOVICA'), 0, "R");
			$doc->SetXY(40, $y_pos);
			$doc->MultiCell(15, 4, _convert('IZNOS'), 0, "R");
			
			$doc->SetXY(70, $y_pos);
			$doc->MultiCell(20, 4, _convert('PDV'), 0, "L");
			$doc->SetXY(80, $y_pos);
			$doc->MultiCell(19, 4, _convert('OSNOVICA'), 0, "R");
			$doc->SetXY(100, $y_pos);
			$doc->MultiCell(15, 4, _convert('IZNOS'), 0, "R");
			
			$doc->Ln(1);
			
			$la_sum = array(0, 0);
			
			/*
			// nekad kasnije za implementirati
			$l_sql = 'select receiving_items.tax_1, sum(receiving_items.amount * receiving_items.price_buy) as s1, sum(receiving_items.amount * receiving_items.price_sell) as s2 '.
				' from receivings, receiving_items '.
				' where receivings.id = receiving_items.receiving_id '.
				' and receivings.id = '.$id.
				' group by 1 '.
				' order by 1';
			*/
			$l_sql = 'select tax_groups.tax_1, sum(receiving_items.amount * receiving_items.price_buy) as s1, sum(receiving_items.amount * receiving_items.price_sell) as s2 '.
				' from receivings, receiving_items, articles, tax_groups '.
				' where receivings.id = receiving_items.receiving_id '.
				' and receiving_items.article_id = articles.id '.
				' and articles.tax_group_id = tax_groups.id '.
				' and receivings.id = '.$id.
				' group by 1 '.
				' order by 1';
				
				
			$x = get_data($l_sql, 1);
			for($i = 0; isset($x['tax_1']) && $i < sizeof($x['tax_1']); $i++){
				$y_pos = $doc->GetY();
				$doc->SetXY(10, $y_pos);
				$doc->MultiCell(20, 4, _convert(round($x['tax_1'][$i]).' %'), 0, "L");
				$doc->SetXY(20, $y_pos);
				$doc->MultiCell(19, 4, _convert(sprintf('%01.2f', $x['s1'][$i])), 0, "R");
				$doc->SetXY(40, $y_pos);
				$doc->MultiCell(15, 4, _convert(sprintf('%01.2f', $x['s1'][$i] * $x['tax_1'][$i] / 100)), 0, "R");
				
				$doc->SetXY(70, $y_pos);
				$doc->MultiCell(20, 4, _convert(round($x['tax_1'][$i]).' %'), 0, "L");
				$doc->SetXY(80, $y_pos);
				$doc->MultiCell(19, 4, _convert(sprintf('%01.2f', $x['s2'][$i] / (1 + $x['tax_1'][$i] / 100))), 0, "R");
				$doc->SetXY(100, $y_pos);
				$doc->MultiCell(15, 4, _convert(sprintf('%01.2f', $x['s2'][$i] - ($x['s2'][$i] / (1 + $x['tax_1'][$i] / 100)))), 0, "R");
				
				$doc->Ln(1);
				
				$la_sum[0] += $x['s1'][$i] * $x['tax_1'][$i];
				$la_sum[1] += $x['s2'][$i] - ($x['s2'][$i] / (1 + $x['tax_1'][$i] / 100));
			}
			
			$y_pos = $doc->GetY();
			$doc->SetXY(10, $y_pos);
			$doc->MultiCell(30, 4, _convert('Sveukupno PDV:'), 0, "L");
			$doc->SetXY(40, $y_pos);
			$doc->MultiCell(15, 4, _convert(sprintf('%01.2f', $la_sum[0] / 100)), 0, "R");
			
			$doc->SetXY(70, $y_pos);
			$doc->MultiCell(30, 4, _convert('Sveukupno PDV:'), 0, "L");
			$doc->SetXY(100, $y_pos);
			$doc->MultiCell(15, 4, _convert(sprintf('%01.2f', $la_sum[1])), 0, "R");
		}
		
		
		$l_sql = 'select trosak1, trosak2, trosak3, uk_trosak, uk_nabavna_cijena, koef_troska '.
			' from receivings '.
			' where receivings.id = '.$id;
		$tr_data = get_data($l_sql);
		
		$doc->Ln(5);
		$y_pos = $doc->GetY();
		$doc->SetXY(10, $y_pos);
		$doc->MultiCell(260, 4, _convert('Trosak 1: '.$tr_data['trosak1'].' EUR       Trosak 2: '.$tr_data['trosak2'].' EUR       '.
			'Trosak 3: '.$tr_data['trosak3'].' EUR       UK Trosak: '.$tr_data['uk_trosak'].' EUR       '.
			'Nabavna cijena: '.$tr_data['uk_nabavna_cijena'].' EUR       Koef. Troska: '.$tr_data['koef_troska'].''), 0, "L");
		$doc->Ln(1);
		
		
		$doc->Cell(220);
		$doc->Cell(50,20,"Obradio",'B',0,'C');
		$doc->Ln();
	
		if($a_param['destination'] == 'screen'){
			
			$doc->output();
			die();
			
		}else{
			
			$l_name = 'medjuskladisnica.pdf';
			$l_dest = $g_path.'app/webroot/res/files/'.$l_name;
			
			$doc->output($l_dest, 'F');
			
			return array('path' => $g_path.'app/webroot/res/files/', 'name' => $l_name);
		}
	}
	
	
	
	function all_print_inventura($a_param = array(), $id = null){
		
		$g_path = get_item('select value from settings where name = "putanja.abs" ');
			
		if(!defined('FPDF_FONTPATH')){
			define('FPDF_FONTPATH', 'font/');
		}
		
		//require_once($g_path.'app/pdf/fpdf.php');
		require_once($g_path.'app/libs/fpdf/fpdf.php');
		require_once($g_path.'app/libs/fpdf/fpdi.php');
		
		$doc =& new FPDI('L', 'mm', 'A4');
		
		$doc->AddFont("ArialCE", "B", "6488f5d4657b269b9f5770508e80e299_arialbd.php");
		$doc->AddFont("ArialCE", "", "332cc83ebee36613c19564e6e776c81b_arial.php");
		
		//$doc = new Document;
		
		$doc->AddPage('P');
	    
		$doc->SetFont('Helvetica','B',12);
		$doc->Cell(100);
		$doc->Cell(0,5,_convert($a_param['restName']["Setting"]["value"]),0,2);
		$doc->SetFont('Helvetica','',8);
		
		$doc->Cell(0,5,_convert($a_param['restpost']["Setting"]["value"].' '.$a_param['restComm']["Setting"]["value"].','.
			$a_param['restStreet']["Setting"]["value"].' '.$a_param['restStrNum']["Setting"]["value"].$a_param['restStrNumMark']["Setting"]["value"]),0,2);
		
		$doc->SetFont('Helvetica','',8);
		$doc->Cell(0,5,_convert($a_param['restPlaceid']["Setting"]["value"]),0,0);
		
		$rec = $a_param['rec'];
		
		
		$doc->Ln();
		$doc->SetFont('Helvetica','B',12);  
		$doc->Cell(80,8,$rec["Supplier"]["name"],'',0);
	    $doc->Cell(20);
		$doc->SetFont('Helvetica','B',10);
        $doc->Cell(80,8, 'Inventura '.$rec["Receiving"]["broj_inventura"],'LTRB',0,'C');

		$dt = date('d.m.Y', strtotime($rec["Receiving"]["created"]));

		$doc->Ln();
		$doc->SetFont('Helvetica','B',8);  
		$doc->Cell(80,5,'  ','',0);
	    $doc->Cell(20);
	    $doc->SetFont('Courier','',8);
		$doc->Cell(80,5,"Broj dokumenta:    ".$rec["Receiving"]["number"],'LR',0);
		
		$doc->Ln();
		$doc->SetFont('Helvetica','B',8);  
		$doc->Cell(80,5,'','',0);
	    $doc->Cell(20);
	    $doc->SetFont('Courier','',8);
		$doc->Cell(80,5,"Datum dokumenta:   ".$dt,'LBR',0);
		$doc->Ln();

		/*
		$doc->SetFont('Helvetica','B',8);  
		$doc->Cell(80,5,'  ','',0);
	    $doc->Cell(20);
		$doc->SetFont('Courier','',8);
		$doc->Cell(80,5,"".'','LBR',0);
		$doc->Ln();
		
		$doc->SetFont('Helvetica','',8);  
		$doc->Cell(80,5,"OIB: ".$rec["Supplier"]["oib"],'LRB',0,'R');
		$doc->Ln(10);
		*/
		$doc->Ln(10);
	    
		$doc->SetFont('Helvetica','B',8);
		
		// Column headings
		
		// Colors, line width and bold font
		 $doc->SetFillColor(255,255,255); // slova  u headeru
		 
		 
		$l_pdv = get_item('select value from settings where name = "duty.free" ');
		if($l_pdv == 'no'){
			$l_pdv = 'false';
		}
		
		if($l_pdv == 'false'){
			$header = array('Red.br','Sifra','Naziv artikla/usluge', 'Kol', 'Nab. Cj.', 'Nab.Vrijed.', 'Prod.Cj.', 'UK PC');
			$w = array(10,20,50,10,15,15,15,18);
		}else{
			$header = array('Red.br','Sifra','Naziv artikla/usluge', 'Kol', 'Nab. Cj.', 'Nab.Vrijed.', 'Prod.Cj.', 'UK PC', 'PDV%', 'Iz.PDV');
			$w = array(10,20,50,10,15,15,15,18,10,18);
		}

		
		for($i=0;$i<count($header);$i++)
			$doc->Cell($w[$i],7,$header[$i],0,0,'C',true);
		$doc->Ln();
	
		$fill = false;
		
		if($id == 0){
			// resetiranje, odnosno, kad licenca nije valjana, onda se u izvjesce stavi samo zaglavlje, a kad dodju pravi podaci, onda se ti pravi podaci ne ispisuju
			$a_param['items'] = array();
		}
		
		
		$doc->SetFont('Helvetica','',8);
		$priceBuy = 0;
		$priceSell= 0;
		$fakt =0;
		$price = 0;
		$marza = 0;
		$pdv = 0;
		$discount = 0;
		$redbr = 1;
		foreach($a_param['items'] as $item){
			
			$iid = $item["Article"]["tax_group_id"];
			$taxGroup = array();
			$taxGroup["TaxGroup"] = get_data('select id, name, tax_1, tax_2 from tax_groups where id = '.$iid, 0);
			
			// zasad ove dvije linije ispod zanemari
			// mora uzeti vrijednosti koje se pamte unutar receiving_items, a ne unutar articles... jer se vrijednost u articles moze mijenjati, a ovdje treba uvijek ista vrijednost
			//$taxGroup["TaxGroup"]["tax_1"] = $item["ReceivingItem"]["tax_1"];
			//$taxGroup["TaxGroup"]["tax_2"] = $item["ReceivingItem"]["tax_2"];
			
			
			
			$doc->Cell($w[0],6,$redbr,0,0,'C',$fill);
			$doc->Cell($w[1],6,$item["Article"]["barcode"],0,0,'L',$fill);
			
			$posY = $doc->GetY();
			$posX = $doc->GetX();
			$nl = 0;
			if(strlen($item["Article"]["name"])>40)
			{
				$temptext = str_split($item["Article"]["name"],40);
				foreach($temptext as $newline) {
					$doc->Cell($w[2],3,$newline,0,2,'L');
					$nl += 1;
				}
				$posX += $w[2];
				$doc->SetY($posY);
				$posY +=  $nl*3; // pozicija sledeceg reda
			}
			else
			{
				$doc->Cell($w[2],6,$item["Article"]["name"],0,0,'L',$fill);	
				$posX = $doc->GetX();
				$posY +=  6;
			}
			
			$doc->SetX($posX);
			
		
			$doc->Cell($w[3],6,number_format($item["ReceivingItem"]["amount"],2, '.', ' '),0,0,'R',$fill);
			$doc->Cell($w[4],6,number_format($item["ReceivingItem"]["price_buy"],2, '.', ' '),0,0,'R',$fill);
			$doc->Cell($w[5],6,number_format($item["ReceivingItem"]["price_buy"]*$item["ReceivingItem"]["amount"],2, '.', ' '),0,0,'R',$fill);
			//$doc->Cell($w[6],6,number_format($item["ReceivingItem"]["discount"]),0,0,'R',$fill); 
			//$doc->Cell($w[7],6,number_format(($item["ReceivingItem"]["discount"])*$item["ReceivingItem"]["price_buy"]*$item["ReceivingItem"]["amount"]/100,2, '.', ' '),0,0,'R',$fill);
			$doc->Cell($w[6],6,number_format($item["ReceivingItem"]["price_buy"]*(1- ($item["ReceivingItem"]["discount"]/100))),0,0,'R',$fill);
			$doc->Cell($w[7],6,number_format($item["ReceivingItem"]["price_buy"]*(1- ($item["ReceivingItem"]["discount"]/100))*$item["ReceivingItem"]["amount"],2, '.', ' '),0,0,'R',$fill);
			
			if($l_pdv == 'false'){
				//$doc->Cell($w[10],6,number_format($item["ReceivingItem"]["price_sell"],2, '.', ' '),0,0,'R',$fill);
				//$doc->Cell($w[11],6,number_format($item["ReceivingItem"]["amount"]*$item["ReceivingItem"]["price_sell"],2, '.', ' '),0,0,'R',$fill);
			}else{
				//$doc->Cell($w[10],6,number_format($taxGroup["TaxGroup"]["tax_1"]),0,0,'R',$fill);
				//$doc->Cell($w[11],6,number_format($item["ReceivingItem"]["price_sell"]/(1 + $taxGroup["TaxGroup"]["tax_1"]/100)* $taxGroup["TaxGroup"]["tax_1"] /100*$item["ReceivingItem"]["amount"] ,2, '.', ' '),0,0,'R',$fill);
				$doc->Cell($w[8],6,number_format($item["ReceivingItem"]["price_sell"],2, '.', ' '),0,0,'R',$fill);
				$doc->Cell($w[9],6,number_format($item["ReceivingItem"]["amount"]*$item["ReceivingItem"]["price_sell"],2, '.', ' '),0,0,'R',$fill);
			}
			
			
			$doc->Ln();
			
			$priceBuy = $priceBuy + $item["ReceivingItem"]["price_buy"]*$item["ReceivingItem"]["amount"];
			$discount = $discount + (($item["ReceivingItem"]["discount"])*$item["ReceivingItem"]["price_buy"]*$item["ReceivingItem"]["amount"]/100);
			$fakt = $fakt + $item["ReceivingItem"]["price_buy"]*(1- ($item["ReceivingItem"]["discount"]/100))*$item["ReceivingItem"]["amount"];
			if($l_pdv == 'false'){
				$marza = $marza + ($item["ReceivingItem"]["price_sell"]/(1+0/100)- ($item["ReceivingItem"]["price_buy"]*(1- ($item["ReceivingItem"]["discount"]/100)))) *$item["ReceivingItem"]["amount"];
				$pdv = $pdv + 0;
			}else{
				$marza = $marza + ($item["ReceivingItem"]["price_sell"]/(1+$taxGroup["TaxGroup"]["tax_1"]/100)- ($item["ReceivingItem"]["price_buy"]*(1- ($item["ReceivingItem"]["discount"]/100)))) *$item["ReceivingItem"]["amount"];
				$pdv = $pdv + $item["ReceivingItem"]["price_sell"]/(1 + $taxGroup["TaxGroup"]["tax_1"]/100)* $taxGroup["TaxGroup"]["tax_1"] /100*$item["ReceivingItem"]["amount"];
			}
			$price = $price + $item["ReceivingItem"]["price_sell"]*$item["ReceivingItem"]["amount"];
			
			
			$redbr = $redbr+1;
			$doc->SetY($posY);
		}
		
		// Closing line
		$doc->Cell(array_sum($w),0,'','T');
		$doc->Ln();
		$doc->Cell($w[0]+$w[1]+$w[2]+$w[3]+$w[4],6,'',0,0,'L',$fill);
		$doc->Cell($w[5],6,number_format($priceBuy,2, '.', ' '),0,0,'R',$fill);
		//$doc->Cell($w[6],6,'',0,0,'R',$fill);
		//$doc->Cell($w[7],6,number_format($discount,2, '.', ' '),0,0,'R',$fill);
		$doc->Cell($w[6],6,'',0,0,'R',$fill);
		$doc->Cell($w[7],6,number_format($fakt,2, '.', ' '),0,0,'R',$fill);
		if($l_pdv == 'false'){
			//$doc->Cell($w[10],6,'',0,0,'R',$fill);
			//$doc->Cell($w[11],6,number_format($price,2, '.', ' '),0,0,'R',$fill);
		}else{
			//$doc->Cell($w[10],6,'',0,0,'R',$fill);
			//$doc->Cell($w[11],6,number_format($pdv,2, '.', ' '),0,0,'R',$fill);
			$doc->Cell($w[8],6,'',0,0,'R',$fill);
			$doc->Cell($w[9],6,number_format($price,2, '.', ' '),0,0,'R',$fill);
		}
		
		$doc->Ln();
    $doc->Cell(array_sum($w),0,'','T');
		$doc->Ln(6);
		
		if($l_pdv == 'false'){
		}else{
			$doc->MultiCell(210, 4, _convert('R E K A P I T U L A C I J A:'), 0, "L");
			$doc->Ln(2);
			
			$y_pos = $doc->GetY();
			$doc->MultiCell(60, 4, _convert('Nabavna vrijednost:'), 0, "L");
			$doc->SetXY(70, $y_pos);
			$doc->MultiCell(60, 4, _convert('Prodajna vrijednost:'), 0, "L");
			$doc->Ln(1);
			
			$y_pos = $doc->GetY();
			$doc->SetXY(10, $y_pos);
			$doc->MultiCell(20, 4, _convert('PDV'), 0, "L");
			$doc->SetXY(20, $y_pos);
			$doc->MultiCell(19, 4, _convert('OSNOVICA'), 0, "R");
			$doc->SetXY(40, $y_pos);
			$doc->MultiCell(15, 4, _convert('IZNOS'), 0, "R");
			
			$doc->SetXY(70, $y_pos);
			$doc->MultiCell(20, 4, _convert('PDV'), 0, "L");
			$doc->SetXY(80, $y_pos);
			$doc->MultiCell(19, 4, _convert('OSNOVICA'), 0, "R");
			$doc->SetXY(100, $y_pos);
			$doc->MultiCell(15, 4, _convert('IZNOS'), 0, "R");
			
			$doc->Ln(1);
			
			$la_sum = array(0, 0);
			
			/*
			// nekad kasnije za implementirati
			$l_sql = 'select receiving_items.tax_1, sum(receiving_items.amount * receiving_items.price_buy) as s1, sum(receiving_items.amount * receiving_items.price_sell) as s2 '.
				' from receivings, receiving_items '.
				' where receivings.id = receiving_items.receiving_id '.
				' and receivings.id = '.$id.
				' group by 1 '.
				' order by 1';
			*/
			$l_sql = 'select tax_groups.tax_1, sum(receiving_items.amount * receiving_items.price_buy) as s1, sum(receiving_items.amount * receiving_items.price_sell) as s2 '.
				' from receivings, receiving_items, articles, tax_groups '.
				' where receivings.id = receiving_items.receiving_id '.
				' and receiving_items.article_id = articles.id '.
				' and articles.tax_group_id = tax_groups.id '.
				' and receivings.id = '.$id.
				' group by 1 '.
				' order by 1';
				
				
			$x = get_data($l_sql, 1);
			for($i = 0; isset($x['tax_1']) && $i < sizeof($x['tax_1']); $i++){
				$y_pos = $doc->GetY();
				$doc->SetXY(10, $y_pos);
				$doc->MultiCell(20, 4, _convert(round($x['tax_1'][$i]).' %'), 0, "L");
				$doc->SetXY(20, $y_pos);
				$doc->MultiCell(19, 4, _convert(sprintf('%01.2f', $x['s1'][$i])), 0, "R");
				$doc->SetXY(40, $y_pos);
				$doc->MultiCell(15, 4, _convert(sprintf('%01.2f', $x['s1'][$i] * $x['tax_1'][$i] / 100)), 0, "R");
				
				$doc->SetXY(70, $y_pos);
				$doc->MultiCell(20, 4, _convert(round($x['tax_1'][$i]).' %'), 0, "L");
				$doc->SetXY(80, $y_pos);
				$doc->MultiCell(19, 4, _convert(sprintf('%01.2f', $x['s2'][$i] / (1 + $x['tax_1'][$i] / 100))), 0, "R");
				$doc->SetXY(100, $y_pos);
				$doc->MultiCell(15, 4, _convert(sprintf('%01.2f', $x['s2'][$i] - ($x['s2'][$i] / (1 + $x['tax_1'][$i] / 100)))), 0, "R");
				
				$doc->Ln(1);
				
				$la_sum[0] += $x['s1'][$i] * $x['tax_1'][$i];
				$la_sum[1] += $x['s2'][$i] - ($x['s2'][$i] / (1 + $x['tax_1'][$i] / 100));
			}
			
			$y_pos = $doc->GetY();
			$doc->SetXY(10, $y_pos);
			$doc->MultiCell(30, 4, _convert('Sveukupno PDV:'), 0, "L");
			$doc->SetXY(40, $y_pos);
			$doc->MultiCell(15, 4, _convert(sprintf('%01.2f', $la_sum[0] / 100)), 0, "R");
			
			$doc->SetXY(70, $y_pos);
			$doc->MultiCell(30, 4, _convert('Sveukupno PDV:'), 0, "L");
			$doc->SetXY(100, $y_pos);
			$doc->MultiCell(15, 4, _convert(sprintf('%01.2f', $la_sum[1])), 0, "R");
		}
		
		
		
		$l_sql = 'select trosak1, trosak2, trosak3, uk_trosak, uk_nabavna_cijena, koef_troska '.
			' from receivings '.
			' where receivings.id = '.$id;
		$tr_data = get_data($l_sql);
		
		$doc->Ln(5);
		$y_pos = $doc->GetY();
		$doc->SetXY(10, $y_pos);
		$doc->MultiCell(260, 4, _convert('Trosak 1: '.$tr_data['trosak1'].' EUR   Trosak 2: '.$tr_data['trosak2'].' EUR   '.
			'Trosak 3: '.$tr_data['trosak3'].' EUR   UK Trosak: '.$tr_data['uk_trosak'].' EUR    '.
			'Nabav. cijena: '.$tr_data['uk_nabavna_cijena'].' EUR    Koef. Troska: '.$tr_data['koef_troska'].''), 0, "L");
		$doc->Ln(1);
		
		
		
		$doc->Cell(131);
		$doc->Cell(50,20,"Obradio",'B',0,'C');
		$doc->Ln();
	
		if($a_param['destination'] == 'screen'){
			
			$doc->output();
			die();
			
		}else{
			
			$l_name = 'inventura.pdf';
			$l_dest = $g_path.'app/webroot/res/files/'.$l_name;
			
			$doc->output($l_dest, 'F');
			
			return array('path' => $g_path.'app/webroot/res/files/', 'name' => $l_name);
		}
	}
	

	function all_print_medjus($a_param = array(), $id = null){
		
		$g_path = get_item('select value from settings where name = "putanja.abs" ');
			
		if(!defined('FPDF_FONTPATH')){
			define('FPDF_FONTPATH', 'font/');
		}
		
		//require_once($g_path.'app/pdf/fpdf.php');
		require_once($g_path.'app/libs/fpdf/fpdf.php');
		require_once($g_path.'app/libs/fpdf/fpdi.php');
		
		$doc =& new FPDI('L', 'mm', 'A4');
		
		$doc->AddFont("ArialCE", "B", "6488f5d4657b269b9f5770508e80e299_arialbd.php");
		$doc->AddFont("ArialCE", "", "332cc83ebee36613c19564e6e776c81b_arial.php");
		
		//$doc = new Document;
		
		$doc->AddPage('P');
		
		$doc->SetFont('Helvetica','B',12);
		$doc->Cell(100);
		$doc->Cell(0,5,_convert($a_param['restName']["Setting"]["value"]),0,2);
		$doc->SetFont('Helvetica','',8);
		
		$doc->Cell(0,5,_convert($a_param['restpost']["Setting"]["value"].' '.$a_param['restComm']["Setting"]["value"].','.
			$a_param['restStreet']["Setting"]["value"].' '.$a_param['restStrNum']["Setting"]["value"].$a_param['restStrNumMark']["Setting"]["value"]),0,2);
		
		$doc->SetFont('Helvetica','',8);
		$doc->Cell(0,5,_convert($a_param['restPlaceid']["Setting"]["value"]),0,0);
		
		
		
		if($id > 0){
			$x = get_data('select * from medjus where id = '.$id, 0);
		}else{
			$x['broj'] = '';
			$x['datum'] = '';
			$x['sa_skl'] = '';
			$x['customer_id'] = 0;
			$x['napomena'] = '';
			$l_customer = '';
		}
		
		$doc->Ln();
		$doc->Ln();
		$doc->Ln();
		$doc->SetFont('Helvetica','B',15);
		$doc->Cell(190, 8, 'MEDJUSKLADISNICA PROIZVODNJE BROJ: '.$x['broj'], 'B', 0, 'C');
		$doc->Ln();
		$doc->Ln();
		$doc->SetFont('Helvetica','',10);
		$doc->Cell(100, 5, 'Datum dokumenta: '.$x['datum'], '', 0, 'L');
		$doc->Ln();
		$doc->Cell(100, 5, 'Sa skladista: '.$x['sa_skl'], '', 0, 'L');
		$l_customer = get_item('select name from customers where id = '.$x['customer_id']);
		$doc->Cell(90, 5, 'Korisniku: '.$l_customer, '', 0, 'L');
		$doc->Ln();
		$doc->Cell(100, 5, 'Napomena: '.$x['napomena'], '', 0, 'L');
		$doc->Ln();
		$doc->Ln();
		
		
		if($id > 0){
			$header = array('Red.br', 'Naziv normativa', 'kol', 'NC', 'PC', 'Uk NC', 'Uk PC');
			$w = array(15, 80, 15, 18, 18, 18, 18);
			$pos = array('C', 'L', 'C', 'R', 'R', 'R', 'R');
			$pos_in = array('R', 'L', 'C', 'R', 'R', 'R', 'R');
			
			for($i = 0; $i < count($header); $i++){
				$doc->Cell($w[$i], 5, $header[$i], 'B', 0, $pos[$i]);
			}
			$doc->Ln(6);
			
			
			
			$l_kol = 0;
			$l_uk_nc = 0;
			$l_uk_pc = 0;
			$la_field = array('', 'product_id', 'kolicina', 'nc', 'pc', 'uk_nc', 'uk_pc');
			$l_sql = 'select product_id, kolicina, nc, pc, uk_nc, uk_pc from medju_items where medju_id = '.$id;
			$y = get_data($l_sql, 1);
			for($i = 0; $i < sizeof($y['product_id']); $i++){
				for($j = 0; $j < count($header); $j++){
					switch($j){
						case 0:
							$doc->Cell($w[$j], 5, ($i + 1).'.  ', '', 0, $pos_in[$j]);
							break;
						
						case 1:
							$l_product = get_item('select name from products where id = '.$y[$la_field[$j]][$i]);
							$doc->Cell($w[$j], 5, $l_product, '', 0, $pos_in[$j]);
							break;
						
						default:
							if($j == 2){
								$l_kol += $y[$la_field[$j]][$i];
							}
							if($j == 5){
								$l_uk_nc += $y[$la_field[$j]][$i];
							}
							if($j == 6){
								$l_uk_pc += $y[$la_field[$j]][$i];
							}
							$doc->Cell($w[$j], 5, $y[$la_field[$j]][$i], '', 0, $pos_in[$j]);
					}
				}
				$doc->Ln();
			}
			
			$header = array('Ukupno:', sprintf('%01.4f', $l_kol), '', sprintf('%01.2f', $l_uk_nc), sprintf('%01.2f', $l_uk_pc));
			$w = array(95, 15, 36, 18, 18);
			$pos = array('L', 'C', 'L', 'R', 'R');
			
			for($i = 0; $i < count($header); $i++){
				$doc->Cell($w[$i], 5, $header[$i], 'T', 0, $pos[$i]);
			}
			$doc->Ln(6);
					
			$doc->Ln();
			$doc->Ln();
			$doc->Ln();
					
			$doc->SetFont('Helvetica','B',15);
			$doc->Cell(190, 8, 'IZRACUN MATERIJALA (UTROSENIH ARTIKALA)', 'B', 0, 'C');
			$doc->Ln();
			$doc->Ln();
			
			$doc->SetFont('Helvetica','',10);
			$header = array('Red.br', 'Naziv artikla', 'kol', 'j.mj.', 'NC', 'Uk NC');
			$w = array(15, 80, 15, 15, 18, 18);
			$pos = array('C', 'L', 'C', 'C', 'R', 'R');
			$pos_in = array('R', 'L', 'C', 'C', 'R', 'R');
			
			for($i = 0; $i < count($header); $i++){
				$doc->Cell($w[$i], 5, $header[$i], 'B', 0, $pos[$i]);
			}
			$doc->Ln(6);
			
			
			
			$l_kol = 0;
			$l_uk_nc = 0;
			$la_field = array('', 'article_id', 'kolicina', 'unit', 'nc', 'uk_nc');
			$l_sql = 'select product_items.article_id, articles.name, sum(medju_items.kolicina * product_items.amount) as kolicina, articles.unit, articles.price_buy as nc, 0 as uk_nc '.
				' from medju_items, products, product_items, articles '.
				' where medju_items.product_id = products.id '.
				' and products.id = product_items.product_id '.
				' and product_items.article_id = articles.id '.
				' and medju_items.medju_id = '.$id.
				' group by product_items.article_id, articles.name, articles.unit, articles.price_buy '.
				' order by articles.name, product_items.article_id ';
			$y = get_data($l_sql, 1);
			for($i = 0; $i < sizeof($y['article_id']); $i++){
				for($j = 0; $j < count($header); $j++){
					switch($j){
						case 0:
							$doc->Cell($w[$j], 5, ($i + 1).'.  ', '', 0, $pos_in[$j]);
							break;
						
						case 1:
							$l_article = get_item('select name from articles where id = '.$y[$la_field[$j]][$i]);
							$doc->Cell($w[$j], 5, $l_article, '', 0, $pos_in[$j]);
							break;
							
						default:
							if($j == 2){
								$l_kol += $y[$la_field[$j]][$i];
							}
							if($j == 4){
								$y[$la_field[5]][$i] = $y[$la_field[2]][$i] * $y[$la_field[$j]][$i];
								$l_uk_nc += $y[$la_field[5]][$i];
							}
							$doc->Cell($w[$j], 5, $y[$la_field[$j]][$i], '', 0, $pos_in[$j]);
					}
				}
				$doc->Ln();
			}
					
			$header = array('Ukupno:', '', sprintf('%01.2f', $l_uk_nc));
			$w = array(95, 48, 18);
			$pos = array('L', 'C', 'R');
			
			for($i = 0; $i < count($header); $i++){
				$doc->Cell($w[$i], 5, $header[$i], 'T', 0, $pos[$i]);
			}
			$doc->Ln(6);
		}
		
		$doc->Ln();
		$doc->Ln();
		$doc->Ln();
		$doc->Ln();
		$doc->Cell(10);
		$doc->Cell(60, 20, 'Izradio', 'B', 0, 'C');
		$doc->Cell(40);
		$doc->Cell(60, 20, 'Odobrio', 'B', 0, 'C');
		$doc->Ln();
		
		if($a_param['destination'] == 'screen'){
			
			$doc->output();
			die();
			
		}else{
			
			$l_name = 'medjuskladisnica-proizvodnja.pdf';
			$l_dest = $g_path.'app/webroot/res/files/'.$l_name;
			
			$doc->output($l_dest, 'F');
			
			return array('path' => $g_path.'app/webroot/res/files/', 'name' => $l_name);
		}
	}

	function all_print_skladiste($a_param = array(), $id = null){
		
		$g_path = get_item('select value from settings where name = "putanja.abs" ');
			
		if(!defined('FPDF_FONTPATH')){
			define('FPDF_FONTPATH', 'font/');
		}
		
		require_once($g_path.'app/libs/fpdf/fpdf.php');
		require_once($g_path.'app/libs/fpdf/fpdi.php');
		
		$doc =& new FPDI('L', 'mm', 'A4');
		
		$doc->AddFont("ArialCE", "B", "6488f5d4657b269b9f5770508e80e299_arialbd.php");
		$doc->AddFont("ArialCE", "", "332cc83ebee36613c19564e6e776c81b_arial.php");
		
		$doc->AddPage('L');
	    
		$doc->SetFont('Helvetica','B',12);
		$doc->Cell(100);
		$doc->Cell(0,5,_convert($a_param['restName']["Setting"]["value"]),0,2);
		$doc->SetFont('Helvetica','',8);
		
		$doc->Cell(0,5,_convert($a_param['restpost']["Setting"]["value"].' '.$a_param['restComm']["Setting"]["value"].','.
			$a_param['restStreet']["Setting"]["value"].' '.$a_param['restStrNum']["Setting"]["value"].$a_param['restStrNumMark']["Setting"]["value"]),0,2);
		
		$doc->SetFont('Helvetica','',8);
		$doc->Cell(0,5,_convert($a_param['restPlaceid']["Setting"]["value"]),0,0);
		
		$rec = $a_param['rec'];
		
		$doc->Ln();
		$doc->SetFont('Helvetica','B',12);  
		$doc->Cell(80,8,$rec["Supplier"]["name"],'LTR',0);
	    $doc->Cell(20);
		$doc->SetFont('Helvetica','B',10);
        $doc->Cell(80,8, _convert('Skladišni dokument ').$rec["Receiving"]["broj_povratnica"],'LTRB',0,'C');

		$dt = date('d.m.Y', strtotime($rec["Receiving"]["created"]));

		$doc->Ln();
		$doc->SetFont('Helvetica','B',8);  
		$doc->Cell(80,5,'  ','LR',0);
	    $doc->Cell(20);
	    $doc->SetFont('Courier','',8);
		$doc->Cell(80,5,"Broj dokumenta:    ".$rec["Receiving"]["number"],'LR',0);
		
		$doc->Ln();
		$doc->SetFont('Helvetica','B',8);  
		$doc->Cell(80,5,$rec["Supplier"]["address"].' '.$rec["Supplier"]["city"],'LR',0);
	    $doc->Cell(20);
	    $doc->SetFont('Courier','',8);
		$doc->Cell(80,5,"Datum dokumenta:   ".$dt,'LBR',0);
		$doc->Ln();
		
		$doc->SetFont('Helvetica','',8);  
		$doc->Cell(80,5,"OIB: ".$rec["Supplier"]["oib"],'LRB',0,'R');
		$doc->Ln(10);
	    
		$doc->SetFont('Helvetica','B',8);
		
		$doc->SetFillColor(255,255,255);

		$l_pdv = get_item('select value from settings where name = "duty.free" ');
		if($l_pdv == 'no'){
			$l_pdv = 'false';
		}
		
		if($l_pdv == 'false'){
			$header = array('Red.br','Sifra','Naziv artikla/usluge', 'Kol (+/-)', 'Nab. Cj.', 'Nab.Vrijed.', 'Rabat%', 'Iz.Rabat', 'Fakt.Cj.', 'Fakt. Vrijed.','MPC' ,'Uk cijena');
			$w = array(10,20,50,12,15,15,10,15,15,18,18,18,18,20);
		}else{
			$header = array('Red.br','Sifra','Naziv artikla/usluge', 'Kol (+/-)', 'Nab. Cj.', 'Nab.Vrijed.', 'Rabat%', 'Iz.Rabat', 'Fakt.Cj.', 'Fakt. Vrijed.', 'PDV%', 'Iz.PDV', 'MPC' ,'Uk cijena');
			$w = array(10,20,50,12,15,15,10,15,15,18,18,18,10,18,18,20);
		}
		
		for($i=0;$i<count($header);$i++)
			$doc->Cell($w[$i],7,_convert($header[$i]),0,0,'C',true);
		$doc->Ln();
	
		$fill = false;
		
		if($id == 0){
			$a_param['items'] = array();
		}
		
		$doc->SetFont('Helvetica','',8);
		$priceBuy = 0;
		$priceSell= 0;
		$fakt =0;
		$price = 0;
		$marza = 0;
		$pdv = 0;
		$discount = 0;
		$redbr = 1;
		foreach($a_param['items'] as $item){
			
			$iid = $item["Article"]["tax_group_id"];
			$taxGroup = array();
			$taxGroup["TaxGroup"] = get_data('select id, name, tax_1, tax_2 from tax_groups where id = '.$iid, 0);
			
			$doc->Cell($w[0],6,$redbr,0,0,'C',$fill);
			$doc->Cell($w[1],6,$item["Article"]["barcode"],0,0,'L',$fill);
			
			$posY = $doc->GetY();
			$posX = $doc->GetX();
			$nl = 0;
			if(strlen($item["Article"]["name"])>40)
			{
				$temptext = str_split($item["Article"]["name"],40);
				foreach($temptext as $newline) {
					$doc->Cell($w[2],3,_convert($newline),0,2,'L');
					$nl += 1;
				}
				$posX += $w[2];
				$doc->SetY($posY);
				$posY +=  $nl*3;
			}
			else
			{
				$doc->Cell($w[2],6,_convert($item["Article"]["name"]),0,0,'L',$fill);	
				$posX = $doc->GetX();
				$posY +=  6;
			}
			
			$doc->SetX($posX);
			
			$doc->Cell($w[3],6,number_format($item["ReceivingItem"]["amount"],2, '.', ' '),0,0,'R',$fill);
			$doc->Cell($w[4],6,number_format($item["ReceivingItem"]["price_buy"],2, '.', ' '),0,0,'R',$fill);
			$doc->Cell($w[5],6,number_format($item["ReceivingItem"]["price_buy"]*$item["ReceivingItem"]["amount"],2, '.', ' '),0,0,'R',$fill);
			$doc->Cell($w[6],6,number_format($item["ReceivingItem"]["discount"]),0,0,'R',$fill); 
			$doc->Cell($w[7],6,number_format(($item["ReceivingItem"]["discount"])*$item["ReceivingItem"]["price_buy"]*$item["ReceivingItem"]["amount"]/100,2, '.', ' '),0,0,'R',$fill);
			$doc->Cell($w[8],6,number_format($item["ReceivingItem"]["price_buy"]*(1- ($item["ReceivingItem"]["discount"]/100)),2, '.', ' '),0,0,'R',$fill);
			$doc->Cell($w[9],6,number_format($item["ReceivingItem"]["price_buy"]*(1- ($item["ReceivingItem"]["discount"]/100))*$item["ReceivingItem"]["amount"],2, '.', ' '),0,0,'R',$fill);
			
			if($l_pdv == 'false'){
				$doc->Cell($w[10],6,number_format($item["ReceivingItem"]["price_sell"],2, '.', ' '),0,0,'R',$fill);
				$doc->Cell($w[11],6,number_format($item["ReceivingItem"]["amount"]*$item["ReceivingItem"]["price_sell"],2, '.', ' '),0,0,'R',$fill);
			}else{
				$doc->Cell($w[10],6,number_format($taxGroup["TaxGroup"]["tax_1"]),0,0,'R',$fill);
				$doc->Cell($w[11],6,number_format($item["ReceivingItem"]["price_sell"]/(1 + $taxGroup["TaxGroup"]["tax_1"]/100)* $taxGroup["TaxGroup"]["tax_1"] /100*$item["ReceivingItem"]["amount"] ,2, '.', ' '),0,0,'R',$fill);
				$doc->Cell($w[12],6,number_format($item["ReceivingItem"]["price_sell"],2, '.', ' '),0,0,'R',$fill);
				$doc->Cell($w[13],6,number_format($item["ReceivingItem"]["amount"]*$item["ReceivingItem"]["price_sell"],2, '.', ' '),0,0,'R',$fill);
			}
			
			$doc->Ln();
			
			$priceBuy = $priceBuy + $item["ReceivingItem"]["price_buy"]*$item["ReceivingItem"]["amount"];
			$discount = $discount + (($item["ReceivingItem"]["discount"])*$item["ReceivingItem"]["price_buy"]*$item["ReceivingItem"]["amount"]/100);
			$fakt = $fakt + $item["ReceivingItem"]["price_buy"]*(1- ($item["ReceivingItem"]["discount"]/100))*$item["ReceivingItem"]["amount"];
			$marza = $marza + ($item["ReceivingItem"]["price_sell"]/(1+$taxGroup["TaxGroup"]["tax_1"]/100)- ($item["ReceivingItem"]["price_buy"]*(1- ($item["ReceivingItem"]["discount"]/100)))) *$item["ReceivingItem"]["amount"];
			$pdv = $pdv + $item["ReceivingItem"]["price_sell"]/(1 + $taxGroup["TaxGroup"]["tax_1"]/100)* $taxGroup["TaxGroup"]["tax_1"] /100*$item["ReceivingItem"]["amount"];
			$price = $price + $item["ReceivingItem"]["price_sell"]*$item["ReceivingItem"]["amount"];
			
			$redbr = $redbr+1;
			$doc->SetY($posY);
		}
		
		$doc->Cell(array_sum($w) - 36,0,'','T');
		$doc->Ln();
		$doc->Cell($w[0]+$w[1]+$w[2]+$w[3]+$w[4],6,'',0,0,'L',$fill);
		$doc->Cell($w[5],6,number_format($priceBuy,2, '.', ' '),0,0,'R',$fill);
		$doc->Cell($w[6],6,'',0,0,'R',$fill);
		$doc->Cell($w[7],6,number_format($discount,2, '.', ' '),0,0,'R',$fill);
		$doc->Cell($w[8],6,'',0,0,'R',$fill);
		$doc->Cell($w[9],6,number_format($fakt,2, '.', ' '),0,0,'R',$fill);
		
		if($l_pdv == 'false'){
			$doc->Cell($w[10],6,'',0,0,'R',$fill);
			$doc->Cell($w[11],6,number_format($price,2, '.', ' '),0,0,'R',$fill);
		}else{
			$doc->Cell($w[10],6,'',0,0,'R',$fill);
			$doc->Cell($w[11],6,number_format($pdv,2, '.', ' '),0,0,'R',$fill);
			$doc->Cell($w[12],6,'',0,0,'R',$fill);
			$doc->Cell($w[13],6,number_format($price,2, '.', ' '),0,0,'R',$fill);
		}
		
		$doc->Ln();
		$doc->Cell(array_sum($w) - 36,0,'','T');
		$doc->Ln(6);
		
		$doc->Cell(220);
		$doc->Cell(50,20,"Obradio",'B',0,'C');
		$doc->Ln();
		
		if($a_param['destination'] == 'screen'){
			
			$doc->output();
			die();
			
		}else{
			
			$l_name = 'skladiste.pdf';
			$l_dest = $g_path.'app/webroot/res/files/'.$l_name;
			
			$doc->output($l_dest, 'F');
			
			return array('path' => $g_path.'app/webroot/res/files/', 'name' => $l_name);
		}
	}

?>