<?
	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";
	
	
	// otknjizi
	if($_GET['type'] == 1){
		$l_posted = get_item('select posted from receivings where id = '.$_GET['id']);
		if($l_posted == 1){
			$l_type = get_item('select type from receivings where id = '.$_GET['id']);
			
			$l_sql = 'select article_id, price_buy, price_sell, amount '.
				' from receiving_items where receiving_id = '.$_GET['id'].
				' order by article_id ';
			$x = get_data($l_sql, 1);
			for($i = 0; $i < sizeof($x['article_id']); $i++){
				$l_sql2 = 'select amount, price_buy from articles where id = '.$x['article_id'][$i];
				$y = get_data($l_sql2, 0);
				
				switch($l_type){
					case 0:
						$l_kol_z = $y['amount'];
						$l_kol_p = $x['amount'][$i];
						
						$l_sql3 = 'update articles '.
							'set amount = '.($l_kol_z - $l_kol_p).
							' where id = '.$x['article_id'][$i];
						break;
						
					case 1:
					case 2:
					case 3:
						$l_kol_z = $y['amount'];
						$l_kol_p = abs($x['amount'][$i]);
						
						$l_sql3 = 'update articles '.
							'set amount = '.($l_kol_z + $l_kol_p).
							' where id = '.$x['article_id'][$i];
						break;
					
					case 4:
						$l_kol_z = $y['amount'];
						$l_kol_p = $x['amount'][$i];
						
						$l_sql3 = 'update articles '.
							'set amount = '.($l_kol_z - $l_kol_p).
							' where id = '.$x['article_id'][$i];
						break;
					
					case 6:
						// Skladiste - amount moze biti + ili -, otknjizavanje vraca stanje
						$l_kol_z = $y['amount'];
						$l_kol_p = $x['amount'][$i]; // bez abs()
						
						$l_sql3 = 'update articles '.
							'set amount = '.($l_kol_z - $l_kol_p).
							' where id = '.$x['article_id'][$i];
						break;
						
					default:
						pe('Pogreška 568438. Ne postoji tip transakcije koji ste pokušali upisati/mijenjati!');
				}
				
				sql_exe($l_sql3);
				
				
				$g_log_step = 0;
				$l_log_sql = 'insert into meta_log_data values(0, '.$g_log_step.', 0, 1, "'.addslashes($l_sql3).'", '.$_SESSION['__id'].', now())';
				sql_exe($l_log_sql);
			}
			
			$l_sql = 'update receivings set posted = 0 where id = '.$_GET['id'];
			sql_exe($l_sql);
		}
	}
	
	// proknjizi
	if($_GET['type'] == 2){
		$l_posted = get_item('select posted from receivings where id = '.$_GET['id']);
		if($l_posted == 0){
			$l_type = get_item('select type from receivings where id = '.$_GET['id']);
			
			$l_sql = 'select article_id, price_buy, price_sell, amount, koef_troska, discount '.
				' from receiving_items where receiving_id = '.$_GET['id'].
				' order by article_id ';
			$x = get_data($l_sql, 1);
			for($i = 0; $i < sizeof($x['article_id']); $i++){
				$l_sql2 = 'select amount, price_buy, price_sell from articles where id = '.$x['article_id'][$i];
				$y = get_data($l_sql2, 0);
				
				switch($l_type){
					case 0:
						$l_kol_z = $y['amount'];
						$l_kol_p = $x['amount'][$i];
						
						if($y['amount'] < 0){
							$l_sql3 = 'update articles '.
								' set amount = '.($l_kol_p + $l_kol_z).', '.
								//' price_buy = '.$x['price_buy'][$i].', '.
								' price_buy = '.($x['price_buy'][$i] * (1 - $x['discount'][$i] / 100) * $x['koef_troska'][$i]).', '.
								' price_sell = '.$x['price_sell'][$i].
								' where id = '.$x['article_id'][$i];
						}else{
							$l_sql3 = 'update articles '.
								'set amount = '.($l_kol_p + $l_kol_z).', '.
								//' price_buy = '.(($l_kol_p * $x['price_buy'][$i] + $l_kol_z * $y['price_buy']) / ($l_kol_p + $l_kol_z)).', '.
								' price_buy = '.($x['price_buy'][$i] * (1 - $x['discount'][$i] / 100) * $x['koef_troska'][$i]).', '.
								' price_sell = '.$x['price_sell'][$i].
								' where id = '.$x['article_id'][$i];
						}
						
						
						if($y['price_sell'] != $x['price_sell'][$i]){
							$l_bar_code = get_item('select barcode from articles where id = '.$x['article_id'][$i]);
							
							$l_sql_jos = 'insert into zopc values(0, now(), "'.$x['article_id'][$i].'", "'.($l_kol_p + $l_kol_z).'", "'.$y['price_sell'].'", '.
								' "'.$x['price_sell'][$i].'", "'.round(($l_kol_p + $l_kol_z) * $y['price_sell']).'", '.
								' "'.round(($l_kol_p + $l_kol_z) * $x['price_sell'][$i], 2).'", '.
								' "'.round(($l_kol_p + $l_kol_z) * ($x['price_sell'][$i] - $y['price_sell'])).'", "3", "'.$l_bar_code.'" )';
							sql_exe($l_sql_jos);
							
							$g_log_step = 0;
							$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);
						}
						
						
						
						break;
						
					case 1:
					case 2:
					case 3:
						$l_kol_z = $y['amount'];
						$l_kol_p = -1 * abs($x['amount'][$i]);
						
						$l_sql3 = 'update articles '.
							' set amount = '.($l_kol_p + $l_kol_z).
							' where id = '.$x['article_id'][$i];
						break;
						
					case 4:
						$l_kol_z = $y['amount'];
						$l_kol_p = $x['amount'][$i];
						
						$l_sql3 = 'update articles '.
							' set amount = '.($l_kol_p + $l_kol_z).' '.
							//' price_buy = '.$x['price_buy'][$i].', '.
							//' price_sell = '.$x['price_sell'][$i].
							' where id = '.$x['article_id'][$i];
							
							
						
						/*
						if($y['price_sell'] != $x['price_sell'][$i]){
							$l_bar_code = get_item('select barcode from articles where id = '.$x['article_id'][$i]);
							
							$l_sql_jos = 'insert into zopc values(0, now(), "'.$x['article_id'][$i].'", "'.($l_kol_p + $l_kol_z).'", "'.$y['price_sell'].'", '.
								' "'.$x['price_sell'][$i].'", "'.round(($l_kol_p + $l_kol_z) * $y['price_sell']).'", '.
								' "'.round(($l_kol_p + $l_kol_z) * $x['price_sell'][$i], 2).'", '.
								' "'.round(($l_kol_p + $l_kol_z) * ($x['price_sell'][$i] - $y['price_sell'])).'", "3", "'.$l_bar_code.'" )';
							sql_exe($l_sql_jos);
							
							$g_log_step = 0;
							$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);
						}
						*/
						
						
						
							
						break;
					
					case 6:
						// Skladiste - amount moze biti + ili -, proknjizavanje dodaje amount
						$l_kol_z = $y['amount'];
						$l_kol_p = $x['amount'][$i]; // bez abs(), direktno kako je uneseno
						
						$l_sql3 = 'update articles '.
							' set amount = '.($l_kol_p + $l_kol_z).
							' where id = '.$x['article_id'][$i];
						break;
					
					default:
						pe('Pogreška 568439. Ne postoji tip transakcije koji ste pokušali upisati/mijenjati!');
				}
				
				sql_exe($l_sql3);
				
				
				$g_log_step = 0;
				$l_log_sql = 'insert into meta_log_data values(0, '.$g_log_step.', 0, 1, "'.addslashes($l_sql3).'", '.$_SESSION['__id'].', now())';
				sql_exe($l_log_sql);
			}
			
			$l_sql = 'update receivings set posted = 1 where id = '.$_GET['id'];
			sql_exe($l_sql);
		}
	}
	
	print '1';
?>
