<?php




function get_avg_price_outlet($pm_autoid,$req_qty,$outlet,$conn,$db_update,$info){

	if(!$outlet){$outlet = 0;}
	
	// get the data //
	$log_result = array();
	$sql = "SELECT * FROM invlog a, mst_product b where a.pm_autoid = b.pm_autoid AND 
			a.pm_autoid = '$pm_autoid' AND a.curr_qty > 0 AND a.outlet_id = '$outlet' ORDER BY a.log_refdate ASC, a.log_autoid ASC ";
	$sql_con = $conn->query($sql) or die($sql);
	while($rs = $sql_con->fetch_assoc()){
		$log_price 		= $rs['log_price'];
		$curr_qty		= $rs['curr_qty'];
		$log_autoid 	= $rs['log_autoid'];
		$pm_name	 	= $rs['pm_name'];
		$ums			= $rs['log_ums'];
		$pm_code		= $rs['pm_code'];
		$log_result[] = $log_price.";".$curr_qty.";".$log_autoid.";".$pm_name.";".$ums.";".$pm_code;
	}


$_info = explode(",",$info);
$ref_autoid 	= $_info[0];
$ref_transno 	= $_info[1];
$ref_transdate 	= $_info[2];
$ref_desc		= $_info[3];


if($ref_desc=="PRODUKSI"){
		$itt_autoid = 32;
		}

$total_req_qty = $req_qty;
$subtotal = 0;
for($i=0;$i<5;$i++){
	
	$log = explode(";",$log_result[$i]);
	
	$price 			= $log[0];
	$curr_qty 		= $log[1];
	$log_autoid 	= $log[2];
	$pm_name 		= $conn->real_escape_string($log[3]);
	$pm_ums			= $log[4];
	$pm_code		= $log[5];
	$itt_desc		= "Stok Keluar - Produksi : ".$ref_transno. " - ".dtostr($ref_transdate);
	

	if($req_qty >= $curr_qty){
		$update = "UPDATE invlog SET curr_qty = curr_qty - $curr_qty WHERE log_autoid = '$log_autoid'";
		if($db_update=="Y"){$conn->query($update) or die($update);}
		$insert = "INSERT INTO invlog 
					(log_refno,log_refdate,log_refautoid,pm_autoid,pm_code,pm_name,log_ums,
					itt_autoid,itt_desc,
					outlet_id,log_outqty,log_price,log_userentry,log_dateentry,log_reftype)
					VALUES
					('$ref_transno','$ref_transdate','$ref_autoid','$pm_autoid','$pm_code','$pm_name','$pm_ums',
					'$itt_autoid','$itt_desc',
					'$outlet','$curr_qty','$price','SYS',CURRENT_TIMESTAMP(),'$ref_desc')";
		//if($db_update=="Y"){$conn->query($insert) or die($insert);}
		$subtotal = $subtotal + ($curr_qty * $price);
		}
	else{

		$update = "UPDATE invlog SET curr_qty = curr_qty - $req_qty WHERE log_autoid = '$log_autoid'";
		if($db_update=="Y"){$conn->query($update) or die($update);}
		$insert = "INSERT INTO invlog 
					(log_refno,log_refdate,log_refautoid,pm_autoid,pm_code,pm_name,log_ums,
					itt_autoid,itt_desc,
					outlet_id,log_outqty,log_price,log_userentry,log_dateentry,log_reftype)
					VALUES
					('$ref_transno','$ref_transdate','$ref_autoid','$pm_autoid','$pm_code','$pm_name','$pm_ums',
					'$itt_autoid','$itt_desc',
					'$outlet','$req_qty','$price','SYS',CURRENT_TIMESTAMP(),'$ref_desc')";
		//if($db_update=="Y"){$conn->query($insert) or die($insert);}
		
		$subtotal = $subtotal + ($req_qty * $price);
	}

	$req_qty = $req_qty - $curr_qty;

	if($req_qty<=0){break;}


}
return $subtotal / $total_req_qty;

	
}			

function get_avg_stok_outlet($pm_autoid,$outlet,$conn){
	
	if(!$outlet){$outlet = 0;}
	
	
	$sql = "SELECT SUM(a.curr_qty) as qty, b.pm_avg_price FROM invlog a, mst_product b WHERE a.pm_autoid = b.pm_autoid 
			AND a.pm_autoid = '$pm_autoid' AND outlet_id = '$outlet' 
			AND a.log_inqty > 0 AND a.curr_qty > 0 ORDER BY a.log_autoid DESC";
	$sql_con = $conn->query($sql) or die($sql);
	$rs = $sql_con->fetch_assoc();
	$pm_avg_price 	= $rs['pm_avg_price'];
	$qty 			= $rs['qty'];
	
	return $qty;
}

function get_last_price_outlet($pm_autoid,$outlet,$conn){


	$sql = "SELECT SUM(curr_qty * log_avg_price) as subtotal, SUM(curr_qty) as total_qty FROM invlog WHERE 
			pm_autoid = '$pm_autoid' 
			AND outlet_id = '$outlet' 
			AND curr_qty > 0 ORDER BY log_refdate DESC, log_autoid DESC ";

	$dcon = $conn->query($sql) or die($sql);
	$rs = $dcon->fetch_assoc();
	
	$total_price = $rs['subtotal'];
	$qty 			= $rs['total_qty'];
	

	if(!$qty){$qty = 1;}
	
	$log_price = $total_price/$qty;
	//$convert = $rsd['pm_ums_convert'];
	if(!$convert){$convert = 1;}
	
	return $log_price ;

}

function get_hpp_avg_outlet($kode,$conn,$dine_in,$outlet,$info,$order_qty,$db_update){
	
	$sql = "SELECT * FROM mst_product WHERE pm_code = '$kode'";
	$dcon = $conn->query($sql) or die($sql);
	$rs = $dcon->fetch_assoc();
	$pm_autoid 	= $rs['pm_autoid'];
	
	
	
	$gas 		= $rs['pm_gas'] ;
	$pm_waste 	= $rs['pm_waste'];
	$prod_qty	= $rs['pm_prod_qty'];
	
	if(!$prod_qty){$prod_qty = 1;}
	
	if(!$db_update){$db_update = "N";}
	
	
	$x = "";
	$total_ing = 0;
	$sqlt = "SELECT * FROM mst_product_detail a, mst_product b  WHERE a.pmd_pm_autoid = b.pm_autoid 
			AND a.pm_autoid = '$pm_autoid' AND a.pmd_role = 'ingredient'";
	$dcont = $conn->query($sqlt) or die($sqlt);
	while($rsd = $dcont->fetch_assoc()){
		
		$qty 			= $rsd['pmd_qty'] ;
		
		$pmd_autoid 	= $rsd['pmd_pm_autoid'];
		
		//$pm_bprice 		= $rsd['pm_avg_price'];
		//$pm_bprice 	= get_fifo_price($pmd_autoid,$qty,0,$conn,$db_update,$info); //  get latest price //
		

		
		$pm_bprice		= get_last_price_outlet($pmd_autoid,$outlet,$conn);
		$add_cost 		= $rsd['pmd_add_cost'];
		$convert 		= 1/$add_cost;
		
		$pm_bprice 		= $pm_bprice ;

		$waste 			= $rsd['pmd_waste'];
		$pmd_role 		= $rsd['pmd_role'];
		
		$cost_waste = ($qty/ (100 - $waste)) * 100;

		$hpp_subtotal = $pm_bprice * $add_cost *  $cost_waste;
		
		$total_ing = $total_ing + $hpp_subtotal;

		$x = $x. $rsd['pm_name'] . " - ".$pm_bprice;
	}
	//return $x;

	$total_pack = 0;
	$total_pack_batch = 0;
	$sqlt = "SELECT * FROM mst_product_detail a, mst_product b  WHERE a.pmd_pm_autoid = b.pm_autoid 
			AND a.pm_autoid = '$pm_autoid' AND a.pmd_role = 'packaging'";
	$dcont = $conn->query($sqlt) or die($sqlt);
	while($rsd = $dcont->fetch_assoc()){
		
		$qty 	= $rsd['pmd_qty'] ;
		
		
		$pmd_autoid = $rsd['pmd_pm_autoid'];
		
		$price			= get_last_price_outlet($pmd_autoid,$outlet,$conn);
		$pm_convert = $rsd['pm_ums_convert'];
		if(!$pm_convert){$pm_convert = 1;}
		$price = $price / $pm_convert;
		
		if($rsd['pmd_qty_setting'] == "BATCH"){
			$price = $price / $prod_qty;
			$subtotal = ($qty * $price) ;
			$xtotal = $subtotal * $prod_qty;
			$total_pack_batch = $total_pack_batch + $xtotal;

		}else{
			$subtotal = ($qty * $price) * $prod_qty;
			$total_pack = $total_pack + $subtotal;
		}

	}

	


	$total_group = $total_ing + $gas;
	
	$hpp_per_item = ($total_group / $prod_qty);


	$pm_waste_cost	= $hpp_per_item/(100-$pm_waste);

	$pm_total_hpp = $pm_waste_cost * 100;
	
	
	
	if($dine_in == "Y"){$total_pack = 0;}
	
	$total_hpp = ($pm_total_hpp) * $prod_qty  + $total_pack + $total_pack_batch;

	return $total_hpp;
	//return $total_pack;

}







?>