<?
	header("Content-Type: text/html; charset=utf-8");
	
	$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";
	
	$l_postfix = '';
	
	
	if(strlen($_POST['name']) > 50 ){
		$_POST['name'] = substr($_POST['name'], 0, 50);
	}
	$l_sql = 'select id from articles where name = "'.$_POST['name'].'" limit 1 ';
	
	$l_id = get_item($l_sql);
	
	if($l_id > 0){
		
		$l_inserted = -1;
		
	}else{
		
		$l_name = trim(str_replace(array('0.03', '0,03'), array('', ''), $_POST['name']));
		
		$l_sql = 'select id from articles where name = "'.$l_name.'" limit 1 ';
		
		$l_id = get_item($l_sql);
		
		if($l_id > 0){
			
			$l_inserted = -1;
			
		}else{
			
			if(isset($_POST['is_003']) && $_POST['is_003'] == 1){
				$l_postfix = $l_name.' (lit)';
				$l_sql = 'insert into articles (tax_group_id, name, amount, price_buy, price_sell, unit, display) '.
					' values("'.$_POST['tax_group_id'].'", "'.$l_name.'", 0, 0.01, 0.03, "lit", "'.$l_postfix.'") ';
			}else{
				$l_postfix = $_POST['name'].' (KOM)';
				$l_sql = 'insert into articles (tax_group_id, name, amount, price_buy, price_sell, unit, display) '.
					' values("'.$_POST['tax_group_id'].'", "'.$_POST['name'].'", 0, 0.01, 0.03, "KOM", "'.$l_postfix.'") ';
			}
			
			$l_inserted = sql_exe($l_sql, true);
			
		}
		
		
	}
	
	
	
	
	$result = array(
		'ok' => $l_inserted,
		'data' => array('id' => $l_inserted, 'name' => $l_postfix),
	);
	$la_json = json_encode($result);
	print $la_json;
	exit;
?>
