<?php



// GET VENDOR //
$sql_pn = "select * from mst_vendor";
$pn_conn = $conn->query($sql_pn) or die($sql_pn);
while($pn = $pn_conn->fetch_array()){
	$_ven_autoid = $pn['ven_autoid'];
	$_ven_name[$_ven_autoid] = $pn['ven_name'];
}

// INV TRANS TYPE
$sql_pn = "select * from invtranstype";
$pn_conn = $conn->query($sql_pn) or die($sql_pn);
while($pn = $pn_conn->fetch_array()){
	$_itt_autoid = $pn['itt_autoid'];
	$_itt_desc[$_itt_autoid] = $pn['itt_desc'];
}


$sqlcoa = "SELECT * FROM mst_coa";
$prop_conn = $conn->query($sqlcoa) or die($sqlcoa);
while($coa = $prop_conn->fetch_assoc()){;
	$coa_id = $coa['coa_autoid'];
	$_coa_accno[$coa_id] = $coa['coa_accno'];
	$_coa_accdesc[$coa_id] = $coa['coa_accdesc'];
}

// GET OUTLET //
$sqlcoa = "SELECT * FROM mst_customer";
$prop_conn = $conn->query($sqlcoa) or die($sqlcoa);
while($coa = $prop_conn->fetch_assoc()){;
	$_outlet_id = $coa['cust_autoid'];

	$_outlet_name[$_outlet_id] = $coa['cust_name'];
}


// GET PRODUCT //
$sqld = "SELECT * FROM mst_product";
$dcond = $conn->query($sqld) or die($sqld);
while($rowd = $dcond->fetch_array()){
	$_pm_autoid = $rowd['pm_autoid'];

	$_pm_code[$_pm_autoid] = $rowd['pm_code'];
	$_pm_name[$_pm_autoid] = $rowd['pm_name'];

	$_pm_ums[$_pm_autoid] = strtoupper($rowd['pm_ums']);
	$_pm_umb[$_pm_autoid] = strtoupper($rowd['pm_umb']);				
	$_pm_convert[$_pm_autoid] = $rowd['pm_ums_convert'];
}


// GET KATEGORI //
$sqld = "SELECT * FROM mst_partclass";
$dcond = $conn->query($sqld) or die($sqld);
while($rowd = $dcond->fetch_array()){
	$_pt_autoid = $rowd['pt_autoid'];
	$_pc_autoid = $rowd['pc_autoid'];
	$_pt_name[$_pc_autoid] = $rowd['pt_name'];
	$_pc_name[$_pc_autoid] = $rowd['pc_name'];				
}


// GET WAREHOUSE //
$sqlwhs = "select * from warehouse ";
$dconwhs = $conn->query($sqlwhs) or die($sqlwhs);
while($rswhs = $dconwhs->fetch_assoc()){
		$whsid = $rswhs['whs_autoid'];
		$whsname[$whsid] = $rswhs['whs_name'];
};	


function getExtension($str) {
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
 }


	
$sql_access = "SELECT * FROM useraccess WHERE user_id = '$userid'";
$dcon_acs = $conn->query($sql_access) or die($sql_access);
$acs = $dcon_acs->fetch_array();
	$order_flag 	= $acs['order_flag'];
	$inv_flag 		= $acs['inv_flag'];
	$md_flag 		= $acs['md_flag'];
	$rpt_flag 		= $acs['rpt_flag'];
	$content_flag 	= $acs['content_flag'];
	$setting_flag 	= $acs['setting_flag'];
	$prc_flag 		= $acs['prc_flag'];
	$prd_flag 		= $acs['prd_flag'];
	$acc_flag 		= $acs['acc_flag'];	
	
	
	
/* INPUT FORM */

function get_input_terms($connection,$rs_value,$onchange){
	
	if($connection){
	$sql_terms = "select * from mst_terms ORDER BY terms_id";
	$terms_con = $connection->query($sql_terms) or die($sql_terms);
	$input_terms = "<select class='input w-80' name='mst_terms' id='mst_terms'> <option value=''>Select Terms</option>"; 			

	 //  create content from the loop    
		while($rs_terms = $terms_con->fetch_assoc()) {
			$terms_id = $rs_terms['terms_id'];
			if($rs_value==$terms_id){
			   $input_terms.= "<option value='".$terms_id."' selected='selected'>".$rs_terms['terms_desc']."</option>";
			}else{
				$input_terms.= "<option value='".$terms_id."'>".$rs_terms['terms_desc']."</option>";
				}
		}
		// and close the table outside the loop
		$input_terms.="</select>";
		return $input_terms;
	}
}

function get_input_disctype($rs_value){
	if($rs_value=="P"){
		$persen_selected = 'selected="selected"';
		}
	if($rs_value=="R"){
		$rp_selected = 'selected="selected"';
		}
		
	$input_disctype = '<select class="input" name="mst_disctype"  id="mst_disctype" onchange="calc_gtotal()">
							<option '.$rp_selected.' value="R">Rp.</option>
							<option '.$persen_selected.' value="P">%</option>
						</select>';
		return $input_disctype;
	}

$arr_month = array("","Januari","Februari","Maret","April","Mei","Juni","Juli","Agustus","September","Oktober","November","Desember");


/*
function getHpp($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'] * $order_qty;
	$pm_waste 	= $rs['pm_waste'] * $order_qty;

	
	if(!$db_update){$db_update = "N";}
	
	$total_ing = 0;
	$sqlt = "SELECT * FROM mst_product_detail  WHERE pm_autoid = '$pm_autoid' AND pmd_role = 'ingredient'";
	$dcont = $conn->query($sqlt) or die($sqlt);
	while($rsd = $dcont->fetch_assoc()){
		
		$qty = $rsd['pmd_qty'] * $order_qty;
		
		$pmd_autoid = $rsd['pmd_pm_autoid'];
		
		//$pm_bprice = $rsd['pmd_price'];

		$pm_bprice 		= get_fifo_price($pmd_autoid,$qty,0,$conn,$db_update,$info); //  get latest price //
		
		$add_cost 		= $rsd['pmd_add_cost'];
		$convert 		= 1/$add_cost;
		
		$pm_bprice = $pm_bprice * $convert;

		$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;

		
	}


	$total_pack = 0;
	$sqlt = "SELECT * FROM mst_product_detail  WHERE pm_autoid = '$pm_autoid' AND pmd_role = 'packaging'";
	$dcont = $conn->query($sqlt) or die($sqlt);
	while($rsd = $dcont->fetch_assoc()){
		
		$qty 	= $rsd['pmd_qty'] * $order_qty;
		
		
		$pmd_autoid = $rsd['pmd_pm_autoid'];
		
		
		$price 		= get_fifo_price($pmd_autoid,$qty,0,$conn,$db_update,$info); //  get latest price //

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

	
	if($pm_waste>0){
		$pm_waste_cost	= $total/(100-$pm_waste) * $order_qty;
	}
	



	$total_hpp = $total_ing + $gas + $pm_waste_cost + $total_pack;

	
	return $total_hpp;

}


function get_fifo_price($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 = '0' 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($pm_autoid,$outlet,$conn){
	
	if(!$outlet){$outlet = 0;}
	
	
	$sql = "SELECT SUM(a.curr_qty) as qty, b.pm_avg_price, SUM(a.curr_qty * a.log_price) as subtotal 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'];
	
	$subtotal 		= $rs['subtotal'];
	$qty 			= $rs['qty'];
	
	if(!$qty){$qty = 1;}
	
	$pm_avg_price = $subtotal/$qty;
	
	return $pm_avg_price.";".$qty;
}

// log price per satuan kecil //
function get_avg_price($pm_autoid,$outlet,$conn,$ums_type){
	
	if(!$outlet){$outlet = 0;}
	
	
	$sql = "SELECT SUM(a.curr_qty) as qty, b.pm_avg_price, SUM(a.curr_qty * a.log_price) as subtotal, b.pm_ums_convert 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'];
	
	$subtotal 		= $rs['subtotal'];
	$qty 			= $rs['qty'];
	
	if(!$qty){$qty = 1;}
	
	$pm_avg_price = $subtotal/$qty;
	$convert = $rs['pm_ums_convert'];
	
	if(!$convert){$convert = 1;}
	
	if($ums_type=="SMALL"){
			$price =  $pm_avg_price ;		
	}else{
			$price =  $pm_avg_price * $convert;
		}
		
	return toDecimal($price);
	
}





function toDecimal($val){
	if(!$val){$val = 0;}
	
	return round($val,2);
}

/*NOTES

invlog - log_price -> harga satuan kecil untuk central
CENTRAL - modul barang masuk -> purchase, inv transaction, produksi(ok)
OUTLET - modul brg masuk -> penerimaan REQ ORDER, inv trans, produksi
*/

?>