<?php

$sqln = "select * from invtranstype";
$itt_con = $conn->query($sqln) or die($sqln);
while($itt = $itt_con->fetch_assoc()){
	$x_itt_autoid = $itt['itt_autoid'];
	$x_itt_desc[$x_itt_autoid] = $itt['itt_desc'];
	}

$sql = "SELECT * FROM invtrans a, invtransd b, mst_product c 
		WHERE a.invt_autoid = b.invt_autoid AND b.pm_autoid = c.pm_autoid AND a.invt_autoid = '$autoid'";
$sql_con = $conn->query($sql) or die($sql);
while($rs = $sql_con->fetch_assoc()){
		$transno = $rs['invt_transno'];
		$transdate 		= $rs['invt_transdate'];
		$pm_autoid 	= $conn->real_escape_string($rs['pm_autoid']);
		$pm_code 	= $conn->real_escape_string($rs['pm_code']);
		$pm_name 	= $conn->real_escape_string($rs['pm_name']);
		$qty 			= $rs['invtd_qty'];
		$f_whs	 		= $rs['invt_fwhs'];
		$t_whs	 		= $rs['invt_twhs'];
		$whs_code 		= $conn->real_escape_string($rs['whs_code']);
		$whs_name 		= $conn->real_escape_string($rs['whs_name']);
		$ums 			= strtoupper($rs['pm_ums']);
		$trans_attr 	= $rs['itt_code'];
		$itt_autoid 	= $rs['itt_autoid'];
		
		$itt_desc = $x_itt_desc[$itt_autoid];
		
		
		$convert = $rs['pm_ums_convert'];
		if(!$convert){$convert = 1;};
		
		
		// new //
		$umb = strtoupper($rs['pm_umb']);
		$invt_ums = $rs['invtd_um'];
		
		
		if($invt_ums == $umb){
			$qty = $qty * $convert; // convert to ums qty
			$price = $rs['invtd_bprice']; // hrg satuan besar //
			$ums_price = toDecimal( $rs['invtd_bprice'] / $convert);
			}
		else{
			$ums_price = toDecimal($rs['invtd_bprice']);
			$price = toDecimal($rs['invtd_bprice'] * $convert );			
			}
		

		
		$outlet = $rs['outlet_id'];
		
		if($trans_attr=="+"){
			$sql_in = "INSERT INTO invlog 
					(log_refno,log_refdate,log_refautoid,pm_autoid,pm_code,
					pm_name,log_inqty,whs_autoid,log_userentry,log_dateentry,log_reftype,
					itt_autoid,log_ums,itt_desc,log_price,log_avg_price,curr_qty,outlet_id) 
					VALUES 
					('$transno','$transdate','$autoid','$pm_autoid','$pm_code',
					'$pm_name','$qty','$t_whs','$userid',CURRENT_TIMESTAMP(),'INVT','
					$itt_autoid','$ums', '$itt_desc','$ums_price','$price','$qty','$outlet')";
			if($qty>0){
				if (!$conn->query($sql_in) === TRUE) { echo "Error: " . $$sql_in . "<br>" . $conn->error;return false;}	}

				}

		elseif($trans_attr=="-"){
			$sql_in = "INSERT INTO invlog 
					(log_refno,log_refdate,log_refautoid,pm_autoid,pm_code,
					pm_name,log_outqty,whs_autoid,log_userentry,log_dateentry,log_reftype,
					itt_autoid,log_ums,itt_desc,log_price,log_avg_price,outlet_id) 
					VALUES 
					('$transno','$transdate','$autoid','$pm_autoid','$pm_code',
					'$pm_name','$qty','$t_whs','$userid',CURRENT_TIMESTAMP(),'INVT','
					$itt_autoid','$ums', '$itt_desc','$ums_price','$price','$outlet')";
			if($qty>0){
				if (!$conn->query($sql_in) === TRUE) { echo "Error: " . $$sql_in . "<br>" . $conn->error;return false;}	}
				// curr_stok_update($pm_autoid,$qty,$outlet,$conn,"Y");

				}	
//			$last_stok = get_stok_info($pm_autoid,$outlet,$conn);
			
			if($last_stok<=0){
				$update = "UPDATE invlog set curr_qty = 0 WHERE pm_autoid = '$pm_autoid' AND outlet_id = '$outlet'";
				//echo $last_stok;
			//	$conn->query($update);
				}
			//	echo $sql_in."<br>";
	}

				$sql = "UPDATE invtrans SET invt_status = 2 WHERE invt_autoid = '$autoid'";
				if (!$conn->query($sql) === TRUE) { echo "Error: " . $sql . "<br>" . $conn->error;return false;}	
				header('Location:index?src='.$act.'');
?>