<?
	error_reporting(E_ERROR | E_WARNING | E_PARSE);
	//error_reporting(E_ALL);
	ini_set('display_errors', '0');
	
	$g_path = 'my/';
	
	include $g_path."u_globals.php";
	include $g_path."html/f.php";
	include $g_path."u_spoj_na_bazu.php";
	include $g_path."u_headers.php";
	include $g_path."u_log.php";
	

	if(!function_exists('get_n_rows')){
		require('get_n_rows_file.php');
	}
	

	if($_GET['a'] == 6){
		$g_path = get_item('select value from settings where name = "putanja.abs" ');
		$g_path_sl = get_item('select value from settings where name = "putanja.http" ');
		
		define("FPDF_FONTPATH", "font/");
		require_once($g_path.'app/libs/fpdf/fpdf.php');
		require_once($g_path.'app/libs/fpdf/fpdi.php');
		
		switch($_GET['b']){
			case 7:
				
				$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($_GET['addition'] == 'Primka'){
					$l_naslov = 'REKAPITULACIJA PRIMKI';
					$l_datum_od = $_POST['primka_od'];
					$l_datum_do = $_POST['primka_do'];
					
					$la_header = array('', 'Broj primke', 'Datum', 'Broj dokumenta', 'Dobavljač', 'NC', 'PC', '');
					$la_width = array(0, 30, 30, 35, 120, 20, 20, 0);
				}
				
				if($_GET['addition'] == 'Annulments'){
					$l_naslov = 'REKAPITULACIJA OTPISA';
					$l_datum_od = $_POST['otpis_od'];
					$l_datum_do = $_POST['otpis_do'];
					
					$la_header = array('', 'Šifra', 'Naziv', '   NC', '   PC', '   NCuk', '   PCuk', '  Prodano', '  Otpisano', '  Dozv.kalo', '   Razlika', 'NC otpisa', 'PDV po NC', '');
					$la_width = array(0, 12, 56, 17, 17, 17, 17, 28, 26, 26, 26, 17, 20, 0);
				}
				
				
				$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';
				
				
				
				
				/* 
				// ovaj kod vise ne treba, izrada ovog izvjesca, prebacena je u file "_all_reports.php" => function all_rekapitreceivings($date_start, $date_end)
				if($_GET['addition'] == 'Primka'){
					$la_value = array('', 'broj_primka', 'document_date', 'number', 'supplier_id', 'price_buy', '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.'" order by document_date desc, id desc ', 1);
					
					$l_nc_ukup = 0;
					$l_pc_ukup = 0;
					for($i = 0; $i < sizeof($x['id']) + 1; $i++){
						
						if($i == (sizeof($x['id']) + 1 - 1)){
							// suma, zadnji red
							$la_header_last = array('', '', '', 'UKUPNO', $l_nc_ukup, $l_pc_ukup);
						}
						
						$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];
								
								$pdf->SetFont("ArialCE", "B", 9);
								$pdf->MultiCell($la_width[$j + 1], 4, _convert($x[$la_value[$j + 1]][$i]), 'TB', 'L');
								
							}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;
										
								}
								
								if($la_value[$j + 1] != ''){
									$pdf->MultiCell($la_width[$j + 1], 5, _convert($x[$la_value[$j + 1]][$i]), 0, "L");
								}
							}
							
							
							
							
							if($j == 4){
								$l_nc_ukup += $x[$la_value[$j + 1]][$i];
							}
							if($j == 5){
								$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 = 'rekapitulacija-primki.pdf';
				}
				*/
				
				
				if($_GET['addition'] == 'Annulments'){
					$la_value = array('', 'sifra', 'naziv', 'NC', 'PC', 'NCuk', 'PCuk', 'prodano', 'otpisano', 'dozvoljeni_kalo', 'razlika', 'nc_otpisa', 'pdv_iznos', '');
					
					$x = get_data('select articles.id as sifra, articles.name as naziv, tax_groups.tax_1, receiving_items.price_buy as NC, receiving_items.price_sell as PC, '.
						' sum(receiving_items.price_buy * receiving_items.amount) as NCuk, sum(receiving_items.price_sell * receiving_items.amount) as PCuk, '.
						' sum(0) as prodano, sum(receiving_items.amount) as otpisano, '.
						' sum(0) as dozvoljeni_kalo, sum(0) as razlika, sum(0) as nc_otpisa, sum(0) as pdv_iznos '.
						' from receivings, receiving_items, articles, tax_groups '.
						' where receivings.id = receiving_items.receiving_id '.
						' and receiving_items.article_id = articles.id '.
						' and articles.tax_group_id = tax_groups.id '.
						' and receivings.type = 1 '.
						' and receivings.posted = 1 '.
						' and receivings.document_date between "'.$l_dat_1.'" and "'.$l_dat_2.'" '.
						' group by articles.id, articles.name, tax_groups.tax_1, receiving_items.price_buy, receiving_items.price_sell '.
						' order by articles.id', 1);
						
					
					//$l_min_rbr = get_item('select min(id) from sales where date >= "'.$l_dat_1.'" ');
					//$l_max_rbr = get_item('select max(id) from sales where date <= "'.$l_dat_2.'" ');
					
					$l_tip_prog = get_item('select value from settings where name = "tipprog" ');
					
					if($l_tip_prog == 'u'){
						$y = get_data('select product_items.article_id, articles.unit, tax_groups.tax_1, articles.dozvoljeni_kalo, sum(product_items.amount) as prodano '.
							' from sales, sale_items, products, product_items, articles, tax_groups '.
							' where product_items.product_id = products.id '.
							' and products.id = sale_items.product_id '.
							' and sale_items.sale_id = sales.id '.
							' and product_items.article_id = articles.id '.
							' and products.tax_group_id = tax_groups.id '.
							//' and sales.id between '.$l_min_rbr.' and '.$l_max_rbr.
							' and sales.date between "'.$l_dat_1.'" and "'.$l_dat_2.'" '.
							// ' and sales.closed = 1 '. 		//uvjet izbacen jer je EDI tako rekao (ne treba za otpise)... mozda ce ovo trebati i vratiti, ali ne treba dok on ne javi
							' group by product_items.article_id, articles.unit, tax_groups.tax_1, articles.dozvoljeni_kalo '.
							' order by product_items.article_id' );
					}
					
					if($l_tip_prog == 'm'){
						$y = get_data('select retail_items.article_id, articles.unit, retail_items.tax_1, articles.dozvoljeni_kalo, sum(retail_items.amount) as prodano '.
							' from sales, retail_items, articles '.
							' where retail_items.sale_id = sales.id '.
							' and retail_items.article_id = articles.id '.
							//' and sales.id between '.$l_min_rbr.' and '.$l_max_rbr.
							' and sales.date between "'.$l_dat_1.'" and "'.$l_dat_2.'" '.
							//' and sales.closed = 1 '. 		//uvjet izbacen jer je EDI tako rekao (ne treba za otpise)... mozda ce ovo trebati i vratiti, ali ne treba dok on ne javi
							' group by retail_items.article_id, articles.unit, retail_items.tax_1, articles.dozvoljeni_kalo '.
							' order by retail_items.article_id' );
					}
					
					
					
					$la_sum = array();
					$la_sum_sum = array();
					
					$x['sifra'] []= '';
					
					$l_nc_ukup = 0;
					$l_pc_ukup = 0;
					$l_pdv_ukup = 0;
					
					$l_sum_cnt = 0;
					
					for($i = 0; $i < sizeof($x['sifra']) + 1; $i++){
						
						
						if($i < sizeof($x['sifra']) - 1){
							// provjeri je li i sljedeci red ista sifra artikla... ako je, dodaj sve u sumarni red, a trenutni ne ispisuj
							
							if($x['sifra'][$i] == $x['sifra'][$i + 1]){
								$la_sum['NC'][$l_sum_cnt] = $x['NC'][$i];
								$la_sum['PC'][$l_sum_cnt] = $x['PC'][$i];
								$la_sum['otpisano'][$l_sum_cnt] = $x['otpisano'][$i];
								$l_sum_cnt++;
								
								continue;
							}else{
								
								if(sizeof($la_sum['NC']) > 0){
									$la_sum['NC'][$l_sum_cnt] = $x['NC'][$i];
									$la_sum['PC'][$l_sum_cnt] = $x['PC'][$i];
									$la_sum['otpisano'][$l_sum_cnt] = $x['otpisano'][$i];
									$l_sum_cnt++;
								
									for($j = 0; $j < sizeof($la_sum['NC']); $j++){
										$la_sum_sum['NC'] += $la_sum['NC'][$j] * $la_sum['otpisano'][$j];
										$la_sum_sum['PC'] += $la_sum['PC'][$j] * $la_sum['otpisano'][$j];
										$la_sum_sum['otpisano'] += $la_sum['otpisano'][$j];
									}
									$x['NC'][$i] = $la_sum_sum['NC'] / $la_sum_sum['otpisano'];
									$x['NCuk'][$i] = $la_sum_sum['NC'];
									$x['PC'][$i] = $la_sum_sum['PC'] / $la_sum_sum['otpisano'];
									$x['PCuk'][$i] = $la_sum_sum['PC'];
									$x['otpisano'][$i] = $la_sum_sum['otpisano'];
								}
								
								$la_sum = array();
								$la_sum_sum = array();
								$l_sum_cnt = 0;
							}
							
						}
						
						
						if($i == (sizeof($x['sifra']) + 1 - 1)){
							// suma, zadnji red
							$la_header_last = array('', '', '', 'UKUPNO', sprintf('%01.2f', $l_nc_ukup), sprintf('%01.2f', $l_pc_ukup), '', '', '', 'PDV', 'za uplatu:', sprintf('%01.2f', $l_pdv_ukup), '', '');
						}
						
						$l_pos = 0;
						$l_prodano = 0;
						$l_unit = '';
						$l_razlika = 0;
						$l_kalo_posto = 0;
						$l_pdv = 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['sifra']) + 1 - 1)){
								// suma, zadnji red
								
								if($j == sizeof($la_value) - 2){
									continue;
								}
								
								$x[$la_value[$j + 1]][$i - 1] = $la_header_last[$j];
								
								$pdf->SetFont("ArialCE", "B", 9);
								$pdf->MultiCell($la_width[$j + 1], 4, _convert($x[$la_value[$j + 1]][$i - 1]), 'TB', 'R');
								
								
							}else{
								
								switch($j){
									case 1:
										$l_align = 'L';
										$pdf->SetFont("ArialCE", "", 8);
										break;
										
									default:
										$pdf->SetFont("ArialCE", "", 9);
										$l_align = 'R';
								}
								
								switch($j){
									case 2:
									case 3:
									case 4:
									case 5:
										$x[$la_value[$j + 1]][$i] = sprintf('%01.2f', $x[$la_value[$j + 1]][$i]);
										break;
										
									case 6:
										// prodano
										for($k = 0; $k < sizeof($y['article_id']); $k++){
											if($x['sifra'][$i] == $y['article_id'][$k] && isset($y['prodano'][$k])){
												$x[$la_value[$j + 1]][$i] = sprintf('%01.4f', $y['prodano'][$k]).' '.$y['unit'][$k];
												$l_prodano = $y['prodano'][$k];
												$l_unit = $y['unit'][$k];
												//$l_pdv = $y['tax_1'][$k];
												$l_kalo_posto = $y['dozvoljeni_kalo'][$k];
												break;
											}
										}
										
										//exit;
										break;
										
									case 7:
										// otpisano
										$x[$la_value[$j + 1]][$i] = sprintf('%01.4f', $x[$la_value[$j + 1]][$i]).' '.$l_unit;
										break;
										
									case 8:
										// dozvoljeni kalo
										$x[$la_value[$j + 1]][$i] = sprintf('%01.4f', ($l_kalo_posto * $l_prodano / 100)).' '.$l_unit;
										break;
										
									case 9:
										// razlika
										$l_razlika = $x[$la_value[8]][$i] - $l_kalo_posto * $l_prodano / 100;
										$x[$la_value[$j + 1]][$i] = sprintf('%01.4f', $l_razlika).' '.$l_unit;
										break;
										
									case 10:
										// NC otpisa
										$x[$la_value[$j + 1]][$i] = sprintf('%01.2f', $l_razlika * $x[$la_value[3]][$i]);
										break;
										
									case 11:
										// PDV iznos
										if($l_razlika <= 0){
											$x[$la_value[$j + 1]][$i] = '0.00';
										}else{
											$l_pdv = $x['tax_1'][$i];
											$x[$la_value[$j + 1]][$i] = sprintf('%01.2f', $l_pdv / 100 * $l_razlika * $x[$la_value[3]][$i]);
										}
										break;
								}
								
								if($la_value[$j + 1] != '' && $i != sizeof($x['sifra']) + 1 - 2){
									$pdf->MultiCell($la_width[$j + 1], 5, _convert($x[$la_value[$j + 1]][$i]), 0, $l_align);
								}
							}
							
							
							
							if($j == 4){
								$l_nc_ukup += $x[$la_value[$j + 1]][$i];
							}
							if($j == 5){
								$l_pc_ukup += $x[$la_value[$j + 1]][$i];
							}
							if($j == 11){
								$l_pdv_ukup += $x[$la_value[$j + 1]][$i];
							}
							
						}
						
						
						//if($i == (sizeof($x['sifra']) + 1 - 1)){
							//exit;
						//}
						
						
						
						
						if($i != sizeof($x['sifra']) + 1 - 2){
							$l_y += 5;
						}
						
						if($i == (sizeof($x['sifra']) + 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 = 'rekapitulacija-otpisa.pdf';
				}
				
				
				break;
				
				
				
				
				
				
			case 10:
				$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);
				$pdf->MultiCell(277, 5, _convert('ZAPISNIK O DODJELJENIM POPUSTIMA KUPCIMA'), 0, "C");
				
				$pdf->SetFont("ArialCE", "B", 8);
				$pdf->SetXY(10, 17);
				$pdf->MultiCell(277, 5, _convert('(zapisnik o promjeni cijena tijekom prodaje)'), 0, "C");
				
				$pdf->SetFont("ArialCE", "B", 8);
				$pdf->SetXY(10, 23);
				$pdf->MultiCell(277, 5, _convert('Period od '.$_POST['primka_od'].' do '.$_POST['primka_do']), 0, "C");
				
				
				$la_header = array('', 'Račun br.', 'Datum', 'Barcode', 'Artikl', 'Kolicina', 'Osnovna cijena', 'Popust %', 'Cijena s popustom', 'PDV', 'Uk PC', 'Uk PC s popustom', 'Popust', '');
				$la_header_align = array('', 'L', 'L', 'L', 'L', 'L', 'L', 'C', 'L', 'L', 'L', 'L', 'L', '');
				$la_width = array(0, 27, 18, 15, 70, 15, 18, 14, 20, 12, 25, 25, 20, 0);
				$la_value = array('', 'number', 'date', 'barcode', 'name', 'amount', 'base_price', 'discount_percent', 'cp', 'tax_1', 'UkPC', 'UkPCP', 'popust', '');
				$la_value_align = array('', 'L', 'L', 'L', 'L', 'C', 'L', 'C', 'L', 'L', 'L', 'L', 'L', '');
				
				$pdf->SetFont("ArialCE", "B", 8);
				
				
				$pdf->SetXY(10, 32);
				$pdf->MultiCell(28, 5, _convert('SKLADIŠTE ROBA'), 'B', "L");
				
				
				$l_pos = 0;
				for($i = 0; $i < sizeof($la_header) - 1; $i++){
					$l_pos += $la_width[$i];
					$pdf->SetXY(10 + $l_pos, 40);
					$pdf->MultiCell($la_width[$i + 1], 3, _convert($la_header[$i + 1]), 0, $la_header_align[$i + 1]);
				}
				
				$pdf->SetFont("ArialCE", "", 8);
				$l_y = 48;
				
				
				$la_tmp = explode('-', $_POST['primka_od']);
				$l_dat_1 = $la_tmp[2].'-'.$la_tmp[1].'-'.$la_tmp[0];
				
				$la_tmp = explode('-', $_POST['primka_do']);
				$l_dat_2 = $la_tmp[2].'-'.$la_tmp[1].'-'.$la_tmp[0].' 23:59:59';
				
				
				
				
				
				
				
				$x = get_data('select retail_items.id, sales.number, sales.date, articles.barcode, articles.name, retail_items.amount, retail_items.base_price, '.
					' retail_items.discount_percent, (1 - retail_items.discount_percent / 100) * retail_items.base_price as cp, tax_1,  '.
					' retail_items.amount * retail_items.base_price as UkPC, '.
					' (1 - retail_items.discount_percent / 100) * retail_items.base_price * retail_items.amount as UkPCP,  '.
					' retail_items.amount * retail_items.base_price * retail_items.discount_percent / 100 as popust '.
					' from sales, retail_items, articles '.
					' where sales.id = retail_items.sale_id '.
					' and retail_items.article_id = articles.id '.
					' and discount_percent > 0 '.
					' and articles.isService = 0 '.
					' and sales.date between "'.$l_dat_1.'" and "'.$l_dat_2.'" '.
					' order by sales.date desc, sales.id desc, retail_items.id desc ', 1);
				
				$l_uk_pc = 0;
				$l_uk_pcp = 0;
				$l_uk_p = 0;
				for($i = 0; $i < sizeof($x['id']) + 1; $i++){
					
					if($i == (sizeof($x['id']) + 1 - 1)){
						// suma, zadnji red
						$la_header_last = array('', '', '', '', '', '', '', 'UKUPNO', '', sprintf('%01.2f', $l_uk_pc).' EUR', sprintf('%01.2f', $l_uk_pcp).' EUR', sprintf('%01.2f', $l_uk_p).' EUR');
					}
					
					$l_pos = 0;
					for($j = 0; $j < sizeof($la_value) - 1; $j++){
						$l_pos += $la_width[$j];
						$pdf->SetXY(10 + $l_pos, $l_y);
						
						
						if($i == (sizeof($x['id']) + 1 - 1)){
							// suma, zadnji red
							$x[$la_value[$j + 1]][$i] = $la_header_last[$j];
							
							$pdf->SetFont("ArialCE", "B", 8);
							$pdf->MultiCell($la_width[$j + 1], 4, _convert($x[$la_value[$j + 1]][$i]), 'TB', 'L');
							
						}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);
									$x[$la_value[$j + 1]][$i] = u_datum($x[$la_value[$j + 1]][$i], 1).'.'.u_mjesec($x[$la_value[$j + 1]][$i], 1).'.'.u_godina($x[$la_value[$j + 1]][$i]);
									break;
									
								case 5:
								case 7:
								case 9:
								case 10:
								case 11:
									$x[$la_value[$j + 1]][$i] = sprintf('%01.2f', $x[$la_value[$j + 1]][$i]).' EUR';
									break;
									
							}
							
							if($la_value[$j + 1] != ''){
								$pdf->MultiCell($la_width[$j + 1], 5, _convert($x[$la_value[$j + 1]][$i]), 0, $la_value_align[$j + 1]);
							}
						}
						
						
						
						
						if($j == 9){
							$l_uk_pc += $x[$la_value[$j + 1]][$i];
						}
						if($j == 10){
							$l_uk_pcp += $x[$la_value[$j + 1]][$i];
						}
						if($j == 11){
							$l_uk_p += $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", 8);
							$l_pos = 0;
							for($k = 0; $k < sizeof($la_header) - 1; $k++){
								$l_pos += $la_width[$k];
								$pdf->SetXY(10 + $l_pos, $l_y);
								$pdf->MultiCell($la_width[$k + 1], 3, _convert($la_header[$k + 1]), 0, "L");
							}
							
							$pdf->SetFont("ArialCE", "", 9);
							$l_y += 7;
						}
					}
				}
				
				
				
				$l_y += 7;
				if($l_y >= 180){
					$pdf->AddPage();
					$l_y = 12;
				}
				
				
				
				$pdf->SetFont("ArialCE", "B", 8);
				
				$pdf->SetXY(10, $l_y);
				$pdf->MultiCell(24, 5, _convert('POPIS USLUGA'), 'B', "L");
				
				
				$l_y += 7;
				if($l_y >= 180){
					$pdf->AddPage();
					$l_y = 12;
				}
				
				
				$l_pos = 0;
				for($i = 0; $i < sizeof($la_header) - 1; $i++){
					$l_pos += $la_width[$i];
					$pdf->SetXY(10 + $l_pos, $l_y);
					$pdf->MultiCell($la_width[$i + 1], 3, _convert($la_header[$i + 1]), 0, $la_header_align[$i + 1]);
				}
				
				$pdf->SetFont("ArialCE", "", 8);
				
				
				$l_y += 8;
				if($l_y >= 180){
					$pdf->AddPage();
					$l_y = 12;
				}
				
				
				$x = get_data('select retail_items.id, sales.number, sales.date, articles.barcode, articles.name, retail_items.amount, retail_items.base_price, '.
					' retail_items.discount_percent, (1 - retail_items.discount_percent / 100) * retail_items.base_price as cp, tax_1,  '.
					' retail_items.amount * retail_items.base_price as UkPC, '.
					' (1 - retail_items.discount_percent / 100) * retail_items.base_price * retail_items.amount as UkPCP,  '.
					' retail_items.amount * retail_items.base_price * retail_items.discount_percent / 100 as popust '.
					' from sales, retail_items, articles '.
					' where sales.id = retail_items.sale_id '.
					' and retail_items.article_id = articles.id '.
					' and discount_percent > 0 '.
					' and articles.isService = 1 '.
					' and sales.date between "'.$l_dat_1.'" and "'.$l_dat_2.'" '.
					' order by sales.date desc, sales.id desc, retail_items.id desc ', 1);
				
				$l_uk_pc = 0;
				$l_uk_pcp = 0;
				$l_uk_p = 0;
				for($i = 0; $i < sizeof($x['id']) + 1; $i++){
					
					if($i == (sizeof($x['id']) + 1 - 1)){
						// suma, zadnji red
						$la_header_last = array('', '', '', '', '', '', '', 'UKUPNO', '', sprintf('%01.2f', $l_uk_pc).' EUR', sprintf('%01.2f', $l_uk_pcp).' EUR', sprintf('%01.2f', $l_uk_p).' EUR');
					}
					
					$l_pos = 0;
					for($j = 0; $j < sizeof($la_value) - 1; $j++){
						$l_pos += $la_width[$j];
						$pdf->SetXY(10 + $l_pos, $l_y);
						
						
						if($i == (sizeof($x['id']) + 1 - 1)){
							// suma, zadnji red
							$x[$la_value[$j + 1]][$i] = $la_header_last[$j];
							
							$pdf->SetFont("ArialCE", "B", 8);
							$pdf->MultiCell($la_width[$j + 1], 4, _convert($x[$la_value[$j + 1]][$i]), 'TB', 'L');
							
						}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);
									$x[$la_value[$j + 1]][$i] = u_datum($x[$la_value[$j + 1]][$i], 1).'.'.u_mjesec($x[$la_value[$j + 1]][$i], 1).'.'.u_godina($x[$la_value[$j + 1]][$i]);
									break;
									
								case 5:
								case 7:
								case 9:
								case 10:
								case 11:
									$x[$la_value[$j + 1]][$i] = sprintf('%01.2f', $x[$la_value[$j + 1]][$i]).' EUR';
									break;
									
							}
							
							if($la_value[$j + 1] != ''){
								$pdf->MultiCell($la_width[$j + 1], 5, _convert($x[$la_value[$j + 1]][$i]), 0, $la_value_align[$j + 1]);
							}
						}
						
						
						
						
						if($j == 9){
							$l_uk_pc += $x[$la_value[$j + 1]][$i];
						}
						if($j == 10){
							$l_uk_pcp += $x[$la_value[$j + 1]][$i];
						}
						if($j == 11){
							$l_uk_p += $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(10 + $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 = 'zapisnik-o-promjeni-cijena-popustima.pdf';
				break;
				
				
				
				
				
				
			case 13:
				$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);
				$pdf->MultiCell(277, 5, _convert('ZAPISNIK - PROMJENA CIJENE ARTIKLA'), 0, "C");
				
				$pdf->SetFont("ArialCE", "B", 8);
				$pdf->SetXY(10, 17);
				
				$pdf->MultiCell(277, 5, _convert('('.get_item('select name from zopc_razlog_promjene where id = "'.$_POST['vrsta'].'" ').')'), 0, "C");
				
				$pdf->SetFont("ArialCE", "B", 8);
				$pdf->SetXY(10, 23);
				$pdf->MultiCell(277, 5, _convert('Period od '.$_POST['primka_od'].' do '.$_POST['primka_do']), 0, "C");
				
				
				$la_header = array('', 'Datum', 'Barcode', 'Sifra artikla', 'Artikl', 'Kolicina', 'Stara prod. cijena', 'Stara prod. vrijednost', 
					'Nova prod. cijena', 'Nova prod. vrijednost', 'Razlika', '');
				$la_header_align = array('', 'L', 'L', 'L', 'L', 'L', 'L', 'C', 'L', 'L', 'L', '');
				$la_width = array(0, 27, 18, 13, 70, 20, 20, 20, 20, 20, 25, 0);
				$la_value = array('', 'date', 'barcode', 'article_id', 'name', 'amount', 'price_sell_old', 'price_old', 'price_sell_new', 'price_new', 'razlika', '');
				$la_value_align = array('', 'L', 'L', 'L', 'L', 'C', 'L', 'C', 'L', 'L', 'L', '');
				
				$pdf->SetFont("ArialCE", "B", 8);
				$l_pos = 0;
				for($i = 0; $i < sizeof($la_header) - 1; $i++){
					$l_pos += $la_width[$i];
					$pdf->SetXY(10 + $l_pos, 32);
					$pdf->MultiCell($la_width[$i + 1], 3, _convert($la_header[$i + 1]), 0, $la_header_align[$i + 1]);
				}
				
				$pdf->SetFont("ArialCE", "", 8);
				$l_y = 40;
				
				
				$la_tmp = explode('-', $_POST['primka_od']);
				$l_dat_1 = $la_tmp[2].'-'.$la_tmp[1].'-'.$la_tmp[0];
				
				$la_tmp = explode('-', $_POST['primka_do']);
				$l_dat_2 = $la_tmp[2].'-'.$la_tmp[1].'-'.$la_tmp[0].' 23:59:59';
				
				$x = get_data('select zopc.datum, articles.barcode, zopc.article_id, articles.name, zopc.amount, zopc.price_sell_old, zopc.price_old, '.
					' zopc.price_sell_new, zopc.price_new, zopc.razlika '.
					' from zopc, articles  '.
					' where zopc.article_id = articles.id  '.
					' and zopc_razlog_promjene_id = '.$_POST['vrsta'].
					' and zopc.datum between "'.$l_dat_1.'" and "'.$l_dat_2.'" '.
					' order by zopc.datum desc, articles.name', 1);
				
				//pe($x);
				
				$l_uk_pc = 0;
				for($i = 0; $i < sizeof($x['datum']) + 1; $i++){
					
					if($i == (sizeof($x['datum']) + 1 - 1)){
						// suma, zadnji red
						$la_header_last = array('', '', '', '', '', '', '', 'UKUPNO', '', sprintf('%01.2f', $l_uk_pc).' EUR');
					}
					
					$l_pos = 0;
					for($j = 0; $j < sizeof($la_value) - 1; $j++){
						$l_pos += $la_width[$j];
						$pdf->SetXY(10 + $l_pos, $l_y);
						
						
						if($i == (sizeof($x['datum']) + 1 - 1)){
							// suma, zadnji red
							$x[$la_value[$j + 1]][$i] = $la_header_last[$j];
							
							$pdf->SetFont("ArialCE", "B", 8);
							$pdf->MultiCell($la_width[$j + 1], 4, _convert($x[$la_value[$j + 1]][$i]), 'TB', 'L');
							
						}else{
							switch($j){
								case 0:
									//$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);
									$x[$la_value[$j + 1]][$i] = u_datum($x[$la_value[$j + 1]][$i], 1).'.'.u_mjesec($x[$la_value[$j + 1]][$i], 1).'.'.u_godina($x[$la_value[$j + 1]][$i]);
									break;
									
								case 5:
								case 7:
								case 9:
								case 10:
								case 11:
									$x[$la_value[$j + 1]][$i] = sprintf('%01.2f', $x[$la_value[$j + 1]][$i]).' EUR';
									break;
									
							}
							
							if($la_value[$j + 1] != ''){
								$pdf->MultiCell($la_width[$j + 1], 5, _convert($x[$la_value[$j + 1]][$i]), 0, $la_value_align[$j + 1]);
							}
						}
						
						
						
						
						if($j == 9){
							$l_uk_pc += $x[$la_value[$j + 1]][$i];
						}
					}
					$l_y += 5;
					
					if($i == (sizeof($x['datum']) + 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(10 + $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 = 'zapisnik-promjena-cijene-artikla.pdf';
				break;
				
				
				
				
				
				
				
				
			case 12:
				$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);
				$pdf->MultiCell(277, 5, _convert('KNJIGA POPISA'), 0, "C");
				
				
				$la_kp_dod = array(
					'Primke',
					'Povratnice',
					'Medjuskladisnice',
					'Izdatnice',
					'Otpisi',
					'Zapisnik o promjeni cijene (Ulazne kalkulacije)',
					'Inventure',
					'Dnevni promet',
					'Zapisnik o promjeni cijene (Promjena cijene)',
					'Knjiga prometa usluga',
				);
				
				
				switch($_POST['knjiga_popisa_radio']){
					case 1:
						$l_podnaslov = 'Komplet knjiga popisa';
						break;
						
					case 2:
						$l_podnaslov = 'Knjiga popisa';
						break;
						
					case 3:
						$l_podnaslov = $la_kp_dod[$_POST['kp_dokument_radio'] - 1];
						break;
				}
				
				
				$l_podnaslov_2 = '';
				if($_POST['kp_promet_roba_usluga'] == 1){
						$l_podnaslov_2 = ' - robe';
				}
				if($_POST['kp_promet_roba_usluga'] == 2){
						$l_podnaslov_2 = ' - usluge';
				}
				
				
				$pdf->SetFont("ArialCE", "B", 8);
				$pdf->SetXY(10, 17);
				$pdf->MultiCell(277, 5, _convert('('.$l_podnaslov.')'.$l_podnaslov_2), 0, "C");
				
				
				if($_POST['knjiga_popisa_radio'] == 3){
					$la_kp_dod_uvjet = array(
						' and 1 = 0 ',
						' and 1 = 0 ',
						' and 1 = 0 ',
						' and 1 = 0 ',
						' and 1 = 0 ',
						' and 1 = 0 ',
						' and 1 = 0 ',
						' and 1 = 0 ',
						' and 1 = 0 ',
						' and 1 = 0 ',
					);
					$la_kp_dod_uvjet[$_POST['kp_dokument_radio'] - 1] = '';
				}else{
					$la_kp_dod_uvjet = array(
						'',
						'',
						'',
						'',
						'',
						'',
						'',
						'',
						'',
						'',
					);
					// ovaj uvjet prolazi samo kod izvjesca "Pregled odredjenog dokumenta => Knjiga prometa usluga", inace se nikad ne uzima u obzir
					$la_kp_dod_uvjet[9] = ' and 1 = 0 ';
				}
				
				if($_POST['knjiga_popisa_radio'] == 2){
					$l_vrijeme_str = 'Period od '.$_POST['primka_od'].' do '.$_POST['primka_do'];
				}else{
					$l_vrijeme_str = 'Cijelo razdoblje';
				}
				$pdf->SetFont("ArialCE", "B", 8);
				$pdf->SetXY(10, 23);
				$pdf->MultiCell(277, 5, _convert($l_vrijeme_str), 0, "C");
				
				
				
				$la_header = array('', 'R.br.', 'Datum', 'Opis knjiženja', 'Vezani dokument', 'Nabava (EUR)', 'Prodaja (EUR)', '');
				$la_header_align = array('', 'R', 'C', 'L', 'L', 'R', 'R', '');
				$la_width = array(0, 10, 27, 90, 60, 25, 25, 0);
				//$la_value = array('', 'number', 'date', 'article_id', 'name', 'amount', 'base_price', 'discount_percent', 'cp', 'tax_1', 'UkPC', 'UkPCP', 'popust', '');
				$la_value_align = array('', 'R', 'C', 'L', 'L', 'R', 'R', '');
				
				$pdf->SetFont("ArialCE", "B", 8);
				$l_pos = 0;
				for($i = 0; $i < sizeof($la_header) - 1; $i++){
					$l_pos += $la_width[$i];
					$pdf->SetXY(10 + $l_pos, 32);
					$pdf->MultiCell($la_width[$i + 1], 3, _convert($la_header[$i + 1]), 0, $la_header_align[$i + 1]);
				}
				
				
				$pdf->SetFont("ArialCE", "", 8);
				$l_y = 40;
				
				
				
				if($_POST['knjiga_popisa_radio'] == 2){
					
					$la_tmp = explode('-', $_POST['primka_od']);
					$l_dat_1 = $la_tmp[2].'-'.sprintf('%02d', $la_tmp[1]).'-'.sprintf('%02d', $la_tmp[0]);
					
					$la_tmp = explode('-', $_POST['primka_do']);
					$l_dat_2 = $la_tmp[2].'-'.sprintf('%02d', $la_tmp[1]).'-'.sprintf('%02d', $la_tmp[0]).' 23:59:59';
					
					$l_datum = ' and datum between "'.$l_dat_1.'" and "'.$l_dat_2.'" ';
					
				}else{
					
					if($_POST['knjiga_popisa_radio'] == 3){
						
						$la_tmp = explode('-', $_POST['dokument_primka_od']);
						$l_dat_1 = $la_tmp[2].'-'.sprintf('%02d', $la_tmp[1]).'-'.sprintf('%02d', $la_tmp[0]);
						
						$la_tmp = explode('-', $_POST['dokument_primka_do']);
						$l_dat_2 = $la_tmp[2].'-'.sprintf('%02d', $la_tmp[1]).'-'.sprintf('%02d', $la_tmp[0]).' 23:59:59';
						
						$l_datum = ' and datum between "'.$l_dat_1.'" and "'.$l_dat_2.'" ';
						
					}else{
						
						
						$l_datum = '';
						
						
					}
				}
				
				//pe($_POST);
				
				$result = array();
				
				
				
				
				
				
				if($_POST['knjiga_popisa_radio'] == 2){
					$la_donos = array();
					
					$l_datum_2 = ' and datum < "'.$l_dat_1.'" ';
					
					$l_sql = 
					// 1 - Primka Kalkulacija
					'select mid(document_date, 1, 10) as datum, price_sell as razlika, concat("Primka broj ", broj_primka) as vezani_dokument, "Primka kalkulacija" as opis, "1" as rbr '.
						' from receivings '.
						' where type = 0 '.
						' and posted = 1 '.
						str_replace(' and datum ', ' and document_date ', $l_datum_2).
						
					/*
					' union all '.
					// 2 - Zapisnik o promjeni cijene (1) - Popust kupcu
					'select mid(sales.date, 1, 10) as datum, sum(retail_items.amount * retail_items.base_price * retail_items.discount_percent / 100) as razlika, concat("Racun broj ", sales.number) as vezani_dokument, "Zapisnik o promjeni cijene (1) - Dodjelom popusta" as opis, "6" as rbr '.
						' from sales, retail_items, articles '.
						' where sales.id = retail_items.sale_id '.
						' and retail_items.article_id = articles.id '.
						' and discount_percent > 0  '.
						str_replace(' and datum ', ' and sales.date ', $l_datum_2).
						' group by mid(sales.date, 1, 10) '.
						' having sum(retail_items.amount * retail_items.base_price * retail_items.discount_percent / 100) <> 0 '.
						
					' union all '.
					// 3 - Zapisnik o promjeni cijene (2) - Ulazna kalkulacija
					'select mid(datum, 1, 10) as datum, sum(razlika) as razlika, concat("Zapisnik o promjeni cijene broj #REPLACE-RBR#") as vezani_dokument, "Zapisnik o promjeni cijene (2) - Promjena cijene" as opis, "7" as rbr '.
						' from zopc '.
						' where zopc_razlog_promjene_id = 2 '.
						$l_datum_2.
						' group by mid(datum, 1, 10) '.
						' having sum(razlika) <> 0 '.
						
					' union all '.
					// 4 - Zapisnik o promjeni cijene (3) - Promjena cijene na skladištu
					'select mid(datum, 1, 10) as datum, sum(razlika) as razlika, concat("Promjena cijene na skladištu broj #REPLACE-RBR#") as vezani_dokument, "Zapisnik o promjeni cijene (3) - Promjena cijene na skladištu" as opis, "8" as rbr '.
						' from zopc '.
						' where zopc_razlog_promjene_id = 3 '.
						$l_datum_2.
						' group by mid(datum, 1, 10) '.
						' having sum(razlika) <> 0 '.
						
					*/
						
					' union all '.
					// 2 - Zapisnik o promjeni cijene (1) - Popust kupcu
					'select mid(sales.date, 1, 10) as datum, retail_items.amount * retail_items.base_price * retail_items.discount_percent / 100 as razlika, concat("Racun broj ", sales.number) as vezani_dokument, "Zapisnik o promjeni cijene (1) - Dodjelom popusta" as opis, "6" as rbr '.
						' from sales, retail_items, articles '.
						' where sales.id = retail_items.sale_id '.
						' and retail_items.article_id = articles.id '.
						' and discount_percent > 0  '.
						' and articles.isService = 0  '.
						str_replace(' and datum ', ' and sales.date ', $l_datum_2).
						
						
					' union all '.
					// 3 - Zapisnik o promjeni cijene (2) - Ulazna kalkulacija
					'select mid(datum, 1, 10) as datum, razlika, concat("Zapisnik o promjeni cijene broj #REPLACE-RBR#") as vezani_dokument, "Zapisnik o promjeni cijene (2) - Promjena cijene" as opis, "7" as rbr '.
						' from zopc '.
						' where zopc_razlog_promjene_id = 2 '.
						$l_datum_2.
						' and razlika <> 0 '.
						
					' union all '.
					// 4 - Zapisnik o promjeni cijene (3) - Promjena cijene na skladištu
					'select mid(datum, 1, 10) as datum, razlika, concat("Promjena cijene na skladištu broj #REPLACE-RBR#") as vezani_dokument, "Zapisnik o promjeni cijene (3) - Promjena cijene na skladištu" as opis, "8" as rbr '.
						' from zopc '.
						' where zopc_razlog_promjene_id = 3 '.
						$l_datum_2.
						' and razlika <> 0 '.
						
					' union all '.
					// 5- Otpis
					'select mid(document_date, 1, 10) as datum, price_sell as razlika, concat("Otpis broj ", broj_otpis) as vezani_dokument, "Otpis" as opis, "2" as rbr '.
						' from receivings '.
						' where type = 1 '.
						' and posted = 1 '.
						str_replace(' and datum ', ' and document_date ', $l_datum_2).
						
					' union all '.
					// 6- Povrat
					'select mid(document_date, 1, 10) as datum, price_sell as razlika, concat("Povratnica broj ", broj_povratnica) as vezani_dokument, "Povratnica" as opis, "3" as rbr '.
						' from receivings '.
						' where type = 2 '.
						' and posted = 1 '.
						str_replace(' and datum ', ' and document_date ', $l_datum_2).
						
					' union all '.
					// 7- Inventura
					'select mid(date, 1, 10) as datum, price_sell as razlika, concat("Inventura broj ", broj_inventura) as vezani_dokument, "Inventura" as opis, "5" as rbr '.
						' from receivings '.
						' where type = 4 '.
						' and posted = 1 '.
						str_replace(' and datum ', ' and date ', $l_datum_2).
						
					' union all '.
					// 8- Medjuskladisnica
					'select mid(document_date, 1, 10) as datum, price_sell as razlika, concat("Medjuskladisnica broj ", broj_medjuskladisnica) as vezani_dokument, "Medjuskladisnica" as opis, "4" as rbr '.
						' from receivings '.
						' where type = 3 '.
						' and posted = 1 '.
						str_replace(' and datum ', ' and document_date ', $l_datum_2).
						
					' union all '.
					// 9- Dnevni promet
					'select mid(sales.date, 1, 10) as datum, sum(retail_items.price) as razlika, '.
						' concat("Dnevni promet ", if(articles.isService = 0, "roba ", "usluga "), DATE_FORMAT(mid(sales.date, 1, 10), "%d.%m.%Y.")) as vezani_dokument, '.
						' concat("Dnevni promet ", if(articles.isService = 0, "roba ", "usluga ")) as opis, "9" as rbr '.
						' from sales, retail_items, articles '.
						' where sales.id = retail_items.sale_id '.
						' and retail_items.article_id = articles.id '.
						str_replace(' and datum ', ' and sales.date ', $l_datum_2).
						' group by mid(sales.date, 1, 10), articles.isService  '.
						' having sum(sales.price) <> 0 '.
						
					
					' order by datum ';
					
					$y = get_data($l_sql, 1);
					
					//p($y);
					
					
					for($i = 0; $i < sizeof($y['datum']); $i++){
						$la_donos[$i] = array(
							'datum' => u_datum($y['datum'][$i], 1).'-'.u_mjesec($y['datum'][$i], 1).'-'.u_godina($y['datum'][$i]),
							'opis' => $y['opis'][$i],
							'dokument' => $y['vezani_dokument'][$i],
							'nabava' => '0',
							'prodaja' => '0',
							'razlika' => $y['razlika'][$i],
							'rbr' => $y['rbr'][$i],
						);
					}
					for($i = 0; $i < sizeof($y['datum']); $i++){
						switch($la_donos[$i]['rbr']){
							case '9':
								$la_donos[$i]['prodaja'] = $la_donos[$i]['razlika'];
								break;
							
							default:
								$la_donos[$i]['nabava'] = $la_donos[$i]['razlika'];
						}
						$la_donos[$i]['razlika'] = '';
					}
					
					for($i = 0; $i < sizeof($y['datum']); $i++){
						switch($la_donos[$i]['rbr']){
							case '6':
								$la_donos[$i]['nabava'] = -1 * $la_donos[$i]['nabava'];
								break;
							/*
							case '7':
							case '8':
							*/
							
							case '2':
							case '3':
							case '4':
								$la_donos[$i]['nabava'] = '-'.abs($la_donos[$i]['nabava']);
								break;
						}
					}
					
					$la_cnt = array(0, 0, 0, 0, 0);
					for($i = 0; $i < sizeof($y['datum']); $i++){
						switch($la_donos[$i]['rbr']){
							case '6':
							case '7':
							case '8':
								$la_cnt[$la_donos[$i]['rbr']]++;
								$la_donos[$i]['dokument'] = str_replace('#REPLACE-RBR#', $la_cnt[$la_donos[$i]['rbr']], $la_donos[$i]['dokument']);
								break;
						}
					}
					//pe($la_donos);
					
					
					$la_donos_ukup_roba = array(
						'datum' => $la_donos[sizeof($la_donos) - 1]['datum'],
						'nabava' => 0,
						'prodaja' => 0,
					);
					$la_donos_ukup_usluge = array(
						'datum' => $la_donos[sizeof($la_donos) - 1]['datum'],
						'nabava' => 0,
						'prodaja' => 0,
					);
					for($i = 0; $i < sizeof($la_donos); $i++){
						if(strstr($la_donos[$i]['opis'], 'uslug')){
							$la_donos_ukup_usluge['nabava'] += $la_donos[$i]['nabava'];
							$la_donos_ukup_usluge['prodaja'] += $la_donos[$i]['prodaja'];
						}else{
							$la_donos_ukup_roba['nabava'] += $la_donos[$i]['nabava'];
							$la_donos_ukup_roba['prodaja'] += $la_donos[$i]['prodaja'];
						}
					}
				}else{
					$la_donos_ukup_roba = array(
						'datum' => '',
						'nabava' => 0,
						'prodaja' => 0,
					);
					$la_donos_ukup_usluge = array(
						'datum' => '',
						'nabava' => 0,
						'prodaja' => 0,
					);
				}
				
				
				
				
				
				
				
				
				$l_sql = 
				// 1 - Primka Kalkulacija
				'select mid(document_date, 1, 10) as datum, price_sell as razlika, concat("Primka broj ", broj_primka) as vezani_dokument, "Primka kalkulacija" as opis, "1" as rbr '.
					' from receivings '.
					' where type = 0 '.
					' and posted = 1 '.
					str_replace(' and datum ', ' and document_date ', $l_datum).
					$la_kp_dod_uvjet[0].
					
				/*
				' union all '.
				// 2 - Zapisnik o promjeni cijene (1) - Popust kupcu
				'select mid(sales.date, 1, 10) as datum, sum(retail_items.amount * retail_items.base_price * retail_items.discount_percent / 100) as razlika, concat("Racun broj ", sales.number) as vezani_dokument, "Zapisnik o promjeni cijene (1) - Dodjelom popusta" as opis, "6" as rbr '.
					' from sales, retail_items, articles '.
					' where sales.id = retail_items.sale_id '.
					' and retail_items.article_id = articles.id '.
					' and discount_percent > 0  '.
					str_replace(' and datum ', ' and sales.date ', $l_datum).
					$la_kp_dod_uvjet[3].
					' group by mid(sales.date, 1, 10) '.
					' having sum(retail_items.amount * retail_items.base_price * retail_items.discount_percent / 100) <> 0 '.
					
				' union all '.
				// 3 - Zapisnik o promjeni cijene (2) - Ulazna kalkulacija
				'select mid(datum, 1, 10) as datum, sum(razlika) as razlika, concat("Zapisnik o promjeni cijene broj #REPLACE-RBR#") as vezani_dokument, "Zapisnik o promjeni cijene (2) - Promjena cijene" as opis, "7" as rbr '.
					' from zopc '.
					' where zopc_razlog_promjene_id = 2 '.
					$l_datum.
					$la_kp_dod_uvjet[8].
					' group by mid(datum, 1, 10) '.
					' having sum(razlika) <> 0 '.
					
				' union all '.
				// 4 - Zapisnik o promjeni cijene (3) - Promjena cijene na skladištu
				'select mid(datum, 1, 10) as datum, sum(razlika) as razlika, concat("Promjena cijene na skladištu broj #REPLACE-RBR#") as vezani_dokument, "Zapisnik o promjeni cijene (3) - Promjena cijene na skladištu" as opis, "8" as rbr '.
					' from zopc '.
					' where zopc_razlog_promjene_id = 3 '.
					$l_datum.
					$la_kp_dod_uvjet[5].
					' group by mid(datum, 1, 10) '.
					' having sum(razlika) <> 0 '.
				*/
					
					
				' union all '.
				'select mid(sales.date, 1, 10) as datum, retail_items.amount * retail_items.base_price * retail_items.discount_percent / 100 as razlika, concat("Racun broj ", sales.number) as vezani_dokument, "Zapisnik o promjeni cijene (1) - Dodjelom popusta" as opis, "6" as rbr '.
					' from sales, retail_items, articles '.
					' where sales.id = retail_items.sale_id '.
					' and retail_items.article_id = articles.id '.
					' and discount_percent > 0  '.
					' and articles.isService = 0  '.
					str_replace(' and datum ', ' and sales.date ', $l_datum).
					$la_kp_dod_uvjet[3].
					
					
				' union all '.
				// 3 - Zapisnik o promjeni cijene (2) - Ulazna kalkulacija
				'select mid(datum, 1, 10) as datum, razlika, concat("Zapisnik o promjeni cijene broj #REPLACE-RBR#") as vezani_dokument, "Zapisnik o promjeni cijene (2) - Promjena cijene" as opis, "7" as rbr '.
					' from zopc '.
					' where zopc_razlog_promjene_id = 2 '.
					$l_datum.
					$la_kp_dod_uvjet[8].
					' and razlika <> 0 '.
					
				' union all '.
				// 4 - Zapisnik o promjeni cijene (3) - Promjena cijene na skladištu
				'select mid(datum, 1, 10) as datum, razlika, concat("Promjena cijene na skladištu broj #REPLACE-RBR#") as vezani_dokument, "Zapisnik o promjeni cijene (3) - Promjena cijene na skladištu" as opis, "8" as rbr '.
					' from zopc '.
					' where zopc_razlog_promjene_id = 3 '.
					$l_datum.
					$la_kp_dod_uvjet[5].
					' and razlika <> 0 '.
					
				' union all '.
				// 5- Otpis
				'select mid(document_date, 1, 10) as datum, price_sell as razlika, concat("Otpis broj ", broj_otpis) as vezani_dokument, "Otpis" as opis, "2" as rbr '.
					' from receivings '.
					' where type = 1 '.
					' and posted = 1 '.
					str_replace(' and datum ', ' and document_date ', $l_datum).
					$la_kp_dod_uvjet[4].
					
				' union all '.
				// 6- Povrat
				'select mid(document_date, 1, 10) as datum, price_sell as razlika, concat("Povratnica broj ", broj_povratnica) as vezani_dokument, "Povratnica" as opis, "3" as rbr '.
					' from receivings '.
					' where type = 2 '.
					' and posted = 1 '.
					str_replace(' and datum ', ' and document_date ', $l_datum).
					$la_kp_dod_uvjet[1].
					
				' union all '.
				// 7- Inventura
				'select mid(date, 1, 10) as datum, price_sell as razlika, concat("Inventura broj ", broj_inventura) as vezani_dokument, "Inventura" as opis, "5" as rbr '.
					' from receivings '.
					' where type = 4 '.
					' and posted = 1 '.
					str_replace(' and datum ', ' and date ', $l_datum).
					$la_kp_dod_uvjet[6].
					
				' union all '.
				// 8- Medjuskladisnica
				'select mid(document_date, 1, 10) as datum, price_sell as razlika, concat("Medjuskladisnica broj ", broj_medjuskladisnica) as vezani_dokument, "Medjuskladisnica" as opis, "4" as rbr '.
					' from receivings '.
					' where type = 3 '.
					' and posted = 1 '.
					str_replace(' and datum ', ' and document_date ', $l_datum).
					$la_kp_dod_uvjet[2].
					
				' union all '.
				// 9- Dnevni promet
				'select mid(sales.date, 1, 10) as datum, sum(retail_items.price) as razlika, '.
					' concat("Dnevni promet ", if(articles.isService = 0, "roba ", "usluga "), DATE_FORMAT(mid(sales.date, 1, 10), "%d.%m.%Y.")) as vezani_dokument, '.
					' concat("Dnevni promet ", if(articles.isService = 0, "roba ", "usluga ")) as opis, "9" as rbr '.
					' from sales, retail_items, articles '.
					' where sales.id = retail_items.sale_id '.
					' and retail_items.article_id = articles.id '.
					str_replace(' and datum ', ' and sales.date ', $l_datum).
					$la_kp_dod_uvjet[7].
					' group by mid(sales.date, 1, 10), articles.isService '.
					' having sum(sales.price) <> 0 '.
					
				' union all '.
				// 10 - knjiga prometa usluga
				'select mid(sales.date, 1, 10) as datum, sum(retail_items.price) as razlika, '.
					' concat("Dnevni promet usluga ", DATE_FORMAT(mid(sales.date, 1, 10), "%d.%m.%Y.")) as vezani_dokument, '.
					' "Dnevni promet usluga " as opis, "10" as rbr '.
					' from sales, retail_items, articles '.
					' where sales.id = retail_items.sale_id '.
					' and retail_items.article_id = articles.id '.
					' and articles.isService = 1 '.
					str_replace(' and datum ', ' and sales.date ', $l_datum).
					$la_kp_dod_uvjet[9].
					' group by mid(sales.date, 1, 10), articles.isService '.
					' having sum(sales.price) <> 0 '.
				
				' order by datum, rbr ';
				
				$x = get_data($l_sql, 1);
				
				
				
				
				if($_POST['knjiga_popisa_radio'] == 2){
					$result[0] = array(
						'datum' => $la_donos_ukup_roba['datum'],
						'opis' => 'Donos robe',
						'dokument' => 'Donos robe',
						'nabava' => $la_donos_ukup_roba['nabava'],
						'prodaja' => $la_donos_ukup_roba['prodaja'],
						'razlika' => 0,
						'rbr' => '',
					);
					$result[1] = array(
						'datum' => $la_donos_ukup_usluge['datum'],
						'opis' => 'Donos usluga',
						'dokument' => 'Donos usluga',
						'nabava' => $la_donos_ukup_usluge['nabava'],
						'prodaja' => $la_donos_ukup_usluge['prodaja'],
						'razlika' => 0,
						'rbr' => '',
					);
				}else{
					$result[0] = array(
						'datum' => '',
						'opis' => '',
						'dokument' => '',
						'nabava' => 0,
						'prodaja' => 0,
						'razlika' => 0,
						'rbr' => '',
					);
					$result[1] = array(
						'datum' => '',
						'opis' => '',
						'dokument' => '',
						'nabava' => 0,
						'prodaja' => 0,
						'razlika' => 0,
						'rbr' => '',
					);
				}
				
				
				//pe($x);
				
				
				for($i = 0; $i < sizeof($x['datum']); $i++){
					$result[2 + $i] = array(
						'datum' => u_datum($x['datum'][$i], 1).'-'.u_mjesec($x['datum'][$i], 1).'-'.u_godina($x['datum'][$i]),
						'opis' => $x['opis'][$i],
						'dokument' => $x['vezani_dokument'][$i],
						'nabava' => '0',
						'prodaja' => '0',
						'razlika' => $x['razlika'][$i],
						'rbr' => $x['rbr'][$i],
					);
				}
				for($i = 0; $i < sizeof($x['datum']); $i++){
					switch($result[2 + $i]['rbr']){
						case '9':
						case '10':
							$result[2 + $i]['prodaja'] = $result[2 + $i]['razlika'];
							break;
						
						default:
							$result[2 + $i]['nabava'] = $result[2 + $i]['razlika'];
					}
					$result[2 + $i]['razlika'] = '';
				}
				
				for($i = 0; $i < sizeof($x['datum']); $i++){
					switch($result[2 + $i]['rbr']){
						case '6':
							$result[2 + $i]['nabava'] = -1 * $result[2 + $i]['nabava'];
							break;
						/*
						case '7':
						case '8':
						*/
						
						case '2':
						case '3':
						case '4':
							$result[2 + $i]['nabava'] = '-'.abs($result[2 + $i]['nabava']);
							break;
					}
				}
				
				$la_cnt = array(0, 0, 0, 0, 0);
				for($i = 0; $i < sizeof($x['datum']); $i++){
					switch($result[2 + $i]['rbr']){
						case '6':
						case '7':
						case '8':
							$la_cnt[$result[2 + $i]['rbr']]++;
							$result[2 + $i]['dokument'] = str_replace('#REPLACE-RBR#', $la_cnt[$result[2 + $i]['rbr']], $result[2 + $i]['dokument']);
							break;
					}
				}
				
				
				
				
				//pe($result);
				
				
				
				
				
				
				// ispis u PDF
				
				if($_POST['knjiga_popisa_radio'] == 2){
					$l_start_result = 0;
				}else{
					$l_start_result = 2;
				}
				
				$la_ukup_l = array();
				$la_ukup_d = array();
				//pe($result);
				
				$l_cnt_all = 1;
				for($j = $l_start_result; $j < sizeof($result); $j++){
					
					
					if($_POST['kp_promet_roba_usluga'] == 1 && $j == 1){
						continue;
					}
					
					//pe($result);
					
					if($_POST['kp_promet_roba_usluga'] == 1 && trim($result[$j]['opis']) == 'Dnevni promet usluga'){
						continue;
					}
					
					if($_POST['kp_promet_roba_usluga'] == 2 && trim($result[$j]['opis']) != 'Dnevni promet usluga'){
						continue;
					}
					
					
					
					$l_pos = 0;
					$l_cnt = 1;
					
					$l_pos += $la_width[0];
					$pdf->SetXY(10 + $l_pos, $l_y);
					$pdf->MultiCell($la_width[1], 5, _convert($l_cnt_all), 0, $la_value_align[1]);
					
					foreach($result[$j] as $key => $value){
						if($key == 'razlika'){
							break;
						}
						$l_pos += $la_width[$l_cnt];
						$pdf->SetXY(10 + $l_pos, $l_y);
						if($key == 'nabava' || $key == 'prodaja'){
							$pdf->MultiCell($la_width[$l_cnt + 1], 5, _convert(sprintf('%01.2f', round($value, 2))), 0, $la_value_align[$l_cnt + 1]);
						}else{
							$pdf->MultiCell($la_width[$l_cnt + 1], 5, _convert($value), 0, $la_value_align[$l_cnt + 1]);
						}
						
						if($key == 'nabava'){
							$la_ukup_l[0] += $value;
						}
						if($key == 'prodaja'){
							if(strstr($result[$j]['opis'], 'uslug')){
								$la_ukup_r[2] += $value;
							}else{
								$la_ukup_r[1] += $value;
							}
						}
						
						$l_cnt++;
					}
					if($l_cnt > 1){
						$l_cnt_all++;
					}
					$l_y += 5;
					
					
					if($l_y >= 170){
						$pdf->AddPage();
						$l_y = 12;
						
						$pdf->SetFont("ArialCE", "B", 8);
						$l_pos = 0;
						for($k = 0; $k < sizeof($la_header) - 1; $k++){
							$l_pos += $la_width[$k];
							$pdf->SetXY(10 + $l_pos, $l_y);
							$pdf->MultiCell($la_width[$k + 1], 5, _convert($la_header[$k + 1]), 0, $la_header_align[$k + 1]);
						}
						
						$pdf->SetFont("ArialCE", "", 8);
						$l_y += 7;
					}
					
				}
				
				$pdf->SetFont("ArialCE", "B", 8);
				
				$l_y += 2;
				
				//pe('aaaaa');
				
				
				$pdf->SetXY(15, $l_y);
				$pdf->MultiCell(197, 5, _convert('UKUPNO NABAVA'), 'T', 'L');
				
				$pdf->SetXY(197, $l_y);
				$pdf->MultiCell(25, 5, _convert(sprintf('%01.2f', round($la_ukup_l[0], 2))), 'T', 'R');
				
				$pdf->SetXY(222, $l_y);
				$pdf->MultiCell(25, 5, '', 'T', 'R');
				
				$l_y += 4;
				
				
				
				if($_POST['kp_promet_roba_usluga'] == 1){
					$pdf->SetXY(15, $l_y);
					$pdf->MultiCell(197, 5, _convert('UKUPNO PRODAJA ROBA'), '', 'L');
					
					$pdf->SetXY(197, $l_y);
					$pdf->MultiCell(25, 5, '', '', 'R');
					
					$pdf->SetXY(222, $l_y);
					$pdf->MultiCell(25, 5, _convert(sprintf('%01.2f', round($la_ukup_r[1], 2))), '', 'R');
					
					$l_y += 4;
				}
				
				
				
				if($_POST['kp_promet_roba_usluga'] == 2){
					$pdf->SetXY(15, $l_y);
					$pdf->MultiCell(197, 5, _convert('UKUPNO PRODAJA USLUGA'), 'B', 'L');
					
					$pdf->SetXY(197, $l_y);
					$pdf->MultiCell(25, 5, '', 'B', 'R');
					
					$pdf->SetXY(222, $l_y);
					$pdf->MultiCell(25, 5, _convert(sprintf('%01.2f', round($la_ukup_r[2], 2))), 'B', 'R');
					
					$l_y += 6;
				}
				
				
				
				
				
				
				$pdf->SetXY(15, $l_y);
				$pdf->MultiCell(197, 5, _convert('SALDO'), 'B', 'L');
				
				$pdf->SetXY(197, $l_y);
				$pdf->MultiCell(25, 5, _convert(sprintf('%01.2f', round($la_ukup_l[0] - $la_ukup_r[1], 2))), 'B', 'R');
				
				$pdf->SetXY(222, $l_y);
				$pdf->MultiCell(25, 5, _convert(''), 'B', 'R');
				
				//p($result);
				//pe($x);
				
				
				/*
				// 1 - Primka Kalkulacija
				$result['1'] = array();
				
				$x = get_data('select mid(document_date, 1, 10) as datum, price_sell as razlika, broj_primka as vezani_dokument '.
					' from receivings '.
					' where type = 0 '.
					' and posted = 1 '.
					str_replace(' and datum ', ' and document_date ', $l_datum).
					' order by document_date ', 1);
				for($i = 0; $i < sizeof($x['datum']); $i++){
					$result['1'][$i] = array(
						'datum' => $x['datum'][$i],
						'opis' => 'Primka kalkulacija',
						'dokument' => 'Primka broj '.$x['vezani_dokument'][$i],
						'nabava' => $x['razlika'][$i],
						'prodaja' => '0',
					);
				}
				
				
				// 2 - Zapisnik o promjeni cijene (1) - Popust kupcu
				$result['2'] = array();
				$x = get_data('select mid(sales.date, 1, 10) as datum, sum(retail_items.amount * retail_items.base_price * retail_items.discount_percent / 100) as razlika '.
					' from sales, retail_items, articles '.
					' where sales.id = retail_items.sale_id '.
					' and retail_items.article_id = articles.id '.
					' and discount_percent > 0  '.
					' and sales.date between "'.$l_dat_1.'" and "'.$l_dat_2.'" '.
					' group by mid(sales.date, 1, 10) '.
					' having sum(retail_items.amount * retail_items.base_price * retail_items.discount_percent / 100) <> 0 '.
					' order by mid(sales.date, 1, 10) ', 1);
				for($i = 0; $i < sizeof($x['datum']); $i++){
					$result['2'][$i] = array(
						'datum' => $x['datum'][$i],
						'opis' => 'Zapisnik o promjeni cijene (1) - Popust kupcu',
						'dokument' => 'Popust kupcu broj '.($i + 1),
						'nabava' => -1 * $x['razlika'][$i],
						'prodaja' => '0',
					);
				}
					
					
					
				// 3 - Zapisnik o promjeni cijene (2) - Ulazna kalkulacija
				$result['3'] = array();
				
				$x = get_data('select mid(datum, 1, 10) as datum, sum(razlika) as razlika '.
					' from zopc '.
					' where zopc_razlog_promjene_id = 2 '.
					$l_datum.
					' group by mid(datum, 1, 10) '.
					' having sum(razlika) <> 0 '.
					' order by datum ', 1);
				for($i = 0; $i < sizeof($x['datum']); $i++){
					$result['3'][$i] = array(
						'datum' => $x['datum'][$i],
						'opis' => 'Zapisnik o promjeni cijene (2) - Ulazna kalkulacija',
						'dokument' => 'Ulazna kalkulacija broj '.($i + 1),
						'nabava' => -1 * $x['razlika'][$i],
						'prodaja' => '0',
					);
				}
				
				
				// 4 - Zapisnik o promjeni cijene (3) - Promjena cijene na skladištu
				$result['4'] = array();
				
				$x = get_data('select mid(datum, 1, 10) as datum, sum(razlika) as razlika '.
					' from zopc '.
					' where zopc_razlog_promjene_id = 3 '.
					$l_datum.
					' group by mid(datum, 1, 10) '.
					' having sum(razlika) <> 0 '.
					' order by datum ', 1);
				for($i = 0; $i < sizeof($x['datum']); $i++){
					$result['4'][$i] = array(
						'datum' => $x['datum'][$i],
						'opis' => 'Zapisnik o promjeni cijene (3) - Promjena cijene na skladištu',
						'dokument' => 'Promjena cijene na skladištu broj '.($i + 1),
						'nabava' => -1 * $x['razlika'][$i],
						'prodaja' => '0',
					);
				}
				
				
				
				// 5- Otpis
				$result['5'] = array();
				
				$x = get_data('select mid(document_date, 1, 10) as datum, price_sell as razlika, broj_otpis as vezani_dokument '.
					' from receivings '.
					' where type = 1 '.
					' and posted = 1 '.
					str_replace(' and datum ', ' and document_date ', $l_datum).
					' order by document_date ', 1);
				for($i = 0; $i < sizeof($x['datum']); $i++){
					$result['5'][$i] = array(
						'datum' => $x['datum'][$i],
						'opis' => 'Otpis',
						'dokument' => 'Otpis broj '.$x['vezani_dokument'][$i],
						'nabava' => -1 * abs($x['razlika'][$i]),
						'prodaja' => '0',
					);
				}
				
				
				
				// 6- Povrat
				$result['6'] = array();
				
				$x = get_data('select mid(document_date, 1, 10) as datum, price_sell as razlika, broj_povratnica as vezani_dokument '.
					' from receivings '.
					' where type = 2 '.
					' and posted = 1 '.
					str_replace(' and datum ', ' and document_date ', $l_datum).
					' order by document_date ', 1);
				for($i = 0; $i < sizeof($x['datum']); $i++){
					$result['6'][$i] = array(
						'datum' => $x['datum'][$i],
						'opis' => 'Povratnica',
						'dokument' => 'Povratnica broj '.$x['vezani_dokument'][$i],
						'nabava' => -1 * abs($x['razlika'][$i]),
						'prodaja' => '0',
					);
				}
				
				
				
				// 7- Inventura
				$result['7'] = array();
				
				$x = get_data('select mid(document_date, 1, 10) as datum, price_sell as razlika, broj_inventura as vezani_dokument '.
					' from receivings '.
					' where type = 4 '.
					' and posted = 1 '.
					str_replace(' and datum ', ' and document_date ', $l_datum).
					' order by document_date ', 1);
				for($i = 0; $i < sizeof($x['datum']); $i++){
					$result['7'][$i] = array(
						'datum' => $x['datum'][$i],
						'opis' => 'Inventura',
						'dokument' => 'Inventura broj '.$x['vezani_dokument'][$i],
						'nabava' => $x['razlika'][$i],
						'prodaja' => '0',
					);
				}
				
				
				
				// 8- Medjuskladisnica
				$result['8'] = array();
				
				$x = get_data('select mid(document_date, 1, 10) as datum, price_sell as razlika, broj_medjuskladisnica as vezani_dokument '.
					' from receivings '.
					' where type = 3 '.
					' and posted = 1 '.
					str_replace(' and datum ', ' and document_date ', $l_datum).
					' order by document_date ', 1);
				for($i = 0; $i < sizeof($x['datum']); $i++){
					$result['8'][$i] = array(
						'datum' => $x['datum'][$i],
						'opis' => 'Medjuskladisnica',
						'dokument' => 'Medjuskladisnica broj '.$x['vezani_dokument'][$i],
						'nabava' => -1 * abs($x['razlika'][$i]),
						'prodaja' => '0',
					);
				}
				
				
				
				// 9- Dnevni promet
				$result['9'] = array();
				
				$x = get_data('select mid(sales.date, 1, 10) as datum, sum(sales.price) as razlika, "" as vezani_dokument '.
					' from sales '.
					' where 1 = 1 '.
					str_replace(' and datum ', ' and sales.date ', $l_datum).
					' group by mid(sales.date, 1, 10) '.
					' having sum(sales.price) <> 0 '.
					' order by mid(sales.date, 1, 10) ', 1);
				for($i = 0; $i < sizeof($x['datum']); $i++){
					$result['9'][$i] = array(
						'datum' => $x['datum'][$i],
						'opis' => 'Dnevni promet',
						'dokument' => 'Dnevni promet '.$x['datum'][$i],
						'nabava' => '0',
						'prodaja' => $x['razlika'][$i],
					);
				}
				*/
			
				$l_name = 'knjiga-popisa.pdf';
				break;
				
				
				
			case 14:
				// izvjesce prebaceno u "ajax mode" (file: _all_reports.php) pa ovaj kod ovdje ne treba...
				/*
				$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");
				
				$l_datum_od = $_POST['keub_od'];
				$l_datum_do = $_POST['keub_do'];
				
				$la_dat1 = explode('-', $l_datum_od);
				$la_dat2 = explode('-', $l_datum_do);
				
				$pdf->SetFont("ArialCE", "B", 11);
				$pdf->SetXY(10, 30);
				$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, 38);
					$pdf->MultiCell(87, 5, _convert('KRUH'), 'B', 'C');
					
					$pdf->SetXY(113, 38);
					$pdf->MultiCell(50, 5, _convert('PECIVO I DR. PEK. PROIZVODI'), 'B', 'C');
					
					$pdf->SetXY(190, 38);
					$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, 45);
						$pdf->MultiCell(13.75, 3, _convert($x['name'][$i]), 0, 'C');
					}
					
					$pdf->Line(5, 55, 175, 55);
					
					
					
					
					
					$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, 45);
						$pdf->MultiCell(13.75, 3, _convert($x2['name'][$i]), 0, 'C');
					}
					
					$pdf->Line(190, 55, 280, 55);
					
					
					
					
					
					
					$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 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 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, 51 + 5 * $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, 51 + 5 * $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, 51 + 5 * $l_red);
							$pdf->MultiCell(13.75, 3, _convert(sprintf('%01.4f', $l_sum)), 0, 'C');
						}
						
					}
					
					
					$l_red = 0;
					$l_datum_old = '-1';
					$la_sum = 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]){
								if($j < sizeof($x2['id']) - 1){
									$pdf->SetXY(20 + (sizeof($x['id']) + 1 + $j) * 13, 51 + 5 * $l_red);
									$pdf->MultiCell(13.75, 3, _convert(sprintf('%01.4f', $y2['masa'][$i])), 0, 'C');
									$la_sum[$j] += $y2['masa'][$i];
								}
							}
						}
						
					}
					
					
					
					$pdf->Line(5, 55 + 5 * $l_red, 175, 55 + 5 * $l_red);
					$pdf->Line(190, 55 + 5 * $l_red, 280, 55 + 5 * $l_red);
					
					$pdf->SetXY(5, 56 + 5 * $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, 56 + 5 * $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, 56 + 5 * $l_red);
							$pdf->MultiCell(13.75, 3, _convert(sprintf('%01.4f', $la_sum[$j])), 0, 'C');
						}
					}
					
					
					$pdf->SetXY(20 + (sizeof($x['id']) - 1) * 13, 56 + 5 * $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-obrazac.pdf';
				break;
				*/
		}
	}
	
	$l_dest = $g_path.'app/webroot/iso/res/files/'.$l_name;
	
	$pdf->Output($l_dest, 'F');
	
	header('location:'.$g_path_sl.'app/webroot/iso/res/files/'.$l_name);
	exit;
?>