<?
	if($_POST){
		ob_start();
	}
	
	$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";
	
	function create_uuid($table, $pomak = 0){
		$l_max = get_item('SELECT max(id) from '.$table);
		
		print $l_max."<br>";
		
		if(!$l_max){
			$l_max = '00000000-0000-0000-0000-000000000000';
		}
		$la_tmp_guid = explode('-', $l_max);
		
		$l_new_str = '';
		$l_str = $la_tmp_guid[0];
		$l_str_2 = $la_tmp_guid[4];
		
		if($l_str != '00000000' && substr($l_str_2, 0, 3) != '000' && substr($l_str_2, 0, 3) != '999'){
			$l_new_str = '';
			for($i = 0; $i < 8; $i++){
				if($l_str[$i] == 'f'){
					$l_new_str .= $l_str[$i];
					continue;
				}
				//$l_new_str .= chr(1 + ord($l_str[$i]));
				
				$l_new_str .= dechex(1 + hexdec($l_str[$i]));
			}
		}else{
			$l_new_str = $la_tmp_guid[0];
		}
		
		if(!is_numeric($la_tmp_guid[4])){
			$la_tmp_guid[4] = '000000000000';
		}
		
		//print $l_new_str."<br>";
		//print $la_tmp_guid[4]."<br>";
		
		if($l_new_str == $l_str && substr($l_str_2, 0, 3) != '000'){
			$l_str = $la_tmp_guid[4];
			$l_str[0] = '9';
			$l_str[1] = '9';
			$l_str[2] = '9';
			$la_tmp_guid[4] = $l_str;
		}else{
			$la_tmp_guid[0] = $l_new_str;
		}
		
		$la_tmp_guid[4] = sprintf("%012d", 1 + $pomak + intval($la_tmp_guid[4]));
		$result = $la_tmp_guid[0].'-'.$la_tmp_guid[1].'-'.$la_tmp_guid[2].'-'.$la_tmp_guid[3].'-'.$la_tmp_guid[4];
		
		//print $result."<br>";
		//exit;
		
		return $result;
	}
	
	function ins_db($x, $sufix = '', $direct = 1, $insert_empty_row = 0, $sf_tab = '', $new_row_counter = 0){
		$result = '';
		
		$la_type = array();
		$la_type_maska = array();
		
		$insert_empty_row == 0 ? $l_all_empty = 1 : $l_all_empty = 0;
		
		$l_pamti_type_value_rbr = '';
		
		$result = 'insert into '.$x['ime_tablice'][0].' values(';
		for($i = 0; $i < sizeof($x['rbr_atribut']); $i++){
			
			if($insert_empty_row == 0 && $x['aktivan'][$i] == 1){
				if($_POST['formfield_'.$x['rbr_atribut'][$i].$sufix.$sf_tab] != ''){
					$l_all_empty = 0;
				}
			}
			
			//p($x['aktivan'][$i].' - '.strtolower($x['kljuc'][$i]).' - '.$direct);
			
			if($x['aktivan'][$i] == 0){
				if(strtolower($x['kljuc'][$i]) == 'pri'){
					if($direct == 1){
						
						
						//$uuid = get_item('SELECT UUID() as uuid');
						//$uuid = create_uuid($x['ime_tablice'][0], $new_row_counter);
						$uuid = '0';
						$result .= '"'.$uuid.'"';
						//print $sufix." ".$uuid." xx <br>";
					}else{
						$result .= '"{_GENERATE_PRIMARY_KEY_8562f3423_}"';
					}
				}else{
					
					if(strlen(trim($_POST['formfield_'.$x['rbr_atribut'][$i].$sufix.$sf_tab])) > 0){
						$result .= ', "'.addslashes($_POST['formfield_'.$x['rbr_atribut'][$i].$sufix.$sf_tab]).'"';
					}else{
						if(strlen(trim($x['default_value'][$i])) > 0){
							if(trim($x['default_value'][$i]) == 'd_current_date'){
								$result .= ', now()';
							}else{
								if(trim($x['default_value'][$i]) == 'vlasnik'){
									if($_COOKIE['subaplication']){
										$result .= ', "'.addslashes(get_item('select full_name from users where md5(username) = "'.$_COOKIE['subaplication'].'" ')).'"';
									}else{
										$result .= ', "'.addslashes($x['default_value'][$i]).'"';
									}
								}else{
									$result .= ', "'.addslashes($x['default_value'][$i]).'"';
								}
							}
						}else{
							$result .= ', NULL';
						}
					}
				}
			}else{
				switch($_POST['formfield_'.$x['rbr_atribut'][$i].$sufix.$sf_tab]){
					case 'db_guid()':
						//$result .= ', "'.addslashes(get_item('SELECT UUID() as uuid')).'"';
						$result .= ', "'.addslashes(create_uuid($x['ime_tablice'][0])).'"';
						break;
						
					case 'now()':
						$result .= ', now()';
						break;
						
					case 'maxouterdb':
						$result .= ', '.(1 + get_item('select max(rbr_users_outer_db) from zaposlenik '));
						break;
						
					default:
						$l_value = $_POST['formfield_'.$x['rbr_atribut'][$i].$sufix.$sf_tab];
						$l_navodnik = '"';
						
						$la_type[$i] = explode('-', $x['tip_polja'][$i]);
						$la_type[$i][0] = strtolower(trim($la_type[$i][0]));
						
						$la_type_maska[$i] = explode('-', $x['tip_polja_maska'][$i]);
						$la_type_maska[$i][0] = strtolower(trim($la_type_maska[$i][0]));
						
						if($la_type[$i][0] == 'datetime' || $la_type_maska[$i][0] == 'date'){
							$la_tmp = explode('-', $l_value);
							$l_value = $la_tmp[2].'-'.$la_tmp[1].'-'.$la_tmp[0];
							if($l_value == '--'){
								$l_value = 'NULL';
								$l_navodnik = '';
							}
						}
						
						if($la_type[$i][0] == 'datetimebig'){
							$la_tmp_0 = explode(' ', $l_value);
							$la_tmp = explode('-', $la_tmp_0[0]);
							$l_value = $la_tmp[2].'-'.$la_tmp[1].'-'.$la_tmp[0];
							if($l_value == '--'){
								$l_value = 'NULL';
								$l_navodnik = '';
							}else{
								$l_value = $l_value.' '.$la_tmp_0[1];
							}
						}
						
						if($la_type[$i][0] == 'smallint' && $la_type_maska[$i][0] == 'checkbox'){
							if($l_value == ''){
								$l_value = '0';
								$l_navodnik = '';
							}
						}
						if($la_type[$i][0] == 'smallint' && $la_type_maska[$i][0] == 'number'){
							if($l_value == ''){
								$l_value = 'NULL';
								$l_navodnik = '';
							}
						}
						if($la_type[$i][0] == 'decimal'){
							if($l_value == ''){
								$l_value = 'NULL';
								$l_navodnik = '';
							}
						}
						if($la_type[$i][0] == 'int'){
							if($l_value == ''){
								$l_value = 'NULL';
								$l_navodnik = '';
							}
						}
						if($la_type_maska[$i][0] == 'pass'){
							if($l_value != ''){
								$l_value = md5($l_value);
							}
						}
						
						
						
						
						
						if($x['ime_polja'][$i] == 'type'){
							if($x['rbr_atribut'][$i] == '14'){
								$l_pamti_type_value_rbr = $l_value;
							}
						}
						if($x['tip_polja_maska'][$i] == 'label-rbr'){
							$la_replace_1 = [
								'{_u_godina()_}',
								'{_u_godina_current()_}',
							];
							$la_replace_2 = [
								u_godina(),
								u_godina_current(),
							];
							
							if($x['ime_tablice'][$i] == 'medjus'){
								$la_rules = explode(':', $x['rules'][$i]);
								$l_cnt = 1 + intval(get_item(str_replace($la_replace_1, $la_replace_2, $la_rules[1])));
								$l_value = u_godina_current().'/'.sprintf("%06d", $l_cnt);
							}
							
							if($x['ime_tablice'][$i] == 'receivings'){
								
								$la_rules = explode(':', $x['rules'][$i]);
								if(isset($l_pamti_type_value_rbr) && isset($la_rules[2]) && $l_pamti_type_value_rbr == $la_rules[2]){
									$l_cnt = 1 + intval(get_item(str_replace($la_replace_1, $la_replace_2, $la_rules[1])));
									$l_value = u_godina_current().'/'.sprintf("%06d", $l_cnt);
								}else{
									$l_value = '';
								}
							}
						}
						
						
						
						
						$result .= ', '.$l_navodnik.addslashes($l_value).$l_navodnik;
				}
			}
		}
		//p($result);
		//p('------');
		//print $result." ========> <br>";
		//print "<br>";
		$result .= ')';
		
		//pe($result);
		
		if($l_all_empty == 1){
			$result = '';
		}
		
		return $result;
	}
	
	function upd_db($x, $sufix = '', $sf_tab = ''){
		$result = '';
		
		//print "Q".$sf_tab."Q====\n";
		
		switch($sf_tab){
			case '':
				$sf_tab = '';
				$sf_tab_2 = '';
				break;
				
			default:
				$sf_tab = $sf_tab - 1;
				$l_sf_tab = intval($sf_tab);
				$sf_tab = '_'.$l_sf_tab;
				$sf_tab_2 = '_'.($l_sf_tab + 1);
		}
		
		//print $sf_tab."====\n";
		//print $sf_tab_2."====\n";
		
		for($i = 0; $i < sizeof($x['rbr_atribut']); $i++){
			if($x['kljuc'][$i] == 'PRI'){
				$l_kljuc = $x['ime_polja'][$i];
				continue;
			}
			if($x['aktivan_edit'][$i] == 0){
				//p($x['rbr'][$i].' => '.$x['rbr_atribut'][$i].' => '.$x['aktivan_edit'][$i].' => '.$x['ime_polja'][$i].' -- ');
				
				
				if($x['ime_polja'][$i] == 'modified_by' && $x['default_value'][$i] == 'vlasnik' && $_COOKIE['subaplication']){
					// samo kod polja modified_by, kad je postavljen defaultno kao vlasnik, a i da postoji kuki "subaplication"
					$result .= ', '.$x['ime_polja'][$i].' = "'.addslashes(get_item('select full_name from users where md5(username) = "'.$_COOKIE['subaplication'].'" ')).'"';
				}
				
				continue;
			}
			
			
			switch($_POST['formfield_'.$x['rbr_atribut'][$i].$sufix.$sf_tab_2]){
				case 'db_guid()':
					//$result .= ', '.$x['ime_polja'][$i].' = "'.addslashes(get_item('SELECT UUID() as uuid')).'" ';
					$result .= ', '.$x['ime_polja'][$i].' = "'.addslashes(create_uuid($x['ime_tablice'][0])).'" ';
					break;
					
				case 'now()':
					$result .= ', '.$x['ime_polja'][$i].' = now()';
					break;
					
				default:
					$l_value = $_POST['formfield_'.$x['rbr_atribut'][$i].$sufix.$sf_tab_2];
					
					if($x['tip_polja_maska'][$i] == 'pass'){
						if($l_value == ''){
							continue;
						}else{
							$l_value = md5($l_value);
						}
					}
					
					if($x['tip_polja'][$i] == 'datetime' || $x['tip_polja_maska'][$i] == 'date'){
						$la_tmp = explode('-', $l_value);
						$l_value = $la_tmp[2].'-'.$la_tmp[1].'-'.$la_tmp[0];
					}
					
					if($x['tip_polja'][$i] == 'datetimebig'){
						$la_tmp_0 = explode(' ', $l_value);
						
						$la_tmp = explode('-', $la_tmp_0[0]);
						$l_value = $la_tmp[2].'-'.$la_tmp[1].'-'.$la_tmp[0].' '.$la_tmp_0[1];
					}
					
					$result .= ', '.$x['ime_polja'][$i].' = "'.addslashes($l_value).'"';
			}
		}
		
		//print 'get'.$sufix.$sf_tab."\n";
		
		$result = 'update '.$x['ime_tablice'][0].' set '.substr($result, 2).' where '.$l_kljuc.' = "'.$_POST['get'.$sufix.$sf_tab].'" ';
		
		//pe($result);
		//print $result." <br>\n";
		
		return $result;
	}
	
	
	function del_db($x, $sufix = '', $sf_tab = ''){
		
		switch($sf_tab){
			case '':
				$l_sf_tab = '';
				break;
				
			default:
				$l_sf_tab = '_'.intval($sf_tab - 1);
		}
		
		if($_POST['get'.$sufix.$l_sf_tab] == ''){
			return '';
		}
		
		for($i = 0; $i < sizeof($x['rbr_atribut']); $i++){
			if($x['kljuc'][$i] == 'PRI'){
				$l_kljuc = $x['ime_polja'][$i];
				continue;
			}
		}
		
		$result = 'delete from '.$x['ime_tablice'][0].' where '.$l_kljuc.' = "'.$_POST['get'.$sufix.$l_sf_tab].'" ';
		
		return $result;
	}
	
	
	function ins_upd_db($form_name, $redak = 0, $return_last_inserted = 0, $last_generated_id = '', $sf_tab = ''){
		$la_sql = array();
		
		/*
		print $form_name." ";
		print $redak." ";
		print $sf_tab." ";
		print "<br> ";
		*/
		
		
		$x = get_data('select meta_tablica_list.ime_tablice, meta_item.rbr_atribut, meta_item.ime_polja, tip_polja, tip_polja_maska, meta_item.kljuc, '.
			' meta_item.aktivan, meta_item.aktivan_edit, meta_item.default_value, meta_forma.insert_empty_row, meta_item.rules '.
			' from meta_forma, meta_tab, meta_grupa, meta_item, meta_tablica_list '.
			' where meta_forma.rbr = meta_tab.rbr_meta_forma '.
			' and meta_tab.rbr = meta_grupa.rbr_meta_tab '.
			' and meta_grupa.rbr = meta_item.rbr_meta_grupa '.
			' and meta_forma.rbr_meta_tablica_list = meta_tablica_list.rbr '.
			' and meta_forma.ime = "'.$form_name.'" '.
			' order by meta_item.rbr_atribut ', 1);
		
		//print $redak."aaa<br>";
		
		//pe($x);
		
		//print_r($_POST);
		//print $redak."aaa<br><br>\n";
		
		if($redak == 0){
			//print "a ";
			if($_POST['get']){
				$la_sql []= upd_db($x);
			}else{
				$la_sql []= ins_db($x, '', 0);
			}
		}else{
			
			//print $redak."++++ \n";
			
			
			$l_new_row_counter = 0;
			
			for($i = 1; $i <= $redak; $i++){
				if($_POST['subform_table_tabs'] > 0){
					
					//print 'get_'.$i."\n";
					
					if($_POST['get_'.$i]){
						$la_sql []= upd_db($x, '_'.$i);
					}else{
						$la_sql []= ins_db($x, '_'.$i, 1, 0, '', $l_new_row_counter);
						$l_new_row_counter++;
					}
					
				}else{
					if($_POST['delete_row_'.$i.'_'.$sf_tab] == '1'){
						$la_sql []= del_db($x, '_'.$i, $sf_tab);
					}else{
						
						//print 'get_'.$i.'_'.$sf_tab."\n";
						
						if($_POST['get_'.$i.'_'.($sf_tab - 1)]){
							
							$la_sql []= upd_db($x, '_'.$i, $sf_tab);
							
							if(substr($la_sql[sizeof($la_sql) - 1], 0, 27) == 'update receiving_items set '){
								
								$l_old_price = get_item('select price_sell from articles where id = '.$_POST['formfield_2_'.$i.'_1']);
								if($l_old_price != $_POST['formfield_6_'.$i.'_1']){
									// prvo izvrsi ovo jer se u ovom if-u promijeni vrijednost za price_sell pa je treba ranije dohvatiti
									$la_articles = get_data('select amount, price_sell from articles where id = "'.$_POST['formfield_2_'.$i.'_1'].'" ', 0);
									
									
									/*
									$l_sql_jos = 'update articles set price_sell = "'.$_POST['formfield_6_'.$i.'_1'].'" where id = '.$_POST['formfield_2_'.$i.'_1'];
									sql_exe($l_sql_jos);
									
									global $g_log_step;
									$l_log_sql = 'insert into meta_log_data values(0, '.$g_log_step.', 0, 2, "'.addslashes($l_sql_jos).'", '.$_SESSION['__id'].', now())';
									sql_exe($l_log_sql);
									*/
									
									
									/*
									$l_bar_code = get_item('select barcode from articles where id = '.$_POST['formfield_2_'.$i.'_1']);
								
									$l_sql_jos = 'insert into zopc values(0, now(), "'.$_POST['formfield_2_'.$i.'_1'].'", "'.$la_articles['amount'].'", "'.$la_articles['price_sell'].'", '.
										' "'.$_POST['formfield_6_'.$i.'_1'].'", "'.round($la_articles['amount'] * $la_articles['price_sell'], 2).'", '.
										' "'.round($la_articles['amount'] * $_POST['formfield_6_'.$i.'_1'], 2).'", '.
										' "'.round(($_POST['formfield_6_'.$i.'_1'] - $la_articles['price_sell']) * $la_articles['amount']).'", "3", "'.$l_bar_code.'" )';
									sql_exe($l_sql_jos);
									
									$l_log_sql = 'insert into meta_log_data values(0, '.$g_log_step.', 0, 1, "'.addslashes($l_sql_jos).'", '.$_SESSION['__id'].', now())';
									sql_exe($l_log_sql);
									*/
								}
								
							}
							
						}else{
							$la_sql []= ins_db($x, '_'.$i, 1, 0, '_'.$sf_tab, $l_new_row_counter);
							$l_new_row_counter++;
							
							if(substr($la_sql[sizeof($la_sql) - 1], 0, 35) == 'insert into receiving_items values('){
								
								$l_old_price = get_item('select price_sell from articles where id = '.$_POST['formfield_2_'.$i.'_1']);
								if($l_old_price != $_POST['formfield_6_'.$i.'_1']){
									// prvo izvrsi ovo jer se u ovom if-u promijeni vrijednost za price_sell pa je treba ranije dohvatiti
									$la_articles = get_data('select amount, price_sell from articles where id = "'.$_POST['formfield_2_'.$i.'_1'].'" ', 0);
									
									
									/*
									$l_sql_jos = 'update articles set price_sell = "'.$_POST['formfield_6_'.$i.'_1'].'" where id = '.$_POST['formfield_2_'.$i.'_1'];
									sql_exe($l_sql_jos);
									
									global $g_log_step;
									$l_log_sql = 'insert into meta_log_data values(0, '.$g_log_step.', 0, 2, "'.addslashes($l_sql_jos).'", '.$_SESSION['__id'].', now())';
									sql_exe($l_log_sql);
									*/
									
									/*
									$l_bar_code = get_item('select barcode from articles where id = '.$_POST['formfield_2_'.$i.'_1']);
									
									$l_sql_jos = 'insert into zopc values(0, now(), "'.$_POST['formfield_2_'.$i.'_1'].'", "'.$la_articles['amount'].'", "'.$la_articles['price_sell'].'", '.
										' "'.$_POST['formfield_6_'.$i.'_1'].'", "'.round($la_articles['amount'] * $la_articles['price_sell'], 2).'", '.
										' "'.round($la_articles['amount'] * $_POST['formfield_6_'.$i.'_1'], 2).'", '.
										' "'.round(($_POST['formfield_6_'.$i.'_1'] - $la_articles['price_sell']) * $la_articles['amount']).'", "3", "'.$l_bar_code.'" )';
									sql_exe($l_sql_jos);
									
									$l_log_sql = 'insert into meta_log_data values(0, '.$g_log_step.', 0, 1, "'.addslashes($l_sql_jos).'", '.$_SESSION['__id'].', now())';
									sql_exe($l_log_sql);
									*/
								}
								
							}
						}
					}
				}
			}
			
			//p($la_sql);
			//pe('qqqqq');
			
		}
		
		//print_r($la_sql);
		//exit;
		
		$result = 1;
		for($i = 0; $i < sizeof($la_sql); $i++){
			if($la_sql[$i] == ''){
				continue;
			}
			
			
			//print $redak." ".$last_generated_id." -> ".$la_sql[$i]."<br><br>";
			
			if($redak > 0 && $last_generated_id != ''){
				$la_sql[$i] = str_replace('_{get_last_inserted_id()}_', $last_generated_id, $la_sql[$i]);
			}
			
			
			if($redak == 0 && $return_last_inserted == 1){
				if(substr($la_sql[$i], 0, 12) == 'insert into '){
					//$uuid = get_item('SELECT UUID() as uuid');
					
					$la_table_name = explode(' ', substr($la_sql[$i], 12));
					//$uuid = create_uuid($la_table_name[0]);
					$uuid = '0';
					
					//print $i."<br>";
					//print $uuid."<br>";
					//print "<br>";
					//print "<br>";
					
					$la_sql[$i] = str_replace('{_GENERATE_PRIMARY_KEY_8562f3423_}', $uuid, $la_sql[$i]);
				}
				if(substr($la_sql[$i], 0, 7) == 'update '){
					
					$l_exp = ' where '.$x['ime_polja'][0].' = "';
					$la_str = explode($l_exp, $la_sql[$i]);
					
					$uuid = trim($la_str[sizeof($la_str) - 1]);
					$uuid = substr($uuid, 0, strlen($uuid) - 1);
				}
			}
			
			
			
			
			$l_zadnji_upis = '';
			
			$la_sql[$i] = str_replace('"NULL"', 'NULL', $la_sql[$i]);
			
			$l_res = sql_exe($la_sql[$i]);
			
			if(sizeof($la_sql) == 1 && !$_POST['get']){
				$l_zadnji_upis = mysql_insert_id();
			}
			
			$result = intval($result * $l_res);
			
			
			
			
			
			
			
			
			
			
			
			//print $la_sql[$i]."<br><br>\n\n";
			//exit;
			
			
			
			
			
			
			
			
			
			
			
			
			if($result){
				global $g_log_step;
				$la_sql_tmp = explode(' ', $la_sql[$i]);
				
				$l_log_action = 0;
				if(substr($la_sql[$i], 0, 7) == 'insert '){
					$l_log_action = 1;
				}
				if(substr($la_sql[$i], 0, 7) == 'update '){
					$l_log_action = 2;
				}
				if(substr($la_sql[$i], 0, 7) == 'delete '){
					$l_log_action = 3;
				}
				
				$l_log_sql = 'insert into meta_log_data values(0, '.$g_log_step.', 0, '.$l_log_action.', "'.addslashes($la_sql[$i]).'", '.$_SESSION['__id'].', now())';
				$g_log_data = sql_exe($l_log_sql, true);
				
				
				if(substr($la_sql[$i], 0, 24) == 'insert into zopc values(' || substr($la_sql[$i], 0, 16) == 'update zopc set '){
					$l_sql_jos = 'update articles set price_sell = "'.$_POST['formfield_5'].'" where id = '.$_POST['formfield_2'];
					sql_exe($l_sql_jos);
					
					$l_log_sql = 'insert into meta_log_data values(0, '.$g_log_step.', 0, '.$l_log_action.', "'.addslashes($l_sql_jos).'", '.$_SESSION['__id'].', now())';
					sql_exe($l_log_sql);
				}
				
				
				//print $l_log_sql;
				//exit;
			}
			
			
			//print $redak." -- ".$l_res." +++ ".$result."<br>";
		}
		
		
		if($redak == 0 && $result == 1){
			$result = $uuid;
			if(sizeof($la_sql) == 1 && !$_POST['get']){
				$result = $l_zadnji_upis;
			}
			//print $redak." -- ".$l_res." +++ ".$result."<br>";
		}
		
		return $result;
	}
	
	
	if($_GET['admin_opcije']){
		$ima_li = get_item('select rbr from opcija where opcija_rbr = '.$_GET['admin_opcije']);
		
		if($ima_li){
			$sql = 'update opcija set opcija_value = "'.addslashes($_POST['opcija_value']).'" where rbr = '.$ima_li;
		}else{
			$sql = 'insert into opcija values(0, '.$_GET['admin_opcije'].', "'.addslashes($_POST['opcija_value']).'" )';
		}
		sql_exe($sql);
		
		$la_tmp = explode('?', $_SERVER['HTTP_REFERER']);
		
		header('location: default.php?'.$la_tmp[1]);
		exit;
	}
	
	if($_GET['bck_upload'] == 2){
		$la_tmp = explode('?', $_SERVER['HTTP_REFERER']);
		
		$ima_li = get_item('select url from opcija_background where rbr_zaposlenik = '.$_SESSION['__id']);
		if($ima_li){
			unlink(str_replace('/res/res/', '/res/', $g_path_res.$ima_li));
		}
		
		$sql = 'delete from opcija_background where rbr_zaposlenik = '.$_SESSION['__id'];
		sql_exe($sql);
		
		header('location: default.php?'.$la_tmp[1]);
		exit;
	}
		
	if($_POST){
		//print_r($_POST);
		//print "<br><br>";
		
		
		if($_GET['bck_upload'] == 1){
			$la_tmp = explode('?', $_SERVER['HTTP_REFERER']);
			
			if($_FILES['imgFile']['error'] > 0){
				header('location: default.php?'.$la_tmp[1].'&err='.$_FILES["file"]["error"]);
			}else{
				$l_name = $_FILES['imgFile']['name'];
				$l_file = $_FILES['imgFile']['tmp_name'];
				
				$l_dest_name = "";
				
				$la_name = explode(".", $l_name);
				for($i = 0; $i < sizeof($la_name) - 1; $i++){
					$l_dest_name .= $la_name[$i];
				}
				$l_dest_name .= "-".u_datum(0, 1).u_sat().u_minuta().u_sekunda().rand(10000000000, 99999999999).".".$la_name[sizeof($la_name) - 1];
				
				$l_dest = 'res/files/'.mk_f_name($_SESSION['__id']).'/'.$l_dest_name;
				$is_ok = move_uploaded_file($l_file, $l_dest);
				
				$ima_li = get_item('select url from opcija_background where rbr_zaposlenik = '.$_SESSION['__id']);
				if($ima_li){
					unlink(str_replace('/res/res/', '/res/', $g_path_res.$ima_li));
					$sql = 'update opcija_background set url = "'.$l_dest.'", repeat_type = "'.$_POST['repeat_type'].'" where rbr_zaposlenik = '.$_SESSION['__id'];
				}else{
					$sql = 'insert into opcija_background values(0, '.$_SESSION['__id'].', "'.$l_dest.'", "'.$_POST['repeat_type'].'") ';
				}
				sql_exe($sql);
				
				header('location: default.php?'.$la_tmp[1]);
			}
			
			exit;
		}
		
		
		if($_GET['pdv'] == 1){
			$la_tmp = explode('?', $_SERVER['HTTP_REFERER']);
			
			$sql = 'update opcija_pdv set ime = "'.$_POST['pdv'].'" where rbr = 1';
			sql_exe($sql);
			
			header('location: default.php?'.$la_tmp[1]);
			exit;
		}
		
		if($_GET['napomene'] == 1){
			$la_tmp = explode('?', $_SERVER['HTTP_REFERER']);
			
			$sql = 'update opcija_napomene set ponuda = "'.addslashes($_POST['txt1']).'", predracun = "'.addslashes($_POST['txt2']).'", '.
				' potvrda_narudzbe = "'.addslashes($_POST['txt3']).'", otpremnica = "'.addslashes($_POST['txt4']).'", '.
				' racun = "'.addslashes($_POST['txt5']).'" where rbr = 1';
			sql_exe($sql);
			
			header('location: default.php?'.$la_tmp[1]);
			exit;
		}
		
		if($_GET['format_racuna'] == 1){
			$la_tmp = explode('?', $_SERVER['HTTP_REFERER']);
			
			$sql = 'update opcija_format_racuna set txt1 = "'.$_POST['txt1'].'", txt2 = "'.$_POST['txt2'].'" where rbr = 1';
			sql_exe($sql);
			
			header('location: default.php?'.$la_tmp[1]);
			exit;
		}
		
		if($_GET['mjesto_izdavanja_racuna'] == 1){
			$la_tmp = explode('?', $_SERVER['HTTP_REFERER']);
			
			$sql = 'update opcija_mjesto_izdavanja_racuna set ime = "'.$_POST['mjesto_izdavanja_racuna'].'" where rbr = 1';
			sql_exe($sql);
			
			header('location: default.php?'.$la_tmp[1]);
			exit;
		}
		
		if($_GET['access'] == 1){
			$la_tmp = explode('?', $_SERVER['HTTP_REFERER']);
			$la2_tmp = explode('&table', $la_tmp[1]);
			
			//print $_POST['f_access_1']." - <br>";
			//print $_POST['f_access_2']." - <br>";
			
			
			
			$l_max_1 = get_item('select max(rbr) from meta_prikaz ');
			$l_max_2 = get_item('select max(rbr) from meta_forma ');
			
			for($i = 1; $i <= $l_max_1; $i++){
				if($_POST['f_access_1_'.$i.'_1'] == 1){
					$sql = 'delete from access where rbr_zaposlenik = '.$_POST['user'].' and rbr_modul_type = 1 and rbr_modul = '.$i.' and rbr_access_type = 1 ';
					sql_exe($sql);
				}else{
					$ima_li = get_item('select rbr from access where rbr_zaposlenik = '.$_POST['user'].' and rbr_modul_type = 1 and rbr_modul = '.$i.' and rbr_access_type = 1 ');
					if(!$ima_li){
						$sql = 'insert into access values(0, '.$_POST['user'].', 1, '.$i.', 1) ';
						sql_exe($sql);
					}
				}
				if($_POST['f_access_1_'.$i.'_2'] == 1){
					$sql = 'delete from access where rbr_zaposlenik = '.$_POST['user'].' and rbr_modul_type = 1 and rbr_modul = '.$i.' and rbr_access_type = 2 ';
					sql_exe($sql);
				}else{
					$ima_li = get_item('select rbr from access where rbr_zaposlenik = '.$_POST['user'].' and rbr_modul_type = 1 and rbr_modul = '.$i.' and rbr_access_type = 2 ');
					if(!$ima_li){
						$sql = 'insert into access values(0, '.$_POST['user'].', 1, '.$i.', 2) ';
						sql_exe($sql);
					}
				}
			}
			
			for($i = 1; $i <= $l_max_2; $i++){
				$z2 = get_item('select rbr from meta_subforma where rbr_meta_forma = '.$i);
				if($z2){
					continue;
				}
				
				$z3 = get_item('select rbr from meta_subforma_table where rbr_meta_forma = '.$i);
				if($z3){
					continue;
				}
				
				if($_POST['f_access_2_'.$i.'_1'] == 1){
					$sql = 'delete from access where rbr_zaposlenik = '.$_POST['user'].' and rbr_modul_type = 2 and rbr_modul = '.$i.' and rbr_access_type = 1 ';
					sql_exe($sql);
				}else{
					$ima_li = get_item('select rbr from access where rbr_zaposlenik = '.$_POST['user'].' and rbr_modul_type = 2 and rbr_modul = '.$i.' and rbr_access_type = 1 ');
					if(!$ima_li){
						$sql = 'insert into access values(0, '.$_POST['user'].', 2, '.$i.', 1) ';
						sql_exe($sql);
					}
				}
				if($_POST['f_access_2_'.$i.'_2'] == 1){
					$sql = 'delete from access where rbr_zaposlenik = '.$_POST['user'].' and rbr_modul_type = 2 and rbr_modul = '.$i.' and rbr_access_type = 2 ';
					sql_exe($sql);
				}else{
					$ima_li = get_item('select rbr from access where rbr_zaposlenik = '.$_POST['user'].' and rbr_modul_type = 2 and rbr_modul = '.$i.' and rbr_access_type = 2 ');
					if(!$ima_li){
						$sql = 'insert into access values(0, '.$_POST['user'].', 2, '.$i.', 2) ';
						sql_exe($sql);
					}
				}
			}
			
			/*
			if($_POST['f_access_1'] == 1){
				$sql = 'delete from access where rbr_zaposlenik = '.$_SESSION['__id'].' and rbr_modul_type = '.$_POST['ptype'].' and rbr_modul = '.$_POST['ptable'].' and rbr_access_type = 1 ';
				sql_exe($sql);
			}else{
				$ima_li = get_item('select rbr from access where rbr_zaposlenik = '.$_SESSION['__id'].' and rbr_modul_type = '.$_POST['ptype'].' and rbr_modul = '.$_POST['ptable'].' and rbr_access_type = 1 ');
				if(!$ima_li){
					$sql = 'insert into access values(0, '.$_SESSION['__id'].', '.$_POST['ptype'].', '.$_POST['ptable'].', 1) ';
					sql_exe($sql);
				}
			}
			
			if($_POST['f_access_2'] == 1){
				$sql = 'delete from access where rbr_zaposlenik = '.$_SESSION['__id'].' and rbr_modul_type = '.$_POST['ptype'].' and rbr_modul = '.$_POST['ptable'].' and rbr_access_type = 2 ';
				sql_exe($sql);
			}else{
				$ima_li = get_item('select rbr from access where rbr_zaposlenik = '.$_SESSION['__id'].' and rbr_modul_type = '.$_POST['ptype'].' and rbr_modul = '.$_POST['ptable'].' and rbr_access_type = 2 ');
				if(!$ima_li){
					$sql = 'insert into access values(0, '.$_SESSION['__id'].', '.$_POST['ptype'].', '.$_POST['ptable'].', 2) ';
					sql_exe($sql);
				}
			}
			*/
			
			header('location: default.php?'.$la2_tmp[0]);
			exit;
		}
		
		
		if($_GET['prijevod'] == 1){
			$la_tmp = explode('?', $_SERVER['HTTP_REFERER']);
			$la2_tmp = explode('&table', $la_tmp[1]);
			
			switch($_POST['ptype']){
				case 1:
					$l_table = 'meta_prikaz_item_lang';
					break;
					
				case 2:
					$l_table = 'meta_item_lang';
					break;
			}
			
			for($i = 1; $i <= $_POST['max_rbr']; $i++){
				$l_ima_li = get_item('select rbr from '.$l_table.' where rbr = '.$_POST['ptable'].' and rbr_atribut = '.$i.' and rbr_jezik = '.$_POST['plang']);
				if($l_ima_li){
					$sql = 'update '.$l_table.' set label = "'.$_POST['f_'.$i].'" where rbr = '.$_POST['ptable'].' and rbr_atribut = '.$i;
				}else{
					$sql = 'insert into '.$l_table.' values ('.$_POST['ptable'].', '.$i.', '.$_POST['plang'].', "'.$_POST['f_'.$i].'")';
				}
				sql_exe($sql);
				
				//print $sql."<br>\n";
			}
			
			header('location: default.php?'.$la2_tmp[0]);
			exit;
		}
		
		
		if($_GET['search'] == 1){
			$l_cnt = 0;
			$l_q = '';
			foreach($_POST as $key => $value){
				//print $key." - ".$value."<br>";
				$value = str_replace('NULL', '', $value);
				$key = str_replace('searchField', 'sf', $key);
				$l_q .= '&'.$key."=".$value;
				$l_cnt++;
			}
			
			$la_tmp = explode('?', $_SERVER['HTTP_REFERER']);
			$la2_tmp = explode('&sf', $la_tmp[1]);
			
			//print $l_q."<br>";
			//print $la_tmp[1]."<br>";
			
			
			header('location: default.php?'.$la2_tmp[0].$l_q);
			exit;
		}
		
		$l_forma_ime = str_replace('forma_', '', $_POST['formname']);
		
		$result = ins_upd_db($l_forma_ime, 0, 1);
		$l_keep_generated_id = $result;
		
		//print $result." --- <br>\n";
		//exit;
		
		if($_POST['get']){
			$result ? $l_q = '&edited=1' : $l_q = '&error=1';
		}else{
			$result ? $l_q = '&added=1' : $l_q = '&error=1';
		}
		
		
		if($_POST['subform_table_tabs'] > 0){
			$y1 = get_item('select rbr from meta_forma where ime = "'.$l_forma_ime.'" ');
			if($y1){
				$y2 = get_item('select rbr_meta_forma from meta_subforma_table where rbr_meta_forma_child = '.$y1.' limit 1 ');
				if($y2){
					$y3 = get_item('select ime from meta_forma where rbr = '.$y2);
					if($y3){
						$result = ins_upd_db($y3, $_POST['subform_table_tabs'], 0, $l_keep_generated_id);
						
						if($_POST['get']){
							$result ? $l_q = '&edited=1' : $l_q = '&error=1';
						}else{
							$result ? $l_q = '&added=1' : $l_q = '&error=1';
						}
					}
				}
			}
		}
		
		//exit;
		
		if($_POST['subform_tabs'] > 0){
			//print $_POST['subform_tabs']." aaaaaaaa aa<br><br>";
			for($i = 0; $i < $_POST['subform_tabs']; $i++){
				if($_POST['subform_lines_'.$i] > 0){
					
					//print 'select rbr from meta_forma where ime = "'.$l_forma_ime.'"<br>'."\n" ;
					
					$y1 = get_item('select rbr from meta_forma where ime = "'.$l_forma_ime.'" ');
					if($y1){
						
						$y2 = get_data('select rbr_meta_forma from meta_subforma where rbr_meta_forma_child = '.$y1.' limit '.$i.', 1', 1);
						//print 'select rbr_meta_forma from meta_subforma where rbr_meta_forma_child = '.$y1.' limit '.$i.', 1<br>'."\n" ;
						
						
						$l_size = sizeof($y2['rbr_meta_forma']);
						
						if($l_size > 0){
							for($j = 0; $j < $l_size; $j++){
								
								//print $i." === ".$j."\n";
								
								$y3 = get_item('select ime from meta_forma where rbr = '.$y2['rbr_meta_forma'][$j]);
								
								if($y3){
									
									$result = ins_upd_db($y3, $_POST['subform_lines_'.$i], 0, $l_keep_generated_id, zbroji($i));
									
									if($_POST['get']){
										$result ? $l_q = '&edited=1' : $l_q = '&error=1';
									}else{
										$result ? $l_q = '&added=1' : $l_q = '&error=1';
									}
								}
							}
						}
						
					}
					/*
					if($y2){
						$y3 = get_item('select ime from meta_forma where rbr = '.$y2);
					}
					if($y3){
						$result = ins_upd_db($y3, $_POST['subform_lines_'.$i], 0, $result);
						
						if($_POST['get']){
							$result ? $l_q = '&edited=1' : $l_q = '&error=1';
						}else{
							$result ? $l_q = '&added=1' : $l_q = '&error=1';
						}
					}
					*/
				}
			}
		}
		
		
		
		
		
		
		
		
		//exit;
		
		
		
		
		
		
		
		
		
		
		
		
		
		
		$la_tmp = explode('?', $_SERVER['HTTP_REFERER']);
		
		$la_old = array('/default.php', $g_path_sl);
		$la_new = array('/', '');
		$l_lang_ref = str_replace($la_old, $la_new, $la_tmp[0]);
		
		
		$la_var = explode('&', $la_tmp[1]);
		
		//pe($la_var);
		
		$l_addition = '';
		for($i = 0; $i < sizeof($la_var); $i++){
			$la_var_2 = explode('=', $la_var[$i]);
			if($la_var_2[0] == 'a'){
				$l_a = $la_var_2[1];
			}
			if($la_var_2[0] == 'b'){
				$l_b = $la_var_2[1];
			}
			if($la_var_2[0] == 'c'){
				if($la_var_2[1] == '2'){
					$l_c = $la_var_2[1];
				}
				$l_c_special = $la_var_2[1];
			}
			if($la_var_2[0] == 'd'){
				$l_d = $la_var_2[1];
			}
			if($la_var_2[0] == 'addition'){
				$l_addition = $la_var_2[1];
			}
		}
		
		
		$l_qstr = 'a='.$l_a.'&b='.$l_b;
		if($l_c){
			$l_qstr .= '&c='.$l_c;
		}
		if($l_d){
			$l_qstr .= '&d='.$l_d;
		}
		if($l_addition){
			$l_qstr .= '&addition='.$l_addition;
		}
		
		//exit;
		
		$_POST['get'] ? $l_edited_id = '#'.$l_a.'/'.$l_b.'/'.$_POST['get'] : $l_edited_id = '';
		$l_keep_generated_id ? $l_edited_id = '#'.$l_a.'/'.$l_b.'/'.$l_keep_generated_id : $l_edited_id = '';
		
		//print 'location: default.php?'.$l_qstr.$l_q.$l_edited_id;
		
		
		//print $l_keep_generated_id." -- <br>";
		//exit;
		
		
		// primke
		
		
		if($l_a == 6 && $l_b == 7 && $l_c_special){
			if($l_c_special == -1){
				$l_id = $l_keep_generated_id;
			}
			if($l_c_special > 0){
				$l_id = $l_c_special;
			}
			
			
			// izracun cijene kao: ukupna_cijena = kolicina * jedinicna_cijena * popust (za slucaj da bude neka greska za ukupnu cijenu u kalkulatoru)
			$l_sql = 'update receiving_items set price = amount * price_buy * (1 - discount / 100) where receiving_id = '.$l_id;
			sql_exe($l_sql);
			//pe($l_sql);
			
			
			
			$l_sql = 'select sum(price) as s1, sum(amount * price_sell) as s2 from receiving_items where receiving_id = '.$l_id;
			$x = get_data($l_sql, 0);
			
			$l_sql = 'update receivings set price_buy = '.$x['s1'].', price_sell = '.round($x['s2'], 2).' where id = '.$l_id;
			sql_exe($l_sql);
			//pe($l_sql);
			
			
			
			// update tax_1 i tax_2 polja u primkama - SAMO  U PRIMKAMA
			
			$x = get_item('select type from receivings where id = '.$l_id);
			
			if($x == 0 && strlen($x) > 0){
				$x = get_data('select id, article_id from receiving_items where receiving_id = '.$l_id.' order by 1, 2 ', 1);
				for($i = 0; $i < sizeof($x['id']); $i++){
					$y = get_item('select tax_group_id from articles where id = "'.$x['article_id'][$i].'" ', 0);
					$z = get_data('select tax_1, tax_2 from tax_groups where id = "'.$y.'" ', 0);
					
					$l_sql = 'update receiving_items set tax_1 = '.$z['tax_1'].', tax_2 = '.$z['tax_2'].' where id = '.$x['id'][$i];
					sql_exe($l_sql);
				}
			}
		}
		
		
		// !!!!!!!!!!!!!!!  ipak NE TREBA kod inventure raditi update tax_1 i tax_2... to se radi samo kod primki... pogledaj programski kod malo iznad
		// inventura
		/*
		if($l_a == 6 && $l_b == 8 && $l_c_special){
			if($l_c_special == -1){
				$l_id = $l_keep_generated_id;
			}
			if($l_c_special > 0){
				$l_id = $l_c_special;
			}
			
			$x = get_data('select id, article_id from receiving_items where receiving_id = '.$l_id.' order by 1, 2 ', 1);
			for($i = 0; $i < sizeof($x['id']); $i++){
				$y = get_item('select tax_group_id from articles where id = "'.$x['article_id'][$i].'" ', 0);
				$z = get_data('select tax_1, tax_2 from tax_groups where id = "'.$y.'" ', 0);
				
				$l_sql = 'update receiving_items set tax_1 = '.$z['tax_1'].', tax_2 = '.$z['tax_2'].' where id = '.$x['id'][$i];
				sql_exe($l_sql);
			}
		}
		*/
		
		
		
		
		if($_POST['spremi_i_dodaj'] && $_POST['spremi_i_dodaj_grupa']){
			if($_POST['spremi_i_dodaj_back_rbr']){
				
				if($_POST['spremi_i_dodaj_back_rbr'] == -1){
					// da se ponovo otvori forma koja je ovaj cas prvi put snimljena, a ne da se ide na pregled primki
					$_POST['spremi_i_dodaj_back_rbr'] = $l_keep_generated_id;
				}
				
				if($l_a == 6 && $l_b == 7 && $l_c_special){
					// primke
					header('location: '.$l_lang_ref.'default.php?'.$l_qstr.'&c='.$_POST['spremi_i_dodaj_back_rbr'].'&primkamsg=saved');
				}else{
					if($l_a == 6 && $l_b == 8 && $l_c_special){
						// inventura
						header('location: '.$l_lang_ref.'default.php?'.$l_qstr.'&c='.$_POST['spremi_i_dodaj_back_rbr'].'&inventuramsg=saved');
					}else{
						if($l_a == 6 && $l_b == 9 && $l_c_special){
							// medjus
							header('location: '.$l_lang_ref.'default.php?'.$l_qstr.'&c='.$_POST['spremi_i_dodaj_back_rbr'].'&medjusmsg=saved');
						}else{
							// ostalo
							header('location: '.$l_lang_ref.'default.php?'.$l_qstr.'&c='.$_POST['spremi_i_dodaj_back_rbr']);
						}
					}
				}
			}else{
				header('location: '.$l_lang_ref.'default.php?'.$l_qstr.$l_q.'&c=-1&sid='.$l_keep_generated_id.'&sidg='.$_POST['spremi_i_dodaj_grupa']);
			}
			exit;
		}else{
			header('location: '.$l_lang_ref.'default.php?'.$l_qstr.$l_q.$l_edited_id);
			exit;
		}
		
		ob_end_clean();
	}
	
	
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<?
	include $g_path."html/h_title.php";
	include $g_path."html/h_meta.php";
	include $g_path."html/h_css.php";
	include $g_path."html/h_js.php";
	
	$l_edited = intval(intval($_GET['edited']) || intval($_GET['added']) || intval($_GET['error']));
	
?>
</head>
<body onload="check_open(<?=$l_edited?>, <?=$_GET['a']?>, <?=$_GET['b']?>, '<?=$_GET['addition']?>')" onhashchange="check_hash_change()">
	<div id="bodyWrap">
		<div id="main">
			<div id="mainContent">
<?
				//include $g_path."html/h_menu_left.php";
				
				//print "11";
				//print_r($_SERVER['HTTP_REFERER']);
				//print_r($_COOKIES);
				//print "aa";
				
				include $g_path."html/h_content.php";
?>
			</div>
		</div>
	</div>
	<div id="bodyBottom">
<?
		include $g_path."html/h_footer.php";
?>
	</div>
<?
	include $g_path."html/h_alarm.php";
?>
</body>
</html>
