<?php
	
	
	if(!function_exists('get_n_rows')){
		require('get_n_rows_file.php');
	}
	
	
	function get_valuta(){
		$result = get_item('select value from settings where name = "general.currency" ');
		if(!$result){
			$result = 'Kn';
		}
		
		return $result;
	}
	
	function all_dnevni_promet($date, $vrsta_izvjesca, $user_id){
		
		$g_path = get_item('select value from settings where name = "putanja.abs" ');
		
		$g_v = get_valuta();
		
		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');
		
		$pdf =& new FPDI('P', 'mm', 'A4');
		
		$pdf->AddFont("ArialCE", "B", "6488f5d4657b269b9f5770508e80e299_arialbd.php");
		$pdf->AddFont("ArialCE", "", "332cc83ebee36613c19564e6e776c81b_arial.php");
		
		// set the sourcefile
		$pgNumber = $pdf->setSourceFile($g_path.'app/libs/fpdf/prazni-dokument.pdf');
		
		//$pdf->SetAutoPageBreak(true, 10);
		
		
		$l_pdv = get_item('select value from settings where name = "duty.free" ');
		
		if($l_pdv == 'no'){
			$l_pdv = 'false';
		}
		
		if($l_pdv == 'false'){
			$l_pdv_koment = get_item('select value from settings where name = "duty.comment" ');
		}else{
			$l_pdv_koment = '';
		}
		
		
		$x = get_item('select value from settings where name = "general.name" ').', '.
			get_n_rows(get_item('select value from settings where name = "invoice.header" '), 4);
		
		$la_tmp = explode('-', $date);
		
		$l_datum = $la_tmp[0].'.'.$la_tmp[1].'.'.$la_tmp[2].'.';
		
		$l_datum_db = $la_tmp[2].'-'.$la_tmp[1].'-'.$la_tmp[0];
		$l_datum_from = $l_datum_db.' 00:00:00';
		$l_datum_to = $l_datum_db.' 23:59:59';
		
		
		$la_uvjet = array('1', '2, 3, 4, 5, 7', '1, 2, 3, 4, 5, 7', '8', '6');
		$la_card_details = array('0', '1', '0', '0', '0');
		$la_skip_sum = array('0', '0', '1', '0', '0');
		
		$l_tip_prog = get_item('select value from settings where name = "tipprog" ');
		
		if($l_tip_prog == 'm'){
			$la_type_desc = array('Novčanice/Kovanice', 'Kartice', 'Gotovina (Novčanice+Kartice)', 'Ostalo', 'Transakcijski račun (Virman)');
			
			$la_tax_desc = array(
				'GOTOVINSKI PROMET (Novčanice/Kovanice)',
				'GOTOVINSKI PROMET (Kartice)',
				'Ukupno Gotovina (Novčanice+Kartice)',
				'PROMET OSTALO',
				'PROMET TRANSAKCIJSKI RAČUN (VIRMAN)',
			);
		}
		if($l_tip_prog == 'u'){
			$la_type_desc = array('Gotovina - novčanice', 'Gotovina - kartice', 'Gotovina (Novčanice+Kartice)', 'Ostalo', 'Transakcijski račun (Virman)');
			
			$la_tax_desc = array(
				'GOTOVINSKI PROMET (Novčanice)',
				'GOTOVINSKI PROMET (Kartice)',
				'Ukupno Gotovina (Novčanice+Kartice)',
				'PROMET OSTALO',
				'PROMET TRANSAKCIJSKI RAČUN (VIRMAN)',
			);
		}
		
		
		$la_tax = array();
		$la_pn = array();
		
		for($k = 0; $k < sizeof($la_uvjet); $k++){
			$la_tax[$k] = array();
			$la_pn[$k] = 0;
		}
		
		$la_tax_ukup = array();
		$la_pn_ukup = 0;
		$la_cards = array();
			
		
		
		$l_min_rbr = 99999999999;
		$l_max_rbr = 0;
		
		$l_num_format = '';
			
		for($k = 0; $k < sizeof($la_uvjet); $k++){
			$y = get_data('select * from sales where payment_type_id in ('.$la_uvjet[$k].') '.
				' and created between "'.$l_datum_from.'" and "'.$l_datum_to.'" ', 1);
			//print 'select * from sales where payment_type_id in ('.$la_uvjet[$k].') and created between "'.$l_datum_from.'" and "'.$l_datum_to.'" '."\n";
			$l_id = '0';
			for($i = 0; isset($y['id']) && $i < sizeof($y['id']); $i++){
				$l_id .= ', '.$y['id'][$i];
				
				if($l_num_format == ''){
					$l_num_format = $y['number'][0];
				}
					
				$la_number = explode('-', $y['number'][$i]);
				if($la_number[0] < $l_min_rbr){
					$l_min_rbr = $la_number[0];
				}
				if($la_number[0] > $l_max_rbr){
					$l_max_rbr = $la_number[0];
				}
				
				if($la_card_details[$k] == '1'){
					if(empty($la_cards[$y['payment_type_id'][$i]])){
						$la_cards[$y['payment_type_id'][$i]] = 0;
					}
					$la_cards[$y['payment_type_id'][$i]] += $y['price'][$i];
				}
			}
			
			
			
			
			
			if($l_tip_prog == 'm'){
				$z = get_data('select * from retail_items where sale_id in ('.$l_id.') order by sale_id, id ', 1);
				
				$l_item = '';
				
				for($i = 0; isset($z['id']) && $i < sizeof($z['id']); $i++){
					if($l_pdv == 'false'){
						$z['tax_1'][$i] = 'no_pdv';
					}
					
					if(empty($la_tax[$k][$z['tax_1'][$i]])){
						$la_tax[$k][$z['tax_1'][$i]] = 0;
					}
					$la_tax[$k][$z['tax_1'][$i]] += $z['price'][$i] - $z['feedback_compensation'][$i];
					
					if(empty($la_tax_ukup[$z['tax_1'][$i]])){
						$la_tax_ukup[$z['tax_1'][$i]] = 0;
					}
					if($la_skip_sum[$k] == '0'){
						$la_tax_ukup[$z['tax_1'][$i]] += $z['price'][$i] - $z['feedback_compensation'][$i];
					}
					
					if(empty($la_pn[$k])){
						$la_pn[$k] = 0;
					}
					$la_pn[$k] += $z['feedback_compensation'][$i];
					
					if($la_skip_sum[$k] == '0'){
						$la_pn_ukup += $z['feedback_compensation'][$i];
					}
				}
			}
			
			if($l_tip_prog == 'u'){
				
				//print $l_id."<br>";
				
				/*
				$z = get_data('select tax_groups.id, tax_groups.name, sale_items.sale_id, sale_items.amount, sale_items.base_price, '.
					' sale_items.discount_percent, sale_items.price, sale_items.tax_1, sale_items.tax_2 '.
					' from sale_items, products, tax_groups '.
					' where sale_items.product_id = products.id '.
					' and products.tax_group_id = tax_groups.id '.
					' and sale_items.sale_id in ('.$l_id.')  '.
					' order by sale_items.sale_id, sale_items.id', 1);
				*/
				
				if($user_id > 0){
					$l_cond = ' and sales.user_id = '.$user_id;
				}else{
					$l_cond = '';
				}
				
				$z = get_data('select tax_groups.id, tax_groups.name, sale_items.sale_id, sale_items.amount, sale_items.base_price, '.
					' sale_items.discount_percent, sale_items.price, sale_items.tax_1, sale_items.tax_2 '.
					' from sales, sale_items, products, tax_groups '.
					' where sales.id = sale_items.sale_id '.
					' and sale_items.product_id = products.id '.
					' and products.tax_group_id = tax_groups.id '.
					' and sale_items.sale_id in ('.$l_id.')  '.
					$l_cond.
					' order by sale_items.sale_id, sale_items.id', 1);
				
				$l_item = '';
				
				for($i = 0; isset($z['id']) && $i < sizeof($z['id']); $i++){
					if($l_pdv == 'false'){
						$z['tax_1'][$i] = 'no_pdv';
					}
					
					
					if(empty($la_tax[$k][$z['name'][$i]]['tax_1'][$z['tax_1'][$i]])){
						$la_tax[$k][$z['name'][$i]]['tax_1'][$z['tax_1'][$i]] = 0;
					}
					$la_tax[$k][$z['name'][$i]]['tax_1'][$z['tax_1'][$i]] += $z['price'][$i];
					
					//if($z['tax_2'][$i] > 0){
						if(empty($la_tax[$k][$z['name'][$i]]['tax_2'][$z['tax_1'][$i]][$z['tax_2'][$i]])){
							$la_tax[$k][$z['name'][$i]]['tax_2'][$z['tax_1'][$i]][$z['tax_2'][$i]] = 0;
						}
						$la_tax[$k][$z['name'][$i]]['tax_2'][$z['tax_1'][$i]][$z['tax_2'][$i]] += $z['price'][$i];
					//}
					
					
					
					if(empty($la_tax_ukup[$z['name'][$i]]['tax_1'][$z['tax_1'][$i]])){
						$la_tax_ukup[$z['name'][$i]]['tax_1'][$z['tax_1'][$i]] = 0;
					}
					if($la_skip_sum[$k] == '0'){
						$la_tax_ukup[$z['name'][$i]]['tax_1'][$z['tax_1'][$i]] += $z['price'][$i];
					}							
					
					//if($z['tax_2'][$i] > 0){
						if(empty($la_tax_ukup[$z['name'][$i]]['tax_2'][$z['tax_1'][$i]][$z['tax_2'][$i]])){
							$la_tax_ukup[$z['name'][$i]]['tax_2'][$z['tax_1'][$i]][$z['tax_2'][$i]] = 0;
						}
						if($la_skip_sum[$k] == '0'){
							$la_tax_ukup[$z['name'][$i]]['tax_2'][$z['tax_1'][$i]][$z['tax_2'][$i]] += $z['price'][$i];
						}
					//}
					
					
					
					if(empty($la_pn[$k])){
						$la_pn[$k] = 0;
					}
					//$la_pn[$k] += $z['feedback_compensation'][$i];
					
					if($la_skip_sum[$k] == '0'){
						//$la_pn_ukup += $z['feedback_compensation'][$i];
					}
				}
				
				
			}
		}
		
		
		
		
		
		
		if($l_min_rbr == 99999999999){
			$l_min_rbr = '-';
		}
		if($l_max_rbr == 0){
			$l_max_rbr = '-';
		}
		
		
		if($l_num_format == ''){
			if($l_min_rbr == 99999999999){
				$l_min_rbr = '-';
			}
			if($l_max_rbr == 0){
				$l_max_rbr = '-';
			}
			
		}else{
			
			$la_number = explode('-', $l_num_format);
			for($j = 1; $j < sizeof($la_number); $j++){
				$l_min_rbr .= '-'.$la_number[$j];
			}
			for($j = 1; $j < sizeof($la_number); $j++){
				$l_max_rbr .= '-'.$la_number[$j];
			}
		}
		
		
		
		
		
		if($l_tip_prog == 'u'){
			for($i = 1; $i <= 1; $i++){
				// add a page
				$pdf->AddPage();
				
				// import page 1
				$tplIdx = $pdf->importPage($i);
				
				// use the imported page and place it at point 0,0 with a width of 210 mm
				$pdf->useTemplate($tplIdx, 0, 0, 210, 297);
				
				
				if($vrsta_izvjesca == ''){
					$l_vrsta_izvjesca = '';
				}else{
					$l_vrsta_izvjesca = '('.$vrsta_izvjesca.')';
				}
				
				$pdf->SetFont("ArialCE", "B", 11);
				$pdf->SetTextColor(0, 0, 0);
				
				$pdf->SetXY(10, 10);
				$pdf->MultiCell(210, 4, _convert($x), 0, "L");
				
				$pdf->SetFont("ArialCE", "B", 10);
				
				$pdf->SetXY(10, 33);
				$pdf->MultiCell(210, 4, _convert('Dnevni promet '.$l_vrsta_izvjesca), 0, "L");
				
				$pdf->SetXY(10, 37);
				$pdf->MultiCell(210, 4, _convert('Period od '.$l_datum.' do '.$l_datum), 0, "L");
				
				$pdf->SetXY(10, 41);
				$pdf->MultiCell(210, 4, _convert('Brojevi računa od '.$l_min_rbr.' do '.$l_max_rbr), 0, "L");
				
				if($user_id > 0){
					
					$l_sql = 'select concat(full_name, ", ", username) as username '.
						' from users '.
						' where id =  '.$user_id;
					$l_user_name = get_item($l_sql);
					
					
					$pdf->SetXY(10, 45);
					$pdf->MultiCell(210, 4, _convert('OPERATER: '.$l_user_name), 0, "L");
				}
				
				$pdf->SetFont("ArialCE", "B", 11);
				
				
				
				
				$pdf->SetFont("ArialCE", "B", 10);
				$pdf->SetXY(10, 52);
				$pdf->MultiCell(210, 5, _convert('PROMETI PO NAČINIMA PLAĆANJA:'), 0, "L");
				$pdf->SetXY(10, 56);
				$pdf->MultiCell(210, 5, _convert('-------------------------------------------------------------------------'), 0, "L");
				
				$pdf->SetFont("ArialCE", "", 9);
				
				
				
				$la_ukup_sve = array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
				$l_ukup_sve_sve = 0;
				
				for($k = 0; $k < sizeof($la_uvjet); $k++){
					foreach($la_tax[$k] as $key => $value){
						foreach($value['tax_1'] as $key2 => $value2){
							$la_ukup_sve[$k] += $value2;
						}
					}
				}
				for($k = 0; $k < sizeof($la_uvjet); $k++){
					if($k != 2){
						$l_ukup_sve_sve += $la_ukup_sve[$k]; 
					}
				}
				
				
				
				$l_cnt = 0;
				
				//$pdf->SetXY(10, 60 + 4 * $l_cnt);
				//$pdf->MultiCell(50, 5, _convert($la_type_desc[2]), 0, "L");
				
				//$pdf->SetXY(67, 60 + 4 * $l_cnt);
				//$pdf->MultiCell(30, 5, _convert(sprintf('%01.2f', $la_ukup_sve[2]).' Kn'), 0, "R");
				
				//$l_cnt++;
				
				//p($la_type_desc);
				
				for($k = 0; $k < sizeof($la_type_desc); $k++){
					if($k == 2){
						continue;
					}
					
					//p($la_type_desc[$k]);
					//p($la_ukup_sve[$k]);
					//p('--');
					$pdf->SetXY(10, 60 + 4 * $l_cnt);
					$pdf->MultiCell(50, 5, _convert($la_type_desc[$k]), 0, "L");
					
					$pdf->SetXY(67, 60 + 4 * $l_cnt);
					$pdf->MultiCell(30, 5, _convert(sprintf('%01.2f', $la_ukup_sve[$k]).' '.$g_v), 0, "R");
					
					$l_cnt++;
					
					
					if($k == 1){
						$pdf->SetFont("ArialCE", "", 8);
						$x_card = get_data('select id, name from payment_types where type = 1 and enabled = 1 order by id ', 1);
						for($kk = 0; $kk < sizeof($x_card['id']); $kk++){
							$pdf->SetXY(12, 60 + 4 * $l_cnt);
							$pdf->MultiCell(140, 4, _convert($x_card['name'][$kk].': '.sprintf('%01.2f', $la_cards[$x_card['id'][$kk]]).' '.$g_v), 0, "L");
							$l_cnt++;
						}
						$pdf->SetFont("ArialCE", "", 9);
					}
					
				}
				
				$pdf->SetXY(10, 60 + 4 * $l_cnt);
				$pdf->MultiCell(210, 5, _convert('---------------------------------------------------------------------------------'), 0, "L");
				
				$pdf->SetXY(10, 63 + 4 * $l_cnt);
				$pdf->MultiCell(210, 5, _convert('SVEUKUPNO'), 0, "L");
				$pdf->SetXY(67, 63 + 4 * $l_cnt);
				$pdf->MultiCell(30, 5, _convert(sprintf('%01.2f', $l_ukup_sve_sve).' '.$g_v), 0, "R");
				
				$pdf->SetXY(10, 66 + 4 * $l_cnt);
				$pdf->MultiCell(210, 5, _convert('---------------------------------------------------------------------------------'), 0, "L");
				
				
				
				$pdf->SetFont("ArialCE", "B", 10);
				$l_cnt++;
				$l_cnt++;
				$l_cnt++;
				$pdf->SetXY(10, 66 + 4 * $l_cnt);
				$pdf->MultiCell(210, 5, _convert('PROMETI I POREZNA RAZRADA:'), 0, "L");
				
				$pdf->SetFont("ArialCE", "", 9);
				
				
				
				
				
				
				$l_cnt = 12 + sizeof($la_type_desc);
				
				$l_ukup_sve = 0;
				
				$la_rekapitulacija = array();
				
				$l_pnp_ukup = 0;
				$l_ukup_sve_sve = 0;
				
				for($k = 0; $k < sizeof($la_uvjet); $k++){
					
					if(50 + 4 * ($l_cnt + 3 + 1) > 190){
						$pdf->AddPage();
						$l_cnt = -8;
					}
					
					$pdf->SetFont("ArialCE", "", 9);
					
					$pdf->SetXY(10, 50 + 4 * $l_cnt);
					$pdf->MultiCell(210, 4, _convert('--------------------------------------------------------------------------------------------------------------------------------------------'), 0, "L");
					
					$pdf->SetXY(10, 50 + 4 * ($l_cnt + 1));
					$pdf->MultiCell(120, 4, _convert($la_tax_desc[$k]), 0, "L");
					
					if($k == 1){
						$pdf->SetXY(58, 50 + 4 * ($l_cnt + 1));
						$pdf->MultiCell(40, 4, _convert(sprintf('%01.2f', $la_ukup_sve[$k]).' '.$g_v), 0, "L");
						
						
						$pdf->SetFont("ArialCE", "", 8);
						$x_card = get_data('select id, name from payment_types where type = 1 and enabled = 1 order by id ', 1);
						for($kk = 0; $kk < sizeof($x_card['id']); $kk++){
							$l_cnt++;
							$pdf->SetXY(12, 50 + 4 * ($l_cnt + 1));
							$pdf->MultiCell(140, 4, _convert($x_card['name'][$kk].': '.sprintf('%01.2f', $la_cards[$x_card['id'][$kk]]).' '.$g_v), 0, "L");
						}
						$pdf->SetFont("ArialCE", "", 9);
					}
					
					
					$pdf->SetFont("ArialCE", "", 9);
					$pdf->SetXY(10, 50 + 4 * ($l_cnt + 2));
					$pdf->MultiCell(210, 4, _convert('--------------------------------------------------------------------------------------------------------------------------------------------'), 0, "L");
					
					
					
					$pdf->SetFont("ArialCE", "B", 8);
					$pdf->SetXY(10, 50 + 4 * ($l_cnt + 3));
					$pdf->MultiCell(35, 4, _convert('Opis'), 0, "L");
					
					$pdf->SetXY(50, 50 + 4 * ($l_cnt + 3));
					$pdf->MultiCell(19, 4, _convert('Osnovica'), 0, "L");
					
					$pdf->SetXY(70, 50 + 4 * ($l_cnt + 3));
					$pdf->MultiCell(14, 4, _convert('PDV'), 0, "L");
					
					$pdf->SetXY(85, 50 + 4 * ($l_cnt + 3));
					$pdf->MultiCell(19, 4, _convert('PDV '.$g_v), 0, "L");
					
					$pdf->SetXY(105, 50 + 4 * ($l_cnt + 3));
					$pdf->MultiCell(14, 4, _convert('PNP'), 0, "L");
					
					$pdf->SetXY(120, 50 + 4 * ($l_cnt + 3));
					$pdf->MultiCell(19, 4, _convert('PNP '.$g_v), 0, "L");
						
					$pdf->SetXY(140, 50 + 4 * ($l_cnt + 3));
					$pdf->MultiCell(25, 4, _convert('Ukupno'), 0, "L");
					
					
					
					$pdf->SetFont("ArialCE", "", 8);
					
					$l_ukup_sve = 0;
					$la_ukup = array();
					
					$l_osnovica_sum = 0;
					$l_pdv_kn = 0;
					$l_pnp_kn = 0;
					
					foreach($la_tax[$k] as $key => $value){
						foreach($value['tax_1'] as $key2 => $value2){
							
							$l_ukup_porez = 0;
							
							$pdf->SetXY(10, 50 + 4 * ($l_cnt + 3 + 1));
							$pdf->MultiCell(35, 4, _convert($key), 0, "L");
							
							$l_ukup_porez += $key2;
							
							$l_key_3_move = 0;
							// zbog strukture podataka, samo jedan zapis ima (iako koristim foreach petlju, najbrze mi bilo iskoristit tu petlju)
							foreach($la_tax[$k][$key]['tax_2'][$key2] as $key3 => $value3){
								
								$l_key_3_move = $key3;
								
								$pdf->SetXY(105, 50 + 4 * ($l_cnt + 3 + 1));
								$pdf->MultiCell(14, 4, _convert($key3), 0, "L");
								
								$l_ukup_porez += $key3;
								
								
								if($k != 2){
									if(empty($la_rekapitulacija[$key2])){
										$la_rekapitulacija[$key2] = array();
										$la_rekapitulacija[$key2]['pdv'] = 0;
										$la_rekapitulacija[$key2]['pnp'] = array();
									}
								}
								
								if(empty($la_ukup[$key2])){
									$la_ukup[$key2] = array();
									$la_ukup[$key2]['pdv'] = 0;
									$la_ukup[$key2]['pnp'] = array();
								}
								
								$l_osnovica = $value3 / (1 + ($key2 + $key3) / 100);
									
								$la_ukup[$key2]['pdv'] += $l_osnovica;
								if(empty($la_ukup[$key2]['pnp'][$key3])){
									$la_ukup[$key2]['pnp'][$key3] = 0;
								}
								$la_ukup[$key2]['pnp'][$key3] += $l_osnovica;
								
								if($k != 2){
									$la_rekapitulacija[$key2]['pdv'] += $l_osnovica;
									if(empty($la_rekapitulacija[$key2]['pnp'][$key3])){
										$la_rekapitulacija[$key2]['pnp'][$key3] = 0;
									}
									$la_rekapitulacija[$key2]['pnp'][$key3] += $l_osnovica;
								}
								
							}
							
							$pdf->SetXY(46, 50 + 4 * ($l_cnt + 3 + 1));
							$pdf->MultiCell(19, 4, _convert(sprintf('%01.2f', $value2 / (1 + $l_ukup_porez / 100))), 0, "R");
							$l_osnovica_sum += $value2 / (1 + $l_ukup_porez / 100);
							
							$pdf->SetXY(70, 50 + 4 * ($l_cnt + 3 + 1));
							$pdf->MultiCell(14, 4, _convert($key2 == 'no_pdv' ? '  -' : $key2), 0, "L");
							
							$pdf->SetXY(79, 50 + 4 * ($l_cnt + 3 + 1));
							$pdf->MultiCell(19, 4, _convert(sprintf('%01.2f', $value2 / (1 + $l_ukup_porez / 100) * $key2 / 100)), 0, "R");
							$l_pdv_kn += $value2 / (1 + $l_ukup_porez / 100) * $key2 / 100;
							
							$pdf->SetXY(114, 50 + 4 * ($l_cnt + 3 + 1));
							$pdf->MultiCell(19, 4, _convert(sprintf('%01.2f', $value2 / (1 + $l_ukup_porez / 100) * $l_key_3_move / 100)), 0, "R");
							$l_pnp_kn += $value2 / (1 + $l_ukup_porez / 100) * $l_key_3_move / 100;
								
							$pdf->SetXY(133, 50 + 4 * ($l_cnt + 3 + 1));
							$pdf->MultiCell(19, 4, _convert(sprintf('%01.2f', $value2)), 0, "R");
							
							$l_ukup_sve += $value2;
							
							
							$l_cnt++;
						}
						
					}
					
					
					$pdf->SetFont("ArialCE", "B", 8);
					
					$pdf->SetXY(10, 50 + 4 * ($l_cnt + 3 + 1));
					$pdf->MultiCell(35, 4, _convert('Ukupno '.$g_v), 0, "L");
					
					$pdf->SetXY(46, 50 + 4 * ($l_cnt + 3 + 1));
					$pdf->MultiCell(19, 4, _convert(sprintf('%01.2f', $l_osnovica_sum)), 0, "R");
					
					$pdf->SetXY(79, 50 + 4 * ($l_cnt + 3 + 1));
					$pdf->MultiCell(19, 4, _convert(sprintf('%01.2f', $l_pdv_kn)), 0, "R");
					
					$pdf->SetXY(114, 50 + 4 * ($l_cnt + 3 + 1));
					$pdf->MultiCell(19, 4, _convert(sprintf('%01.2f', $l_pnp_kn)), 0, "R");
					
					$pdf->SetXY(133, 50 + 4 * ($l_cnt + 3 + 1));
					$pdf->MultiCell(19, 4, _convert(sprintf('%01.2f', $l_ukup_sve)), 0, "R");
					
					if($k != 2){
						$l_ukup_sve_sve += $l_ukup_sve; 
					}
					
					
					$l_cnt++;
					
					$pdf->SetFont("ArialCE", "", 7);
					$pdf->SetXY(10, 50 + 4 * ($l_cnt + 3 + 1));
					$pdf->MultiCell(210, 4, _convert('----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------'), 0, "L");
					
					$l_cnt++;
					
					
					//p($la_ukup);
				
					$l_pnp = 0;
					foreach($la_ukup as $key => $value){
						
						$pdf->SetFont("ArialCE", "", 8);
						
						$pdf->SetXY(10, 50 + 4 * ($l_cnt + 3 + 1));
						$pdf->MultiCell(19, 4, _convert('PDV '.($key == 'no_pdv' ? '  -' : $key.'%:')), 0, "L");
						
						$pdf->SetXY(30, 50 + 4 * ($l_cnt + 3 + 1));
						$pdf->MultiCell(35, 4, _convert((sprintf('%01.2f', $value['pdv'] * $key / 100)).' '.$g_v), 0, "L");
						
						$l_cnt++;
						
						$pdf->SetXY(10, 50 + 4 * ($l_cnt + 3 + 1));
						$pdf->MultiCell(35, 4, _convert('Osnovica'), 0, "L");
						
						$pdf->SetXY(50, 50 + 4 * ($l_cnt + 3 + 1));
						$pdf->MultiCell(35, 4, _convert('PDV'), 0, "L");
						
						$pdf->SetXY(90, 50 + 4 * ($l_cnt + 3 + 1));
						$pdf->MultiCell(35, 4, _convert('PNP'), 0, "L");
						
						$l_cnt++;
						
						foreach($la_ukup[$key]['pnp'] as $key2 => $value2){
							$pdf->SetXY(10, 50 + 4 * ($l_cnt + 3 + 1));
							$pdf->MultiCell(35, 4, _convert(sprintf('%01.2f', $value2)), 0, "L");
							
							$pdf->SetXY(50, 50 + 4 * ($l_cnt + 3 + 1));
							$pdf->MultiCell(35, 4, _convert(sprintf('%01.2f', $value2 * $key / 100)), 0, "L");
							
							$pdf->SetXY(90, 50 + 4 * ($l_cnt + 3 + 1));
							$pdf->MultiCell(35, 4, _convert(sprintf('%01.2f', $value2 * $key2 / 100)), 0, "L");
							$l_pnp += ($value2 * $key2 / 100);
							
							if($k != 2){
								$l_pnp_ukup += ($value2 * $key2 / 100);
							}
							
							//p($value2 * $key2 / 100);
							
							$l_cnt++;
						}
						
						$pdf->SetFont("ArialCE", "", 7);
						$pdf->SetXY(10, 50 + 4 * ($l_cnt + 3 + 1));
						$pdf->MultiCell(210, 4, _convert('----------------------------------------------------------------------------------------------------------------'), 0, "L");
						
						$l_cnt++;
					}
					
					$pdf->SetFont("ArialCE", "", 8);
					
					$pdf->SetXY(10, 50 + 4 * ($l_cnt + 3 + 1));
					$pdf->MultiCell(19, 4, _convert('PNP:'), 0, "L");
					
					$pdf->SetXY(30, 50 + 4 * ($l_cnt + 3 + 1));
					$pdf->MultiCell(35, 4, _convert(sprintf('%01.2f', $l_pnp).' '.$g_v), 0, "L");
					
					$l_cnt++;
					
					
					
					$l_cnt++;
					$l_cnt++;
					$l_cnt++;
					$l_cnt++;
					$l_cnt++;
					
				}
				
				
				$l_cnt++;
				$l_cnt++;
				
				
				
				$l_sveukupno = 0;
				foreach($la_rekapitulacija as $key => $value){
					$l_sveukupno += $value['pdv'];
				}
				
				//p($l_sveukupno);
				//p($la_ukup);
				//p($la_rekapitulacija);
				//exit;
				
				$pdf->SetFont("ArialCE", "", 9);
				
				$pdf->SetXY(10, 50 + 4 * $l_cnt);
				$pdf->MultiCell(210, 4, _convert('--------------------------------------------------------------------------------------------------------------------------------------------'), 0, "L");
				
				$pdf->SetXY(10, 50 + 4 * ($l_cnt + 1));
				$pdf->MultiCell(70, 4, _convert('SVEUKUPNO: '.sprintf('%01.2f', $l_ukup_sve_sve).''.$g_v), 0, "L");
				
				
				$pdf->SetFont("ArialCE", "", 9);
				$pdf->SetXY(10, 50 + 4 * ($l_cnt + 2));
				$pdf->MultiCell(210, 4, _convert('--------------------------------------------------------------------------------------------------------------------------------------------'), 0, "L");
				
				foreach($la_rekapitulacija as $key => $value){
					
					$pdf->SetFont("ArialCE", "", 8);
					
					$pdf->SetXY(10, 50 + 4 * ($l_cnt + 3 + 1));
					$pdf->MultiCell(19, 4, _convert('PDV '.($key == 'no_pdv' ? '  -' : $key.'%:')), 0, "L");
					
					$pdf->SetXY(30, 50 + 4 * ($l_cnt + 3 + 1));
					$pdf->MultiCell(35, 4, _convert((sprintf('%01.2f', $value['pdv'] * $key / 100)).' '.$g_v), 0, "L");
					
					$l_cnt++;
					
					$pdf->SetXY(10, 50 + 4 * ($l_cnt + 3 + 1));
					$pdf->MultiCell(35, 4, _convert('Osnovica'), 0, "L");
					
					$pdf->SetXY(50, 50 + 4 * ($l_cnt + 3 + 1));
					$pdf->MultiCell(35, 4, _convert('PDV'), 0, "L");
					
					$pdf->SetXY(90, 50 + 4 * ($l_cnt + 3 + 1));
					$pdf->MultiCell(35, 4, _convert('PNP'), 0, "L");
					
					$l_cnt++;
					
					foreach($la_rekapitulacija[$key]['pnp'] as $key2 => $value2){
						$pdf->SetXY(10, 50 + 4 * ($l_cnt + 3 + 1));
						$pdf->MultiCell(35, 4, _convert(sprintf('%01.2f', $value2)), 0, "L");
						
						$pdf->SetXY(50, 50 + 4 * ($l_cnt + 3 + 1));
						$pdf->MultiCell(35, 4, _convert(sprintf('%01.2f', $value2 * $key / 100)), 0, "L");
						
						$pdf->SetXY(90, 50 + 4 * ($l_cnt + 3 + 1));
						$pdf->MultiCell(35, 4, _convert(sprintf('%01.2f', $value2 * $key2 / 100)), 0, "L");
						
						$l_cnt++;
					}
					
					$pdf->SetFont("ArialCE", "", 7);
					$pdf->SetXY(10, 50 + 4 * ($l_cnt + 3 + 1));
					$pdf->MultiCell(210, 4, _convert('----------------------------------------------------------------------------------------------------------------'), 0, "L");
					
					$l_cnt++;
				}
				
				$pdf->SetFont("ArialCE", "", 8);
				
				$pdf->SetXY(10, 50 + 4 * ($l_cnt + 3 + 1));
				$pdf->MultiCell(19, 4, _convert('PNP:'), 0, "L");
				
				$pdf->SetXY(30, 50 + 4 * ($l_cnt + 3 + 1));
				$pdf->MultiCell(35, 4, _convert(sprintf('%01.2f', $l_pnp_ukup).' '.$g_v), 0, "L");
				
				$l_cnt++;

				$l_cnt++;
				$l_cnt++;
				$l_cnt++;
				$l_cnt++;
				$l_cnt++;
				$l_cnt++;

				$l_polog = get_item('select value from settings where name = "general.polog_blagajne" ');
				
				$pdf->SetXY(10, 50 + 4 * (++$l_cnt));
				$pdf->MultiCell(210, 4, _convert('Polog novcanica/kovanica: '.sprintf('%01.2f', $l_polog).' '.$g_v), 0, "L");
				
				$pdf->SetXY(10, 50 + 4 * (++$l_cnt));
				$pdf->MultiCell(210, 4, _convert($l_pdv_koment), 0, "L");
					
				//p($la_rekapitulacija);
				//p($la_ukup);
				//p($la_tax[$k]);
				//exit;
				
				

			}
		}
		
		if($l_tip_prog == 'm'){
			//for($i = 1; $i <= $pgNumber; $i++){
			for($i = 1; $i <= 1; $i++){
				// add a page
				$pdf->AddPage();
				
				// import page 1
				$tplIdx = $pdf->importPage($i);
				
				// use the imported page and place it at point 0,0 with a width of 210 mm
				$pdf->useTemplate($tplIdx, 0, 0, 210, 297);
				
				
				
				if($vrsta_izvjesca == ''){
					$l_vrsta_izvjesca = '';
				}else{
					$l_vrsta_izvjesca = '('.$vrsta_izvjesca.')';
				}
				
				$pdf->SetFont("ArialCE", "B", 11);
				$pdf->SetTextColor(0, 0, 0);
				
				$pdf->SetXY(10, 10);
				$pdf->MultiCell(210, 5, _convert($x), 0, "L");
				
				$pdf->SetXY(10, 30);
				$pdf->MultiCell(210, 5, _convert('Dnevni promet '.$l_vrsta_izvjesca), 0, "L");
				
				$pdf->SetXY(10, 35);
				$pdf->MultiCell(210, 5, _convert('Period od '.$l_datum.' do '.$l_datum), 0, "L");
				
				$pdf->SetXY(10, 40);
				$pdf->MultiCell(210, 5, _convert('Brojevi računa od '.$l_min_rbr.' do '.$l_max_rbr), 0, "L");
				
				
				$l_ukup_sve = 0;
				$l_cnt = 0;
				
				
				for($k = 0; $k < sizeof($la_uvjet); $k++){
					
					if(50 + 4 * ($l_cnt + 1) > 210){
						$pdf->AddPage();
						$l_cnt = -8;
					}
					
					
					$l_ukup = 0;
					foreach($la_tax[$k] as $key => $value){
						$l_ukup += $value;
					}
					
					$pdf->SetFont("ArialCE", "", 9);
					
					$pdf->SetXY(10, 50 + 4 * $l_cnt);
					$pdf->MultiCell(210, 4, _convert('------------------------------------------------------------------------------------------------------'), 0, "L");
					
					$pdf->SetXY(10, 50 + 4 * ($l_cnt + 1));
					$pdf->MultiCell(70, 4, _convert($la_tax_desc[$k]), 0, "L");
					
					$pdf->SetXY(90, 50 + 4 * ($l_cnt + 1));
					$pdf->MultiCell(30, 4, _convert(sprintf('%01.2f', $l_ukup).' '.$g_v), 0, "R");
					
					
					if($la_pn[$k] > 0){
						$l_cnt++;
						$pdf->SetXY(10, 50 + 4 * ($l_cnt + 1));
						$pdf->MultiCell(70, 4, _convert('Povratna naknada'), 0, "L");
						
						$pdf->SetXY(90, 50 + 4 * ($l_cnt + 1));
						$pdf->MultiCell(30, 4, _convert(sprintf('%01.2f', $la_pn[$k]).' '.$g_v), 0, "R");
						
						$l_cnt++;
						$pdf->SetXY(10, 50 + 4 * ($l_cnt + 1));
						$pdf->MultiCell(70, 4, _convert('UKUPNO'), 0, "L");
						
						$pdf->SetXY(90, 50 + 4 * ($l_cnt + 1));
						$pdf->MultiCell(30, 4, _convert(sprintf('%01.2f', $l_ukup + $la_pn[$k]).' '.$g_v), 0, "R");
					}
					
					
			
					$l_ukup = 0;
					$l_cnt_2 = 0;
					
					foreach($la_tax[$k] as $key => $value){
						$l_cnt_2++;
						$l_osnovica = round($value * 100 / (100 + intval($key)), 2);
						$l_iznos = round(($key * $value / (100 + intval($key))), 2);
						$l_ukup += $value;
						if($la_skip_sum[$k] == '0'){
							$l_ukup_sve += $value;
						}
						
						$pdf->SetFont("ArialCE", "", 8);
						
						if($l_cnt_2 == 1){
							$pdf->SetFont("ArialCE", "", 9);
							
							$pdf->SetXY(10, 50 + 4 * ($l_cnt + 2));
							$pdf->MultiCell(210, 4, _convert('------------------------------------------------------------------------------------------------------'), 0, "L");
							
							$pdf->SetFont("ArialCE", "", 8);
							if($la_card_details[$k] == '1'){
								$la_tmp_uvjet = explode(',', $la_uvjet[$k]);
								for($z = 0; $z < sizeof($la_tmp_uvjet); $z++){
									if(empty($la_cards[trim($la_tmp_uvjet[$z])])){
										$la_cards[trim($la_tmp_uvjet[$z])] = 0;
									}
					
									$l_cnt++;
									$pdf->SetXY(10, 50 + 4 * ($l_cnt + 2));
									$pdf->MultiCell(80, 4, _convert(get_item('select full_name from payment_types where id = '.trim($la_tmp_uvjet[$z]))), 0, "L");
									$pdf->SetXY(100, 50 + 4 * ($l_cnt + 2));
									$pdf->MultiCell(50, 4, _convert($la_cards[trim($la_tmp_uvjet[$z])]), 0, "L");
								}
								$pdf->SetXY(10, 50 + 4 * (++$l_cnt + 2));
								$pdf->SetFont("ArialCE", "", 9);
								$pdf->MultiCell(210, 4, _convert('------------------------------------------------------------------------------------------------------'), 0, "L");
								$pdf->SetFont("ArialCE", "", 8);
								if($l_pdv == 'false'){
									if($la_pn[$k] == 0){
										$l_cnt--;
									}
								}
							}
							
							if($l_pdv == 'false'){
							}else{
								$pdf->SetXY(10, 49 + 4 * ($l_cnt + 3));
								$pdf->MultiCell(210, 4, _convert('Porezi '.$la_type_desc[$k].':'), 0, "L");
							}
						}
					
						
						if($l_pdv == 'false'){
							$l_cnt--;
							$l_cnt--;
						}else{
							$pdf->SetXY(10, 50 + 4 * ($l_cnt + 3 + 1));
							$pdf->MultiCell(25, 4, _convert('PDV '.round($key).'%'), 0, "L");
							
							$pdf->SetXY(40, 50 + 4 * ($l_cnt + 3 + 1));
							$pdf->MultiCell(25, 4, _convert('OSNOVICA'), 0, "L");
							
							$pdf->SetXY(70, 50 + 4 * ($l_cnt + 3 + 1));
							$pdf->MultiCell(25, 4, _convert('POREZ'), 0, "L");
							
							$pdf->SetXY(100, 50 + 4 * ($l_cnt + 3 + 1));
							$pdf->MultiCell(25, 4, _convert('UKUPNO'), 0, "L");
							
							$pdf->SetXY(10, 50 + 4 * ($l_cnt + 3 + 2));
							$pdf->MultiCell(25, 4, _convert(''), 0, "L");
							
							$pdf->SetXY(40, 50 + 4 * ($l_cnt + 3 + 2));
							$pdf->MultiCell(25, 4, _convert(sprintf('%01.2f', $l_osnovica)), 0, "L");
							
							$pdf->SetXY(70, 50 + 4 * ($l_cnt + 3 + 2));
							$pdf->MultiCell(25, 4, _convert(sprintf('%01.2f', $l_iznos)), 0, "L");
							
							$pdf->SetXY(100, 50 + 4 * ($l_cnt + 3 + 2));
							$pdf->MultiCell(25, 4, _convert(sprintf('%01.2f', $l_osnovica + $l_iznos)), 0, "L");
						}
						
						$l_cnt++;
						$l_cnt++;
					}
					
					$l_cnt++;
					
					/*
					if($la_pn[$k] > 0){
						if($l_pdv == 'false'){
							$l_cnt--;
							$l_cnt--;
						}
						$pdf->SetXY(10, 50 + 4 * ($l_cnt + 3 + 1));
						$pdf->MultiCell(100, 4, _convert('Ukupno povratne naknade: '.sprintf('%01.2f', $la_pn[$k]).' kn'), 0, "L");
						$l_cnt++;
					}
					*/
					
					if($l_pdv == 'false'){
						$l_cnt++;
					}
					
					if($la_card_details[$k] == '1'){
						$l_cnt_2++;
						$l_cnt = $l_cnt + 1 + $l_cnt_2;
					}else{
						$l_cnt_2++;
						$l_cnt = $l_cnt + 2 + $l_cnt_2;
					}
					
					$l_cnt++;
					if($l_pdv == 'false'){
						$l_cnt--;
					}
					
				}
				
				
					
				if(50 + 4 * ($l_cnt + 1) > 210){
					$pdf->AddPage();
					$l_cnt = -8;
				}
				
				
				if($l_pdv == 'false'){
					$l_cnt++;
					$l_cnt++;
				}
					
				$pdf->SetFont("ArialCE", "", 9);
				
				if($l_pdv == 'false'){
					$l_cnt--;
					$l_cnt--;
				}
				
				$pdf->SetXY(10, 50 + 4 * $l_cnt);
				$pdf->MultiCell(210, 4, _convert('=========================================================='), 0, "L");
					
				$pdf->SetFont("ArialCE", "B", 9);
				
				$l_cnt++;
				$pdf->SetXY(10, 50 + 4 * $l_cnt);
				$pdf->MultiCell(210, 4, _convert('SVEUKUPNO PROMET: '.sprintf('%01.2f', $l_ukup_sve + $la_pn_ukup).' '.$g_v), 0, "L");
				
				$l_cnt++;
				
				$pdf->SetFont("ArialCE", "", 9);
				
				$pdf->SetXY(10, 50 + 4 * $l_cnt);
				$pdf->MultiCell(210, 4, _convert('=========================================================='), 0, "L");
				
				$pdf->SetFont("ArialCE", "", 8);
				
				$l_cnt++;
				
				if($l_pdv == 'false'){
				}else{
					$l_cnt++;
					
					$pdf->SetXY(10, 50 + 4 * $l_cnt);
					$pdf->MultiCell(30, 4, _convert('POREZI UKUPNO'), 0, "L");
					
					$pdf->SetXY(40, 50 + 4 * $l_cnt);
					$pdf->MultiCell(25, 4, _convert('OSNOVICA'), 0, "L");
					
					$pdf->SetXY(70, 50 + 4 * $l_cnt);
					$pdf->MultiCell(25, 4, _convert('POREZ'), 0, "L");
					
					$pdf->SetXY(100, 50 + 4 * $l_cnt);
					$pdf->MultiCell(25, 4, _convert('UKUPNO'), 0, "L");
					
					
					$l_ispis = '';
					foreach($la_tax_ukup as $key => $value){
						
						$l_cnt++;
						$pdf->SetXY(10, 50 + 4 * $l_cnt);
						$pdf->MultiCell(25, 4, _convert(round($key).'%'), 0, "L");
						
						$l_osnovica = round(($value / (100 + intval($key)) * 100), 2);
						$l_iznos = $value - $l_osnovica;
						
						$pdf->SetXY(40, 50 + 4 * $l_cnt);
						$pdf->MultiCell(25, 4, _convert(sprintf('%01.2f', $l_osnovica)), 0, "L");
						
						$pdf->SetXY(70, 50 + 4 * $l_cnt);
						$pdf->MultiCell(25, 4, _convert(sprintf('%01.2f', $l_iznos)), 0, "L");
						
						$pdf->SetXY(100, 50 + 4 * $l_cnt);
						$pdf->MultiCell(25, 4, _convert(sprintf('%01.2f', $value)), 0, "L");
					}
				}
				
				
				$l_cnt++;
				$l_cnt++;
				
				if($la_pn_ukup > 0){
					if($l_pdv == 'false'){
						$l_cnt--;
						$l_cnt--;
					}
					$pdf->SetXY(10, 50 + 4 * $l_cnt);
					$pdf->MultiCell(100, 4, _convert('Sveukupno povratne naknade: '.sprintf('%01.2f', $la_pn_ukup).' '.$g_v), 0, "L");
				}
				
				if($l_pdv == 'false'){
					$l_cnt--;
				}
				
				if(50 + 4 * ($l_cnt + 1) > 210){
					$pdf->AddPage();
					$l_cnt = -8;
				}
				
				
				$l_cnt++;
				$l_cnt++;
				
				$l_polog = get_item('select value from settings where name = "general.polog_blagajne" ');
				
				$pdf->SetXY(10, 50 + 4 * (++$l_cnt));
				$pdf->MultiCell(210, 4, _convert('Polog novcanica/kovanica: '.sprintf('%01.2f', $l_polog).' '.$g_v), 0, "L");
				
				$pdf->SetXY(10, 50 + 4 * (++$l_cnt));
				$pdf->MultiCell(210, 4, _convert($l_pdv_koment), 0, "L");
				
				
			}
		}
			
		$l_name = 'dnevni_promet.pdf';
		$l_dest = $g_path.'app/webroot/res/files/'.$l_name;
		
		$pdf->Output($l_dest, 'F');
		
		return array('path' => $g_path.'app/webroot/res/files/', 'name' => $l_name);
	}
	
	
	
	
	
	function all_ispis_normativa($date_start, $date_end){
		
		$g_path = get_item('select value from settings where name = "putanja.abs" ');
		
		$g_v = get_valuta();
		
		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');
		
		$pdf =& new FPDI('P', 'mm', 'A4');
		
		$pdf->AddFont("ArialCE", "B", "6488f5d4657b269b9f5770508e80e299_arialbd.php");
		$pdf->AddFont("ArialCE", "", "332cc83ebee36613c19564e6e776c81b_arial.php");
		
		// set the sourcefile
		$pgNumber = $pdf->setSourceFile($g_path.'app/libs/fpdf/prazni-dokument.pdf');
		
		//$pdf->SetAutoPageBreak(true, 10);
		
		
		$x = get_item('select value from settings where name = "general.name" ').', '.
			get_n_rows(get_item('select value from settings where name = "invoice.header" '), 4);
		
		$la_tmp_1 = explode('-', $date_start);
		$la_tmp_2 = explode('-', $date_end);
		
		$l_datum_1 = $la_tmp_1[0].'.'.$la_tmp_1[1].'.'.$la_tmp_1[2].'.';
		$l_datum_2 = $la_tmp_2[0].'.'.$la_tmp_2[1].'.'.$la_tmp_2[2].'.';
		
		$l_datum_db_1 = $la_tmp_1[2].'-'.$la_tmp_1[1].'-'.$la_tmp_1[0];
		$l_datum_db_2 = $la_tmp_2[2].'-'.$la_tmp_2[1].'-'.$la_tmp_2[0];
		$l_datum_from = $l_datum_db_1.' 00:00:00';
		$l_datum_to = $l_datum_db_2.' 23:59:59';
		
		
		$l_tip_prog = get_item('select value from settings where name = "tipprog" ');
			
		
		
		if($l_tip_prog == 'm'){
		}
	
		
		
		if($l_tip_prog == 'u'){
			
			
			$l_item = '';
			
			//$l_xls = 'rbr;naziv;kolicina;cijena;ukupno'."\n";
		
			$l_cnt = 0;
			for($i = 1; $i <= 1; $i++){
				// add a page
				$pdf->AddPage();
				
				// import page 1
				$tplIdx = $pdf->importPage($i);
				
				// use the imported page and place it at point 0,0 with a width of 210 mm
				$pdf->useTemplate($tplIdx, 0, 0, 210, 297);
				
				
				
				$pdf->SetFont("ArialCE", "B", 11);
				$pdf->SetTextColor(0, 0, 0);
				
				$pdf->SetXY(10, 10);
				$pdf->MultiCell(210, 5, _convert($x), 0, "L");
				
				
				
				$pdf->SetFont("ArialCE", "", 9);
				
				$pdf->SetXY(10, 30);
				$pdf->MultiCell(210, 4, _convert('--------------------------------------------------------------------------------------------------------------------------------------------------------------------------'), 0, "L");
				
				$pdf->SetFont("ArialCE", "B", 13);
				
				$pdf->SetXY(10, 36);
				$pdf->MultiCell(180, 5, _convert('Ispis normativa'), 0, "C");
				
				//$pdf->SetXY(10, 38);
				//$pdf->MultiCell(210, 5, _convert('Period od '.$l_datum_1.' do '.$l_datum_2), 0, "L");
				
				$pdf->SetFont("ArialCE", "", 9);
				$pdf->SetXY(10, 42);
				$pdf->MultiCell(210, 4, _convert('--------------------------------------------------------------------------------------------------------------------------------------------------------------------------'), 0, "L");
				
				
				$l_cnt++;
				$l_cnt++;
				$l_cnt++;
				
				$x = get_data('select id, name from tax_groups order by field(id, 1, 2, 3, 4, 5, 6, 7, 8) desc, id ', 1);
				for($j = 0; $j < sizeof($x['id']); $j++){
					
					if(10 + 4 * ($l_cnt + 3 + 1) > 220){
						$pdf->AddPage();
						$l_cnt = -8;
					}
					
					
					
					$l_cnt++;
					$l_cnt++;
					
					$pdf->SetFont("ArialCE", "B", 12);
					$pdf->SetXY(10, 30 + 4 * ($l_cnt + 3));
					$pdf->MultiCell(170, 4, _convert('Grupa normativa: '.trim($x['name'][$j])), 0, "C");
					$pdf->SetFont("ArialCE", "", 9);
					
					$l_cnt++;
					$l_cnt++;
					$l_cnt++;
					
					
					
					
					$y = get_data('select products.name, products.price, product_items.amount, articles.name as a_name, articles.unit, articles.price_buy, articles.price_sell '.
						' from products, product_items, articles '.
						' where products.id = product_items.product_id '.
						' and product_items.article_id = articles.id '.
						' and products.tax_group_id = '.$x['id'][$j].
						' order by products.name, articles.name', 1);
					$la_data = array();
					$la_name = array();
					for($k = 0; $k < sizeof($y['name']); $k++){
						if(sizeof($la_name) == 0 || !in_array($y['name'][$k], $la_name)){
							array_push($la_name, $y['name'][$k]);
							$l_article_cnt = sizeof($la_name) - 1;
							$la_data[$l_article_cnt]['pc'] = $y['price'][$k];
							$la_data[$l_article_cnt]['nc'] = 0;
							$la_data[$l_article_cnt]['matrix'] = array();
							$l_local_k = 0;
						}
						
						$la_data[$l_article_cnt]['nc'] += $y['amount'][$k] * $y['price_buy'][$k];
						
						/*
						p($l_article_cnt);
						p($k);
						p('--');
						*/
						$la_data[$l_article_cnt]['matrix'][$l_local_k] = array();
						$la_data[$l_article_cnt]['matrix'][$l_local_k]['naziv'] = $y['a_name'][$k];
						$la_data[$l_article_cnt]['matrix'][$l_local_k]['jmj'] = $y['unit'][$k];
						$la_data[$l_article_cnt]['matrix'][$l_local_k]['kol'] = $y['amount'][$k];
						$la_data[$l_article_cnt]['matrix'][$l_local_k]['nc'] = $y['price_buy'][$k];
						$la_data[$l_article_cnt]['matrix'][$l_local_k]['pc'] = $y['price_sell'][$k];
						$la_data[$l_article_cnt]['matrix'][$l_local_k]['ncun'] = $y['amount'][$k] * $y['price_buy'][$k];
						
						$l_local_k++;
						
						//break;
					}
					
					
					for($k = 0; $k < sizeof($la_name); $k++){
						$pdf->SetFont("ArialCE", "B", 9);
						$pdf->SetXY(10, 30 + 4 * ($l_cnt + 3));
						$pdf->MultiCell(70, 4, _convert('Normativ: '.trim($la_name[$k])), 0, "L");
						
						$pdf->SetXY(120, 30 + 4 * ($l_cnt + 3));
						$pdf->MultiCell(30, 4, _convert('PC = '.sprintf('%01.2f', $la_data[$k]['pc']).' '.$g_v), 0, "L");
						
						$pdf->SetXY(160, 30 + 4 * ($l_cnt + 3));
						$pdf->MultiCell(30, 4, _convert('NC = '.sprintf('%01.2f', $la_data[$k]['nc']).' '.$g_v), 0, "L");
						$pdf->SetFont("ArialCE", "", 9);
						
						$l_cnt++;
						
						
						$pdf->SetFont("ArialCE", "", 8);
						$pdf->SetXY(10, 30 + 4 * ($l_cnt + 3));
						$pdf->MultiCell(210, 4, _convert('----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------'), 0, "L");
						$pdf->SetFont("ArialCE", "", 9);
						
						$l_cnt++;
						
						
						/*
						$pdf->SetXY(10, 30 + 4 * ($l_cnt + 3));
						$pdf->MultiCell(70, 4, _convert('Sastojci:'), 0, "L");
						
						$l_cnt++;
						*/
						
						
						
						$pdf->SetXY(10, 30 + 4 * ($l_cnt + 3));
						$pdf->MultiCell(70, 4, _convert('Naziv'), 0, "L");
						
						$pdf->SetXY(105, 30 + 4 * ($l_cnt + 3));
						$pdf->MultiCell(70, 4, _convert('j.mj.'), 0, "L");
						
						$pdf->SetXY(120, 30 + 4 * ($l_cnt + 3));
						$pdf->MultiCell(70, 4, _convert('kol.'), 0, "L");
						
						$pdf->SetXY(140, 30 + 4 * ($l_cnt + 3));
						$pdf->MultiCell(70, 4, _convert('NC'), 0, "L");
						
						$pdf->SetXY(160, 30 + 4 * ($l_cnt + 3));
						$pdf->MultiCell(70, 4, _convert('PC'), 0, "L");
						
						$pdf->SetXY(180, 30 + 4 * ($l_cnt + 3));
						$pdf->MultiCell(70, 4, _convert('ncun'), 0, "L");
						
						$l_cnt++;
						
						
						$pdf->SetFont("ArialCE", "", 8);
						$pdf->SetXY(10, 30 + 4 * ($l_cnt + 3));
						$pdf->MultiCell(210, 4, _convert('----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------'), 0, "L");
						$pdf->SetFont("ArialCE", "", 9);
						
						$l_cnt++;
						
						
						for($k2 = 0; $k2 < sizeof($la_data[$k]['matrix']); $k2++){
							
							$pdf->SetXY(10, 30 + 4 * ($l_cnt + 3));
							$pdf->MultiCell(70, 4, _convert(trim($la_data[$k]['matrix'][$k2]['naziv'])), 0, "L");
							
							$pdf->SetXY(105, 30 + 4 * ($l_cnt + 3));
							$pdf->MultiCell(70, 4, _convert(trim($la_data[$k]['matrix'][$k2]['jmj'])), 0, "L");
							
							$pdf->SetXY(120, 30 + 4 * ($l_cnt + 3));
							$pdf->MultiCell(70, 4, _convert(trim($la_data[$k]['matrix'][$k2]['kol'])), 0, "L");
							
							$pdf->SetXY(140, 30 + 4 * ($l_cnt + 3));
							$pdf->MultiCell(70, 4, _convert(sprintf('%01.2f', $la_data[$k]['matrix'][$k2]['nc'])), 0, "L");
							
							$pdf->SetXY(160, 30 + 4 * ($l_cnt + 3));
							$pdf->MultiCell(70, 4, _convert(sprintf('%01.2f', $la_data[$k]['matrix'][$k2]['pc'])), 0, "L");
							
							$pdf->SetXY(180, 30 + 4 * ($l_cnt + 3));
							$pdf->MultiCell(70, 4, _convert(sprintf('%01.2f', $la_data[$k]['matrix'][$k2]['ncun'])), 0, "L");
							
							$l_cnt++;
							
							
							
							if(10 + 4 * ($l_cnt + 3 + 1) > 220){
								$pdf->AddPage();
								$l_cnt = -8;
							}
							
						}
						
						
						$pdf->SetFont("ArialCE", "", 8);
						$pdf->SetXY(10, 30 + 4 * ($l_cnt + 3));
						$pdf->MultiCell(210, 4, _convert('----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------'), 0, "L");
						$pdf->SetFont("ArialCE", "", 9);
						
						$l_cnt++;
						$l_cnt++;
						$l_cnt++;
							
						
						if(10 + 4 * ($l_cnt + 3 + 1) > 220){
							$pdf->AddPage();
							$l_cnt = -8;
						}
						
						
					}
					
					
					
					/*
					$pdf->SetFont("ArialCE", "", 8);
					$pdf->SetXY(10, 30 + 4 * ($l_cnt + 3));
					$pdf->MultiCell(210, 4, _convert('----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------'), 0, "L");
					$pdf->SetFont("ArialCE", "", 9);
					
					$l_cnt++;
					*/
					
					
					//break;
					
				}
				
				
			}
		}
		
		
		
		
		
			
		$l_name = 'ispis_normativa.pdf';
		$l_dest = $g_path.'app/webroot/res/files/'.$l_name;
		
		$pdf->Output($l_dest, 'F');
		
		
		$fp = @fopen($g_path.'app/webroot/res/files/'.str_replace('.pdf', '.csv', $l_name), 'wb');
		fwrite($fp, $l_xls);
		fclose($fp);
		
		
		return array('path' => $g_path.'app/webroot/res/files/', 'name' => $l_name);
	}
	
	
	
	
	
	function all_pregled_prodaje_po_normativu($date_start, $date_end, $user_id){
		
		$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');
		
		$pdf =& new FPDI('P', 'mm', 'A4');
		
		$pdf->AddFont("ArialCE", "B", "6488f5d4657b269b9f5770508e80e299_arialbd.php");
		$pdf->AddFont("ArialCE", "", "332cc83ebee36613c19564e6e776c81b_arial.php");
		
		// set the sourcefile
		$pgNumber = $pdf->setSourceFile($g_path.'app/libs/fpdf/prazni-dokument.pdf');
		
		//$pdf->SetAutoPageBreak(true, 10);
		
		
		$x = get_item('select value from settings where name = "general.name" ').', '.
			get_n_rows(get_item('select value from settings where name = "invoice.header" '), 4);
		
		$la_tmp_1 = explode('-', $date_start);
		$la_tmp_2 = explode('-', $date_end);
		
		$l_datum_1 = $la_tmp_1[0].'.'.$la_tmp_1[1].'.'.$la_tmp_1[2].'.';
		$l_datum_2 = $la_tmp_2[0].'.'.$la_tmp_2[1].'.'.$la_tmp_2[2].'.';
		
		$l_datum_db_1 = $la_tmp_1[2].'-'.$la_tmp_1[1].'-'.$la_tmp_1[0];
		$l_datum_db_2 = $la_tmp_2[2].'-'.$la_tmp_2[1].'-'.$la_tmp_2[0];
		$l_datum_from = $l_datum_db_1.' 00:00:00';
		$l_datum_to = $l_datum_db_2.' 23:59:59';
		
		
		$l_tip_prog = get_item('select value from settings where name = "tipprog" ');
			
		if($l_tip_prog == 'm'){
		}
	
		if($l_tip_prog == 'u'){
			
			$y = get_data('select id from sales where created between "'.$l_datum_from.'" and "'.$l_datum_to.'" ', 1);
				
			$l_id = '0';
			for($i = 0; isset($y['id']) && $i < sizeof($y['id']); $i++){
				$l_id .= ', '.$y['id'][$i];
			}
			
			/*
			$z = get_data('select products.id, products.name, sale_items.base_price, discount_percent, sum(sale_items.amount) as amount,  sum(sale_items.price) as price '.
				' from sale_items, products '.
				' where sale_items.product_id = products.id '.
				' and sale_items.sale_id in ('.$l_id.')  '.
				' group by products.id, products.name, sale_items.base_price, discount_percent '.
				' order by products.name, sale_items.sale_id, sale_items.id', 1);
			*/
				
			if($user_id > 0){
				$l_cond = ' and sales.user_id = '.$user_id;
			}else{
				$l_cond = '';
			}
				
			$z = get_data('select products.id, products.name, sale_items.base_price, discount_percent, sum(sale_items.amount) as amount,  sum(sale_items.price) as price '.
				' from sales, sale_items, products '.
				' where sales.id = sale_items.sale_id '.
				' and sale_items.product_id = products.id '.
				' and sale_items.sale_id in ('.$l_id.')  '.
				$l_cond.
				' group by products.id, products.name, sale_items.base_price, discount_percent '.
				' order by products.name, sale_items.sale_id, sale_items.id', 1);
				
			
			$l_item = '';
			
			$l_xls = 'rbr;naziv;kolicina;NC;NC ukup;PC;PC ukup'."\n";
		
			$l_cnt = 0;
			for($i = 1; $i <= 1; $i++){
				// add a page
				$pdf->AddPage();
				
				// import page 1
				$tplIdx = $pdf->importPage($i);
				
				// use the imported page and place it at point 0,0 with a width of 210 mm
				$pdf->useTemplate($tplIdx, 0, 0, 210, 297);
				
				
				
				$pdf->SetFont("ArialCE", "B", 11);
				$pdf->SetTextColor(0, 0, 0);
				
				$pdf->SetXY(10, 10);
				$pdf->MultiCell(210, 5, _convert($x), 0, "L");
				
				
				
				$pdf->SetFont("ArialCE", "", 9);
				
				$pdf->SetXY(10, 30);
				$pdf->MultiCell(210, 4, _convert('--------------------------------------------------------------------------------------------------------------------------------------------------------------------'), 0, "L");
				
				$pdf->SetFont("ArialCE", "B", 11);
				
				$pdf->SetXY(10, 33);
				$pdf->MultiCell(210, 5, _convert('Pregled prodaje po normativu'), 0, "L");
				
				$pdf->SetXY(10, 38);
				$pdf->MultiCell(210, 5, _convert('Period od '.$l_datum_1.' do '.$l_datum_2), 0, "L");
				
				
				
				if($user_id > 0){
					
					$l_sql = 'select concat(full_name, ", ", username) as username '.
						' from users '.
						' where id =  '.$user_id;
					$l_user_name = get_item($l_sql);
					
					
					$pdf->SetFont("ArialCE", "", 10);
					$pdf->SetXY(85, 38);
					$pdf->MultiCell(110, 5, _convert('OPERATER: '.$l_user_name), 0, "L");
				}
				
				
				
				$pdf->SetFont("ArialCE", "", 9);
				$pdf->SetXY(10, 42);
				$pdf->MultiCell(210, 4, _convert('--------------------------------------------------------------------------------------------------------------------------------------------------------------------'), 0, "L");
					
					
				$l_cnt++;
				
				$pdf->SetFont("ArialCE", "B", 8);
				$pdf->SetXY(10, 30 + 4 * ($l_cnt + 3));
				$pdf->MultiCell(70, 4, _convert('Naziv'), 0, "L");
				
				$pdf->SetXY(85, 30 + 4 * ($l_cnt + 3));
				$pdf->MultiCell(25, 4, _convert('Kolicina'), 0, "L");
				
				$pdf->SetXY(110, 30 + 4 * ($l_cnt + 3));
				$pdf->MultiCell(25, 4, _convert('NC'), 0, "L");
				
				$pdf->SetXY(130, 30 + 4 * ($l_cnt + 3));
				$pdf->MultiCell(25, 4, _convert('NC Ukup'), 0, "L");
				
				$pdf->SetXY(150, 30 + 4 * ($l_cnt + 3));
				$pdf->MultiCell(25, 4, _convert('PC'), 0, "L");
				
				$pdf->SetXY(170, 30 + 4 * ($l_cnt + 3));
				$pdf->MultiCell(25, 4, _convert('PC Ukup'), 0, "L");
				
				$l_cnt++;
				
				$pdf->SetFont("ArialCE", "", 9);
				
				$l_ukup = 0;
				$l_ukup_nc = 0;
				for($j = 0; isset($z['name']) && $j < sizeof($z['name']); $j++){
					
					if(10 + 4 * ($l_cnt + 3 + 1) > 220){
						$pdf->AddPage();
						$l_cnt = -8;
					}
					
					$pdf->SetXY(10, 30 + 4 * ($l_cnt + 3));
					$pdf->MultiCell(70, 4, _convert(trim($z['name'][$j])), 0, "L");
					
					$pdf->SetXY(85, 30 + 4 * ($l_cnt + 3));
					$pdf->MultiCell(25, 4, _convert($z['amount'][$j]), 0, "L");
					
					
					$l_nc = get_item('select sum(product_items.amount * articles.price_buy) as zbr
						from product_items, articles
						where product_items.article_id = articles.id
						and product_items.product_id = '.$z['id'][$j]);
					
					
					$pdf->SetXY(110, 30 + 4 * ($l_cnt + 3));
					$pdf->MultiCell(25, 4, _convert(sprintf('%01.2f', $l_nc)), 0, "L");
					
					$pdf->SetXY(130, 30 + 4 * ($l_cnt + 3));
					$pdf->MultiCell(25, 4, _convert(sprintf('%01.2f', $l_nc * $z['amount'][$j])), 0, "L");
					
					$pdf->SetXY(150, 30 + 4 * ($l_cnt + 3));
					$pdf->MultiCell(25, 4, _convert(sprintf('%01.2f', $z['base_price'][$j] * (1 - $z['discount_percent'][$j] / 100))), 0, "L");
					
					$pdf->SetXY(170, 30 + 4 * ($l_cnt + 3));
					$pdf->MultiCell(25, 4, _convert(sprintf('%01.2f', $z['price'][$j])), 0, "L");
					
					$l_ukup += $z['price'][$j];
					$l_ukup_nc += $l_nc * $z['amount'][$j];
					
					
					$l_xls .= ($j + 1).';'.
						'"'.$z['name'][$j].'";'.
						str_replace('.', ',', $z['amount'][$j]).';'.
						_convert(str_replace('.', ',', sprintf('%01.2f', $l_nc))).';'.
						_convert(str_replace('.', ',', sprintf('%01.2f', $l_nc * $z['amount'][$j]))).';'.
						_convert(str_replace('.', ',', sprintf('%01.2f', $z['base_price'][$j] * (1 - $z['discount_percent'][$j] / 100)))).';'.
						_convert(str_replace('.', ',', sprintf('%01.2f', $z['price'][$j]))).';'.
						"\n";
					
					
					$l_cnt++;
				}
				
				
				$l_xls .= ';Ukupno'.
					';'.
					';'.
					';'.
					_convert(str_replace('.', ',', sprintf('%01.2f', $l_ukup_nc))).';'.
					';'.
					_convert(str_replace('.', ',', sprintf('%01.2f', $l_ukup))).';'.
					"\n";
				
				
				$pdf->SetFont("ArialCE", "", 9);
				$pdf->SetXY(10, 30 + 4 * ($l_cnt + 3));
				$pdf->MultiCell(210, 4, _convert('--------------------------------------------------------------------------------------------------------------------------------------------------------------------'), 0, "L");
				$l_cnt++;
				
				$pdf->SetXY(10, 30 + 4 * ($l_cnt + 3));
				$pdf->MultiCell(110, 4, _convert('UKUPNO EUR'), 0, "L");
				
				$pdf->SetXY(130, 30 + 4 * ($l_cnt + 3));
				$pdf->MultiCell(40, 4, _convert(sprintf('%01.2f', $l_ukup_nc)), 0, "L");
				
				$pdf->SetXY(170, 30 + 4 * ($l_cnt + 3));
				$pdf->MultiCell(40, 4, _convert(sprintf('%01.2f', $l_ukup)), 0, "L");
				
				$l_cnt++;
				$pdf->SetFont("ArialCE", "", 9);
				$pdf->SetXY(10, 30 + 4 * ($l_cnt + 3));
				$pdf->MultiCell(210, 4, _convert('--------------------------------------------------------------------------------------------------------------------------------------------------------------------'), 0, "L");
				$l_cnt++;
				
				$pdf->SetXY(10, 30 + 4 * ($l_cnt + 3));
				$pdf->MultiCell(110, 4, _convert('RUC'), 0, "L");
				
				$pdf->SetXY(170, 30 + 4 * ($l_cnt + 3));
				$pdf->MultiCell(40, 4, _convert(sprintf('%01.2f', $l_ukup - $l_ukup_nc)), 0, "L");
			}
				
			
			
			
				
			$pdf->SetFont("ArialCE", "", 8);
			
			/*
			$z = get_data('select sale_items.tax_1, sale_items.tax_2, sum(sale_items.price) as price '.
				' from sale_items, products, tax_groups '.
				' where sale_items.product_id = products.id '.
				' and products.tax_group_id = tax_groups.id '.
				' and sale_items.sale_id in ('.$l_id.')  '.
				' group by sale_items.tax_1, sale_items.tax_2 '.
				' order by sale_items.tax_1, sale_items.tax_2', 1);
			*/
				
			$z = get_data('select sale_items.tax_1, sale_items.tax_2, sum(sale_items.price) as price '.
				' from sales, sale_items, products, tax_groups '.
				' where sales.id = sale_items.sale_id '.
				' and sale_items.product_id = products.id '.
				' and products.tax_group_id = tax_groups.id '.
				' and sale_items.sale_id in ('.$l_id.')  '.
				$l_cond.
				' group by sale_items.tax_1, sale_items.tax_2 '.
				' order by sale_items.tax_1, sale_items.tax_2', 1);
				
				
				
			$la_ukup = array();
			for($j = 0; isset($z['tax_1']) && $j < sizeof($z['tax_1']); $j++){
				
				$la_ukup['pdv'][$z['tax_1'][$j]] = $z['price'][$j];
				$la_ukup['pnp'][$z['tax_1'][$j]][$z['tax_2'][$j]] = $z['price'][$j];
				
				$la_ukup['osnovica']['pdv'][$z['tax_1'][$j]] = $z['price'][$j] / (1 + ($z['tax_1'][$j] + $z['tax_2'][$j]) / 100);
				$la_ukup['osnovica']['pnp'][$z['tax_1'][$j]][$z['tax_2'][$j]] = $z['price'][$j] / (1 + ($z['tax_1'][$j] + $z['tax_2'][$j]) / 100);
			}
			
			
			if(isset($la_ukup['osnovica'])){
				foreach($la_ukup['osnovica']['pnp'] as $key => $value){
					foreach($value as $key2 => $value2){
						if(empty($la_ukup['osnovica']['ukup'][$key]['pdv'])){
							$la_ukup['osnovica']['ukup'][$key]['pdv'] = 0;
						}
						if(empty($la_ukup['osnovica']['ukup'][$key]['pnp'])){
							$la_ukup['osnovica']['ukup'][$key]['pnp'] = 0;
						}
						$la_ukup['osnovica']['ukup'][$key]['pdv'] += $value2 * ($key / 100);
						$la_ukup['osnovica']['ukup'][$key]['pnp'] += $value2 * ($key2 / 100);
					}
				}
			}
			
			
			$l_cnt++;
			$l_cnt++;
			
			$l_pnp = 0;
			
			
			$l_pdv = get_item('select value from settings where name = "duty.free" ');
			
			if($l_pdv == 'no'){
				$l_pdv = 'false';
			}
			
			if($l_pdv == 'false'){
				
				if(isset($la_ukup['osnovica'])){
					
					$pdf->SetFont("ArialCE", "", 8);
					
					$pdf->SetXY(10, 30 + 4 * ($l_cnt + 3 + 1));
					$pdf->MultiCell(35, 4, _convert('Osnovica'), 0, "L");
					
					$pdf->SetXY(50, 30 + 4 * ($l_cnt + 3 + 1));
					$pdf->MultiCell(35, 4, _convert('PNP'), 0, "L");
					
					$l_cnt++;
					
					
					$la_osnovica = array();
					
					
					//p($la_ukup['osnovica']);
					
					foreach($la_ukup['osnovica']['ukup'] as $key => $value){
						
						foreach($la_ukup['osnovica']['pnp'][$key] as $key2 => $value2){
							
							if(!isset($la_osnovica[$key2])){
								$la_osnovica[$key2] = 0;
							}
							
							$la_osnovica[$key2] += ($value2 * (1 + $key2 / 100) + $value2 * $key / 100) / (1 + ($key2 / 100));
							
						}
						
					}
					
					
					foreach($la_osnovica as $key => $value){
						$pdf->SetXY(10, 30 + 4 * ($l_cnt + 3 + 1));
						$pdf->MultiCell(35, 4, _convert(sprintf('%01.2f', $la_osnovica[$key])), 0, "L");
						
						$pdf->SetXY(50, 30 + 4 * ($l_cnt + 3 + 1));
						$pdf->MultiCell(35, 4, _convert(sprintf('%01.2f', $key / 100 * $la_osnovica[$key])), 0, "L");
						
						$l_pnp += $key / 100 * $la_osnovica[$key];
						
						$l_cnt++;
					}
					
					
					
					$pdf->SetFont("ArialCE", "", 7);
					$pdf->SetXY(10, 30 + 4 * ($l_cnt + 3 + 1));
					$pdf->MultiCell(210, 4, _convert('--------------------------------------------------------------------'), 0, "L");
					
					$l_cnt++;
					
				}
				
				
			}else{
				
				if(isset($la_ukup['osnovica'])){
					foreach($la_ukup['osnovica']['ukup'] as $key => $value){
						
						$pdf->SetFont("ArialCE", "", 8);
						
						$pdf->SetXY(10, 30 + 4 * ($l_cnt + 3 + 1));
						$pdf->MultiCell(19, 4, _convert('PDV '.$key.'%:'), 0, "L");
						
						$pdf->SetXY(30, 30 + 4 * ($l_cnt + 3 + 1));
						$pdf->MultiCell(35, 4, _convert((sprintf('%01.2f', $value['pdv'])).' EUR'), 0, "L");
						
						$l_cnt++;
						
						
						$pdf->SetXY(10, 30 + 4 * ($l_cnt + 3 + 1));
						$pdf->MultiCell(35, 4, _convert('Osnovica'), 0, "L");
						
						$pdf->SetXY(50, 30 + 4 * ($l_cnt + 3 + 1));
						$pdf->MultiCell(35, 4, _convert('PDV'), 0, "L");
						
						$pdf->SetXY(90, 30 + 4 * ($l_cnt + 3 + 1));
						$pdf->MultiCell(35, 4, _convert('PNP'), 0, "L");
						
						$l_cnt++;
						
						
						foreach($la_ukup['osnovica']['pnp'][$key] as $key2 => $value2){
							
							$pdf->SetXY(10, 30 + 4 * ($l_cnt + 3 + 1));
							$pdf->MultiCell(35, 4, _convert(sprintf('%01.2f', $value2)), 0, "L");
							
							$pdf->SetXY(50, 30 + 4 * ($l_cnt + 3 + 1));
							$pdf->MultiCell(35, 4, _convert(sprintf('%01.2f', $value2 * $key / 100)), 0, "L");
							
							$pdf->SetXY(90, 30 + 4 * ($l_cnt + 3 + 1));
							$pdf->MultiCell(35, 4, _convert(sprintf('%01.2f', $value2 * $key2 / 100)), 0, "L");
							
							$l_pnp += ($value2 * $key2 / 100);
							
							$l_cnt++;
							
						}
						
						$pdf->SetFont("ArialCE", "", 7);
						$pdf->SetXY(10, 30 + 4 * ($l_cnt + 3 + 1));
						$pdf->MultiCell(210, 4, _convert('----------------------------------------------------------------------------------------------------------------'), 0, "L");
						
						$l_cnt++;
						
					}
				}
				
			}
			
			
			
			
			$pdf->SetFont("ArialCE", "", 8);
			
			$pdf->SetXY(10, 30 + 4 * ($l_cnt + 3 + 1));
			$pdf->MultiCell(19, 4, _convert('PNP:'), 0, "L");
			
			$pdf->SetXY(30, 30 + 4 * ($l_cnt + 3 + 1));
			$pdf->MultiCell(35, 4, _convert(sprintf('%01.2f', $l_pnp).' EUR'), 0, "L");
			
			$l_cnt++;
			
		}
		
		
		
			
		$l_name = 'pregled_prodaje_po_normativu.pdf';
		$l_dest = $g_path.'app/webroot/res/files/'.$l_name;
		
		$pdf->Output($l_dest, 'F');
		
		
		$fp = @fopen($g_path.'app/webroot/res/files/'.str_replace('.pdf', '.csv', $l_name), 'wb');
		fwrite($fp, $l_xls);
		fclose($fp);
		
		
		return array('path' => $g_path.'app/webroot/res/files/', 'name' => $l_name);
	}

	function all_pregled_napojnice($date_start, $date_end){
		$g_path = get_item('select value from settings where name = "putanja.abs" ');
		$g_v = get_valuta();

		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');

		$pdf =& new FPDI('P', 'mm', 'A4');

		$pdf->AddFont("ArialCE", "B", "6488f5d4657b269b9f5770508e80e299_arialbd.php");
		$pdf->AddFont("ArialCE", "", "332cc83ebee36613c19564e6e776c81b_arial.php");

		$la_tmp_1 = explode('-', $date_start);
		$la_tmp_2 = explode('-', $date_end);

		$l_datum_1 = $la_tmp_1[0].'.'.$la_tmp_1[1].'.'.$la_tmp_1[2].'.';
		$l_datum_2 = $la_tmp_2[0].'.'.$la_tmp_2[1].'.'.$la_tmp_2[2].'.';

		$l_datum_db_1 = $la_tmp_1[2].'-'.$la_tmp_1[1].'-'.$la_tmp_1[0];
		$l_datum_db_2 = $la_tmp_2[2].'-'.$la_tmp_2[1].'-'.$la_tmp_2[0];
		$l_datum_from = $l_datum_db_1.' 00:00:00';
		$l_datum_to = $l_datum_db_2.' 23:59:59';

		$l_tip_prog = get_item('select value from settings where name = "tipprog" ');

		// add a page
		$pdf->AddPage();

		$pdf->SetFont("ArialCE", "B", 11);
		$pdf->SetTextColor(0, 0, 0);

		$x = get_item('select value from settings where name = "general.name" ').', '.
			get_n_rows(get_item('select value from settings where name = "invoice.header" '), 4);
		$pdf->MultiCell(210, 5, _convert($x), 0, "L");
		$pdf->Ln();

		$pdf->SetFont("ArialCE", "B", 10);
		$pdf->MultiCell(210, 4, _convert('Pregled napojnice'), 0, "L");
		$pdf->MultiCell(210, 4, _convert('Period od '.$l_datum_1.' do '.$l_datum_2), 0, "L");

		//Baksis START
		$baksisSQL = "SELECT 
					  t.user_id,
					  u.full_name AS user_full_name,
					  pt.type,
					  SUM(t.iznos) AS ukupno 
					FROM tips t
					LEFT JOIN users u ON u.id=t.user_id
					LEFT JOIN payment_types pt ON pt.id=t.payment_type_id
					WHERE 
					  t.iznos IS NOT NULL
					  AND DATE(t.date) BETWEEN '".$l_datum_from."' AND '".$l_datum_to."' 
					GROUP BY t.user_id, pt.type
					ORDER BY u.full_name, pt.type";
		//$baksis = get_data($baksisSQL);
		$baksisHandle = sql_exe($baksisSQL);
		$tmpBaksisRes = [];
		while ($row = mysql_fetch_array ($baksisHandle, MYSQL_ASSOC)){
			$tmpBaksisRes[] = $row;
		}
		$baksisRes = [];
		$totalBaksis = 0;
		foreach($tmpBaksisRes as $tmpBR){
			$baksisRes[$tmpBR['user_id']]['user_full_name'] = $tmpBR['user_full_name'];
			$baksisRes[$tmpBR['user_id']][$tmpBR['type']] = $tmpBR;
			$baksisRes[$tmpBR['user_id']]['ukupno'] += $tmpBR['ukupno'];
			$totalBaksis += $tmpBR['ukupno'];
		}
		$paymentTypes = [
			1 => 'Kartice',
			2 => 'Gotovina',
			3 => 'Ostalo',
			4 => 'Virman'
		];
		$pdf->MultiCell(210, 4, _convert('Rekapitulacija napojnice: '.sprintf('%1.2f', $totalBaksis).' '.$g_v), 0, "L");
		$pdf->Ln();
		$pdf->SetFont("ArialCE", "", 9);
		foreach($baksisRes as $br){
			$pdf->Cell(60, 4, _convert($br['user_full_name']), 0, 0, "L");
			$pdf->Cell(20, 4, sprintf('%5.2f', $br['ukupno']), 0, 0, "R");
			$pdf->Ln();

			foreach($paymentTypes as $ptKey=>$ptValue){
				$pdf->Cell(60, 4, _convert($ptValue), 0, 0, "L");
				$pdf->Cell(20, 4, sprintf('%5.2f', $br[$ptKey]['ukupno']), 0, 0, "R");
				$pdf->Ln();
			}
			$pdf->Ln();
		}

		$pdf->Ln();
		$pdf->Ln();

		$baksisSQL = "SELECT 
							  p.type, 
							  SUM(t.iznos) AS ukupan_iznos_tips 
							FROM payment_types p 
							JOIN tips t ON p.id = t.payment_type_id 
							WHERE DATE(t.date) BETWEEN '".$l_datum_from."' AND '".$l_datum_to."' 
							GROUP BY p.type
							ORDER BY p.type";
		$baksisHandle = sql_exe($baksisSQL);
		$tmpBaksisRes = [];
		$pdf->SetFont("ArialCE", "B", 10);
		$pdf->Write(10, 'Ukupni iznos napojnice: '.sprintf('%1.2f', $totalBaksis).' '.$g_v);
		$pdf->SetFont("ArialCE", "", 9);
		$pdf->Ln();
		while ($row = mysql_fetch_array ($baksisHandle, MYSQL_ASSOC)){
			$tmpBaksisRes[] = $row;
			$pdf->Cell(60, 4, _convert($paymentTypes[$row['type']]), 0, 0, "L");
			$pdf->Cell(20, 4, sprintf('%5.2f', $row['ukupan_iznos_tips']), 0, 0, "R");
			$pdf->Ln();
		}
		//Baksis END

		$l_name = 'pregled_napojnice.pdf';
		$l_dest = $g_path.'app/webroot/res/files/'.$l_name;

		$pdf->Output($l_dest,'F');
		return array('path' => $g_path.'app/webroot/res/files/', 'name' => $l_name);

	}
	
	function all_pregled_prodaje_po_nacinu_placanja($date_start, $date_end, $user_id){
		
		$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');
		
		$pdf =& new FPDI('P', 'mm', 'A4');
		
		$pdf->AddFont("ArialCE", "B", "6488f5d4657b269b9f5770508e80e299_arialbd.php");
		$pdf->AddFont("ArialCE", "", "332cc83ebee36613c19564e6e776c81b_arial.php");
		
		// set the sourcefile
		$pgNumber = $pdf->setSourceFile($g_path.'app/libs/fpdf/prazni-dokument.pdf');
		
		//$pdf->SetAutoPageBreak(true, 10);
		
		
		$l_pdv = get_item('select value from settings where name = "duty.free" ');
		
		if($l_pdv == 'no'){
			$l_pdv = 'false';
		}
		
		if($l_pdv == 'false'){
			$l_pdv_koment = get_item('select value from settings where name = "duty.comment" ');
		}else{
			$l_pdv_koment = '';
		}
		
		$x = get_item('select value from settings where name = "general.name" ').', '.
			get_n_rows(get_item('select value from settings where name = "invoice.header" '), 4);
		
		
		$la_tmp_1 = explode('-', $date_start);
		$la_tmp_2 = explode('-', $date_end);
		
		$l_datum_1 = $la_tmp_1[0].'.'.$la_tmp_1[1].'.'.$la_tmp_1[2].'.';
		$l_datum_2 = $la_tmp_2[0].'.'.$la_tmp_2[1].'.'.$la_tmp_2[2].'.';
		
		$l_datum_db_1 = $la_tmp_1[2].'-'.$la_tmp_1[1].'-'.$la_tmp_1[0];
		$l_datum_db_2 = $la_tmp_2[2].'-'.$la_tmp_2[1].'-'.$la_tmp_2[0];
		$l_datum_from = $l_datum_db_1.' 00:00:00';
		$l_datum_to = $l_datum_db_2.' 23:59:59';
		
		
		
		$la_uvjet = array('1', '2, 3, 4, 5, 7', '1, 2, 3, 4, 5, 7', '8', '6');
		$la_card_details = array('0', '1', '0', '0', '0');
		$la_skip_sum = array('0', '0', '1', '0', '0');
		
		$l_tip_prog = get_item('select value from settings where name = "tipprog" ');
		
		if($l_tip_prog == 'm'){
			$la_type_desc = array('Novčanice/Kovanice', 'Kartice', 'Gotovina (Novčanice+Kartice)', 'Ostalo', 'Transakcijski račun (Virman)');
		}
		if($l_tip_prog == 'u'){
			$la_type_desc = array('Gotovina (novčanice)', 'Gotovina (kartice)', 'Gotovina (Novčanice+Kartice)', 'Ostalo', 'Transakcijski račun (Virman)');
		}
		
		$la_tax_desc = array(
			'GOTOVINSKI PROMET (Novčanice/Kovanice)',
			'GOTOVINSKI PROMET (Kartice)',
			'Ukupno Gotovina (Novčanice+Kartice)',
			'PROMET OSTALO',
			'PROMET TRANSAKCIJSKI RAČUN (VIRMAN)',
		);
		
		$la_tax = array();
		$la_pn = array();
		
		for($k = 0; $k < sizeof($la_uvjet); $k++){
			$la_tax[$k] = array();
			$la_pn[$k] = 0;
		}
		
		$la_tax_ukup = array();
		$la_pn_ukup = 0;
		$la_cards = array();
			
		
		$l_min_rbr = 99999999999;
		$l_max_rbr = 0;
		
		for($k = 0; $k < sizeof($la_uvjet); $k++){
			$y = get_data('select * from sales where payment_type_id in ('.$la_uvjet[$k].') '.
				' and created between "'.$l_datum_from.'" and "'.$l_datum_to.'" ', 1);
			$l_id = '0';
			for($i = 0; isset($y['id']) && $i < sizeof($y['id']); $i++){
				$l_id .= ', '.$y['id'][$i];
				
				if($la_card_details[$k] == '1'){
					if(empty($la_cards[$y['payment_type_id'][$i]])){
						$la_cards[$y['payment_type_id'][$i]] = 0;
					}
					$la_cards[$y['payment_type_id'][$i]] += $y['price'][$i];
				}
			}
			
			
			if($l_tip_prog == 'u'){
				
				/*
				$z = get_data('select tax_groups.id, tax_groups.name, sale_items.sale_id, sale_items.amount, sale_items.base_price, '.
					' sale_items.discount_percent, sale_items.price, sale_items.tax_1, sale_items.tax_2 '.
					' from sale_items, products, tax_groups '.
					' where sale_items.product_id = products.id '.
					' and products.tax_group_id = tax_groups.id '.
					' and sale_items.sale_id in ('.$l_id.')  '.
					' order by sale_items.sale_id, sale_items.id', 1);
				*/
					
				
				if($user_id > 0){
					$l_cond = ' and sales.user_id = '.$user_id;
				}else{
					$l_cond = '';
				}
				
				$z = get_data('select tax_groups.id, tax_groups.name, sale_items.sale_id, sale_items.amount, sale_items.base_price, '.
					' sale_items.discount_percent, sale_items.price, sale_items.tax_1, sale_items.tax_2 '.
					' from sales, sale_items, products, tax_groups '.
					' where sales.id = sale_items.sale_id '.
					' and sale_items.product_id = products.id '.
					' and products.tax_group_id = tax_groups.id '.
					' and sale_items.sale_id in ('.$l_id.')  '.
					$l_cond.
					' order by sale_items.sale_id, sale_items.id', 1);
					
					
					
					
				
				$l_item = '';
				
				for($i = 0; isset($z['id']) && $i < sizeof($z['id']); $i++){
					if($l_pdv == 'false'){
						$z['tax_1'][$i] = 'no_pdv';
					}
					
					
					if(empty($la_tax[$k][$z['name'][$i]]['tax_1'][$z['tax_1'][$i]])){
						$la_tax[$k][$z['name'][$i]]['tax_1'][$z['tax_1'][$i]] = 0;
					}
					$la_tax[$k][$z['name'][$i]]['tax_1'][$z['tax_1'][$i]] += $z['price'][$i];
					
					if(empty($la_tax[$k][$z['name'][$i]]['tax_2'][$z['tax_1'][$i]][$z['tax_2'][$i]])){
						$la_tax[$k][$z['name'][$i]]['tax_2'][$z['tax_1'][$i]][$z['tax_2'][$i]] = 0;
					}
					$la_tax[$k][$z['name'][$i]]['tax_2'][$z['tax_1'][$i]][$z['tax_2'][$i]] += $z['price'][$i];
					
					
					
					if(empty($la_tax_ukup[$z['name'][$i]]['tax_1'][$z['tax_1'][$i]])){
						$la_tax_ukup[$z['name'][$i]]['tax_1'][$z['tax_1'][$i]] = 0;
					}
					if($la_skip_sum[$k] == '0'){
						$la_tax_ukup[$z['name'][$i]]['tax_1'][$z['tax_1'][$i]] += $z['price'][$i];
					}							
					
					if(empty($la_tax_ukup[$z['name'][$i]]['tax_2'][$z['tax_1'][$i]][$z['tax_2'][$i]])){
						$la_tax_ukup[$z['name'][$i]]['tax_2'][$z['tax_1'][$i]][$z['tax_2'][$i]] = 0;
					}
					if($la_skip_sum[$k] == '0'){
						$la_tax_ukup[$z['name'][$i]]['tax_2'][$z['tax_1'][$i]][$z['tax_2'][$i]] += $z['price'][$i];
					}
					
					
					
					if(empty($la_pn[$k])){
						$la_pn[$k] = 0;
					}
				}
				
				
			}
		}
		
		
		
		
		
		
		
		
		if($l_tip_prog == 'u'){
			for($i = 1; $i <= 1; $i++){
				// add a page
				$pdf->AddPage();

				// import page 1
				$tplIdx = $pdf->importPage($i);

				// use the imported page and place it at point 0,0 with a width of 210 mm
				$pdf->useTemplate($tplIdx, 0, 0, 210, 297);



				$pdf->SetFont("ArialCE", "B", 11);
				$pdf->SetTextColor(0, 0, 0);

				$pdf->SetXY(10, 10);
				$pdf->MultiCell(210, 5, _convert($x), 0, "L");

				$pdf->SetFont("ArialCE", "B", 10);
				$pdf->SetXY(10, 33);
				$pdf->MultiCell(210, 4, _convert('Pregled po nacinu placanja'), 0, "L");

				$pdf->SetXY(10, 37);
				$pdf->MultiCell(210, 4, _convert('Period od '.$l_datum_1.' do '.$l_datum_2), 0, "L");

				
				
				if($user_id > 0){
					
					$l_sql = 'select concat(full_name, ", ", username) as username '.
						' from users '.
						' where id =  '.$user_id;
					$l_user_name = get_item($l_sql);
					
					
					$pdf->SetXY(10, 41);
					$pdf->MultiCell(210, 4, _convert('OPERATER: '.$l_user_name), 0, "L");
				}
				
				
				
				
				
				
				
				
				
				$pdf->SetFont("ArialCE", "B", 10);
				$pdf->SetXY(10, 52);
				$pdf->MultiCell(210, 5, _convert('PROMETI PO NAČINIMA PLAĆANJA:'), 0, "L");
				$pdf->SetXY(10, 56);
				$pdf->MultiCell(210, 5, _convert('-------------------------------------------------------------------------'), 0, "L");
				
				$pdf->SetFont("ArialCE", "", 9);
				
				
				
				$la_ukup_sve = array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
				$l_ukup_sve_sve = 0;
				
				for($k = 0; $k < sizeof($la_uvjet); $k++){
					foreach($la_tax[$k] as $key => $value){
						foreach($value['tax_1'] as $key2 => $value2){
							$la_ukup_sve[$k] += $value2;
						}
					}
				}
				for($k = 0; $k < sizeof($la_uvjet); $k++){
					if($k != 2){
						$l_ukup_sve_sve += $la_ukup_sve[$k]; 
					}
				}
				
				
				
				$l_cnt = 0;
				
				/*
				$pdf->SetXY(10, 60 + 4 * $l_cnt);
				$pdf->MultiCell(50, 5, _convert($la_type_desc[2]), 0, "L");
				
				$pdf->SetXY(67, 60 + 4 * $l_cnt);
				$pdf->MultiCell(30, 5, _convert(sprintf('%01.2f', $la_ukup_sve[2]).' Kn'), 0, "R");
				
				$l_cnt++;
				*/
				
				for($k = 0; $k < sizeof($la_type_desc); $k++){
					if($k == 2){
						continue;
					}
					$pdf->SetXY(10, 60 + 4 * $l_cnt);
					$pdf->MultiCell(50, 5, _convert($la_type_desc[$k]), 0, "L");
					
					$pdf->SetXY(67, 60 + 4 * $l_cnt);
					$pdf->MultiCell(30, 5, _convert(sprintf('%01.2f', $la_ukup_sve[$k]).' EUR'), 0, "R");
					
					$l_cnt++;
					
					
					if($k == 1){
						$pdf->SetFont("ArialCE", "", 8);
						$x_card = get_data('select id, name from payment_types where type = 1 and enabled = 1 order by id ', 1);
						for($kk = 0; $kk < sizeof($x_card['id']); $kk++){
							$pdf->SetXY(12, 60 + 4 * $l_cnt);
							$pdf->MultiCell(140, 4, _convert($x_card['name'][$kk].': '.sprintf('%01.2f', $la_cards[$x_card['id'][$kk]]).' EUR'), 0, "L");
							$l_cnt++;
						}
						$pdf->SetFont("ArialCE", "", 9);
					}
					
				}
				
				$pdf->SetXY(10, 60 + 4 * $l_cnt);
				$pdf->MultiCell(210, 5, _convert('---------------------------------------------------------------------------------'), 0, "L");
				
				$pdf->SetXY(10, 63 + 4 * $l_cnt);
				$pdf->MultiCell(210, 5, _convert('SVEUKUPNO'), 0, "L");
				$pdf->SetXY(67, 63 + 4 * $l_cnt);
				$pdf->MultiCell(30, 5, _convert(sprintf('%01.2f', $l_ukup_sve_sve).' EUR'), 0, "R");
				
				$pdf->SetXY(10, 66 + 4 * $l_cnt);
				$pdf->MultiCell(210, 5, _convert('---------------------------------------------------------------------------------'), 0, "L");
				
				
				
				$pdf->SetFont("ArialCE", "B", 10);
				$l_cnt++;
				$l_cnt++;
				$l_cnt++;
				$pdf->SetXY(10, 66 + 4 * $l_cnt);
				$pdf->MultiCell(210, 5, _convert('PROMETI I POREZNA RAZRADA:'), 0, "L");
				
				$pdf->SetFont("ArialCE", "", 9);
				
				
				
				
				
				
				$l_cnt = 12 + sizeof($la_type_desc);
				
				
				
				$l_ukup_sve = 0;
				
				$la_rekapitulacija = array();
				
				$l_pnp_ukup = 0;
				$l_ukup_sve_sve = 0;
				
				for($k = 0; $k < sizeof($la_uvjet); $k++){
					
					if(50 + 4 * ($l_cnt + 3 + 1) > 190){
						$pdf->AddPage();
						$l_cnt = -8;
					}
					
					$pdf->SetFont("ArialCE", "", 9);
					
					$pdf->SetXY(10, 50 + 4 * $l_cnt);
					$pdf->MultiCell(210, 4, _convert('--------------------------------------------------------------------------------------------------------------------------------------------'), 0, "L");
					
					$pdf->SetXY(10, 50 + 4 * ($l_cnt + 1));
					$pdf->MultiCell(100, 4, _convert($la_tax_desc[$k]), 0, "L");
					
					
					if($k == 1){
						$pdf->SetXY(58, 50 + 4 * ($l_cnt + 1));
						$pdf->MultiCell(40, 4, _convert(sprintf('%01.2f', $la_ukup_sve[$k]).' EUR'), 0, "L");
						
						
						$pdf->SetFont("ArialCE", "", 8);
						$x_card = get_data('select id, name from payment_types where type = 1 and enabled = 1 order by id ', 1);
						for($kk = 0; $kk < sizeof($x_card['id']); $kk++){
							$l_cnt++;
							$pdf->SetXY(12, 50 + 4 * ($l_cnt + 1));
							$pdf->MultiCell(140, 4, _convert($x_card['name'][$kk].': '.sprintf('%01.2f', $la_cards[$x_card['id'][$kk]]).' EUR'), 0, "L");
						}
						$pdf->SetFont("ArialCE", "", 9);
					}
					
					
					$pdf->SetFont("ArialCE", "", 9);
					$pdf->SetXY(10, 50 + 4 * ($l_cnt + 2));
					$pdf->MultiCell(210, 4, _convert('--------------------------------------------------------------------------------------------------------------------------------------------'), 0, "L");
					
					
					
					$pdf->SetFont("ArialCE", "B", 8);
					$pdf->SetXY(10, 50 + 4 * ($l_cnt + 3));
					$pdf->MultiCell(35, 4, _convert('Opis'), 0, "L");
					
					$pdf->SetXY(50, 50 + 4 * ($l_cnt + 3));
					$pdf->MultiCell(19, 4, _convert('Osnovica'), 0, "L");
					
					$pdf->SetXY(70, 50 + 4 * ($l_cnt + 3));
					$pdf->MultiCell(14, 4, _convert('PDV'), 0, "L");
					
					$pdf->SetXY(85, 50 + 4 * ($l_cnt + 3));
					$pdf->MultiCell(19, 4, _convert('PDV EUR'), 0, "L");
					
					$pdf->SetXY(105, 50 + 4 * ($l_cnt + 3));
					$pdf->MultiCell(14, 4, _convert('PNP'), 0, "L");
					
					$pdf->SetXY(120, 50 + 4 * ($l_cnt + 3));
					$pdf->MultiCell(19, 4, _convert('PNP EUR'), 0, "L");
						
					$pdf->SetXY(140, 50 + 4 * ($l_cnt + 3));
					$pdf->MultiCell(25, 4, _convert('Ukupno'), 0, "L");
					
					
					
					$pdf->SetFont("ArialCE", "", 8);
					
					$l_ukup_sve = 0;
					$la_ukup = array();
					
					$l_osnovica_sum = 0;
					$l_pdv_kn = 0;
					$l_pnp_kn = 0;
					
					foreach($la_tax[$k] as $key => $value){
						foreach($value['tax_1'] as $key2 => $value2){
							
							$l_ukup_porez = 0;
							
							$pdf->SetXY(10, 50 + 4 * ($l_cnt + 3 + 1));
							$pdf->MultiCell(35, 4, _convert($key), 0, "L");
							
							$l_ukup_porez += $key2;
							
							$l_key_3_move = 0;
							
							// zbog strukture podataka, samo jedan zapis ima (iako koristim foreach petlju, najbrze mi bilo iskoristit tu petlju)
							foreach($la_tax[$k][$key]['tax_2'][$key2] as $key3 => $value3){
								
								$l_key_3_move = $key3;
								
								$pdf->SetXY(105, 50 + 4 * ($l_cnt + 3 + 1));
								$pdf->MultiCell(14, 4, _convert($key3), 0, "L");
								$l_ukup_porez += $key3;
								
								
								if($k != 2){
									if(empty($la_rekapitulacija[$key2])){
										$la_rekapitulacija[$key2] = array();
										$la_rekapitulacija[$key2]['pdv'] = 0;
										$la_rekapitulacija[$key2]['pnp'] = array();
									}
								}
								if(empty($la_ukup[$key2])){
									$la_ukup[$key2] = array();
									$la_ukup[$key2]['pdv'] = 0;
									$la_ukup[$key2]['pnp'] = array();
								}
								
								$l_osnovica = $value3 / (1 + ($key2 + $key3) / 100);
								
								$la_ukup[$key2]['pdv'] += $l_osnovica;
								if(empty($la_ukup[$key2]['pnp'][$key3])){
									$la_ukup[$key2]['pnp'][$key3] = 0;
								}
								$la_ukup[$key2]['pnp'][$key3] += $l_osnovica;
								
								if($k != 2){
									$la_rekapitulacija[$key2]['pdv'] += $l_osnovica;
									if(empty($la_rekapitulacija[$key2]['pnp'][$key3])){
										$la_rekapitulacija[$key2]['pnp'][$key3] = 0;
									}
									$la_rekapitulacija[$key2]['pnp'][$key3] += $l_osnovica;
								}
							}
							
							$pdf->SetXY(46, 50 + 4 * ($l_cnt + 3 + 1));
							$pdf->MultiCell(19, 4, _convert(sprintf('%01.2f', $value2 / (1 + $l_ukup_porez / 100))), 0, "R");
							$l_osnovica_sum += $value2 / (1 + $l_ukup_porez / 100);
							
							$pdf->SetXY(70, 50 + 4 * ($l_cnt + 3 + 1));
							//$pdf->MultiCell(14, 4, _convert($key2), 0, "L");
							$pdf->MultiCell(19, 4, _convert(($key2 == 'no_pdv' ? '  -' : $key2.'%:')), 0, "L");
							
							$pdf->SetXY(79, 50 + 4 * ($l_cnt + 3 + 1));
							$pdf->MultiCell(19, 4, _convert(sprintf('%01.2f', $value2 / (1 + $l_ukup_porez / 100) * $key2 / 100)), 0, "R");
							$l_pdv_kn += $value2 / (1 + $l_ukup_porez / 100) * $key2 / 100;
							
							$pdf->SetXY(114, 50 + 4 * ($l_cnt + 3 + 1));
							$pdf->MultiCell(19, 4, _convert(sprintf('%01.2f', $value2 / (1 + $l_ukup_porez / 100) * $l_key_3_move / 100)), 0, "R");
							$l_pnp_kn += $value2 / (1 + $l_ukup_porez / 100) * $l_key_3_move / 100;
							
							$pdf->SetXY(133, 50 + 4 * ($l_cnt + 3 + 1));
							$pdf->MultiCell(19, 4, _convert(sprintf('%01.2f', $value2)), 0, "R");
							
							$l_ukup_sve += $value2;
							
							$l_cnt++;
						}
						
					}
					
					
					$pdf->SetFont("ArialCE", "B", 8);
					
					$pdf->SetXY(10, 50 + 4 * ($l_cnt + 3 + 1));
					$pdf->MultiCell(35, 4, _convert('Ukupno EUR'), 0, "L");
					
					$pdf->SetXY(46, 50 + 4 * ($l_cnt + 3 + 1));
					$pdf->MultiCell(19, 4, _convert(sprintf('%01.2f', $l_osnovica_sum)), 0, "R");
					
					$pdf->SetXY(79, 50 + 4 * ($l_cnt + 3 + 1));
					$pdf->MultiCell(19, 4, _convert(sprintf('%01.2f', $l_pdv_kn)), 0, "R");
					
					$pdf->SetXY(114, 50 + 4 * ($l_cnt + 3 + 1));
					$pdf->MultiCell(19, 4, _convert(sprintf('%01.2f', $l_pnp_kn)), 0, "R");
					
					$pdf->SetXY(133, 50 + 4 * ($l_cnt + 3 + 1));
					$pdf->MultiCell(19, 4, _convert(sprintf('%01.2f', $l_ukup_sve)), 0, "R");
					
					if($k != 2){
						$l_ukup_sve_sve += $l_ukup_sve; 
					}
					
					$l_cnt++;
					
					$pdf->SetFont("ArialCE", "", 7);
					$pdf->SetXY(10, 50 + 4 * ($l_cnt + 3 + 1));
					$pdf->MultiCell(210, 4, _convert('----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------'), 0, "L");
					
					$l_cnt++;
					
					
					$l_pnp = 0;
					foreach($la_ukup as $key => $value){
						
						$pdf->SetFont("ArialCE", "", 8);
						
						$pdf->SetXY(10, 50 + 4 * ($l_cnt + 3 + 1));
						//$pdf->MultiCell(19, 4, _convert('PDV '.$key.'%:'), 0, "L");
						$pdf->MultiCell(19, 4, _convert('PDV '.($key == 'no_pdv' ? '  -' : $key.'%:')), 0, "L");
						
						$pdf->SetXY(30, 50 + 4 * ($l_cnt + 3 + 1));
						$pdf->MultiCell(35, 4, _convert((sprintf('%01.2f', $value['pdv'] * $key / 100)).' EUR'), 0, "L");
						
						$l_cnt++;
						
						$pdf->SetXY(10, 50 + 4 * ($l_cnt + 3 + 1));
						$pdf->MultiCell(35, 4, _convert('Osnovica'), 0, "L");
						
						$pdf->SetXY(50, 50 + 4 * ($l_cnt + 3 + 1));
						$pdf->MultiCell(35, 4, _convert('PDV'), 0, "L");
						
						$pdf->SetXY(90, 50 + 4 * ($l_cnt + 3 + 1));
						$pdf->MultiCell(35, 4, _convert('PNP'), 0, "L");
						
						$l_cnt++;
						
						foreach($la_ukup[$key]['pnp'] as $key2 => $value2){
							$pdf->SetXY(10, 50 + 4 * ($l_cnt + 3 + 1));
							$pdf->MultiCell(35, 4, _convert(sprintf('%01.2f', $value2)), 0, "L");
							
							$pdf->SetXY(50, 50 + 4 * ($l_cnt + 3 + 1));
							$pdf->MultiCell(35, 4, _convert(sprintf('%01.2f', $value2 * $key / 100)), 0, "L");
							
							$pdf->SetXY(90, 50 + 4 * ($l_cnt + 3 + 1));
							$pdf->MultiCell(35, 4, _convert(sprintf('%01.2f', $value2 * $key2 / 100)), 0, "L");
							$l_pnp += ($value2 * $key2 / 100);
							if($k != 2){
								$l_pnp_ukup += ($value2 * $key2 / 100);
							}
							
							$l_cnt++;
						}
						
						$pdf->SetFont("ArialCE", "", 7);
						$pdf->SetXY(10, 50 + 4 * ($l_cnt + 3 + 1));
						$pdf->MultiCell(210, 4, _convert('----------------------------------------------------------------------------------------------------------------'), 0, "L");
						
						$l_cnt++;
					}
					
					$pdf->SetFont("ArialCE", "", 8);
					
					$pdf->SetXY(10, 50 + 4 * ($l_cnt + 3 + 1));
					$pdf->MultiCell(19, 4, _convert('PNP:'), 0, "L");
					
					$pdf->SetXY(30, 50 + 4 * ($l_cnt + 3 + 1));
					$pdf->MultiCell(35, 4, _convert(sprintf('%01.2f', $l_pnp).' EUR'), 0, "L");
					
					$l_cnt++;
					
					
					
					$l_cnt++;
					$l_cnt++;
					$l_cnt++;
					$l_cnt++;
					$l_cnt++;
					
				}
				
				
				$l_cnt++;
				$l_cnt++;
				
				
				$l_sveukupno = 0;
				foreach($la_rekapitulacija as $key => $value){
					$l_sveukupno += $value['pdv'];
				}
				
				$pdf->SetFont("ArialCE", "", 9);
				
				$pdf->SetXY(10, 50 + 4 * $l_cnt);
				$pdf->MultiCell(210, 4, _convert('--------------------------------------------------------------------------------------------------------------------------------------------'), 0, "L");
				
				$pdf->SetXY(10, 50 + 4 * ($l_cnt + 1));
				$pdf->MultiCell(70, 4, _convert('SVEUKUPNO: '.sprintf('%01.2f', $l_ukup_sve_sve).' EUR'), 0, "L");
				
				
				$pdf->SetFont("ArialCE", "", 9);
				$pdf->SetXY(10, 50 + 4 * ($l_cnt + 2));
				$pdf->MultiCell(210, 4, _convert('--------------------------------------------------------------------------------------------------------------------------------------------'), 0, "L");
				
				foreach($la_rekapitulacija as $key => $value){
					
					$pdf->SetFont("ArialCE", "", 8);
					
					$pdf->SetXY(10, 50 + 4 * ($l_cnt + 3 + 1));
					//$pdf->MultiCell(19, 4, _convert('PDV '.$key.'%:'), 0, "L");
					$pdf->MultiCell(19, 4, _convert('PDV '.($key == 'no_pdv' ? '  -' : $key.'%:')), 0, "L");
					
					$pdf->SetXY(30, 50 + 4 * ($l_cnt + 3 + 1));
					$pdf->MultiCell(35, 4, _convert((sprintf('%01.2f', $value['pdv'] * $key / 100)).' EUR'), 0, "L");
					
					$l_cnt++;
					
					$pdf->SetXY(10, 50 + 4 * ($l_cnt + 3 + 1));
					$pdf->MultiCell(35, 4, _convert('Osnovica'), 0, "L");
					
					$pdf->SetXY(50, 50 + 4 * ($l_cnt + 3 + 1));
					$pdf->MultiCell(35, 4, _convert('PDV'), 0, "L");
					
					$pdf->SetXY(90, 50 + 4 * ($l_cnt + 3 + 1));
					$pdf->MultiCell(35, 4, _convert('PNP'), 0, "L");
					
					$l_cnt++;
					
					foreach($la_rekapitulacija[$key]['pnp'] as $key2 => $value2){
						$pdf->SetXY(10, 50 + 4 * ($l_cnt + 3 + 1));
						$pdf->MultiCell(35, 4, _convert(sprintf('%01.2f', $value2)), 0, "L");
						
						$pdf->SetXY(50, 50 + 4 * ($l_cnt + 3 + 1));
						$pdf->MultiCell(35, 4, _convert(sprintf('%01.2f', $value2 * $key / 100)), 0, "L");
						
						$pdf->SetXY(90, 50 + 4 * ($l_cnt + 3 + 1));
						$pdf->MultiCell(35, 4, _convert(sprintf('%01.2f', $value2 * $key2 / 100)), 0, "L");
						
						$l_cnt++;
					}
					
					$pdf->SetFont("ArialCE", "", 7);
					$pdf->SetXY(10, 50 + 4 * ($l_cnt + 3 + 1));
					$pdf->MultiCell(210, 4, _convert('----------------------------------------------------------------------------------------------------------------'), 0, "L");
					
					$l_cnt++;
				}
				
				$pdf->SetFont("ArialCE", "", 8);
				
				$pdf->SetXY(10, 50 + 4 * ($l_cnt + 3 + 1));
				$pdf->MultiCell(19, 4, _convert('PNP:'), 0, "L");
				
				$pdf->SetXY(30, 50 + 4 * ($l_cnt + 3 + 1));
				$pdf->MultiCell(35, 4, _convert(sprintf('%01.2f', $l_pnp_ukup).' EUR'), 0, "L");
				
				$l_cnt++;
				
				$l_cnt++;
				$l_cnt++;
				$l_cnt++;
				$l_cnt++;
				$l_cnt++;
				$l_cnt++;

				$l_polog = get_item('select value from settings where name = "general.polog_blagajne" ');
				
				$pdf->SetXY(10, 50 + 4 * (++$l_cnt));
				$pdf->MultiCell(210, 4, _convert('Polog novcanica/kovanica: '.sprintf('%01.2f', $l_polog).' EUR'), 0, "L");
				
				$pdf->SetXY(10, 50 + 4 * (++$l_cnt));
				$pdf->MultiCell(210, 4, _convert($l_pdv_koment), 0, "L");
					
			}
		}
		
		$l_name = 'pregled_prodaje_po_nacinu_placanja.pdf';
		$l_dest = $g_path.'app/webroot/res/files/'.$l_name;
		
		$pdf->Output($l_dest, 'F');
		
		return array('path' => $g_path.'app/webroot/res/files/', 'name' => $l_name);
	}
	
	
	
	
	function all_pregled_nabave_materijalno($date_start, $date_end){
		
		
		$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');
		
		$pdf =& new FPDI('P', 'mm', 'A4');
		
		$pdf->AddFont("ArialCE", "B", "6488f5d4657b269b9f5770508e80e299_arialbd.php");
		$pdf->AddFont("ArialCE", "", "332cc83ebee36613c19564e6e776c81b_arial.php");
		
		// set the sourcefile
		$pgNumber = $pdf->setSourceFile($g_path.'app/libs/fpdf/prazni-dokument.pdf');
		
		//$pdf->SetAutoPageBreak(true, 10);
		
		
		
		$l_tip_prog = get_item('select value from settings where name = "tipprog" ');
		
		
		$l_pdv = get_item('select value from settings where name = "duty.free" ');
		
		if($l_pdv == 'no'){
			$l_pdv = 'false';
		}
		
		if($l_pdv == 'false'){
			$l_pdv_koment = get_item('select value from settings where name = "duty.comment" ');
		}else{
			$l_pdv_koment = '';
		}
		
		$x = get_item('select value from settings where name = "general.name" ').', '.
			get_n_rows(get_item('select value from settings where name = "invoice.header" '), 4);
		
		
		$la_tmp = explode('-', $date_start);
		$la_tmp2 = explode('-', $date_end);
		
		$l_datum = $la_tmp[0].'.'.$la_tmp[1].'.'.$la_tmp[2].'.';
		$l_datum2 = $la_tmp2[0].'.'.$la_tmp2[1].'.'.$la_tmp2[2].'.';
		
		$l_datum_db = $la_tmp[2].'-'.$la_tmp[1].'-'.$la_tmp[0];
		$l_datum_db2 = $la_tmp2[2].'-'.$la_tmp2[1].'-'.$la_tmp2[0];
		$l_datum_from = $l_datum_db.' 00:00:00';
		$l_datum_to = $l_datum_db2.' 23:59:59';
		
		
		
		
			for($i = 1; $i <= 1; $i++){
				// add a page
				$pdf->AddPage();
				
				// import page 1
				$tplIdx = $pdf->importPage($i);
				
				// use the imported page and place it at point 0,0 with a width of 210 mm
				$pdf->useTemplate($tplIdx, 0, 0, 210, 297);
				
				
				
				$pdf->SetFont("ArialCE", "B", 11);
				$pdf->SetTextColor(0, 0, 0);
				
				$pdf->SetXY(10, 10);
				$pdf->MultiCell(210, 5, _convert($x), 0, "L");
				
				$pdf->SetXY(10, 35);
				$pdf->MultiCell(210, 5, _convert('PREGLED NABAVE ROBA'), 0, "L");
				
				$pdf->SetXY(10, 40);
				$pdf->MultiCell(210, 5, _convert('Period od '.$l_datum.' do '.$l_datum2), 0, "L");
				
				
				$pdf->SetFont("ArialCE", "B", 8);
				
				$pdf->SetXY(10, 50);
				$pdf->MultiCell(74, 5, _convert('ARTIKL'), 'B', "L");
				
				$pdf->SetXY(84, 50);
				$pdf->MultiCell(20, 5, _convert('KOLIČINA'), 'B', "C");
				
				$pdf->SetXY(104, 50);
				$pdf->MultiCell(20, 5, _convert('NC EUR'), 'B', "R");
				
				$pdf->SetXY(124, 50);
				$pdf->MultiCell(35, 5, _convert('Ukupno EUR po NC*'), 'B', "R");
				
				$pdf->SetFont("ArialCE", "", 8);
				
				
				$y = get_data(
					' SELECT articles.name, SUM(receiving_items.amount) AS amount, articles.price_buy, '.
							' SUM(receiving_items.amount * articles.price_buy) AS sum_price_buy '.
					' FROM receivings, receiving_items, articles '.
					' WHERE receivings.id = receiving_items.receiving_id '.
					' AND receiving_items.article_id = articles.id '.
					' AND receivings.type = 0 '.
					' AND receivings.document_date BETWEEN "'.$l_datum_from.'" and "'.$l_datum_to.'" '.
					' GROUP BY 1, 3 '.
					' ORDER BY articles.name '.
					'', 1);
					
				$l_cnt = 0;
				$l_cnt++;
				$l_cnt_2 = 0;
				
				$l_sum = 0;
				
				for($j = 0; isset($y['name']) && $j < sizeof($y['name']); $j++){
					
					if(50 + 3 * $l_cnt + $l_cnt_2 > 266){
						$pdf->AddPage();
						$l_cnt = -13;
						$l_cnt_2 = 0;
					}
					
					$pdf->SetXY(10, 53 + 3 * $l_cnt + $l_cnt_2);
					$pdf->MultiCell(73, 4, _convert($y['name'][$j]), 0, "L");
					
					$pdf->SetXY(84, 53 + 3 * $l_cnt + $l_cnt_2);
					$pdf->MultiCell(20, 4, _convert($y['amount'][$j]), 0, "R");
					
					$pdf->SetXY(104, 53 + 3 * $l_cnt + $l_cnt_2);
					$pdf->MultiCell(20, 4, _convert($y['price_buy'][$j]), 0, "R");
					
					$pdf->SetXY(124, 53 + 3 * $l_cnt + $l_cnt_2);
					$pdf->MultiCell(35, 4, _convert(sprintf('%01.2f', $y['sum_price_buy'][$j])), 0, "R");
					
					$l_sum += $y['sum_price_buy'][$j];
					
					$l_cnt++;
				}
				
				
				$pdf->SetFont("ArialCE", "B", 8);
				
				
				
				$pdf->SetXY(10, 55 + 3 * $l_cnt + $l_cnt_2);
				$pdf->MultiCell(114, 5, _convert('SVEUKUPNO nabava za period (EUR):'), 'BT', "L");
				
				$pdf->SetXY(124, 55 + 3 * $l_cnt + $l_cnt_2);
				$pdf->MultiCell(35, 5, _convert(sprintf('%01.2f', $l_sum).' *'), 'BT', "R");
				
				$l_cnt++;
				$l_cnt++;
				
				$pdf->SetXY(10, 55 + 3 * $l_cnt + $l_cnt_2);
				$pdf->MultiCell(114, 5, _convert('* izračun po srednjoj nabavnoj cijeni artikala'), 0, "L");
				
				
			}

		$l_name = 'pregled_nabave_materijalno.pdf';
		$l_dest = $g_path.'app/webroot/res/files/'.$l_name;
		
		$pdf->Output($l_dest, 'F');
		
		return array('path' => $g_path.'app/webroot/res/files/', 'name' => $l_name);
}

	
	
	// dodaje jedan <...TaxSubtotal> blok (osnovica, porez, stopa) u mapu po stopi
	function f2_dodaj_stopu(&$map, $blk){
		$osn = 0;
		$tax = 0;
		$rk = '0.00';
		if(preg_match('/<cbc:TaxableAmount[^>]*>([\-\d.]+)<\/cbc:TaxableAmount>/', $blk, $m)){
			$osn = floatval($m[1]);
		}
		if(preg_match('/<cbc:TaxAmount[^>]*>([\-\d.]+)<\/cbc:TaxAmount>/', $blk, $m)){
			$tax = floatval($m[1]);
		}
		if(preg_match('/<cbc:Percent>([\-\d.]+)<\/cbc:Percent>/', $blk, $m)){
			$rk = sprintf('%01.2f', floatval($m[1]));
		}
		if(!isset($map[$rk])){
			$map[$rk] = array('osn' => 0, 'tax' => 0);
		}
		$map[$rk]['osn'] += $osn;
		$map[$rk]['tax'] += $tax;
	}
	
	
	// raspodijeli točan iznos poreza (po stopama) na retke prema osnovici, s ispravkom
	// zaokruživanja na najveći redak - da zbroj redaka bude točno jednak službenom iznosu.
	function f2_alociraj(&$rows, $rate_field, $out_field, $osn_field, $targets){
		$sum = array();
		$big = array();
		foreach($rows as $i => $r){
			$rk = sprintf('%01.2f', $r[$rate_field]);
			if(isset($targets[$rk])){
				$val = round($r[$osn_field] * floatval($rk) / 100, 2);
				if(!isset($sum[$rk])){ $sum[$rk] = 0; }
				$sum[$rk] += $val;
				if(!isset($big[$rk]) || $r[$osn_field] > $rows[$big[$rk]][$osn_field]){
					$big[$rk] = $i;
				}
			}else{
				$val = 0;
			}
			$rows[$i][$out_field] = $val;
		}
		foreach($targets as $rk => $t){
			if(isset($big[$rk])){
				$diff = round(floatval($t) - (isset($sum[$rk]) ? $sum[$rk] : 0), 2);
				if($diff != 0){
					$rows[$big[$rk]][$out_field] = round($rows[$big[$rk]][$out_field] + $diff, 2);
				}
			}
		}
	}
	
	
	// raspodijeli UKUPNI PNP na retke koji imaju PNP stopu (>0) prema osnovici;
	// ako nijedan redak nema stopu, cijeli PNP ide na redak s najvećom osnovicom.
	function f2_alociraj_pnp(&$rows, $total){
		$total = round($total, 2);
		foreach($rows as $i => $r){ $rows[$i]['pnp'] = 0; }
		$idx = array(); $base = 0;
		foreach($rows as $i => $r){ if(floatval($r['pnprate']) > 0){ $idx[] = $i; $base += $r['osn']; } }
		if(sizeof($idx) == 0){
			foreach($rows as $i => $r){ $idx[] = $i; $base += $r['osn']; }
		}
		if($total == 0 || $base <= 0 || sizeof($idx) == 0){ return; }
		$sum = 0; $big = $idx[0];
		foreach($idx as $i){
			$val = round($total * $rows[$i]['osn'] / $base, 2);
			$rows[$i]['pnp'] = $val; $sum += $val;
			if($rows[$i]['osn'] > $rows[$big]['osn']){ $big = $i; }
		}
		$diff = round($total - $sum, 2);
		if($diff != 0){ $rows[$big]['pnp'] = round($rows[$big]['pnp'] + $diff, 2); }
	}
	
	
	function f2_tablica_zaglavlje($pdf, $fy, $g_v, $l_pdv, $line_wide){
		$pdf->SetFont("ArialCE", "", 9);
		$pdf->SetXY(10, $fy);
		$pdf->MultiCell(210, 4, _convert($line_wide), 0, "L");
		$fy += 4;
		
		$pdf->SetFont("ArialCE", "B", 8);
		$pdf->SetXY(10, $fy);
		$pdf->MultiCell(35, 4, _convert('Opis'), 0, "L");
		$pdf->SetXY(50, $fy);
		$pdf->MultiCell(19, 4, _convert('Osnovica'), 0, "L");
		if($l_pdv != 'false'){
			$pdf->SetXY(70, $fy);
			$pdf->MultiCell(14, 4, _convert('PDV'), 0, "L");
			$pdf->SetXY(85, $fy);
			$pdf->MultiCell(19, 4, _convert('PDV '.$g_v), 0, "L");
		}
		$pdf->SetXY(105, $fy);
		$pdf->MultiCell(14, 4, _convert('PNP'), 0, "L");
		$pdf->SetXY(120, $fy);
		$pdf->MultiCell(19, 4, _convert('PNP '.$g_v), 0, "L");
		$pdf->SetXY(140, $fy);
		$pdf->MultiCell(25, 4, _convert('Ukupno'), 0, "L");
		$fy += 4;
		
		$pdf->SetFont("ArialCE", "", 9);
		$pdf->SetXY(10, $fy);
		$pdf->MultiCell(210, 4, _convert($line_wide), 0, "L");
		$fy += 4;
		
		return $fy;
	}
	
	
	function f2_artikli_zaglavlje($pdf, $fy, $g_v, $l_pdv, $line_wide){
		$pdf->SetFont("ArialCE", "", 9);
		$pdf->SetXY(10, $fy);
		$pdf->MultiCell(210, 4, _convert($line_wide), 0, "L");
		$fy += 4;
		
		$pdf->SetFont("ArialCE", "B", 8);
		$pdf->SetXY(10, $fy);
		$pdf->MultiCell(58, 4, _convert('Artikl'), 0, "L");
		$pdf->SetXY(68, $fy);
		$pdf->MultiCell(13, 4, _convert('Kol.'), 0, "R");
		$pdf->SetXY(83, $fy);
		$pdf->MultiCell(18, 4, _convert('Osnovica'), 0, "L");
		if($l_pdv != 'false'){
			$pdf->SetXY(101, $fy);
			$pdf->MultiCell(10, 4, _convert('PDV%'), 0, "R");
			$pdf->SetXY(111, $fy);
			$pdf->MultiCell(16, 4, _convert('PDV '.$g_v), 0, "L");
		}
		$pdf->SetXY(128, $fy);
		$pdf->MultiCell(16, 4, _convert('PNP '.$g_v), 0, "L");
		$pdf->SetXY(146, $fy);
		$pdf->MultiCell(22, 4, _convert('Ukupno'), 0, "L");
		$fy += 4;
		
		$pdf->SetFont("ArialCE", "", 9);
		$pdf->SetXY(10, $fy);
		$pdf->MultiCell(210, 4, _convert($line_wide), 0, "L");
		$fy += 4;
		
		return $fy;
	}
	
	
	// F2 - E-RAČUNI (fiskalizirani): total + razrada po grupama (kao F1) + razrada po stopama + tablica po artiklima.
	// Poseban, neovisan dio - ne dira postojeći F1 izvještaj.
	function f2_eracun_rekapitulacija($pdf, $x, $g_v, $l_pdv, $l_datum, $l_datum2, $l_datum_db, $l_datum_db2){
		
		$f2_table_exists = get_item('SHOW TABLES LIKE "eracun" ');
		if($f2_table_exists == ''){
			return;
		}
		
		$f2_where = ' where (external_status = "FISCALIZATION:OK" or external_status = "FISCALIZATION_PAYMENT_REPORT:OK") '.
			' and (tip_poruke is null or tip_poruke != "Kopija") '.
			' and DATE(sent_at) >= "'.$l_datum_db.'" and DATE(sent_at) <= "'.$l_datum_db2.'" ';
		
		$f2_suma = get_item('select COALESCE(SUM(iznos), 0) from eracun '.$f2_where);
		if($f2_suma === '' || $f2_suma === null){
			$f2_suma = 0;
		}
		
		$f2_join_where = ' where (e.external_status = "FISCALIZATION:OK" or e.external_status = "FISCALIZATION_PAYMENT_REPORT:OK") '.
			' and (e.tip_poruke is null or e.tip_poruke != "Kopija") '.
			' and DATE(e.sent_at) >= "'.$l_datum_db.'" and DATE(e.sent_at) <= "'.$l_datum_db2.'" ';
		
		$f2_stavke_exists = get_item('SHOW TABLES LIKE "eracun_stavke" ');
		
		// ===== TOČNI iznosi iz UBL XML-a (kako ih je izdavatelj zaokružio po računu) =====
		// PDV: cac:TaxSubtotal (kat. S) ; PNP (porez na potrošnju): cac:AllowanceCharge (HR:PP)
		$f2_vat_rate = array(); // 'stopa' => array('osn','tax')
		$f2_pnp_rate = array(); // 'stopa' => array('osn','tax')
		$f2_data = get_data('select ubl_xml from eracun '.$f2_where, 1);
		if(isset($f2_data['ubl_xml'])){
			for($fi = 0; $fi < sizeof($f2_data['ubl_xml']); $fi++){
				$f2_xml = $f2_data['ubl_xml'][$fi];
				if($f2_xml == ''){
					continue;
				}
				// PDV: samo standardne (S) kategorije; exempt/out-of-scope (E/O/Z) preskačemo
				if(preg_match_all('/<cac:TaxSubtotal>(.*?)<\/cac:TaxSubtotal>/s', $f2_xml, $mv)){
					foreach($mv[1] as $blk){
						if(preg_match('/<cbc:ID>\s*([A-Z])\s*<\/cbc:ID>/', $blk, $cid) && $cid[1] != 'S'){
							continue;
						}
						f2_dodaj_stopu($f2_vat_rate, $blk);
					}
				}
				// PNP (porez na potrošnju): cac:AllowanceCharge s razlogom HR:PP
				// Amount=iznos PNP-a, BaseAmount=osnovica, MultiplierFactorNumeric=stopa
				if(preg_match_all('/<cac:AllowanceCharge>(.*?)<\/cac:AllowanceCharge>/s', $f2_xml, $mp)){
					foreach($mp[1] as $blk){
						if(!preg_match('/<cbc:ChargeIndicator>\s*true\s*<\/cbc:ChargeIndicator>/i', $blk)){
							continue;
						}
						if(stripos($blk, 'HR:PP') === false && stripos($blk, 'potro') === false){
							continue;
						}
						$pamt = 0; $pbase = 0; $prate = '0.00';
						if(preg_match('/<cbc:Amount[^>]*>([\-\d.]+)<\/cbc:Amount>/', $blk, $mm)){ $pamt = floatval($mm[1]); }
						if(preg_match('/<cbc:BaseAmount[^>]*>([\-\d.]+)<\/cbc:BaseAmount>/', $blk, $mm)){ $pbase = floatval($mm[1]); }
						if(preg_match('/<cbc:MultiplierFactorNumeric>([\-\d.]+)<\/cbc:MultiplierFactorNumeric>/', $blk, $mm)){ $prate = sprintf('%01.2f', floatval($mm[1])); }
						if(!isset($f2_pnp_rate[$prate])){ $f2_pnp_rate[$prate] = array('osn' => 0, 'tax' => 0); }
						$f2_pnp_rate[$prate]['osn'] += $pbase;
						$f2_pnp_rate[$prate]['tax'] += $pamt;
					}
				}
			}
		}
		ksort($f2_vat_rate);
		ksort($f2_pnp_rate);
		
		$f2_pdv_total = 0;
		foreach($f2_vat_rate as $v){ $f2_pdv_total += $v['tax']; }
		$f2_pnp_total = 0;
		foreach($f2_pnp_rate as $v){ $f2_pnp_total += $v['tax']; }
		
		// ciljevi za raspodjelu (točno po stopi)
		$vat_targets = array();
		foreach($f2_vat_rate as $rk => $v){ $vat_targets[$rk] = $v['tax']; }
		$pnp_targets = array();
		foreach($f2_pnp_rate as $rk => $v){ $pnp_targets[$rk] = $v['tax']; }
		
		$f2_grupe = array();
		$f2_artikli = array();
		
		if($f2_stavke_exists != ''){
			
			// --- osnovica po grupi artikala (tax_groups.name) ---
			$g = get_data('select coalesce(tg.name, "(bez grupe)") as gname, es.vat_percentage as vat, '.
				' coalesce(tg.tax_2, 0) as pnprate, sum(es.line_extension_amount) as osn '.
				' from eracun_stavke es '.
				' join eracun e on es.eracun_id = e.id '.
				' left join products p on es.product_id = p.id '.
				' left join tax_groups tg on p.tax_group_id = tg.id '.
				$f2_join_where.
				' group by tg.name, es.vat_percentage, tg.tax_2 '.
				' order by gname ', 1);
			
			for($i = 0; isset($g['gname']) && $i < sizeof($g['gname']); $i++){
				$f2_grupe[] = array(
					'name' => $g['gname'][$i],
					'vat' => floatval($g['vat'][$i]),
					'pnprate' => floatval($g['pnprate'][$i]),
					'osn' => floatval($g['osn'][$i])
				);
			}
			
			// --- osnovica po pojedinačnom artiklu ---
			$a = get_data('select coalesce(p.name, concat("#", es.product_id)) as aname, es.vat_percentage as vat, '.
				' coalesce(tg.tax_2, 0) as pnprate, sum(es.quantity) as kol, sum(es.line_extension_amount) as osn '.
				' from eracun_stavke es '.
				' join eracun e on es.eracun_id = e.id '.
				' left join products p on es.product_id = p.id '.
				' left join tax_groups tg on p.tax_group_id = tg.id '.
				$f2_join_where.
				' group by es.product_id, es.vat_percentage, tg.tax_2 '.
				' order by osn desc ', 1);
			
			for($i = 0; isset($a['aname']) && $i < sizeof($a['aname']); $i++){
				$f2_artikli[] = array(
					'name' => $a['aname'][$i],
					'vat' => floatval($a['vat'][$i]),
					'pnprate' => floatval($a['pnprate'][$i]),
					'kol' => floatval($a['kol'][$i]),
					'osn' => floatval($a['osn'][$i])
				);
			}
			
		}else{
			
			// fallback bez tablice eracun_stavke: samo razrada po stopi iz UBL-a
			foreach($f2_vat_rate as $rk => $v){
				$f2_grupe[] = array('name' => 'PDV '.$rk.'%', 'vat' => floatval($rk), 'pnprate' => 0, 'osn' => $v['osn']);
			}
		}
		
		// raspodjela TOČNOG PDV-a i PNP-a na retke (zbroj redaka = službeni iznos)
		f2_alociraj($f2_grupe, 'vat', 'pdv', 'osn', $vat_targets);
		f2_alociraj_pnp($f2_grupe, $f2_pnp_total);
		f2_alociraj($f2_artikli, 'vat', 'pdv', 'osn', $vat_targets);
		f2_alociraj_pnp($f2_artikli, $f2_pnp_total);
		
		$line_wide = str_repeat('-', 138);
		$line_narrow = str_repeat('-', 112);
		$line_pay = str_repeat('-', 81);
		
		// apsolutno pozicioniranje (SetXY) - isključi automatski prijelom stranice,
		// prijelome kontroliramo ručno preko $fy
		$pdf->SetAutoPageBreak(false);
		
		// ---------------- novi list ----------------
		$pdf->AddPage();
		$tpl = $pdf->importPage(1);
		$pdf->useTemplate($tpl, 0, 0, 210, 297);
		
		$pdf->SetFont("ArialCE", "B", 11);
		$pdf->SetTextColor(0, 0, 0);
		$pdf->SetXY(10, 10);
		$pdf->MultiCell(210, 5, _convert($x), 0, "L");
		
		$pdf->SetXY(10, 35);
		$pdf->MultiCell(210, 5, _convert('Rekapitulacija prometa - E-RAČUNI (F2)'), 0, "L");
		$pdf->SetXY(10, 40);
		$pdf->MultiCell(210, 5, _convert('Period od '.$l_datum.' do '.$l_datum2), 0, "L");
		
		$pdf->SetFont("ArialCE", "B", 10);
		$pdf->SetXY(10, 52);
		$pdf->MultiCell(210, 5, _convert('E-RAČUNI (fiskalizirani)'), 0, "L");
		$pdf->SetXY(10, 56);
		$pdf->MultiCell(210, 5, _convert($line_pay), 0, "L");
		
		$pdf->SetFont("ArialCE", "", 9);
		$pdf->SetXY(10, 61);
		$pdf->MultiCell(70, 5, _convert('UKUPNO F2 '.$g_v.':'), 0, "L");
		$pdf->SetXY(67, 61);
		$pdf->MultiCell(30, 5, _convert(sprintf('%01.2f', $f2_suma)), 0, "R");
		
		$pdf->SetXY(10, 65);
		$pdf->MultiCell(210, 5, _convert($line_pay), 0, "L");
		
		$fy = 76;
		
		// ============ TABLICA PO GRUPAMA (kao F1) ============
		$pdf->SetFont("ArialCE", "B", 10);
		$pdf->SetXY(10, $fy);
		$pdf->MultiCell(210, 5, _convert('PROMETI I POREZNA RAZRADA:'), 0, "L");
		$fy += 6;
		
		$fy = f2_tablica_zaglavlje($pdf, $fy, $g_v, $l_pdv, $line_wide);
		
		$pdf->SetFont("ArialCE", "", 8);
		$osn_sum = 0;
		$pdv_sum = 0;
		$pnp_sum = 0;
		$uk_sum = 0;
		foreach($f2_grupe as $r){
			$rpnp = isset($r['pnp']) ? $r['pnp'] : 0;
			$uk = $r['osn'] + $r['pdv'] + $rpnp;
			$pdf->SetXY(10, $fy);
			$pdf->MultiCell(35, 4, _convert($r['name']), 0, "L");
			$pdf->SetXY(46, $fy);
			$pdf->MultiCell(19, 4, _convert(sprintf('%01.2f', $r['osn'])), 0, "R");
			if($l_pdv != 'false'){
				$pdf->SetXY(70, $fy);
				$pdf->MultiCell(14, 4, _convert(sprintf('%01.2f', $r['vat'])), 0, "L");
				$pdf->SetXY(79, $fy);
				$pdf->MultiCell(19, 4, _convert(sprintf('%01.2f', $r['pdv'])), 0, "R");
			}
			if($rpnp != 0){
				$pdf->SetXY(105, $fy);
				$pdf->MultiCell(14, 4, _convert(sprintf('%01.2f', $r['pnprate'])), 0, "L");
			}
			$pdf->SetXY(114, $fy);
			$pdf->MultiCell(19, 4, _convert(sprintf('%01.2f', $rpnp)), 0, "R");
			$pdf->SetXY(133, $fy);
			$pdf->MultiCell(19, 4, _convert(sprintf('%01.2f', $uk)), 0, "R");
			$osn_sum += $r['osn'];
			$pdv_sum += $r['pdv'];
			$pnp_sum += $rpnp;
			$uk_sum += $uk;
			$fy += 4;
		}
		
		$pdf->SetFont("ArialCE", "B", 8);
		$pdf->SetXY(10, $fy);
		$pdf->MultiCell(35, 4, _convert('Ukupno '.$g_v), 0, "L");
		$pdf->SetXY(46, $fy);
		$pdf->MultiCell(19, 4, _convert(sprintf('%01.2f', $osn_sum)), 0, "R");
		if($l_pdv != 'false'){
			$pdf->SetXY(79, $fy);
			$pdf->MultiCell(19, 4, _convert(sprintf('%01.2f', $pdv_sum)), 0, "R");
		}
		$pdf->SetXY(114, $fy);
		$pdf->MultiCell(19, 4, _convert(sprintf('%01.2f', $pnp_sum)), 0, "R");
		$pdf->SetXY(133, $fy);
		$pdf->MultiCell(19, 4, _convert(sprintf('%01.2f', $uk_sum)), 0, "R");
		$fy += 4;
		
		$pdf->SetFont("ArialCE", "", 9);
		$pdf->SetXY(10, $fy);
		$pdf->MultiCell(210, 4, _convert($line_wide), 0, "L");
		$fy += 6;
		
		$pdf->SetXY(10, $fy);
		$pdf->MultiCell(120, 4, _convert('SVEUKUPNO: '.sprintf('%01.2f', $uk_sum).' '.$g_v), 0, "L");
		$fy += 4;
		$pdf->SetXY(10, $fy);
		$pdf->MultiCell(210, 4, _convert($line_wide), 0, "L");
		$fy += 6;
		
		// ============ RAZRADA PO STOPAMA (kao F1) ============
		foreach($f2_vat_rate as $vk => $v){
			$pdf->SetFont("ArialCE", "", 8);
			$pdf->SetXY(10, $fy);
			$pdf->MultiCell(19, 4, _convert('PDV '.$vk.'%:'), 0, "L");
			$pdf->SetXY(30, $fy);
			$pdf->MultiCell(35, 4, _convert(sprintf('%01.2f', $v['tax']).' '.$g_v), 0, "L");
			$fy += 4;
			
			$pdf->SetXY(10, $fy);
			$pdf->MultiCell(35, 4, _convert('Osnovica'), 0, "L");
			$pdf->SetXY(50, $fy);
			$pdf->MultiCell(35, 4, _convert('PDV'), 0, "L");
			$fy += 4;
			
			$pdf->SetXY(10, $fy);
			$pdf->MultiCell(35, 4, _convert(sprintf('%01.2f', $v['osn'])), 0, "L");
			$pdf->SetXY(50, $fy);
			$pdf->MultiCell(35, 4, _convert(sprintf('%01.2f', $v['tax'])), 0, "L");
			$fy += 4;
			
			$pdf->SetFont("ArialCE", "", 7);
			$pdf->SetXY(10, $fy);
			$pdf->MultiCell(210, 4, _convert($line_narrow), 0, "L");
			$fy += 4;
		}
		
		// PNP (porez na potrošnju) po stopama - iz UBL-a (hrextac:HRTaxSubtotal)
		foreach($f2_pnp_rate as $pk => $v){
			$pdf->SetFont("ArialCE", "", 8);
			$pdf->SetXY(10, $fy);
			$pdf->MultiCell(30, 4, _convert('PNP '.$pk.'%:'), 0, "L");
			$pdf->SetXY(40, $fy);
			$pdf->MultiCell(45, 4, _convert('osn. '.sprintf('%01.2f', $v['osn']).' -> '.sprintf('%01.2f', $v['tax']).' '.$g_v), 0, "L");
			$fy += 4;
		}
		
		$pdf->SetFont("ArialCE", "", 8);
		$pdf->SetXY(10, $fy);
		$pdf->MultiCell(19, 4, _convert('PNP:'), 0, "L");
		$pdf->SetXY(30, $fy);
		$pdf->MultiCell(35, 4, _convert(sprintf('%01.2f', $f2_pnp_total).' '.$g_v), 0, "L");
		$fy += 8;
		
		// ============ TABLICA PO ARTIKLIMA ============
		if(sizeof($f2_artikli) > 0){
			
			if($fy > 250){
				$pdf->AddPage();
				$tpl = $pdf->importPage(1);
				$pdf->useTemplate($tpl, 0, 0, 210, 297);
				$fy = 20;
			}
			
			$pdf->SetFont("ArialCE", "B", 10);
			$pdf->SetXY(10, $fy);
			$pdf->MultiCell(210, 5, _convert('ARTIKLI (E-RAČUNI):'), 0, "L");
			$fy += 6;
			
			$fy = f2_artikli_zaglavlje($pdf, $fy, $g_v, $l_pdv, $line_wide);
			
			$pdf->SetFont("ArialCE", "", 8);
			$a_osn = 0;
			$a_pdv = 0;
			$a_pnp = 0;
			$a_uk = 0;
			$a_kol = 0;
			foreach($f2_artikli as $r){
				if($fy > 285){
					$pdf->AddPage();
					$tpl = $pdf->importPage(1);
					$pdf->useTemplate($tpl, 0, 0, 210, 297);
					$fy = 20;
					$fy = f2_artikli_zaglavlje($pdf, $fy, $g_v, $l_pdv, $line_wide);
					$pdf->SetFont("ArialCE", "", 8);
				}
				$rpnp = isset($r['pnp']) ? $r['pnp'] : 0;
				$uk = $r['osn'] + $r['pdv'] + $rpnp;
				$pdf->SetXY(10, $fy);
				$pdf->MultiCell(58, 4, _convert($r['name']), 0, "L");
				$pdf->SetXY(68, $fy);
				$pdf->MultiCell(13, 4, _convert(sprintf('%01.2f', $r['kol'])), 0, "R");
				$pdf->SetXY(83, $fy);
				$pdf->MultiCell(17, 4, _convert(sprintf('%01.2f', $r['osn'])), 0, "R");
				if($l_pdv != 'false'){
					$vr = rtrim(rtrim(sprintf('%01.2f', $r['vat']), '0'), '.');
					$pdf->SetXY(101, $fy);
					$pdf->MultiCell(10, 4, _convert($vr), 0, "R");
					$pdf->SetXY(111, $fy);
					$pdf->MultiCell(16, 4, _convert(sprintf('%01.2f', $r['pdv'])), 0, "R");
				}
				$pdf->SetXY(128, $fy);
				$pdf->MultiCell(16, 4, _convert(sprintf('%01.2f', $rpnp)), 0, "R");
				$pdf->SetXY(146, $fy);
				$pdf->MultiCell(22, 4, _convert(sprintf('%01.2f', $uk)), 0, "R");
				$a_osn += $r['osn'];
				$a_pdv += $r['pdv'];
				$a_pnp += $rpnp;
				$a_uk += $uk;
				$a_kol += $r['kol'];
				$fy += 4;
			}
			
			$pdf->SetFont("ArialCE", "", 9);
			$pdf->SetXY(10, $fy);
			$pdf->MultiCell(210, 4, _convert($line_wide), 0, "L");
			$fy += 4;
			
			$pdf->SetFont("ArialCE", "B", 8);
			$pdf->SetXY(10, $fy);
			$pdf->MultiCell(58, 4, _convert('Ukupno '.$g_v), 0, "L");
			$pdf->SetXY(68, $fy);
			$pdf->MultiCell(13, 4, _convert(sprintf('%01.2f', $a_kol)), 0, "R");
			$pdf->SetXY(83, $fy);
			$pdf->MultiCell(17, 4, _convert(sprintf('%01.2f', $a_osn)), 0, "R");
			if($l_pdv != 'false'){
				$pdf->SetXY(111, $fy);
				$pdf->MultiCell(16, 4, _convert(sprintf('%01.2f', $a_pdv)), 0, "R");
			}
			$pdf->SetXY(128, $fy);
			$pdf->MultiCell(16, 4, _convert(sprintf('%01.2f', $a_pnp)), 0, "R");
			$pdf->SetXY(146, $fy);
			$pdf->MultiCell(22, 4, _convert(sprintf('%01.2f', $a_uk)), 0, "R");
			$fy += 4;
		}
	}
	
	
	function all_rekapitulacija_prometa($date_start, $date_end){
		
		$g_path = get_item('select value from settings where name = "putanja.abs" ');
		
		$g_v = get_valuta();
		
		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');
		
		$pdf =& new FPDI('P', 'mm', 'A4');
		
		$pdf->AddFont("ArialCE", "B", "6488f5d4657b269b9f5770508e80e299_arialbd.php");
		$pdf->AddFont("ArialCE", "", "332cc83ebee36613c19564e6e776c81b_arial.php");
		
		// set the sourcefile
		$pgNumber = $pdf->setSourceFile($g_path.'app/libs/fpdf/prazni-dokument.pdf');
		
		//$pdf->SetAutoPageBreak(true, 10);
		
		
		
		$l_tip_prog = get_item('select value from settings where name = "tipprog" ');
		
		
		$l_pdv = get_item('select value from settings where name = "duty.free" ');
		
		if($l_pdv == 'no'){
			$l_pdv = 'false';
		}
		
		if($l_pdv == 'false'){
			$l_pdv_koment = get_item('select value from settings where name = "duty.comment" ');
		}else{
			$l_pdv_koment = '';
		}
		
		$x = get_item('select value from settings where name = "general.name" ').', '.
			get_n_rows(get_item('select value from settings where name = "invoice.header" '), 4);
		
		
		$la_tmp = explode('-', $date_start);
		$la_tmp2 = explode('-', $date_end);
		
		$l_datum = $la_tmp[0].'.'.$la_tmp[1].'.'.$la_tmp[2].'.';
		$l_datum2 = $la_tmp2[0].'.'.$la_tmp2[1].'.'.$la_tmp2[2].'.';
		
		$l_datum_db = $la_tmp[2].'-'.$la_tmp[1].'-'.$la_tmp[0];
		$l_datum_db2 = $la_tmp2[2].'-'.$la_tmp2[1].'-'.$la_tmp2[0];
		$l_datum_from = $l_datum_db.' 00:00:00';
		$l_datum_to = $l_datum_db2.' 23:59:59';
		
		
		$la_uvjet = array('1', '2, 3, 4, 5, 7', '1, 2, 3, 4, 5, 7', '8', '6');
		$la_card_details = array('0', '1', '0', '0', '0');
		$la_skip_sum = array('0', '0', '1', '0', '0');
		
		
		
		
		
		if($l_tip_prog == 'm'){
			$la_type_desc = array('Novčanice/Kovanice', 'Kartice', 'Gotovina (Novčanice+Kartice)', 'Ostalo', 'Transakcijski račun (Virman)');
		}
		if($l_tip_prog == 'u'){
			$la_type_desc = array('Gotovina (novčanice)', 'Gotovina (kartice)', 'Gotovina (Novčanice+Kartice)', 'Ostalo', 'Transakcijski račun (Virman)');
		}
		
		
		$la_tax_desc = array(
			'GOTOVINSKI PROMET (Novčanice/Kovanice)',
			'GOTOVINSKI PROMET (Kartice)',
			'Ukupno Gotovina (Novčanice+Kartice)',
			'PROMET OSTALO',
			'PROMET TRANSAKCIJSKI RAČUN (VIRMAN)',
		);
		
		
		
		
		
		
		if($l_tip_prog == 'u'){
			
			
			
			$la_tax = array();
			$la_pn = array();
			
			//for($k = 0; $k < sizeof($la_uvjet); $k++){
			for($k = 0; $k < 1; $k++){
				$la_tax[$k] = array();
				$la_pn[$k] = 0;
			}
			
			$la_tax_ukup = array();
			$la_pn_ukup = 0;
			$la_cards = array();
				
			
			$l_tip_prog = get_item('select value from settings where name = "tipprog" ');
			
			$l_min_rbr = 99999999999;
			$l_max_rbr = 0;
			
			//for($k = 0; $k < sizeof($la_uvjet); $k++){
			for($k = 0; $k < 1; $k++){
				
				$y = get_data('select * from sales '.
					' where 1 = 1 '.
					// ' and payment_type_id in ('.$la_uvjet[$k].') '.
					' and created between "'.$l_datum_from.'" and "'.$l_datum_to.'" ', 1);
					
				$l_id = '0';
				for($i = 0; isset($y['id']) && $i < sizeof($y['id']); $i++){
					$l_id .= ', '.$y['id'][$i];
					
					if(empty($la_cards[$y['payment_type_id'][$i]])){
						$la_cards[$y['payment_type_id'][$i]] = 0;
					}
					$la_cards[$y['payment_type_id'][$i]] += $y['price'][$i];
				}
				
				$z = get_data('select tax_groups.id, tax_groups.name, sale_items.sale_id, sale_items.amount, sale_items.base_price, '.
					' sale_items.discount_percent, sale_items.price, sale_items.tax_1, sale_items.tax_2 '.
					' from sale_items, products, tax_groups '.
					' where sale_items.product_id = products.id '.
					' and products.tax_group_id = tax_groups.id '.
					' and sale_items.sale_id in ('.$l_id.')  '.
					' order by sale_items.sale_id, sale_items.id', 1);
				
				//p($value2 * $key2 / 100);
				
				$l_item = '';
				
				for($i = 0; isset($z['id']) && $i < sizeof($z['id']); $i++){
					if($l_pdv == 'false'){
						$z['tax_1'][$i] = 'no_pdv';
					}
					
					
					if(empty($la_tax[$k][$z['name'][$i]]['tax_1'][$z['tax_1'][$i]])){
						$la_tax[$k][$z['name'][$i]]['tax_1'][$z['tax_1'][$i]] = 0;
					}
					$la_tax[$k][$z['name'][$i]]['tax_1'][$z['tax_1'][$i]] += $z['price'][$i];
					
					if(empty($la_tax[$k][$z['name'][$i]]['tax_2'][$z['tax_1'][$i]][$z['tax_2'][$i]])){
						$la_tax[$k][$z['name'][$i]]['tax_2'][$z['tax_1'][$i]][$z['tax_2'][$i]] = 0;
					}
					$la_tax[$k][$z['name'][$i]]['tax_2'][$z['tax_1'][$i]][$z['tax_2'][$i]] += $z['price'][$i];
					
					
					
					if(empty($la_tax_ukup[$z['name'][$i]]['tax_1'][$z['tax_1'][$i]])){
						$la_tax_ukup[$z['name'][$i]]['tax_1'][$z['tax_1'][$i]] = 0;
					}
					if($la_skip_sum[$k] == '0'){
						$la_tax_ukup[$z['name'][$i]]['tax_1'][$z['tax_1'][$i]] += $z['price'][$i];
					}							
					
					if(empty($la_tax_ukup[$z['name'][$i]]['tax_2'][$z['tax_1'][$i]][$z['tax_2'][$i]])){
						$la_tax_ukup[$z['name'][$i]]['tax_2'][$z['tax_1'][$i]][$z['tax_2'][$i]] = 0;
					}
					if($la_skip_sum[$k] == '0'){
						$la_tax_ukup[$z['name'][$i]]['tax_2'][$z['tax_1'][$i]][$z['tax_2'][$i]] += $z['price'][$i];
					}
					
					
					
					if(empty($la_pn[$k])){
						$la_pn[$k] = 0;
					}
				}
			}
			
			
			
			for($i = 1; $i <= 1; $i++){
				// add a page
				$pdf->AddPage();
				
				// import page 1
				$tplIdx = $pdf->importPage($i);
				
				// use the imported page and place it at point 0,0 with a width of 210 mm
				$pdf->useTemplate($tplIdx, 0, 0, 210, 297);
				
				
				
				$pdf->SetFont("ArialCE", "B", 11);
				$pdf->SetTextColor(0, 0, 0);
				
				$pdf->SetXY(10, 10);
				$pdf->MultiCell(210, 5, _convert($x), 0, "L");
				
				$pdf->SetXY(10, 35);
				$pdf->MultiCell(210, 5, _convert('Rekapitulacija prometa'), 0, "L");
				
				$pdf->SetXY(10, 40);
				$pdf->MultiCell(210, 5, _convert('Period od '.$l_datum.' do '.$l_datum2), 0, "L");
				
				
				
				
				if($l_tip_prog == 'u'){
					
					
					
					
					
					function get_local_tax_prema_nacinu_placanja($l_datum_from, $l_datum_to){
						
						$la_uvjet = array('1', '2, 3, 4, 5, 7', '1, 2, 3, 4, 5, 7', '8', '6');
						$la_card_details = array('0', '1', '0', '0', '0');
						$la_skip_sum = array('0', '0', '1', '0', '0');
						
						$la_type_desc = array('Gotovina (novčanice)', 'Gotovina (kartice)', 'Gotovina (Novčanice+Kartice)', 'Ostalo', 'Transakcijski račun (Virman)');
						
						$la_tax_desc = array(
							'GOTOVINSKI PROMET (Novčanice/Kovanice)',
							'GOTOVINSKI PROMET (Kartice)',
							'Ukupno Gotovina (Novčanice+Kartice)',
							'PROMET OSTALO',
							'PROMET TRANSAKCIJSKI RAČUN (VIRMAN)',
						);
						
						$la_tax = array();
						$la_pn = array();
						
						for($k = 0; $k < sizeof($la_uvjet); $k++){
							$la_tax[$k] = array();
							$la_pn[$k] = 0;
						}
						
						$la_tax_ukup = array();
						$la_pn_ukup = 0;
							
						
						$l_tip_prog = get_item('select value from settings where name = "tipprog" ');
						
						$l_min_rbr = 99999999999;
						$l_max_rbr = 0;
						
						
						for($k = 0; $k < sizeof($la_uvjet); $k++){
							$y = get_data('select * from sales where payment_type_id in ('.$la_uvjet[$k].') '.
								' and created between "'.$l_datum_from.'" and "'.$l_datum_to.'" ', 1);
							$l_id = '0';
							for($i = 0; isset($y['id']) && $i < sizeof($y['id']); $i++){
								$l_id .= ', '.$y['id'][$i];
							}
							
							
							if($l_tip_prog == 'u'){
								
								$z = get_data('select tax_groups.id, tax_groups.name, sale_items.sale_id, sale_items.amount, sale_items.base_price, '.
									' sale_items.discount_percent, sale_items.price, sale_items.tax_1, sale_items.tax_2 '.
									' from sale_items, products, tax_groups '.
									' where sale_items.product_id = products.id '.
									' and products.tax_group_id = tax_groups.id '.
									' and sale_items.sale_id in ('.$l_id.')  '.
									' order by sale_items.sale_id, sale_items.id', 1);
								
								$l_item = '';
								
								for($i = 0; isset($z['id']) && $i < sizeof($z['id']); $i++){
									if($l_pdv == 'false'){
										$z['tax_1'][$i] = 'no_pdv';
									}
									
									
									if(empty($la_tax[$k][$z['name'][$i]]['tax_1'][$z['tax_1'][$i]])){
										$la_tax[$k][$z['name'][$i]]['tax_1'][$z['tax_1'][$i]] = 0;
									}
									$la_tax[$k][$z['name'][$i]]['tax_1'][$z['tax_1'][$i]] += $z['price'][$i];
									
									if(empty($la_tax[$k][$z['name'][$i]]['tax_2'][$z['tax_1'][$i]][$z['tax_2'][$i]])){
										$la_tax[$k][$z['name'][$i]]['tax_2'][$z['tax_1'][$i]][$z['tax_2'][$i]] = 0;
									}
									$la_tax[$k][$z['name'][$i]]['tax_2'][$z['tax_1'][$i]][$z['tax_2'][$i]] += $z['price'][$i];
									
									
									
									if(empty($la_tax_ukup[$z['name'][$i]]['tax_1'][$z['tax_1'][$i]])){
										$la_tax_ukup[$z['name'][$i]]['tax_1'][$z['tax_1'][$i]] = 0;
									}
									if($la_skip_sum[$k] == '0'){
										$la_tax_ukup[$z['name'][$i]]['tax_1'][$z['tax_1'][$i]] += $z['price'][$i];
									}							
									
									if(empty($la_tax_ukup[$z['name'][$i]]['tax_2'][$z['tax_1'][$i]][$z['tax_2'][$i]])){
										$la_tax_ukup[$z['name'][$i]]['tax_2'][$z['tax_1'][$i]][$z['tax_2'][$i]] = 0;
									}
									if($la_skip_sum[$k] == '0'){
										$la_tax_ukup[$z['name'][$i]]['tax_2'][$z['tax_1'][$i]][$z['tax_2'][$i]] += $z['price'][$i];
									}
									
									
									
									if(empty($la_pn[$k])){
										$la_pn[$k] = 0;
									}
								}
								
								
							}
						}
						
						return $la_tax;
					}
					
					
					
					
					$la_tax_local = get_local_tax_prema_nacinu_placanja($l_datum_from, $l_datum_to);
					
					
					
					$pdf->SetFont("ArialCE", "B", 10);
					$pdf->SetXY(10, 52);
					$pdf->MultiCell(210, 5, _convert('PROMETI PO NAČINIMA PLAĆANJA:'), 0, "L");
					$pdf->SetXY(10, 56);
					$pdf->MultiCell(210, 5, _convert('-------------------------------------------------------------------------'), 0, "L");
					
					$pdf->SetFont("ArialCE", "", 9);
					
					
					
					$la_ukup_sve = array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);
					$l_ukup_sve_sve = 0;
					
					//p($la_tax_local);
					//p('-------------------');
					for($k = 0; $k < sizeof($la_uvjet); $k++){
						
						//p($la_tax_local[$k]);
						
						
						foreach($la_tax_local[$k] as $key => $value){
							foreach($value['tax_1'] as $key2 => $value2){
								$la_ukup_sve[$k] += $value2;
							}
						}
					}
					for($k = 0; $k < sizeof($la_uvjet); $k++){
						if($k != 2){
							$l_ukup_sve_sve += $la_ukup_sve[$k]; 
						}
					}
					
					
					
					$l_cnt = 0;
					
					/*
					$pdf->SetXY(10, 60 + 4 * $l_cnt);
					$pdf->MultiCell(50, 5, _convert($la_type_desc[2]), 0, "L");
					
					$pdf->SetXY(67, 60 + 4 * $l_cnt);
					$pdf->MultiCell(30, 5, _convert(sprintf('%01.2f', $la_ukup_sve[2]).' Kn'), 0, "R");
					
					$l_cnt++;
					*/
					
					for($k = 0; $k < sizeof($la_type_desc); $k++){
						if($k == 2){
							continue;
						}
						
						$pdf->SetXY(10, 60 + 4 * $l_cnt);
						$pdf->MultiCell(50, 5, _convert($la_type_desc[$k]), 0, "L");
						
						$pdf->SetXY(67, 60 + 4 * $l_cnt);
						$pdf->MultiCell(30, 5, _convert(sprintf('%01.2f', $la_ukup_sve[$k]).' '.$g_v), 0, "R");
						
						$l_cnt++;
						
						if($k == 1){
							$pdf->SetFont("ArialCE", "", 8);
							$x_card = get_data('select id, name from payment_types where type = 1 and enabled = 1 order by id ', 1);
							for($kk = 0; $kk < sizeof($x_card['id']); $kk++){
								$pdf->SetXY(12, 60 + 4 * $l_cnt);
								$pdf->MultiCell(140, 4, _convert($x_card['name'][$kk].': '.sprintf('%01.2f', $la_cards[$x_card['id'][$kk]]).' '.$g_v), 0, "L");
								$l_cnt++;
							}
							$pdf->SetFont("ArialCE", "", 9);
						}
						
					}
					
					$pdf->SetXY(10, 60 + 4 * $l_cnt);
					$pdf->MultiCell(210, 5, _convert('---------------------------------------------------------------------------------'), 0, "L");
					
					$pdf->SetXY(10, 63 + 4 * $l_cnt);
					$pdf->MultiCell(210, 5, _convert('SVEUKUPNO'), 0, "L");
					$pdf->SetXY(67, 63 + 4 * $l_cnt);
					$pdf->MultiCell(30, 5, _convert(sprintf('%01.2f', $l_ukup_sve_sve).' '.$g_v), 0, "R");
					
					$pdf->SetXY(10, 66 + 4 * $l_cnt);
					$pdf->MultiCell(210, 5, _convert('---------------------------------------------------------------------------------'), 0, "L");
					
					
					
					$pdf->SetFont("ArialCE", "B", 10);
					$l_cnt++;
					$l_cnt++;
					$l_cnt++;
					$l_cnt++;
					$l_cnt++;
					$pdf->SetXY(10, 66 + 4 * $l_cnt);
					$pdf->MultiCell(210, 5, _convert('PROMETI I POREZNA RAZRADA:'), 0, "L");
					
					$pdf->SetFont("ArialCE", "", 9);
					
					
					
					
					
					
					$l_cnt = 9 + sizeof($la_type_desc) + sizeof($x_card['id']);
				}
				
				
				
				if($l_tip_prog == 'm'){
					$l_cnt = 0;
				}
				
				
				
				
				
				
				$l_ukup_sve_sve = 0;
				$l_ukup_sve = 0;
				$l_cnt = $l_cnt - 2;
				
				$la_rekapitulacija = array();
				
				$l_pnp_ukup = 0;
				
				for($k = 0; $k < 1; $k++){
					
					if(50 + 4 * ($l_cnt + 3 + 1) > 210){
						$pdf->AddPage();
						$l_cnt = -8;
					}
					
					$pdf->SetFont("ArialCE", "", 9);
					
					/*
					$pdf->SetXY(10, 50 + 4 * $l_cnt);
					$pdf->MultiCell(210, 4, _convert('--------------------------------------------------------------------------------------------------------------------------------------------'), 0, "L");
					
					$pdf->SetXY(10, 50 + 4 * ($l_cnt + 1));
					$pdf->MultiCell(70, 4, _convert($la_tax_desc[$k]), 0, "L");
					
					*/
					
					$pdf->SetFont("ArialCE", "", 9);
					$pdf->SetXY(10, 50 + 4 * ($l_cnt + 2));
					$pdf->MultiCell(210, 4, _convert('--------------------------------------------------------------------------------------------------------------------------------------------'), 0, "L");
					
					$pdf->SetFont("ArialCE", "B", 8);
					$pdf->SetXY(10, 50 + 4 * ($l_cnt + 3));
					$pdf->MultiCell(35, 4, _convert('Opis'), 0, "L");
					
					$pdf->SetXY(50, 50 + 4 * ($l_cnt + 3));
					$pdf->MultiCell(19, 4, _convert('Osnovica'), 0, "L");
					
					if($l_pdv == 'false'){
					}else{
						$pdf->SetXY(70, 50 + 4 * ($l_cnt + 3));
						$pdf->MultiCell(14, 4, _convert('PDV'), 0, "L");
						
						$pdf->SetXY(85, 50 + 4 * ($l_cnt + 3));
						$pdf->MultiCell(19, 4, _convert('PDV '.$g_v), 0, "L");
					}
					
					$pdf->SetXY(105, 50 + 4 * ($l_cnt + 3));
					$pdf->MultiCell(14, 4, _convert('PNP'), 0, "L");
					
					$pdf->SetXY(120, 50 + 4 * ($l_cnt + 3));
					$pdf->MultiCell(19, 4, _convert('PNP '.$g_v), 0, "L");
						
					$pdf->SetXY(140, 50 + 4 * ($l_cnt + 3));
					$pdf->MultiCell(25, 4, _convert('Ukupno'), 0, "L");
					
					$l_cnt++;
					
					$pdf->SetFont("ArialCE", "", 9);
					$pdf->SetXY(10, 50 + 4 * ($l_cnt + 3));
					$pdf->MultiCell(210, 4, _convert('--------------------------------------------------------------------------------------------------------------------------------------------'), 0, "L");
					
					
					$pdf->SetFont("ArialCE", "", 8);
					
					$l_ukup_sve = 0;
					$la_ukup = array();
					
					$l_osnovica_sum = 0;
					$l_pdv_kn = 0;
					$l_pnp_kn = 0;
					
					foreach($la_tax[$k] as $key => $value){
						foreach($value['tax_1'] as $key2 => $value2){
							
							$l_ukup_porez = 0;
							
							$pdf->SetXY(10, 50 + 4 * ($l_cnt + 3 + 1));
							$pdf->MultiCell(35, 4, _convert($key), 0, "L");
							
							$l_ukup_porez += $key2;
							
							$l_key_3_move = 0;
							
							// zbog strukture podataka, samo jedan zapis ima (iako koristim foreach petlju, najbrze mi bilo iskoristit tu petlju)
							foreach($la_tax[$k][$key]['tax_2'][$key2] as $key3 => $value3){
								$l_key_3_move = $key3;
								
								$pdf->SetXY(105, 50 + 4 * ($l_cnt + 3 + 1));
								$pdf->MultiCell(14, 4, _convert($key3), 0, "L");
								$l_ukup_porez += $key3;
								
								
								if($k != 2){
									if(empty($la_rekapitulacija[$key2])){
										$la_rekapitulacija[$key2] = array();
										$la_rekapitulacija[$key2]['pdv'] = 0;
										$la_rekapitulacija[$key2]['pnp'] = array();
									}
								}
								if(empty($la_ukup[$key2])){
									$la_ukup[$key2] = array();
									$la_ukup[$key2]['pdv'] = 0;
									$la_ukup[$key2]['pnp'] = array();
								}
								
								$l_osnovica = $value3 / (1 + ($key2 + $key3) / 100);
								
								$la_ukup[$key2]['pdv'] += $l_osnovica;
								if(empty($la_ukup[$key2]['pnp'][$key3])){
									$la_ukup[$key2]['pnp'][$key3] = 0;
								}
								$la_ukup[$key2]['pnp'][$key3] += $l_osnovica;
								
								if($k != 2){
									$la_rekapitulacija[$key2]['pdv'] += $l_osnovica;
									if(empty($la_rekapitulacija[$key2]['pnp'][$key3])){
										$la_rekapitulacija[$key2]['pnp'][$key3] = 0;
									}
									$la_rekapitulacija[$key2]['pnp'][$key3] += $l_osnovica;
								}
								
							}
							
							$pdf->SetXY(46, 50 + 4 * ($l_cnt + 3 + 1));
							$pdf->MultiCell(19, 4, _convert(sprintf('%01.2f', $value2 / (1 + $l_ukup_porez / 100))), 0, "R");
							$l_osnovica_sum += $value2 / (1 + $l_ukup_porez / 100);
							
							
							if($key2 == 'no_pdv'){
							}else{
								$pdf->SetXY(70, 50 + 4 * ($l_cnt + 3 + 1));
								$pdf->MultiCell(14, 4, _convert($key2), 0, "L");
								
								$pdf->SetXY(79, 50 + 4 * ($l_cnt + 3 + 1));
								$pdf->MultiCell(19, 4, _convert(sprintf('%01.2f', $value2 / (1 + $l_ukup_porez / 100) * $key2 / 100)), 0, "R");
								$l_pdv_kn += $value2 / (1 + $l_ukup_porez / 100) * $key2 / 100;
							}
							
							
							$pdf->SetXY(114, 50 + 4 * ($l_cnt + 3 + 1));
							$pdf->MultiCell(19, 4, _convert(sprintf('%01.2f', $value2 / (1 + $l_ukup_porez / 100) * $l_key_3_move / 100)), 0, "R");
							$l_pnp_kn += $value2 / (1 + $l_ukup_porez / 100) * $l_key_3_move / 100;
								
							$pdf->SetXY(133, 50 + 4 * ($l_cnt + 3 + 1));
							$pdf->MultiCell(19, 4, _convert(sprintf('%01.2f', $value2)), 0, "R");
							
							$l_ukup_sve += $value2;
							
							$l_cnt++;
						}
						
					}
					
					$pdf->SetFont("ArialCE", "B", 8);
					
					$pdf->SetXY(10, 50 + 4 * ($l_cnt + 3 + 1));
					$pdf->MultiCell(35, 4, _convert('Ukupno '.$g_v), 0, "L");
					
					$pdf->SetXY(46, 50 + 4 * ($l_cnt + 3 + 1));
					$pdf->MultiCell(19, 4, _convert(sprintf('%01.2f', $l_osnovica_sum)), 0, "R");
					
					if($l_pdv == 'false'){
					}else{
						$pdf->SetXY(79, 50 + 4 * ($l_cnt + 3 + 1));
						$pdf->MultiCell(19, 4, _convert(sprintf('%01.2f', $l_pdv_kn)), 0, "R");
					}
					
					$pdf->SetXY(114, 50 + 4 * ($l_cnt + 3 + 1));
					$pdf->MultiCell(19, 4, _convert(sprintf('%01.2f', $l_pnp_kn)), 0, "R");
					
					$pdf->SetXY(133, 50 + 4 * ($l_cnt + 3 + 1));
					$pdf->MultiCell(19, 4, _convert(sprintf('%01.2f', $l_ukup_sve)), 0, "R");
					
					
					if($k != 2){
						$l_ukup_sve_sve += $l_ukup_sve; 
					}
					
					
					
					$l_pnp = 0;
					foreach($la_ukup as $key => $value){
						foreach($la_ukup[$key]['pnp'] as $key2 => $value2){
							$l_pnp += ($value2 * $key2 / 100);
							if($k != 2){
								$l_pnp_ukup += ($value2 * $key2 / 100);
							}
						}
					}
				}
				
				
				$l_cnt++;
				$l_cnt++;
				$l_cnt++;
				$l_cnt++;
				$l_cnt++;
				
				
				$l_sveukupno = 0;
				foreach($la_rekapitulacija as $key => $value){
					$l_sveukupno += $value['pdv'];
				}
				
				$pdf->SetFont("ArialCE", "", 9);
				
				$pdf->SetXY(10, 50 + 4 * $l_cnt);
				$pdf->MultiCell(210, 4, _convert('--------------------------------------------------------------------------------------------------------------------------------------------'), 0, "L");
				
				$pdf->SetXY(10, 50 + 4 * ($l_cnt + 1));
				$pdf->MultiCell(70, 4, _convert('SVEUKUPNO: '.sprintf('%01.2f', $l_ukup_sve_sve).' '.$g_v), 0, "L");
				
				
				$pdf->SetFont("ArialCE", "", 9);
				$pdf->SetXY(10, 50 + 4 * ($l_cnt + 2));
				$pdf->MultiCell(210, 4, _convert('--------------------------------------------------------------------------------------------------------------------------------------------'), 0, "L");
				
				foreach($la_rekapitulacija as $key => $value){
					
					$pdf->SetFont("ArialCE", "", 8);
					
					if($key == 'no_pdv'){
					}else{
						$pdf->SetXY(10, 50 + 4 * ($l_cnt + 3 + 1));
						$pdf->MultiCell(19, 4, _convert('PDV '.$key.'%:'), 0, "L");
						
						$pdf->SetXY(30, 50 + 4 * ($l_cnt + 3 + 1));
						$pdf->MultiCell(35, 4, _convert((sprintf('%01.2f', $value['pdv'] * $key / 100)).' '.$g_v), 0, "L");
					}
					
					$l_cnt++;
					
					$pdf->SetXY(10, 50 + 4 * ($l_cnt + 3 + 1));
					$pdf->MultiCell(35, 4, _convert('Osnovica'), 0, "L");
					
					if($key == 'no_pdv'){
					}else{
						$pdf->SetXY(50, 50 + 4 * ($l_cnt + 3 + 1));
						$pdf->MultiCell(35, 4, _convert('PDV'), 0, "L");
					}
					
					$pdf->SetXY(90, 50 + 4 * ($l_cnt + 3 + 1));
					$pdf->MultiCell(35, 4, _convert('PNP'), 0, "L");
					
					$l_cnt++;
					
					foreach($la_rekapitulacija[$key]['pnp'] as $key2 => $value2){
						$pdf->SetXY(10, 50 + 4 * ($l_cnt + 3 + 1));
						$pdf->MultiCell(35, 4, _convert(sprintf('%01.2f', $value2)), 0, "L");
						
						if($key == 'no_pdv'){
						}else{
							$pdf->SetXY(50, 50 + 4 * ($l_cnt + 3 + 1));
							$pdf->MultiCell(35, 4, _convert(sprintf('%01.2f', $value2 * $key / 100)), 0, "L");
						}
						
						$pdf->SetXY(90, 50 + 4 * ($l_cnt + 3 + 1));
						$pdf->MultiCell(35, 4, _convert(sprintf('%01.2f', $value2 * $key2 / 100)), 0, "L");
						
						$l_cnt++;
					}
					
					$pdf->SetFont("ArialCE", "", 7);
					$pdf->SetXY(10, 50 + 4 * ($l_cnt + 3 + 1));
					$pdf->MultiCell(210, 4, _convert('----------------------------------------------------------------------------------------------------------------'), 0, "L");
					
					$l_cnt++;
				}
				
				$pdf->SetFont("ArialCE", "", 8);
				
				$pdf->SetXY(10, 50 + 4 * ($l_cnt + 3 + 1));
				$pdf->MultiCell(19, 4, _convert('PNP:'), 0, "L");
				
				$pdf->SetXY(30, 50 + 4 * ($l_cnt + 3 + 1));
				$pdf->MultiCell(35, 4, _convert(sprintf('%01.2f', $l_pnp_ukup).' '.$g_v), 0, "L");
				
				$l_cnt++;
				
				$l_cnt++;
				$l_cnt++;
				$l_cnt++;
				$l_cnt++;
				$l_cnt++;
				$l_cnt++;
				
				$l_polog = get_item('select value from settings where name = "general.polog_blagajne" ');
				
				$pdf->SetXY(10, 50 + 4 * (++$l_cnt));
				$pdf->MultiCell(210, 4, _convert('Polog novcanica/kovanica: '.sprintf('%01.2f', $l_polog).' '.$g_v), 0, "L");
				
				$pdf->SetXY(10, 50 + 4 * (++$l_cnt));
				$pdf->MultiCell(210, 4, _convert($l_pdv_koment), 0, "L");
				
				
				// F2 - E-RAČUNI (fiskalizirani) - grupe + artikli (poseban dio, ne dira F1)
				f2_eracun_rekapitulacija($pdf, $x, $g_v, $l_pdv, $l_datum, $l_datum2, $l_datum_db, $l_datum_db2);
				
			}
			
		}
		
		
		
		
		
		if($l_tip_prog == 'm'){
			$la_tax = array();
			$la_pn = array();
			
			for($k = 0; $k < sizeof($la_uvjet); $k++){
				$la_tax[$k] = array();
				$la_pn[$k] = 0;
			}
			
			$la_tax_ukup = array();
			$la_pn_ukup = 0;
			$la_cards = array();
				
			
			$l_min_rbr = 99999999999;
			$l_max_rbr = 0;
			
			$l_num_format = '';
			
			for($k = 0; $k < sizeof($la_uvjet); $k++){
				$y = get_data('select * from sales where payment_type_id in ('.$la_uvjet[$k].') and created between "'.$l_datum_from.'" and "'.$l_datum_to.'" ', 1);
				//print 'select * from sales where payment_type_id in ('.$la_uvjet[$k].') and created between "'.$l_datum_from.'" and "'.$l_datum_to.'" '."\n";
				$l_id = '0';
				for($i = 0; isset($y['id']) && $i < sizeof($y['id']); $i++){
					$l_id .= ', '.$y['id'][$i];
					
					if($l_num_format == ''){
						$l_num_format = $y['number'][0];
					}
					
					$la_number = explode('-', $y['number'][$i]);
					if($la_number[0] < $l_min_rbr){
						$l_min_rbr = $la_number[0];
					}
					if($la_number[0] > $l_max_rbr){
						$l_max_rbr = $la_number[0];
					}
					
					if($la_card_details[$k] == '1'){
						if(empty($la_cards[$y['payment_type_id'][$i]])){
							$la_cards[$y['payment_type_id'][$i]] = 0;
						}
						$la_cards[$y['payment_type_id'][$i]] += $y['price'][$i];
					}
				}
				
				
				$z = get_data('select * from retail_items where sale_id in ('.$l_id.') order by sale_id, id ', 1);
				$l_item = '';
				
				for($i = 0; isset($z['id']) && $i < sizeof($z['id']); $i++){
					if($l_pdv == 'false'){
						$z['tax_1'][$i] = 'no_pdv';
					}
					
					if(empty($la_tax[$k][$z['tax_1'][$i]])){
						$la_tax[$k][$z['tax_1'][$i]] = 0;
					}
					$la_tax[$k][$z['tax_1'][$i]] += $z['price'][$i] - $z['feedback_compensation'][$i];
					
					if(empty($la_tax_ukup[$z['tax_1'][$i]])){
						$la_tax_ukup[$z['tax_1'][$i]] = 0;
					}
					if($la_skip_sum[$k] == '0'){
						$la_tax_ukup[$z['tax_1'][$i]] += $z['price'][$i] - $z['feedback_compensation'][$i];
					}
					
					if(empty($la_pn[$k])){
						$la_pn[$k] = 0;
					}
					$la_pn[$k] += $z['feedback_compensation'][$i];
					
					if($la_skip_sum[$k] == '0'){
						$la_pn_ukup += $z['feedback_compensation'][$i];
					}
				}
			}
			
			//exit;
			
			
			
			if($l_num_format == ''){
				if($l_min_rbr == 99999999999){
					$l_min_rbr = '-';
				}
				if($l_max_rbr == 0){
					$l_max_rbr = '-';
				}
				
			}else{
				
				$la_number = explode('-', $l_num_format);
				for($j = 1; $j < sizeof($la_number); $j++){
					$l_min_rbr .= '-'.$la_number[$j];
				}
				for($j = 1; $j < sizeof($la_number); $j++){
					$l_max_rbr .= '-'.$la_number[$j];
				}
			}
			
			
			//for($i = 1; $i <= $pgNumber; $i++){
			for($i = 1; $i <= 1; $i++){
				// add a page
				$pdf->AddPage();
				
				// import page 1
				$tplIdx = $pdf->importPage($i);
				
				// use the imported page and place it at point 0,0 with a width of 210 mm
				$pdf->useTemplate($tplIdx, 0, 0, 210, 297);
				
				
				
				$pdf->SetFont("ArialCE", "B", 11);
				$pdf->SetTextColor(0, 0, 0);
				
				$pdf->SetXY(10, 10);
				$pdf->MultiCell(210, 5, _convert($x), 0, "L");
				
				$pdf->SetXY(10, 30);
				$pdf->MultiCell(210, 5, _convert('Rekapitulacija prometa'), 0, "L");
				
				$pdf->SetXY(10, 35);
				$pdf->MultiCell(210, 5, _convert('Period od '.$l_datum.' do '.$l_datum2), 0, "L");
				
				$pdf->SetXY(10, 40);
				$pdf->MultiCell(210, 5, _convert('Brojevi računa od '.$l_min_rbr.' do '.$l_max_rbr), 0, "L");
				
				
				$l_ukup_sve = 0;
				$l_cnt = 0;
				
				
				for($k = 0; $k < sizeof($la_uvjet); $k++){
					
					if(50 + 4 * ($l_cnt + 1) > 210){
						$pdf->AddPage();
						$l_cnt = -8;
					}
					
					
					$l_ukup = 0;
					foreach($la_tax[$k] as $key => $value){
						$l_ukup += $value;
					}
					
					$pdf->SetFont("ArialCE", "", 9);
					
					$pdf->SetXY(10, 50 + 4 * $l_cnt);
					$pdf->MultiCell(210, 4, _convert('------------------------------------------------------------------------------------------------------'), 0, "L");
					
					$pdf->SetXY(10, 50 + 4 * ($l_cnt + 1));
					$pdf->MultiCell(70, 4, _convert($la_tax_desc[$k]), 0, "L");
					
					$pdf->SetXY(90, 50 + 4 * ($l_cnt + 1));
					$pdf->MultiCell(30, 4, _convert(sprintf('%01.2f', $l_ukup).' '.$g_v), 0, "R");
					
					
					if($la_pn[$k] > 0){
						$l_cnt++;
						$pdf->SetXY(10, 50 + 4 * ($l_cnt + 1));
						$pdf->MultiCell(70, 4, _convert('Povratna naknada'), 0, "L");
						
						$pdf->SetXY(90, 50 + 4 * ($l_cnt + 1));
						$pdf->MultiCell(30, 4, _convert(sprintf('%01.2f', $la_pn[$k]).' '.$g_v), 0, "R");
						
						$l_cnt++;
						$pdf->SetXY(10, 50 + 4 * ($l_cnt + 1));
						$pdf->MultiCell(70, 4, _convert('UKUPNO'), 0, "L");
						
						$pdf->SetXY(90, 50 + 4 * ($l_cnt + 1));
						$pdf->MultiCell(30, 4, _convert(sprintf('%01.2f', $l_ukup + $la_pn[$k]).' '.$g_v), 0, "R");
					}
					
			
					$l_ukup = 0;
					$l_cnt_2 = 0;
					
					foreach($la_tax[$k] as $key => $value){
						$l_cnt_2++;
						$l_osnovica = round($value * 100 / (100 + intval($key)), 2);
						$l_iznos = round(($key * $value / (100 + intval($key))), 2);
						$l_ukup += $value;
						if($la_skip_sum[$k] == '0'){
							$l_ukup_sve += $value;
						}
						
						$pdf->SetFont("ArialCE", "", 8);
						
						if($l_cnt_2 == 1){
							$pdf->SetFont("ArialCE", "", 9);
							
							$pdf->SetXY(10, 50 + 4 * ($l_cnt + 2));
							$pdf->MultiCell(210, 4, _convert('------------------------------------------------------------------------------------------------------'), 0, "L");
							
							$pdf->SetFont("ArialCE", "", 8);
							if($la_card_details[$k] == '1'){
								$la_tmp_uvjet = explode(',', $la_uvjet[$k]);
								for($z = 0; $z < sizeof($la_tmp_uvjet); $z++){
									if(empty($la_cards[trim($la_tmp_uvjet[$z])])){
										$la_cards[trim($la_tmp_uvjet[$z])] = 0;
									}
					
									$l_cnt++;
									$pdf->SetXY(10, 50 + 4 * ($l_cnt + 2));
									$pdf->MultiCell(80, 4, _convert(get_item('select full_name from payment_types where id = '.trim($la_tmp_uvjet[$z]))), 0, "L");
									$pdf->SetXY(100, 50 + 4 * ($l_cnt + 2));
									$pdf->MultiCell(50, 4, _convert($la_cards[trim($la_tmp_uvjet[$z])]), 0, "L");
								}
								$pdf->SetXY(10, 50 + 4 * (++$l_cnt + 2));
								$pdf->SetFont("ArialCE", "", 9);
								$pdf->MultiCell(210, 4, _convert('------------------------------------------------------------------------------------------------------'), 0, "L");
								$pdf->SetFont("ArialCE", "", 8);
								if($l_pdv == 'false'){
									if($la_pn[$k] == 0){
										$l_cnt--;
									}
								}
							}
							
							if($l_pdv == 'false'){
							}else{
								$pdf->SetXY(10, 49 + 4 * ($l_cnt + 3));
								$pdf->MultiCell(210, 4, _convert('Porezi '.$la_type_desc[$k].':'), 0, "L");
							}
						}
					
						
						if($l_pdv == 'false'){
							$l_cnt--;
							$l_cnt--;
						}else{
							$pdf->SetXY(10, 50 + 4 * ($l_cnt + 3 + 1));
							$pdf->MultiCell(25, 4, _convert('PDV '.round($key).'%'), 0, "L");
							
							$pdf->SetXY(40, 50 + 4 * ($l_cnt + 3 + 1));
							$pdf->MultiCell(25, 4, _convert('OSNOVICA'), 0, "L");
							
							$pdf->SetXY(70, 50 + 4 * ($l_cnt + 3 + 1));
							$pdf->MultiCell(25, 4, _convert('POREZ'), 0, "L");
							
							$pdf->SetXY(100, 50 + 4 * ($l_cnt + 3 + 1));
							$pdf->MultiCell(25, 4, _convert('UKUPNO'), 0, "L");
							
							$pdf->SetXY(10, 50 + 4 * ($l_cnt + 3 + 2));
							$pdf->MultiCell(25, 4, _convert(''), 0, "L");
							
							$pdf->SetXY(40, 50 + 4 * ($l_cnt + 3 + 2));
							$pdf->MultiCell(25, 4, _convert(sprintf('%01.2f', $l_osnovica)), 0, "L");
							
							$pdf->SetXY(70, 50 + 4 * ($l_cnt + 3 + 2));
							$pdf->MultiCell(25, 4, _convert(sprintf('%01.2f', $l_iznos)), 0, "L");
							
							$pdf->SetXY(100, 50 + 4 * ($l_cnt + 3 + 2));
							$pdf->MultiCell(25, 4, _convert(sprintf('%01.2f', $l_osnovica + $l_iznos)), 0, "L");
						}
						
						$l_cnt++;
						$l_cnt++;
					}
					
					$l_cnt++;
					
					if($l_pdv == 'false'){
						$l_cnt++;
					}
					
					if($la_card_details[$k] == '1'){
						$l_cnt_2++;
						$l_cnt = $l_cnt + 1 + $l_cnt_2;
					}else{
						$l_cnt_2++;
						$l_cnt = $l_cnt + 2 + $l_cnt_2;
					}
					
					$l_cnt++;
					if($l_pdv == 'false'){
						$l_cnt--;
					}
					
				}
				
				
				
				if(50 + 4 * ($l_cnt + 1) > 210){
					$pdf->AddPage();
					$l_cnt = -8;
				}
				
				
				if($l_pdv == 'false'){
					$l_cnt++;
					$l_cnt++;
				}
					
				$pdf->SetFont("ArialCE", "", 9);
				if($l_pdv == 'false'){
					$l_cnt--;
					$l_cnt--;
				}
				
				$pdf->SetXY(10, 50 + 4 * $l_cnt);
				$pdf->MultiCell(210, 4, _convert('=========================================================='), 0, "L");
					
				$pdf->SetFont("ArialCE", "B", 9);
				
				$l_cnt++;
				$pdf->SetXY(10, 50 + 4 * $l_cnt);
				$pdf->MultiCell(210, 4, _convert('SVEUKUPNO PROMET: '.sprintf('%01.2f', $l_ukup_sve + $la_pn_ukup).' '.$g_v), 0, "L");
				
				$l_cnt++;
				
				$pdf->SetFont("ArialCE", "", 9);
				
				$pdf->SetXY(10, 50 + 4 * $l_cnt);
				$pdf->MultiCell(210, 4, _convert('=========================================================='), 0, "L");
				
				$pdf->SetFont("ArialCE", "", 8);
				
				$l_cnt++;
				
				
				
				
				
				$pdf->SetFont("ArialCE", "", 8);
				
				$l_cnt++;
				$l_cnt++;
				$pdf->SetXY(10, 50 + 4 * $l_cnt);
				$pdf->MultiCell(210, 4, _convert('PROMET PO OPERATERIMA'), 0, "L");
				
				$l_cnt++;
				$pdf->SetXY(10, 50 + 4 * $l_cnt);
				$pdf->MultiCell(210, 4, _convert('-------------------------------------------------------------------'), 0, "L");
				
				
				
				$z_op = get_data('select users.id, users.full_name, sum(sales.price) as money '.
					' from users, sales '.
					' where users.id = sales.user_id '.
					' and sales.created between "'.$l_datum_from.'" and "'.$l_datum_to.'" '.
					' group by 1 '.
					' order by 1', 1);
					
					
				for($k = 0; isset($z_op['id']) && $k < sizeof($z_op['id']); $k++){
					$l_cnt++;
					$pdf->SetXY(10, 50 + 4 * $l_cnt);
					$pdf->MultiCell(45, 4, _convert($z_op['full_name'][$k]), 0, "L");
					$pdf->SetXY(55, 50 + 4 * $l_cnt);
					$pdf->MultiCell(20, 4, _convert($z_op['money'][$k].' '.$g_v), 0, "R");
					
					
					if(50 + 4 * ($l_cnt + 1) > 265){
						$pdf->AddPage();
						$l_cnt = -8;
					}
				}
				
				
				$l_cnt++;
				$pdf->SetXY(10, 50 + 4 * $l_cnt);
				$pdf->MultiCell(210, 4, _convert('-------------------------------------------------------------------'), 0, "L");
				

				if(50 + 4 * ($l_cnt + 1) > 265){
					$pdf->AddPage();
					$l_cnt = -8;
				}
					
				$l_cnt++;
				$l_cnt++;
				$l_cnt++;
				$pdf->SetXY(10, 50 + 4 * $l_cnt);
				$pdf->MultiCell(210, 4, _convert(''), 0, "L");
				
				
				
				
				if(50 + 4 * ($l_cnt + 1) > 240){
					$pdf->AddPage();
					$l_cnt = -8;
				}
				
				
				if($l_pdv == 'false'){
				}else{
					$l_cnt++;
					
					$pdf->SetXY(10, 50 + 4 * $l_cnt);
					$pdf->MultiCell(30, 4, _convert('POREZI UKUPNO'), 0, "L");
					
					$pdf->SetXY(40, 50 + 4 * $l_cnt);
					$pdf->MultiCell(25, 4, _convert('OSNOVICA'), 0, "L");
					
					$pdf->SetXY(70, 50 + 4 * $l_cnt);
					$pdf->MultiCell(25, 4, _convert('POREZ'), 0, "L");
					
					$pdf->SetXY(100, 50 + 4 * $l_cnt);
					$pdf->MultiCell(25, 4, _convert('UKUPNO'), 0, "L");
					
					
					$l_ispis = '';
					foreach($la_tax_ukup as $key => $value){
						
						$l_cnt++;
						$pdf->SetXY(10, 50 + 4 * $l_cnt);
						$pdf->MultiCell(25, 4, _convert(round($key).'%'), 0, "L");
						
						$l_osnovica = round(($value / (100 + intval($key)) * 100), 2);
						$l_iznos = $value - $l_osnovica;
						
						$pdf->SetXY(40, 50 + 4 * $l_cnt);
						$pdf->MultiCell(25, 4, _convert(sprintf('%01.2f', $l_osnovica)), 0, "L");
						
						$pdf->SetXY(70, 50 + 4 * $l_cnt);
						$pdf->MultiCell(25, 4, _convert(sprintf('%01.2f', $l_iznos)), 0, "L");
						
						$pdf->SetXY(100, 50 + 4 * $l_cnt);
						$pdf->MultiCell(25, 4, _convert(sprintf('%01.2f', $value)), 0, "L");
					}
				}
				
				
				$l_cnt++;
				$l_cnt++;
				
				if($la_pn_ukup > 0){
					if($l_pdv == 'false'){
						$l_cnt--;
						$l_cnt--;
					}
					$pdf->SetXY(10, 50 + 4 * $l_cnt);
					$pdf->MultiCell(100, 4, _convert('Sveukupno povratne naknade: '.sprintf('%01.2f', $la_pn_ukup).' '.$g_v), 0, "L");
				}
				
				
				$pdf->SetXY(10, 50 + 4 * (++$l_cnt));
				$pdf->MultiCell(210, 4, _convert($l_pdv_koment), 0, "L");
				
				
				// F2 - E-RAČUNI (fiskalizirani) - grupe + artikli (poseban dio, ne dira F1)
				f2_eracun_rekapitulacija($pdf, $x, $g_v, $l_pdv, $l_datum, $l_datum2, $l_datum_db, $l_datum_db2);
				
				
			}
		}
			//pe('111116');
			
		$l_name = 'rekapitulacija_prometa.pdf';
		$l_dest = $g_path.'app/webroot/res/files/'.$l_name;
		
		$pdf->Output($l_dest, 'F');
		
		return array('path' => $g_path.'app/webroot/res/files/', 'name' => $l_name);
	}
	
	
	
	// Renderira tablicu "po grupama" (Naziv/Kolicina/NC/PC + Skupni prikaz + SVEUKUPNO)
	// identično kao F1 dio izvještaja. Poziva se na već pripremljenoj stranici.
	// $z je rezultat get_data(... , 1) sa poljima name, kol, price_buy, NC, price_sell, PC, gname.
	function ppa_render_grupe(&$pdf, $z, $l_has_groups){
		$l_dash = '------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------';
		$l_cnt = 1;
		
		$pdf->SetFont("ArialCE", "B", 8);
		$pdf->SetXY(10, 30 + 4 * ($l_cnt + 3));
		$pdf->MultiCell(60, 4, _convert('Naziv'), 0, "L");
		$pdf->SetXY(105, 30 + 4 * ($l_cnt + 3));
		$pdf->MultiCell(18, 4, _convert('Kolicina'), 0, "L");
		$pdf->SetXY(121, 30 + 4 * ($l_cnt + 3));
		$pdf->MultiCell(23, 4, _convert('Nabavna cijena'), 0, "L");
		$pdf->SetXY(144, 30 + 4 * ($l_cnt + 3));
		$pdf->MultiCell(25, 4, _convert('Ukup NC'), 0, "L");
		$pdf->SetXY(160, 30 + 4 * ($l_cnt + 3));
		$pdf->MultiCell(25, 4, _convert('Prodajna cijena'), 0, "L");
		$pdf->SetXY(185, 30 + 4 * ($l_cnt + 3));
		$pdf->MultiCell(25, 4, _convert('Ukupno PC'), 0, "L");
		$l_cnt++;
		
		$la_gname = array('');
		if($l_has_groups && isset($z['gname'][0])){
			$l_cnt++;
			$l_cnt++;
			$pdf->SetFont("ArialCE", "B", 9);
			$pdf->SetXY(10, 30 + 4 * ($l_cnt + 3));
			$pdf->MultiCell(65, 4, _convert('Grupa: '.$z['gname'][0]), 0, "L");
			$l_cnt++;
			$pdf->SetFont("ArialCE", "", 9);
			$pdf->SetXY(10, 30 + 4 * ($l_cnt + 3));
			$pdf->MultiCell(210, 4, _convert($l_dash), 0, "L");
			$l_cnt++;
			$la_gname[0] = $z['gname'][0];
		}
		
		$pdf->SetFont("ArialCE", "", 9);
		$l_ukup_nc = 0;
		$l_ukup_pc = 0;
		$la_ukup_nc_g = array(0);
		$la_ukup_pc_g = array(0);
		$l_row_cnt = 0;
		for($j = 0; isset($z['name']) && $j < sizeof($z['name']); $j++){
			if(10 + 4 * ($l_cnt + 3 + 1) > 240){
				$pdf->AddPage();
				$l_cnt = -8;
			}
			if($l_has_groups && $j > 0 && $z['gname'][$j] != $z['gname'][$j - 1]){
				$pdf->SetFont("ArialCE", "", 9);
				$pdf->SetXY(10, 30 + 4 * ($l_cnt + 3));
				$pdf->MultiCell(210, 4, _convert($l_dash), 0, "L");
				$l_cnt++;
				$pdf->SetFont("ArialCE", "B", 9);
				$pdf->SetXY(10, 30 + 4 * ($l_cnt + 3));
				$pdf->MultiCell(65, 4, _convert('Ukupno grupa: '.$z['gname'][$j - 1]), 0, "L");
				$pdf->SetXY(146, 30 + 4 * ($l_cnt + 3));
				$pdf->MultiCell(40, 4, _convert(sprintf('%01.2f', $la_ukup_nc_g[$l_row_cnt])), 0, "L");
				$pdf->SetXY(185, 30 + 4 * ($l_cnt + 3));
				$pdf->MultiCell(40, 4, _convert(sprintf('%01.2f', $la_ukup_pc_g[$l_row_cnt])), 0, "L");
				$pdf->SetFont("ArialCE", "", 9);
				$l_cnt++;
				$l_cnt++;
				$l_cnt++;
				$l_cnt++;
				$pdf->SetFont("ArialCE", "B", 9);
				$pdf->SetXY(10, 30 + 4 * ($l_cnt + 3));
				$pdf->MultiCell(65, 4, _convert('Grupa: '.$z['gname'][$j]), 0, "L");
				$pdf->SetFont("ArialCE", "", 9);
				$l_cnt++;
				$pdf->SetFont("ArialCE", "", 9);
				$pdf->SetXY(10, 30 + 4 * ($l_cnt + 3));
				$pdf->MultiCell(210, 4, _convert($l_dash), 0, "L");
				$l_cnt++;
				$l_row_cnt++;
				$la_ukup_nc_g[$l_row_cnt] = 0;
				$la_ukup_pc_g[$l_row_cnt] = 0;
				$la_gname[$l_row_cnt] = $z['gname'][$j];
			}
			$pdf->SetXY(10, 30 + 4 * ($l_cnt + 3));
			$pdf->MultiCell(90, 4, _convert($z['name'][$j]), 0, "L");
			$pdf->SetXY(106, 30 + 4 * ($l_cnt + 3));
			$pdf->MultiCell(25, 4, _convert(sprintf('%01.4f', $z['kol'][$j])), 0, "L");
			$pdf->SetXY(127, 30 + 4 * ($l_cnt + 3));
			$pdf->MultiCell(23, 4, _convert(sprintf('%01.2f', $z['price_buy'][$j])), 0, "L");
			$pdf->SetXY(146, 30 + 4 * ($l_cnt + 3));
			$pdf->MultiCell(25, 4, _convert(sprintf('%01.2f', $z['NC'][$j])), 0, "L");
			$pdf->SetXY(165, 30 + 4 * ($l_cnt + 3));
			$pdf->MultiCell(25, 4, _convert(sprintf('%01.2f', $z['price_sell'][$j])), 0, "L");
			$pdf->SetXY(185, 30 + 4 * ($l_cnt + 3));
			$pdf->MultiCell(25, 4, _convert(sprintf('%01.2f', $z['PC'][$j])), 0, "L");
			$l_ukup_nc += $z['NC'][$j];
			$la_ukup_nc_g[$l_row_cnt] += $z['NC'][$j];
			$l_ukup_pc += $z['PC'][$j];
			$la_ukup_pc_g[$l_row_cnt] += $z['PC'][$j];
			$l_cnt++;
		}
		
		if($l_has_groups && isset($z['gname']) && sizeof($z['gname']) > 0){
			$pdf->SetFont("ArialCE", "", 9);
			$pdf->SetXY(10, 30 + 4 * ($l_cnt + 3));
			$pdf->MultiCell(210, 4, _convert($l_dash), 0, "L");
			$l_cnt++;
			$pdf->SetFont("ArialCE", "B", 9);
			$pdf->SetXY(10, 30 + 4 * ($l_cnt + 3));
			$pdf->MultiCell(65, 4, _convert('Ukupno grupa: '.$z['gname'][sizeof($z['gname']) - 1]), 0, "L");
			$pdf->SetXY(146, 30 + 4 * ($l_cnt + 3));
			$pdf->MultiCell(40, 4, _convert(sprintf('%01.2f', $la_ukup_nc_g[$l_row_cnt])), 0, "L");
			$pdf->SetXY(185, 30 + 4 * ($l_cnt + 3));
			$pdf->MultiCell(40, 4, _convert(sprintf('%01.2f', $la_ukup_pc_g[$l_row_cnt])), 0, "L");
			$pdf->SetFont("ArialCE", "", 9);
			$l_cnt++;
			$l_cnt++;
			$l_cnt++;
		}
		
		if($l_has_groups && isset($la_gname) && sizeof($la_gname) > 0){
			$l_cnt++;
			$l_cnt++;
			$l_cnt++;
			$l_cnt++;
			$pdf->SetFont("ArialCE", "B", 9);
			$pdf->SetXY(10, 30 + 4 * ($l_cnt + 3));
			$pdf->MultiCell(210, 4, _convert($l_dash), 0, "L");
			$l_cnt++;
			$pdf->SetFont("ArialCE", "B", 9);
			$pdf->SetXY(10, 30 + 4 * ($l_cnt + 3));
			$pdf->MultiCell(210, 4, _convert('Skupni prikaz'), 0, "L");
			$l_cnt++;
			$pdf->SetFont("ArialCE", "B", 9);
			$pdf->SetXY(10, 30 + 4 * ($l_cnt + 3));
			$pdf->MultiCell(210, 4, _convert($l_dash), 0, "L");
			$l_cnt++;
			$pdf->SetFont("ArialCE", "", 9);
			$la_new_nc_g = array();
			$la_new_pc_g = array();
			$la_new_gname = array();
			for($j = 0; $j < sizeof($la_gname); $j++){
				$la_new_gname[$j] = $la_gname[$j];
			}
			for($j = 0; $j < sizeof($la_gname); $j++){
				switch($j){
					case 2:
					case 3:
					case 4:
					case 5:
						$la_new_nc_g[2] += $la_ukup_nc_g[$j];
						$la_new_pc_g[2] += $la_ukup_pc_g[$j];
						$la_new_gname[$j] = '';
						break;
					default:
						$la_new_nc_g[$j] = $la_ukup_nc_g[$j];
						$la_new_pc_g[$j] = $la_ukup_pc_g[$j];
						$la_new_gname[$j] = $la_gname[$j];
				}
			}
			$la_new_gname[2] = 'Piće (M, B, P, V)';
			$la_new_gname[6] = 'Piće (Žestoko)';
			for($j = 0; $j < sizeof($la_new_gname); $j++){
				if(10 + 4 * ($l_cnt + 3 + 1) > 240){
					$pdf->AddPage();
					$l_cnt = -8;
				}
				if($la_new_gname[$j]){
					$pdf->SetXY(10, 30 + 4 * ($l_cnt + 3));
					$pdf->MultiCell(110, 4, _convert($la_new_gname[$j]), 0, "L");
					$pdf->SetXY(146, 30 + 4 * ($l_cnt + 3));
					$pdf->MultiCell(40, 4, _convert(sprintf('%01.2f', $la_new_nc_g[$j])), 0, "L");
					$pdf->SetXY(185, 30 + 4 * ($l_cnt + 3));
					$pdf->MultiCell(40, 4, _convert(sprintf('%01.2f', $la_new_pc_g[$j])), 0, "L");
					$l_cnt++;
				}
			}
		}
		
		$pdf->SetFont("ArialCE", "B", 9);
		$pdf->SetXY(10, 30 + 4 * ($l_cnt + 3));
		$pdf->MultiCell(210, 4, _convert($l_dash), 0, "L");
		$l_cnt++;
		$pdf->SetXY(10, 30 + 4 * ($l_cnt + 3));
		$pdf->MultiCell(110, 4, _convert('SVEUKUPNO EUR'), 0, "L");
		$pdf->SetXY(146, 30 + 4 * ($l_cnt + 3));
		$pdf->MultiCell(40, 4, _convert(sprintf('%01.2f', $l_ukup_nc)), 0, "L");
		$pdf->SetXY(185, 30 + 4 * ($l_cnt + 3));
		$pdf->MultiCell(40, 4, _convert(sprintf('%01.2f', $l_ukup_pc)), 0, "L");
		$l_cnt++;
		$pdf->SetFont("ArialCE", "B", 9);
		$pdf->SetXY(10, 30 - 1 + 4 * ($l_cnt + 3));
		$pdf->MultiCell(210, 4, _convert($l_dash), 0, "L");
		$l_cnt++;
	}
	
	
	function all_pregled_prodaje_po_artiklu($date_start, $date_end, $vrsta_izvjesca = 'DA', $dobavljac_id = 0){
		
		$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');
		
		$pdf =& new FPDI('P', 'mm', 'A4');
		
		$pdf->AddFont("ArialCE", "B", "6488f5d4657b269b9f5770508e80e299_arialbd.php");
		$pdf->AddFont("ArialCE", "", "332cc83ebee36613c19564e6e776c81b_arial.php");
		
		// set the sourcefile
		$pgNumber = $pdf->setSourceFile($g_path.'app/libs/fpdf/prazni-dokument.pdf');
		
		//$pdf->SetAutoPageBreak(true, 10);
		
		
		$x = get_item('select value from settings where name = "general.name" ').', '.
			get_n_rows(get_item('select value from settings where name = "invoice.header" '), 4);
		
		$la_tmp_1 = explode('-', $date_start);
		$la_tmp_2 = explode('-', $date_end);
		
		$l_datum_1 = $la_tmp_1[0].'.'.$la_tmp_1[1].'.'.$la_tmp_1[2].'.';
		$l_datum_2 = $la_tmp_2[0].'.'.$la_tmp_2[1].'.'.$la_tmp_2[2].'.';
		
		$l_datum_db_1 = $la_tmp_1[2].'-'.$la_tmp_1[1].'-'.$la_tmp_1[0];
		$l_datum_db_2 = $la_tmp_2[2].'-'.$la_tmp_2[1].'-'.$la_tmp_2[0];
		$l_datum_from = $l_datum_db_1.' 00:00:00';
		$l_datum_to = $l_datum_db_2.' 23:59:59';
		
		
		$l_tip_prog = get_item('select value from settings where name = "tipprog" ');
			
		if($l_tip_prog == 'm'){
		}
	
		if($l_tip_prog == 'u'){
			
			$y = get_data('select id from sales where created between "'.$l_datum_from.'" and "'.$l_datum_to.'" ', 1);
				
			$l_id = '0';
			for($i = 0; isset($y['id']) && $i < sizeof($y['id']); $i++){
				$l_id .= ', '.$y['id'][$i];
			}
			
			
			
			
			// dobavljac_id
			$l_dobavljac_join = '';
			
			if($dobavljac_id != "null"){
				$l_add_dobavljac_sql =
					' and receiving_items.article_id = articles.id '.
					' and receiving_items.receiving_id = receivings.id '.
					' and receivings.type = 0 ';
				if($dobavljac_id == -1){
					$l_add_dobavljac_sql .= ' and receivings.supplier_id is null ';
				}else{
					$l_add_dobavljac_sql .= ' and receivings.supplier_id = '.intval($dobavljac_id);
				}
				
				$l_dobavljac_join = '  , receiving_items, receivings ';
				
			}else{
				$l_add_dobavljac_sql = '';
			}
			//$l_add_dobavljac_sql = '';
			//$l_dobavljac_join = '';
			
			
			
			
			if($vrsta_izvjesca == 'DA'){
				$z = get_data('select articles.name, sum(sale_items.amount * product_items.amount) as kol, '.
					' articles.price_buy, sum(sale_items.amount * product_items.amount * articles.price_buy) as NC, '.
					' articles.price_sell, sum(sale_items.amount * product_items.amount * articles.price_sell) as PC, prikaz_groups.id as gid, prikaz_groups.name as gname '.
					' from sale_items, products, product_items, articles, prikaz_groups '.$l_dobavljac_join.
					' where sale_items.product_id = products.id '.
					' and products.id = product_items.product_id '.
					' and product_items.article_id = articles.id '.
					' and articles.prikaz_group_id = prikaz_groups.id '.
					' and sale_items.sale_id in ('.$l_id.') '.
					$l_add_dobavljac_sql.
					' group by articles.name, articles.price_buy, articles.price_sell, prikaz_groups.id, prikaz_groups.name '.
					' order by prikaz_groups.id, prikaz_groups.name, articles.name', 1);
				
				$l_has_groups = 1;
			}else{
				$z = get_data('select articles.name, sum(sale_items.amount * product_items.amount) as kol, '.
					' articles.price_buy, sum(sale_items.amount * product_items.amount * articles.price_buy) as NC, '.
					' articles.price_sell, sum(sale_items.amount * product_items.amount * articles.price_sell) as PC, 0 as gid, "" as gname '.
					' from sale_items, products, product_items, articles '.$l_dobavljac_join.
					' where sale_items.product_id = products.id '.
					' and products.id = product_items.product_id '.
					' and product_items.article_id = articles.id '.
					' and sale_items.sale_id in ('.$l_id.') '.
					$l_add_dobavljac_sql.
					' group by articles.name, articles.price_buy, articles.price_sell '.
					' order by articles.name', 1);
				
				$l_has_groups = 0;
			}
			
			//p($z);
			//exit;
			
			$l_item = '';
			
			$l_cnt = 1;
			for($i = 1; $i <= 1; $i++){
				// add a page
				$pdf->AddPage();
				
				// import page 1
				$tplIdx = $pdf->importPage($i);
				
				// use the imported page and place it at point 0,0 with a width of 210 mm
				$pdf->useTemplate($tplIdx, 0, 0, 210, 297);
				
				
				
				$pdf->SetFont("ArialCE", "B", 11);
				$pdf->SetTextColor(0, 0, 0);
				
				$pdf->SetXY(10, 10);
				$pdf->MultiCell(210, 5, _convert($x), 0, "L");
				
				
				
				$pdf->SetFont("ArialCE", "", 9);
				
				$pdf->SetXY(10, 29);
				$pdf->MultiCell(210, 4, _convert('------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------'), 0, "L");
				
				$pdf->SetFont("ArialCE", "B", 11);
				
				if($vrsta_izvjesca == 'DA'){
					$l_add_title = ' - prikaz po grupama';
				}else{
					$l_add_title = '';
				}
				
				if($dobavljac_id != "null"){
					if($dobavljac_id == -1){
						$l_add_title_dobavljac = ' - Artikli bez dobavljača';
					}else{
						$l_add_title_dobavljac = ' - dobavljač: '.get_item('select name from suppliers where id = '.intval($dobavljac_id));
					}
				}else{
					$l_add_title_dobavljac = ' - Svi dobavljači';
				}
				
				
				$pdf->SetXY(10, 32);
				$pdf->MultiCell(210, 5, _convert('Pregled prodaje po artiklu'.$l_add_title.$l_add_title_dobavljac), 0, "L");
				
				$pdf->SetXY(10, 37);
				$pdf->MultiCell(210, 5, _convert('Period od '.$l_datum_1.' do '.$l_datum_2), 0, "L");
				
				$pdf->SetFont("ArialCE", "", 9);
				$pdf->SetXY(10, 41);
				$pdf->MultiCell(210, 4, _convert('------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------'), 0, "L");
					
					
					
				
				$pdf->SetFont("ArialCE", "B", 8);
				$pdf->SetXY(10, 30 + 4 * ($l_cnt + 3));
				$pdf->MultiCell(60, 4, _convert('Naziv'), 0, "L");
				
				$pdf->SetXY(105, 30 + 4 * ($l_cnt + 3));
				$pdf->MultiCell(18, 4, _convert('Kolicina'), 0, "L");
				
				$pdf->SetXY(121, 30 + 4 * ($l_cnt + 3));
				$pdf->MultiCell(23, 4, _convert('Nabavna cijena'), 0, "L");
				
				$pdf->SetXY(144, 30 + 4 * ($l_cnt + 3));
				$pdf->MultiCell(25, 4, _convert('Ukup NC'), 0, "L");
				
				$pdf->SetXY(160, 30 + 4 * ($l_cnt + 3));
				$pdf->MultiCell(25, 4, _convert('Prodajna cijena'), 0, "L");
				
				$pdf->SetXY(185, 30 + 4 * ($l_cnt + 3));
				$pdf->MultiCell(25, 4, _convert('Ukupno PC'), 0, "L");
				
				$l_cnt++;
				
				$la_gname = array('');
				
				if($l_has_groups && isset($z['gname'][0])){
					$l_cnt++;
					$l_cnt++;
					
					$pdf->SetFont("ArialCE", "B", 9);
					$pdf->SetXY(10, 30 + 4 * ($l_cnt + 3));
					$pdf->MultiCell(65, 4, _convert('Grupa: '.$z['gname'][0]), 0, "L");
					$l_cnt++;
				
					$pdf->SetFont("ArialCE", "", 9);
					$pdf->SetXY(10, 30 + 4 * ($l_cnt + 3));
					$pdf->MultiCell(210, 4, _convert('------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------'), 0, "L");
					$l_cnt++;
					
					$la_gname[0] = $z['gname'][0];
					
				}
				
				
				
				$pdf->SetFont("ArialCE", "", 9);
				
				$l_ukup_nc = 0;
				$l_ukup_pc = 0;
				$la_ukup_nc_g = array(0);
				$la_ukup_pc_g = array(0);
				$l_row_cnt = 0;
				for($j = 0; isset($z['name']) && $j < sizeof($z['name']); $j++){
					
					if(10 + 4 * ($l_cnt + 3 + 1) > 240){
						$pdf->AddPage();
						$l_cnt = -8;
					}
					
					
					if($l_has_groups && $j > 0 && $z['gname'][$j] != $z['gname'][$j - 1]){
						
						$pdf->SetFont("ArialCE", "", 9);
						$pdf->SetXY(10, 30 + 4 * ($l_cnt + 3));
						$pdf->MultiCell(210, 4, _convert('------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------'), 0, "L");
						$l_cnt++;
						
						$pdf->SetFont("ArialCE", "B", 9);
						$pdf->SetXY(10, 30 + 4 * ($l_cnt + 3));
						$pdf->MultiCell(65, 4, _convert('Ukupno grupa: '.$z['gname'][$j - 1]), 0, "L");
						
						$pdf->SetXY(146, 30 + 4 * ($l_cnt + 3));
						$pdf->MultiCell(40, 4, _convert(sprintf('%01.2f', $la_ukup_nc_g[$l_row_cnt])), 0, "L");
						
						$pdf->SetXY(185, 30 + 4 * ($l_cnt + 3));
						$pdf->MultiCell(40, 4, _convert(sprintf('%01.2f', $la_ukup_pc_g[$l_row_cnt])), 0, "L");
						
						$pdf->SetFont("ArialCE", "", 9);
						
						
						$l_cnt++;
						$l_cnt++;
						$l_cnt++;
						$l_cnt++;
						
					
						$pdf->SetFont("ArialCE", "B", 9);
						$pdf->SetXY(10, 30 + 4 * ($l_cnt + 3));
						$pdf->MultiCell(65, 4, _convert('Grupa: '.$z['gname'][$j]), 0, "L");
						$pdf->SetFont("ArialCE", "", 9);
						$l_cnt++;
						
						$pdf->SetFont("ArialCE", "", 9);
						$pdf->SetXY(10, 30 + 4 * ($l_cnt + 3));
						$pdf->MultiCell(210, 4, _convert('------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------'), 0, "L");
						$l_cnt++;
						
						$l_row_cnt++;
						$la_ukup_nc_g[$l_row_cnt] = 0;
						$la_ukup_pc_g[$l_row_cnt] = 0;
						$la_gname[$l_row_cnt] = $z['gname'][$j];
					}
					
					$pdf->SetXY(10, 30 + 4 * ($l_cnt + 3));
					$pdf->MultiCell(90, 4, _convert($z['name'][$j]), 0, "L");
					
					$pdf->SetXY(106, 30 + 4 * ($l_cnt + 3));
					$pdf->MultiCell(25, 4, _convert(sprintf('%01.4f', $z['kol'][$j])), 0, "L");
					
					$pdf->SetXY(127, 30 + 4 * ($l_cnt + 3));
					$pdf->MultiCell(23, 4, _convert(sprintf('%01.2f', $z['price_buy'][$j])), 0, "L");
					
					$pdf->SetXY(146, 30 + 4 * ($l_cnt + 3));
					$pdf->MultiCell(25, 4, _convert(sprintf('%01.2f', $z['NC'][$j])), 0, "L");
					
					$pdf->SetXY(165, 30 + 4 * ($l_cnt + 3));
					$pdf->MultiCell(25, 4, _convert(sprintf('%01.2f', $z['price_sell'][$j])), 0, "L");
					
					$pdf->SetXY(185, 30 + 4 * ($l_cnt + 3));
					$pdf->MultiCell(25, 4, _convert(sprintf('%01.2f', $z['PC'][$j])), 0, "L");
					
					$l_ukup_nc += $z['NC'][$j];
					$la_ukup_nc_g[$l_row_cnt] += $z['NC'][$j];
					$l_ukup_pc += $z['PC'][$j];
					$la_ukup_pc_g[$l_row_cnt] += $z['PC'][$j];
					
					$l_cnt++;
				}
				
				
				if($l_has_groups && isset($z['gname']) && sizeof($z['gname']) > 0){
					$pdf->SetFont("ArialCE", "", 9);
					$pdf->SetXY(10, 30 + 4 * ($l_cnt + 3));
					$pdf->MultiCell(210, 4, _convert('------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------'), 0, "L");
					$l_cnt++;
					
					$pdf->SetFont("ArialCE", "B", 9);
					$pdf->SetXY(10, 30 + 4 * ($l_cnt + 3));
					$pdf->MultiCell(65, 4, _convert('Ukupno grupa: '.$z['gname'][sizeof($z['gname']) - 1]), 0, "L");
					
					$pdf->SetXY(146, 30 + 4 * ($l_cnt + 3));
					$pdf->MultiCell(40, 4, _convert(sprintf('%01.2f', $la_ukup_nc_g[$l_row_cnt])), 0, "L");
					
					$pdf->SetXY(185, 30 + 4 * ($l_cnt + 3));
					$pdf->MultiCell(40, 4, _convert(sprintf('%01.2f', $la_ukup_pc_g[$l_row_cnt])), 0, "L");
					
					$pdf->SetFont("ArialCE", "", 9);
					
					
					$l_cnt++;
					$l_cnt++;
					$l_cnt++;
				}
				
				
				
				if($l_has_groups && isset($la_gname) && sizeof($la_gname) > 0){
					$l_cnt++;
					$l_cnt++;
					$l_cnt++;
					$l_cnt++;
					
					
					
					$pdf->SetFont("ArialCE", "B", 9);
					$pdf->SetXY(10, 30 + 4 * ($l_cnt + 3));
					$pdf->MultiCell(210, 4, _convert('------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------'), 0, "L");
					$l_cnt++;
					
					$pdf->SetFont("ArialCE", "B", 9);
					$pdf->SetXY(10, 30 + 4 * ($l_cnt + 3));
					$pdf->MultiCell(210, 4, _convert('Skupni prikaz'), 0, "L");
					$l_cnt++;
					
					$pdf->SetFont("ArialCE", "B", 9);
					$pdf->SetXY(10, 30 + 4 * ($l_cnt + 3));
					$pdf->MultiCell(210, 4, _convert('------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------'), 0, "L");
					$l_cnt++;
					
					
					$pdf->SetFont("ArialCE", "", 9);
					
					$la_new_nc_g = array();
					$la_new_pc_g = array();
					$la_new_gname = array();
					
					for($j = 0; $j < sizeof($la_gname); $j++){
						$la_new_gname[$j] = $la_gname[$j];
					}
					
					for($j = 0; $j < sizeof($la_gname); $j++){
						switch($j){
							case 2:
							case 3:
							case 4:
							case 5:
								$la_new_nc_g[2] += $la_ukup_nc_g[$j];
								$la_new_pc_g[2] += $la_ukup_pc_g[$j];
								$la_new_gname[$j] = '';
								break;
							
							default:
								$la_new_nc_g[$j] = $la_ukup_nc_g[$j];
								$la_new_pc_g[$j] = $la_ukup_pc_g[$j];
								$la_new_gname[$j] = $la_gname[$j];
						}
					}
					$la_new_gname[2] = 'Piće (M, B, P, V)';
					$la_new_gname[6] = 'Piće (Žestoko)';
					
					//p($la_new_gname);
					
					for($j = 0; $j < sizeof($la_new_gname); $j++){
						
						if(10 + 4 * ($l_cnt + 3 + 1) > 240){
							$pdf->AddPage();
							$l_cnt = -8;
						}
						
						
						if($la_new_gname[$j]){
							$pdf->SetXY(10, 30 + 4 * ($l_cnt + 3));
							$pdf->MultiCell(110, 4, _convert($la_new_gname[$j]), 0, "L");
							
							$pdf->SetXY(146, 30 + 4 * ($l_cnt + 3));
							$pdf->MultiCell(40, 4, _convert(sprintf('%01.2f', $la_new_nc_g[$j])), 0, "L");
							
							$pdf->SetXY(185, 30 + 4 * ($l_cnt + 3));
							$pdf->MultiCell(40, 4, _convert(sprintf('%01.2f', $la_new_pc_g[$j])), 0, "L");
							
							$l_cnt++;
						}
						
					}
					
				}
				
				
				
				$pdf->SetFont("ArialCE", "B", 9);
				$pdf->SetXY(10, 30 + 4 * ($l_cnt + 3));
				$pdf->MultiCell(210, 4, _convert('------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------'), 0, "L");
				$l_cnt++;
				
				$pdf->SetXY(10, 30 + 4 * ($l_cnt + 3));
				$pdf->MultiCell(110, 4, _convert('SVEUKUPNO EUR'), 0, "L");
				
				$pdf->SetXY(146, 30 + 4 * ($l_cnt + 3));
				$pdf->MultiCell(40, 4, _convert(sprintf('%01.2f', $l_ukup_nc)), 0, "L");
				
				$pdf->SetXY(185, 30 + 4 * ($l_cnt + 3));
				$pdf->MultiCell(40, 4, _convert(sprintf('%01.2f', $l_ukup_pc)), 0, "L");
				
				$l_cnt++;
				$pdf->SetFont("ArialCE", "B", 9);
				$pdf->SetXY(10, 30 - 1 + 4 * ($l_cnt + 3));
				$pdf->MultiCell(210, 4, _convert('------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------'), 0, "L");
				$l_cnt++;
				
				
			}
			
			//p($la_ukup);
			//exit;
			
			// ================================================================
			// F2 - E-RAČUNI (fiskalizirani): ista struktura kao F1, nova stranica
			// + zbirni prikaz F1 + F2 po artiklu (kolicine)
			// ================================================================
			$l_es_exists = get_item('SHOW TABLES LIKE "eracun_stavke" ');
			if($l_es_exists != ''){
				$l_dash = '------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------';
				
				$l_e_where = ' (e.external_status = "FISCALIZATION:OK" or e.external_status = "FISCALIZATION_PAYMENT_REPORT:OK") '.
					' and (e.tip_poruke is null or e.tip_poruke != "Kopija") '.
					' and DATE(e.sent_at) >= "'.$l_datum_db_1.'" and DATE(e.sent_at) <= "'.$l_datum_db_2.'" ';
				$ye = get_data('select e.id from eracun e where '.$l_e_where, 1);
				$l_eid = '0';
				for($i = 0; isset($ye['id']) && $i < sizeof($ye['id']); $i++){
					$l_eid .= ', '.$ye['id'][$i];
				}
				
				if($vrsta_izvjesca == 'DA'){
					$z2 = get_data('select articles.name, sum(eracun_stavke.quantity * product_items.amount) as kol, '.
						' articles.price_buy, sum(eracun_stavke.quantity * product_items.amount * articles.price_buy) as NC, '.
						' articles.price_sell, sum(eracun_stavke.quantity * product_items.amount * articles.price_sell) as PC, prikaz_groups.id as gid, prikaz_groups.name as gname '.
						' from eracun_stavke, products, product_items, articles, prikaz_groups '.$l_dobavljac_join.
						' where eracun_stavke.product_id = products.id '.
						' and products.id = product_items.product_id '.
						' and product_items.article_id = articles.id '.
						' and articles.prikaz_group_id = prikaz_groups.id '.
						' and eracun_stavke.eracun_id in ('.$l_eid.') '.
						$l_add_dobavljac_sql.
						' group by articles.name, articles.price_buy, articles.price_sell, prikaz_groups.id, prikaz_groups.name '.
						' order by prikaz_groups.id, prikaz_groups.name, articles.name', 1);
				}else{
					$z2 = get_data('select articles.name, sum(eracun_stavke.quantity * product_items.amount) as kol, '.
						' articles.price_buy, sum(eracun_stavke.quantity * product_items.amount * articles.price_buy) as NC, '.
						' articles.price_sell, sum(eracun_stavke.quantity * product_items.amount * articles.price_sell) as PC, 0 as gid, "" as gname '.
						' from eracun_stavke, products, product_items, articles '.$l_dobavljac_join.
						' where eracun_stavke.product_id = products.id '.
						' and products.id = product_items.product_id '.
						' and product_items.article_id = articles.id '.
						' and eracun_stavke.eracun_id in ('.$l_eid.') '.
						$l_add_dobavljac_sql.
						' group by articles.name, articles.price_buy, articles.price_sell '.
						' order by articles.name', 1);
				}
				
				// ---- nova stranica: F2 zaglavlje ----
				$pdf->AddPage();
				$tplIdx = $pdf->importPage(1);
				$pdf->useTemplate($tplIdx, 0, 0, 210, 297);
				
				$pdf->SetFont("ArialCE", "B", 11);
				$pdf->SetTextColor(0, 0, 0);
				$pdf->SetXY(10, 10);
				$pdf->MultiCell(210, 5, _convert($x), 0, "L");
				
				$pdf->SetFont("ArialCE", "", 9);
				$pdf->SetXY(10, 29);
				$pdf->MultiCell(210, 4, _convert($l_dash), 0, "L");
				
				$pdf->SetFont("ArialCE", "B", 11);
				$pdf->SetXY(10, 32);
				$pdf->MultiCell(210, 5, _convert('Pregled prodaje po artiklu - E-RAČUNI (F2)'.$l_add_title.$l_add_title_dobavljac), 0, "L");
				$pdf->SetXY(10, 37);
				$pdf->MultiCell(210, 5, _convert('Period od '.$l_datum_1.' do '.$l_datum_2), 0, "L");
				
				$pdf->SetFont("ArialCE", "", 9);
				$pdf->SetXY(10, 41);
				$pdf->MultiCell(210, 4, _convert($l_dash), 0, "L");
				
				ppa_render_grupe($pdf, $z2, $l_has_groups);
				
				// ================================================================
				// ZBIRNO F1 + F2 po artiklu (kolicine)
				// ================================================================
				$la_f_kol = array();
				for($j = 0; isset($z['name']) && $j < sizeof($z['name']); $j++){
					$nm = $z['name'][$j];
					if(!isset($la_f_kol[$nm])){ $la_f_kol[$nm] = array('f1' => 0, 'f2' => 0); }
					$la_f_kol[$nm]['f1'] += $z['kol'][$j];
				}
				for($j = 0; isset($z2['name']) && $j < sizeof($z2['name']); $j++){
					$nm = $z2['name'][$j];
					if(!isset($la_f_kol[$nm])){ $la_f_kol[$nm] = array('f1' => 0, 'f2' => 0); }
					$la_f_kol[$nm]['f2'] += $z2['kol'][$j];
				}
				ksort($la_f_kol);
				
				$pdf->AddPage();
				$tplIdx = $pdf->importPage(1);
				$pdf->useTemplate($tplIdx, 0, 0, 210, 297);
				
				$pdf->SetFont("ArialCE", "B", 11);
				$pdf->SetXY(10, 10);
				$pdf->MultiCell(210, 5, _convert($x), 0, "L");
				
				$pdf->SetFont("ArialCE", "", 9);
				$pdf->SetXY(10, 29);
				$pdf->MultiCell(210, 4, _convert($l_dash), 0, "L");
				
				$pdf->SetFont("ArialCE", "B", 11);
				$pdf->SetXY(10, 32);
				$pdf->MultiCell(210, 5, _convert('Zbirno F1 + F2 po artiklu (kolicine)'.$l_add_title_dobavljac), 0, "L");
				$pdf->SetXY(10, 37);
				$pdf->MultiCell(210, 5, _convert('Period od '.$l_datum_1.' do '.$l_datum_2), 0, "L");
				
				$pdf->SetFont("ArialCE", "", 9);
				$pdf->SetXY(10, 41);
				$pdf->MultiCell(210, 4, _convert($l_dash), 0, "L");
				
				$l_cnt = 1;
				$pdf->SetFont("ArialCE", "B", 8);
				$pdf->SetXY(10, 30 + 4 * ($l_cnt + 3));
				$pdf->MultiCell(90, 4, _convert('Artikl'), 0, "L");
				$pdf->SetXY(110, 30 + 4 * ($l_cnt + 3));
				$pdf->MultiCell(30, 4, _convert('F1 kolicina'), 0, "R");
				$pdf->SetXY(145, 30 + 4 * ($l_cnt + 3));
				$pdf->MultiCell(30, 4, _convert('F2 kolicina'), 0, "R");
				$pdf->SetXY(180, 30 + 4 * ($l_cnt + 3));
				$pdf->MultiCell(30, 4, _convert('Ukupno'), 0, "R");
				$l_cnt++;
				
				$pdf->SetFont("ArialCE", "", 9);
				$pdf->SetXY(10, 30 + 4 * ($l_cnt + 3));
				$pdf->MultiCell(210, 4, _convert($l_dash), 0, "L");
				$l_cnt++;
				
				$l_tot_f1 = 0;
				$l_tot_f2 = 0;
				foreach($la_f_kol as $nm => $kol){
					if(10 + 4 * ($l_cnt + 3 + 1) > 240){
						$pdf->AddPage();
						$l_cnt = -8;
					}
					$pdf->SetFont("ArialCE", "", 9);
					$pdf->SetXY(10, 30 + 4 * ($l_cnt + 3));
					$pdf->MultiCell(95, 4, _convert($nm), 0, "L");
					$pdf->SetXY(110, 30 + 4 * ($l_cnt + 3));
					$pdf->MultiCell(30, 4, _convert(sprintf('%01.4f', $kol['f1'])), 0, "R");
					$pdf->SetXY(145, 30 + 4 * ($l_cnt + 3));
					$pdf->MultiCell(30, 4, _convert(sprintf('%01.4f', $kol['f2'])), 0, "R");
					$pdf->SetXY(180, 30 + 4 * ($l_cnt + 3));
					$pdf->MultiCell(30, 4, _convert(sprintf('%01.4f', $kol['f1'] + $kol['f2'])), 0, "R");
					$l_tot_f1 += $kol['f1'];
					$l_tot_f2 += $kol['f2'];
					$l_cnt++;
				}
				
				$pdf->SetFont("ArialCE", "B", 9);
				$pdf->SetXY(10, 30 + 4 * ($l_cnt + 3));
				$pdf->MultiCell(210, 4, _convert($l_dash), 0, "L");
				$l_cnt++;
				$pdf->SetXY(10, 30 + 4 * ($l_cnt + 3));
				$pdf->MultiCell(95, 4, _convert('SVEUKUPNO'), 0, "L");
				$pdf->SetXY(110, 30 + 4 * ($l_cnt + 3));
				$pdf->MultiCell(30, 4, _convert(sprintf('%01.4f', $l_tot_f1)), 0, "R");
				$pdf->SetXY(145, 30 + 4 * ($l_cnt + 3));
				$pdf->MultiCell(30, 4, _convert(sprintf('%01.4f', $l_tot_f2)), 0, "R");
				$pdf->SetXY(180, 30 + 4 * ($l_cnt + 3));
				$pdf->MultiCell(30, 4, _convert(sprintf('%01.4f', $l_tot_f1 + $l_tot_f2)), 0, "R");
			}
			
		}
		
		
		
			
		$l_name = 'pregled_prodaje_po_artiklu.pdf';
		$l_dest = $g_path.'app/webroot/res/files/'.$l_name;
		
		$pdf->Output($l_dest, 'F');
		
		return array('path' => $g_path.'app/webroot/res/files/', 'name' => $l_name);
	}
	
	
	
	function all_porez_na_potrosnju($date_start, $date_end){
		
		$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');
		
		$pdf =& new FPDI('P', 'mm', 'A4');
		
		$pdf->AddFont("ArialCE", "B", "6488f5d4657b269b9f5770508e80e299_arialbd.php");
		$pdf->AddFont("ArialCE", "", "332cc83ebee36613c19564e6e776c81b_arial.php");
		
		// set the sourcefile
		$pgNumber = $pdf->setSourceFile($g_path.'app/libs/fpdf/prazni-dokument.pdf');
		
		//$pdf->SetAutoPageBreak(true, 10);
		
		
		
		$l_tip_prog = get_item('select value from settings where name = "tipprog" ');
		
		
		$l_pdv = get_item('select value from settings where name = "duty.free" ');
		
		if($l_pdv == 'no'){
			$l_pdv = 'false';
		}
		
		if($l_pdv == 'false'){
			$l_pdv_koment = get_item('select value from settings where name = "duty.comment" ');
		}else{
			$l_pdv_koment = '';
		}
		
		$x = get_item('select value from settings where name = "general.name" ').', '.
			get_n_rows(get_item('select value from settings where name = "invoice.header" '), 4);
		
		
		$la_tmp = explode('-', $date_start);
		$la_tmp2 = explode('-', $date_end);
		
		$l_datum = $la_tmp[0].'.'.$la_tmp[1].'.'.$la_tmp[2].'.';
		$l_datum2 = $la_tmp2[0].'.'.$la_tmp2[1].'.'.$la_tmp2[2].'.';
		
		$l_datum_db = $la_tmp[2].'-'.$la_tmp[1].'-'.$la_tmp[0];
		$l_datum_db2 = $la_tmp2[2].'-'.$la_tmp2[1].'-'.$la_tmp2[0];
		$l_datum_from = $l_datum_db.' 00:00:00';
		$l_datum_to = $l_datum_db2.' 23:59:59';
		
		
		if($l_tip_prog == 'u'){
			$la_tax = array();
			$la_pn = array();
			
			$la_tax[0] = array();
			$la_pn[0] = 0;
			
			$la_sumarno = array(
				'opis' => array(),
				'osnovica' => array(),
				'stopa' => array(),
				'ukupno' => array()
			);
				
			$la_preslika_txt = array(
				'2' => 'Vino',
				'13' => 'Zestoka pica',
				'5' => 'Pivo',
				'16' => 'Bezalkoholna pica'
			);
			
			$la_preslika = array(
				'real' => array(2, 3, 4, 5, 6, 7),
				'sum' => array(2, 13, 13, 5, 16, 16),
			);
			
			$l_tip_prog = get_item('select value from settings where name = "tipprog" ');
			
			$l_min_rbr = 99999999999;
			$l_max_rbr = 0;
			
			$y = get_data('select * from sales '.
				' where 1 = 1 '.
				' and created between "'.$l_datum_from.'" and "'.$l_datum_to.'" ', 1);
				
			$l_id = '0';
			for($i = 0; isset($y['id']) && $i < sizeof($y['id']); $i++){
				$l_id .= ', '.$y['id'][$i];
			}
			
			
			$z = get_data('select tax_groups.id, tax_groups.name, sale_items.sale_id, sale_items.amount, sale_items.base_price, '.
				' sale_items.discount_percent, sale_items.price, sale_items.tax_1, sale_items.tax_2 '.
				' from sale_items, products, tax_groups '.
				' where sale_items.product_id = products.id '.
				' and products.tax_group_id = tax_groups.id '.
				' and sale_items.sale_id in ('.$l_id.')  '.
				' order by sale_items.sale_id, sale_items.id', 1);
			
			
			$l_item = '';
			
			for($i = 0; isset($z['id']) && $i < sizeof($z['id']); $i++){
				if($l_pdv == 'false'){
					$z['tax_1'][$i] = 'no_pdv';
				}
				
				
				if(empty($la_tax[0][$z['name'][$i]]['id'])){
					$la_tax[0][$z['name'][$i]]['id'] = $z['id'][$i];
				}
				if(empty($la_tax[0][$z['name'][$i]]['tax_1'][$z['tax_1'][$i]])){
					$la_tax[0][$z['name'][$i]]['tax_1'][$z['tax_1'][$i]] = 0;
				}
				$la_tax[0][$z['name'][$i]]['tax_1'][$z['tax_1'][$i]] += $z['price'][$i];
				
				if(empty($la_tax[0][$z['name'][$i]]['tax_2'][$z['tax_1'][$i]][$z['tax_2'][$i]])){
					$la_tax[0][$z['name'][$i]]['tax_2'][$z['tax_1'][$i]][$z['tax_2'][$i]] = 0;
				}
				$la_tax[0][$z['name'][$i]]['tax_2'][$z['tax_1'][$i]][$z['tax_2'][$i]] += $z['price'][$i];
				
				
				
				if(empty($la_pn[0])){
					$la_pn[0] = 0;
				}
			}
			
			
			
			for($i = 1; $i <= 1; $i++){
				// add a page
				$pdf->AddPage();
				
				// import page 1
				$tplIdx = $pdf->importPage($i);
				
				// use the imported page and place it at point 0,0 with a width of 210 mm
				$pdf->useTemplate($tplIdx, 0, 0, 210, 297);
				
				
				
				$pdf->SetFont("ArialCE", "B", 11);
				$pdf->SetTextColor(0, 0, 0);
				
				$pdf->SetXY(10, 10);
				$pdf->MultiCell(210, 5, _convert($x), 0, "L");
				
				$pdf->SetXY(10, 30);
				$pdf->MultiCell(210, 5, _convert('Porez na potrosnju'), 0, "L");
				
				$pdf->SetXY(10, 35);
				$pdf->MultiCell(210, 5, _convert('Period od '.$l_datum.' do '.$l_datum2), 0, "L");
				
				
				$l_cnt = -2;
				
				if(50 + 4 * ($l_cnt + 3 + 1) > 210){
					$pdf->AddPage();
					$l_cnt = -8;
				}
				
				$pdf->SetFont("ArialCE", "", 9);
				$pdf->SetXY(10, 50 + 4 * ($l_cnt + 2));
				$pdf->MultiCell(210, 4, _convert('---------------------------------------------------------------------------------------------------------------'), 0, "L");
				
				$pdf->SetFont("ArialCE", "B", 8);
				$pdf->SetXY(10, 50 + 4 * ($l_cnt + 3));
				$pdf->MultiCell(35, 4, _convert('Opis'), 0, "L");
				
				$pdf->SetXY(50, 50 + 4 * ($l_cnt + 3));
				$pdf->MultiCell(25, 4, _convert('Osnovica'), 0, "L");
				
				$pdf->SetXY(80, 50 + 4 * ($l_cnt + 3));
				$pdf->MultiCell(25, 4, _convert('Stopa'), 0, "L");
					
				$pdf->SetXY(113, 50 + 4 * ($l_cnt + 3));
				$pdf->MultiCell(25, 4, _convert('Ukupno'), 0, "L");
				
				$l_cnt++;
				
				$pdf->SetFont("ArialCE", "", 9);
				$pdf->SetXY(10, 50 + 4 * ($l_cnt + 3));
				$pdf->MultiCell(210, 4, _convert('---------------------------------------------------------------------------------------------------------------'), 0, "L");
				
				
				$pdf->SetFont("ArialCE", "", 8);
				
				$l_iznos_ukup = 0;
				$l_osnovica_ukup = 0;
				
				
				foreach($la_tax[0] as $key => $value){
					foreach($value['tax_1'] as $key2 => $value2){
						
						$l_skip = 0;
						foreach($la_tax[0][$key]['tax_2'][$key2] as $key3 => $value3){
							if($key3 == 0){
								$l_skip = 1;
								break;
							}
						}
						if($l_skip == 1){
							continue;
						}
						
						
						$l_ukup_porez = 0;
						
						$pdf->SetXY(10, 50 + 4 * ($l_cnt + 3 + 1));
						$pdf->MultiCell(35, 4, _convert($key), 0, "L");
						
						$l_ukup_porez += $key2;
						
						// zbog strukture podataka, samo jedan zapis ima (iako koristim foreach petlju, najbrze mi bilo iskoristit tu petlju)
						foreach($la_tax[0][$key]['tax_2'][$key2] as $key3 => $value3){
							if($key3 == 0){
								break;
							}
							$pdf->SetXY(80, 50 + 4 * ($l_cnt + 3 + 1));
							$pdf->MultiCell(25, 4, _convert($key3), 0, "L");
							
							$l_ukup_porez += $key3;
						}
						
						$pdf->SetXY(50, 50 + 4 * ($l_cnt + 3 + 1));
						$pdf->MultiCell(15, 4, _convert(sprintf('%01.2f', $value2 / (1 + $l_ukup_porez / 100))), 0, "R");
						
						$l_pos = array_search($la_tax[0][$key]['id'], $la_preslika['real']);
						$l_new = $la_preslika['sum'][$l_pos];
						
						if(empty($la_sumarno['osnovica'][$l_new])){
							$la_sumarno['osnovica'][$l_new] = 0;
						}
						if(empty($la_sumarno['ukupno'][$l_new])){
							$la_sumarno['ukupno'][$l_new] = 0;
						}
						$la_sumarno['osnovica'][$l_new] += $value2 / (1 + $l_ukup_porez / 100);
						$la_sumarno['ukupno'][$l_new] += $key3 * $value2 / (1 + $l_ukup_porez / 100) / 100;
						
						$la_sumarno['stopa'][$l_new] = $key3;
						
						$pdf->SetXY(110, 50 + 4 * ($l_cnt + 3 + 1));
						$pdf->MultiCell(15, 4, _convert(sprintf('%01.2f', $key3 * $value2 / (1 + $l_ukup_porez / 100) / 100)), 0, "R");
						
						
						$l_osnovica_ukup += $value2 / (1 + $l_ukup_porez / 100);
						$l_iznos_ukup += $key3 * $value2 / (1 + $l_ukup_porez / 100) / 100;
						
						
						$l_cnt++;
					}
					
				}
				
				
				
				
				$pdf->SetFont("ArialCE", "", 9);
				
				$pdf->SetXY(10, 50 + 4 * ($l_cnt + 3 + 1));
				$pdf->MultiCell(210, 4, _convert('---------------------------------------------------------------------------------------------------------------'), 0, "L");
				
				$l_cnt++;
				
				$pdf->SetXY(10, 50 + 4 * ($l_cnt + 3 + 1));
				$pdf->MultiCell(35, 4, _convert('Ukupno'), 0, "L");
						
				$pdf->SetXY(45, 50 + 4 * ($l_cnt + 3 + 1));
				$pdf->MultiCell(20, 4, _convert(sprintf('%01.2f', $l_osnovica_ukup).' EUR'), 0, "R");
				
				$pdf->SetXY(105, 50 + 4 * ($l_cnt + 3 + 1));
				$pdf->MultiCell(20, 4, _convert(sprintf('%01.2f', $l_iznos_ukup).' EUR'), 0, "R");
				
				
				
				$l_cnt++;
				$l_cnt++;
				$l_cnt++;
				$l_cnt++;
				$l_cnt++;
				$l_cnt++;
				
				
				
				if(50 + 4 * ($l_cnt + 3 + 1) > 200){
					$pdf->AddPage();
					$l_cnt = -8;
				}
				
				$pdf->SetFont("ArialCE", "", 9);
				$pdf->SetXY(10, 50 + 4 * ($l_cnt + 2));
				$pdf->MultiCell(210, 4, _convert('------------------------------------------------- SUMARNO ---------------------------------------------'), 0, "L");
				
				$pdf->SetFont("ArialCE", "B", 8);
				$pdf->SetXY(10, 50 + 4 * ($l_cnt + 3));
				$pdf->MultiCell(35, 4, _convert('Opis'), 0, "L");
				
				$pdf->SetXY(50, 50 + 4 * ($l_cnt + 3));
				$pdf->MultiCell(25, 4, _convert('Osnovica'), 0, "L");
				
				$pdf->SetXY(80, 50 + 4 * ($l_cnt + 3));
				$pdf->MultiCell(25, 4, _convert('Stopa'), 0, "L");
					
				$pdf->SetXY(113, 50 + 4 * ($l_cnt + 3));
				$pdf->MultiCell(25, 4, _convert('Ukupno'), 0, "L");
				
				$l_cnt++;
				
				$pdf->SetFont("ArialCE", "", 9);
				$pdf->SetXY(10, 50 + 4 * ($l_cnt + 3));
				$pdf->MultiCell(210, 4, _convert('---------------------------------------------------------------------------------------------------------------'), 0, "L");
				
				$l_cnt++;
				
				
				foreach($la_sumarno['osnovica'] as $key => $value){
					$la_sumarno['opis'][$key] = $la_preslika_txt[$key];
				}
				
				$pdf->SetFont("ArialCE", "", 8);
				
				foreach($la_sumarno['osnovica'] as $key => $value){
					$pdf->SetXY(10, 50 + 4 * ($l_cnt + 3));
					$pdf->MultiCell(35, 4, _convert($la_sumarno['opis'][$key]), 0, "L");
					
					$pdf->SetXY(50, 50 + 4 * ($l_cnt + 3));
					$pdf->MultiCell(15, 4, _convert(sprintf('%01.2f', $la_sumarno['osnovica'][$key])), 0, "R");
					
					$pdf->SetXY(80, 50 + 4 * ($l_cnt + 3));
					$pdf->MultiCell(25, 4, _convert($la_sumarno['stopa'][$key]), 0, "L");
					
					$pdf->SetXY(110, 50 + 4 * ($l_cnt + 3));
					$pdf->MultiCell(15, 4, _convert(sprintf('%01.2f', $la_sumarno['ukupno'][$key])), 0, "R");
					
					$l_cnt++;
				}
				
				
				
				$pdf->SetFont("ArialCE", "", 9);
				
				$pdf->SetXY(10, 50 + 4 * ($l_cnt + 3));
				$pdf->MultiCell(210, 4, _convert('---------------------------------------------------------------------------------------------------------------'), 0, "L");
				
				$l_cnt++;
				
				$pdf->SetXY(10, 50 + 4 * ($l_cnt + 3));
				$pdf->MultiCell(35, 4, _convert('Ukupno'), 0, "L");
						
				$pdf->SetXY(45, 50 + 4 * ($l_cnt + 3));
				$pdf->MultiCell(20, 4, _convert(sprintf('%01.2f', $l_osnovica_ukup).' EUR'), 0, "R");
				
				$pdf->SetXY(105, 50 + 4 * ($l_cnt + 3));
				$pdf->MultiCell(20, 4, _convert(sprintf('%01.2f', $l_iznos_ukup).' EUR'), 0, "R");
				
				//p($la_sumarno);
				//p($la_tax[0]);
				//exit;
				
				
				$pdf->SetXY(10, 50 + 4 * ((++$l_cnt + 5)));
				$pdf->MultiCell(210, 4, _convert($l_pdv_koment), 0, "L");
					
			}
			
		}
		
			
		$l_name = 'porez_na_potrosnju.pdf';
		$l_dest = $g_path.'app/webroot/res/files/'.$l_name;
		
		$pdf->Output($l_dest, 'F');
		
		return array('path' => $g_path.'app/webroot/res/files/', 'name' => $l_name);
	}
	
	
	
	
	function all_stanje_skladista($date, $time, $group_id){
		
		$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');
		
		$pdf =& new FPDI('L', 'mm', 'A4');
		
		$pdf->AddFont("ArialCE", "B", "6488f5d4657b269b9f5770508e80e299_arialbd.php");
		$pdf->AddFont("ArialCE", "", "332cc83ebee36613c19564e6e776c81b_arial.php");
		
		// set the sourcefile
		$pgNumber = $pdf->setSourceFile($g_path.'app/libs/fpdf/prazni-dokument-L.pdf');
		
		
		// add a page
		$pdf->AddPage();
		
		// import page 1
		$tplIdx = $pdf->importPage(1);
		
		// use the imported page and place it at point 0,0 with a width of 210 mm
		$pdf->useTemplate($tplIdx, 0, 0, 297, 210);
		
		
		$pdf->SetTextColor(0, 0, 0);
		
		$x = get_item('select value from settings where name = "general.name" ').', '.
			get_n_rows(get_item('select value from settings where name = "invoice.header" '), 4);
		$pdf->SetFont("ArialCE", "B", 9);
		$pdf->SetXY(10, 10);
		$pdf->MultiCell(110, 4, _convert($x), 0, "L");
		
		$pdf->SetFont("ArialCE", "B", 12);
		$pdf->SetXY(170, 13);
		$pdf->MultiCell(110, 5, _convert('Stanje skladista na dan '.$date.', '.$time.'h'), 0, "C");
		
		$pdf->SetFont("ArialCE", "", 8);
		
		
		
		$l_tip_prog = get_item('select value from settings where name = "tipprog" ');
		
		$l_cond_title = '';
		
		if($l_tip_prog == 'm'){
			$la_header = array('R.br.', 'Barcode', 'Naziv-artikla', 'j.mj.', 'Kol', 'NC', 'PC', 'ukNC', 'ukPC');
			$la_width = array(10, 30, 95, 12, 17, 19, 19, 25, 25);
			
			if($group_id > 0){
				$l_cond_title = ' - '.get_item('select name from article_groups where id = '.intval($group_id));
			}
			
		}
		
		
		if($l_tip_prog == 'u'){
			$la_header = array('R.br.', 'Naziv-artikla', 'j.mj.', 'Kol', 'NC', 'PC', 'ukNC', 'ukPC');
			$la_width = array(10, 105, 12, 22, 20, 20, 25, 25);
			
			
			if($group_id > 0){
				$l_cond_title = ' - '.get_item('select name from prikaz_groups where id = '.intval($group_id));
			}
			
		}
		
		
		$pdf->SetFont("ArialCE", "B", 9);
		
		$pdf->SetXY(10, 29);
		$pdf->MultiCell(273, 5, _convert('SKLADIŠTE ROBA'.$l_cond_title), 'B', "L");
		
		$l_pos = 0;
		for($i = 0; $i < sizeof($la_header); $i++){
			if($i > 0){
				$l_pos += $la_width[$i - 1];
			}
			$pdf->SetXY(10 + $l_pos, 37);
			$pdf->MultiCell($la_width[$i], 5, _convert($la_header[$i]), 0, "L");
		}
		
		
		$pdf->SetFont("ArialCE", "", 9);
		
		
		$la_tmp = explode('-', $date);
		$l_date_str = $la_tmp[2].'-'.$la_tmp[1].'-'.$la_tmp[0];
		
		$l_tip_prog = get_item('select value from settings where name = "tipprog" ');
		
		$l_ukNC = 0;
		$l_ukPC = 0;
		
		
		$l_cond = '';
		
		if($l_tip_prog == 'm'){
			
			if($group_id > 0){
				$l_cond = ' and articles.article_group_id = '.$group_id;
			}
			
			$l_str_start = 'select receivings.document_date as date, receivings.type, receiving_items.amount, articles.id, articles.name '.
				' from receivings, receiving_items, articles '.
				' where receivings.id = receiving_items.receiving_id '.
				' and receiving_items.article_id = articles.id '.
				' and receivings.posted = 1 '.
				' and articles.id = __{_x_id_i_}__ '.
				' and articles.isService = 0 '.
				' and receivings.document_date >= "'.$l_date_str.' '.$time.'" '.
				' union all '.
				' select sales.date, "I" as type, retail_items.amount, articles.id, articles.name '.
				' from sales, retail_items, articles '.
				' where sales.id = retail_items.sale_id '.
				' and retail_items.article_id = articles.id '.
				' and articles.id = __{_x_id_i_}__ '.
				' and articles.isService = 0 '.
				' and sales.date >= "'.$l_date_str.' '.$time.'" '.
				' order by date, type ';
		}
		
		if($l_tip_prog == 'u'){
			
			if($group_id > 0){
				$l_cond = ' and articles.prikaz_group_id = '.$group_id;
			}
			
			
			$l_str_start = 'select receivings.document_date as date, receivings.type, receiving_items.amount, articles.id, articles.name '.
				' from receivings, receiving_items, articles '.
				' where receivings.id = receiving_items.receiving_id '.
				' and receiving_items.article_id = articles.id '.
				' and receivings.posted = 1 '.
				' and articles.id = __{_x_id_i_}__ '.
				' and articles.isService = 0 '.
				' and receivings.document_date >= "'.$l_date_str.' '.$time.'" '.
				' union all '.
				' select sales.date, "I" as type, sale_items.amount * product_items.amount as amount, articles.id, articles.name '.
				' from sales, sale_items, products, product_items, articles '.
				' where sales.id = sale_items.sale_id '.
				' and sale_items.product_id = products.id '.
				' and products.id = product_items.product_id '.
				' and product_items.article_id = articles.id '.
				' and articles.id = __{_x_id_i_}__ '.
				' and articles.isService = 0 '.
				' and sales.date >= "'.$l_date_str.' '.$time.'" '.
				' union all '.
				' select medjus.datum as date, "F" as type, medju_items.kolicina * product_items.amount as amount, articles.id, articles.name '.
				' from medjus, medju_items, products, product_items, articles '.
				' where medjus.id = medju_items.medju_id '.
				' and medju_items.product_id = products.id '.
				' and products.id = product_items.product_id '.
				' and product_items.article_id = articles.id '.
				' and articles.id = __{_x_id_i_}__ '.
				' and articles.isService = 0 '.
				' and medjus.datum >= "'.$l_date_str.' '.$time.'" '.
				' order by date desc, type ';
		}
		
		
		
		
		
		
		$x = get_data('select id, name, unit, amount, price_buy, price_sell, barcode from articles where articles.isService = 0 '.$l_cond.' order by name, id ', 1);
		$l_y = 7;
		for($i = 0; $i < sizeof($x['id']); $i++){
			
			$l_str = str_replace('__{_x_id_i_}__', $x['id'][$i], $l_str_start);
			
			$y = get_data($l_str, 1);
			
			for($j = 0; isset($y['date']) && $j < sizeof($y['date']); $j++){
				
				switch($y['type'][$j]){
					case 'F':
						// proizvodnja ("Factory")
						$x['amount'][$i] += abs($y['amount'][$j]);
						break;
						
					case 'I':
						// prodaja("Invoice")
						$x['amount'][$i] += $y['amount'][$j];
						break;
						
					case '0':
						// primka
						$x['amount'][$i] -= $y['amount'][$j];
						break;
						
					case '1':
						// otpis
						$x['amount'][$i] += abs($y['amount'][$j]);
						break;
						
					case '2':
						// povratnica
						$x['amount'][$i] += abs($y['amount'][$j]);
						break;
						
					case '3':
						// medjuskladisnica
						$x['amount'][$i] += abs($y['amount'][$j]);
						break;
						
					case '4':
						// inventura
						$x['amount'][$i] += $y['amount'][$j];
						break;
					
					default:
				}
				
			}
			
			
			
			
			$la_data = array();
			$la_data[0] = $i + 1;
			
			if($l_tip_prog == 'm'){
				$l_pomak = 1;
				$la_data[0 + $l_pomak] = $x['barcode'][$i];
			}
			if($l_tip_prog == 'u'){
				$l_pomak = 0;
			}
			
			$la_data[1 + $l_pomak] = strlen($x['name'][$i]) > 42 ? substr($x['name'][$i], 0, 42) : $x['name'][$i];
			$la_data[2 + $l_pomak] = strlen($x['unit'][$i]) > 5 ? substr($x['unit'][$i], 0, 5) : $x['unit'][$i];
			$la_data[3 + $l_pomak] = sprintf('%01.3f', $x['amount'][$i]);
			$la_data[4 + $l_pomak] = $x['price_buy'][$i];
			$la_data[5 + $l_pomak] = $x['price_sell'][$i];
			$la_data[6 + $l_pomak] = sprintf('%01.2f', $x['amount'][$i] * $x['price_buy'][$i]);
			if($x['amount'][$i] > 0){
				$l_ukNC += $x['amount'][$i] * $x['price_buy'][$i];
			}
			$la_data[7 + $l_pomak] = sprintf('%01.2f', $x['amount'][$i] * $x['price_sell'][$i]);
			if($x['amount'][$i] > 0){
				$l_ukPC += $x['amount'][$i] * $x['price_sell'][$i];
			}
			
			
			
			
			$l_pos = 0;
			for($j = 0; $j < sizeof($la_data); $j++){
				if($j > 0){
					$l_pos += $la_width[$j - 1];
				}
				$pdf->SetXY(10 + $l_pos, 36 + $l_y);
				$pdf->MultiCell($la_width[$j], 5, _convert($la_data[$j]), 0, "L");
			}
			$l_y += 5;
			
			
			if($l_y >= 150){
				$pdf->AddPage();
				$l_y = -25;
			}
		}
		
		
		
		if($l_tip_prog == 'm'){
			$la_header_bottom = array('', '', '', '', '', 'Ukupno EUR', '', sprintf('%01.2f', $l_ukNC), sprintf('%01.2f', $l_ukPC));
			$la_width_bottom = array(10, 26, 95, 12, 17, 25, 13, 25, 19);
		}
		if($l_tip_prog == 'u'){
			$la_header_bottom = array('', '', '', '', 'Ukupno EUR', '', sprintf('%01.2f', $l_ukNC), sprintf('%01.2f', $l_ukPC));
			$la_width_bottom = array(10, 105, 12, 22, 25, 15, 25, 19);
		}
		
		$pdf->SetFont("ArialCE", "B", 9);
		$l_pos = 0;
		for($i = 0; $i < sizeof($la_header_bottom); $i++){
			if($i > 0){
				$l_pos += $la_width_bottom[$i - 1];
			}
			$pdf->SetXY(10 + $l_pos, 36 + $l_y + 1);
			$pdf->MultiCell($la_width_bottom[$i], 5, _convert($la_header_bottom[$i]), 'T', "L");
		}
		
		
		
		
		
		$l_y += 10;
		
		
		if($l_y >= 150){
			$pdf->AddPage();
			$l_y = -25;
		}
		
			
		
		$pdf->SetFont("ArialCE", "B", 9);
		
		$pdf->SetXY(10, 36 + $l_y + 3);
		$pdf->MultiCell(273, 15, _convert('Napomena: Ukupna vrijednost je samo za artikle sa stanjem > 0'), 'B', "L");
		$l_y += 30;
		
		
		
		if($l_y >= 150){
			$pdf->AddPage();
			$l_y = -25;
		}
		
		
		
		
		$pdf->SetFont("ArialCE", "B", 9);
		
		$pdf->SetXY(10, 36 + $l_y + 3);
		$pdf->MultiCell(273, 5, _convert('POPIS USLUGA'.$l_cond_title), 'B', "L");
		
		
		if($l_tip_prog == 'm'){
			$la_header = array('R.br.', 'Barcode', 'Naziv-artikla', 'j.mj.', 'NC', 'PC');
			$la_width = array(10, 33, 125, 17, 19, 19);
		}
		
		if($l_tip_prog == 'u'){
			$la_header = array('R.br.', 'Naziv-artikla', 'j.mj.', 'NC', 'PC');
			$la_width = array(10, 125, 17, 20, 20);
		}
		
		
		
		$l_y += 7;
		
		if($l_y >= 150){
			$pdf->AddPage();
			$l_y = -25;
		}
		
		$l_pos = 0;
		for($i = 0; $i < sizeof($la_header); $i++){
			if($i > 0){
				$l_pos += $la_width[$i - 1];
			}
			$pdf->SetXY(10 + $l_pos, 36 + $l_y + 3);
			$pdf->MultiCell($la_width[$i], 5, _convert($la_header[$i]), 0, "L");
		}
		
		
		$l_y += 5;
		
		if($l_y >= 150){
			$pdf->AddPage();
			$l_y = -25;
		}
		
		$pdf->SetFont("ArialCE", "", 9);
		
		
		$x = get_data('select id, name, unit, amount, price_buy, price_sell, barcode from articles where articles.isService = 1 '.$l_cond.' order by name, id ', 1);
		
		for($i = 0; $i < sizeof($x['id']); $i++){
			
			$la_data = array();
			$la_data[0] = $i + 1;
			
			if($l_tip_prog == 'm'){
				$l_pomak = 1;
				$la_data[0 + $l_pomak] = $x['barcode'][$i];
			}
			if($l_tip_prog == 'u'){
				$l_pomak = 0;
			}
			
			$la_data[1 + $l_pomak] = $x['name'][$i];
			$la_data[2 + $l_pomak] = $x['unit'][$i];
			$la_data[3 + $l_pomak] = $x['price_buy'][$i];
			$la_data[4 + $l_pomak] = $x['price_sell'][$i];
			
			
			
			$l_pos = 0;
			for($j = 0; $j < sizeof($la_data); $j++){
				if($j > 0){
					$l_pos += $la_width[$j - 1];
				}
				$pdf->SetXY(10 + $l_pos, 36 + $l_y + 3);
				$pdf->MultiCell($la_width[$j], 5, _convert($la_data[$j]), 0, "L");
			}
			
			$l_y += 5;
			
			if($l_y >= 150){
				$pdf->AddPage();
				$l_y = -25;
			}
			
		}
		
		
		
		$l_name = 'stanje_skladista.pdf';
		$l_dest = $g_path.'app/webroot/res/files/'.$l_name;
		
		$pdf->Output($l_dest, 'F');
		
		return array('path' => $g_path.'app/webroot/res/files/', 'name' => $l_name);
	}
	
		function all_stanje_skladista_iz_artikala($date, $time){
		
		$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');
		
		$pdf =& new FPDI('L', 'mm', 'A4');
		
		$pdf->AddFont("ArialCE", "B", "6488f5d4657b269b9f5770508e80e299_arialbd.php");
		$pdf->AddFont("ArialCE", "", "332cc83ebee36613c19564e6e776c81b_arial.php");
		
		// set the sourcefile
		$pgNumber = $pdf->setSourceFile($g_path.'app/libs/fpdf/prazni-dokument-L.pdf');
		
		
		// add a page
		$pdf->AddPage();
		
		// import page 1
		$tplIdx = $pdf->importPage(1);
		
		// use the imported page and place it at point 0,0 with a width of 210 mm
		$pdf->useTemplate($tplIdx, 0, 0, 297, 210);
		
		
		$pdf->SetTextColor(0, 0, 0);
		
		$x = get_item('select value from settings where name = "general.name" ').', '.
			get_n_rows(get_item('select value from settings where name = "invoice.header" '), 4);
		$pdf->SetFont("ArialCE", "B", 9);
		$pdf->SetXY(10, 10);
		$pdf->MultiCell(110, 4, _convert($x), 0, "L");
		
		$pdf->SetFont("ArialCE", "B", 12);
		$pdf->SetXY(130, 13);
		$pdf->MultiCell(150, 5, _convert('Stanje skladista (iz artikala) na dan '.$date.', '.$time.'h'), 0, "C");
		
		$pdf->SetFont("ArialCE", "", 8);
		
		
		
		$l_tip_prog = get_item('select value from settings where name = "tipprog" ');
		
		$l_cond_title = '';
		
		if($l_tip_prog == 'm'){
			$la_header = array('R.br.', 'Barcode', 'Naziv-artikla', 'j.mj.', 'Kol', 'NC', 'PC', 'ukNC', 'ukPC');
			$la_width = array(10, 30, 95, 12, 17, 19, 19, 25, 25);
			
		}
		
		
		if($l_tip_prog == 'u'){
			$la_header = array('R.br.', 'Naziv-artikla', 'j.mj.', 'Kol', 'NC', 'PC', 'ukNC', 'ukPC');
			$la_width = array(10, 105, 12, 22, 20, 20, 25, 25);
			
			
		}
		
		
		$pdf->SetFont("ArialCE", "B", 9);
		
		$pdf->SetXY(10, 29);
		$pdf->MultiCell(273, 5, _convert('SKLADIŠTE ROBA'.$l_cond_title), 'B', "L");
		
		$l_pos = 0;
		for($i = 0; $i < sizeof($la_header); $i++){
			if($i > 0){
				$l_pos += $la_width[$i - 1];
			}
			$pdf->SetXY(10 + $l_pos, 37);
			$pdf->MultiCell($la_width[$i], 5, _convert($la_header[$i]), 0, "L");
		}
		
		
		$pdf->SetFont("ArialCE", "", 9);
		
		
		$la_tmp = explode('-', $date);
		$l_date_str = $la_tmp[2].'-'.$la_tmp[1].'-'.$la_tmp[0];
		
		$l_tip_prog = get_item('select value from settings where name = "tipprog" ');
		
		$l_ukNC = 0;
		$l_ukPC = 0;
		
		
		$l_cond = '';
		
		
		
		$x = get_data('select id, name, unit, amount, price_buy, price_sell, barcode from articles where articles.isService = 0 '.$l_cond.' order by name, id ', 1);
		$l_y = 7;
		for($i = 0; $i < sizeof($x['id']); $i++){
			
			$la_data = array();
			$la_data[0] = $i + 1;
			
			if($l_tip_prog == 'm'){
				$l_pomak = 1;
				$la_data[0 + $l_pomak] = $x['barcode'][$i];
			}
			if($l_tip_prog == 'u'){
				$l_pomak = 0;
			}
			
			$la_data[1 + $l_pomak] = strlen($x['name'][$i]) > 42 ? substr($x['name'][$i], 0, 42) : $x['name'][$i];
			$la_data[2 + $l_pomak] = strlen($x['unit'][$i]) > 5 ? substr($x['unit'][$i], 0, 5) : $x['unit'][$i];
			$la_data[3 + $l_pomak] = sprintf('%01.3f', $x['amount'][$i]);
			$la_data[4 + $l_pomak] = $x['price_buy'][$i];
			$la_data[5 + $l_pomak] = $x['price_sell'][$i];
			$la_data[6 + $l_pomak] = sprintf('%01.2f', $x['amount'][$i] * $x['price_buy'][$i]);
			if($x['amount'][$i] > 0){
				$l_ukNC += $x['amount'][$i] * $x['price_buy'][$i];
			}
			$la_data[7 + $l_pomak] = sprintf('%01.2f', $x['amount'][$i] * $x['price_sell'][$i]);
			if($x['amount'][$i] > 0){
				$l_ukPC += $x['amount'][$i] * $x['price_sell'][$i];
			}
			
			
			
			
			$l_pos = 0;
			for($j = 0; $j < sizeof($la_data); $j++){
				if($j > 0){
					$l_pos += $la_width[$j - 1];
				}
				$pdf->SetXY(10 + $l_pos, 36 + $l_y);
				$pdf->MultiCell($la_width[$j], 5, _convert($la_data[$j]), 0, "L");
			}
			$l_y += 5;
			
			
			if($l_y >= 150){
				$pdf->AddPage();
				$l_y = -25;
			}
		}
		
		
		
		if($l_tip_prog == 'm'){
			$la_header_bottom = array('', '', '', '', '', 'Ukupno EUR', '', sprintf('%01.2f', $l_ukNC), sprintf('%01.2f', $l_ukPC));
			$la_width_bottom = array(10, 26, 95, 12, 17, 25, 13, 25, 19);
		}
		if($l_tip_prog == 'u'){
			$la_header_bottom = array('', '', '', '', 'Ukupno EUR', '', sprintf('%01.2f', $l_ukNC), sprintf('%01.2f', $l_ukPC));
			$la_width_bottom = array(10, 105, 12, 22, 25, 15, 25, 19);
		}
		
		$pdf->SetFont("ArialCE", "B", 9);
		$l_pos = 0;
		for($i = 0; $i < sizeof($la_header_bottom); $i++){
			if($i > 0){
				$l_pos += $la_width_bottom[$i - 1];
			}
			$pdf->SetXY(10 + $l_pos, 36 + $l_y + 1);
			$pdf->MultiCell($la_width_bottom[$i], 5, _convert($la_header_bottom[$i]), 'T', "L");
		}
		
		
		
		
		
		$l_y += 10;
		
		
		if($l_y >= 150){
			$pdf->AddPage();
			$l_y = -25;
		}
		
			
		
		$pdf->SetFont("ArialCE", "B", 9);
		
		$pdf->SetXY(10, 36 + $l_y + 3);
		$pdf->MultiCell(273, 15, _convert('Napomena: Ukupna vrijednost je samo za artikle sa stanjem > 0'), '', "L");
		
		
		
		$l_name = 'stanje_skladista_iz_artikala.pdf';
		$l_dest = $g_path.'app/webroot/res/files/'.$l_name;
		
		$pdf->Output($l_dest, 'F');
		
		return array('path' => $g_path.'app/webroot/res/files/', 'name' => $l_name);
	}
	
	
	// Zaglavlje F2 tablice (e-računi) - prodani_artikli (landscape)
	function pa_f2_kolone(&$pdf, $g_v){
		$pdf->SetFont("ArialCE", "B", 8);
		$pdf->SetXY(7, 45);   $pdf->MultiCell(24, 5, _convert('Šifra'), 'B', "R");
		$pdf->SetXY(32, 45);  $pdf->MultiCell(73, 5, _convert('Naziv'), 'B', "L");
		$pdf->SetXY(106, 45); $pdf->MultiCell(20, 5, _convert('Kol'), 'B', "R");
		$pdf->SetXY(130, 45); $pdf->MultiCell(30, 5, _convert('Neto '.$g_v), 'B', "R");
		$pdf->SetXY(162, 45); $pdf->MultiCell(18, 5, _convert('PDV %'), 'B', "R");
		$pdf->SetXY(182, 45); $pdf->MultiCell(35, 5, _convert('Bruto '.$g_v), 'B', "R");
		$pdf->SetFont("ArialCE", "", 8);
	}
	
	// Zaglavlje zbirne tablice F1 + F2 - prodani_artikli (landscape)
	function pa_zbir_kolone(&$pdf, $g_v){
		$pdf->SetFont("ArialCE", "B", 8);
		$pdf->SetXY(7, 45);   $pdf->MultiCell(24, 5, _convert('Šifra'), 'B', "R");
		$pdf->SetXY(32, 45);  $pdf->MultiCell(58, 5, _convert('Naziv'), 'B', "L");
		$pdf->SetXY(90, 45);  $pdf->MultiCell(24, 5, _convert('F1 Kol'), 'B', "R");
		$pdf->SetXY(114, 45); $pdf->MultiCell(24, 5, _convert('F2 Kol'), 'B', "R");
		$pdf->SetXY(138, 45); $pdf->MultiCell(24, 5, _convert('Uk. Kol'), 'B', "R");
		$pdf->SetXY(164, 45); $pdf->MultiCell(30, 5, _convert('F1 Iznos '.$g_v), 'B', "R");
		$pdf->SetXY(198, 45); $pdf->MultiCell(30, 5, _convert('F2 Iznos '.$g_v), 'B', "R");
		$pdf->SetXY(232, 45); $pdf->MultiCell(32, 5, _convert('Uk. Iznos '.$g_v), 'B', "R");
		$pdf->SetFont("ArialCE", "", 8);
	}
	
	
	function all_prodani_artikli($date_start, $date_end, $vrsta_izvjesca, $grupa_izvjesca, $dobavljac_id){
		
		$g_path = get_item('select value from settings where name = "putanja.abs" ');
		
		$g_v = get_valuta();
		
		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');
		
		$pdf =& new FPDI('L', 'mm', 'A4');
		
		$pdf->AddFont("ArialCE", "B", "6488f5d4657b269b9f5770508e80e299_arialbd.php");
		$pdf->AddFont("ArialCE", "", "332cc83ebee36613c19564e6e776c81b_arial.php");
		
		// set the sourcefile
		$pgNumber = $pdf->setSourceFile($g_path.'app/libs/fpdf/prazni-dokument.pdf');
		
		
		//$pdf->SetAutoPageBreak(true, 10);
		
		
		$l_pdv = get_item('select value from settings where name = "duty.free" ');
		
		if($l_pdv == 'no'){
			$l_pdv = 'false';
		}
		
		if($l_pdv == 'false'){
			$l_pdv_koment = get_item('select value from settings where name = "duty.comment" ');
		}else{
			$l_pdv_koment = '';
		}
		
		$x = get_item('select value from settings where name = "general.name" ').', '.
			get_n_rows(get_item('select value from settings where name = "invoice.header" '), 4);
		
		$la_tmp = explode('-', $date_start);
		$la_tmp2 = explode('-', $date_end);
		
		$l_datum = $la_tmp[0].'.'.$la_tmp[1].'.'.$la_tmp[2].'.';
		$l_datum2 = $la_tmp2[0].'.'.$la_tmp2[1].'.'.$la_tmp2[2].'.';
		
		$l_datum_txt = $la_tmp[0].$la_tmp[1].$la_tmp[2];
		$l_datum_txt2 = $la_tmp2[0].$la_tmp2[1].$la_tmp2[2];
		
		$l_datum_db = $la_tmp[2].'-'.$la_tmp[1].'-'.$la_tmp[0];
		$l_datum_db2 = $la_tmp2[2].'-'.$la_tmp2[1].'-'.$la_tmp2[0];
		$l_datum_from = $l_datum_db.' 00:00:00';
		$l_datum_to = $l_datum_db2.' 23:59:59';
		
		
		$l_xls = 'rbr;barcode;naziv;kolicina;tbr(iznos PDV);nc;pc;popust(%);pc1;Pov.naknada;uk pc1'."\n";
		
		if($l_datum_txt == $l_datum_txt2){
			$l_txt_ok = 1;
			$l_txt = $l_datum_txt.'PROMET'."\n";
		}else{
			$l_txt_ok = 0;
			$l_txt = 'Neispravno upisani datumi - dozvoljeno je odabrati izradu izvjesca samo za jedan datum'."\n";
		}
		
		//for($i = 1; $i <= $pgNumber; $i++){
		for($i = 1; $i <= 1; $i++){
			// add a page
			$pdf->AddPage();
			
			// import page 1
			$tplIdx = $pdf->importPage($i);
			
			// use the imported page and place it at point 0,0 with a width of 210 mm
			$pdf->useTemplate($tplIdx, 0, 0, 297, 210);
			
			$pdf->SetFont("ArialCE", "B", 11);
			$pdf->SetTextColor(0, 0, 0);
			
			$pdf->SetXY(10, 10);
			$pdf->MultiCell(210, 5, _convert($x), 0, "L");
			
			
			switch($vrsta_izvjesca){
				case 1:
					$l_add_title = ' - Svi artikli';
					$l_add_service_sql = '';
					break;
					
				case 2:
					$l_add_title = ' - samo roba';
					$l_add_service_sql = ' and articles.isService = 0 ';
					break;
					
				case 3:
					$l_add_title = ' - samo usluge';
					$l_add_service_sql = ' and articles.isService = 1 ';
					break;
				
				default:
					$l_add_title = '';
					$l_add_service_sql = '';
			}
			
			if($grupa_izvjesca > 0){
				$l_add_title_grupa = ' - grupa: '.get_item('select name from article_groups where id = '.intval($grupa_izvjesca));
			}else{
				$l_add_title_grupa = ' - Sve grupe';
			}
			
			if($dobavljac_id != "null"){
				if($dobavljac_id == -1){
					$l_add_title_dobavljac = ' - Artikli bez dobavljača';
				}else{
					$l_add_title_dobavljac = ' - dobavljač: '.get_item('select name from suppliers where id = '.intval($dobavljac_id));
				}
			}else{
				$l_add_title_dobavljac = ' - Svi dobavljači';
			}
			
			
			$pdf->SetXY(10, 30);
			$pdf->MultiCell(210, 5, _convert('Pregled prodaje po artiklu'.$l_add_title.$l_add_title_grupa.$l_add_title_dobavljac), 0, "L");
			
			$pdf->SetXY(10, 35);
			$pdf->MultiCell(210, 5, _convert('Period od '.$l_datum.' do '.$l_datum2), 0, "L");
			
			$pdf->SetFont("ArialCE", "B", 8);
			
			
			
			$pdf->SetXY(10, 45);
			$pdf->MultiCell(22, 5, _convert('Šifra'), 'B', "R");
			
			$pdf->SetXY(32, 45);
			$pdf->MultiCell(74, 5, _convert('Naziv'), 'B', "L");
			
			$pdf->SetXY(106, 45);
			$pdf->MultiCell(9, 5, _convert('Kol'), 'B', "C");
			
			$pdf->SetXY(115, 45);
			$pdf->MultiCell(19, 5, _convert('NC '.$g_v), 'B', "R");
			
			$pdf->SetXY(134, 45);
			$pdf->MultiCell(17, 5, _convert('Marža '.$g_v), 'B', "R");
			
			$pdf->SetXY(151, 45);
			$pdf->MultiCell(23, 5, _convert('Marža UK '.$g_v), 'B', "R");
			
			$pdf->SetXY(174, 45);
			$pdf->MultiCell(15, 5, _convert('Marža %'), 'B', "R");
			
			$pdf->SetXY(189, 45);
			$pdf->MultiCell(14, 5, _convert('PC'), 'B', "R");
			
			$pdf->SetXY(203, 45);
			$pdf->MultiCell(17, 5, _convert('PC Popust'), 'B', "R");
			
			$pdf->SetXY(220, 45);
			$pdf->MultiCell(20, 5, _convert('Pov.naknada'), 'B', "C");
			
			$pdf->SetXY(240, 45);
			$pdf->MultiCell(15, 5, _convert('Ukupno'), 'B', "R");
			
			$pdf->SetFont("ArialCE", "", 8);
			
			//$y = get_data('select articles.barcode, articles.name, articles.price_buy, retail_items.base_price as cijena, retail_items.tax_1, '.
			
			
			//p($grupa_izvjesca);
			
			
			$l_gruop_attr = '';
			$l_gruop_attr_end = '';
			$l_group_join = '';
			
			if($grupa_izvjesca > 0){
				$l_add_grupa_sql = ' and articles.article_group_id = '.intval($grupa_izvjesca);
			}else{
				if($grupa_izvjesca == -1){
					$l_add_grupa_sql = '';
					
					$l_gruop_attr = ' article_groups.name as group_name, ';
					$l_gruop_attr_end = ' article_groups.name, ';
					$l_group_join = '  left outer join article_groups on articles.article_group_id = article_groups.id ';
					
					
				}else{
					$l_add_grupa_sql = '';
				}
			}
			
			
			// dobavljac_id
			$l_dobavljac_join = '';
			
			if($dobavljac_id != "null"){
				$l_add_dobavljac_sql =
					' and receiving_items.article_id = articles.id '.
					' and receiving_items.receiving_id = receivings.id '.
					' and receivings.type = 0 ';
				if($dobavljac_id == -1){
					$l_add_dobavljac_sql .= ' and receivings.supplier_id is null ';
				}else{
					$l_add_dobavljac_sql .= ' and receivings.supplier_id = '.intval($dobavljac_id);
				}
				
				$l_dobavljac_join = '  , receiving_items, receivings ';
				
			}else{
				$l_add_dobavljac_sql = '';
			}
			
			
			
			$y = get_data('select '.$l_gruop_attr.' articles.barcode, articles.name, articles.unit, articles.isService, retail_items.price_buy, retail_items.base_price as cijena, '.
				' retail_items.tax_1, discount_percent, retail_items.feedback_compensation, '.
				' sum(retail_items.amount * retail_items.base_price * (1 - discount_percent / 100)) as ukup, sum(retail_items.amount) as kol, '.
				' sum(retail_items.price) as cijena_ukup, '.
				' sum(retail_items.feedback_compensation) as fc_ukup, '.
				' sum(retail_items.price - retail_items.base_price * retail_items.amount) as popust_ukup '.
				' from ((sales, retail_items, articles'.$l_dobavljac_join.') '.$l_group_join.' ) '.
				' where sales.id = retail_items.sale_id '.
				' and retail_items.article_id = articles.id '.
				' and sales.created between "'.$l_datum_from.'" and "'.$l_datum_to.'"'.
				$l_add_service_sql.
				$l_add_grupa_sql.
				$l_add_dobavljac_sql.
				' group by '.$l_gruop_attr_end.' articles.barcode, articles.name, articles.unit, articles.isService, retail_items.price_buy, retail_items.base_price, retail_items.tax_1, discount_percent, retail_items.feedback_compensation '.
				' order by '.$l_gruop_attr_end.' articles.barcode, articles.name, articles.unit, articles.isService, retail_items.price_buy '.
				'', 1);
			
			
			$l_ukup_nc = 0;
			$l_ukup_pc = 0;
			$l_ukup_pc_neto = 0;
			$l_ukup_pc_popust = 0;
			$l_ukup_pc_popust_robe = 0;
			$l_ukup_pc_popust_usluge = 0;
			$l_ukup_marze = 0;
			$l_ukup_fc = 0;
			$l_cnt = 0;
			//$l_cnt++;
			$l_cnt++;
			$l_cnt_2 = 0;
			
			
			
			
			$l_old_group_name = '---123--old_name';
			
			
			for($j = 0; isset($y['name']) && $j < sizeof($y['name']); $j++){
				
				//print $l_cnt." - ";
				//print $l_cnt_2." -- <br>";
				//print (50 + 4 * ($l_cnt + 1) + $l_cnt_2)." ---<br>";
				
				if(50 + 3 * $l_cnt + $l_cnt_2 > 186){
					$pdf->AddPage();
					$l_cnt = -13;
					$l_cnt_2 = 0;
					//print "+++++<br>";
					//print (50 + 4 * ($l_cnt + 1) + $l_cnt_2)."<br>";
				}
				
				
				
				if($grupa_izvjesca == -1){
					if($l_old_group_name != $y['group_name'][$j]){
						$l_old_group_name = $y['group_name'][$j];
						
						if($l_old_group_name == ''){
							$l_old_group_name_print = 'Nije svrsteno u niti jednu grupu';
						}else{
							$l_old_group_name_print = $y['group_name'][$j];
						}
						
						$pdf->SetFont("ArialCE", "B", 8);
						$pdf->SetXY(10, 52 + 3 * $l_cnt + $l_cnt_2);
						$pdf->MultiCell(245, 5, _convert('GRUPA ARTIKLA: '.$l_old_group_name_print), "B", "L");
						$pdf->SetFont("ArialCE", "", 8);
						
						$l_cnt++;
						$l_cnt++;
						$l_cnt++;
					}
				}
				
				
				$pdf->SetXY(7, 50 + 3 * $l_cnt + $l_cnt_2);
				$pdf->MultiCell(24, 4, _convert($y['barcode'][$j]), 0, "R");
				
				$pdf->SetXY(32, 50 + 3 * $l_cnt + $l_cnt_2);
				$pdf->MultiCell(73, 4, _convert($y['name'][$j]), 0, "L");
				
				$pdf->SetXY(106, 50 + 3 * $l_cnt + $l_cnt_2);
				$pdf->MultiCell(12, 4, _convert($y['kol'][$j]), 0, "R");
				
				$pdf->SetXY(116, 50 + 3 * $l_cnt + $l_cnt_2);
				$pdf->MultiCell(17, 4, _convert($y['price_buy'][$j]), 0, "R");
				
				if($l_pdv == 'false'){
					$l_marza = $y['cijena'][$j] * (1 - $y['discount_percent'][$j] / 100) - $y['price_buy'][$j];
				}else{
					$l_marza = $y['cijena'][$j] * (1 - $y['discount_percent'][$j] / 100) / (1 + $y['tax_1'][$j] / 100) - $y['price_buy'][$j];
				}
				
				$pdf->SetXY(135, 50 + 3 * $l_cnt + $l_cnt_2);
				$pdf->MultiCell(17, 4, _convert(sprintf('%01.2f', $l_marza)), 0, "R");
				
				$pdf->SetXY(152, 50 + 3 * $l_cnt + $l_cnt_2);
				$pdf->MultiCell(19, 4, _convert(sprintf('%01.2f', $y['kol'][$j] * $l_marza)), 0, "R");
				
				$pdf->SetXY(171, 50 + 3 * $l_cnt + $l_cnt_2);
				if(empty($y['price_buy'][$j]) || $y['price_buy'][$j] == 0){
						$pdf->MultiCell(16, 4, _convert('-    '), 0, "R");
				}else{
					if($l_pdv == 'false'){
						$pdf->MultiCell(16, 4, _convert(sprintf('%01.2f', ($y['cijena'][$j] * (1 - $y['discount_percent'][$j] / 100) / $y['price_buy'][$j] - 1) * 100)), 0, "R");
					}else{
						$pdf->MultiCell(16, 4, _convert(sprintf('%01.2f', (($y['cijena'][$j] * (1 - $y['discount_percent'][$j] / 100) / (1 + $y['tax_1'][$j] / 100) / $y['price_buy'][$j]) - 1) * 100)), 0, "R");
					}
				}
				
				$pdf->SetXY(186, 50 + 3 * $l_cnt + $l_cnt_2);
				$pdf->MultiCell(15, 4, _convert(sprintf('%01.2f', $y['kol'][$j] == 0 ? 0 : ($y['cijena'][$j] + $y['fc_ukup'][$j] / $y['kol'][$j]) )), 0, "R");
				
				$pdf->SetXY(202, 50 + 3 * $l_cnt + $l_cnt_2);
				$pdf->MultiCell(17, 4, _convert(sprintf('%01.2f', $y['kol'][$j] == 0 ? 0 :  ($y['cijena'][$j] * (1 - $y['discount_percent'][$j] / 100) + $y['fc_ukup'][$j] / $y['kol'][$j]))), 0, "R");
				
				$pdf->SetXY(220, 50 + 3 * $l_cnt + $l_cnt_2);
				$pdf->MultiCell(15, 4, _convert(sprintf('%01.2f', $y['fc_ukup'][$j])), 0, "R");
				
				$pdf->SetXY(240, 50 + 3 * $l_cnt + $l_cnt_2);
				$pdf->MultiCell(15, 4, _convert(sprintf('%01.2f', $y['cijena_ukup'][$j])), 0, "R");

				//$l_cnt_2++;
				
				
				$l_xls .= 
					($j + 1).';'.
					$y['barcode'][$j].';'.
					'"'.$y['name'][$j].'";'.
					str_replace('.', ',', $y['kol'][$j]).';'.
					str_replace('.', ',', $y['tax_1'][$j]).';'.
					_convert(str_replace('.', ',', sprintf('%01.2f', $y['price_buy'][$j]))).';'.
					_convert(str_replace('.', ',', sprintf('%01.2f',  $y['kol'][$j] == 0 ? 0 : ($y['cijena'][$j] + $y['fc_ukup'][$j] / $y['kol'][$j])))).';'.
					str_replace('.', ',', $y['discount_percent'][$j]).';'.
					_convert(str_replace('.', ',', sprintf('%01.2f',  $y['kol'][$j] == 0 ? 0 : ($y['cijena'][$j] * (1 - $y['discount_percent'][$j] / 100) + $y['fc_ukup'][$j] / $y['kol'][$j])))).';'.
					//_convert(str_replace('.', ',', sprintf('%01.2f', $y['kol'][$j] * $y['cijena'][$j] * (1 - $y['discount_percent'][$j] / 100)))).
					//_convert(str_replace('.', ',', sprintf('%01.2f', $y['cijena_ukup'][$j] * (1 - $y['discount_percent'][$j] / 100)))).
					_convert(str_replace('.', ',', sprintf('%01.2f', $y['fc_ukup'][$j]))).';'.
					_convert(str_replace('.', ',', sprintf('%01.2f', $y['cijena_ukup'][$j]))).
					"\n";
					
				if($l_txt_ok == 1){
					
					$l_barcode_empty_num = '';
					$l_y_barcode = $y['barcode'][$j];
					if(strlen($l_y_barcode) > 13){
						$l_y_barcode = substr($l_y_barcode, 0, 13);
					}else{
						for($k = 0; $k < 13 - strlen($l_y_barcode); $k++){
							$l_barcode_empty_num .= ' ';
						}
					}
					
					$l_name_empty_num = '';
					$l_y_name = $y['name'][$j];
					if(strlen($l_y_name) > 25){
						$l_y_name = substr($l_y_name, 0, 25);
					}else{
						for($k = 0; $k < 25 - strlen($l_y_name); $k++){
							$l_name_empty_num .= ' ';
						}
					}
					
					$l_unit_empty_num = '';
					$l_y_unit = $l_y_unit;
					if(strlen($l_y_unit) > 5){
						$l_y_unit = substr($l_y_unit, 0, 5);
					}else{
						for($k = 0; $k < 5 - strlen($l_y_unit); $k++){
							$l_unit_empty_num .= ' ';
						}
					}
					
					$y['isService'][$j] == 1 ?
						$l_y_isService = 3
						:
						$l_y_isService = 1;
						
						
					$l_kol_empty_num = '';
					$l_y_kol = abs($y['kol'][$j]);
					$l_y_kol = sprintf('%01.3f', $l_y_kol);
					$l_y_kol = str_replace('.', '', $l_y_kol);
					for($k = 0; $k < 10 - strlen($l_y_kol); $k++){
						$l_kol_empty_num .= '0';
					}
					if($y['kol'][$j] < 0){
						$l_kol_empty_num[0] = '-';
					}
					
					
					$l_cijena_empty_num = '';
					$l_y_cijena = abs($y['cijena'][$j]);
					$l_y_cijena = sprintf('%01.2f', $l_y_cijena);
					$l_y_cijena = str_replace('.', '', $l_y_cijena);
					for($k = 0; $k < 10 - strlen($l_y_cijena); $k++){
						$l_cijena_empty_num .= '0';
					}
					if($y['cijena'][$j] < 0){
						$l_cijena_empty_num[0] = '-';
					}
					
					
					$l_cijena_ukup_empty_num = '';
					$l_y_cijena_ukup = abs($y['cijena_ukup'][$j]);
					$l_y_cijena_ukup = sprintf('%01.2f', $l_y_cijena_ukup);
					$l_y_cijena_ukup = str_replace('.', '', $l_y_cijena_ukup);
					for($k = 0; $k < 12 - strlen($l_y_cijena_ukup); $k++){
						$l_cijena_ukup_empty_num .= '0';
					}
					if($y['cijena_ukup'][$j] < 0){
						$l_cijena_ukup_empty_num[0] = '-';
					}
					
					
					$l_pdv_empty_num = '';
					$l_y_pdv = abs($y['cijena_ukup'][$j] - $y['cijena_ukup'][$j] / (1 + $y['tax_1'][$j] / 100));
					$l_y_pdv = sprintf('%01.2f', $l_y_pdv);
					$l_y_pdv = str_replace('.', '', $l_y_pdv);
					for($k = 0; $k < 10 - strlen($l_y_pdv); $k++){
						$l_pdv_empty_num .= '0';
					}
					if($y['cijena_ukup'][$j] - $y['cijena_ukup'][$j] / (1 + $y['tax_1'][$j] / 100) < 0){
						$l_pdv_empty_num[0] = '-';
					}
					
					
					$l_popust_empty_num = '';
					$l_y_popust = abs($y['popust_ukup'][$j]);
					$l_y_popust = sprintf('%01.2f', $l_y_popust);
					$l_y_popust = str_replace('.', '', $l_y_popust);
					for($k = 0; $k < 10 - strlen($l_y_popust); $k++){
						$l_popust_empty_num .= '0';
					}
					if($y['popust_ukup'][$j] < 0){
						$l_popust_empty_num[0] = '-';
					}
					
					
					$l_vrsta_pdv = 1;
					
					
					switch(intval($y['tax_1'])){
						case 5:
							$l_tarifni_broj = '00004';
							break;
							
						case 13:
							$l_tarifni_broj = '00003';
							break;
							
						case 25:
							$l_tarifni_broj = '00001';
							break;
						
						default:
							$l_tarifni_broj = '00000';
					}
					
					
					$l_pdv_stopa_empty_num = '';
					$l_y_pdv_stopa = $y['tax_1'][$j];
					$l_y_pdv_stopa = sprintf('%01.2f', $l_y_pdv_stopa);
					$l_y_pdv_stopa = str_replace('.', '', $l_y_pdv_stopa);
					for($k = 0; $k < 4 - strlen($l_y_pdv_stopa); $k++){
						$l_pdv_stopa_empty_num .= '0';
					}
					
					
					//p($l_y_name);
					//p(strlen($l_y_name));
					//p($l_cijena_ukup_empty_num.$l_y_cijena_ukup);
					//p($l_pdv_empty_num.$l_y_pdv);
					//p($y['popust_ukup'][$j]);
					//p($l_popust_empty_num.$l_y_popust);
					
					$l_txt .= 
						$l_y_barcode.$l_barcode_empty_num.
						$l_y_name.$l_name_empty_num.
						$l_y_unit.$l_unit_empty_num.
						$l_y_isService.
						$l_kol_empty_num.$l_y_kol.
						$l_cijena_empty_num.$l_y_cijena.
						$l_cijena_ukup_empty_num.$l_y_cijena_ukup.
						$l_pdv_empty_num.$l_y_pdv.
						$l_popust_empty_num.$l_y_popust.
						$l_vrsta_pdv.
						$l_tarifni_broj.
						$l_pdv_stopa_empty_num.$l_y_pdv_stopa.
						"\r\n";
					//p($l_txt);
					//p('---');
				}
				
				$l_cnt++;
				$l_cnt++;
				
				$l_ukup_nc += $y['kol'][$j] * $y['price_buy'][$j];
				$l_ukup_pc += ($y['cijena_ukup'][$j] - $y['fc_ukup'][$j]) / (1 - $y['discount_percent'][$j] / 100) + $y['fc_ukup'][$j];
				//$l_ukup_pc_neto += (($y['cijena_ukup'][$j] - $y['fc_ukup'][$j]) / (1 - $y['discount_percent'][$j] / 100)) / (1 + $y['tax_1'][$j] / 100);
				$l_ukup_pc_neto += $y['cijena_ukup'][$j] / (1 + $y['tax_1'][$j] / 100);
				$l_ukup_pc_popust += $y['cijena_ukup'][$j];
				if($y['isService'][$j] == 0){
					$l_ukup_pc_popust_robe += $y['cijena_ukup'][$j];
				}
				if($y['isService'][$j] == 1){
					$l_ukup_pc_popust_usluge += $y['cijena_ukup'][$j];
				}
				
				$l_ukup_fc += $y['fc_ukup'][$j];
			}
			
				
			$pdf->SetXY(10, 50 + 3 * $l_cnt + $l_cnt_2);
			$pdf->MultiCell(210, 4, _convert('===================================='), 0, "L");
			
			//$l_cnt++;
			
			$pdf->SetFont("ArialCE", "B", 9);
			
			//print $l_cnt." - ";
			//print $l_cnt_2." -- <br>";
			//print (50 + 4 * ($l_cnt + 1) + $l_cnt_2)." ---<br>";
			
			if(50 + 3 * ($l_cnt + 1) + $l_cnt_2 > 150){
				$pdf->AddPage();
				$l_cnt = -13;
				$l_cnt_2 = 0;
				//print "------<br>";
				//print (50 + 4 * ($l_cnt + 1) + $l_cnt_2)."<br>";
			}
			
			$l_cnt++;
			
			
			$pdf->SetFont("ArialCE", "B", 8);
			
			$l_cnt_2++;
			$pdf->SetXY(10, 50 + 3 * (++$l_cnt) + $l_cnt_2);
			$pdf->MultiCell(210, 4, _convert('Ukupno po NC: '.sprintf('%01.2f', $l_ukup_nc).' '.$g_v), 0, "L");
			
			$l_cnt_2++;
			$pdf->SetXY(10, 50 + 3 * (++$l_cnt) + $l_cnt_2);
			$pdf->MultiCell(210, 4, _convert('Ukupno po PC: '.sprintf('%01.2f', $l_ukup_pc).' '.$g_v), 0, "L");
			
			$l_cnt_2++;
			$pdf->SetXY(10, 50 + 3 * (++$l_cnt) + $l_cnt_2);
			$pdf->MultiCell(210, 4, _convert('--------------------------------------------'), 0, "L");
			
			$l_cnt_2++;
			$pdf->SetXY(10, 50 + 3 * (++$l_cnt) + $l_cnt_2);
			//$pdf->MultiCell(210, 4, _convert('Ukupno po PC s popustima: '.sprintf('%01.2f', $l_ukup_pc_popust).' kn'), 0, "L");
			$pdf->MultiCell(210, 4, _convert('Popust: '.sprintf('%01.2f', ($l_ukup_pc - $l_ukup_pc_popust)).' '.$g_v), 0, "L");
			
			$l_cnt_2++;
			$pdf->SetXY(10, 50 + 3 * (++$l_cnt) + $l_cnt_2);
			$pdf->MultiCell(210, 4, _convert('--------------------------------------------'), 0, "L");
			
			$l_cnt_2++;
			$pdf->SetXY(10, 50 + 3 * (++$l_cnt) + $l_cnt_2);
			//$pdf->MultiCell(210, 4, _convert('Ukupno Marže: '.sprintf('%01.2f', $l_ukup_marze).' kn'), 0, "L");
			$pdf->MultiCell(210, 4, _convert('Ukupno prodano: '.sprintf('%01.2f', $l_ukup_pc_popust).' '.$g_v), 0, "L");
			
			
			switch($vrsta_izvjesca){
				case 2:
				case 3:
					break;
					
				default:
					$l_cnt_2++;
					$pdf->SetXY(10, 50 + 3 * (++$l_cnt) + $l_cnt_2);
					$pdf->MultiCell(210, 4, _convert('Od toga       roba: '.sprintf('%01.2f', $l_ukup_pc_popust_robe).' '.$g_v), 0, "L");
					$l_cnt_2++;
					$pdf->SetXY(10, 50 + 3 * (++$l_cnt) + $l_cnt_2);
					$pdf->MultiCell(210, 4, _convert('                 usluga: '.sprintf('%01.2f', $l_ukup_pc_popust_usluge).' '.$g_v), 0, "L");
					break;
			}
			
			
			$l_cnt_2++;
			$pdf->SetXY(10, 50 + 3 * (++$l_cnt) + $l_cnt_2);
			$pdf->MultiCell(210, 4, _convert('--------------------------------------------'), 0, "L");
			
			$l_cnt_2++;
			$pdf->SetXY(10, 50 + 3 * (++$l_cnt) + $l_cnt_2);
			$pdf->MultiCell(210, 4, _convert('Ukupno marža: '.sprintf('%01.2f', $l_ukup_pc_neto - $l_ukup_nc).' '.$g_v), 0, "L");
			
			$l_cnt_2++;
			$pdf->SetXY(10, 50 + 3 * (++$l_cnt) + $l_cnt_2);
			$pdf->MultiCell(210, 4, _convert('--------------------------------------------'), 0, "L");
			
			$l_cnt_2++;
			$pdf->SetXY(10, 50 + 3 * (++$l_cnt) + $l_cnt_2);
			$pdf->MultiCell(210, 4, _convert('Uključena povratna naknada: '.sprintf('%01.2f', $l_ukup_fc).' '.$g_v), 0, "L");
			
			
			$l_xls .= 'Ukupno po NC:;'._convert(str_replace('.', ',', sprintf('%01.2f', ($l_ukup_nc))))."\n";
			$l_xls .= 'Ukupno po PC:;'._convert(str_replace('.', ',', sprintf('%01.2f', ($l_ukup_pc))))."\n";
			$l_xls .= 'Popust:;'._convert(str_replace('.', ',', sprintf('%01.2f', ($l_ukup_pc - $l_ukup_pc_popust))))."\n";
			$l_xls .= 'Ukupno prodano:;'._convert(str_replace('.', ',', sprintf('%01.2f', ($l_ukup_pc_popust))))."\n";
			
			switch($vrsta_izvjesca){
				case 2:
				case 3:
					break;
					
				default:
					$l_xls .= 'Od toga     robe:;'._convert(str_replace('.', ',', sprintf('%01.2f', ($l_ukup_pc_popust_robe))))."\n";
					$l_xls .= '                usluge:;'._convert(str_replace('.', ',', sprintf('%01.2f', ($l_ukup_pc_popust_usluge))))."\n";
					break;
			}
			
			$l_xls .= 'Ukupno marza:;'._convert(str_replace('.', ',', sprintf('%01.2f', ($l_ukup_pc_neto - $l_ukup_nc))))."\n";
			$l_xls .= 'Uključena povratna naknada:;'._convert(str_replace('.', ',', sprintf('%01.2f', $l_ukup_fc)));
		}
		
		// ================================================================
		// F2 - E-RAČUNI (retail: stavke vezane na articles preko article_id)
		// + zbirni prikaz F1 + F2 po artiklu (kolicine i iznosi)
		// ================================================================
		$l_es_tbl = get_item('SHOW TABLES LIKE "eracun_stavke" ');
		$l_es_col = '';
		if($l_es_tbl != ''){
			$l_es_col = get_item('SHOW COLUMNS FROM eracun_stavke LIKE "article_id" ');
		}
		if($l_es_tbl != '' && $l_es_col != ''){
			
			$l_es_status_in = ' e.external_status in ('.
				'"FISCALIZATION:OK", '.
				'"FISCALIZATION_PAYMENT_REPORT:OK", '.
				'"FISCALIZATION_PAYMENT_REPORT:ERROR", '.
				'"FISCALIZATION_REJECTION_REPORT:OK", '.
				'"FISCALIZATION_REJECTION_REPORT:ERROR", '.
				'"FISCALIZATION_NOT_DELIVERED_REPORT:OK") ';
			
			$y2 = get_data('select articles.barcode, articles.name, articles.isService, '.
				' sum(eracun_stavke.quantity) as kol, '.
				' sum(eracun_stavke.line_extension_amount) as neto, '.
				' sum(eracun_stavke.line_extension_amount * (100 + coalesce(eracun_stavke.vat_percentage, 0)) / 100) as bruto, '.
				' eracun_stavke.vat_percentage as pdv '.
				' from eracun_stavke, articles, eracun e '.$l_dobavljac_join.
				' where eracun_stavke.article_id = articles.id '.
				' and eracun_stavke.eracun_id = e.id '.
				' and eracun_stavke.article_id is not null '.
				' and (e.tip_poruke is null or e.tip_poruke != "Kopija") '.
				' and date(e.sent_at) >= "'.$l_datum_db.'" and date(e.sent_at) <= "'.$l_datum_db2.'" '.
				' and '.$l_es_status_in.
				$l_add_service_sql.
				$l_add_grupa_sql.
				$l_add_dobavljac_sql.
				' group by articles.barcode, articles.name, articles.isService, eracun_stavke.vat_percentage '.
				' order by articles.name, articles.barcode', 1);
			
			// ---------- F2 stranica ----------
			$pdf->AddPage();
			$tplIdx = $pdf->importPage(1);
			$pdf->useTemplate($tplIdx, 0, 0, 297, 210);
			
			$pdf->SetFont("ArialCE", "B", 11);
			$pdf->SetTextColor(0, 0, 0);
			$pdf->SetXY(10, 10);
			$pdf->MultiCell(250, 5, _convert($x), 0, "L");
			
			$pdf->SetXY(10, 30);
			$pdf->MultiCell(280, 5, _convert('Pregled prodaje po artiklu - E-RAČUNI (F2)'.$l_add_title.$l_add_title_grupa.$l_add_title_dobavljac), 0, "L");
			$pdf->SetXY(10, 35);
			$pdf->MultiCell(210, 5, _convert('Period od '.$l_datum.' do '.$l_datum2), 0, "L");
			
			pa_f2_kolone($pdf, $g_v);
			$pdf->SetFont("ArialCE", "", 8);
			
			$l_ly = 50;
			$l_f2_kol = 0;
			$l_f2_neto = 0;
			$l_f2_bruto = 0;
			for($j = 0; isset($y2['name']) && $j < sizeof($y2['name']); $j++){
				if($l_ly > 195){
					$pdf->AddPage();
					$tplIdx = $pdf->importPage(1);
					$pdf->useTemplate($tplIdx, 0, 0, 297, 210);
					pa_f2_kolone($pdf, $g_v);
					$pdf->SetFont("ArialCE", "", 8);
					$l_ly = 50;
				}
				$pdf->SetXY(7, $l_ly);   $pdf->MultiCell(24, 4, _convert($y2['barcode'][$j]), 0, "R");
				$pdf->SetXY(32, $l_ly);  $pdf->MultiCell(73, 4, _convert($y2['name'][$j]), 0, "L");
				$pdf->SetXY(106, $l_ly); $pdf->MultiCell(20, 4, _convert(sprintf('%01.3f', $y2['kol'][$j])), 0, "R");
				$pdf->SetXY(130, $l_ly); $pdf->MultiCell(30, 4, _convert(sprintf('%01.2f', $y2['neto'][$j])), 0, "R");
				$pdf->SetXY(162, $l_ly); $pdf->MultiCell(18, 4, _convert(sprintf('%01.2f', $y2['pdv'][$j])), 0, "R");
				$pdf->SetXY(182, $l_ly); $pdf->MultiCell(35, 4, _convert(sprintf('%01.2f', $y2['bruto'][$j])), 0, "R");
				$l_f2_kol += $y2['kol'][$j];
				$l_f2_neto += $y2['neto'][$j];
				$l_f2_bruto += $y2['bruto'][$j];
				$l_ly += 4;
			}
			
			$l_ly += 3;
			$pdf->SetFont("ArialCE", "B", 8);
			$pdf->SetXY(32, $l_ly);  $pdf->MultiCell(73, 4, _convert('UKUPNO F2'), 'T', "L");
			$pdf->SetXY(106, $l_ly); $pdf->MultiCell(20, 4, _convert(sprintf('%01.3f', $l_f2_kol)), 'T', "R");
			$pdf->SetXY(130, $l_ly); $pdf->MultiCell(30, 4, _convert(sprintf('%01.2f', $l_f2_neto)), 'T', "R");
			$pdf->SetXY(182, $l_ly); $pdf->MultiCell(35, 4, _convert(sprintf('%01.2f', $l_f2_bruto)), 'T', "R");
			
			// ---------- ZBIRNO F1 + F2 ----------
			$la_zbir = array();
			for($j = 0; isset($y['name']) && $j < sizeof($y['name']); $j++){
				$l_key = $y['barcode'][$j].'|'.$y['name'][$j];
				if(!isset($la_zbir[$l_key])){
					$la_zbir[$l_key] = array('barcode' => $y['barcode'][$j], 'name' => $y['name'][$j], 'f1k' => 0, 'f2k' => 0, 'f1i' => 0, 'f2i' => 0);
				}
				$la_zbir[$l_key]['f1k'] += $y['kol'][$j];
				$la_zbir[$l_key]['f1i'] += $y['cijena_ukup'][$j];
			}
			for($j = 0; isset($y2['name']) && $j < sizeof($y2['name']); $j++){
				$l_key = $y2['barcode'][$j].'|'.$y2['name'][$j];
				if(!isset($la_zbir[$l_key])){
					$la_zbir[$l_key] = array('barcode' => $y2['barcode'][$j], 'name' => $y2['name'][$j], 'f1k' => 0, 'f2k' => 0, 'f1i' => 0, 'f2i' => 0);
				}
				$la_zbir[$l_key]['f2k'] += $y2['kol'][$j];
				$la_zbir[$l_key]['f2i'] += $y2['bruto'][$j];
			}
			ksort($la_zbir);
			
			$pdf->AddPage();
			$tplIdx = $pdf->importPage(1);
			$pdf->useTemplate($tplIdx, 0, 0, 297, 210);
			
			$pdf->SetFont("ArialCE", "B", 11);
			$pdf->SetXY(10, 10);
			$pdf->MultiCell(250, 5, _convert($x), 0, "L");
			
			$pdf->SetXY(10, 30);
			$pdf->MultiCell(280, 5, _convert('Zbirno F1 + F2 (e-računi) po artiklu'.$l_add_title.$l_add_title_grupa.$l_add_title_dobavljac), 0, "L");
			$pdf->SetXY(10, 35);
			$pdf->MultiCell(210, 5, _convert('Period od '.$l_datum.' do '.$l_datum2), 0, "L");
			
			pa_zbir_kolone($pdf, $g_v);
			$pdf->SetFont("ArialCE", "", 8);
			
			$l_ly = 50;
			$l_t_f1k = 0; $l_t_f2k = 0; $l_t_f1i = 0; $l_t_f2i = 0;
			foreach($la_zbir as $l_r){
				if($l_ly > 195){
					$pdf->AddPage();
					$tplIdx = $pdf->importPage(1);
					$pdf->useTemplate($tplIdx, 0, 0, 297, 210);
					pa_zbir_kolone($pdf, $g_v);
					$pdf->SetFont("ArialCE", "", 8);
					$l_ly = 50;
				}
				$l_ukk = $l_r['f1k'] + $l_r['f2k'];
				$l_uki = $l_r['f1i'] + $l_r['f2i'];
				$pdf->SetXY(7, $l_ly);   $pdf->MultiCell(24, 4, _convert($l_r['barcode']), 0, "R");
				$pdf->SetXY(32, $l_ly);  $pdf->MultiCell(58, 4, _convert($l_r['name']), 0, "L");
				$pdf->SetXY(90, $l_ly);  $pdf->MultiCell(24, 4, _convert(sprintf('%01.3f', $l_r['f1k'])), 0, "R");
				$pdf->SetXY(114, $l_ly); $pdf->MultiCell(24, 4, _convert(sprintf('%01.3f', $l_r['f2k'])), 0, "R");
				$pdf->SetXY(138, $l_ly); $pdf->MultiCell(24, 4, _convert(sprintf('%01.3f', $l_ukk)), 0, "R");
				$pdf->SetXY(164, $l_ly); $pdf->MultiCell(30, 4, _convert(sprintf('%01.2f', $l_r['f1i'])), 0, "R");
				$pdf->SetXY(198, $l_ly); $pdf->MultiCell(30, 4, _convert(sprintf('%01.2f', $l_r['f2i'])), 0, "R");
				$pdf->SetXY(232, $l_ly); $pdf->MultiCell(32, 4, _convert(sprintf('%01.2f', $l_uki)), 0, "R");
				$l_t_f1k += $l_r['f1k']; $l_t_f2k += $l_r['f2k'];
				$l_t_f1i += $l_r['f1i']; $l_t_f2i += $l_r['f2i'];
				$l_ly += 4;
			}
			
			$l_ly += 3;
			$pdf->SetFont("ArialCE", "B", 8);
			$pdf->SetXY(32, $l_ly);  $pdf->MultiCell(58, 4, _convert('SVEUKUPNO'), 'T', "L");
			$pdf->SetXY(90, $l_ly);  $pdf->MultiCell(24, 4, _convert(sprintf('%01.3f', $l_t_f1k)), 'T', "R");
			$pdf->SetXY(114, $l_ly); $pdf->MultiCell(24, 4, _convert(sprintf('%01.3f', $l_t_f2k)), 'T', "R");
			$pdf->SetXY(138, $l_ly); $pdf->MultiCell(24, 4, _convert(sprintf('%01.3f', $l_t_f1k + $l_t_f2k)), 'T', "R");
			$pdf->SetXY(164, $l_ly); $pdf->MultiCell(30, 4, _convert(sprintf('%01.2f', $l_t_f1i)), 'T', "R");
			$pdf->SetXY(198, $l_ly); $pdf->MultiCell(30, 4, _convert(sprintf('%01.2f', $l_t_f2i)), 'T', "R");
			$pdf->SetXY(232, $l_ly); $pdf->MultiCell(32, 4, _convert(sprintf('%01.2f', $l_t_f1i + $l_t_f2i)), 'T', "R");
		}
		
			
		$l_name = 'prodani_artikli.pdf';
		$l_dest = $g_path.'app/webroot/res/files/'.$l_name;
		
		$pdf->Output($l_dest, 'F');
		
		
		$fp = @fopen($g_path.'app/webroot/res/files/'.str_replace('.pdf', '.csv', $l_name), 'wb');
		fwrite($fp, $l_xls);
		fclose($fp);
		
		
		$fp = @fopen($g_path.'app/webroot/res/files/'.str_replace('.pdf', '.txt', $l_name), 'wb');
		fwrite($fp, $l_txt);
		fclose($fp);
		
		
		return array('path' => $g_path.'app/webroot/res/files/', 'name' => $l_name);
	}
	
	
	function all_kartica_artikla($articles_id, $l_f2 = 0){
		
		$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');
		
		$pdf =& new FPDI('L', 'mm', 'A4');
		
		$pdf->AddFont("ArialCE", "B", "6488f5d4657b269b9f5770508e80e299_arialbd.php");
		$pdf->AddFont("ArialCE", "", "332cc83ebee36613c19564e6e776c81b_arial.php");
		
		// set the sourcefile
		$pgNumber = $pdf->setSourceFile($g_path.'app/libs/fpdf/prazni-dokument.pdf');
		
		
		// add a page
		$pdf->AddPage();
		
		// import page 1
		$tplIdx = $pdf->importPage(1);
		
		// use the imported page and place it at point 0,0 with a width of 210 mm
		$pdf->useTemplate($tplIdx, 0, 0, 297, 210);
		
		
		$pdf->SetTextColor(0, 0, 0);
		
		$x = get_item('select value from settings where name = "general.name" ').', '.
			get_n_rows(get_item('select value from settings where name = "invoice.header" '), 4);
		$pdf->SetFont("ArialCE", "B", 9);
		$pdf->SetXY(10, 10);
		$pdf->MultiCell(110, 4, _convert($x), 0, "L");
		
		$la_artikl = get_data('select name, barcode, price_sell from articles where id = '.$articles_id, 0);
		$l_artikl = $la_artikl['name'];
		$l_barcode = $la_artikl['barcode'];
		$l_pc = $la_artikl['price_sell'];
		
		$pdf->SetFont("ArialCE", "B", 12);
		$pdf->SetXY(10, 13);
		$pdf->MultiCell(290, 5, _convert('Kartica artikla: '.$l_barcode.' '.$l_artikl.' ('.lova($l_pc).' EUR)'), 0, "C");
		
		$pdf->SetFont("ArialCE", "", 8);
		
		
		
		$l_tip_prog = get_item('select value from settings where name = "tipprog" ');
		
		// PORD F2: proknjizene kolicine sa ERACUN skladisnih dokumenata (receivings.type = 6, note sadrzi ERACUN)
		$l_f2_union = '';
		if($l_f2){
			$l_f2_union =
				' union all '.
				' select receivings.document_date as date, "P" as type, receiving_items.amount, articles.id, articles.name '.
				' from receivings, receiving_items, articles '.
				' where receivings.id = receiving_items.receiving_id '.
				' and receiving_items.article_id = articles.id '.
				' and receivings.posted = 1 '.
				' and receivings.type = 6 '.
				' and receivings.note like "%ERACUN%" '.
				' and articles.id = '.$articles_id;
		}
		
		if($l_tip_prog == 'm'){
			$x = get_data('select receivings.document_date as date, receivings.type, receiving_items.amount, articles.id, articles.name '.
				' from receivings, receiving_items, articles '.
				' where receivings.id = receiving_items.receiving_id '.
				' and receiving_items.article_id = articles.id '.
				' and receivings.posted = 1 '.
				' and articles.id = '.$articles_id.
				' union all '.
				' select sales.date, "I" as type, retail_items.amount, articles.id, articles.name '.
				' from sales, retail_items, articles '.
				' where sales.id = retail_items.sale_id '.
				' and retail_items.article_id = articles.id '.
				' and articles.id = '.$articles_id.
				' union all '.
				' select now() as date, "T" as type, amount, articles.id, articles.name '.
				' from articles '.
				' where articles.id = '.$articles_id.
				$l_f2_union.
				' order by date, type', 1);
		}
		
		if($l_tip_prog == 'u'){
			$x = get_data('select receivings.document_date as date, receivings.type, receiving_items.amount, articles.id, articles.name '.
				' from receivings, receiving_items, articles '.
				' where receivings.id = receiving_items.receiving_id '.
				' and receiving_items.article_id = articles.id '.
				' and receivings.posted = 1 '.
				' and articles.id = '.$articles_id.
				' union all '.
				' select sales.date, "I" as type, sale_items.amount * product_items.amount as amount, articles.id, articles.name '.
				' from sales, sale_items, products, product_items, articles '.
				' where sales.id = sale_items.sale_id '.
				' and sale_items.product_id = products.id '.
				' and products.id = product_items.product_id '.
				' and product_items.article_id = articles.id '.
				' and articles.id = '.$articles_id.
				' union all '.
				' select medjus.datum as date, "F" as type, abs(medju_items.kolicina) * product_items.amount as amount, articles.id, articles.name '.
				' from medjus, medju_items, products, product_items, articles '.
				' where medjus.id = medju_items.medju_id '.
				' and medju_items.product_id = products.id '.
				' and products.id = product_items.product_id '.
				' and product_items.article_id = articles.id '.
				' and articles.id = '.$articles_id.
				' and medjus.posted = 1 '.
				' and medjus.povrat = 0 '.
				' union all '.
				' select now() as date, "T" as type, amount, articles.id, articles.name '.
				' from articles '.
				' where articles.id = '.$articles_id.
				$l_f2_union.
				' order by date, type', 1);
		}
		
			
		for($i = 0; isset($x['date']) && $i < sizeof($x['date']); $i++){
			$x['date'][$i] = substr($x['date'][$i], 0, 10);
		}
		
		
		if(isset($x['date'][0])){
			$l_datum = $x['date'][0];
			$l_datum2 = date('d.m.Y', strtotime('now'));
			
			$la_tmp = explode('-', $l_datum);
			$l_datum = $la_tmp[2].'.'.$la_tmp[1].'.'.$la_tmp[0];
			
			$pdf->SetXY(10, 19);
			$pdf->MultiCell(290, 5, _convert('Period od '.$l_datum.'. do '.$l_datum2.'.'), 0, "C");
		}else{
			
			if($articles_id == -1){
				$pdf->SetXY(10, 19);
				$pdf->MultiCell(290, 5, _convert('Period od 01-01-2000 do 01-01-2000'), 0, "C");
			}else{
				$pdf->SetXY(10, 19);
				$pdf->MultiCell(290, 5, _convert('Period od --- do ---'), 0, "C");
			}
		}
		
		if($l_f2){
			$la_header = array('DATUM', 'STANJE', 'NABAVA', 'PROD F2', 'PRODAJA', 'OTPIS', 'POVRAT', 'MEDJU', 'PROIZV', 'INV', 'DONOS');
			$la_width = array(0, 25, 23, 23, 23, 23, 23, 23, 23, 23, 23);
		}else{
			$la_header = array('DATUM', 'STANJE', 'NABAVA', 'PRODAJA', 'OTPIS', 'POVRAT', 'MEDJU', 'PROIZV', 'INV', 'DONOS');
			$la_width = array(0, 25, 23, 23, 23, 23, 23, 23, 23, 23);
		}
		
		$pdf->SetFont("ArialCE", "B", 9);
		$l_pos = 0;
		for($i = 0; $i < sizeof($la_header); $i++){
			$l_pos += $la_width[$i];
			$pdf->SetXY(10 + $l_pos, 30);
			$pdf->MultiCell($la_width[$i], 5, _convert($la_header[$i]), 0, "L");
		}
		
		
		
		
		if($l_f2){
			$la_arr = array(
				'datum' => array(),
				'stanje' => array(),
				'nabava' => array(),
				'pordf2' => array(),
				'prodaja' => array(),
				'otpis' => array(),
				'povrat' => array(),
				'medju' => array(),
				'proizv' => array(),
				'inv' => array(),
				'donos' => array(),
			);
		}else{
			$la_arr = array(
				'datum' => array(),
				'stanje' => array(),
				'nabava' => array(),
				'prodaja' => array(),
				'otpis' => array(),
				'povrat' => array(),
				'medju' => array(),
				'proizv' => array(),
				'inv' => array(),
				'donos' => array(),
			);
		}
		if($l_f2){
			$la_ukup = array(
				'datum' => 'UKUPNO',
				'stanje' => '',
				'nabava' => 0,
				'pordf2' => 0,
				'prodaja' => 0,
				'otpis' => 0,
				'povrat' => 0,
				'medju' => 0,
				'proizv' => 0,
				'inv' => 0,
				'donos' => '',
			);
		}else{
			$la_ukup = array(
				'datum' => 'UKUPNO',
				'stanje' => '',
				'nabava' => 0,
				'prodaja' => 0,
				'otpis' => 0,
				'povrat' => 0,
				'medju' => 0,
				'proizv' => 0,
				'inv' => 0,
				'donos' => '',
			);
		}
		
		for($i = 0; isset($x['date']) && $i < sizeof($x['date']); $i++){
			if(!in_array($x['date'][$i], $la_arr['datum'])){
				
				//print $x['date'][$i]." yyyy ".date('d.m.Y', strtotime($x['date'][$i]))." xxxxxxxx ".date('Y-m-d', strtotime($x['date'][$i]));
				//print "<br>\n";
				//array_push($la_arr['datum'], date('d.m.Y', strtotime($x['date'][$i])));
				
				array_push($la_arr['datum'], date('Y-m-d', strtotime($x['date'][$i])));
				
				$l_pointer = sizeof($la_arr['datum']) - 1;
				
				if($i == 0){
					$la_arr['donos'][0] = 0;
				}else{
					$la_arr['donos'][$l_pointer] = $la_arr['stanje'][$l_pointer - 1];
				}
				
				
				/*
				if($x['type'][$i] == 'T'){
					// today
					$la_arr['stanje'][$l_pointer] = $x['amount'][$i];
				}else{
					// all other
					$la_arr['stanje'][$l_pointer] = $la_arr['donos'][$l_pointer - 1];
				}
				*/
				
				$la_arr['stanje'][$l_pointer] = 0;
				$la_arr['nabava'][$l_pointer] = 0;
				$la_arr['prodaja'][$l_pointer] = 0;
				$la_arr['otpis'][$l_pointer] = 0;
				$la_arr['povrat'][$l_pointer] = 0;
				$la_arr['medju'][$l_pointer] = 0;
				$la_arr['proizv'][$l_pointer] = 0;
				$la_arr['inv'][$l_pointer] = 0;
				if($l_f2){
					$la_arr['pordf2'][$l_pointer] = 0;
				}
				//$la_arr['donos'][$l_pointer] = 0;
			}
			
			switch($x['type'][$i]){
				case 'F':
					// proizvodnja ("Factory")
					$la_arr['proizv'][$l_pointer] += $x['amount'][$i];
					$la_ukup['proizv'] += $x['amount'][$i];
					break;
					
				case 'I':
					// prodaja("Invoice")
					$la_arr['prodaja'][$l_pointer] += $x['amount'][$i];
					$la_ukup['prodaja'] += $x['amount'][$i];
					break;
					
				case '0':
					// primka
					$la_arr['nabava'][$l_pointer] += $x['amount'][$i];
					$la_ukup['nabava'] += $x['amount'][$i];
					break;
					
				case '1':
					// otpis
					$la_arr['otpis'][$l_pointer] += $x['amount'][$i];
					$la_ukup['otpis'] += $x['amount'][$i];
					break;
					
				case '2':
					// povratnica
					$la_arr['povrat'][$l_pointer] += $x['amount'][$i];
					$la_ukup['povrat'] += $x['amount'][$i];
					break;
					
				case '3':
					// medjuskladisnica
					$la_arr['medju'][$l_pointer] += $x['amount'][$i];
					$la_ukup['medju'] += $x['amount'][$i];
					break;
					
				case '4':
					// inventura
					$la_arr['inv'][$l_pointer] += $x['amount'][$i];
					$la_ukup['inv'] += $x['amount'][$i];
					break;
				
				case 'P':
					// PROD F2 - proknjizeno sa ERACUN skladisnih dokumenata
					// prodaja je negativna -> postaje pozitivan "prodano"; storno je pozitivan -> netira (Invoice - Storno)
					if($l_f2){
						$la_arr['pordf2'][$l_pointer] += -1 * $x['amount'][$i];
						$la_ukup['pordf2'] += -1 * $x['amount'][$i];
					}
					break;
				
				default:
			}
			
			$la_arr['stanje'][$l_pointer] =
				$la_arr['donos'][$l_pointer] + 
				$la_arr['nabava'][$l_pointer] -
				$la_arr['prodaja'][$l_pointer] -
				abs($la_arr['otpis'][$l_pointer]) -
				abs($la_arr['povrat'][$l_pointer]) -
				abs($la_arr['medju'][$l_pointer]) -
				abs($la_arr['proizv'][$l_pointer]) +
				$la_arr['inv'][$l_pointer];
			
			if($l_f2){
				$la_arr['stanje'][$l_pointer] -= $la_arr['pordf2'][$l_pointer];
			}
			
			/*
			// ako je zadnji zapis u arrayu ili se prelazi na sljedeci datum  u arrayu
			if(($i == sizeof($x['date']) - 1) || ($x['date'][$i] != $x['date'][$i + 1])){
				// zatvori redak kartice
				$la_arr['donos'][$l_pointer] = 
					$la_arr['stanje'][$l_pointer] -
					//$la_arr['nabava'][$l_pointer] -
					$la_arr['prodaja'][$l_pointer] -
					abs($la_arr['otpis'][$l_pointer]) -
					abs($la_arr['povrat'][$l_pointer]) -
					abs($la_arr['medju'][$l_pointer]) -
					abs($la_arr['proizv'][$l_pointer]) -
					$la_arr['inv'][$l_pointer];
			}
			*/
		}
		
		/*
		print "<br>\n";
		print "<br>\n";
		print "<br>\n";
		print "<br>\n";
		print_r($la_arr['datum']);
		exit;
		*/
		
		
		$pdf->SetFont("ArialCE", "", 9);
		$l_y = 35;
		
		for($i = sizeof($la_arr['datum']) - 1; $i >= 0; $i--){
			$l_pos = 0;
			$l_cnt_value = 0;
			foreach($la_arr as $key => $value){
				
				if($l_cnt_value == 0){
					$la_arr[$key][$i] = date('d.m.Y.', strtotime($la_arr[$key][$i]));
				}
				
				$l_pos += $la_width[$l_cnt_value];
				$pdf->SetXY(10 + $l_pos, $l_y);
				
				if($l_cnt_value > 0 && $la_arr[$key][$i] != 0){
					$la_arr[$key][$i] = sprintf('%01.4f', $la_arr[$key][$i]);
				}
				
				$pdf->MultiCell($la_width[$l_cnt_value], 5, _convert($la_arr[$key][$i]), 0, "L");
				$l_cnt_value++;
			}
			
			$l_y += 5;
			
			if($l_y >= 180){
				$pdf->AddPage();
				$l_y = 12;
			}
			
		}
		
		
		
		$pdf->SetFont("ArialCE", "B", 9);
		
		$l_pos = 0;
		$l_cnt_value = 0;
		foreach($la_ukup as $key => $value){
			$l_pos += $la_width[$l_cnt_value];
			$pdf->SetXY(10 + $l_pos, $l_y);
			
			if($l_cnt_value > 0 && $value > 0){
				$value = sprintf('%01.4f', $value);
			}
			
			$pdf->MultiCell($la_width[$l_cnt_value], 5, _convert($value), 'T', "L");
			$l_cnt_value++;
		}
		
		//print_r($la_arr);
		//print_r($x);
		
		//exit;
			
			
			
		$l_name = 'kartica_artikla.pdf';
		$l_dest = $g_path.'app/webroot/res/files/'.$l_name;
		
		$pdf->Output($l_dest, 'F');
		
		return array('path' => $g_path.'app/webroot/res/files/', 'name' => $l_name);
	}
	
	
	
	
	
	function all_kolicina_materijala_po_stolovima(){
		$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');
		
		$pdf =& new FPDI('P', 'mm', 'A4');
		
		$pdf->AddFont("ArialCE", "B", "6488f5d4657b269b9f5770508e80e299_arialbd.php");
		$pdf->AddFont("ArialCE", "", "332cc83ebee36613c19564e6e776c81b_arial.php");
		
		// set the sourcefile
		$pgNumber = $pdf->setSourceFile($g_path.'app/libs/fpdf/prazni-dokument.pdf');
		
		
		// add a page
		$pdf->AddPage();
		
		// import page 1
		$tplIdx = $pdf->importPage(1);
		
		// use the imported page and place it at point 0,0 with a width of 210 mm
		$pdf->useTemplate($tplIdx, 0, 0, 210, 297);
		
		
		$pdf->SetTextColor(0, 0, 0);
		
		$x = get_item('select value from settings where name = "general.name" ').', '.
			get_n_rows(get_item('select value from settings where name = "invoice.header" '), 4);
		$pdf->SetFont("ArialCE", "B", 9);
		$pdf->SetXY(10, 10);
		$pdf->MultiCell(110, 4, _convert($x), 0, "L");
		
		$pdf->SetFont("ArialCE", "B", 12);
		$pdf->SetXY(0, 13);
		$pdf->MultiCell(200, 40, _convert('Količina materijala po stolovima'), 0, "C");
		
		$pdf->SetFont("ArialCE", "", 8);
		
		$l_tip_prog = get_item('select value from settings where name = "tipprog" ');
		if($l_tip_prog == 'u'){
			
			
			$l_y = 40;
			
			
			
			
			
			
			
			$pdf->SetFont("ArialCE", "B", 8);
			$pdf->SetXY(10, $l_y);
			$pdf->MultiCell(200, 5, _convert('Popis stavaka računa izdanih na svih stolovima:'), 0, "L");
			$l_y += 5;
			
			
			$pdf->SetFont("ArialCE", "", 8);
			$y = get_data('select products.name, control_ns_details.base_price, control_ns_details.discount_percent, sum(control_ns_details.amount) as amount, sum(control_ns_details.price) as price '.
				' from control_ns_details, products '.
				' where control_ns_details.product_id = products.id '.
				' group by 1, 2, 3 '.
				' order by control_ns_details.table_id, products.name, control_ns_details.created ', 1);
			$l_y += 2;
			for($j = 0; isset($y['name']) && $j < sizeof($y['name']); $j++){
				$pdf->SetXY(10, $l_y);
				$pdf->MultiCell(50, 5, _convert($y['name'][$j]), 0, "L");
				$pdf->SetXY(66, $l_y);
				$pdf->MultiCell(19, 5, _convert($y['amount'][$j].' kom'), 0, "R");
				$pdf->SetXY(87, $l_y);
				$pdf->MultiCell(15, 5, _convert(sprintf('%01.2f', $y['base_price'][$j]).' Kn'), 0, "R");
				$pdf->SetXY(104, $l_y);
				$pdf->MultiCell(15, 5, _convert(sprintf('%01.2f', $y['discount_percent'][$j]).'%'), 0, "R");
				$pdf->SetXY(121, $l_y);
				$pdf->MultiCell(15, 5, _convert(sprintf('%01.2f', $y['price'][$j] * (1 - $y['discount_percent'][$j] / 100)).' Kn'), 0, "R");
				$l_y += 4;
				
				
				if($l_y >= 260){
					$pdf->AddPage();
					$l_y = 12;
				}
				
			}
			$pdf->SetXY(10, $l_y);
			$pdf->MultiCell(150, 5, _convert('--------------------------------------------------------------------------------------------------------------------------------'), 0, "L");
			$l_y += 4;
			
			
			
			
			
			
			$x = get_data('select users.full_name, control_ns.created, tables.name, control_ns.price, control_ns.user_id, control_ns.table_id '.
				' from control_ns, users, tables '.
				' where control_ns.user_id = users.id '.
				' and control_ns.table_id = tables.id '.
				' order by full_name, created, tables.name', 1);
				
			$l_ukup_sve = 0;
			for($i = 0; isset($x['full_name']) && $i < sizeof($x['full_name']); $i++){
				$l_ukup_sve += $x['price'][$i];
			}
			
			$pdf->SetFont("ArialCE", "B", 8);
			$pdf->SetXY(10, $l_y);
			$pdf->MultiCell(140, 5, _convert('Ukupno narudžbi na stolovima: '.sprintf('%01.2f', $l_ukup_sve).' Kn'), 0, "L");
			$l_y += 18;
			$pdf->SetFont("ArialCE", "", 8);
			
			
			
			
			
			
			$pdf->SetXY(10, $l_y);
			$pdf->MultiCell(200, 5, _convert('Količina robe koju je potrebno skinuti sa skladišta (neizdana roba po stolovima):'), 0, "L");
			$l_y += 5;
			
			
			$pdf->SetFont("ArialCE", "B", 8);
			$pdf->SetXY(10, $l_y);
			$pdf->MultiCell(35, 5, _convert('Naziv'), 0, "L");
			$pdf->SetXY(67, $l_y);
			$pdf->MultiCell(25, 5, _convert('Količina'), 0, "L");
			$pdf->SetXY(92, $l_y);
			$pdf->MultiCell(25, 5, _convert('Nabavna cijena'), 0, "L");
			$pdf->SetXY(119, $l_y);
			$pdf->MultiCell(25, 5, _convert('Iznos'), 0, "R");
			$l_y += 4;
			$pdf->SetFont("ArialCE", "", 8);
			
			$z = get_data('select articles.name, articles.id, articles.unit, articles.price_buy, sum(control_ns_details.amount * product_items.amount) as zbroj '.
				' from control_ns_details, products, product_items, articles '.
				' where control_ns_details.product_id = products.id '.
				' and products.id = product_items.product_id '.
				' and product_items.article_id = articles.id '.
				' group by articles.name, articles.id, articles.unit, articles.price_buy '.
				' order by articles.name, articles.id, articles.unit, articles.price_buy ', 1);
			$l_ukup_nc = 0;
			for($i = 0; isset($z['name']) && $i < sizeof($z['name']); $i++){
				$pdf->SetXY(10, $l_y);
				$pdf->MultiCell(55, 5, _convert($z['name'][$i]), 0, "L");
				$pdf->SetXY(67, $l_y);
				$pdf->MultiCell(25, 5, _convert(sprintf('%01.4f', $z['zbroj'][$i]).' '.$z['unit'][$i]), 0, "L");
				$pdf->SetXY(92, $l_y);
				$pdf->MultiCell(25, 5, _convert(sprintf('%01.2f', $z['price_buy'][$i]).' Kn/'.$z['unit'][$i]), 0, "L");
				$pdf->SetXY(119, $l_y);
				$pdf->MultiCell(25, 5, _convert(sprintf('%01.2f', $z['zbroj'][$i] * $z['price_buy'][$i]).' Kn'), 0, "R");
				$l_y += 4;
				$l_ukup_nc += $z['zbroj'][$i] * $z['price_buy'][$i];
				
				if($l_y >= 260){
					$pdf->AddPage();
					$l_y = 12;
				}
			}
			
			$pdf->SetFont("ArialCE", "B", 8);
			$pdf->SetXY(99, $l_y);
			$pdf->MultiCell(45, 5, _convert('Ukupno NC: '.sprintf('%01.2f', $l_ukup_nc).' Kn'), 0, "R");
			$l_y += 5;
			$pdf->SetFont("ArialCE", "", 8);
			
			
			
			
			
			
			$pdf->SetXY(0, $l_y);
			$pdf->MultiCell(200, 5, _convert('(Potrebno oduzeti od stanja skladišta, kako bi dobili ispravno stanje)'), 0, "C");
			$l_y += 14;
			
			
			
			
			
			$l_name_old = '-1-[2]-3-';
			$l_ukup = 0;
			for($i = 0; isset($x['full_name']) && $i < sizeof($x['full_name']); $i++){
				
				if($l_name_old != $x['full_name'][$i]){
					
					$pdf->SetFont("ArialCE", "B", 8);
					
					if($l_name_old != '-1-[2]-3-'){
						$l_y += 1;
						$pdf->SetXY(54, $l_y);
						$pdf->MultiCell(40, 5, _convert('Ukupno: '.sprintf('%01.2f', $l_ukup).' Kn'), 0, "R");
						$l_y += 5;
						$l_ukup = 0;
						
						
						$pdf->SetFont("ArialCE", "", 8);
						/*
						$y = get_data('select products.name, control_ns_details.amount, control_ns_details.base_price, control_ns_details.price, control_ns_details.discount_percent '.
							' from control_ns_details, products '.
							' where control_ns_details.product_id = products.id '.
							' and control_ns_details.user_id = '.$x['user_id'][$i - 1].
							' order by control_ns_details.table_id, control_ns_details.created ', 1);
						*/
						$y = get_data('select products.name, control_ns_details.base_price, control_ns_details.discount_percent, sum(control_ns_details.amount) as amount, sum(control_ns_details.price) as price '.
							' from control_ns_details, products '.
							' where control_ns_details.product_id = products.id '.
							' and control_ns_details.user_id = '.$x['user_id'][$i - 1].
							' group by 1, 2, 3 '.
							' order by control_ns_details.table_id, products.name, control_ns_details.created ', 1);
						$l_y += 2;
						for($j = 0; isset($y['name']) && $j < sizeof($y['name']); $j++){
							$pdf->SetXY(10, $l_y);
							$pdf->MultiCell(50, 5, _convert($y['name'][$j]), 0, "L");
							$pdf->SetXY(66, $l_y);
							$pdf->MultiCell(19, 5, _convert($y['amount'][$j].' kom'), 0, "R");
							$pdf->SetXY(87, $l_y);
							$pdf->MultiCell(15, 5, _convert(sprintf('%01.2f', $y['base_price'][$j]).' Kn'), 0, "R");
							$pdf->SetXY(104, $l_y);
							$pdf->MultiCell(15, 5, _convert(sprintf('%01.2f', $y['discount_percent'][$j]).'%'), 0, "R");
							$pdf->SetXY(121, $l_y);
							$pdf->MultiCell(15, 5, _convert(sprintf('%01.2f', $y['price'][$j] * (1 - $y['discount_percent'][$j] / 100)).' Kn'), 0, "R");
							$l_y += 4;
							
							
							if($l_y >= 260){
								$pdf->AddPage();
								$l_y = 12;
							}
							
						}
						$pdf->SetXY(10, $l_y);
						$pdf->MultiCell(150, 5, _convert('--------------------------------------------------------------------------------------------------------------------------------'), 0, "L");
						$l_y += 4;
						
						$pdf->SetFont("ArialCE", "B", 8);
					}
					
					$l_name_old = $x['full_name'][$i];
					$l_y += 2;
					
					$pdf->SetXY(10, $l_y);
					$pdf->MultiCell(100, 5, _convert($x['full_name'][$i]), 0, "L");
					$pdf->SetFont("ArialCE", "", 8);
					
					
					$l_y += 5;
					
					if($l_y >= 260){
						$pdf->AddPage();
						$l_y = 12;
					}
				}
				
				$la0_tmp = explode(' ', $x['created'][$i]);
				$la_tmp = explode('-', $la0_tmp[0]);
				$pdf->SetXY(10, $l_y);
				$pdf->MultiCell(35, 5, _convert($la_tmp[2].'-'.$la_tmp[1].'-'.$la_tmp[0].' '.$la0_tmp[1]), 0, "L");
				$pdf->SetXY(47, $l_y);
				$pdf->MultiCell(30, 5, _convert($x['name'][$i]), 0, "L");
				$pdf->SetXY(79, $l_y);
				$pdf->MultiCell(15, 5, _convert(sprintf('%01.2f', $x['price'][$i]).' Kn'), 0, "R");
				$l_ukup += $x['price'][$i];
				
				$l_y += 4;
				
				if($l_y >= 260){
					$pdf->AddPage();
					$l_y = 12;
				}
				
			}
			
			if($l_ukup > 0){
				$l_y += 1;
				$pdf->SetXY(54, $l_y);
				$pdf->SetFont("ArialCE", "B", 8);
				$pdf->MultiCell(40, 5, _convert('Ukupno: '.sprintf('%01.2f', $l_ukup).' Kn'), 0, "R");
				$l_y += 4;
				
				
				$pdf->SetFont("ArialCE", "", 8);
				/*
				$y = get_data('select products.name, control_ns_details.amount, control_ns_details.base_price, control_ns_details.price, control_ns_details.discount_percent '.
					' from control_ns_details, products '.
					' where control_ns_details.product_id = products.id '.
					' and control_ns_details.user_id = '.$x['user_id'][sizeof($x['user_id']) - 1].
					' order by control_ns_details.table_id, control_ns_details.created ', 1);
				*/
				$y = get_data('select products.name, control_ns_details.base_price, control_ns_details.discount_percent, sum(control_ns_details.amount) as amount, sum(control_ns_details.price) as price '.
					' from control_ns_details, products '.
					' where control_ns_details.product_id = products.id '.
					' and control_ns_details.user_id = '.$x['user_id'][sizeof($x['user_id']) - 1].
					' group by 1, 2, 3 '.
					' order by control_ns_details.table_id, products.name, control_ns_details.created ', 1);
				$l_y += 2;
				for($j = 0; isset($y['name']) && $j < sizeof($y['name']); $j++){
					$pdf->SetXY(10, $l_y);
					$pdf->MultiCell(50, 5, _convert($y['name'][$j]), 0, "L");
					$pdf->SetXY(66, $l_y);
					$pdf->MultiCell(15, 5, _convert($y['amount'][$j].' kom'), 0, "R");
					$pdf->SetXY(83, $l_y);
					$pdf->MultiCell(15, 5, _convert(sprintf('%01.2f', $y['base_price'][$j]).' Kn'), 0, "R");
					$pdf->SetXY(100, $l_y);
					$pdf->MultiCell(15, 5, _convert(sprintf('%01.2f', $y['discount_percent'][$j]).'%'), 0, "R");
					$pdf->SetXY(117, $l_y);
					$pdf->MultiCell(15, 5, _convert(sprintf('%01.2f', $y['price'][$j] * (1 - $y['discount_percent'][$j] / 100)).' Kn'), 0, "R");
					
					$l_y += 4;
					
					if($l_y >= 260){
						$pdf->AddPage();
						$l_y = 12;
					}
				}
				$pdf->SetXY(10, $l_y);
				$pdf->MultiCell(150, 5, _convert('--------------------------------------------------------------------------------------------------------------------------------'), 0, "L");
				$l_y += 10;
			}
			
			
		}
		
		
		$l_name = 'kolicina_materijala_po_stolovima.pdf';
		$l_dest = $g_path.'app/webroot/res/files/'.$l_name;
		
		$pdf->Output($l_dest, 'F');
		
		return array('path' => $g_path.'app/webroot/res/files/', 'name' => $l_name);
	}
	
	
	
	function all_keub($date_start, $date_end){
		$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');
		
		$pdf =& new FPDI('L', 'mm', 'A4');
		
		$pdf->AddFont("ArialCE", "B", "6488f5d4657b269b9f5770508e80e299_arialbd.php");
		$pdf->AddFont("ArialCE", "", "332cc83ebee36613c19564e6e776c81b_arial.php");
		
		// set the sourcefile
		$pgNumber = $pdf->setSourceFile($g_path.'app/libs/fpdf/prazni-dokument.pdf');
		
		
		// add a page
		$pdf->AddPage();
		
		// import page 1
		$tplIdx = $pdf->importPage(1);
		
		// use the imported page and place it at point 0,0 with a width of 210 mm
		$pdf->useTemplate($tplIdx, 0, 0, 210, 297);
		
		
		$pdf->SetTextColor(0, 0, 0);
		
		$x = get_item('select value from settings where name = "general.name" ').', '.
			get_n_rows(get_item('select value from settings where name = "invoice.header" '), 4);
		$pdf->SetFont("ArialCE", "B", 9);
		$pdf->SetXY(10, 10);
		$pdf->MultiCell(110, 4, _convert($x), 0, "L");
		
		
		$l_tip_prog = get_item('select value from settings where name = "tipprog" ');
		if($l_tip_prog == 'u'){
			
			$l_datum_od = $date_start;
			$l_datum_do = $date_end;
			
			$la_dat1 = explode('-', $l_datum_od);
			$la_dat2 = explode('-', $l_datum_do);
			
			$pdf->SetFont("ArialCE", "B", 11);
			$pdf->SetXY(10, 28);
			$pdf->MultiCell(277, 5, _convert('EVIDENCIJA PEKARSKE PROIZVODNJE, NABAVE I POTROŠNJE BRAŠNA (u kg) - MJESEC: '.$la_dat2[1].'.'.$la_dat2[2].'.'), 0, "C");
			
			$pdf->SetFont("ArialCE", "", 9);
			if($la_dat1[1] == $la_dat2[1] && $la_dat1[2] == $la_dat2[2]){
				
				$pdf->SetFont("ArialCE", "", 8);
				
				$pdf->SetXY(23, 35);
				$pdf->MultiCell(87, 5, _convert('KRUH'), 'B', 'C');
				
				$pdf->SetXY(113, 35);
				$pdf->MultiCell(50, 5, _convert('PECIVO I DR. PEK. PROIZVODI'), 'B', 'C');
				
				$pdf->SetXY(190, 35);
				$pdf->MultiCell(90, 5, _convert('BRAŠNO'), 'B', 'C');
				
				
				$x = get_data('select id, name from keub_groups order by id ', 1);
				
				$l_size = sizeof($x['id']);
				$x['id'][$l_size] = $l_size;
				$x['name'][$l_size] = 'Ukupna dnevna proizvod.';
				$pdf->SetFont("ArialCE", "", 7);
				for($i = 0; $i < sizeof($x['id']); $i++){
					$pdf->SetXY(20 + $i * 13, 41);
					$pdf->MultiCell(13.75, 3, _convert($x['name'][$i]), 0, 'C');
				}
				
				$pdf->Line(5, 51, 175, 51);
				
				
				
				
				
				$x2 = get_data('select id, name from keubarticle_groups order by id ', 1);
				
				$pdf->SetFont("ArialCE", "", 7);
				for($i = 0; $i < sizeof($x2['id']); $i++){
					$pdf->SetXY(20 + ($i + 1 + sizeof($x['id'])) * 13, 41);
					$pdf->MultiCell(13.75, 3, _convert($x2['name'][$i]), 0, 'C');
				}
				
				$pdf->Line(190, 51, 280, 51);
				
				
				
				
				
				
				$l_sum_sum = 0;
				
				$y = get_data('select medjus.datum, products.keub_group_id, sum(medju_items.kolicina * products.tezina) as masa '.
					' from medjus, medju_items, products '.
					' where medjus.id = medju_items.medju_id '.
					' and medju_items.product_id = products.id '.
					' and medjus.datum between "'.$la_dat1[2].'-'.$la_dat1[1].'-'.$la_dat1[0].' 00:00:00" and "'.$la_dat2[2].'-'.$la_dat2[1].'-'.$la_dat2[0].' 23:59:59" '.
					' and medjus.posted = 1 '.
					' and medjus.povrat = 0 '.
					' and products.keub = 1 '.
					' group by medjus.datum, products.keub_group_id '.
					' order by medjus.datum, products.keub_group_id ', 1);
					
				for($i = 0; $i < sizeof($y['keub_group_id']); $i++){
					$la_dat = explode('-', substr($y['datum'][$i], 0, 10));
					$y['datum'][$i] = $la_dat[2].'-'.$la_dat[1].'-'.$la_dat[0];
				}
				
				
				
				
				
				$y2 = get_data('select medjus.datum, articles.keubarticle_group_id, sum(medju_items.kolicina * product_items.amount) as masa '.
					' from medjus, medju_items, products, product_items, articles '.
					' where medjus.id = medju_items.medju_id '.
					' and medju_items.product_id = products.id '.
					' and product_items.product_id = products.id '.
					' and product_items.article_id = articles.id '.
					' and medjus.datum between "'.$la_dat1[2].'-'.$la_dat1[1].'-'.$la_dat1[0].' 00:00:00" and "'.$la_dat2[2].'-'.$la_dat2[1].'-'.$la_dat2[0].' 23:59:59" '.
					' and medjus.posted = 1 '.
					' and medjus.povrat = 0 '.
					' and articles.keub = 1 '.
					' group by medjus.datum, articles.keubarticle_group_id '.
					' order by medjus.datum, articles.keubarticle_group_id ', 1);
				
				for($i = 0; $i < sizeof($y2['keubarticle_group_id']); $i++){
					$la_dat = explode('-', substr($y2['datum'][$i], 0, 10));
					$y2['datum'][$i] = $la_dat[2].'-'.$la_dat[1].'-'.$la_dat[0];
				}
				
				
				
				
				
				$l_red = 0;
				$l_datum_old = '-1';
				$la_sum = array();
				
				for($i = 0; $i < sizeof($y['keub_group_id']); $i++){
					
					if($y['datum'][$i] != $l_datum_old){
						$l_datum_old = $y['datum'][$i];
						$l_red++;
						
						$pdf->SetXY(5, 48 + 4 * $l_red);
						$pdf->MultiCell(15, 3, _convert($y['datum'][$i]), 0, 'L');
						
						$l_sum = 0;
					}
					
					for($j = 0; $j < sizeof($x['id']); $j++){
						if($x['id'][$j] == $y['keub_group_id'][$i]){
							if($j < sizeof($x['id']) - 1){
								$pdf->SetXY(20 + $j * 13, 48 + 4 * $l_red);
								$pdf->MultiCell(13.75, 3, _convert(sprintf('%01.4f', $y['masa'][$i])), 0, 'C');
								$l_sum += $y['masa'][$i];
								$la_sum[$j] += $y['masa'][$i];
								$l_sum_sum += $y['masa'][$i];
							}
						}
					}
					if(($i < sizeof($y['keub_group_id']) - 1 && $y['datum'][$i + 1] != $l_datum_old) || ($i == sizeof($y['keub_group_id']) - 1)){
						$pdf->SetXY(20 + (sizeof($x['id']) - 1) * 13, 48 + 4 * $l_red);
						$pdf->MultiCell(13.75, 3, _convert(sprintf('%01.4f', $l_sum)), 0, 'C');
					}
					
				}
				
				
				$l_red = 0;
				$l_datum_old = '-1';
				$la_sum_2 = array();
				
				for($i = 0; $i < sizeof($y2['keubarticle_group_id']); $i++){
					if($y2['datum'][$i] != $l_datum_old){
						$l_datum_old = $y2['datum'][$i];
						$l_red++;
						$l_sum = 0;
					}
					
					for($j = 0; $j < sizeof($x2['id']); $j++){
						if($x2['id'][$j] == $y2['keubarticle_group_id'][$i]){
							$pdf->SetXY(20 + (sizeof($x['id']) + 1 + $j) * 13, 48 + 4 * $l_red);
							$pdf->MultiCell(13.75, 3, _convert(sprintf('%01.4f', $y2['masa'][$i])), 0, 'C');
							$la_sum_2[$j] += $y2['masa'][$i];
						}
					}
					
				}
				
				
				
				$pdf->Line(5, 53 + 4 * $l_red, 175, 53 + 4 * $l_red);
				$pdf->Line(190, 53 + 4 * $l_red, 280, 53 + 4 * $l_red);
				
				$pdf->SetXY(5, 54 + 4 * $l_red);
				$pdf->MultiCell(15, 3, _convert('UKUPNO'), 0, 'L');
				
				for($j = 0; $j < sizeof($x['id']); $j++){
					if($j < sizeof($x['id']) - 1){
						$pdf->SetXY(20 + $j * 13, 54 + 4 * $l_red);
						$pdf->MultiCell(13.75, 3, _convert(sprintf('%01.4f', $la_sum[$j])), 0, 'C');
					}
				}
				
				for($j = 0; $j < sizeof($x2['id']); $j++){
					if($j < sizeof($x2['id'])){
						$pdf->SetXY(20 + (sizeof($x['id']) + 1 + $j) * 13, 54 + 4 * $l_red);
						$pdf->MultiCell(13.75, 3, _convert(sprintf('%01.4f', $la_sum_2[$j])), 0, 'C');
					}
				}
				
				
				$pdf->SetXY(20 + (sizeof($x['id']) - 1) * 13, 54 + 4 * $l_red);
				$pdf->MultiCell(13.75, 3, _convert(sprintf('%01.4f', $l_sum_sum)), 0, 'C');
				
			}else{
				$pdf->SetXY(10, 57);
				$pdf->MultiCell(277, 5, _convert('Izvješće je moguće izraditi samo za jedan mjesec, za datume unutar istog mjeseca.'), 0, "C");
			}
		}
		
		$l_name = 'keub.pdf';
		$l_dest = $g_path.'app/webroot/res/files/'.$l_name;
		
		$pdf->Output($l_dest, 'F');
		
		return array('path' => $g_path.'app/webroot/res/files/', 'name' => $l_name);
	}
	
	
	
	
	function all_rekapitproizvmedjus($date_start, $date_end, $keub_or_all, $povrat_ili_ne){
		
		$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');
		
		$pdf = new FPDI('P', 'mm', 'A4');
		
		$pdf->AddFont("ArialCE", "B", "6488f5d4657b269b9f5770508e80e299_arialbd.php");
		$pdf->AddFont("ArialCE", "", "332cc83ebee36613c19564e6e776c81b_arial.php");
		
		// set the sourcefile
		$pgNumber = $pdf->setSourceFile($g_path.'app/libs/fpdf/prazni-dokument.pdf');
		
		
		// add a page
		$pdf->AddPage();
		
		// import page 1
		$tplIdx = $pdf->importPage(1);
		
		// use the imported page and place it at point 0,0 with a width of 210 mm
		$pdf->useTemplate($tplIdx, 0, 0, 210, 297);
		
		
		$pdf->SetTextColor(0, 0, 0);
		
		$x = get_item('select value from settings where name = "general.name" ').', '.
			get_n_rows(get_item('select value from settings where name = "invoice.header" '), 4);
		$pdf->SetFont("ArialCE", "B", 9);
		$pdf->SetXY(10, 10);
		$pdf->MultiCell(110, 4, _convert($x), 0, "L");
		
		
		$l_tip_prog = get_item('select value from settings where name = "tipprog" ');
		if($l_tip_prog == 'u'){
			
			
			$la_tmp_1 = explode('-', $date_start);
			$la_tmp_2 = explode('-', $date_end);
			
			$l_datum_1 = $la_tmp_1[0].'.'.$la_tmp_1[1].'.'.$la_tmp_1[2].'.';
			$l_datum_2 = $la_tmp_2[0].'.'.$la_tmp_2[1].'.'.$la_tmp_2[2].'.';
			
			$l_datum_db_1 = $la_tmp_1[2].'-'.$la_tmp_1[1].'-'.$la_tmp_1[0];
			$l_datum_db_2 = $la_tmp_2[2].'-'.$la_tmp_2[1].'-'.$la_tmp_2[0];
			$l_datum_from = $l_datum_db_1.' 00:00:00';
			$l_datum_to = $l_datum_db_2.' 23:59:59';
			
			
			
			
			$pdf->SetFont("ArialCE", "B", 11);
			$pdf->SetXY(0, 30);
			$pdf->MultiCell(210, 5, _convert('Rekapitulacija proizvodnih međuskladišnica'), 0, "C");
			
			$pdf->SetFont("ArialCE", "", 10);
			$pdf->SetXY(0, 35);
			$pdf->MultiCell(210, 5, _convert('Period od '.$l_datum_1.' do '.$l_datum_2), 0, "C");
			
			
			if($keub_or_all){
				$l_add_sql = ' and products.keub = 1 ';
				$l_keub_txt = ' - Prikaz samo KEUB produkata';
			}else{
				$l_add_sql = '';
				$l_keub_txt = '';
			}
			
			
			$l_add_2_sql = '';
			if($povrat_ili_ne == 1){
				// nisu povrat
				$l_add_2_sql = ' and medjus.povrat = 0 ';
				
				$pdf->SetFont("ArialCE", "", 9);
				$pdf->SetXY(0, 40);
				$pdf->MultiCell(210, 5, _convert('"Obicne" medjuskladisnice'.$l_keub_txt), 0, "C");
			}
			if($povrat_ili_ne == 2){
				// jesu povrat
				$l_add_2_sql = ' and medjus.povrat = 1 ';
				
				$pdf->SetFont("ArialCE", "", 9);
				$pdf->SetXY(0, 40);
				$pdf->MultiCell(210, 5, _convert('"Povratne" medjuskladisnice'.$l_keub_txt), 0, "C");
			}
			
			
			$pdf->SetFont("ArialCE", "B", 9);
			$pdf->Ln(5);
			$pdf->Cell(50, 4, _convert('Izvješćem su obuhvaćene sljedeće proizvodne međuskladišnice:'), 0, 0, "L");
			$pdf->Ln(6);
			
			$pdf->SetFont("ArialCE", "", 9);
			$l_sql = 'select broj, datum '.
				' from medjus '.
				' where medjus.posted = 1 '.
				' and medjus.datum between "'.$l_datum_from.'" and "'.$l_datum_to.'" '.
				$l_add_2_sql.
				' order by medjus.id ';
			$y = get_data($l_sql, 1);
			for($i = 0; $i < sizeof($y['broj']); $i++){
				$la_tmp_1 = explode(' ', $y['datum'][$i]);
				$la_tmp_1 = explode('-', $la_tmp_1[0]);
				$l_datum_pm = $la_tmp_1[2].'.'.$la_tmp_1[1].'.'.$la_tmp_1[0].'.';
				$pdf->Cell(50, 4, _convert('Broj '.$y['broj'][$i].' ('.$l_datum_pm.')'), 0, 0, "L");
				$pdf->Ln(4);
			}
			
			
			
			////////////////////////////////////////////////////////////////////////////////
			////////////////////////////////////////////////////////////////////////////////
			
			
			$pdf->SetFont("ArialCE", "B", 10);
			$pdf->Ln(7);
			$pdf->Cell(50, 5, _convert('Produkti'), 0, 0, "L");
			$pdf->Ln(5);
			
			
			$header = array('Red.br', 'Naziv normativa', 'kol', 'Uk NC', 'Uk PC');
			$w = array(15, 80, 15, 18, 18);
			$pos = array('C', 'L', 'C', 'R', 'R');
			$pos_in = array('R', 'L', 'C', 'R', 'R');
			
			
			$pdf->SetFont("ArialCE", "", 9);
			$pdf->Ln();
			
			for($i = 0; $i < count($header); $i++){
				$pdf->Cell($w[$i], 5, $header[$i], 'B', 0, $pos[$i]);
			}
			$pdf->Ln(6);
			
			
			
			$l_kol = 0;
			$l_uk_nc = 0;
			$l_uk_pc = 0;
			$la_field = array('', 'name', 'kolicina', 'uk_nc', 'uk_pc');
			
			
			$l_sql = 'select products.name, sum(medju_items.kolicina) as kolicina, sum(medju_items.uk_nc) as uk_nc, sum(medju_items.uk_pc) as uk_pc '.
				' from medjus, medju_items, products '.
				' where medjus.id = medju_items.medju_id '.
				' and medju_items.product_id = products.id '.
				' and medjus.posted = 1 '.
				$l_add_sql.
				$l_add_2_sql.
				' and medjus.datum between "'.$l_datum_from.'" and "'.$l_datum_to.'" '.
				' group by products.name '.
				' order by products.name ';
			$y = get_data($l_sql, 1);
			
			for($i = 0; $i < sizeof($y['name']); $i++){
				for($j = 0; $j < count($header); $j++){
					switch($j){
						case 0:
							$pdf->Cell($w[$j], 5, ($i + 1).'.  ', '', 0, $pos_in[$j]);
							break;
						
						case 1:
							// product name
							$pdf->Cell($w[$j], 5, _convert($y[$la_field[$j]][$i]), '', 0, $pos_in[$j]);
							break;
						
						default:
							if($j == 2){
								$l_kol += $y[$la_field[$j]][$i];
							}
							if($j == 3){
								$l_uk_nc += $y[$la_field[$j]][$i];
							}
							if($j == 4){
								$l_uk_pc += $y[$la_field[$j]][$i];
							}
							$pdf->Cell($w[$j], 5, $y[$la_field[$j]][$i], '', 0, $pos_in[$j]);
					}
				}
				$pdf->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, 18, 18);
			$pos = array('L', 'C', 'R', 'R');
			
			for($i = 0; $i < count($header); $i++){
				$pdf->Cell($w[$i], 5, $header[$i], 'T', 0, $pos[$i]);
			}
			$pdf->Ln(15);
			
			
			
			
			
			$pdf->SetFont("ArialCE", "B", 10);
			$pdf->Cell(50, 5, _convert('Izračun materijala'), 0, 0, "L");
			$pdf->Ln(10);
			
			
			$pdf->SetFont("ArialCE", "", 9);
			$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++){
				$pdf->Cell($w[$i], 5, $header[$i], 'B', 0, $pos[$i]);
			}
			$pdf->Ln(6);
			
			
			$l_kol = 0;
			$l_uk_nc = 0;
			$la_field = array('', 'name', 'kolicina', 'unit', 'nc', 'uk_nc');
			$l_sql = 'select articles.name, articles.unit, articles.price_buy as nc, sum(medju_items.kolicina * product_items.amount) as kolicina, 0 as uk_nc '.
				' from medjus, medju_items, products, product_items, articles '.
				' where medjus.id = medju_items.medju_id '.
				' and medju_items.product_id = products.id '.
				' and products.id = product_items.product_id '. 
				' and product_items.article_id = articles.id '.
				' and medjus.posted = 1 '.
				$l_add_sql.
				$l_add_2_sql.
				' and medjus.datum between "'.$l_datum_from.'" and "'.$l_datum_to.'" '.
				' group by articles.name, articles.unit, articles.price_buy '.
				' order by articles.name ';
			$y = get_data($l_sql, 1);
			for($i = 0; $i < sizeof($y['name']); $i++){
				for($j = 0; $j < count($header); $j++){
					switch($j){
						case 0:
							$pdf->Cell($w[$j], 5, ($i + 1).'.  ', '', 0, $pos_in[$j]);
							break;
						
						case 1:
							// article name
							$pdf->Cell($w[$j], 5, _convert($y[$la_field[$j]][$i]), '', 0, $pos_in[$j]);
							break;
							
						default:
							if($j == 2){
								$l_kol += $y[$la_field[$j]][$i];
								$y[$la_field[$j]][$i] = sprintf('%01.4f', $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];
								$y[$la_field[5]][$i] = sprintf('%01.2f', $y[$la_field[5]][$i]);
							}
							$pdf->Cell($w[$j], 5, $y[$la_field[$j]][$i], '', 0, $pos_in[$j]);
					}
				}
				$pdf->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++){
				$pdf->Cell($w[$i], 5, $header[$i], 'T', 0, $pos[$i]);
			}
			$pdf->Ln(6);
			
			
			$pdf->Ln();
			$pdf->Ln();
			$pdf->Ln();
			$pdf->Ln();
			$pdf->Cell(10);
			$pdf->Cell(60, 20, 'Izradio', 'B', 0, 'C');
			$pdf->Cell(40);
			$pdf->Cell(60, 20, 'Odobrio', 'B', 0, 'C');
			$pdf->Ln();
			
			////////////////////////////////////////////////////////////////////////////////
			////////////////////////////////////////////////////////////////////////////////
			
			
		}
		
		
		$l_name = 'rekapitproizvmedjus.pdf';
		$l_dest = $g_path.'app/webroot/res/files/'.$l_name;
		
		$pdf->Output($l_dest, 'F');
		
		return array('path' => $g_path.'app/webroot/res/files/', 'name' => $l_name);
	}
	
	
	
	
	function all_rekapitreceivings($date_start, $date_end, $grupiraj_dobavljac, $rbr_dobavljac){
		
		$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');
		
		$pdf = new FPDI('L', 'mm', 'A4');
		
		$pdf->AddFont("ArialCE", "B", "6488f5d4657b269b9f5770508e80e299_arialbd.php");
		$pdf->AddFont("ArialCE", "", "332cc83ebee36613c19564e6e776c81b_arial.php");
		
		// set the sourcefile
		$pgNumber = $pdf->setSourceFile($g_path.'app/libs/fpdf/prazni-dokument-L.pdf');
		
		
		// add a page
		$pdf->AddPage();
		
		// import page 1
		$tplIdx = $pdf->importPage(1);
		
		// use the imported page and place it at point 0,0 with a width of 210 mm
		$pdf->useTemplate($tplIdx, 0, 0, 297, 210);
		
		
		$pdf->SetTextColor(0, 0, 0);
		
		$x = get_item('select value from settings where name = "general.name" ').', '.
			get_n_rows(get_item('select value from settings where name = "invoice.header" '), 4);
		$pdf->SetFont("ArialCE", "B", 9);
		$pdf->SetXY(10, 10);
		$pdf->MultiCell(110, 4, _convert($x), 0, "L");
		
		
		$pdf->SetFont("ArialCE", "B", 11);
		$pdf->SetXY(10, 13);
		
		
		if($rbr_dobavljac){
			$y = ': '.get_item('select name from suppliers where id = '.$rbr_dobavljac);
		}else{
			$y = '';
		}
		
		$l_naslov = 'REKAPITULACIJA PRIMKI'.$y;
		
		
		if($grupiraj_dobavljac){
			$l_naslov .= ' - grupirano';
			
			$la_header = array('', 'Dobavljač', 'Broj primki', 'Marža', 'NC', 'PC NETO', 'PC', '');
			$la_width = array(0, 150, 20, 20, 20, 20, 20, 0);
		}else{
			
			$la_header = array('', 'Broj primke', 'Datum', 'Broj dokumenta', 'Dobavljač', 'Marža', 'NC', 'PC NETO', 'PC', '');
			$la_width = array(0, 25, 25, 30, 120, 20, 20, 20, 20, 0);
		}
		
		$l_datum_od = $date_start;
		$l_datum_do = $date_end;
		
		$pdf->MultiCell(277, 5, _convert($l_naslov), 0, "C");
		
		$pdf->SetFont("ArialCE", "B", 8);
		$pdf->SetXY(10, 19);
		$pdf->MultiCell(277, 5, _convert('Period od '.$l_datum_od.' do '.$l_datum_do), 0, "C");
		
		
		
		
		$pdf->SetFont("ArialCE", "B", 9);
		$l_pos = 0;
		for($i = 0; $i < sizeof($la_header) - 1; $i++){
			$l_pos += $la_width[$i];
			$pdf->SetXY(5 + $l_pos, 30);
			$pdf->MultiCell($la_width[$i + 1], 5, _convert($la_header[$i + 1]), 0, "C");
		}
		
		$pdf->SetFont("ArialCE", "", 9);
		$l_y = 35;
		
		$la_tmp = explode('-', $l_datum_od);
		$l_dat_1 = $la_tmp[2].'-'.$la_tmp[1].'-'.$la_tmp[0];
		
		$la_tmp = explode('-', $l_datum_do);
		$l_dat_2 = $la_tmp[2].'-'.$la_tmp[1].'-'.$la_tmp[0].' 23:59:59';
		
		
		
		if($rbr_dobavljac){
			$l_dobavljac_sql = ' and receivings.supplier_id = '.$rbr_dobavljac;
		}else{
			$l_dobavljac_sql = '';
		}
		
		if($grupiraj_dobavljac){
			$la_value = array('', 'supplier_id', 'broj_primki', 'marza_per_receiving', 'price_buy', 'price_sell_minus_pdv', 'price_sell', '');
			
			$x = get_data('select receivings.supplier_id as id, receivings.supplier_id, count(*) as broj_primki, 0 as marza_per_receiving, '.
				' sum(receivings.price_buy) as price_buy, 0 as price_sell_minus_pdv, '.
				' sum(receivings.price_sell) as price_sell '.
				' from receivings '.
				' where receivings.type = 0 '.
				' and receivings.posted = 1 '.
				' and receivings.document_date between "'.$l_dat_1.'" and "'.$l_dat_2.'" '.
				$l_dobavljac_sql.
				' group by receivings.supplier_id, receivings.supplier_id '.
				' order by broj_primki desc ', 1);
				
			for($i = 0; $i < sizeof($x['id']); $i++){
				$x['marza_per_receiving'][$i] = get_item('select sum(receiving_items.marza_per_receiving) as zbr from receivings, receiving_items where receivings.id = receiving_items.receiving_id and receivings.supplier_id = '.$x['id'][$i]);
				$x['price_sell_minus_pdv'][$i] = get_item('select sum(price_sell_ukup / (1 + (tax_1 / 100) + (tax_2 / 100))) as zbr from receivings, receiving_items where receivings.id = receiving_items.receiving_id and receivings.supplier_id = '.$x['id'][$i]);
			}
				
				
		}else{
			$la_value = array('', 'broj_primka', 'document_date', 'number', 'supplier_id', 'marza_per_receiving', 'price_buy', 'price_sell_minus_pdv', 'price_sell');
			
			$x = get_data('select * '.
				' from receivings '.
				' where type = 0 '.
				' and posted = 1 '.
				' and document_date between "'.$l_dat_1.'" and "'.$l_dat_2.'" '.
				$l_dobavljac_sql.
				' order by document_date desc, id desc ', 1);
		}
		
		$l_marza = 0;
		$l_nc_ukup = 0;
		$l_pc_ukup_neto = 0;
		$l_pc_ukup = 0;
		$l_br_primki = 0;
		
		
		
		//p($x['id']);
		//pe($x['supplier_id']);
		
		
		for($i = 0; $i < sizeof($x['id']) + 1; $i++){
			
			if($i == (sizeof($x['id']) + 1 - 1)){
				// suma, zadnji red
				if($grupiraj_dobavljac){
					$la_header_last = array('UKUPNO', $l_br_primki, $l_marza, $l_nc_ukup, $l_pc_ukup_neto, $l_pc_ukup);
				}else{
					$la_header_last = array('', '', '', 'UKUPNO', $l_marza, $l_nc_ukup, $l_pc_ukup_neto, $l_pc_ukup);
				}
			}
			
			//p('11');
			//p(sizeof($la_value));
			
			$l_pos = 0;
			for($j = 0; $j < sizeof($la_value) - 1; $j++){
				$l_pos += $la_width[$j];
				$pdf->SetXY(5 + $l_pos, $l_y);
				
				
				if($i == (sizeof($x['id']) + 1 - 1)){
					// suma, zadnji red
					$x[$la_value[$j + 1]][$i] = $la_header_last[$j];
					
					
					$l_align = 'L';
					
					if($grupiraj_dobavljac){
						switch($j){
							case 1:
								$l_align = 'C';
								break;
								
							case 2:
							case 3:
							case 4:
							case 5:
								$l_align = 'R';
								$x[$la_value[$j + 1]][$i] = sprintf('%01.2f', $x[$la_value[$j + 1]][$i]);
								break;
						}
						
						
					}else{
						switch($j){
							case 4:
							case 5:
							case 6:
							case 7:
								$l_align = 'R';
								$x[$la_value[$j + 1]][$i] = sprintf('%01.2f', $x[$la_value[$j + 1]][$i]);
								break;
						}
					}
					
					
					$pdf->SetFont("ArialCE", "B", 9);
					$pdf->MultiCell($la_width[$j + 1], 4, _convert($x[$la_value[$j + 1]][$i]), 'TB', $l_align);
					
				}else{
					$l_align = 'L';
					
					if($grupiraj_dobavljac){
						
						//p($grupiraj_dobavljac);
						
						switch($j){
							case 0:
								$x[$la_value[$j + 1]][$i] = get_item('select name from suppliers where id = '.$x[$la_value[$j + 1]][$i]);
								break;
								
							case 1:
								$l_align = 'C';
								break;
								
							case 2:
							case 3:
							case 4:
							case 5:
								$x[$la_value[$j + 1]][$i] = sprintf('%01.2f', $x[$la_value[$j + 1]][$i]);
								$l_align = 'R';
								break;
						}
						
						
					}else{
						switch($j){
							case 1:
								$x[$la_value[$j + 1]][$i] = u_godina($x[$la_value[$j + 1]][$i]).'-'.u_mjesec($x[$la_value[$j + 1]][$i], 1).'-'.u_datum($x[$la_value[$j + 1]][$i], 1);
								break;
								
							case 3:
								$x[$la_value[$j + 1]][$i] = get_item('select name from suppliers where id = '.$x[$la_value[$j + 1]][$i]);
								break;
								
							case 4:
								$x[$la_value[$j + 1]][$i] = sprintf('%01.2f', get_item('select sum(marza_per_receiving) as zbr from receiving_items where receiving_id = '.$x['id'][$i]));
								$l_align = 'R';
								//pe($x);
								break;
								
							case 5:
								$x[$la_value[$j + 1]][$i] = sprintf('%01.2f', $x[$la_value[$j + 1]][$i]);
								$l_align = 'R';
								break;
								
							case 6:
								//$x[$la_value[$j + 1]][$i] = sprintf('%01.2f', $x[$la_value[$j + 1]][$i] / 1.25);
								$x[$la_value[$j + 1]][$i] = sprintf('%01.2f', get_item('select sum(price_sell_ukup / (1 + (tax_1 / 100) + (tax_2 / 100))) as zbr from receiving_items where receiving_id = '.$x['id'][$i]).' limit 1 ');
								$l_align = 'R';
								break;
								
							case 7:
								$x[$la_value[$j + 1]][$i] = sprintf('%01.2f', $x[$la_value[$j + 1]][$i]);
								$l_align = 'R';
								break;
						}
					}
					
					if($la_value[$j + 1] != ''){
						$pdf->MultiCell($la_width[$j + 1], 5, _convert($x[$la_value[$j + 1]][$i]), 0, $l_align);
					}
				}
				
				
				
				
				if($grupiraj_dobavljac){
					if($j == 1){
						$l_br_primki += $x[$la_value[$j + 1]][$i];
					}
					if($j == 2){
						$l_marza += $x[$la_value[$j + 1]][$i];
					}
					if($j == 3){
						$l_nc_ukup += $x[$la_value[$j + 1]][$i];
					}
					if($j == 4){
						$l_pc_ukup_neto += $x[$la_value[$j + 1]][$i];
					}
					if($j == 5){
						$l_pc_ukup += $x[$la_value[$j + 1]][$i];
					}
				}else{
					if($j == 4){
						$l_marza += $x[$la_value[$j + 1]][$i];
					}
					if($j == 5){
						$l_nc_ukup += $x[$la_value[$j + 1]][$i];
					}
					if($j == 6){
						$l_pc_ukup_neto += $x[$la_value[$j + 1]][$i];
					}
					if($j == 7){
						$l_pc_ukup += $x[$la_value[$j + 1]][$i];
					}
				}
			}
			
			$l_y += 5;
			
			if($i == (sizeof($x['id']) + 1 - 1)){
				// suma, zadnji red
			}else{
				if($l_y >= 180){
					$pdf->AddPage();
					$l_y = 12;
					
					$pdf->SetFont("ArialCE", "B", 9);
					$l_pos = 0;
					for($k = 0; $k < sizeof($la_header) - 1; $k++){
						$l_pos += $la_width[$k];
						$pdf->SetXY(5 + $l_pos, $l_y);
						$pdf->MultiCell($la_width[$k + 1], 5, _convert($la_header[$k + 1]), 0, "L");
					}
					
					$pdf->SetFont("ArialCE", "", 9);
					$l_y += 7;
				}
			}
		}
		
		
		
		$l_name = 'rekapitreceivings.pdf';
		$l_dest = $g_path.'app/webroot/res/files/'.$l_name;
		
		$pdf->Output($l_dest, 'F');
		
		return array('path' => $g_path.'app/webroot/res/files/', 'name' => $l_name);
	}
	
	
	
	
	
	
	
	
	
	function all_ispis_cjenika($group, $sort){
		
		$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');
		
		$pdf =& new FPDI('P', 'mm', 'A4');
		
		$pdf->AddFont("ArialCE", "B", "6488f5d4657b269b9f5770508e80e299_arialbd.php");
		$pdf->AddFont("ArialCE", "", "332cc83ebee36613c19564e6e776c81b_arial.php");
		
		// set the sourcefile
		$pgNumber = $pdf->setSourceFile($g_path.'app/libs/fpdf/prazni-dokument.pdf');
		
		//$pdf->SetAutoPageBreak(true, 10);
		
		
		$x = get_item('select value from settings where name = "general.name" ').', '.
			get_n_rows(get_item('select value from settings where name = "invoice.header" '), 4);
		
		$la_tmp_1 = explode('-', date('Y-m-d'));
		
		$l_datum_1 = $la_tmp_1[0].'.'.$la_tmp_1[1].'.'.$la_tmp_1[2].'.';
		
		$l_datum_db_1 = $la_tmp_1[2].'-'.$la_tmp_1[1].'-'.$la_tmp_1[0];
		
		
		$l_tip_prog = get_item('select value from settings where name = "tipprog" ');
		
		
		
		
		
		$l_item = '';
		
	
		$l_cnt = 0;


		// add a page
		$pdf->AddPage();
		
		// import page 1
		$tplIdx = $pdf->importPage(1);
		
		// use the imported page and place it at point 0,0 with a width of 210 mm
		$pdf->useTemplate($tplIdx, 0, 0, 210, 297);
		
		
		
		$pdf->SetFont("ArialCE", "B", 10);
		$pdf->SetTextColor(0, 0, 0);
		
		$pdf->SetXY(10, 10);
		$pdf->MultiCell(210, 5, _convert($x), 0, "L");
		
		
		
		$pdf->SetFont("ArialCE", "", 9);
		
		//$pdf->SetXY(10, 34);
		//$pdf->MultiCell(210, 4, _convert('-------------------------------------------------------------------------------'), 0, "L");
		
		$pdf->SetFont("ArialCE", "B", 14);
		
		$pdf->SetXY(10, 32);
		$pdf->MultiCell(190, 7, _convert('ISPIS CJENIKA'), 0, "C");
		
		$pdf->SetFont("ArialCE", "", 9);
		
		$pdf->SetXY(10, 40);
		$pdf->MultiCell(190, 5, _convert('Datum: '.$l_datum_db_1), 0, "C");

		$pdf->SetFont("ArialCE", "", 9);
		
		
		if($l_tip_prog == 'm'){
			
				
				
			$la_header = array('Šifra', 'Naziv artikla', 'Cijena (EUR)');
			$la_width = array(0, 40, 110, 30);
			$la_pos = array('L', 'L', 'L', 'R');
			
			
			if(empty($group)){
				$group = '-1';
			}
			if(empty($sort)){
				$sort = '1';
			}
			
			switch($group){
				case '-2':
					$l_where = ' ';
					break;
				case '-1':
					$l_where = ' and articles.article_group_id is null ';
					break;
				default:
					$l_where = ' and articles.article_group_id = '.intval($group);
					break;
			}
			
			switch($sort){
				case 1:
					$l_order = ' order by article_groups.id, articles.barcode ';
					break;
					
				case 2:
					$l_order = ' order by article_groups.id, articles.name ';
					break;
					
				case 3:
					$l_order = ' order by article_groups.name, articles.barcode ';
					break;
					
				case 4:
					$l_order = ' order by article_groups.name, articles.name ';
					break;
				
				default:
					$l_order = '';
			}
			
			$x = get_data('select articles.barcode, articles.name as article_name, articles.price_sell, articles.article_group_id, article_groups.name '.
				' from articles left outer join article_groups on articles.article_group_id = article_groups.id '.
				' where 1 = 1 '.$l_where.$l_order.' ', 1);
			
			
			
			
			
			$pdf->SetFont("ArialCE", "B", 9);
			
			$l_pos = 0;
			for($i = 0; $i < sizeof($la_header); $i++){
				$l_pos += $la_width[$i];
				$pdf->SetXY(10 + $l_pos, 55);
				$pdf->MultiCell($la_width[$i + 1], 5, _convert($la_header[$i]), 0, $la_pos[$i + 1]);
			}
			
			
			
			
			/*
			p('select articles.barcode, articles.name as article_name, articles.price_sell, articles.article_group_id, article_groups.name '.
				' from articles left outer join article_groups on articles.article_group_id = article_groups.id '.
				' where 1 = 1 '.$l_where.$l_order.' ');
			pe($x);
			*/
			
			
			$l_row = 0;
			
			$l_group_name = '--..-1-..--';
			
			for($i = 0; $i < sizeof($x['barcode']); $i++){
				
				$l_pos = 0;
				
				if($x['name'][$i] != $l_group_name){
					
					$l_group_name = $x['name'][$i];
					if($x['name'][$i] == ''){
						$x['name'][$i] = 'Bez naziva';
					}
					
					$pdf->SetFont("ArialCE", "B", 9);
					
					$l_row++;
					$pdf->SetXY(10, 60 + 5 * $l_row++);
					$pdf->MultiCell(180, 5, _convert('Grupa: '.$x['name'][$i]), 'B', 'L');
					
					$pdf->SetFont("ArialCE", "", 9);
				}
				
				
				
				$l_pos += $la_width[0];
				$pdf->SetXY(10 + $l_pos, 60 + 5 * $l_row);
				$pdf->MultiCell($la_width[1], 5, _convert($x['barcode'][$i]), 'TB', $la_pos[1]);
				
				$l_pos += $la_width[1];
				$pdf->SetXY(10 + $l_pos, 60 + 5 * $l_row);
				$pdf->MultiCell($la_width[2], 5, _convert($x['article_name'][$i]), 'TB', $la_pos[2]);
				
				$l_pos += $la_width[2];
				$pdf->SetXY(10 + $l_pos, 60 + 5 * $l_row++);
				$pdf->MultiCell($la_width[3], 5, _convert(number_format($x['price_sell'][$i],2, '.', ' ')), 'TB', $la_pos[3]);
				
				
				
				
				if(62 + 6 * $l_row >= 290){
					$pdf->AddPage();
					$l_row = -8;
					
					$pdf->SetFont("ArialCE", "B", 9);
					
					$l_pos = 0;
					for($j = 0; $j < sizeof($la_header); $j++){
						$l_pos += $la_width[$j];
						$pdf->SetXY(10 + $l_pos, 14);
						$pdf->MultiCell($la_width[$j + 1], 5, _convert($la_header[$j]), 0, $la_pos[$j + 1]);
					}
					
					$pdf->SetFont("ArialCE", "", 9);
				}
				
				
			}
			
			
			
			
			
			
			
		}
	
		
		
		//p($l_xls);
		
			
		$l_name = 'ispis_cjenika.pdf';
		$l_dest = $g_path.'app/webroot/res/files/'.$l_name;
		
		$pdf->Output($l_dest, 'F');
		
		
		$fp = @fopen($g_path.'app/webroot/res/files/'.str_replace('.pdf', '.csv', $l_name), 'wb');
		fwrite($fp, $l_xls);
		fclose($fp);
		
		
		return array('path' => $g_path.'app/webroot/res/files/', 'name' => $l_name);
	}
	
	
	
	function all_rekapitulacija_dobavljaca($date_start, $date_end){
		
		
		$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');
		
		$pdf =& new FPDI('L', 'mm', 'A4');
		
		$pdf->AddFont("ArialCE", "B", "6488f5d4657b269b9f5770508e80e299_arialbd.php");
		$pdf->AddFont("ArialCE", "", "332cc83ebee36613c19564e6e776c81b_arial.php");
		
		// set the sourcefile
		$pgNumber = $pdf->setSourceFile($g_path.'app/libs/fpdf/prazni-dokument.pdf');
		
		//$pdf->SetAutoPageBreak(true, 10);
		
		
		$x = get_item('select value from settings where name = "general.name" ').', '.
			get_n_rows(get_item('select value from settings where name = "invoice.header" '), 4);
		
		$x = trim($x)."\nOznaka poslovnog prostora: ".get_item('select value from settings where name = "general.placeid" ');
		
		$la_tmp_1 = explode('-', $date_start);
		$la_tmp_2 = explode('-', $date_end);
		
		$l_datum_1 = $la_tmp_1[0].'.'.$la_tmp_1[1].'.'.$la_tmp_1[2].'.';
		$l_datum_2 = $la_tmp_2[0].'.'.$la_tmp_2[1].'.'.$la_tmp_2[2].'.';
		
		$l_datum_db_1 = $la_tmp_1[2].'-'.$la_tmp_1[1].'-'.$la_tmp_1[0];
		$l_datum_db_2 = $la_tmp_2[2].'-'.$la_tmp_2[1].'-'.$la_tmp_2[0];
		$l_datum_from = $l_datum_db_1.' 00:00:00';
		$l_datum_to = $l_datum_db_2.' 23:59:59';
		
		
		$l_tip_prog = get_item('select value from settings where name = "tipprog" ');
		
		
		
		
		
		$l_item = '';
		
	
		$l_cnt = 0;


		// add a page
		$pdf->AddPage();
		
		// import page 1
		$tplIdx = $pdf->importPage(1);
		
		// use the imported page and place it at point 0,0 with a width of 210 mm
		$pdf->useTemplate($tplIdx, 0, 0, 297, 210);
		
		
		
		$pdf->SetFont("ArialCE", "B", 10);
		$pdf->SetTextColor(0, 0, 0);
		
		$pdf->SetXY(10, 10);
		$pdf->MultiCell(210, 5, _convert($x), 0, "L");
		
		
		
		$pdf->SetFont("ArialCE", "", 9);
		
		$pdf->SetXY(10, 34);
		$pdf->MultiCell(210, 4, _convert('-------------------------------------------------------------------------------'), 0, "L");
		
		$pdf->SetFont("ArialCE", "B", 14);
		
		$pdf->SetXY(10, 38);
		$pdf->MultiCell(290, 7, _convert('REKAPITULACIJA DOBAVLJAČA'), 0, "C");
		
		$pdf->SetFont("ArialCE", "", 10);
		
		$pdf->SetXY(10, 45);
		$pdf->MultiCell(290, 5, _convert('Period od '.$l_datum_1.' do '.$l_datum_2), 0, "C");

		$pdf->SetFont("ArialCE", "", 9);
		
		
		
		if($l_tip_prog == 'm'){
			
			
			$la_header = array('Šifra', 'Naziv artikla', 'NC', 'PC', 'kol', 'izNC', 'izPC');
			$la_width = array(0, 20, 100, 30, 30, 30, 30, 30);
			$la_pos = array('L', 'L', 'L', 'R', 'R', 'R', 'R', 'R');
			
			$pdf->SetFont("ArialCE", "B", 9);
			
			$l_pos = 0;
			for($i = 0; $i < sizeof($la_header); $i++){
				$l_pos += $la_width[$i];
				$pdf->SetXY(10 + $l_pos, 55);
				$pdf->MultiCell($la_width[$i + 1], 5, _convert($la_header[$i]), 0, $la_pos[$i + 1]);
			}
			
			
			$l_row = 0;
			
			$l_group_name = '--..-1-..--';
			
			$x = get_data('select suppliers.name, articles.id, articles.name as article_name, receiving_items.price_buy, '.
				' receiving_items.price_sell, count(receiving_items.amount) as kol, '.
				' sum(receiving_items.price_buy * receiving_items.amount) as izNC, sum(receiving_items.price_sell * receiving_items.amount) as izPC '.
				' from receivings, suppliers, receiving_items, articles '.
				' where receivings.supplier_id = suppliers.id '.
				' and receivings.id = receiving_items.receiving_id '.
				' and receiving_items.article_id = articles.id '.
				' and receivings.date between "'.$l_datum_db_1.'" and "'.$l_datum_db_2.' 23:59:59" '.
				' group by 1, 2, 3, 4, 5 '.
				' order by suppliers.name, articles.name, articles.id ', 1);
				
				
			for($i = 0; $i < sizeof($x['name']); $i++){
				
				
				$l_pos = 0;
				
				if($x['name'][$i] != $l_group_name){
					
					$l_group_name = $x['name'][$i];
					
					$pdf->SetFont("ArialCE", "B", 9);
					
					$l_row++;
					$pdf->SetXY(10, 60 + 5 * $l_row++);
					$pdf->MultiCell(180, 5, _convert('Dobavljač: '.$x['name'][$i]), 'B', 'L');
					
					$pdf->SetFont("ArialCE", "", 9);
				}
				
				
				
				$l_pos += $la_width[0];
				$pdf->SetXY(10 + $l_pos, 60 + 5 * $l_row);
				$pdf->MultiCell($la_width[1], 5, _convert($x['id'][$i]), 'TB', $la_pos[1]);
				
				$l_pos += $la_width[1];
				$pdf->SetXY(10 + $l_pos, 60 + 5 * $l_row);
				$pdf->MultiCell($la_width[2], 5, _convert($x['article_name'][$i]), 'TB', $la_pos[2]);
				
				$l_pos += $la_width[2];
				$pdf->SetXY(10 + $l_pos, 60 + 5 * $l_row);
				$pdf->MultiCell($la_width[3], 5, _convert(number_format($x['price_buy'][$i],2, '.', ' ')), 'TB', $la_pos[3]);
				
				$l_pos += $la_width[3];
				$pdf->SetXY(10 + $l_pos, 60 + 5 * $l_row);
				$pdf->MultiCell($la_width[3], 5, _convert(number_format($x['price_sell'][$i],2, '.', ' ')), 'TB', $la_pos[3]);
				
				$l_pos += $la_width[4];
				$pdf->SetXY(10 + $l_pos, 60 + 5 * $l_row);
				$pdf->MultiCell($la_width[3], 5, _convert(number_format($x['kol'][$i],2, '.', ' ')), 'TB', $la_pos[3]);
				
				$l_pos += $la_width[5];
				$pdf->SetXY(10 + $l_pos, 60 + 5 * $l_row);
				$pdf->MultiCell($la_width[3], 5, _convert(number_format($x['izNC'][$i],2, '.', ' ')), 'TB', $la_pos[3]);
				
				$l_pos += $la_width[6];
				$pdf->SetXY(10 + $l_pos, 60 + 5 * $l_row++);
				$pdf->MultiCell($la_width[3], 5, _convert(number_format($x['izPC'][$i],2, '.', ' ')), 'TB', $la_pos[3]);
				
				
				
				
				if(62 + 6 * $l_row >= 290){
					$pdf->AddPage();
					$l_row = -8;
					
					$pdf->SetFont("ArialCE", "B", 9);
					
					$l_pos = 0;
					for($j = 0; $j < sizeof($la_header); $j++){
						$l_pos += $la_width[$j];
						$pdf->SetXY(10 + $l_pos, 14);
						$pdf->MultiCell($la_width[$j + 1], 5, _convert($la_header[$j]), 0, $la_pos[$j + 1]);
					}
					
					$pdf->SetFont("ArialCE", "", 9);
				}
			}
			
			
			
		}
		if($l_tip_prog == 'u'){
		}
		
		
		
		
		
		
		
		
			
		$l_name = 'rekapitulacija_dobavljaca.pdf';
		$l_dest = $g_path.'app/webroot/res/files/'.$l_name;
		
		$pdf->Output($l_dest, 'F');
		
		
		$fp = @fopen($g_path.'app/webroot/res/files/'.str_replace('.pdf', '.csv', $l_name), 'wb');
		fwrite($fp, $l_xls);
		fclose($fp);
		
		
		return array('path' => $g_path.'app/webroot/res/files/', 'name' => $l_name);
	}
	
	
	function all_rekapitulacija_po_danima($date_start, $date_end){
		
		$g_path = get_item('select value from settings where name = "putanja.abs" ');
		
		$g_v = get_valuta();
		
		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');
		
		$pdf =& new FPDI('L', 'mm', 'A4');
		
		$pdf->AddFont("ArialCE", "B", "6488f5d4657b269b9f5770508e80e299_arialbd.php");
		$pdf->AddFont("ArialCE", "", "332cc83ebee36613c19564e6e776c81b_arial.php");
		
		// set the sourcefile
		$pgNumber = $pdf->setSourceFile($g_path.'app/libs/fpdf/prazni-dokument.pdf');
		
		//$pdf->SetAutoPageBreak(true, 10);
		
		
		$x = get_item('select value from settings where name = "general.name" ').', '.
			get_n_rows(get_item('select value from settings where name = "invoice.header" '), 4);
		
		$x = trim($x)."\nOznaka poslovnog prostora: ".get_item('select value from settings where name = "general.placeid" ');
		
		$la_tmp_1 = explode('-', $date_start);
		$la_tmp_2 = explode('-', $date_end);
		
		$l_datum_1 = $la_tmp_1[0].'.'.$la_tmp_1[1].'.'.$la_tmp_1[2].'.';
		$l_datum_2 = $la_tmp_2[0].'.'.$la_tmp_2[1].'.'.$la_tmp_2[2].'.';
		
		$l_datum_db_1 = $la_tmp_1[2].'-'.$la_tmp_1[1].'-'.$la_tmp_1[0];
		$l_datum_db_2 = $la_tmp_2[2].'-'.$la_tmp_2[1].'-'.$la_tmp_2[0];
		$l_datum_from = $l_datum_db_1.' 00:00:00';
		$l_datum_to = $l_datum_db_2.' 23:59:59';
		
		
		$l_tip_prog = get_item('select value from settings where name = "tipprog" ');
		
		
		
		
		
		$l_item = '';
		
	
		$l_cnt = 0;


		// add a page
		$pdf->AddPage();
		
		// import page 1
		$tplIdx = $pdf->importPage(1);
		
		// use the imported page and place it at point 0,0 with a width of 210 mm
		$pdf->useTemplate($tplIdx, 0, 0, 297, 210);
		
		
		
		$pdf->SetFont("ArialCE", "B", 10);
		$pdf->SetTextColor(0, 0, 0);
		
		$pdf->SetXY(10, 10);
		$pdf->MultiCell(210, 5, _convert($x), 0, "L");
		
		
		
		$pdf->SetFont("ArialCE", "", 9);
		
		$pdf->SetXY(10, 34);
		$pdf->MultiCell(210, 4, _convert('-------------------------------------------------------------------------------'), 0, "L");
		
		$pdf->SetFont("ArialCE", "B", 14);
		
		$pdf->SetXY(10, 38);
		$pdf->MultiCell(290, 7, _convert('REKAPITULACIJA PO DANIMA'), 0, "C");
		
		$pdf->SetFont("ArialCE", "", 10);
		
		$pdf->SetXY(10, 45);
		$pdf->MultiCell(290, 5, _convert('Period od '.$l_datum_1.' do '.$l_datum_2), 0, "C");

		$pdf->SetFont("ArialCE", "", 9);
		
		//$pdf->SetXY(10, 44);
		//$pdf->MultiCell(210, 4, _convert('--------------------------------------------------------------------------------------------------------------------------------------------------------------------'), 0, "L");
		
		
		
		if($l_tip_prog == 'm'){
			$l_table = 'retail_items';
			
			$l_price_start = ' (retail_items.price - feedback_compensation) ';
			$l_pn = ', sum(feedback_compensation) as fc';
		}
	
		if($l_tip_prog == 'u'){
			$l_table = 'sale_items';
			
			$l_price_start = ' sale_items.price ';
			$l_pn = ', 0 as fc';
		}
		
		
		
		$l_pnp_unique = get_item('select tax_2 from '.$l_table.' order by tax_2 desc limit 1 ');
		
		
		
		
		
		
		$l_pdv = get_item('select value from settings where name = "duty.free" ');
		
		if($l_pdv == 'no'){
			$l_pdv = 'false';
		}
		
		if($l_pdv == 'false'){
			
			$l_pdv_koment = get_item('select value from settings where name = "duty.comment" ');
			$l_sum_tax_divide = '1 + (tax_2 / 100)';
			
		}else{
			
			$l_pdv_koment = '';
			$l_sum_tax_divide = '1 + (tax_1 / 100) + (tax_2 / 100)';
			
		}
		
		
		
		
		
		$x = get_data('select mid(sales.date, 1, 10) as date, tax_1, sum('.$l_price_start.' / ('.$l_sum_tax_divide.') ) as price '.$l_pn.
			' from '.$l_table.', sales '.
			' where sales.id = '.$l_table.'.sale_id '.
			' and sales.date between "'.$l_datum_db_1.'" and "'.$l_datum_db_2.' 23:59:59" '.
			' and sale_id is not null '.
			' group by 1, 2 '.
			' order by 1, 2 ', 1);
		
		$la_data = array();
		
		for($i = 0; isset($x['date']) && $i < sizeof($x['date']); $i++){
			
			if(empty($la_data[$x['date'][$i]])){
				$la_data[$x['date'][$i]] = array('0.00' => 0, '5.00' => 0, '13.00' => 0, '25.00' => 0, 'fc' => 0);
			}
			
			if($l_pdv == 'false'){
				$la_data[$x['date'][$i]]['0.00'] += $x['price'][$i];
			}else{
				$la_data[$x['date'][$i]][$x['tax_1'][$i]] += $x['price'][$i];
			}
		}
		
		
		
		$la_pnp_unique = [];
		
		$x = get_data('select mid(sales.date, 1, 10) as date, tax_2, sum('.$l_table.'.price / ('.$l_sum_tax_divide.') ) as price '.
			' from '.$l_table.', sales '.
			' where sales.id = '.$l_table.'.sale_id '.
			' and sales.date between "'.$l_datum_db_1.'" and "'.$l_datum_db_2.' 23:59:59" '.
			' and tax_2 > 0 '.
			' and sale_id is not null '.
			' group by 1, 2 '.
			' order by 1, 2 ', 1);
		
		
		for($i = 0; isset($x['date']) && $i < sizeof($x['date']); $i++){
			
			
			if(isset($x['tax_2']) && isset($x['tax_2'][$i])){
				$la_pnp_unique[$i] = $x['tax_2'][$i];
				$la_data[$x['date'][$i]]['pnp_num'] = $x['tax_2'][$i];
			}
			
			
			if(empty($la_data[$x['date'][$i]])){
				$la_data[$x['date'][$i]] = array('0.00' => 0, '5.00' => 0, '13.00' => 0, '25.00' => 0, 'pnp'.$la_pnp_unique[$i] => 0, 'fc' => 0);
			}
			
			if(empty($la_data[$x['date'][$i]]['pnp'.$la_pnp_unique[$i]])){
				$la_data[$x['date'][$i]]['pnp'.$la_pnp_unique[$i]] = 0;
			}
			$la_data[$x['date'][$i]]['pnp'.$la_pnp_unique[$i]] += $x['price'][$i];
			
			if($l_tip_prog == 'u' && $l_pdv == 'false'){
				$la_data[$x['date'][$i]]['0.00'] -= $x['price'][$i];
			}
			
		}
		
		//p($la_data);
		
		
		if($l_tip_prog == 'm'){
			
			$x = get_data('select mid(sales.date, 1, 10) as date, sum(feedback_compensation) as fc '.
				' from '.$l_table.', sales '.
				' where sales.id = '.$l_table.'.sale_id '.
				' and sales.date between "'.$l_datum_db_1.'" and "'.$l_datum_db_2.' 23:59:59" '.
				' and sale_id is not null '.
				' group by 1 '.
				' order by 1 ', 1);
			
			
			for($i = 0; isset($x['date']) && $i < sizeof($x['date']); $i++){
				
				if(empty($la_data[$x['date'][$i]])){
					$la_data[$x['date'][$i]] = array('0.00' => 0, '5.00' => 0, '13.00' => 0, '25.00' => 0, 'pnp'.$la_pnp_unique[$i] => 0, 'fc' => 0);
				}
				
				$la_data[$x['date'][$i]]['fc'] += $x['fc'][$i];
			}
			
		}
		
		
		
		//p($la_data);
		
		
		
		
		if($l_tip_prog == 'm'){
			if($l_pdv == 'false'){
				$la_header = array('Datum', 'Osn.', 'PN', 'Ukupno');
				$la_width = array(0, 22, 23, 26, 33);
				$la_pos = array('L', 'R', 'R', 'R', 'R');
				$la_column = array('0.00', 'PN');
			}else{
				$la_header = array('Datum', 'Osn. 0%', 'Osn. 5%', 'PDV 5%', 'Osn. 13%', 'PDV 13%', 'Osn. 25%', 'PDV 25%', 'PN', 'Ukupno');
				$la_width = array(0, 22, 17, 27, 17, 37, 27, 37, 27, 20, 27);
				$la_pos = array('L', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R');
				$la_column = array('0.00', '5.00', 'x-5.00', '13.00', 'x-13.00', '25.00', 'x-25.00', 'PN');
			}
		}
	
		if($l_tip_prog == 'u'){
			if($l_pdv == 'false'){
				$la_header = array('Datum', 'Promet bez PNP', 'Osn. PNP', 'PNP', 'Ukupno');
				$la_width = array(0, 20, 30, 30, 20, 25);
				$la_pos = array('L', 'R', 'R', 'R', 'R', 'R');
				$la_column = array('0.00', 'pnp'.$l_pnp_unique, 'x-pnp'.$l_pnp_unique);
			}else{
				$la_header = array('Datum', 'Osn. 0%', 'Osn. 5%', 'PDV 5%', 'Osn. 13%', 'PDV 13%', 'Osn. 25%', 'PDV 25%', 'Osn. PNP', 'PNP', 'Ukupno');
				$la_width = array(0, 20, 20, 25, 20, 35, 20, 35, 20, 30, 20, 25);
				$la_pos = array('L', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R', 'R');
				$la_column = array('0.00', '5.00', 'x-5.00', '13.00', 'x-13.00', '25.00', 'x-25.00', 'pnp'.$l_pnp_unique, 'x-pnp'.$l_pnp_unique);
			}
			
		}
		
		
		$l_xls = '';
		
		
		$pdf->SetFont("ArialCE", "B", 9);
		
		$l_pos = 0;
		for($i = 0; $i < sizeof($la_header); $i++){
			$l_pos += $la_width[$i];
			$pdf->SetXY(10 + $l_pos, 55);
			$pdf->MultiCell($la_width[$i + 1], 5, _convert($la_header[$i]), 0, $la_pos[$i]);
			
			
			$l_xls .= $la_header[$i].';';
		}
		$l_xls .= "\n";
		
		
		$pdf->SetFont("ArialCE", "B", 9);
		
		
		//p($la_data);
		
		
		$la_all = array();
		$la_all_all = 0;
		
		$l_row = 0;
		foreach($la_data as $key => $value){
			$l_pos = 0;
			
			$l_cnt = 0;
			
			$pdf->SetFont("ArialCE", "B", 9);
			
			
			$la_tmp_3 = explode('-', $key);
			$key = $la_tmp_3[2].'.'.$la_tmp_3[1].'.'.$la_tmp_3[0].'.';
			
			
			
			$l_pos += $la_width[$l_cnt];
			$pdf->SetXY(10 + $l_pos, 60 + 5 * $l_row);
			$pdf->MultiCell($la_width[$l_cnt + 1], 5, _convert($key), 'TB', $la_pos[$l_cnt++]);
			
			$l_xls .= $key.';';
			
			$pdf->SetFont("ArialCE", "", 9);
			
			$l_ukup = 0;
			
			for($j = 0; $j < sizeof($la_column); $j++){
			
				$l_pos += $la_width[$l_cnt];
				$pdf->SetXY(10 + $l_pos, 60 + 5 * $l_row);
				
				if(substr($la_column[$j], 0, 2) == 'x-'){
					if(substr($la_column[$j], 2, 3) == 'pnp'){
						// pnp = 2.00%
						//p($la_column[$j]);
						//p($value['pnp_num']);
						//$l_value = floatval(substr($la_column[$j], 5)) * $value[substr($la_column[$j], 2)] / 100;
						$l_value = floatval($value['pnp_num']) * $value['pnp'.$value['pnp_num']] / 100;
					}else{
						//p($la_column[$j]);
						//p($value['pnp_num']);
						//p('-----');
						$l_value = intval(substr($la_column[$j], 2)) * $value[substr($la_column[$j], 2)] / 100;
					}
				}else{
					//p($la_column[$j]);
					if($la_column[$j] == 'PN'){
						$l_value = $value['fc'];
					}else{
						//p($la_column[$j]);
						if(substr($la_column[$j], 0, 3) == 'pnp'){
							$l_value = $value['pnp'.$value['pnp_num']];
						}else{
							$l_value = $value[$la_column[$j]];
						}
					}
				}
				
				if(substr($la_column[$j], 0, 3) != 'pnp' || ($l_tip_prog == 'u' && $l_pdv == 'false')){
					$l_ukup += $l_value;
				}
				
				
				$la_all[$j] += $l_value;
				
				$pdf->MultiCell($la_width[$l_cnt + 1], 5, _convert(sprintf('%01.2f', $l_value)), 'TB', $la_pos[$l_cnt++]);
				
				$l_xls .= sprintf('%01.2f', $l_value).';';
			}
			
			$la_all_all += $l_ukup;
			
			$pdf->SetFont("ArialCE", "B", 9);
			
			$l_pos += $la_width[$l_cnt];
			$pdf->SetXY(10 + $l_pos, 60 + 5 * $l_row);
			$pdf->MultiCell($la_width[$l_cnt + 1], 5, _convert(sprintf('%01.2f', $l_ukup)), 'TB', $la_pos[$l_cnt++]);
			
			$l_xls .= sprintf('%01.2f', $l_ukup).';';
			$l_xls .= "\n";
			
			$l_row++;
			
			
			
			
			if(62 + 6 * $l_row >= 175){
				$pdf->AddPage();
				$l_row = -8;
				
				$pdf->SetFont("ArialCE", "B", 9);
				$l_pos = 0;
				for($i = 0; $i < sizeof($la_header); $i++){
					$l_pos += $la_width[$i];
					$pdf->SetXY(10 + $l_pos, 14);
					$pdf->MultiCell($la_width[$i + 1], 5, _convert($la_header[$i]), 0, $la_pos[$i]);
				}
				
				$pdf->SetFont("ArialCE", "", 9);
			}
			
			
		}
		
		
		
		$pdf->SetFont("ArialCE", "B", 7);
		
		
		$pdf->SetXY(10, 61 + 5 * $l_row);
		$pdf->MultiCell($la_width[1], 5, _convert('UKUPNO EUR'), 0, $la_pos[0]);
			
		$l_pos = $la_width[0];
		
		$pdf->SetFont("ArialCE", "B", 9);
		
		for($i = 1; $i < sizeof($la_header) - 1; $i++){
			$l_pos += $la_width[$i];
			$pdf->SetXY(10 + $l_pos, 61 + 5 * $l_row);
			$pdf->MultiCell($la_width[$i + 1], 5, _convert(sprintf('%01.2f', $la_all[$i - 1])), 0, $la_pos[$i]);
		}
		
		
		$l_last = 1 + sizeof($la_all);
		$l_pos += $la_width[$l_last];
		$pdf->SetXY(10 + $l_pos, 61 + 5 * $l_row);
		$pdf->MultiCell($la_width[$l_last + 1], 5, _convert(sprintf('%01.2f', $la_all_all)), 0, $la_pos[$l_last]);
		
		$l_xls .= sprintf('%01.2f', $la_all_all).';';
		$l_xls .= "\n";
		
		
		
		
		
		if($l_tip_prog == 'm'){
		}
		if($l_tip_prog == 'u'){
		}
		
		
		//p($l_xls);
		
			
		$l_name = 'rekapitulacija_po_danima.pdf';
		$l_dest = $g_path.'app/webroot/res/files/'.$l_name;
		
		$pdf->Output($l_dest, 'F');
		
		
		$fp = @fopen($g_path.'app/webroot/res/files/'.str_replace('.pdf', '.csv', $l_name), 'wb');
		fwrite($fp, $l_xls);
		fclose($fp);
		
		
		return array('path' => $g_path.'app/webroot/res/files/', 'name' => $l_name);
	}
	
	// Funkcija za generiranje PDF ponude za email
	function all_print_offer($offer_id){
		
		$g_path = get_item('select value from settings where name = "putanja.abs" ');
		$g_v = get_valuta();
		
		if(!defined('FPDF_FONTPATH')){
			define('FPDF_FONTPATH', 'font/');
		}
		
		require_once($g_path.'app/libs/fpdf/fpdf.php');
		
		// Dohvati podatke o ponudi
		$offer = get_row('select * from offers where id = '.$offer_id);
		$customer = get_row('select * from customers where id = '.$offer['customer_id']);
		$items = get_rows('select oi.*, a.name as article_name, a.code as article_code from offer_items oi left join articles a on oi.article_id = a.id where oi.offer_id = '.$offer_id);
		
		// Dohvati postavke
		$restName = get_item('select value from settings where name = "general.name"');
		$restPost = get_item('select value from settings where name = "general.post"');
		$restComm = get_item('select value from settings where name = "general.resort"');
		$restStreet = get_item('select value from settings where name = "general.street"');
		$restStrNum = get_item('select value from settings where name = "general.str_num"');
		$restStrNumMark = get_item('select value from settings where name = "general.str_num_mark"');
		$restPlaceid = get_item('select value from settings where name = "general.placeid"');
		$restFooter = get_item('select value from settings where name = "invoice.footer"');
		
		$pdf = new FPDF('P', 'mm', 'A4');
		$pdf->AddPage();
		$pdf->SetFont('Helvetica', 'B', 12);
		
		// Header - naziv firme
		$pdf->Cell(100);
		$pdf->Cell(0, 5, $restName, 0, 2);
		$pdf->SetFont('Helvetica', '', 8);
		$pdf->Cell(0, 5, $restPost.' '.$restComm.', '.$restStreet.' '.$restStrNum.$restStrNumMark, 0, 2);
		$pdf->Cell(0, 5, $restPlaceid, 0, 0);
		
		$pdf->Ln(10);
		
		// Kupac i broj ponude
		$pdf->SetFont('Helvetica', 'B', 12);
		$pdf->Cell(80, 8, $customer['name'], 'LTR', 0);
		$pdf->Cell(20);
		$pdf->SetFont('Helvetica', 'B', 10);
		$pdf->Cell(80, 8, 'Ponuda *'.$offer['number'], 'LTRB', 0, 'C');
		
		$dt = date('d.m.Y', strtotime($offer['created']));
		
		$pdf->Ln();
		$pdf->SetFont('Helvetica', 'B', 8);
		$pdf->Cell(80, 5, '  ', 'LR', 0);
		$pdf->Cell(20);
		$pdf->SetFont('Courier', '', 8);
		$pdf->Cell(80, 5, "Broj dokumenta:    ".$offer['number'], 'LR', 0);
		
		$pdf->Ln();
		$pdf->SetFont('Helvetica', 'B', 8);
		$pdf->Cell(80, 5, $customer['address'].' '.$customer['city'], 'LR', 0);
		$pdf->Cell(20);
		$pdf->SetFont('Courier', '', 8);
		$pdf->Cell(80, 5, "Datum dokumenta:   ".$dt, 'LBR', 0);
		$pdf->Ln();
		
		$pdf->SetFont('Helvetica', '', 8);
		$pdf->Cell(80, 5, "OIB: ".$customer['oib'], 'LRB', 0, 'R');
		$pdf->Ln(10);
		
		// Zaglavlje tablice
		$pdf->SetFont('Helvetica', 'B', 8);
		$pdf->SetFillColor(255, 255, 255);
		$header = array('RB', 'Sifra', 'Naziv artikla/usluge', 'Kolicina', 'PC', 'Rabat %', 'Cijena', 'Vrijednost '.$g_v);
		$w = array(10, 30, 40, 25, 20, 20, 20, 25);
		for($i = 0; $i < count($header); $i++){
			$pdf->Cell($w[$i], 7, $header[$i], 0, 0, 'C', true);
		}
		$pdf->Ln();
		
		// Stavke
		$pdf->SetFont('Helvetica', '', 8);
		$rb = 1;
		$total = 0;
		foreach($items as $item){
			$pdf->Cell($w[0], 6, $rb, 0, 0, 'C');
			$pdf->Cell($w[1], 6, $item['article_code'], 0, 0, 'L');
			$pdf->Cell($w[2], 6, substr($item['article_name'], 0, 25), 0, 0, 'L');
			$pdf->Cell($w[3], 6, $item['quantity'], 0, 0, 'R');
			$pdf->Cell($w[4], 6, number_format($item['price'], 2), 0, 0, 'R');
			$pdf->Cell($w[5], 6, $item['discount'].'%', 0, 0, 'R');
			$discountedPrice = $item['price'] * (1 - $item['discount']/100);
			$pdf->Cell($w[6], 6, number_format($discountedPrice, 2), 0, 0, 'R');
			$lineTotal = $item['quantity'] * $discountedPrice;
			$pdf->Cell($w[7], 6, number_format($lineTotal, 2), 0, 0, 'R');
			$pdf->Ln();
			$total += $lineTotal;
			$rb++;
		}
		
		// Ukupno
		$pdf->Ln(5);
		$pdf->SetFont('Helvetica', 'B', 10);
		$pdf->Cell(165, 8, 'UKUPNO:', 0, 0, 'R');
		$pdf->Cell(25, 8, number_format($total, 2).' '.$g_v, 0, 0, 'R');
		
		// Footer
		$pdf->Ln(20);
		$pdf->Cell(65);
		$pdf->Cell(50, 20, "Obradio", 'B', 0, 'C');
		
		// Spremi PDF
		$l_name = 'ponuda.pdf';
		$l_dest = $g_path.'app/webroot/res/files/'.$l_name;
		$pdf->Output($l_dest, 'F');
		
		return array('path' => $g_path.'app/webroot/res/files/', 'name' => $l_name);
	}
	
	
?>