<?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'];
	}
$gtotal = 0;
$sql = "SELECT * FROM poheader a, podetail b, mst_product c 
		WHERE a.po_autoid = b.po_autoid AND b.pm_autoid = c.pm_autoid AND a.po_autoid = '$autoid'";
$sql_con = $conn->query($sql) or die($sql);
while($rs = $sql_con->fetch_assoc()){
		$transno 	= $rs['po_transno'];
		$transdate 	= $rs['po_transdate'];
		$pm_autoid 	= $conn->real_escape_string($rs['pm_autoid']);
		$pm_code 	= $_pm_code[$pm_autoid];
		$pm_name 	= $conn->real_escape_string($rs['pm_name']);


		$ven_name		= $conn->real_escape_string($_ven_name[$rs['ven_autoid']] );
		$pod_expdate = $rs['pod_expdate'];
		
		$itt_desc 		= "Pembelian No. ".$transno. " Tgl. ".dtostr($transdate). " - ".$ven_name;
		
		$qty 			= $rs['pod_qty'];
		$ums 			= strtoupper($rs['pod_ums']);			
		$convert		= $_pm_convert[$pm_autoid];
		$subtotal		= $rs['pod_subtotal'];
		
		if($convert==0){ $convert = 1;}
		
		if($ums == $_pm_umb[$pm_autoid]){ // convert if um = umb //
			$in_qty = $qty * $convert;	
			$price = $subtotal / $in_qty; 
			$ums = $_pm_ums[$pm_autoid];
		}
		
		else{
			$in_qty = $qty;
			$price = $subtotal / $in_qty;
			}
		
		
			$umb_price = $price * $convert;
		
			$price = toDecimal($price);
			
			$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,whs_code,whs_name,
					itt_autoid,log_ums,itt_desc,curr_qty,log_price,log_expdate,log_avg_price) 
					VALUES 
					('$transno','$transdate','$autoid','$pm_autoid','$pm_code',
					'$pm_name','$in_qty','1','$userid',CURRENT_TIMESTAMP(),'PO','$t_whs','$whs_name',
					'23','$ums', '$itt_desc','$in_qty','$price','$pod_expdate','$umb_price')";
			if($qty>0){
				if (!$conn->query($sql_in) === TRUE) { echo "Error: " . $$sql_in . "<br>" . $conn->error;return false;}	}
				
					$_stok 			= get_avg_stok($pm_autoid,0,$conn);
					$stok 			= explode(";",$_stok);
					$avg_price 		= $stok[0];
					$in_stok_qty	= $stok[1];
			
				$new_avg_price 		= $avg_price * $convert;
				
				$new_avg_price  	=  toDecimal($new_avg_price);				
				
				$update = "UPDATE mst_product SET pm_bprice = '$price',pm_avg_price = '$new_avg_price' WHERE pm_autoid = '$pm_autoid'";
				$conn->query($update) or die($update);			
		

	$gtotal = $gtotal + $subtotal;
	} // end while

	/* JURNAL TRANSACTION
		$conn->query("update setupautoid SET jurnal  = jurnal + 1 WHERE id = 1");

		$sql_autoid = "SELECT * FROM setupautoid";
		$dcon = $conn->query($sql_autoid) or die($sql_autoid);
		$rs = $dcon->fetch_array();
		$jt_autoid = $rs['jurnal'];

		$coa_stok = '11';

			$sql_jtheader = "INSERT INTO jtheader 
					(jt_autoid,jt_transno,jt_transdate,jt_vcno,jt_vcdate,ref_type,ref_autoid,jt_flagpost,jt_userentry,jt_dateentry) 
					VALUES 
					('$jt_autoid','$transno','$transdate','$transno','$transdate','PO','$autoid','Y','SYS',CURRENT_TIMESTAMP())";
			$conn->query($sql_jtheader) or die($sql_jtheader);		
			$sql_jtdetail = "INSERT INTO jtdetail 
						(jt_autoid,jtd_vcno,jtd_vcdate,coa_autoid,jtd_credit,coa_autoid2,jtd_debit2) 
						VALUES 
						('$jt_autoid','$transno','$transdate','$coa_autoid','$gtotal','$coa_stok','$gtotal')";		
			$conn->query($sql_jtdetail) or die($sql_jtdetail);
	*/
				$sql = "UPDATE poheader SET po_status = 1 WHERE po_autoid = '$autoid'";
				if (!$conn->query($sql) === TRUE) { echo "Error: " . $sql . "<br>" . $conn->error;return false;}	
				header('Location:index?src='.$act.'');
?>