<?php
#Menghitung Jumlah Hari
function jumlah_hari($bulan = 0, $tahun = '') { 
	if($bulan < 1 OR $bulan > 12) { 
		return 0; 
	} 
	if( ! is_numeric($tahun) OR strlen($tahun) != 4) { 
		$tahun = date('Y'); 
	} 
	if ($bulan == 2) { 
		if ($tahun % 400 == 0 OR ($tahun % 4 == 0 AND $tahun % 100 != 0)) { 
			return 29; 
		} 
	} 
	$jumlah_hari = array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); 
	return $jumlah_hari[$bulan - 1]; 
}
#Get tanggal dan waktu Indonesia
function get_datetime()
{
	#Ambil waktu server terkini
	$dat_server = mktime(date("G"), date("i"), date("s"), date("n"), date("j"), date("Y"));
	#echo 'Waktu server (US): '.date("Y-m-d H:i:s", $dat_server);
	
	#Ambil perbedaan waktu server dengan GMT
	$diff_gmt = substr(date("O",$dat_server),1,2);
	#karena perbedaan waktu adalah dalam jam, maka kita jadikan detik
	$datdif_gmt = 60 * 60 * $diff_gmt;

	// Hitung perbedaannya
	if (substr(date("O",$dat_server),0,1) == "+"){
		$dat_gmt = $dat_server - $datdif_gmt;
	}else{
		$dat_gmt = $dat_server + $datdif_gmt;
	} 
	#echo 'Waktu GMT: '.date("Y-m-d H:i:s", $dat_gmt);
	
	#Hitung perbedaan GMT dengan waktu Indonesia (GMT+7)
	#karena perbedaan waktu adalah dalam jam, maka kita jadikan detik
	$datdif_id = 60 * 60 * 7;
	$dat_id = $dat_gmt + $datdif_id;
	#echo 'Waktu Indonesia (Asia/Jakarta): '.date("Y-m-d H:i:s", $dat_id);
	
	return date("Y-m-d H:i:s", $dat_id);
}
#Get tanggal Indonesia
function get_date()
{
	#Ambil waktu server terkini
	$dat_server = mktime(date("G"), date("i"), date("s"), date("n"), date("j"), date("Y"));
	#echo 'Waktu server (US): '.date("Y-m-d H:i:s", $dat_server);
	
	#Ambil perbedaan waktu server dengan GMT
	$diff_gmt = substr(date("O",$dat_server),1,2);
	#karena perbedaan waktu adalah dalam jam, maka kita jadikan detik
	$datdif_gmt = 60 * 60 * $diff_gmt;
	
	// Hitung perbedaannya
	if (substr(date("O",$dat_server),0,1) == "+"){
		$dat_gmt = $dat_server - $datdif_gmt;
	}else{
		$dat_gmt = $dat_server + $datdif_gmt;
	} 
	#echo 'Waktu GMT: '.date("Y-m-d H:i:s", $dat_gmt);
	
	#Hitung perbedaan GMT dengan waktu Indonesia (GMT+7)
	#karena perbedaan waktu adalah dalam jam, maka kita jadikan detik
	$datdif_id = 60 * 60 * 7;
	$dat_id = $dat_gmt + $datdif_id;
	#echo 'Waktu Indonesia (Asia/Jakarta): '.date("Y-m-d H:i:s", $dat_id);
	
	return date("Y-m-d", $dat_id);
}
#Get waktu Indonesia
function get_time()
{
	#Ambil waktu server terkini
	$dat_server = mktime(date("G"), date("i"), date("s"), date("n"), date("j"), date("Y"));
	#echo 'Waktu server (US): '.date("Y-m-d H:i:s", $dat_server);
	
	#Ambil perbedaan waktu server dengan GMT
	$diff_gmt = substr(date("O",$dat_server),1,2);
	#karena perbedaan waktu adalah dalam jam, maka kita jadikan detik
	$datdif_gmt = 60 * 60 * $diff_gmt;

	// Hitung perbedaannya
	if (substr(date("O",$dat_server),0,1) == "+"){
		$dat_gmt = $dat_server - $datdif_gmt;
	}else{
		$dat_gmt = $dat_server + $datdif_gmt;
	} 
	#echo 'Waktu GMT: '.date("Y-m-d H:i:s", $dat_gmt);
	
	#Hitung perbedaan GMT dengan waktu Indonesia (GMT+7)
	#karena perbedaan waktu adalah dalam jam, maka kita jadikan detik
	$datdif_id = 60 * 60 * 7;
	$dat_id = $dat_gmt + $datdif_id;
	#echo 'Waktu Indonesia (Asia/Jakarta): '.date("Y-m-d H:i:s", $dat_id);
	
	return date("H:i:s", $dat_id);
}
function get_romawi($bulan){
	 switch($bulan){
        case '01': return "I"; break;
        case '02': return "II"; break;
        case '03': return "III"; break;
        case '04': return "IV"; break;
        case '05': return "V"; break;
        case '06': return "VI"; break;
		case '07': return "VII"; break;
		case '08': return "VIII"; break;
		case '09': return "IX"; break;
		case '10': return "X"; break;
		case '11': return "XI"; break;
		case '12': return "XII"; break;
    };
}
#Start date
function get_startdate(){
	$dt0 = mysql_fetch_array(mysql_query("SELECT tiheula p_startdate FROM transfer_detail WHERE id=186"));
	return $dt0['p_startdate']; 
}
#end date
function get_enddate(){
	$dt01 = mysql_fetch_array(mysql_query("SELECT pandeuri p_enddate FROM transfer_detail WHERE id=186"));
	return $dt01['p_enddate'];
}
#Start date
function get_startdate2($inv){
	$dt1 = mysql_fetch_array(mysql_query("SELECT tiheula p_startdate FROM transfer_detail WHERE id=186",$inv));
	return $dt1['p_startdate']; 
}
#end date
function get_enddate2($inv){
	$dt2 = mysql_fetch_array(mysql_query("SELECT pandeuri p_enddate FROM transfer_detail WHERE id=186",$inv));
	return $dt2['p_enddate'];
}
#Start date
function get_startdateexbc(){
	$dt3 = mysql_fetch_array(mysql_query("SELECT p_startdate FROM periode WHERE p_status=30"));
	return $dt3['p_startdate']; 
}
#end date
function get_enddateexbc(){
	$dt4 = mysql_fetch_array(mysql_query("SELECT p_enddate FROM periode WHERE p_status=30"));
	return $dt4['p_enddate'];
}
#Get kode perusahaan
function get_code()
{
	$max_id = mysql_fetch_object(mysql_query("SELECT prof_code FROM profil"));
	return $max_id->prof_code;	
}
#Get start date -2 year
function start_date_m2year($sdate){
	$year= date('Y-m-d',strtotime('-2 year',strtotime($sdate)));
	return $year;
}
#Get SPB Last No
function get_spblastno($cat)
{
	$max_id = mysql_fetch_row(mysql_query("SELECT SUBSTR((trf_spbno),14,4) FROM transfer WHERE trf_category='$cat' ORDER BY id DESC LIMIT 1"));
	$max_id = $max_id[0];
	return $max_id;	
}	
#Get SPB Month
function get_spbmonth($cat)
{
	$max_id = mysql_fetch_row(mysql_query("SELECT SUBSTR((trf_spbno),11,2) FROM transfer WHERE trf_category='$cat' ORDER BY id DESC LIMIT 1"));
	$max_id = $max_id[0];
	return $max_id;	
}	
#Get SPB No
function getspbno($cat)
{
	$year  = date('Y');
	$month = date('m');
	$seq   = (int)get_spblastno($cat)+1;
	 if ($seq < 10)   $seq = "000".$seq;
	elseif($seq < 100)  $seq = "00".$seq;
	elseif($seq < 1000) $seq = "0".$seq;
	else $seq;
	$last_month = get_spbmonth($cat);
	$getcode = get_code();
	if($last_month == $month){
		$spb_no = "SPB-".$cat."-$year$month-$seq/$getcode";
	}else{
		$spb_no = "SPB-".$cat."-$year$month-0001/$getcode";
	}	
	return $spb_no;
}
#Get LPB Last No
function get_lpblastno($cat)
{
	$max_id = mysql_fetch_row(mysql_query("SELECT SUBSTR((rci_lpbno),14,4) FROM receive WHERE rci_category='$cat' ORDER BY id DESC LIMIT 1"));
	$max_id = $max_id[0];
	return $max_id;	
}	
#Get LPB Month
function get_lpbmonth($cat)
{
	$max_id = mysql_fetch_row(mysql_query("SELECT SUBSTR((rci_lpbno),11,2) FROM receive WHERE rci_category='$cat' ORDER BY id DESC LIMIT 1"));
	$max_id = $max_id[0];
	return $max_id;	
}	
#Get LBP No
function getlpbno($cat)
{
	$year  = date('Y');
	$month = date('m');
	$seq   = (int)get_lpblastno($cat)+1;
	 if ($seq < 10)   $seq = "000".$seq;
	elseif($seq < 100)  $seq = "00".$seq;
	elseif($seq < 1000) $seq = "0".$seq;
	else $seq;
	$last_month = get_lpbmonth($cat);
	$getcode = get_code();
	if($last_month == $month){
		$lpb_no = "LPB-".$cat."-$year$month-$seq/$getcode";
	}else{
		$lpb_no = "LPB-".$cat."-$year$month-0001/$getcode";
	}	
	return $lpb_no;
}
#Get SJlastno
function get_sjlastno()
{
	$max_id = mysql_fetch_row(mysql_query("SELECT SUBSTR((outh_sjno),11,4) FROM outgoing ORDER BY id DESC LIMIT 1"));
	$max_id = $max_id[0];
	return $max_id;	
}	
#Get SJMonth
function get_sjmonth()
{
	$max_id = mysql_fetch_row(mysql_query("SELECT SUBSTR((outh_sjno),8,2) FROM outgoing ORDER BY id DESC LIMIT 1"));
	$max_id = $max_id[0];
	return $max_id;	
}	
function get_sjno()
{
	  $year  = date('Y');
	  $month = date('m');
	  $seq   = (int)get_sjlastno()+1;
		 if ($seq < 10)   $seq = "000".$seq;
	  elseif($seq < 100)  $seq = "00".$seq;
	  elseif($seq < 1000) $seq = "0".$seq;
	  else $seq;
	  $last_month = get_sjmonth();
	  $getcode = get_code();
	  if($last_month == $month){
		$sjno = "SJ-$year$month-$seq/$getcode";
	  }else{
		$sjno = "SJ-$year$month-0001/$getcode";
	  }
	  return $sjno;
}
#Cek Stock
function cek_stock($prodid,$loc)
{
	$data=mysql_fetch_object(mysql_query("SELECT stock_qty FROM stock WHERE stock_prodid='$prodid' AND stock_loc='$loc'"));
	return $data->stock_qty;
}
#Cek Product on Stock
function cek_product($prodid,$loc)
{
	$data=mysql_fetch_object(mysql_query("SELECT stock_prodid FROM stock WHERE stock_prodid='$prodid' AND stock_loc='$loc'"));
	return $data->stock_prodid;
}
#konversi ke tanggal Indonesia
function DateToIndo($date) 
{ 
	/*$BulanIndo = array("Januari", "Februari", "Maret",
					   "April", "Mei", "Juni",
					   "Juli", "Agustus", "September",
					   "Oktober", "November", "Desember");*/
	$tahun = substr($date, 0, 4); 
	$bulan = substr($date, 5, 2); 
	$tgl   = substr($date, 8, 2); 	
	//$result = $tgl . " " . $BulanIndo[(int)$bulan-1] . " ". $tahun;
	$result = $tgl . "-" . $bulan . "-". $tahun;
	return($result);
}

#Get BPBlastno
function get_bpblastno()
{
	$max_id = mysql_fetch_row(mysql_query("SELECT SUBSTR((inch_bpbno),11,4) FROM incoming ORDER BY id DESC LIMIT 1"));
	$max_id = $max_id[0];
	return $max_id;	
}	
#Get BPBMonth
function get_bpbmonth()
{
	$max_id = mysql_fetch_row(mysql_query("SELECT SUBSTR((inch_bpbno),8,2) FROM incoming ORDER BY id DESC LIMIT 1"));
	$max_id = $max_id[0];
	return $max_id;	
}	
#Get BPBNo
function get_bpbno()
{
	  $year  = date('Y');
	  $month = date('m');
	  $seq   = (int)get_bpblastno()+1;
		 if ($seq < 10)   $seq = "000".$seq;
	  elseif($seq < 100)  $seq = "00".$seq;
	  elseif($seq < 1000) $seq = "0".$seq;
	  else $seq;
	  $last_month = get_bpbmonth();
	  $getcode = get_code();
	  if($last_month == $month){
		$bpbno = "BPB$year$month-$seq/$getcode";
	  }else{
		$bpbno = "BPB$year$month-0001/$getcode";
	  }
	  return $bpbno;
}
#Cek Kategori
function cek_kategori($prodid)
{
	$q = mysql_fetch_object(mysql_query("SELECT prod_cat FROM product WHERE id='$prodid'"));
	return $q->prod_cat;
}

#Get LPJ Last No
function get_lpjlastno()
{
	$max_id = mysql_fetch_row(mysql_query("SELECT SUBSTR((fgh_lpno),12,4) FROM finishgoods ORDER BY id DESC LIMIT 1"));
	$max_id = $max_id[0];
	return $max_id;	
}	
#Get LP Month
function get_lpjmonth()
{
	$max_id = mysql_fetch_row(mysql_query("SELECT SUBSTR((fgh_lpno),9,2) FROM finishgoods  ORDER BY id DESC LIMIT 1"));
	$max_id = $max_id[0];
	return $max_id;	
}	
#Get LP No
function getlpjno()
{
	$year  = date('Y');
	$month = date('m');
	$seq   = (int)get_lpjlastno()+1;
	 if ($seq < 10)   $seq = "000".$seq;
	elseif($seq < 100)  $seq = "00".$seq;
	elseif($seq < 1000) $seq = "0".$seq;
	else $seq;
	$last_month = get_lpjmonth();
	$getcode = get_code();
	if($last_month == $month){
		$lpj_no = "LPJ-$year$month-$seq/$getcode";
	}else{
		$lpj_no = "LPJ-$year$month-0001/$getcode";
	}	
	return $lpj_no;
}
#Get LPS Last No
function get_lpslastno()
{
	$max_id = mysql_fetch_row(mysql_query("SELECT SUBSTR((scrh_lpno),12,4) FROM scrap ORDER BY id DESC LIMIT 1"));
	$max_id = $max_id[0];
	return $max_id;	
}	
#Get LP Month
function get_lpsmonth()
{
	$max_id = mysql_fetch_row(mysql_query("SELECT SUBSTR((scrh_lpno),9,2) FROM scrap ORDER BY id DESC LIMIT 1"));
	$max_id = $max_id[0];
	return $max_id;	
}	
#Get LP No
function getlpsno()
{
	$year  = date('Y');
	$month = date('m');
	$seq   = (int)get_lpslastno()+1;
	 if ($seq < 10)   $seq = "000".$seq;
	elseif($seq < 100)  $seq = "00".$seq;
	elseif($seq < 1000) $seq = "0".$seq;
	else $seq;
	$last_month = get_lpsmonth();
	$getcode = get_code();
	if($last_month == $month){
		$lps_no = "LPS-$year$month-$seq/$getcode";
	}else{
		$lps_no = "LPS-$year$month-0001/$getcode";
	}	
	return $lps_no;
}
#Get STO Last No
function get_stolastno()
{
	$max_id = mysql_fetch_row(mysql_query("SELECT SUBSTR((stoh_no),12,4) FROM stockopname ORDER BY id DESC LIMIT 1"));
	$max_id = $max_id[0];
	return $max_id;	
}	
#Get STO Month
function get_stomonth()
{
	$max_id = mysql_fetch_row(mysql_query("SELECT SUBSTR((stoh_no),9,2) FROM stockopname ORDER BY id DESC LIMIT 1"));
	$max_id = $max_id[0];
	return $max_id;	
}	
#Get LP No
function getstono()
{
	$year  = date('Y');
	$month = date('m');
	$seq   = (int)get_stolastno()+1;
	 if ($seq < 10)   $seq = "000".$seq;
	elseif($seq < 100)  $seq = "00".$seq;
	elseif($seq < 1000) $seq = "0".$seq;
	else $seq;
	$last_month = get_stomonth();
	$getcode = get_code();
	if($last_month == $month){
		$sto_no = "STO-$year$month-$seq/$getcode";
	}else{
		$sto_no = "STO-$year$month-0001/$getcode";
	}	
	return $sto_no;
}
#Get ADJ Last No
function get_adjlastno()
{
	$max_id = mysql_fetch_row(mysql_query("SELECT SUBSTR((adjh_no),12,4) FROM adjusment ORDER BY id DESC LIMIT 1"));
	$max_id = $max_id[0];
	return $max_id;	
}	
#Get ADJ Month
function get_adjmonth()
{
	$max_id = mysql_fetch_row(mysql_query("SELECT SUBSTR((adjh_no),9,2) FROM adjusment ORDER BY id DESC LIMIT 1"));
	$max_id = $max_id[0];
	return $max_id;	
}	
#Get ADJ No
function getadjno()
{
	$year  = date('Y');
	$month = date('m');
	$seq   = (int)get_adjlastno()+1;
	 if ($seq < 10)   $seq = "000".$seq;
	elseif($seq < 100)  $seq = "00".$seq;
	elseif($seq < 1000) $seq = "0".$seq;
	else $seq;
	$last_month = get_adjmonth();
	$getcode = get_code();
	if($last_month == $month){
		$adj_no = "ADJ-$year$month-$seq/$getcode";
	}else{
		$adj_no = "ADJ-$year$month-0001/$getcode";
	}	
	return $adj_no;
}

#Upload Data
function get_supid($sup_code)
{
	$q=mysql_fetch_object(mysql_query("SELECT id FROM supplier WHERE sup_code='$sup_code'"));
	return $q->id;
}
function get_docidinc($doc_type)
{
	$q=mysql_fetch_object(mysql_query("SELECT id FROM document_type WHERE doc_type='$doc_type' AND doc_status=1"));
	return $q->id;
}
function get_prodid($prod_code)
{
	$q=mysql_fetch_object(mysql_query("SELECT id FROM product WHERE prod_code='$prod_code'"));
	return $q->id;
}
#Get BPBNo
function get_bpbnoupload($year,$month)
{
	  //$year  = substr(date('Y'),2,2);
	  //$month = date('m');
	  $seq   = (int)get_bpblastno()+1;
		 if ($seq < 10)   $seq = "000".$seq;
	  elseif($seq < 100)  $seq = "00".$seq;
	  elseif($seq < 1000) $seq = "0".$seq;
	  else $seq;
	  $last_month = get_bpbmonth();
	  $getcode = get_code();
	  if($last_month == $month){
		$bpbno = "BPB$year$month-$seq/$getcode";
	  }else{
		$bpbno = "BPB$year$month-0001/$getcode";
	  }
	  return $bpbno;
}
#Outgoing
function get_custid($cust_code)
{
	$q=mysql_fetch_object(mysql_query("SELECT id FROM customer WHERE cust_code='$cust_code'"));
	return $q->id;
}
function get_docidout($doc_type)
{
	$q=mysql_fetch_object(mysql_query("SELECT id FROM document_type WHERE doc_type='$doc_type' AND doc_status=2"));
	return $q->id;
}
function get_uploadsjno($year,$month)
{
	  //$year  = substr(date('Y'),2,2);
	  //$month = date('m');
	  $seq   = (int)get_sjlastno()+1;
		 if ($seq < 10)   $seq = "000".$seq;
	  elseif($seq < 100)  $seq = "00".$seq;
	  elseif($seq < 1000) $seq = "0".$seq;
	  else $seq;
	  $last_month = get_sjmonth();
	  $getcode = get_code();
	  if($last_month == $month){
		$sjno = "SJ-$year$month-$seq/$getcode";
	  }else{
		$sjno = "SJ-$year$month-0001/$getcode";
	  }
	  return $sjno;
}
function get_uploadspbno($cat,$year,$month)
{
	//$year  = date('Y');
	//$month = date('m');
	$seq   = (int)get_spblastno($cat)+1;
	 if ($seq < 10)   $seq = "000".$seq;
	elseif($seq < 100)  $seq = "00".$seq;
	elseif($seq < 1000) $seq = "0".$seq;
	else $seq;
	$last_month = get_spbmonth($cat);
	$getcode = get_code();
	if($last_month == $month){
		$spb_no = "SPB-".$cat."-$year$month-$seq/$getcode";
	}else{
		$spb_no = "SPB-".$cat."-$year$month-0001/$getcode";
	}	
	return $spb_no;
}
function get_uploadlpbno($cat,$year,$month)
{
	//$year  = date('Y');
	//$month = date('m');
	$seq   = (int)get_lpblastno($cat)+1;
	 if ($seq < 10)   $seq = "000".$seq;
	elseif($seq < 100)  $seq = "00".$seq;
	elseif($seq < 1000) $seq = "0".$seq;
	else $seq;
	$last_month = get_lpbmonth($cat);
	$getcode = get_code();
	if($last_month == $month){
		$lpb_no = "LPB-".$cat."-$year$month-$seq/$getcode";
	}else{
		$lpb_no = "LPB-".$cat."-$year$month-0001/$getcode";
	}	
	return $lpb_no;
}
function get_uploadlpjno($year,$month)
{
	//$year  = date('Y');
	//$month = date('m');
	$seq   = (int)get_lpjlastno()+1;
	 if ($seq < 10)   $seq = "000".$seq;
	elseif($seq < 100)  $seq = "00".$seq;
	elseif($seq < 1000) $seq = "0".$seq;
	else $seq;
	$last_month = get_lpjmonth();
	$getcode = get_code();
	if($last_month == $month){
		$lpj_no = "LPJ-$year$month-$seq/$getcode";
	}else{
		$lpj_no = "LPJ-$year$month-0001/$getcode";
	}	
	return $lpj_no;
}
function get_uploadlpsno($year,$month)
{
	//$year  = date('Y');
	//$month = date('m');
	$seq   = (int)get_lpslastno()+1;
	 if ($seq < 10)   $seq = "000".$seq;
	elseif($seq < 100)  $seq = "00".$seq;
	elseif($seq < 1000) $seq = "0".$seq;
	else $seq;
	$last_month = get_lpsmonth();
	$getcode = get_code();
	if($last_month == $month){
		$lps_no = "LPS-$year$month-$seq/$getcode";
	}else{
		$lps_no = "LPS-$year$month-0001/$getcode";
	}	
	return $lps_no;
}
function get_uploadstono($year,$month)
{
	//$year  = date('Y');
	//$month = date('m');
	$seq   = (int)get_stolastno()+1;
	 if ($seq < 10)   $seq = "000".$seq;
	elseif($seq < 100)  $seq = "00".$seq;
	elseif($seq < 1000) $seq = "0".$seq;
	else $seq;
	$last_month = get_stomonth();
	$getcode = get_code();
	if($last_month == $month){
		$sto_no = "STO-$year$month-$seq/$getcode";
	}else{
		$sto_no = "STO-$year$month-0001/$getcode";
	}	
	return $sto_no;
}
#Get WIP Last No
function get_wiplastno()
{
	$max_id = mysql_fetch_row(mysql_query("SELECT SUBSTR((wip_no),12,4) FROM wip ORDER BY id DESC LIMIT 1"));
	$max_id = $max_id[0];
	return $max_id;	
}	
#Get ADJ Month
function get_wipmonth()
{
	$max_id = mysql_fetch_row(mysql_query("SELECT SUBSTR((wip_no),9,2) FROM wip ORDER BY id DESC LIMIT 1"));
	$max_id = $max_id[0];
	return $max_id;	
}	
#Get ADJ No
function getwipno()
{
	$year  = date('Y');
	$month = date('m');
	$seq   = (int)get_wiplastno()+1;
	 if ($seq < 10)   $seq = "000".$seq;
	elseif($seq < 100)  $seq = "00".$seq;
	elseif($seq < 1000) $seq = "0".$seq;
	else $seq;
	$last_month = get_wipmonth();
	$getcode = get_code();
	if($last_month == $month){
		$wip_no = "WIP-$year$month-$seq/$getcode";
	}else{
		$wip_no = "WIP-$year$month-0001/$getcode";
	}	
	return $wip_no;
}
function get_uploadwipno($year,$month)
{
	//$year  = date('Y');
	//$month = date('m');
	$seq   = (int)get_wiplastno()+1;
	 if ($seq < 10)   $seq = "000".$seq;
	elseif($seq < 100)  $seq = "00".$seq;
	elseif($seq < 1000) $seq = "0".$seq;
	else $seq;
	$last_month = get_wipmonth();
	$getcode = get_code();
	if($last_month == $month){
		$wip_no = "WIP-$year$month-$seq/$getcode";
	}else{
		$wip_no = "WIP-$year$month-0001/$getcode";
	}	
	return $wip_no;
}
#Get BOM Last No
function get_bomlastno()
{
	$max_id = mysql_fetch_row(mysql_query("SELECT SUBSTR((bomh_no),12,4) FROM bom ORDER BY id DESC LIMIT 1"));
	$max_id = $max_id[0];
	return $max_id;	
}	
#Get BOM Last Month
function get_bommonth()
{
	$max_id = mysql_fetch_row(mysql_query("SELECT SUBSTR((bomh_no),9,2) FROM bom ORDER BY id DESC LIMIT 1"));
	$max_id = $max_id[0];
	return $max_id;	
}	
#Get BOM No
function getbomno()
{
	$month	   = substr(get_date(),5,2);
	$year 	   = substr(get_date(),0,4);
	$seq   = (int)get_bomlastno()+1;
	 if ($seq < 10)   $seq = "000".$seq;
	elseif($seq < 100)  $seq = "00".$seq;
	elseif($seq < 1000) $seq = "0".$seq;
	else $seq;
	$last_month = get_bommonth();
	$getcode = get_code();
	if($last_month == $month){
		$bom_no = "BOM-$year$month-$seq/$getcode";
	}else{
		$bom_no = "BOM-$year$month-0001/$getcode";
	}	
	return $bom_no;
}
#Get Nomor Pengeluaran
function get_nomorout($add){
    $seq   = (int)get_sjlastno()+$add;
    if ($seq < 10)   $seq = "000".$seq;
    elseif($seq < 100)  $seq = "00".$seq;
    elseif($seq < 1000) $seq = "0".$seq;
    else $seq;
	  
	return $seq;
}
function get_nosjout($add){
	$year  = date('Y');
	$month = date('m');
	$last_month = get_sjmonth();
	$getcode = get_code();
	if($last_month == $month){
		$sjno = "SJ-$year$month-".get_nomorout($add)."/$getcode";
	}else{
		$sjno = "SJ-$year$month-0001/$getcode";
	}
	return $sjno;
}
#Get Nomor Pemasukan
function get_nomorinc($add){
	$seq   = (int)get_bpblastno()+$add;
	if ($seq < 10)   $seq = "000".$seq;
    elseif($seq < 100)  $seq = "00".$seq;
    elseif($seq < 1000) $seq = "0".$seq;
    else $seq;
	
	return $seq;
}
function get_nobpbinc($add){
	$year  = date('Y');
	$month = date('m');
	$last_month = get_bpbmonth();
    $getcode = get_code();
    if($last_month == $month){
		$bpbno = "BPB$year$month-".get_nomorinc($add)."/$getcode";
    }else{
		$bpbno = "BPB$year$month-0001/$getcode";
    }
	return $bpbno;
}
function get_nodocument(){
	$year   = date('Y');
	$month  = date('m');
	$lno = mysql_fetch_array(mysql_query("SELECT SUBSTR(doch_regno,7,8) regno FROM document 
	 		  WHERE MONTH(doch_date)='$month' AND YEAR(doch_date)='$year' AND doch_status<>100 ORDER BY id DESC LIMIT 0,1"));
	$lmonth = mysql_fetch_array(mysql_query("SELECT SUBSTR(doch_regno,5,2) mth FROM document 
	 		  WHERE MONTH(doch_date)='$month' AND YEAR(doch_date)='$year' AND doch_status<>100 ORDER BY id DESC LIMIT 0,1"));
	$lastno = $lno['regno'];
	$lastmonth = $lmonth['mth'];
	if(empty($lastno)){
		$seq = 1;
	}else{
		$seq   = (int)$lastno+1;
	}
	if ($seq < 10)          $seq = "0000000".$seq;
    elseif($seq < 100)      $seq = "000000".$seq;
    elseif($seq < 1000)     $seq = "00000".$seq;
	elseif($seq < 10000)    $seq = "0000".$seq;
	elseif($seq < 100000)   $seq = "000".$seq;
	elseif($seq < 1000000)  $seq = "00".$seq;
	elseif($seq < 10000000) $seq = "0".$seq;
    else $seq;
	if($lastmonth == $month){
		$regno = $year."".$month."".$seq;
    }else{
		$regno = $year."".$month."00000001";
    }
	return $regno;
}
function get_stock($start_date2,$end_date2,$bulan2,$tahun2,$prdid,$prodcat,$inv){
	$year_sto  = substr($end_date2,0,4);
	$month_sto = substr($end_date2,5,2);
	if($prodcat==1){
		$stk =mysql_fetch_array(mysql_query("SELECT (ifnull(b.qty,0) + (ifnull(c.qty,0) + ifnull(g.qty,0)) - (ifnull(d.qty,0) + ifnull(h.qty,0)) + ifnull(e.qty,0)) as end_qty
			FROM product a             
			LEFT JOIN (SELECT stock_prodid, sum(stock_qty) as qty FROM stock WHERE MONTH(stock_date)='$bulan2' AND YEAR(stock_date)='$tahun2' AND stock_status=10 AND stock_loc=1 
					  GROUP BY stock_prodid) AS b ON b.stock_prodid = a.id
			LEFT JOIN (SELECT a.incd_prodid, sum(a.incd_qty) as qty from incoming_detail a 
					 inner join incoming b on b.id = a.incd_hid
					 where b.inch_status=20 and inch_docdate between '$start_date2' and '$end_date2' 
					 group by incd_prodid) AS c ON c.incd_prodid=a.id
			LEFT JOIN (select a.trfd_prodid, sum(a.trfd_qty) as qty from transfer_detailexbc a 
					 inner join transfer b on b.id = a.trfd_hid
					 where b.trf_status=20 and b.trf_category IN (1,2) and trf_spbdate between '$start_date2' and '$end_date2'
					 group by trfd_prodid) AS d ON d.trfd_prodid=a.id     
			LEFT JOIN (select a.adjd_prodid, sum(adjd_qty) as qty from adjusment_detail a
					   inner join adjusment b on b.id=a.adjd_hid
					   where b.adjh_status=20 and adjh_date between '$start_date2' and '$end_date2'
					   group by adjd_prodid) AS e ON e.adjd_prodid=a.id
			LEFT JOIN (select a.stod_prodid, max(b.stoh_date), a.stod_qtysaldo as qty from stockopname_detail a 
					   inner join stockopname b on b.id=a.stod_hid
					   where b.stoh_status=20 and month(b.stoh_date)='$month_sto' and year(b.stoh_date)='$year_sto'
					   group by a.stod_prodid) AS f  ON f.stod_prodid=a.id  
			LEFT JOIN (select a.trfd_prodid, sum(a.trfd_qty) as qty from transfer_detailexbc a 
					 inner join transfer b on b.id = a.trfd_hid
					 where b.trf_status=20 and b.trf_category IN (3,5) and trf_spbdate between '$start_date2' and '$end_date2'
					 group by trfd_prodid) AS g ON g.trfd_prodid=a.id	
			LEFT JOIN (SELECT a.outd_prodid, sum(a.outd_qty) as qty from outgoing_detail a 
			   inner join outgoing b on b.id = a.outd_hid
			   where b.outh_status=20 and outh_docdate between '$start_date2' and '$end_date2'
			   group by outd_prodid) AS h ON h.outd_prodid=a.id		 	      
			WHERE a.prod_cat IN(1) AND a.id='$prdid'  ORDER BY a.prod_code ASC",$inv));
	}elseif($prodcat==2){
		$stk =mysql_fetch_array(mysql_query("SELECT (ifnull(b.qty,0) + (ifnull(c.qty,0)+ifnull(h.qty,0)+ifnull(i.qty,0)) - (ifnull(d.qty,0)+ifnull(g.qty,0)) + ifnull(e.qty,0)) as end_qty
			FROM product a             
			LEFT JOIN (SELECT stock_prodid, SUM(stock_qty) as qty FROM stock WHERE MONTH(stock_date)='$bulan2' AND YEAR(stock_date)='$tahun2' AND stock_status=10 AND stock_loc IN(4) GROUP BY stock_prodid) AS b
					  ON b.stock_prodid = a.id
			LEFT JOIN (SELECT a.incd_prodid, sum(a.incd_qty) as qty from incoming_detail a 
					 inner join incoming b on b.id = a.incd_hid
					 where b.inch_status=20 and inch_docdate between '$start_date2' and '$end_date2' 
					 group by incd_prodid) AS c ON c.incd_prodid=a.id
			LEFT JOIN (SELECT a.outd_prodid, sum(a.outd_qty) as qty from outgoing_detail a 
					 inner join outgoing b on b.id = a.outd_hid
					 where b.outh_status=20 and outh_docdate between '$start_date2' and '$end_date2'
					 group by outd_prodid) AS d ON d.outd_prodid=a.id     
			LEFT JOIN (select a.adjd_prodid, sum(adjd_qty) as qty from adjusment_detail a
					   inner join adjusment b on b.id=a.adjd_hid
					   where b.adjh_status=20 and adjh_date between '$start_date2' and '$end_date2'
					   group by adjd_prodid) AS e ON e.adjd_prodid=a.id
			LEFT JOIN (select a.stod_prodid, max(b.stoh_date), a.stod_qtysaldo as qty from stockopname_detail a 
					   inner join stockopname b on b.id=a.stod_hid
					   where b.stoh_status=20 and month(b.stoh_date)='$month_sto' and year(b.stoh_date)='$year_sto'
					   group by a.stod_prodid) AS f  ON f.stod_prodid=a.id
			LEFT JOIN (select a.trfd_prodid, sum(a.trfd_qty) as qty from transfer_detailexbc a 
				   inner join transfer b on b.id = a.trfd_hid
				   where b.trf_status=20 and b.trf_category IN (1,2) and trf_spbdate between '$start_date2' and '$end_date2'
				   group by trfd_prodid) AS g ON g.trfd_prodid=a.id
			LEFT JOIN (select a.trfd_prodid, sum(a.trfd_qty) as qty from transfer_detailexbc a 
				   inner join transfer b on b.id = a.trfd_hid
				   where b.trf_status=20 and b.trf_category IN (3,5) and trf_spbdate between '$start_date2' and '$end_date2'
				   group by trfd_prodid) AS h ON h.trfd_prodid=a.id
			LEFT JOIN (select a.trfd_prodid, sum(a.trfd_qty) as qty from transfer_detail a 
				   inner join transfer b on b.id = a.trfd_hid
				   where b.trf_status=20 and b.trf_category IN (4) and trf_spbdate between '$start_date2' and '$end_date2'
				   group by trfd_prodid) AS i ON i.trfd_prodid=a.id			
			WHERE a.prod_cat IN(2)  AND a.id='$prdid' ORDER BY a.prod_code ASC",$inv));
	}elseif($prodcat==3){
		$stk =mysql_fetch_array(mysql_query("SELECT (ifnull(b.qty,0) + (ifnull(c.qty,0) + ifnull(h.qty,0) + ifnull(i.qty,0)) - (ifnull(d.qty,0) + ifnull(g.qty,0)) + ifnull(e.qty,0)) as end_qty
			FROM product a             
			LEFT JOIN (SELECT stock_prodid, SUM(stock_qty) as qty FROM stock WHERE MONTH(stock_date)='$bulan2' AND YEAR(stock_date)='$tahun2' AND stock_status=10 AND stock_loc=3 GROUP BY stock_prodid) AS b
					  ON b.stock_prodid = a.id
			LEFT JOIN (select a.trfd_prodid, sum(a.trfd_qty) as qty from transfer_detail a 
					 inner join transfer b on b.id = a.trfd_hid
					 where b.trf_status=20 and b.trf_category IN (4) and trf_spbdate between '$start_date2' and '$end_date2' 
					 group by trfd_prodid) AS c ON c.trfd_prodid=a.id                                       
			LEFT JOIN (SELECT a.outd_prodid, sum(a.outd_qty) as qty from outgoing_detail a 
					 inner join outgoing b on b.id = a.outd_hid
					 where b.outh_status=20 and outh_docdate between '$start_date2' and '$end_date2'
					 group by outd_prodid) AS d ON d.outd_prodid=a.id							    
			LEFT JOIN (select a.adjd_prodid, sum(adjd_qty) as qty from adjusment_detail a
					   inner join adjusment b on b.id=a.adjd_hid
					   where b.adjh_status=20 and adjh_date between '$start_date2' and '$end_date2'
					   group by adjd_prodid) AS e ON e.adjd_prodid=a.id
			LEFT JOIN (select a.stod_prodid, max(b.stoh_date), a.stod_qtysaldo as qty from stockopname_detail a 
					   inner join stockopname b on b.id=a.stod_hid
					   where b.stoh_status=20 and month(b.stoh_date)='$month_sto' and year(b.stoh_date)='$year_sto' 
					   group by a.stod_prodid) AS f  ON f.stod_prodid=a.id  
			LEFT JOIN (select a.trfd_prodid, sum(a.trfd_qty) as qty from transfer_detail a 
					 inner join transfer b on b.id = a.trfd_hid
					 where b.trf_status=20 and b.trf_category IN (5,6) and trf_spbdate between '$start_date2' and '$end_date2'
					 group by trfd_prodid) AS g ON g.trfd_prodid=a.id
			LEFT JOIN (SELECT a.incd_prodid, sum(a.incd_qty) as qty from incoming_detail a 
					 inner join incoming b on b.id = a.incd_hid
					 where b.inch_status=20 and inch_docdate between '$start_date2' and '$end_date2' 
					 group by incd_prodid) AS h ON h.incd_prodid=a.id
			LEFT JOIN (SELECT kv_prodid, sum(kv_qty) as qty from konversi_external where kv_status=20 and kv_date between '$start_date2' and '$end_date2' 
					   group by kv_prodid) AS i ON i.kv_prodid=a.id
			WHERE a.prod_cat=3 AND a.id='$prdid' ORDER BY a.prod_code ASC",$inv));
	}elseif($prodcat==4){
		$stk =mysql_fetch_array(mysql_query("SELECT (ifnull(b.qty,0) + (ifnull(c.qty,0) + ifnull(h.qty,0)) - (ifnull(d.qty,0) + ifnull(g.qty,0)) + ifnull(e.qty,0)) as end_qty
			FROM product a             
			LEFT JOIN (SELECT stock_prodid, SUM(stock_qty) as qty FROM stock WHERE MONTH(stock_date)='$bulan2' AND YEAR(stock_date)='$tahun2' AND stock_status=10 AND stock_loc=3 GROUP BY stock_prodid) AS b
					  ON b.stock_prodid = a.id
			LEFT JOIN (select a.trfd_prodid, sum(a.trfd_qty) as qty from transfer_detail a 
					 inner join transfer b on b.id = a.trfd_hid
					 where b.trf_status=20 and b.trf_category IN (4) and trf_spbdate between '$start_date2' and '$end_date2' 
					 group by trfd_prodid) AS c ON c.trfd_prodid=a.id                                       
			LEFT JOIN (SELECT a.outd_prodid, sum(a.outd_qty) as qty from outgoing_detail a 
					 inner join outgoing b on b.id = a.outd_hid
					 where b.outh_status=20 and outh_docdate between '$start_date2' and '$end_date2'
					 group by outd_prodid) AS d ON d.outd_prodid=a.id							    
			LEFT JOIN (select a.adjd_prodid, sum(adjd_qty) as qty from adjusment_detail a
					   inner join adjusment b on b.id=a.adjd_hid
					   where b.adjh_status=20 and adjh_date between '$start_date2' and '$end_date2'
					   group by adjd_prodid) AS e ON e.adjd_prodid=a.id
			LEFT JOIN (select a.stod_prodid, max(b.stoh_date), a.stod_qtysaldo as qty from stockopname_detail a 
					   inner join stockopname b on b.id=a.stod_hid
					   where b.stoh_status=20 and month(b.stoh_date)='$month_sto' and year(b.stoh_date)='$year_sto' 
					   group by a.stod_prodid) AS f  ON f.stod_prodid=a.id  
			LEFT JOIN (select a.trfd_prodid, sum(a.trfd_qty) as qty from transfer_detail a 
					 inner join transfer b on b.id = a.trfd_hid
					 where b.trf_status=20 and b.trf_category IN (6) and trf_spbdate between '$start_date2' and '$end_date2'
					 group by trfd_prodid) AS g ON g.trfd_prodid=a.id
			LEFT JOIN (SELECT a.incd_prodid, sum(a.incd_qty) as qty from incoming_detail a 
					 inner join incoming b on b.id = a.incd_hid
					 where b.inch_status=20 and inch_docdate between '$start_date2' and '$end_date2' 
					 group by incd_prodid) AS h ON h.incd_prodid=a.id		 		      
			WHERE a.prod_cat=4 AND a.id='$prdid' ORDER BY a.prod_code ASC",$inv));
	}elseif($prodcat==5){
		$stk =mysql_fetch_array(mysql_query("SELECT (ifnull(b.qty,0) + (ifnull(c.qty,0) + ifnull(h.qty,0) + ifnull(i.qty,0)) - (ifnull(d.qty,0)+ifnull(g.qty,0)) + ifnull(e.qty,0)) as end_qty	
			FROM product a             
			LEFT JOIN (SELECT stock_prodid, SUM(stock_qty) as qty FROM stock WHERE MONTH(stock_date)='$bulan2' AND YEAR(stock_date)='$tahun2' AND stock_status=10 AND stock_loc=3 GROUP BY stock_prodid) AS b
				   ON b.stock_prodid = a.id
				LEFT JOIN (select a.trfd_prodid, sum(a.trfd_qty) as qty from transfer_detail a 
				   inner join transfer b on b.id = a.trfd_hid
				   where b.trf_status=20 and b.trf_category IN (4) and trf_spbdate between '$start_date2' and '$end_date2'
				   group by trfd_prodid) AS c ON c.trfd_prodid=a.id                                       
			LEFT JOIN (SELECT a.outd_prodid, sum(a.outd_qty) as qty from outgoing_detail a 
				   inner join outgoing b on b.id = a.outd_hid
				   where b.outh_status=20 and outh_sjdate between '$start_date2' and '$end_date2'
				   group by outd_prodid) AS d ON d.outd_prodid=a.id							    
			LEFT JOIN (select a.adjd_prodid, sum(adjd_qty) as qty from adjusment_detail a
				   inner join adjusment b on b.id=a.adjd_hid
				   where b.adjh_status=20 and adjh_date between '$start_date2' and '$end_date2'
				   group by adjd_prodid) AS e ON e.adjd_prodid=a.id
			LEFT JOIN (select a.stod_prodid, max(b.stoh_date), a.stod_qtysaldo as qty from stockopname_detail a 
				   inner join stockopname b on b.id=a.stod_hid
				   where b.stoh_status=20 and month(b.stoh_date)='$month_sto' and year(b.stoh_date)='$year_sto' 
				   group by a.stod_prodid) AS f  ON f.stod_prodid=a.id  
			LEFT JOIN (select a.trfd_prodid, sum(a.trfd_qty) as qty from transfer_detail a 
				   inner join transfer b on b.id = a.trfd_hid
				   where b.trf_status=20 and b.trf_category IN (5,6) and trf_spbdate between '$start_date2' and '$end_date2'
				   group by trfd_prodid) AS g ON g.trfd_prodid=a.id
			LEFT JOIN (SELECT a.incd_prodid, sum(a.incd_qty) as qty from incoming_detail a 
				   inner join incoming b on b.id = a.incd_hid
				   where b.inch_status=20 and inch_bpbdate between '$start_date2' and '$end_date2'
				   group by incd_prodid) AS h ON h.incd_prodid=a.id
			LEFT JOIN (SELECT kv_prodid, sum(kv_qty) as qty from konversi_external where kv_status=20 and kv_date between '$start_date2' and '$end_date2' 
					   group by kv_prodid) AS i ON i.kv_prodid=a.id
			WHERE a.prod_cat=5  AND a.id='$prdid' ORDER BY a.prod_code ASC",$inv));
	}elseif($prodcat==6){
		$stk =mysql_fetch_array(mysql_query("SELECT (ifnull(b.qty,0) + (ifnull(c.qty,0) + ifnull(g.qty,0)) - (ifnull(d.qty,0) + ifnull(h.qty,0)) + ifnull(e.qty,0)) as end_qty
			FROM product a             
			LEFT JOIN (SELECT stock_prodid, sum(stock_qty) as qty FROM stock WHERE MONTH(stock_date)='$bulan2' AND YEAR(stock_date)='$tahun2' AND stock_status=10 AND stock_loc=1 
					  GROUP BY stock_prodid) AS b ON b.stock_prodid = a.id
			LEFT JOIN (SELECT a.incd_prodid, sum(a.incd_qty) as qty from incoming_detail a 
					 inner join incoming b on b.id = a.incd_hid
					 where b.inch_status=20 and inch_docdate between '$start_date2' and '$end_date2' 
					 group by incd_prodid) AS c ON c.incd_prodid=a.id
			LEFT JOIN (select a.trfd_prodid, sum(a.trfd_qty) as qty from transfer_detailexbc a 
					 inner join transfer b on b.id = a.trfd_hid
					 where b.trf_status=20 and b.trf_category IN (1,2) and trf_spbdate between '$start_date2' and '$end_date2'
					 group by trfd_prodid) AS d ON d.trfd_prodid=a.id     
			LEFT JOIN (select a.adjd_prodid, sum(adjd_qty) as qty from adjusment_detail a
					   inner join adjusment b on b.id=a.adjd_hid
					   where b.adjh_status=20 and adjh_date between '$start_date2' and '$end_date2'
					   group by adjd_prodid) AS e ON e.adjd_prodid=a.id
			LEFT JOIN (select a.stod_prodid, max(b.stoh_date), a.stod_qtysaldo as qty from stockopname_detail a 
					   inner join stockopname b on b.id=a.stod_hid
					   where b.stoh_status=20 and month(b.stoh_date)='$month_sto' and year(b.stoh_date)='$year_sto'
					   group by a.stod_prodid) AS f  ON f.stod_prodid=a.id  
			LEFT JOIN (select a.trfd_prodid, sum(a.trfd_qty) as qty from transfer_detailexbc a 
					 inner join transfer b on b.id = a.trfd_hid
					 where b.trf_status=20 and b.trf_category IN (3,5) and trf_spbdate between '$start_date2' and '$end_date2'
					 group by trfd_prodid) AS g ON g.trfd_prodid=a.id	
			LEFT JOIN (SELECT a.outd_prodid, sum(a.outd_qty) as qty from outgoing_detail a 
			   inner join outgoing b on b.id = a.outd_hid
			   where b.outh_status=20 and outh_docdate between '$start_date2' and '$end_date2'
			   group by outd_prodid) AS h ON h.outd_prodid=a.id		 	      
			WHERE a.prod_cat IN(6) AND a.id='$prdid'  ORDER BY a.prod_code ASC",$inv));
	}elseif($prodcat==7){
		$stk =mysql_fetch_array(mysql_query("SELECT (ifnull(b.qty,0) + (ifnull(c.qty,0)+ifnull(h.qty,0)+ifnull(i.qty,0)) - (ifnull(d.qty,0)+ifnull(g.qty,0)) + ifnull(e.qty,0)) as end_qty
			FROM product a             
			LEFT JOIN (SELECT stock_prodid, SUM(stock_qty) as qty FROM stock WHERE MONTH(stock_date)='$bulan2' AND YEAR(stock_date)='$tahun2' AND stock_status=10 AND stock_loc IN(1,4) GROUP BY stock_prodid) AS b
					  ON b.stock_prodid = a.id
			LEFT JOIN (SELECT a.incd_prodid, sum(a.incd_qty) as qty from incoming_detail a 
					 inner join incoming b on b.id = a.incd_hid
					 where b.inch_status=20 and inch_docdate between '$start_date2' and '$end_date2' 
					 group by incd_prodid) AS c ON c.incd_prodid=a.id
			LEFT JOIN (SELECT a.outd_prodid, sum(a.outd_qty) as qty from outgoing_detail a 
					 inner join outgoing b on b.id = a.outd_hid
					 where b.outh_status=20 and outh_docdate between '$start_date2' and '$end_date2'
					 group by outd_prodid) AS d ON d.outd_prodid=a.id     
			LEFT JOIN (select a.adjd_prodid, sum(adjd_qty) as qty from adjusment_detail a
					   inner join adjusment b on b.id=a.adjd_hid
					   where b.adjh_status=20 and adjh_date between '$start_date2' and '$end_date2'
					   group by adjd_prodid) AS e ON e.adjd_prodid=a.id
			LEFT JOIN (select a.stod_prodid, max(b.stoh_date), a.stod_qtysaldo as qty from stockopname_detail a 
					   inner join stockopname b on b.id=a.stod_hid
					   where b.stoh_status=20 and month(b.stoh_date)='$month_sto' and year(b.stoh_date)='$year_sto'
					   group by a.stod_prodid) AS f  ON f.stod_prodid=a.id
			LEFT JOIN (select a.trfd_prodid, sum(a.trfd_qty) as qty from transfer_detailexbc a 
				   inner join transfer b on b.id = a.trfd_hid
				   where b.trf_status=20 and b.trf_category IN (1,2) and trf_spbdate between '$start_date2' and '$end_date2'
				   group by trfd_prodid) AS g ON g.trfd_prodid=a.id
			LEFT JOIN (select a.trfd_prodid, sum(a.trfd_qty) as qty from transfer_detailexbc a 
				   inner join transfer b on b.id = a.trfd_hid
				   where b.trf_status=20 and b.trf_category IN (3,5) and trf_spbdate between '$start_date2' and '$end_date2'
				   group by trfd_prodid) AS h ON h.trfd_prodid=a.id 
			LEFT JOIN (select a.trfd_prodid, sum(a.trfd_qty) as qty from transfer_detail a 
				   inner join transfer b on b.id = a.trfd_hid
				   where b.trf_status=20 and b.trf_category IN (4) and trf_spbdate between '$start_date2' and '$end_date2'
				   group by trfd_prodid) AS i ON i.trfd_prodid=a.id 	   
			WHERE a.prod_cat IN(7)  AND a.id='$prdid' ORDER BY a.prod_code ASC",$inv));
	}elseif($prodcat==8){
		$stk =mysql_fetch_array(mysql_query("SELECT (ifnull(b.qty,0) + (ifnull(c.qty,0)+ifnull(h.qty,0)+ifnull(i.qty,0)) - (ifnull(d.qty,0)+ifnull(g.qty,0)) + ifnull(e.qty,0)) as end_qty
			FROM product a             
			LEFT JOIN (SELECT stock_prodid, SUM(stock_qty) as qty FROM stock WHERE MONTH(stock_date)='$bulan2' AND YEAR(stock_date)='$tahun2' AND stock_status=10 AND stock_loc IN(1,4) GROUP BY stock_prodid) AS b
					  ON b.stock_prodid = a.id
			LEFT JOIN (SELECT a.incd_prodid, sum(a.incd_qty) as qty from incoming_detail a 
					 inner join incoming b on b.id = a.incd_hid
					 where b.inch_status=20 and inch_docdate between '$start_date2' and '$end_date2' 
					 group by incd_prodid) AS c ON c.incd_prodid=a.id
			LEFT JOIN (SELECT a.outd_prodid, sum(a.outd_qty) as qty from outgoing_detail a 
					 inner join outgoing b on b.id = a.outd_hid
					 where b.outh_status=20 and outh_docdate between '$start_date2' and '$end_date2'
					 group by outd_prodid) AS d ON d.outd_prodid=a.id     
			LEFT JOIN (select a.adjd_prodid, sum(adjd_qty) as qty from adjusment_detail a
					   inner join adjusment b on b.id=a.adjd_hid
					   where b.adjh_status=20 and adjh_date between '$start_date2' and '$end_date2'
					   group by adjd_prodid) AS e ON e.adjd_prodid=a.id
			LEFT JOIN (select a.stod_prodid, max(b.stoh_date), a.stod_qtysaldo as qty from stockopname_detail a 
					   inner join stockopname b on b.id=a.stod_hid
					   where b.stoh_status=20 and month(b.stoh_date)='$month_sto' and year(b.stoh_date)='$year_sto'
					   group by a.stod_prodid) AS f  ON f.stod_prodid=a.id
			LEFT JOIN (select a.trfd_prodid, sum(a.trfd_qty) as qty from transfer_detailexbc a 
				   inner join transfer b on b.id = a.trfd_hid
				   where b.trf_status=20 and b.trf_category IN (1,2) and trf_spbdate between '$start_date2' and '$end_date2'
				   group by trfd_prodid) AS g ON g.trfd_prodid=a.id
			LEFT JOIN (select a.trfd_prodid, sum(a.trfd_qty) as qty from transfer_detailexbc a 
				   inner join transfer b on b.id = a.trfd_hid
				   where b.trf_status=20 and b.trf_category IN (3,5) and trf_spbdate between '$start_date2' and '$end_date2'
				   group by trfd_prodid) AS h ON h.trfd_prodid=a.id   
			LEFT JOIN (select a.trfd_prodid, sum(a.trfd_qty) as qty from transfer_detail a 
				   inner join transfer b on b.id = a.trfd_hid
				   where b.trf_status=20 and b.trf_category IN (4) and trf_spbdate between '$start_date2' and '$end_date2'
				   group by trfd_prodid) AS i ON i.trfd_prodid=a.id
			WHERE a.prod_cat IN(8)  AND a.id='$prdid' ORDER BY a.prod_code ASC",$inv));
	}
	if($prodcat==1 || $prodcat==2 || $prodcat==3 || $prodcat==4 || $prodcat==5 || $prodcat==6 || $prodcat==7 || $prodcat==8){
		return $stk['end_qty'];
	}else{
		return 0;
	}
}
function get_bulan($bln){
	if($bln==1){
		$bulan = "Januari";
	}elseif($bln==2){
		$bulan = "Februari";
	}elseif($bln==3){
		$bulan = "Maret";
	}elseif($bln==4){
		$bulan = "April";
	}elseif($bln==5){
		$bulan = "Mei";
	}elseif($bln==6){
		$bulan = "Juni";
	}elseif($bln==7){
		$bulan = "Juli";
	}elseif($bln==8){
		$bulan = "Agustus";
	}elseif($bln==9){
		$bulan = "September";
	}elseif($bln==10){
		$bulan = "Oktober";
	}elseif($bln==11){
		$bulan = "November";
	}elseif($bln==12){
		$bulan = "Desember";
	}
	return $bulan;
}
function get_validationdate($date){
	$date_trc = (int)("".substr($date,0,4)."".substr($date,5,2)."".substr($date,8,2)."");
	$date_str = (int)("".substr(get_startdate(),0,4)."".substr(get_startdate(),5,2)."".substr(get_startdate(),8,2)."");
	$date_end = (int)("".substr(get_enddate(),0,4)."".substr(get_enddate(),5,2)."".substr(get_enddate(),8,2)."");
	if(($date_trc < $date_str) || ($date_trc > $date_end) ){
		$val = "NO";
	}else{
		$val = "OK";
	}	
	return($val);
}
function get_validationdate2($date,$inv){
	$date_trc = (int)("".substr($date,0,4)."".substr($date,5,2)."".substr($date,8,2)."");
	$date_str = (int)("".substr(get_startdate2($inv),0,4)."".substr(get_startdate2($inv),5,2)."".substr(get_startdate2($inv),8,2)."");
	$date_end = (int)("".substr(get_enddate2($inv),0,4)."".substr(get_enddate2($inv),5,2)."".substr(get_enddate2($inv),8,2)."");
	if(($date_trc < $date_str) || ($date_trc > $date_end) ){
		$val = "NO";
	}else{
		$val = "OK";
	}	
	return($val);
}
function get_valincoming($id_header,$inv){
	$html ="";
	$qr=mysql_query("SELECT * FROM log_incoming WHERE log_status=1 AND id_header='$id_header'",$inv);
    while($d=mysql_fetch_array($qr)){
		$kode = str_replace(' ', '',strtoupper($d['kode']));
		$vc = mysql_fetch_array(mysql_query("SELECT prod_code FROM product WHERE prod_code='$d[kode]' AND prod_cat <> '100'",$inv));
		$val_date = get_validationdate2($d['tgl_dokpab'],$inv);
		if(empty($vc['prod_code'])){$s_kode ="NO";}
		else{$s_kode ="READY";}
		if(($s_kode=="NO") || ($val_date=="NO") ){
			$html = $html."".$d['no_aju']."XX";
		}
	}
	$htm = substr($html, 0,-2);
	if($htm==""){$status = "OK";}
	else{$status = "NO";}	
	return $status;			
}
function get_valincoming_upl($no_dokpab,$jenis_dokpab,$inv){
	$html ="";
	$qr=mysql_query("SELECT * FROM log_incoming WHERE log_status=1000 AND no_dokpab='$no_dokpab' AND jenis_dokpab='$jenis_dokpab' ",$inv);
    while($d=mysql_fetch_array($qr)){
		#$kode = str_replace(' ', '',strtoupper($d['kode']));
		$vc = mysql_fetch_array(mysql_query("SELECT prod_code FROM product WHERE prod_code='$d[kode]' AND prod_cat <> '100'",$inv));
		$val_jns = mysql_fetch_array(mysql_query("SELECT doc_type FROM document_type WHERE doc_type='$d[jenis_dokpab]' AND doc_status=1"));
		if(!empty($val_jns['doc_type'])){
			$jns ="READY";
		}else{
			$jns ="NO";
		}
		if($d['jumlah'] <=0){
			$bgcolorjml ="#FF6A6A";
		}else{
			$bgcolorjml ="#000000";
		}
		$val_date = get_validationdate2($d['tgl_dokpab'],$inv);
		if(empty($vc['prod_code'])){$s_kode ="NO";}
		else{$s_kode ="READY";}
		if(($s_kode=="NO") || ($val_date=="NO") || ($jns =="NO") || ($bgcolorjml =="#FF6A6A") ){
			$html = $html."".$d['no_dokpab']."XX";
		}
	}
	$htm = substr($html, 0,-2);
	if($htm==""){$status = "OK";}
	else{$status = "NO";}	
	return $status;			
}
function get_valoutgoing($start_date2,$end_date2,$bulan2,$tahun2,$id_header,$inv){
	$html ="";
	$qr=mysql_query("SELECT * FROM log_outgoing WHERE log_status=1 AND id_header='$id_header'",$inv);
	while($d=mysql_fetch_array($qr)){
    	$kode = str_replace(' ', '',strtoupper($d['kode']));
		$vc = mysql_fetch_array(mysql_query("SELECT id,prod_code,prod_cat FROM product WHERE prod_code='$d[kode]' AND prod_cat<>'100'",$inv));
		$stock = get_stock($start_date2,$end_date2,$bulan2,$tahun2,$vc['id'],$vc['prod_cat'],$inv);		
		$val_date = get_validationdate2($d['tgl_dokpab'],$inv);		
		if(empty($vc['prod_code'])){$s_kode ="NO";}
		else{$s_kode ="READY";}		
		if($d['jumlah'] > $stock){$s_stock ="NO";}
		else{$s_stock ="READY";}
		if($s_kode=="NO" || $s_stock =="NO" || $val_date=="NO"){
			$html = $html."".$d['no_aju']."XX";
		}
	}
	$htm = substr($html, 0,-2);
	if($htm==""){$status = "OK";}
	else{$status = "NO";}	
	return $status;			
}
function get_valoutgoing_upl($start_date2,$end_date2,$bulan2,$tahun2,$no_dokpab,$jenis_dokpab,$inv){
	$html ="";
	$qr=mysql_query("SELECT * FROM log_outgoing WHERE log_status=1000 AND no_dokpab='$no_dokpab' AND jenis_dokpab='$jenis_dokpab'",$inv);
	while($d=mysql_fetch_array($qr)){
    	$kode = str_replace(' ', '',strtoupper($d['kode']));
		$vc = mysql_fetch_array(mysql_query("SELECT id,prod_code,prod_cat FROM product WHERE prod_code='$d[kode]' AND prod_cat<>'100'",$inv));
		$stock = get_stock($start_date2,$end_date2,$bulan2,$tahun2,$vc['id'],$vc['prod_cat'],$inv);		
		$val_date = get_validationdate2($d['tgl_dokpab'],$inv);
		$val_jns = mysql_fetch_array(mysql_query("SELECT doc_type FROM document_type WHERE doc_type='$d[jenis_dokpab]' AND doc_status=2"));
		if(!empty($val_jns['doc_type'])){
			$jns ="READY";
		}else{
			$jns ="NO";
		}
		if($d['jumlah'] <=0){
			$bgcolorjml ="#FF6A6A";
		}else{
			$bgcolorjml ="#000000";
		}
		if(empty($vc['prod_code'])){$s_kode ="NO";}
		else{$s_kode ="READY";}		
		if($d['jumlah'] > $stock){$s_stock ="NO";}
		else{$s_stock ="READY";}
		if($s_kode=="NO" || $s_stock =="NO" || $val_date=="NO" || $jns =="NO" || ($bgcolorjml =="#FF6A6A") ){
			$html = $html."".$d['no_dokpab']."XX";
		}
	}
	$htm = substr($html, 0,-2);
	if($htm==""){$status = "OK";}
	else{$status = "NO";}	
	return $status;			
}
function valstockopname($month_sto,$year_sto,$cat){
	if($cat==1 || $cat==6){
		$ctgy = "1,6";
	}elseif($cat==2){
		$ctgy = "2";
	}elseif($cat==3){
		$ctgy = "3";
	}elseif($cat==4){
		$ctgy = "4";
	}	
	$qd =mysql_fetch_array(mysql_query("select count(a.stod_prodid) cnt from stockopname_detail a 
		 inner join stockopname b on b.id=a.stod_hid
         inner join product c ON c.id=a.stod_prodid
		 where b.stoh_status=20 and month(b.stoh_date)='$month_sto' and year(b.stoh_date)='$year_sto' and c.prod_cat in($ctgy)"));
	return $qd['cnt'];	 
}

#Akuntansi
function get_pricebb($prodid,$start_date2,$end_date2,$inv){
	$tprice=0;
	$q = mysql_query("SELECT stock_prodid prodid, (stock_price/stock_qty) price FROM stock WHERE stock_prodid='$prodid' AND stock_status=10 AND stock_loc=1
		AND stock_date='$start_date2'
		UNION ALL       
		SELECT a.incd_prodid prodid, (a.incd_price/a.incd_qty) as price FROM incoming_detail a 
		INNER JOIN incoming b ON b.id = a.incd_hid
		WHERE b.inch_status=20 AND inch_bpbdate BETWEEN '$start_date2' AND '$end_date2' AND a.incd_prodid='$prodid'");
	$nums = mysql_num_rows($q);
	while($d=mysql_fetch_array($q)){
		$tprice = $tprice+$d['price']; 	
	}
	if($tprice==0){
		return 0;
	}else{
		return $tprice/$nums;	
	}	
}
function get_pricewip($prodid,$start_date2,$end_date2,$inv){
	$tprice=0;
	$q = mysql_query("SELECT stock_prodid prodid, (stock_price/stock_qty) price FROM stock WHERE stock_prodid='$prodid' AND stock_status=10 AND stock_loc=2
		AND stock_date='$start_date2'
		UNION ALL       
		SELECT a.trfd_prodid prodid, a.trfd_price_satuan as price FROM transfer_detailexbc a 
		INNER JOIN transfer b ON b.id = a.trfd_hid
		WHERE b.trf_status=20 AND trf_spbdate BETWEEN '$start_date2' AND '$end_date2' AND a.trfd_prodid='$prodid' AND b.trf_category=1");
	$nums = mysql_num_rows($q);
	while($d=mysql_fetch_array($q)){
		$tprice = $tprice+$d['price']; 	
	}
	if($tprice==0){
		return 0;
	}else{
		return $tprice/$nums;	
	}	
}
function get_tpricebb_in($prodid,$start_date2,$end_date2,$bulan2,$tahun2,$inv){
	#Qty Beg
	$qtb = mysql_fetch_array(mysql_query("SELECT stock_qty FROM stock WHERE stock_prodid='$prodid' AND stock_status=10 
		   AND MONTH(stock_date)='$bulan2' AND YEAR(stock_date)='$tahun2'",$inv));
	#Qty IN
	$qti = mysql_fetch_array(mysql_query("SELECT a.incd_prodid, sum(a.incd_qty) as qty FROM incoming_detail a 
	   INNER JOIN incoming b ON b.id = a.incd_hid
	   WHERE b.inch_status=20 AND inch_bpbdate BETWEEN '$start_date2' AND '$end_date2' AND a.incd_prodid='$prodid'
	   GROUP BY a.incd_prodid"));
	$tprice   = get_pricebb($prodid,$start_date2,$end_date2,$bulan2,$tahun2,$inv);
	$tqty     = $qtb['stock_qty']+$qti['qty'];
	if(!empty($tprice) && !empty($tqty)){
		return ($tqty*$tprice);
	}else{
		return 0;
	}
}
function get_tpricebb_out($prodid,$start_date2,$end_date2,$bulan2,$tahun2,$inv){
	#Qty OUT
	$qto1 = mysql_fetch_array(mysql_query("select sum(b.qty) qty from (select sum(a.trfd_qty) as qty from transfer_detailexbc a 
	   inner join transfer b on b.id = a.trfd_hid
	   where b.trf_status=20 and b.trf_category IN (1,2) and trf_spbdate between '$start_date2' AND '$end_date2' and a.trfd_prodid='$prodid'
	   union all
	   select sum(a.outd_qty) as qty from outgoing_detail a 
	   inner join outgoing b on b.id = a.outd_hid
	   where b.outh_status=20 and outh_sjdate between '$start_date2' AND '$end_date2' and a.outd_prodid='$prodid') b"));
	$qto2 = mysql_fetch_array(mysql_query("select sum(a.trfd_qty) as qty from transfer_detailexbc a 
	   inner join transfer b on b.id = a.trfd_hid
	   where b.trf_status=20 and b.trf_category IN (3,5) and trf_spbdate between '$start_date2' AND '$end_date2' and a.trfd_prodid='$prodid'"));
	$tprice   = get_pricebb($prodid,$start_date2,$end_date2,$bulan2,$tahun2,$inv);
	$tqtyo    = $qto1['qty']-$qto2['qty'];
	if(!empty($tqtyo) && !empty($tprice)){
		return $tqtyo*$tprice;
	}else{
		return 0;
	}
}
function get_pricebm_in($prodid,$start_date2,$end_date2,$bulan2,$tahun2,$inv){
	#Begining
	$prcbeg = mysql_fetch_array(mysql_query("SELECT (stock_price/stock_qty) price FROM stock WHERE stock_prodid='$prodid' AND stock_status=10 
		   AND MONTH(stock_date)='$bulan2' AND YEAR(stock_date)='$tahun2'",$inv));
	#Price
	$prc = mysql_fetch_array(mysql_query("SELECT b.prodid, SUM(tprice) prodprice FROM
		(SELECT a.incd_prodid prodid, (a.incd_price/a.incd_qty) as tprice FROM incoming_detail a 
		INNER JOIN incoming b ON b.id = a.incd_hid
		WHERE b.inch_status=20 AND inch_bpbdate BETWEEN '$start_date2' AND '$end_date2' AND a.incd_prodid='$prodid') b
		GROUP BY b.prodid",$inv));		
	#Count : jumlah transaksi
	$cnt = mysql_fetch_array(mysql_query("SELECT COUNT(b.prodid) cprodid FROM
		(SELECT a.incd_prodid prodid, (a.incd_price/a.incd_qty) as tprice FROM incoming_detail a 
		INNER JOIN incoming b ON b.id = a.incd_hid
		WHERE b.inch_status=20 AND inch_bpbdate BETWEEN '$start_date2' AND '$end_date2' AND a.incd_prodid='$prodid') b
		GROUP BY b.prodid",$inv));	
	$pricebeg = $prcbeg['price'];   
	$pricein  = $prc['prodprice'];
	$countin  = $cnt['cprodid'];
	if(!empty($pricebeg) && !empty($pricein)){	
		$price = ($pricebeg+$pricein)/($countin+1);
	}elseif(empty($pricebeg) && !empty($pricein)){
		$price = ($pricein)/($countin);
	}elseif(!empty($pricebeg) && empty($pricein)){
		$price = $pricebeg;
	}else{
		$price = 0;
	}
	return $price;
}
function get_tpricebm_in($prodid,$start_date2,$end_date2,$bulan2,$tahun2,$inv){
	#Qty Beg
	$qtb = mysql_fetch_array(mysql_query("SELECT stock_qty FROM stock WHERE stock_prodid='$prodid' AND stock_status=10 
		   AND MONTH(stock_date)='$bulan2' AND YEAR(stock_date)='$tahun2'",$inv));
	#Qty IN
	$qti = mysql_fetch_array(mysql_query("SELECT a.incd_prodid, sum(a.incd_qty) as qty FROM incoming_detail a 
	   INNER JOIN incoming b ON b.id = a.incd_hid
	   WHERE b.inch_status=20 AND inch_bpbdate BETWEEN '$start_date2' AND '$end_date2' AND a.incd_prodid='$prodid'
	   GROUP BY a.incd_prodid"));
	$tprice   = get_pricebm_in($prodid,$start_date2,$end_date2,$bulan2,$tahun2,$inv);
	$tqty     = $qtb['stock_qty']+$qti['qty'];
	if(!empty($tprice) && !empty($tqty)){
		return ($tqty*$tprice);
	}else{
		return 0;
	}
}
function get_pricebm_out($prodid,$start_date2,$end_date2,$bulan2,$tahun2,$inv){
	#Price
	$prc = mysql_fetch_array(mysql_query("SELECT b.prodid, SUM(tprice) prodprice FROM
		(SELECT a.outd_prodid prodid, (a.outd_price/a.outd_qty) as tprice FROM outgoing_detail a 
		INNER JOIN outgoing b ON b.id = a.outd_hid
		WHERE b.outh_status=20 AND outh_sjdate BETWEEN '$start_date2' AND '$end_date2' AND a.outd_prodid='$prodid') b
		GROUP BY b.prodid",$inv));		
	#Count : jumlah transaksi
	$cnt = mysql_fetch_array(mysql_query("SELECT COUNT(b.prodid) cprodid FROM
		(SELECT a.outd_prodid prodid, (a.outd_price/a.outd_qty) as tprice FROM outgoing_detail a 
		INNER JOIN outgoing b ON b.id = a.outd_hid
		WHERE b.outh_status=20 AND outh_sjdate BETWEEN '$start_date2' AND '$end_date2' AND a.outd_prodid='$prodid') b
		GROUP BY b.prodid",$inv));	
	$priceout  = $prc['prodprice'];
	$countout  = $cnt['cprodid'];
	if(!empty($priceout) && !empty($countout)){	
		$price = $priceout/$countout;
	}else{
		$price = 0;
	}
	return $price;
}
function get_tpricebm_out($prodid,$start_date2,$end_date2,$bulan2,$tahun2,$inv){
	#Qty OUT
	$qto1 = mysql_fetch_array(mysql_query("SELECT a.outd_prodid, SUM(a.outd_qty) as tqty FROM outgoing_detail a 
		INNER JOIN outgoing b ON b.id = a.outd_hid
		WHERE b.outh_status=20 AND outh_sjdate BETWEEN '$start_date2' AND '$end_date2' AND a.outd_prodid='$prodid'
		GROUP BY a.outd_prodid"));
	$tprice   = get_pricebm_out($prodid,$start_date2,$end_date2,$bulan2,$tahun2,$inv);
	$tqtyo    = $qto1['tqty'];
	if(!empty($tqtyo) && !empty($tprice)){
		return $tqtyo*$tprice;
	}else{
		return 0;
	}
}
function get_pricebj_in($prodid,$start_date2,$end_date2,$bulan2,$tahun2,$inv){
	#Begining
	$prcbeg = mysql_fetch_array(mysql_query("SELECT (stock_price/stock_qty) price FROM stock WHERE stock_prodid='$prodid' AND stock_status=10 
		   AND MONTH(stock_date)='$bulan2' AND YEAR(stock_date)='$tahun2'",$inv));
	#Price
	$prc = mysql_fetch_array(mysql_query("SELECT b.prodid, SUM(tprice) prodprice FROM
		(SELECT a.trfd_prodid prodid, (a.trfd_price/a.trfd_qty) as tprice FROM transfer_detail a 
		INNER JOIN transfer b ON b.id = a.trfd_hid
		WHERE b.trf_status=20 AND trf_spbdate BETWEEN '$start_date2' AND '$end_date2' AND a.trfd_prodid='$prodid') b
		GROUP BY b.prodid",$inv));		
	#Count : jumlah transaksi
	$cnt = mysql_fetch_array(mysql_query("SELECT COUNT(b.prodid) cprodid FROM
		(SELECT a.trfd_prodid prodid, (a.trfd_price/a.trfd_qty) as tprice FROM transfer_detail a 
		INNER JOIN transfer b ON b.id = a.trfd_hid
		WHERE b.trf_status=20 AND trf_spbdate BETWEEN '$start_date2' AND '$end_date2' AND a.trfd_prodid='$prodid') b
		GROUP BY b.prodid",$inv));	
	$pricebeg = $prcbeg['price'];   
	$pricein  = $prc['prodprice'];
	$countin  = $cnt['cprodid'];
	if(!empty($pricebeg) && !empty($pricein)){	
		$price = ($pricebeg+$pricein)/($countin+1);
	}elseif(empty($pricebeg) && !empty($pricein)){
		$price = ($pricein)/($countin);
	}elseif(!empty($pricebeg) && empty($pricein)){
		$price = $pricebeg;
	}else{
		$price = 0;
	}
	return $price;
}
function get_tpricebj_in($prodid,$start_date2,$end_date2,$bulan2,$tahun2,$inv){
	#Qty Beg
	$qtb = mysql_fetch_array(mysql_query("SELECT stock_qty FROM stock WHERE stock_prodid='$prodid' AND stock_status=10 
		   AND MONTH(stock_date)='$bulan2' AND YEAR(stock_date)='$tahun2'",$inv));
	#Qty IN
	$qti = mysql_fetch_array(mysql_query("SELECT a.trfd_prodid, sum(a.trfd_qty) as qty FROM transfer_detail a 
	   INNER JOIN transfer b ON b.id = a.trfd_hid
	   WHERE b.trf_status=20 AND trf_spbdate BETWEEN '$start_date2' AND '$end_date2' AND a.trfd_prodid='$prodid'
	   GROUP BY a.trfd_prodid"));
	$tprice   = get_pricebj_in($prodid,$start_date2,$end_date2,$bulan2,$tahun2,$inv);
	$tqty     = $qtb['stock_qty']+$qti['qty'];
	if(!empty($tprice) && !empty($tqty)){
		return ($tqty*$tprice);
	}else{
		return 0;
	}
}
function get_pricebj_out($prodid,$start_date2,$end_date2,$bulan2,$tahun2,$inv){
	#Price
	$prc = mysql_fetch_array(mysql_query("SELECT b.prodid, SUM(tprice) prodprice FROM
		(SELECT a.outd_prodid prodid, (a.outd_price/a.outd_qty) as tprice FROM outgoing_detail a 
		INNER JOIN outgoing b ON b.id = a.outd_hid
		WHERE b.outh_status=20 AND outh_sjdate BETWEEN '$start_date2' AND '$end_date2' AND a.outd_prodid='$prodid') b
		GROUP BY b.prodid",$inv));		
	#Count : jumlah transaksi
	$cnt = mysql_fetch_array(mysql_query("SELECT COUNT(b.prodid) cprodid FROM
		(SELECT a.outd_prodid prodid, (a.outd_price/a.outd_qty) as tprice FROM outgoing_detail a 
		INNER JOIN outgoing b ON b.id = a.outd_hid
		WHERE b.outh_status=20 AND outh_sjdate BETWEEN '$start_date2' AND '$end_date2' AND a.outd_prodid='$prodid') b
		GROUP BY b.prodid",$inv));	
	$priceout  = $prc['prodprice'];
	$countout  = $cnt['cprodid'];
	if(!empty($priceout) && !empty($countout)){	
		$price = $priceout/$countout;
	}else{
		$price = 0;
	}
	return $price;
}
function get_tpricebj_out($prodid,$start_date2,$end_date2,$bulan2,$tahun2,$inv){
	#Qty OUT
	$qto1 = mysql_fetch_array(mysql_query("SELECT a.outd_prodid, SUM(a.outd_qty) as tqty FROM outgoing_detail a 
		INNER JOIN outgoing b ON b.id = a.outd_hid
		WHERE b.outh_status=20 AND outh_sjdate BETWEEN '$start_date2' AND '$end_date2' AND a.outd_prodid='$prodid'
		GROUP BY a.outd_prodid"));
	$tprice   = get_pricebj_out($prodid,$start_date2,$end_date2,$bulan2,$tahun2,$inv);
	$tqtyo    = $qto1['tqty'];
	if(!empty($tqtyo) && !empty($tprice)){
		return $tqtyo*$tprice;
	}else{
		return 0;
	}
}
function get_tpricebo_out($prodid,$start_date2,$end_date2,$bulan2,$tahun2,$inv){
	#Qty OUT
	$qto1 = mysql_fetch_array(mysql_query("select sum(b.qty) qty from (select sum(a.trfd_qty) as qty from transfer_detail a 
	   inner join transfer b on b.id = a.trfd_hid
	   where b.trf_status=20 and b.trf_category IN (10) and trf_spbdate between '$start_date2' AND '$end_date2' and a.trfd_prodid='$prodid'
	   union all
	   select sum(a.outd_qty) as qty from outgoing_detail a 
	   inner join outgoing b on b.id = a.outd_hid
	   where b.outh_status=20 and outh_sjdate between '$start_date2' AND '$end_date2' and a.outd_prodid='$prodid') b"));	
	$tprice   = get_pricebb($prodid,$start_date2,$end_date2,$bulan2,$tahun2,$inv);
	$tqtyo    = $qto1['qty'];
	if(!empty($tqtyo) && !empty($tprice)){
		return $tqtyo*$tprice;
	}else{
		return 0;
	}
}
#Start HPP
function get_hpp_bahan($cat,$bulan,$tahun){
	$q = mysql_fetch_array(mysql_query("SELECT SUM(b.incd_price) price FROM incoming a
			INNER JOIN incoming_detail b ON b.incd_hid=a.id INNER JOIN product c ON c.id=b.incd_prodid
			WHERE a.inch_status=20 AND c.prod_cat='$cat' AND MONTH(a.inch_docdate)='$bulan' AND YEAR(a.inch_bpbdate)='$tahun' AND a.inch_paytype='Hutang'"));
	return 0;		
}
function get_hpp_outbahan($bulan,$tahun){
	$q = mysql_fetch_array(mysql_query("SELECT SUM(b.trfd_price) price FROM transfer a
			INNER JOIN transfer_detailexbc b ON b.trfd_hid=a.id
			INNER JOIN product c ON c.id=b.trfd_prodid
			WHERE a.trf_status=20 AND c.prod_cat IN(1,6) AND a.trf_category=1 AND b.trfd_status=10 AND MONTH(a.trf_spbdate)='$bulan' AND YEAR(a.trf_spbdate)='$tahun'"));
	return 0;		
}
function get_hpp_biaya($tipe,$bulan,$tahun){
	$q = mysql_fetch_array(mysql_query("SELECT SUM(bk_amount) jumlah FROM buku_besar WHERE MONTH(bk_tanggal)='$bulan' AND YEAR(bk_tanggal)='$tahun' 
		      AND bk_status=30 AND bk_idmbp='$tipe' GROUP BY bk_idmbp"));
	return $q['jumlah'];		  
}
#Start LR
function get_lrln_penjualan($bulan,$tahun){
	$q=mysql_fetch_array(mysql_query("SELECT SUM(b.outd_price) price FROM outgoing a
			INNER JOIN outgoing_detail b ON b.outd_hid=a.id INNER JOIN product c ON c.id=b.outd_prodid
			WHERE a.outh_status=20 AND c.prod_cat=3 AND MONTH(a.outh_docdate)='$bulan' AND YEAR(a.outh_docdate)='$tahun'"));
	return $q['price'];
}
function get_lr_hpp($bulan,$tahun){
	#Bahan baku
	$pbbb_jan = get_hpp_bahan(1,1,$tahun);
	$pbbb_feb = get_hpp_bahan(1,2,$tahun);
	$pbbb_mar = get_hpp_bahan(1,3,$tahun);
	$pbbb_apr = get_hpp_bahan(1,4,$tahun);
	$pbbb_mei = get_hpp_bahan(1,5,$tahun);
	$pbbb_jun = get_hpp_bahan(1,6,$tahun);
	$pbbb_jul = get_hpp_bahan(1,7,$tahun);
	$pbbb_agu = get_hpp_bahan(1,8,$tahun);
	$pbbb_sep = get_hpp_bahan(1,9,$tahun);
	$pbbb_okt = get_hpp_bahan(1,10,$tahun);
	$pbbb_nov = get_hpp_bahan(1,11,$tahun);
	$pbbb_des = get_hpp_bahan(1,12,$tahun);
	$pbbb_tot = $pbbb_jan+$pbbb_feb+$pbbb_mar+$pbbb_apr+$pbbb_mei+$pbbb_jun+$pbbb_jul+$pbbb_agu+$pbbb_sep+$pbbb_okt+$pbbb_nov+$pbbb_des;
	#Bahan penolong
	$pbbp_jan = get_hpp_bahan(6,1,$tahun);
	$pbbp_feb = get_hpp_bahan(6,2,$tahun);
	$pbbp_mar = get_hpp_bahan(6,3,$tahun);
	$pbbp_apr = get_hpp_bahan(6,4,$tahun);
	$pbbp_mei = get_hpp_bahan(6,5,$tahun);
	$pbbp_jun = get_hpp_bahan(6,6,$tahun);
	$pbbp_jul = get_hpp_bahan(6,7,$tahun);
	$pbbp_agu = get_hpp_bahan(6,8,$tahun);
	$pbbp_sep = get_hpp_bahan(6,9,$tahun);
	$pbbp_okt = get_hpp_bahan(6,10,$tahun);
	$pbbp_nov = get_hpp_bahan(6,11,$tahun);
	$pbbp_des = get_hpp_bahan(6,12,$tahun);
	$pbbp_tot = $pbbp_jan+$pbbp_feb+$pbbp_mar+$pbbp_apr+$pbbp_mei+$pbbp_jun+$pbbp_jul+$pbbp_agu+$pbbp_sep+$pbbp_okt+$pbbp_nov+$pbbp_des;
	#Total bahan baku & penolong
	$tbb_jan = $pbbb_jan+$pbbp_jan;
	$tbb_feb = $pbbb_feb+$pbbp_feb;
	$tbb_mar = $pbbb_mar+$pbbp_mar;
	$tbb_apr = $pbbb_apr+$pbbp_apr;
	$tbb_mei = $pbbb_mei+$pbbp_mei;
	$tbb_jun = $pbbb_jun+$pbbp_jun;
	$tbb_jul = $pbbb_jul+$pbbp_jul;
	$tbb_agu = $pbbb_agu+$pbbp_agu;
	$tbb_sep = $pbbb_sep+$pbbp_sep;
	$tbb_okt = $pbbb_okt+$pbbp_okt;
	$tbb_nov = $pbbb_nov+$pbbp_nov;
	$tbb_des = $pbbb_des+$pbbp_des;
	$tbb_tot = $tbb_jan+$tbb_feb+$tbb_mar+$tbb_apr+$tbb_mei+$tbb_jun+$tbb_jul+$tbb_agu+$tbb_sep+$tbb_okt+$tbb_nov+$tbb_des;
	#Persediaan/Sisa
	$pmbhn1_jan = get_hpp_outbahan(1,$tahun);
	$pmbhn1_feb = get_hpp_outbahan(2,$tahun);
	$pmbhn1_mar = get_hpp_outbahan(3,$tahun);
	$pmbhn1_apr = get_hpp_outbahan(4,$tahun);
	$pmbhn1_mei = get_hpp_outbahan(5,$tahun);
	$pmbhn1_jun = get_hpp_outbahan(6,$tahun);
	$pmbhn1_jul = get_hpp_outbahan(7,$tahun);
	$pmbhn1_agu = get_hpp_outbahan(8,$tahun);
	$pmbhn1_sep = get_hpp_outbahan(9,$tahun);
	$pmbhn1_okt = get_hpp_outbahan(10,$tahun);
	$pmbhn1_nov = get_hpp_outbahan(11,$tahun);
	$pmbhn1_des = get_hpp_outbahan(12,$tahun);
	$pmbhn1_tot = $pmbhn1_jan+$pmbhn1_feb+$pmbhn1_mar+$pmbhn1_apr+$pmbhn1_mei+$pmbhn1_jun+$pmbhn1_jul+$pmbhn1_agu+$pmbhn1_sep+$pmbhn1_okt+$pmbhn1_nov+$pmbhn1_des;
	/*$rbh_jan = $pmbhn1_jan-$tbb_jan;
	$rbh_feb = $pmbhn1_feb-$tbb_feb;
	$rbh_mar = $pmbhn1_mar-$tbb_mar;
	$rbh_apr = $pmbhn1_apr-$tbb_apr;
	$rbh_mei = $pmbhn1_mei-$tbb_mei;
	$rbh_jun = $pmbhn1_jun-$tbb_jun;
	$rbh_jul = $pmbhn1_jul-$tbb_jul;
	$rbh_agu = $pmbhn1_agu-$tbb_agu;
	$rbh_sep = $pmbhn1_sep-$tbb_sep;
	$rbh_okt = $pmbhn1_okt-$tbb_okt;
	$rbh_nov = $pmbhn1_nov-$tbb_nov;
	$rbh_des = $pmbhn1_des-$tbb_des;*/
	$rbh_jan = $tbb_jan-$pmbhn1_jan;
	$rbh_feb = $tbb_feb-$pmbhn1_feb;
	$rbh_mar = $tbb_mar-$pmbhn1_mar;
	$rbh_apr = $tbb_apr-$pmbhn1_apr;
	$rbh_mei = $tbb_mei-$pmbhn1_mei;
	$rbh_jun = $tbb_jun-$pmbhn1_jun;
	$rbh_jul = $tbb_jul-$pmbhn1_jul;
	$rbh_agu = $tbb_agu-$pmbhn1_agu;
	$rbh_sep = $tbb_sep-$pmbhn1_sep;
	$rbh_okt = $tbb_okt-$pmbhn1_okt;
	$rbh_nov = $tbb_nov-$pmbhn1_nov;
	$rbh_des = $tbb_des-$pmbhn1_des;
	$rbh_tot = $rbh_jan+$rbh_feb+$rbh_mar+$rbh_apr+$rbh_mei+$rbh_jun+$rbh_jul+$rbh_agu+$rbh_sep+$rbh_okt+$rbh_nov+$rbh_des;
	#Pemakaian bahan
	$pmbhn_jan = get_hpp_outbahan(1,$tahun);
	$pmbhn_feb = get_hpp_outbahan(2,$tahun);
	$pmbhn_mar = get_hpp_outbahan(3,$tahun);
	$pmbhn_apr = get_hpp_outbahan(4,$tahun);
	$pmbhn_mei = get_hpp_outbahan(5,$tahun);
	$pmbhn_jun = get_hpp_outbahan(6,$tahun);
	$pmbhn_jul = get_hpp_outbahan(7,$tahun);
	$pmbhn_agu = get_hpp_outbahan(8,$tahun);
	$pmbhn_sep = get_hpp_outbahan(9,$tahun);
	$pmbhn_okt = get_hpp_outbahan(10,$tahun);
	$pmbhn_nov = get_hpp_outbahan(11,$tahun);
	$pmbhn_des = get_hpp_outbahan(12,$tahun);
	$pmbhn_tot = $pmbhn_jan+$pmbhn_feb+$pmbhn_mar+$pmbhn_apr+$pmbhn_mei+$pmbhn_jun+$pmbhn_jul+$pmbhn_agu+$pmbhn_sep+$pmbhn_okt+$pmbhn_nov+$pmbhn_des;
	#Beban umum
	$tbbl_jan=0;$tbbl_feb=0;$tbbl_mar=0;$tbbl_apr=0;$tbbl_mei=0;$tbbl_jun=0;
	$tbbl_jul=0;$tbbl_agu=0;$tbbl_sep=0;$tbbl_okt=0;$tbbl_nov=0;$tbbl_des=0;
  	$qb = mysql_query("SELECT id,mbp_kode,mbp_nama FROM master_biayapendapatan WHERE mbp_status=20");
	while($db=mysql_fetch_array($qb)){
		$bbl_jan = get_hpp_biaya($db['id'],1,$tahun);
		$bbl_feb = get_hpp_biaya($db['id'],2,$tahun);
		$bbl_mar = get_hpp_biaya($db['id'],3,$tahun);
		$bbl_apr = get_hpp_biaya($db['id'],4,$tahun);
		$bbl_mei = get_hpp_biaya($db['id'],5,$tahun);
		$bbl_jun = get_hpp_biaya($db['id'],6,$tahun);
		$bbl_jul = get_hpp_biaya($db['id'],7,$tahun);
		$bbl_agu = get_hpp_biaya($db['id'],8,$tahun);
		$bbl_sep = get_hpp_biaya($db['id'],9,$tahun);
		$bbl_okt = get_hpp_biaya($db['id'],10,$tahun);
		$bbl_nov = get_hpp_biaya($db['id'],11,$tahun);
		$bbl_des = get_hpp_biaya($db['id'],12,$tahun);
		$bbl_tot = $bbl_jan+$bbl_feb+$bbl_mar+$bbl_apr+$bbl_mei+$bbl_jun+$bbl_jul+$bbl_agu+$bbl_sep+$bbl_okt+$bbl_nov+$bbl_des;
		$tbbl_jan = $tbbl_jan+$bbl_jan;
		$tbbl_feb = $tbbl_feb+$bbl_feb;
		$tbbl_mar = $tbbl_mar+$bbl_mar;
		$tbbl_apr = $tbbl_apr+$bbl_apr;
		$tbbl_mei = $tbbl_mei+$bbl_mei;
		$tbbl_jun = $tbbl_jun+$bbl_jun;
		$tbbl_jul = $tbbl_jul+$bbl_jul;
		$tbbl_agu = $tbbl_agu+$bbl_agu;
		$tbbl_sep = $tbbl_sep+$bbl_sep;
		$tbbl_okt = $tbbl_okt+$bbl_okt;
		$tbbl_nov = $tbbl_nov+$bbl_nov;
		$tbbl_des = $tbbl_des+$bbl_des;
		$tbbl_tot = $tbbl_jan+$tbbl_feb+$tbbl_mar+$tbbl_apr+$tbbl_mei+$tbbl_jun+$tbbl_jul+$tbbl_agu+$tbbl_sep+$tbbl_okt+$tbbl_nov+$tbbl_des;
	}
	#penyusutan
	$py_jan = get_lr_penyusutan(1,$tahun);
	$py_feb = get_lr_penyusutan(2,$tahun);
	$py_mar = get_lr_penyusutan(3,$tahun);
	$py_apr = get_lr_penyusutan(4,$tahun);
	$py_mei = get_lr_penyusutan(5,$tahun);
	$py_jun = get_lr_penyusutan(6,$tahun);
	$py_jul = get_lr_penyusutan(7,$tahun);
	$py_agu = get_lr_penyusutan(8,$tahun);
	$py_sep = get_lr_penyusutan(9,$tahun);
	$py_okt = get_lr_penyusutan(10,$tahun);
	$py_nov = get_lr_penyusutan(11,$tahun);
	$py_des = get_lr_penyusutan(12,$tahun);
	$py_tot = $py_jan+$py_feb+$py_mar+$py_apr+$py_mei+$py_jun+$py_jul+$py_agu+$py_sep+$py_okt+$py_nov+$py_des;
	#beban umum+penyusutan
	$tbblpy_jan = $tbbl_jan+$py_jan;
	$tbblpy_feb = $tbbl_feb+$py_feb;
	$tbblpy_mar = $tbbl_mar+$py_mar;
	$tbblpy_apr = $tbbl_apr+$py_apr;
	$tbblpy_mei = $tbbl_mei+$py_mei;
	$tbblpy_jun = $tbbl_jun+$py_jun;
	$tbblpy_jul = $tbbl_jul+$py_jul;
	$tbblpy_agu = $tbbl_agu+$py_agu;
	$tbblpy_sep = $tbbl_sep+$py_sep;
	$tbblpy_okt = $tbbl_okt+$py_okt;
	$tbblpy_nov = $tbbl_nov+$py_nov;
	$tbblpy_des = $tbbl_des+$py_des;
	$tbblpy_tot = $tbblpy_jan+$tbblpy_feb+$tbblpy_mar+$tbblpy_apr+$tbblpy_mei+$tbblpy_jun+$tbblpy_jul+$tbblpy_agu+$tbblpy_sep+$tbblpy_okt+$tbblpy_nov+$tbblpy_des;
	#Grand total HPP
	if($bulan==1){
		return $pmbhn_jan+$tbblpy_jan;
	}elseif($bulan==2){
		return $pmbhn_feb+$tbblpy_feb;
	}elseif($bulan==3){
		return $pmbhn_mar+$tbblpy_mar;
	}elseif($bulan==4){
		return $pmbhn_apr+$tbblpy_apr;
	}elseif($bulan==5){
		return $pmbhn_mei+$tbblpy_mei;
	}elseif($bulan==6){
		return $pmbhn_jun+$tbblpy_jun;
	}elseif($bulan==7){
		return $pmbhn_jul+$tbblpy_jul;
	}elseif($bulan==8){
		return $pmbhn_agu+$tbblpy_agu;
	}elseif($bulan==9){
		return $pmbhn_sep+$tbblpy_sep;
	}elseif($bulan==10){
		return $pmbhn_okt+$tbblpy_okt;
	}elseif($bulan==11){
		return $pmbhn_nov+$tbblpy_nov;
	}elseif($bulan==12){
		return $pmbhn_des+$tbblpy_des;
	}
}
function get_lr_bebanusaha($bulan,$tahun){
	$tbbl_jan=0;$tbbl_feb=0;$tbbl_mar=0;$tbbl_apr=0;$tbbl_mei=0;$tbbl_jun=0;
	$tbbl_jul=0;$tbbl_agu=0;$tbbl_sep=0;$tbbl_okt=0;$tbbl_nov=0;$tbbl_des=0;
  	$qb = mysql_query("SELECT id,mbp_kode,mbp_nama FROM master_biayapendapatan WHERE mbp_status=20");
	while($db=mysql_fetch_array($qb)){
		$bbl_jan = get_hpp_biaya($db['id'],1,$tahun);
		$bbl_feb = get_hpp_biaya($db['id'],2,$tahun);
		$bbl_mar = get_hpp_biaya($db['id'],3,$tahun);
		$bbl_apr = get_hpp_biaya($db['id'],4,$tahun);
		$bbl_mei = get_hpp_biaya($db['id'],5,$tahun);
		$bbl_jun = get_hpp_biaya($db['id'],6,$tahun);
		$bbl_jul = get_hpp_biaya($db['id'],7,$tahun);
		$bbl_agu = get_hpp_biaya($db['id'],8,$tahun);
		$bbl_sep = get_hpp_biaya($db['id'],9,$tahun);
		$bbl_okt = get_hpp_biaya($db['id'],10,$tahun);
		$bbl_nov = get_hpp_biaya($db['id'],11,$tahun);
		$bbl_des = get_hpp_biaya($db['id'],12,$tahun);
		$bbl_tot = $bbl_jan+$bbl_feb+$bbl_mar+$bbl_apr+$bbl_mei+$bbl_jun+$bbl_jul+$bbl_agu+$bbl_sep+$bbl_okt+$bbl_nov+$bbl_des;
		$tbbl_jan = $tbbl_jan+$bbl_jan;
		$tbbl_feb = $tbbl_feb+$bbl_feb;
		$tbbl_mar = $tbbl_mar+$bbl_mar;
		$tbbl_apr = $tbbl_apr+$bbl_apr;
		$tbbl_mei = $tbbl_mei+$bbl_mei;
		$tbbl_jun = $tbbl_jun+$bbl_jun;
		$tbbl_jul = $tbbl_jul+$bbl_jul;
		$tbbl_agu = $tbbl_agu+$bbl_agu;
		$tbbl_sep = $tbbl_sep+$bbl_sep;
		$tbbl_okt = $tbbl_okt+$bbl_okt;
		$tbbl_nov = $tbbl_nov+$bbl_nov;
		$tbbl_des = $tbbl_des+$bbl_des;
		$tbbl_tot = $tbbl_jan+$tbbl_feb+$tbbl_mar+$tbbl_apr+$tbbl_mei+$tbbl_jun+$tbbl_jul+$tbbl_agu+$tbbl_sep+$tbbl_okt+$tbbl_nov+$tbbl_des;
	}
	$py_jan = get_lr_penyusutan(1,$tahun);
	$py_feb = get_lr_penyusutan(2,$tahun);
	$py_mar = get_lr_penyusutan(3,$tahun);
	$py_apr = get_lr_penyusutan(4,$tahun);
	$py_mei = get_lr_penyusutan(5,$tahun);
	$py_jun = get_lr_penyusutan(6,$tahun);
	$py_jul = get_lr_penyusutan(7,$tahun);
	$py_agu = get_lr_penyusutan(8,$tahun);
	$py_sep = get_lr_penyusutan(9,$tahun);
	$py_okt = get_lr_penyusutan(10,$tahun);
	$py_nov = get_lr_penyusutan(11,$tahun);
	$py_des = get_lr_penyusutan(12,$tahun);
	$py_tot = $py_jan+$py_feb+$py_mar+$py_apr+$py_mei+$py_jun+$py_jul+$py_agu+$py_sep+$py_okt+$py_nov+$py_des;
	#total
	/*if($bulan==1){
		return $tbbl_jan+$py_jan;
	}elseif($bulan==2){
		return $tbbl_feb+$py_feb;
	}elseif($bulan==3){
		return $tbbl_mar+$py_mar;
	}elseif($bulan==4){
		return $tbbl_apr+$py_apr;
	}elseif($bulan==5){
		return $tbbl_mei+$py_mei;
	}elseif($bulan==6){
		return $tbbl_jun+$py_jun;
	}elseif($bulan==7){
		return $tbbl_jul+$py_jul;
	}elseif($bulan==8){
		return $tbbl_agu+$py_agu;
	}elseif($bulan==9){
		return $tbbl_sep+$py_sep;
	}elseif($bulan==10){
		return $tbbl_okt+$py_okt;
	}elseif($bulan==11){
		return $tbbl_nov+$py_nov;
	}elseif($bulan==12){
		return $tbbl_des+$py_des;
	}*/
	return 0;
}
function get_lr_prndapatan($tipe,$bulan,$tahun){
	$q = mysql_fetch_array(mysql_query("SELECT SUM(bk_amount) jumlah FROM buku_besar WHERE MONTH(bk_tanggal)='$bulan' AND YEAR(bk_tanggal)='$tahun' 
		      AND bk_status=20 AND bk_idmbp='$tipe' GROUP BY bk_idmbp"));
	return $q['jumlah'];		  
}
function get_lr_pendapatanbiaya($tipe,$bulan,$tahun){
	$q=mysql_fetch_array(mysql_query("SELECT SUM(bk_amount) nilai FROM buku_besar WHERE bk_status='$tipe' AND MONTH(bk_tanggal)='$bulan' AND YEAR(bk_tanggal)='$tahun'"));
	return $q['nilai'];
}
function get_lr_penyusutan($bulan,$tahun){
	$q=mysql_fetch_array(mysql_query("SELECT SUM(a.pyd_nilai) nilai FROM penyusutan_detail a INNER JOIN penyusutan b ON b.id=a.pyd_hid
	WHERE b.py_status=10 AND a.pyd_status=10 AND MONTH(a.pyd_tanggal)='$bulan' AND YEAR(a.pyd_tanggal)='$tahun'"));
	return $q['nilai'];
}
#neraca
function get_nr_kas($fromdate,$todate){
	$saldo=0;
	$qcoa = mysql_query("SELECT j_acctno, CONCAT_WS(' - ', j_acctno, j_name1) codename FROM jurnal_master WHERE j_category=1 ORDER BY j_acctno ASC");
	while($dcoa=mysql_fetch_array($qcoa)){
		$typ = substr($dcoa['j_acctno'],0,1);
		$sa=mysql_fetch_array(mysql_query("SELECT id, sa_price FROM stock_acct WHERE sa_date='$fromdate' AND sa_status=10 AND sa_acctno='$dcoa[j_acctno]'"));
	$no=1;
	$saldo = $saldo+$sa['sa_price']; $t_amountin=0; $t_amountout=0;
	$qd=mysql_query("SELECT a.tanggal, a.noreferensi, a.keterangan, a.amountin, a.amountout, a.flag FROM
			(SELECT a.jurnal_date tanggal, a.jurnal_invno noreferensi, b.jurnald_desc keterangan, b.jurnald_amount amountin, 
			0 amountout, 1 flag FROM jurnal a INNER JOIN jurnal_detail b ON b.jurnal_no=a.jurnal_no
			WHERE a.jurnal_status=20 AND a.jurnal_date BETWEEN '$fromdate' AND '$todate' AND jurnald_acctno='$dcoa[j_acctno]' AND b.jurnald_status=10		
			UNION ALL
			SELECT a.jurnal_date tanggal, a.jurnal_invno noreferensi, b.jurnald_desc keterangan, 0 amountin, 
			b.jurnald_amount amountout, 2 flag FROM jurnal a INNER JOIN jurnal_detail b ON b.jurnal_no=a.jurnal_no
			WHERE a.jurnal_status=20 AND a.jurnal_date BETWEEN '$fromdate' AND '$todate' AND jurnald_acctno='$dcoa[j_acctno]' AND b.jurnald_status=20) a
			ORDER BY a.tanggal ASC");
	$num = mysql_num_rows($qd);		
	if($num==0){
		$t_saldo=$saldo;
	}else{		
	while($dd=mysql_fetch_array($qd)){	
		if($typ==1){
			$saldo = $saldo+$dd['amountin']-$dd['amountout'];
		}else{
			$saldo = $saldo-$dd['amountin']+$dd['amountout'];
		}
		$t_saldo=$saldo;
		$t_amountin  = $t_amountin+$dd['amountin'];
		$t_amountout = $t_amountout+$dd['amountout'];
	 } }  } 
	 if(!empty($t_saldo)){
	 	return $t_saldo;
	 }else{
	 	return 0;
	 }
	 
}
function get_nr_bank($fromdate,$todate){
	$saldo=0;
	$qcoa = mysql_query("SELECT j_acctno, CONCAT_WS(' - ', j_acctno, j_name1) codename FROM jurnal_master WHERE j_category=2 ORDER BY j_acctno ASC");
	while($dcoa=mysql_fetch_array($qcoa)){
		$typ = substr($dcoa['j_acctno'],0,1);
		$sa=mysql_fetch_array(mysql_query("SELECT id, sa_price FROM stock_acct WHERE sa_date='$fromdate' AND sa_status=10 AND sa_acctno='$dcoa[j_acctno]'"));
	$no=1;
	$saldo =$saldo+$sa['sa_price']; $t_amountin=0; $t_amountout=0;
	$qd=mysql_query("SELECT a.tanggal, a.noreferensi, a.keterangan, a.amountin, a.amountout, a.flag FROM
			(SELECT a.jurnal_date tanggal, a.jurnal_invno noreferensi, b.jurnald_desc keterangan, b.jurnald_amount amountin, 
			0 amountout, 1 flag FROM jurnal a INNER JOIN jurnal_detail b ON b.jurnal_no=a.jurnal_no
			WHERE a.jurnal_status=20 AND a.jurnal_date BETWEEN '$fromdate' AND '$todate' AND jurnald_acctno='$dcoa[j_acctno]' AND b.jurnald_status=10		
			UNION ALL
			SELECT a.jurnal_date tanggal, a.jurnal_invno noreferensi, b.jurnald_desc keterangan, 0 amountin, 
			b.jurnald_amount amountout, 2 flag FROM jurnal a INNER JOIN jurnal_detail b ON b.jurnal_no=a.jurnal_no
			WHERE a.jurnal_status=20 AND a.jurnal_date BETWEEN '$fromdate' AND '$todate' AND jurnald_acctno='$dcoa[j_acctno]' AND b.jurnald_status=20) a
			ORDER BY a.tanggal ASC");
	$num = mysql_num_rows($qd);		
	if($num==0){
		$t_saldo=$saldo;
	}else{		
	while($dd=mysql_fetch_array($qd)){	
		if($typ==1){
			$saldo = $saldo+$dd['amountin']-$dd['amountout'];
		}else{
			$saldo = $saldo-$dd['amountin']+$dd['amountout'];
		}
		$t_saldo=$saldo;
		$t_amountin  = $t_amountin+$dd['amountin'];
		$t_amountout = $t_amountout+$dd['amountout'];
	 } }  } 
	 if(!empty($t_saldo)){
	 	return $t_saldo;
	 }else{
	 	return 0;
	 }
}
function get_nr_piutang($fromdate,$todate){
	$gt_saldo=0;
	$qh=mysql_query("SELECT cust_name nm_penerima FROM customer WHERE cust_status=10 ORDER BY cust_name ASC");
	while($dh=mysql_fetch_array($qh)){
		$sa=mysql_fetch_array(mysql_query("SELECT a.id, a.sa_price FROM stock_acct a INNER JOIN customer b ON b.id=a.sa_custid 
		    WHERE a.sa_date='$fromdate' AND a.sa_status=10 AND b.cust_name='$dh[nm_penerima]'"));
	$no=1;$saldo = $sa['sa_price']; $t_amountin=0; $t_amountout=0;
	$qd=mysql_query("SELECT a.* FROM (SELECT  b.outh_invdate_acct tanggal, b.outh_invno_acct noreferensi, 
				CONCAT_WS(' - ', 'Penjualan Barang dari No. Dok.', b.outh_docno) as keterangan, SUM(a.outd_price) debit, 0 kredit, b.outh_custid, b.nm_penerima
				FROM outgoing_detail a INNER JOIN outgoing b ON b.id=a.outd_hid WHERE b.outh_status=20 AND b.outh_paytype='Piutang' 
				AND b.outh_docdate BETWEEN '$fromdate' AND '$todate' GROUP BY b.outh_invno_acct
				UNION ALL
				SELECT a.jurnal_date tanggal, b.jurnald_invno_acct noreferensi, b.jurnald_desc keterangan, 0 debit, 
				b.jurnald_amount kredit, a.jurnal_idcust outh_custid, c.cust_name nm_penerima
				FROM jurnal a INNER JOIN jurnal_detail b ON b.jurnal_no=a.jurnal_no INNER JOIN customer c ON c.id=a.jurnal_idcust
				INNER JOIN jurnal_master d ON d.j_acctno=b.jurnald_acctno AND d.j_category=4
				WHERE a.jurnal_status=20 AND a.jurnal_date BETWEEN '$fromdate' AND '$todate' AND b.jurnald_status=20) a 
				WHERE a.nm_penerima='$dh[nm_penerima]' ORDER BY a.tanggal ASC");
	$num = mysql_num_rows($qd);		
	if($num==0){
		$t_saldo=$saldo;
	}else{		
	while($dd=mysql_fetch_array($qd)){	
		$saldo = $saldo + $dd['debit'] - $dd['kredit'];
		$t_saldo=$saldo;
		$t_amountin=$t_amountin+$dd['debit'];
		$t_amountout=$t_amountout+$dd['kredit'];
 		$no++; } } 
  	  $gt_saldo=$gt_saldo+$t_saldo;} 
	 
	 if(!empty($gt_saldo)){
	 	return $gt_saldo;
	 }else{
	 	return 0;
	 }
}
function get_nr_hutang($fromdate,$todate){
	$gt_saldo=0; 
	$qh=mysql_query("SELECT sup_name nm_pemasok FROM supplier WHERE sup_status=10 ORDER BY sup_name ASC");
	while($dh=mysql_fetch_array($qh)){
		$sa=mysql_fetch_array(mysql_query("SELECT a.id, a.sa_price FROM stock_acct a INNER JOIN supplier b ON b.id=a.sa_supid 
		    WHERE a.sa_date='$fromdate' AND a.sa_status=10 AND b.sup_name='$dh[nm_pemasok]'"));
	$no=1;$saldo = $sa['sa_price']; $t_amountin=0; $t_amountout=0;
	$qd=mysql_query("SELECT a.* FROM (SELECT  b.inch_invdate_acct tanggal, b.inch_invno_acct noreferensi, 
				CONCAT_WS(' - ', 'Pembelian Barang dari No. Dok.', b.inch_docno) as keterangan, 0 debit, SUM(a.incd_price) kredit, b.inch_supid, b.nm_pemasok
				FROM incoming_detail a INNER JOIN incoming b ON b.id=a.incd_hid WHERE b.inch_status=20 AND b.inch_paytype='Hutang' 
				AND b.inch_docdate BETWEEN '$fromdate' AND '$todate' GROUP BY b.inch_invno_acct
				UNION ALL
				SELECT a.jurnal_date tanggal, b.jurnald_invno_acct noreferensi, b.jurnald_desc keterangan, b.jurnald_amount debit, 
				0 kredit, a.jurnal_idsupp inch_supid, c.sup_name nm_pemasok
				FROM jurnal a INNER JOIN jurnal_detail b ON b.jurnal_no=a.jurnal_no INNER JOIN supplier c ON c.id=a.jurnal_idsupp
				INNER JOIN jurnal_master d ON d.j_acctno=b.jurnald_acctno AND d.j_category=3
				WHERE a.jurnal_status=20 AND a.jurnal_date BETWEEN '$fromdate' AND '$todate' AND b.jurnald_status=10) a 
				WHERE a.nm_pemasok='$dh[nm_pemasok]' ORDER BY a.tanggal ASC");
	$num = mysql_num_rows($qd);		
	if($num==0){
		$t_saldo=$saldo;
	}else{		
	while($dd=mysql_fetch_array($qd)){	
		$saldo = $saldo - $dd['debit'] + $dd['kredit'];
		$t_saldo=$saldo;
		$t_amountin=$t_amountin+$dd['debit'];
		$t_amountout=$t_amountout+$dd['kredit'];
  		$no++; } } 
   $gt_saldo=$gt_saldo+$t_saldo;} 
   
   if(!empty($gt_saldo)){
	 	return $gt_saldo;
	 }else{
	 	return 0;
	 }
}
function get_nr_mpenyusutan(){
	$gtnilai=0;$gtpay=0;$gtsisa=0; 
	$qh=mysql_query("SELECT id, pyt_tipe FROM penyusutan_tipe WHERE pyt_status=10 ORDER BY id ASC");
	while($dh=mysql_fetch_array($qh)){
		$no=1;$tnilai=0;$tpay=0;$tsisa=0;
		$qd=mysql_query("SELECT * FROM penyusutan WHERE py_status<>100 AND py_tipe='$dh[id]'");
		while($dd=mysql_fetch_array($qd)){	
			$py =mysql_fetch_array(mysql_query("SELECT SUM(pyd_nilai) susut FROM penyusutan_detail WHERE pyd_hid='$dd[id]' AND pyd_status=10"));
			$sisa   = $dd['py_nilai'] - $py['susut']; 
			if($sisa>0){
			$tnilai = $tnilai+$dd['py_nilai'];
			$tpay   = $tpay+$py['susut'];
			$tsisa  = $tsisa+$sisa;	
			}
		}
		$gtnilai= $gtnilai+$tnilai; 
		$gtpay  = $gtpay+$tpay;
		$gtsisa = $gtsisa+$tsisa;
	}
	return $gtnilai;
}
function get_nr_penyusutan(){
	$gtnilai=0;$gtpay=0;$gtsisa=0; 
	$qh=mysql_query("SELECT id, pyt_tipe FROM penyusutan_tipe WHERE  pyt_status=10 ORDER BY id ASC");
	while($dh=mysql_fetch_array($qh)){
		$no=1;$tnilai=0;$tpay=0;$tsisa=0;
		$qd=mysql_query("SELECT * FROM penyusutan WHERE py_status<>100 AND py_tipe='$dh[id]'");
		while($dd=mysql_fetch_array($qd)){	
			$py =mysql_fetch_array(mysql_query("SELECT SUM(pyd_nilai) susut FROM penyusutan_detail WHERE pyd_hid='$dd[id]' AND pyd_status=10"));
			$sisa   = $dd['py_nilai'] - $py['susut']; 
			if($sisa>0){
			$tnilai = $tnilai+$dd['py_nilai'];
			$tpay   = $tpay+$py['susut'];
			$tsisa  = $tsisa+$sisa;	
			}
		}
		$gtnilai= $gtnilai+$tnilai; 
		$gtpay  = $gtpay+$tpay;
		$gtsisa = $gtsisa+$tsisa;
	}
	return $gtpay;
}
function get_nr_perbarang($fromdate,$todate){
	/*#bahan baku
	$pbbb_jan = get_hpp_bahan(1,1,$tahun);
	$pbbb_feb = get_hpp_bahan(1,2,$tahun);
	$pbbb_mar = get_hpp_bahan(1,3,$tahun);
	$pbbb_apr = get_hpp_bahan(1,4,$tahun);
	$pbbb_mei = get_hpp_bahan(1,5,$tahun);
	$pbbb_jun = get_hpp_bahan(1,6,$tahun);
	$pbbb_jul = get_hpp_bahan(1,7,$tahun);
	$pbbb_agu = get_hpp_bahan(1,8,$tahun);
	$pbbb_sep = get_hpp_bahan(1,9,$tahun);
	$pbbb_okt = get_hpp_bahan(1,10,$tahun);
	$pbbb_nov = get_hpp_bahan(1,11,$tahun);
	$pbbb_des = get_hpp_bahan(1,12,$tahun);
	$pbbb_tot = $pbbb_jan+$pbbb_feb+$pbbb_mar+$pbbb_apr+$pbbb_mei+$pbbb_jun+$pbbb_jul+$pbbb_agu+$pbbb_sep+$pbbb_okt+$pbbb_nov+$pbbb_des;
	#bahan penolong
	$pbbp_jan = get_hpp_bahan(6,1,$tahun);
	$pbbp_feb = get_hpp_bahan(6,2,$tahun);
	$pbbp_mar = get_hpp_bahan(6,3,$tahun);
	$pbbp_apr = get_hpp_bahan(6,4,$tahun);
	$pbbp_mei = get_hpp_bahan(6,5,$tahun);
	$pbbp_jun = get_hpp_bahan(6,6,$tahun);
	$pbbp_jul = get_hpp_bahan(6,7,$tahun);
	$pbbp_agu = get_hpp_bahan(6,8,$tahun);
	$pbbp_sep = get_hpp_bahan(6,9,$tahun);
	$pbbp_okt = get_hpp_bahan(6,10,$tahun);
	$pbbp_nov = get_hpp_bahan(6,11,$tahun);
	$pbbp_des = get_hpp_bahan(6,12,$tahun);
	$pbbp_tot = $pbbp_jan+$pbbp_feb+$pbbp_mar+$pbbp_apr+$pbbp_mei+$pbbp_jun+$pbbp_jul+$pbbp_agu+$pbbp_sep+$pbbp_okt+$pbbp_nov+$pbbp_des;
	#total
	$tbb_jan = $pbbb_jan+$pbbp_jan;
	$tbb_feb = $pbbb_feb+$pbbp_feb;
	$tbb_mar = $pbbb_mar+$pbbp_mar;
	$tbb_apr = $pbbb_apr+$pbbp_apr;
	$tbb_mei = $pbbb_mei+$pbbp_mei;
	$tbb_jun = $pbbb_jun+$pbbp_jun;
	$tbb_jul = $pbbb_jul+$pbbp_jul;
	$tbb_agu = $pbbb_agu+$pbbp_agu;
	$tbb_sep = $pbbb_sep+$pbbp_sep;
	$tbb_okt = $pbbb_okt+$pbbp_okt;
	$tbb_nov = $pbbb_nov+$pbbp_nov;
	$tbb_des = $pbbb_des+$pbbp_des;
	$tbb_tot = $tbb_jan+$tbb_feb+$tbb_mar+$tbb_apr+$tbb_mei+$tbb_jun+$tbb_jul+$tbb_agu+$tbb_sep+$tbb_okt+$tbb_nov+$tbb_des;
	#Remain
	$pmbhn1_jan = get_hpp_outbahan(1,$tahun);
	$pmbhn1_feb = get_hpp_outbahan(2,$tahun);
	$pmbhn1_mar = get_hpp_outbahan(3,$tahun);
	$pmbhn1_apr = get_hpp_outbahan(4,$tahun);
	$pmbhn1_mei = get_hpp_outbahan(5,$tahun);
	$pmbhn1_jun = get_hpp_outbahan(6,$tahun);
	$pmbhn1_jul = get_hpp_outbahan(7,$tahun);
	$pmbhn1_agu = get_hpp_outbahan(8,$tahun);
	$pmbhn1_sep = get_hpp_outbahan(9,$tahun);
	$pmbhn1_okt = get_hpp_outbahan(10,$tahun);
	$pmbhn1_nov = get_hpp_outbahan(11,$tahun);
	$pmbhn1_des = get_hpp_outbahan(12,$tahun);
	$pmbhn1_tot = $pmbhn1_jan+$pmbhn1_feb+$pmbhn1_mar+$pmbhn1_apr+$pmbhn1_mei+$pmbhn1_jun+$pmbhn1_jul+$pmbhn1_agu+$pmbhn1_sep+$pmbhn1_okt+$pmbhn1_nov+$pmbhn1_des;
	/*$rbh_jan = $pmbhn1_jan-$tbb_jan;
	$rbh_feb = $pmbhn1_feb-$tbb_feb;
	$rbh_mar = $pmbhn1_mar-$tbb_mar;
	$rbh_apr = $pmbhn1_apr-$tbb_apr;
	$rbh_mei = $pmbhn1_mei-$tbb_mei;
	$rbh_jun = $pmbhn1_jun-$tbb_jun;
	$rbh_jul = $pmbhn1_jul-$tbb_jul;
	$rbh_agu = $pmbhn1_agu-$tbb_agu;
	$rbh_sep = $pmbhn1_sep-$tbb_sep;
	$rbh_okt = $pmbhn1_okt-$tbb_okt;
	$rbh_nov = $pmbhn1_nov-$tbb_nov;
	$rbh_des = $pmbhn1_des-$tbb_des;
	$rbh_jan = $tbb_jan-$pmbhn1_jan;
	$rbh_feb = $tbb_feb-$pmbhn1_feb;
	$rbh_mar = $tbb_mar-$pmbhn1_mar;
	$rbh_apr = $tbb_apr-$pmbhn1_apr;
	$rbh_mei = $tbb_mei-$pmbhn1_mei;
	$rbh_jun = $tbb_jun-$pmbhn1_jun;
	$rbh_jul = $tbb_jul-$pmbhn1_jul;
	$rbh_agu = $tbb_agu-$pmbhn1_agu;
	$rbh_sep = $tbb_sep-$pmbhn1_sep;
	$rbh_okt = $tbb_okt-$pmbhn1_okt;
	$rbh_nov = $tbb_nov-$pmbhn1_nov;
	$rbh_des = $tbb_des-$pmbhn1_des;
	$rbh_tot = $rbh_jan+$rbh_feb+$rbh_mar+$rbh_apr+$rbh_mei+$rbh_jun+$rbh_jul+$rbh_agu+$rbh_sep+$rbh_okt+$rbh_nov+$rbh_des;
	
	return $rbh_tot;*/
	$tendqty=0; $tamount=0;
	$qd = mysql_query("SELECT b.prodid, b.doctype, b.docno, b.docdate, b.inqty, b.outqty, (b.inqty-b.outqty) endqty, b.price, b.prod_code, 
				b.prod_name, b.prod_sat FROM
				(SELECT a.prodid, a.doctype, a.docno, a.docdate, SUM(a.inqty) inqty, SUM(a.outqty) outqty, a.price, b.prod_code, b.prod_name, b.prod_sat  FROM
				(SELECT stock_prodid prodid, stock_doctype doctype, stock_docno docno, stock_docdate docdate, stock_qty inqty, 0 outqty, (stock_price/stock_qty) price 
				FROM stock WHERE stock_status=10 AND stock_date='$fromdate' AND stock_loc=1
				UNION ALL
				SELECT b.incd_prodid prodid, a.inch_doctype doctype , a.inch_docno docno, a.inch_docdate docdate, b.incd_qty inqty, 0 outqty, (b.incd_price/b.incd_qty) price 
				FROM incoming a
				INNER JOIN incoming_detail b ON b.incd_hid=a.id
				WHERE a.inch_status=20 AND a.inch_docdate BETWEEN '$fromdate' AND '$todate'
				UNION ALL
				SELECT a.trfd_prodid prodid, a.trfd_doctype doctype , a.trfd_docno docno, a.trfd_docdate docdate, 0 inqty, a.trfd_qty outqty, 0 price 
				FROM transfer_detailexbc a
				INNER JOIN transfer b ON b.id=a.trfd_hid
				WHERE b.trf_status=20 AND a.trfd_status=10 AND b.trf_category=1 AND b.trf_spbdate BETWEEN '$fromdate' AND '$todate'
				UNION ALL
				SELECT a.trfd_prodid prodid, a.trfd_doctype doctype , a.trfd_docno docno, a.trfd_docdate docdate, a.trfd_qty inqty, 0 outqty, 0 price 
				FROM transfer_detailexbc a
				INNER JOIN transfer b ON b.id=a.trfd_hid
				WHERE b.trf_status=20 AND a.trfd_status=10 AND b.trf_category=3 AND b.trf_spbdate BETWEEN '$fromdate' AND '$todate'
				UNION ALL
				SELECT a.prodid, a.jenis_dokpab_asal doctype , a.no_dokpab_asal docno, a.tgl_dokpab_asal docdate, 0 inqty, a.jumlah outqty, 0 price 
				FROM outgoing_dokasal a
				INNER JOIN outgoing b ON b.id=a.outh_hid            
				WHERE b.outh_status=20 AND a.log_status=20 AND b.outh_docdate BETWEEN '$fromdate' AND '$todate') a
				INNER JOIN product b ON b.id=a.prodid
				WHERE b.prod_cat IN(1,6) GROUP BY a.prodid, a.doctype, a.docno, a.docdate) b
				WHERE (b.inqty-b.outqty) > 0 ORDER BY b.docno, b.prod_code ASC;");
	while($dd=mysql_fetch_array($qd)){	
		$amount  = $dd['endqty']*$dd['price'];	
		$tendqty = $tendqty+$dd['endqty'];
		$tamount = $tamount+$amount;
	}
	return $tamount;	
}
function get_nr_modal($tahun){
	$q=mysql_fetch_array(mysql_query("SELECT mbh_nilai FROM master_mbh WHERE mbh_tipe=4 AND mbh_tahun='$tahun' AND mbh_status=10"));
	return $q['mbh_nilai'];
}
function get_nr_thnbrjln($tahun){
	#penjualan
	$pj_jan = get_lrln_penjualan(1,$tahun);
	$pj_feb = get_lrln_penjualan(2,$tahun);
	$pj_mar = get_lrln_penjualan(3,$tahun);
	$pj_apr = get_lrln_penjualan(4,$tahun);
	$pj_mei = get_lrln_penjualan(5,$tahun);
	$pj_jun = get_lrln_penjualan(6,$tahun);
	$pj_jul = get_lrln_penjualan(7,$tahun);
	$pj_agu = get_lrln_penjualan(8,$tahun);
	$pj_sep = get_lrln_penjualan(9,$tahun);
	$pj_okt = get_lrln_penjualan(10,$tahun);
	$pj_nov = get_lrln_penjualan(11,$tahun);
	$pj_des = get_lrln_penjualan(12,$tahun);
	$pj_tot = $pj_jan+$pj_feb+$pj_mar+$pj_apr+$pj_mei+$pj_jun+$pj_jul+$pj_agu+$pj_sep+$pj_okt+$pj_nov+$pj_des;
	#HPP
	$hpp_jan = get_lr_hpp(1,$tahun);
	$hpp_feb = get_lr_hpp(2,$tahun);
	$hpp_mar = get_lr_hpp(3,$tahun);
	$hpp_apr = get_lr_hpp(4,$tahun);
	$hpp_mei = get_lr_hpp(5,$tahun);
	$hpp_jun = get_lr_hpp(6,$tahun);
	$hpp_jul = get_lr_hpp(7,$tahun);
	$hpp_agu = get_lr_hpp(8,$tahun);
	$hpp_sep = get_lr_hpp(9,$tahun);
	$hpp_okt = get_lr_hpp(10,$tahun);
	$hpp_nov = get_lr_hpp(11,$tahun);
	$hpp_des = get_lr_hpp(12,$tahun);
	$hpp_tot = $hpp_jan+$hpp_feb+$hpp_mar+$hpp_apr+$hpp_mei+$hpp_jun+$hpp_jul+$hpp_agu+$hpp_sep+$hpp_okt+$hpp_nov+$hpp_des;
	#profit kotor
	$fk_jan = $pj_jan-$hpp_jan;
	$fk_feb = $pj_feb-$hpp_feb;
	$fk_mar = $pj_mar-$hpp_mar;
	$fk_apr = $pj_apr-$hpp_apr;
	$fk_mei = $pj_mei-$hpp_mei;
	$fk_jun = $pj_jun-$hpp_jun;
	$fk_jul = $pj_jul-$hpp_jul;
	$fk_agu = $pj_agu-$hpp_agu;
	$fk_sep = $pj_sep-$hpp_sep;
	$fk_okt = $pj_okt-$hpp_okt;
	$fk_nov = $pj_nov-$hpp_nov;
	$fk_des = $pj_des-$hpp_des;
	$fk_tot = $fk_jan+$fk_feb+$fk_mar+$fk_apr+$fk_mei+$fk_jun+$fk_jul+$fk_agu+$fk_sep+$fk_okt+$fk_nov+$fk_des;
	#beban usaha
	$bu_jan = get_lr_bebanusaha(1,$tahun);
	$bu_feb = get_lr_bebanusaha(2,$tahun);
	$bu_mar = get_lr_bebanusaha(3,$tahun);
	$bu_apr = get_lr_bebanusaha(4,$tahun);
	$bu_mei = get_lr_bebanusaha(5,$tahun);
	$bu_jun = get_lr_bebanusaha(6,$tahun);
	$bu_jul = get_lr_bebanusaha(7,$tahun);
	$bu_agu = get_lr_bebanusaha(8,$tahun);
	$bu_sep = get_lr_bebanusaha(9,$tahun);
	$bu_okt = get_lr_bebanusaha(10,$tahun);
	$bu_nov = get_lr_bebanusaha(11,$tahun);
	$bu_des = get_lr_bebanusaha(12,$tahun);
	$bu_tot = $bu_jan+$bu_feb+$bu_mar+$bu_apr+$bu_mei+$bu_jun+$bu_jul+$bu_agu+$bu_sep+$bu_okt+$bu_nov+$bu_des;
	#laba usaha
	$lu_jan = $fk_jan-$bu_jan;
	$lu_feb = $fk_feb-$bu_feb;
	$lu_mar = $fk_mar-$bu_mar;
	$lu_apr = $fk_apr-$bu_apr;
	$lu_mei = $fk_mei-$bu_mei;
	$lu_jun = $fk_jun-$bu_jun;
	$lu_jul = $fk_jul-$bu_jul;
	$lu_agu = $fk_agu-$bu_agu;
	$lu_sep = $fk_sep-$bu_sep;
	$lu_okt = $fk_okt-$bu_okt;
	$lu_nov = $fk_nov-$bu_nov;
	$lu_des = $fk_des-$bu_des;
	$lu_tot = $lu_jan+$lu_feb+$lu_mar+$lu_apr+$lu_mei+$lu_jun+$lu_jul+$lu_agu+$lu_sep+$lu_okt+$lu_nov+$lu_des;
	#pendapatan beban
	$tbbl_jan=0;$tbbl_feb=0;$tbbl_mar=0;$tbbl_apr=0;$tbbl_mei=0;$tbbl_jun=0;
	$tbbl_jul=0;$tbbl_agu=0;$tbbl_sep=0;$tbbl_okt=0;$tbbl_nov=0;$tbbl_des=0;
  	$qb = mysql_query("SELECT id,mbp_kode,mbp_nama FROM master_biayapendapatan WHERE mbp_status=10");
	while($db=mysql_fetch_array($qb)){
		$bbl_jan = get_lr_prndapatan($db['id'],1,$tahun);
		$bbl_feb = get_lr_prndapatan($db['id'],2,$tahun);
		$bbl_mar = get_lr_prndapatan($db['id'],3,$tahun);
		$bbl_apr = get_lr_prndapatan($db['id'],4,$tahun);
		$bbl_mei = get_lr_prndapatan($db['id'],5,$tahun);
		$bbl_jun = get_lr_prndapatan($db['id'],6,$tahun);
		$bbl_jul = get_lr_prndapatan($db['id'],7,$tahun);
		$bbl_agu = get_lr_prndapatan($db['id'],8,$tahun);
		$bbl_sep = get_lr_prndapatan($db['id'],9,$tahun);
		$bbl_okt = get_lr_prndapatan($db['id'],10,$tahun);
		$bbl_nov = get_lr_prndapatan($db['id'],11,$tahun);
		$bbl_des = get_lr_prndapatan($db['id'],12,$tahun);
		$bbl_tot = $bbl_jan+$bbl_feb+$bbl_mar+$bbl_apr+$bbl_mei+$bbl_jun+$bbl_jul+$bbl_agu+$bbl_sep+$bbl_okt+$bbl_nov+$bbl_des;
		$tbbl_jan = $tbbl_jan+$bbl_jan;
		$tbbl_feb = $tbbl_feb+$bbl_feb;
		$tbbl_mar = $tbbl_mar+$bbl_mar;
		$tbbl_apr = $tbbl_apr+$bbl_apr;
		$tbbl_mei = $tbbl_mei+$bbl_mei;
		$tbbl_jun = $tbbl_jun+$bbl_jun;
		$tbbl_jul = $tbbl_jul+$bbl_jul;
		$tbbl_agu = $tbbl_agu+$bbl_agu;
		$tbbl_sep = $tbbl_sep+$bbl_sep;
		$tbbl_okt = $tbbl_okt+$bbl_okt;
		$tbbl_nov = $tbbl_nov+$bbl_nov;
		$tbbl_des = $tbbl_des+$bbl_des;
		$tbbl_tot = $tbbl_jan+$tbbl_feb+$tbbl_mar+$tbbl_apr+$tbbl_mei+$tbbl_jun+$tbbl_jul+$tbbl_agu+$tbbl_sep+$tbbl_okt+$tbbl_nov+$tbbl_des;
	}
	#sebelum pajak
	$sp_jan = $lu_jan+$tbbl_jan;
	$sp_feb = $lu_feb+$tbbl_feb;
	$sp_mar = $lu_mar+$tbbl_mar;
	$sp_apr = $lu_apr+$tbbl_apr;
	$sp_mei = $lu_mei+$tbbl_mei;
	$sp_jun = $lu_jun+$tbbl_jun;
	$sp_jul = $lu_jul+$tbbl_jul;
	$sp_agu = $lu_agu+$tbbl_agu;
	$sp_sep = $lu_sep+$tbbl_sep;
	$sp_okt = $lu_okt+$tbbl_okt;
	$sp_nov = $lu_nov+$tbbl_nov;
	$sp_des = $lu_des+$tbbl_des;
	$sp_tot = $sp_jan+$sp_feb+$sp_mar+$sp_apr+$sp_mei+$sp_jun+$sp_jul+$sp_agu+$sp_sep+$sp_okt+$sp_nov+$sp_des;
	#perkiraan pajak
	$tpp_jan = 0;
	$tpp_feb = 0;
	$tpp_mar = 0;
	$tpp_apr = 0;
	$tpp_mei = 0;
	$tpp_jun = 0;
	$tpp_jul = 0;
	$tpp_agu = 0;
	$tpp_sep = 0;
	$tpp_okt = 0;
	$tpp_nov = 0;
	$tpp_des = 0;
	$tpp_tot = $tpp_jan+$tpp_feb+$tpp_mar+$tpp_apr+$tpp_mei+$tpp_jun+$tpp_jul+$tpp_agu+$tpp_sep+$tpp_okt+$tpp_nov+$tpp_des;
	#laba bersih
	$fb_jan = $sp_jan+$tpp_jan;
	$fb_feb = $sp_feb+$tpp_feb;
	$fb_mar = $sp_mar+$tpp_mar;
	$fb_apr = $sp_apr+$tpp_apr;
	$fb_mei = $sp_mei+$tpp_mei;
	$fb_jun = $sp_jun+$tpp_jun;
	$fb_jul = $sp_jul+$tpp_jul;
	$fb_agu = $sp_agu+$tpp_agu;
	$fb_sep = $sp_sep+$tpp_sep;
	$fb_okt = $sp_okt+$tpp_okt;
	$fb_nov = $sp_nov+$tpp_nov;
	$fb_des = $sp_des+$tpp_des;
	$fb_tot = $fb_jan+$fb_feb+$fb_mar+$fb_apr+$fb_mei+$fb_jun+$fb_jul+$fb_agu+$fb_sep+$fb_okt+$fb_nov+$fb_des;
	
	return $fb_tot;
}
function get_rate($curr,$rate_dfrom){
	$q = mysql_fetch_array(mysql_query("SELECT rate_nto FROM rate WHERE rate_tfrom='$curr' AND rate_dfrom='$rate_dfrom' AND rate_status=10"));
	if($curr=="IDR" || empty($curr)){
		return 1;
	}else{
		return number_format($q['rate_nto'],0,'','');
	}
}
function get_price_incoming($prodid){
	$q = mysql_fetch_array(mysql_query("SELECT CASE WHEN b.sumber='MANUAL' THEN 
					CASE WHEN a.incd_currency='IDR' THEN a.incd_price_satuan
					ELSE a.incd_price_rate END
			   ELSE (a.incd_price/a.incd_qty) END AS price     
		FROM incoming_detail a INNER JOIN incoming b ON b.id=a.incd_hid
		WHERE b.inch_status=20 AND a.incd_prodid=1 ORDER BY a.id DESC LIMIT 1"));
	return $q['price'];
}
function get_price_transfer($prodid){
	$q = mysql_fetch_array(mysql_query("SELECT CASE WHEN a.trfd_currency='IDR' THEN a.trfd_price_satuan
					ELSE a.trfd_price_rate END AS price     
		FROM transfer_detailexbc a INNER JOIN transfer b ON b.id=a.trfd_hid
		WHERE b.trf_status=20 AND a.trfd_prodid='$prodid' ORDER BY a.id DESC LIMIT 1"));
	return $q['price'];
}
function get_pono($tipe){
	if($tipe=="Sales"){
		$tipes = "SO";
	}else{
		$tipes = "PO";
	}
	$year   = date('Y');
	$month  = date('m');
	$lno = mysql_fetch_array(mysql_query("SELECT SUBSTR(po_no,7,6) regno FROM purchase_order WHERE po_type='$tipe' ORDER BY id DESC LIMIT 0,1"));
	$lyear = mysql_fetch_array(mysql_query("SELECT YEAR(po_date) thn FROM purchase_order WHERE po_type='$tipe' ORDER BY id DESC LIMIT 0,1"));
	$lastno = $lno['regno'];
	$lastyear = $lyear['thn'];
	if(empty($lastno)){
		$seq = 1;
	}else{
		$seq   = (int)$lastno+1;
	}
	if ($seq < 10)          $seq = "00000".$seq;
    elseif($seq < 100)      $seq = "0000".$seq;
    elseif($seq < 1000)     $seq = "000".$seq;
	elseif($seq < 10000)    $seq = "00".$seq;
	elseif($seq < 100000)   $seq = "0".$seq;
    else $seq;
	if($lastyear == $year){
		$regno = "".$tipes."".substr($year,2,2)."".$month."".$seq;
    }else{
		$regno = "".$tipes."".substr($year,2,2)."".$month."000001";
    }
	return $regno;
}
function get_jurnalno(){
	$year   = date('Y');
	$month  = date('m');
	$lno = mysql_fetch_array(mysql_query("SELECT SUBSTR(jurnal_no,7,6) regno FROM jurnal ORDER BY id DESC LIMIT 0,1"));
	$lyear = mysql_fetch_array(mysql_query("SELECT YEAR(jurnal_date) thn FROM jurnal ORDER BY id DESC LIMIT 0,1"));
	$lastno = $lno['regno'];
	$lastyear = $lyear['thn'];
	if(empty($lastno)){
		$seq = 1;
	}else{
		$seq   = (int)$lastno+1;
	}
	if ($seq < 10)          $seq = "00000".$seq;
    elseif($seq < 100)      $seq = "0000".$seq;
    elseif($seq < 1000)     $seq = "000".$seq;
	elseif($seq < 10000)    $seq = "00".$seq;
	elseif($seq < 100000)   $seq = "0".$seq;
    else $seq;
	if($lastyear == $year){
		$regno = "JR".substr($year,2,2)."".$month."".$seq;
    }else{
		$regno = "JR".substr($year,2,2)."".$month."000001";
    }
	return $regno;
}
function get_wo($month,$year){
	#$year   = date('Y');
	#$month  = date('m');
	$lno = mysql_fetch_array(mysql_query("SELECT SUBSTR(wo_no,7,6) regno FROM workorder WHERE MONTH(wo_date)='$month' AND YEAR(wo_date)='$year' ORDER BY SUBSTR(wo_no,7,6) DESC LIMIT 0,1"));
	$lmonth = mysql_fetch_array(mysql_query("SELECT MONTH(wo_date) bln FROM workorder WHERE MONTH(wo_date)='$month' AND YEAR(wo_date)='$year' ORDER BY SUBSTR(wo_no,7,6) DESC LIMIT 0,1"));
	$lastno = $lno['regno'];
	$lastmonth = $lmonth['bln'];
	if(empty($lastno)){
		$seq = 1;
	}else{
		$seq   = (int)$lastno+1;
	}
	if ($seq < 10)          $seq = "00000".$seq;
    elseif($seq < 100)      $seq = "0000".$seq;
    elseif($seq < 1000)     $seq = "000".$seq;
	elseif($seq < 10000)    $seq = "00".$seq;
	elseif($seq < 100000)   $seq = "0".$seq;
    else $seq;
	if(!empty($lno['regno'])){
		$regno = "WO".substr($year,2,2)."".$month."".$seq;
    }else{
		$regno = "WO".substr($year,2,2)."".$month."000001";
    }
	return $regno;
}
function get_kvno($kv_type){
	if($kv_type=="KONVERSI"){ $kv ="KV";}else{$kv="SC";}
	$year   = date('Y');
	$month  = date('m');
	$lno = mysql_fetch_array(mysql_query("SELECT SUBSTR(kv_no,7,6) regno FROM konversi WHERE kv_type='$kv_type' ORDER BY id DESC LIMIT 0,1"));
	$lmonth = mysql_fetch_array(mysql_query("SELECT MONTH(kv_date) bln FROM konversi WHERE kv_type='$kv_type' ORDER BY id DESC LIMIT 0,1"));
	$lastno = $lno['regno'];
	$lastmonth = $lmonth['bln'];
	if(empty($lastno)){
		$seq = 1;
	}else{
		$seq   = (int)$lastno+1;
	}
	if ($seq < 10)          $seq = "00000".$seq;
    elseif($seq < 100)      $seq = "0000".$seq;
    elseif($seq < 1000)     $seq = "000".$seq;
	elseif($seq < 10000)    $seq = "00".$seq;
	elseif($seq < 100000)   $seq = "0".$seq;
    else $seq;
	if($lastmonth == $month){	  
		$regno = $kv."".substr($year,2,2)."".$month."".$seq;
    }else{
		$regno = $kv."".substr($year,2,2)."".$month."000001";
    }
	return $regno;
}
function get_pricewip1($prodid,$start_date2,$end_date2,$inv){
	$tprice=0;
	$q = mysql_query("SELECT stock_prodid prodid, (stock_price/stock_qty) price FROM stock WHERE stock_prodid='$prodid' AND stock_status=10 AND stock_loc=2
		AND stock_date='$start_date2'
		UNION ALL       
		SELECT a.trfd_prodid prodid, a.trfd_price_satuan as price FROM transfer_detailexbc a 
		INNER JOIN transfer b ON b.id = a.trfd_hid
		WHERE b.trf_status=20 AND trf_spbdate BETWEEN '$start_date2' AND '$end_date2' AND a.trfd_prodid='$prodid' AND b.trf_category=1");
	$nums = mysql_num_rows($q);
	while($d=mysql_fetch_array($q)){
		$tprice = $tprice+$d['price']; 	
	}
	if($tprice==0){
		return 0;
	}else{
		return $tprice/$nums;	
	}	
}
function get_pricewip2($prodid,$start_date2,$end_date2,$inv){
	$tprice=0;
	$q = mysql_query("SELECT stock_prodid prodid, (stock_price/stock_qty) price FROM stock WHERE stock_prodid='$prodid' AND stock_status=10 AND stock_loc=2
		AND stock_date='$start_date2'
		UNION ALL       
		SELECT wo_prodid prodid, (wo_price/wo_qty) price  FROM  workorder 
		WHERE wo_status=20 AND wo_date BETWEEN '$start_date2' AND '$end_date2' AND wo_prodid='$prodid'");
	$nums = mysql_num_rows($q);
	while($d=mysql_fetch_array($q)){
		$tprice = $tprice+$d['price']; 	
	}
	if($tprice==0){
		return 0;
	}else{
		return $tprice/$nums;	
	}	
}
function get_pricebj($prodid,$start_date2,$end_date2,$inv){
	$tprice=0;
	$q = mysql_query("SELECT stock_prodid prodid, (stock_price/stock_qty) price FROM stock WHERE stock_prodid='$prodid' AND stock_status=10 AND stock_loc=3
		AND stock_date='$start_date2'
		UNION ALL       
		SELECT a.trfd_prodid prodid, a.trfd_price_satuan as price FROM transfer_detail a 
		INNER JOIN transfer b ON b.id = a.trfd_hid
		WHERE b.trf_status=20 AND trf_spbdate BETWEEN '$start_date2' AND '$end_date2' AND a.trfd_prodid='$prodid' AND b.trf_category=4");
	$nums = mysql_num_rows($q);
	while($d=mysql_fetch_array($q)){
		$tprice = $tprice+$d['price']; 	
	}
	if($tprice==0){
		return 0;
	}else{
		return $tprice/$nums;	
	}	
}
function get_stocktrfbb($prodid,$start_date2,$end_date2,$inv){
	$q=mysql_fetch_array(mysql_query("SELECT b.prodid, (b.inqty-b.outqty) endqty, b.price, c.prod_code, c.prod_name, c.prod_sat FROM
						(SELECT a.prodid, SUM(a.inqty) inqty, SUM(a.outqty) outqty, a.price FROM
						(SELECT stock_prodid prodid, stock_qty inqty, 0 outqty, (stock_price/stock_qty) price FROM stock 
						WHERE stock_status=10 AND stock_date='$start_date2' AND stock_loc=1
						UNION ALL
						SELECT b.incd_prodid prodid, b.incd_qty inqty, 0 outqty, (b.incd_price/b.incd_qty) price FROM incoming a
						INNER JOIN incoming_detail b ON b.incd_hid=a.id
						WHERE a.inch_status=20 AND a.inch_bpbdate BETWEEN '$start_date2' AND '$end_date2'
						UNION ALL
						SELECT a.trfd_prodid prodid, 0 inqty, a.trfd_qty outqty, 0 price FROM transfer_detailexbc a
						INNER JOIN transfer b ON b.id=a.trfd_hid
						WHERE b.trf_status=20 AND a.trfd_status=10 AND b.trf_category=1 AND b.trf_spbdate BETWEEN '$start_date2' AND '$end_date2'
						UNION ALL
						SELECT a.trfd_prodid prodid, a.trfd_qty inqty, 0 outqty, 0 price FROM transfer_detailexbc a
						INNER JOIN transfer b ON b.id=a.trfd_hid
						WHERE b.trf_status=20 AND a.trfd_status=10 AND b.trf_category=3 AND b.trf_spbdate BETWEEN '$start_date2' AND '$end_date2') a
						GROUP BY a.prodid) b INNER JOIN product c ON c.id=b.prodid
						WHERE (b.inqty-b.outqty) > 0 AND b.prodid='$prodid'",$inv));
	if(!empty($q['endqty'])){
		return $q['endqty'];
	}else{
		return 0;
	}
}
function get_supcode(){
	$q=mysql_fetch_array(mysql_query("SELECT SUBSTRING(sup_code,4,6) nomor FROM supplier ORDER BY id DESC LIMIT 1"));
	$seq     = (int)$q['nomor']+1;
	 if ($seq < 10)   $seq = "00000".$seq;
	elseif($seq < 100)  $seq = "0000".$seq;
	elseif($seq < 1000) $seq = "000".$seq;
	elseif($seq < 1000) $seq = "00".$seq;
	elseif($seq < 1000) $seq = "0".$seq;
	else $seq;
	$supcode = "SPL$seq";	
	return $supcode;
}
function get_custcode(){
	$q=mysql_fetch_array(mysql_query("SELECT SUBSTRING(cust_code,4,6) nomor FROM customer ORDER BY id DESC LIMIT 1"));
	$seq     = (int)$q['nomor']+1;
	 if ($seq < 10)   $seq = "00000".$seq;
	elseif($seq < 100)  $seq = "0000".$seq;
	elseif($seq < 1000) $seq = "000".$seq;
	elseif($seq < 1000) $seq = "00".$seq;
	elseif($seq < 1000) $seq = "0".$seq;
	else $seq;
	$supcode = "CST$seq";	
	return $supcode;
}
#Get Month Name
function get_monthname($month){
	switch($month){
        case '1': return "JANUARI"; break;
        case '2': return "FEBRUARI"; break;
        case '3': return "MARET"; break;
        case '4': return "APRIL"; break;
        case '5': return "MEI"; break;
        case '6': return "JUNI"; break;
		case '7': return "JULI"; break;
		case '8': return "AGUSTUS"; break;
		case '9': return "SEPTEMBER"; break;
		case '10': return "OKTOBER"; break;
		case '11': return "NOPEMBER"; break;
		case '12': return "DESEMBER"; break;
    };	
}
function get_autokvno($year,$month){
	//$year   = date('Y');
	//$month  = date('m');
	$lno = mysql_fetch_array(mysql_query("SELECT SUBSTR(kv_no,7,6) regno FROM konversi ORDER BY id DESC LIMIT 0,1"));
	$lmonth = mysql_fetch_array(mysql_query("SELECT MONTH(kv_date) bln FROM konversi ORDER BY id DESC LIMIT 0,1"));
	$lastno = $lno['regno'];
	$lastmonth = $lmonth['bln'];
	if(empty($lastno)){
		$seq = 1;
	}else{
		$seq   = (int)$lastno+1;
	}
	if ($seq < 10)          $seq = "00000".$seq;
    elseif($seq < 100)      $seq = "0000".$seq;
    elseif($seq < 1000)     $seq = "000".$seq;
	elseif($seq < 10000)    $seq = "00".$seq;
	elseif($seq < 100000)   $seq = "0".$seq;
    else $seq;
	if($lastmonth == $month){
		$regno = "KV".substr($year,2,2)."".$month."".$seq;
    }else{
		$regno = "KV".substr($year,2,2)."".$month."000001";
    }
	return $regno;
}
#Start date Acct
function get_startdateacct(){
	$dt = mysql_fetch_array(mysql_query("SELECT p_startdate FROM periode WHERE p_status=20 ORDER BY id DESC LIMIT 0,1"));
	return $dt['p_startdate'];
	/*$tahun = substr(get_date(),0,4);
	$bulan = substr(get_date(),5,2);
	return "$tahun-$bulan-01";*/ 
}
#end date Acct
function get_enddateacct(){
	/*$dt = mysql_fetch_array(mysql_query("SELECT p_enddate FROM periode WHERE p_status=20 ORDER BY id DESC LIMIT 0,1"));
	return $dt['p_enddate'];*/
	$tahun = substr(get_date(),0,4);
	$bulan = substr(get_date(),5,2);
	$hari  = jumlah_hari($bulan,$tahun);
	return "$tahun-$bulan-$hari";
}
function get_reqlastno($cat)
{
	$max_id = mysql_fetch_row(mysql_query("SELECT SUBSTR((trf_reqno),14,4) FROM transfer WHERE trf_category='$cat' ORDER BY id DESC LIMIT 1"));
	$max_id = $max_id[0];
	return $max_id;	
}
#Get Req No
function getreqno($cat){
	$year  = date('Y');
	$month = date('m');
	$seq   = (int)get_reqlastno($cat)+1;
	 if ($seq < 10)   $seq = "000".$seq;
	elseif($seq < 100)  $seq = "00".$seq;
	elseif($seq < 1000) $seq = "0".$seq;
	else $seq;
	$last_month = get_spbmonth($cat);
	$getcode = get_code();
	if($last_month == $month){
		$spb_no = "RNO-".$cat."-$year$month-$seq/$getcode";
	}else{
		$spb_no = "RNO-".$cat."-$year$month-0001/$getcode";
	}	
	return $spb_no;
}
function get_uplreqno($month,$year,$cat){
	#$year  = date('Y');
	#$month = date('m');
	$seq   = (int)get_reqlastno($cat)+1;
	 if ($seq < 10)   $seq = "000".$seq;
	elseif($seq < 100)  $seq = "00".$seq;
	elseif($seq < 1000) $seq = "0".$seq;
	else $seq;
	$last_month = get_spbmonth($cat);
	$getcode = get_code();
	if($last_month == $month){
		$spb_no = "RNO-".$cat."-$year$month-$seq/$getcode";
	}else{
		$spb_no = "RNO-".$cat."-$year$month-0001/$getcode";
	}	
	return $spb_no;
}
function get_nr_lastmonth($fromdate,$todate,$acctno){
	$cek = mysql_fetch_array( mysql_query("SELECT j_bbnrstatus FROM jurnal_master WHERE j_status=10 AND j_acctno='$acctno'"));
	$sa=mysql_fetch_array(mysql_query("SELECT sa_price FROM stock_acct WHERE sa_date='$fromdate' AND sa_status=10 AND sa_acctno='$acctno' ORDER BY id DESC LIMIT 0,1"));	
	if($cek['j_bbnrstatus']=="NO"){
		return $sa['sa_price'];
	}else{
		return $sa['sa_price']*-1;
	}	
}
function get_nr_curmonth($fromdate,$todate,$acctno){
	$qcoa = mysql_query("SELECT j_acctno, j_bbnrstatus, j_rumusbb FROM jurnal_master WHERE j_status=10 AND j_acctno='$acctno' ORDER BY j_acctno ASC");
	while($dcoa=mysql_fetch_array($qcoa)){
		$no=1;
		$saldo =0; $t_amountin=0; $t_amountout=0; $saldo_usd =0; $t_amountin_usd=0; $t_amountout_usd=0;
		$qd=mysql_query("SELECT a.tanggal, a.noreferensi, a.keterangan, a.amountin, a.amountin_usd, a.amountout, a.amountout_usd, a.flag FROM
				(SELECT a.jurnal_date tanggal, a.jurnal_invno noreferensi, b.jurnald_desc keterangan, b.jurnald_amount amountin, b.jurnald_amount_usd amountin_usd, 
				0 amountout, 0 amountout_usd, 1 flag FROM jurnal a INNER JOIN jurnal_detail b ON b.jurnal_no=a.jurnal_no
				WHERE a.jurnal_status=20 AND a.jurnal_date BETWEEN '$fromdate' AND '$todate' AND jurnald_acctno='$dcoa[j_acctno]' AND b.jurnald_status=10		
				UNION ALL
				SELECT a.jurnal_date tanggal, a.jurnal_invno noreferensi, b.jurnald_desc keterangan, 0 amountin, 0 amountin_usd,
				b.jurnald_amount amountout, b.jurnald_amount_usd amountout_usd, 2 flag FROM jurnal a INNER JOIN jurnal_detail b ON b.jurnal_no=a.jurnal_no
				WHERE a.jurnal_status=20 AND a.jurnal_date BETWEEN '$fromdate' AND '$todate' AND jurnald_acctno='$dcoa[j_acctno]' AND b.jurnald_status=20) a
				ORDER BY a.tanggal ASC");
		$num = mysql_num_rows($qd);		
		if($num==0){
			$t_saldo_usd = $saldo_usd;
			$t_saldo     = $saldo;
		}else{		
			while($dd=mysql_fetch_array($qd)){	
				if($dcoa['j_rumusbb']=='A'){	
					$saldo_usd 	= $saldo_usd+$dd['amountin_usd']-$dd['amountout_usd'];
					$saldo 		= $saldo+$dd['amountin']-$dd['amountout'];
				}else{
					$saldo_usd 	= $saldo_usd-$dd['amountin_usd']+$dd['amountout_usd'];
					$saldo 		= $saldo-$dd['amountin']+$dd['amountout'];
				}
				$t_saldo_usd = $saldo_usd;
				$t_saldo	 = $saldo;
				$t_amountin_usd  = $t_amountin_usd+$dd['amountin_usd'];
				$t_amountin  	 = $t_amountin+$dd['amountin'];
				$t_amountout_usd = $t_amountout_usd+$dd['amountout_usd'];
				$t_amountout 	 = $t_amountout+$dd['amountout'];
			}
		}
		if($dcoa['j_bbnrstatus']=="NO"){
			return $t_saldo;
		}else{
			return $t_saldo*-1;
		}	
	}
}
function get_hpp($fromdate,$todate){
	$t_curmonth1 =0;
	$qd1 = mysql_query("SELECT a.j_acctno, a.j_name1 FROM jurnal_master a			
			WHERE a.j_status=10 AND a.j_hpp='Y' AND j_rumus=1 ORDER BY a.j_acctno ASC");
	while($dd1 = mysql_fetch_array($qd1)){
		$curmonth1   = get_nr_curmonth($fromdate,$todate,$dd1['j_acctno']);
		$t_curmonth1 = $t_curmonth1+$curmonth1;	
		/*if($curmonth1<0){$curmonth1=$curmonth1*-1;}else{$curmonth1=$curmonth1;}
		if($t_curmonth1<0){$t_curmonth1=$t_curmonth1*-1;}else{$t_curmonth1=$t_curmonth1;}*/	
    } 
    $t_curmonth2 =0;
	$qd2 = mysql_query("SELECT a.j_acctno, a.j_name1 FROM jurnal_master a			
			WHERE a.j_status=10 AND a.j_hpp='Y' AND j_rumus=2 ORDER BY a.j_acctno ASC");
	while($dd2 = mysql_fetch_array($qd2)){
		$curmonth2   = get_nr_curmonth($fromdate,$todate,$dd2['j_acctno']);
		$t_curmonth2 = $t_curmonth2+$curmonth2;		
		/*if($curmonth2<0){$curmonth2=$curmonth2*-1;}else{$curmonth2=$curmonth2;}
		if($t_curmonth2<0){$t_curmonth2=$t_curmonth2*-1;}else{$t_curmonth2=$t_curmonth2;}*/
    } 	
	$t_curmonth18=0;
	$qd18 = mysql_query("SELECT a.j_acctno, a.j_name1 FROM jurnal_master a			
			WHERE a.j_status=10 AND a.j_hpp='Y' AND j_rumus=18 ORDER BY a.j_acctno ASC");
	while($dd18 = mysql_fetch_array($qd18)){
		$curmonth18    = get_nr_curmonth($fromdate,$todate,$dd18['j_acctno']);
		$t_curmonth18  = $t_curmonth18+$curmonth18;			
	}	
    $t12 =$t_curmonth1+$t_curmonth2+$t_curmonth18; 
    $t_curmonth3 =0;
	$qd3 = mysql_query("SELECT a.j_acctno, a.j_name1 FROM jurnal_master a			
			WHERE a.j_status=10 AND a.j_hpp='Y' AND j_rumus=3 ORDER BY a.j_acctno ASC");
	while($dd3 = mysql_fetch_array($qd3)){
		$curmonth3   = get_nr_curmonth($fromdate,$todate,$dd3['j_acctno']);
		$t_curmonth3 = $t_curmonth3+$curmonth3;		
		/*if($curmonth3<0){$curmonth3=$curmonth3*-1;}else{$curmonth3=$curmonth3;}
		if($t_curmonth3<0){$t_curmonth3=$t_curmonth3*-1;}else{$t_curmonth3=$t_curmonth3;}*/
    } 
    if($t_curmonth3<0){$t123 =$t12-(-$t_curmonth3);}else{$t123 =$t12-$t_curmonth3;} 
    $t_curmonth4 =0;
	$qd4 = mysql_query("SELECT a.j_acctno, a.j_name1 FROM jurnal_master a			
			WHERE a.j_status=10 AND a.j_hpp='Y' AND j_rumus=4 ORDER BY a.j_acctno ASC");
	while($dd4 = mysql_fetch_array($qd4)){
		$curmonth4   = get_nr_curmonth($fromdate,$todate,$dd4['j_acctno']);
		$t_curmonth4 = $t_curmonth4+$curmonth4;	
		/*if($curmonth4<0){$curmonth4=$curmonth4*-1;}else{$curmonth4=$curmonth4;}
		if($t_curmonth4<0){$t_curmonth4=$t_curmonth4*-1;}else{$t_curmonth4=$t_curmonth4;}*/	
    } 
   $t1234 =$t123+$t_curmonth4; 
   return $t1234;
}
function get_hppy($fromdate,$todate){
	$t_lastmonth1=0; $t_curmonth1=0; $t_curyear1=0;
	$qd1 = mysql_query("SELECT a.j_acctno, a.j_name1 FROM jurnal_master a			
			WHERE a.j_status=10 AND a.j_hpp='Y' AND j_rumus=1 ORDER BY a.j_acctno ASC");
	while($dd1 = mysql_fetch_array($qd1)){
		$curmonth1   = get_nr_curmonth($fromdate,$todate,$dd1['j_acctno']);
		$t_curmonth1 = $t_curmonth1+$curmonth1;	
		$lastmonth1   = get_nr_lastmonth($fromdate,$todate,$dd1['j_acctno']);
		$t_lastmonth1 = $t_lastmonth1+$lastmonth1;
		$curyear1     = $lastmonth1+$curmonth1;
		$t_curyear1   = $t_curyear1+$curyear1;
		/*if($curmonth1<0){$curmonth1=$curmonth1*-1;}else{$curmonth1=$curmonth1;}
		if($t_curmonth1<0){$t_curmonth1=$t_curmonth1*-1;}else{$t_curmonth1=$t_curmonth1;}*/	
    } 
    $t_lastmonth2=0; $t_curmonth2=0; $t_curyear2=0;
	$qd2 = mysql_query("SELECT a.j_acctno, a.j_name1 FROM jurnal_master a			
			WHERE a.j_status=10 AND a.j_hpp='Y' AND j_rumus=2 ORDER BY a.j_acctno ASC");
	while($dd2 = mysql_fetch_array($qd2)){
		$curmonth2   = get_nr_curmonth($fromdate,$todate,$dd2['j_acctno']);
		$t_curmonth2 = $t_curmonth2+$curmonth2;		
		$lastmonth2   = get_nr_lastmonth($fromdate,$todate,$dd2['j_acctno']);
		$t_lastmonth2 = $t_lastmonth2+$lastmonth2;
		$curyear2     = $lastmonth2+$curmonth2;
		$t_curyear2   = $t_curyear2+$curyear2;
		/*if($curmonth2<0){$curmonth2=$curmonth2*-1;}else{$curmonth2=$curmonth2;}
		if($t_curmonth2<0){$t_curmonth2=$t_curmonth2*-1;}else{$t_curmonth2=$t_curmonth2;}*/
    } 
	$t_curmonth18=0; $t_lastmonth18=0; $t_curyear18 =0;
	$qd18 = mysql_query("SELECT a.j_acctno, a.j_name1 FROM jurnal_master a			
			WHERE a.j_status=10 AND a.j_hpp='Y' AND j_rumus=18 ORDER BY a.j_acctno ASC");
	while($dd18 = mysql_fetch_array($qd18)){
		$curmonth18    = get_nr_curmonth($fromdate,$todate,$dd18['j_acctno']);
		$t_curmonth18  = $t_curmonth18+$curmonth18;	
		$lastmonth18   = get_nr_lastmonth($fromdate,$todate,$dd18['j_acctno']);
		$t_lastmonth18 = $t_lastmonth18+$lastmonth18;
		$curyear18     = $lastmonth18+$curmonth18;
		$t_curyear18   = $t_curyear18+$curyear18;
	}	
    $t12  =$t_curmonth1+$t_curmonth2; 
  	$t12y =$t_curyear1+$t_curyear2+$t_curyear18;
    $t_lastmonth3=0; $t_curmonth3=0; $t_curyear3=0;
	$qd3 = mysql_query("SELECT a.j_acctno, a.j_name1 FROM jurnal_master a			
			WHERE a.j_status=10 AND a.j_hpp='Y' AND j_rumus=3 ORDER BY a.j_acctno ASC");
	while($dd3 = mysql_fetch_array($qd3)){
		$curmonth3   = get_nr_curmonth($fromdate,$todate,$dd3['j_acctno']);
		$t_curmonth3 = $t_curmonth3+$curmonth3;		
		$lastmonth3   = get_nr_lastmonth($fromdate,$todate,$dd3['j_acctno']);
		$t_lastmonth3 = $t_lastmonth3+$lastmonth3;
		$curyear3     = $lastmonth3+$curmonth3;
		$t_curyear3   = $t_curyear3+$curyear3;
		/*if($curmonth3<0){$curmonth3=$curmonth3*-1;}else{$curmonth3=$curmonth3;}
		if($t_curmonth3<0){$t_curmonth3=$t_curmonth3*-1;}else{$t_curmonth3=$t_curmonth3;}*/
    } 
    if($t_curmonth3<0){$t123 =$t12-(-$t_curmonth3);}else{$t123 =$t12-$t_curmonth3;}
	if($t_curyear3<0){$t123y=$t12y-(-$t_curyear3);}else{$t123y=$t12y-$t_curyear3;}
    $t_lastmonth4=0; $t_curmonth4=0; $t_curyear4=0;
	$qd4 = mysql_query("SELECT a.j_acctno, a.j_name1 FROM jurnal_master a			
			WHERE a.j_status=10 AND a.j_hpp='Y' AND j_rumus=4 ORDER BY a.j_acctno ASC");
	while($dd4 = mysql_fetch_array($qd4)){
		$curmonth4   = get_nr_curmonth($fromdate,$todate,$dd4['j_acctno']);
		$t_curmonth4 = $t_curmonth4+$curmonth4;	
		$lastmonth4   = get_nr_lastmonth($fromdate,$todate,$dd4['j_acctno']);
		$t_lastmonth4 = $t_lastmonth4+$lastmonth4;
		$curyear4     = $lastmonth4+$curmonth4;
		$t_curyear4   = $t_curyear4+$curyear4;
		/*if($curmonth4<0){$curmonth4=$curmonth4*-1;}else{$curmonth4=$curmonth4;}
		if($t_curmonth4<0){$t_curmonth4=$t_curmonth4*-1;}else{$t_curmonth4=$t_curmonth4;}*/	
    } 
   $t1234 =$t123+$t_curmonth4; 
   $t1234y=$t123y+$t_curyear4;
   return $t1234y;
}
function get_labarugi($fromdate,$todate){
	$t_curmonth1 =0;
	$qd1 = mysql_query("SELECT a.j_acctno, a.j_name1 FROM jurnal_master a			
			WHERE a.j_status=10 AND a.j_lr='Y' AND j_rumus=5 ORDER BY a.j_acctno ASC");
	while($dd1 = mysql_fetch_array($qd1)){
		$curmonth1   = get_nr_curmonth($fromdate,$todate,$dd1['j_acctno']);
		$t_curmonth1 = $t_curmonth1+$curmonth1;
    } 
    $t_curmonth2 =0;
	$qd2 = mysql_query("SELECT a.j_acctno, a.j_name1 FROM jurnal_master a			
			WHERE a.j_status=10 AND a.j_lr='Y' AND j_rumus=6 ORDER BY a.j_acctno ASC");
	while($dd2 = mysql_fetch_array($qd2)){
		$curmonth2   = get_nr_curmonth($fromdate,$todate,$dd2['j_acctno']);
		$t_curmonth2 = $t_curmonth2+$curmonth2;
		
    } 
    $thpp=get_hpp($fromdate,$todate); 
    $t1 = $thpp + $t_curmonth2; 
    $t_curmonth3 =0;
	$qd3 = mysql_query("SELECT a.j_acctno, a.j_name1 FROM jurnal_master a			
			WHERE a.j_status=10 AND a.j_lr='Y' AND j_rumus=7 ORDER BY a.j_acctno ASC");
	while($dd3 = mysql_fetch_array($qd3)){
		$curmonth3   = get_nr_curmonth($fromdate,$todate,$dd3['j_acctno']);
		$t_curmonth3 = $t_curmonth3+$curmonth3;
		#if($curmonth3<0){$curmonth3=$curmonth3*-1;}else{$curmonth3=$curmonth3;}
		#if($t_curmonth3<0){$t_curmonth3=$t_curmonth3*-1;}else{$t_curmonth3=$t_curmonth3;}
    } 
	#if($t_curmonth3<0){$t_curmonth3=$t_curmonth3*-1;}else{$t_curmonth3=$t_curmonth3;}
	$t2 = $t1 - $t_curmonth3; 
	$t3 = $t_curmonth1 - $t2; 
	$t_curmonth4 =0;
	$qd4 = mysql_query("SELECT a.j_acctno, a.j_name1 FROM jurnal_master a			
			WHERE a.j_status=10 AND a.j_lr='Y' AND j_rumus=8 ORDER BY a.j_acctno ASC");
	while($dd4 = mysql_fetch_array($qd4)){
		$curmonth4   = get_nr_curmonth($fromdate,$todate,$dd4['j_acctno']);
		$t_curmonth4 = $t_curmonth4+$curmonth4;
		#if($curmonth4<0){$curmonth4=$curmonth4*-1;}else{$curmonth4=$curmonth4;}
		#if($t_curmonth4<0){$t_curmonth4=$t_curmonth4*-1;}else{$t_curmonth4=$t_curmonth4;}		
    }
	#if($t_curmonth4<0){$t_curmonth4=$t_curmonth4*-1;}else{$t_curmonth4=$t_curmonth4;}	
	$t4 = $t3 - $t_curmonth4; 
	$t_curmonth5 =0;
	$qd5 = mysql_query("SELECT a.j_acctno, a.j_name1 FROM jurnal_master a			
			WHERE a.j_status=10 AND a.j_lr='Y' AND j_rumus=9 ORDER BY a.j_acctno ASC");
	while($dd5 = mysql_fetch_array($qd5)){
		$curmonth5   = get_nr_curmonth($fromdate,$todate,$dd5['j_acctno']);
		$t_curmonth5 = $t_curmonth5+$curmonth5;
		#if($curmonth5<0){$curmonth5=$curmonth5*-1;}else{$curmonth5=$curmonth5;}
		#if($t_curmonth5<0){$t_curmonth5=$t_curmonth5*-1;}else{$t_curmonth5=$t_curmonth5;}
    } 
	#if($t_curmonth5<0){$t_curmonth5=$t_curmonth5*-1;}else{$t_curmonth5=$t_curmonth5;}
	$t_curmonth6 =0;
	$qd6 = mysql_query("SELECT a.j_acctno, a.j_name1 FROM jurnal_master a			
			WHERE a.j_status=10 AND a.j_lr='Y' AND j_rumus=10 ORDER BY a.j_acctno ASC");
	while($dd6 = mysql_fetch_array($qd6)){
		$curmonth6   = get_nr_curmonth($fromdate,$todate,$dd6['j_acctno']);
		$curmonth6   = $curmonth6*-1;
		$t_curmonth6 = $t_curmonth6+$curmonth6;
		#if($curmonth6<0){$curmonth6=$curmonth6*-1;}else{$curmonth6=$curmonth6;}
		#if($t_curmonth6<0){$t_curmonth6=$t_curmonth6*-1;}else{$t_curmonth6=$t_curmonth6;}
    } 
	if($t_curmonth6<0){$t5 = $t_curmonth5 - (-$t_curmonth6);}else{$t5 = $t_curmonth5 - $t_curmonth6;} 
	$t6 = $t4 + $t5;
	
	$t_lastmonth19=0; $t_curmonth19=0; $t_curyear19=0;
	$qd19 = mysql_query("SELECT a.j_acctno, a.j_name1 FROM jurnal_master a			
			WHERE a.j_status=10 AND a.j_lr='Y' AND j_rumus=19 ORDER BY a.j_acctno ASC");
	while($dd19 = mysql_fetch_array($qd19)){
		$curmonth19    = get_nr_curmonth($fromdate,$todate,$dd19['j_acctno']);		
		$t_curmonth19  = $t_curmonth19+$curmonth19;
		$lastmonth19   = get_nr_lastmonth($fromdate,$todate,$dd19['j_acctno']);		
		$t_lastmonth19 = $t_lastmonth19+$lastmonth19;
		$curyear19     = $lastmonth19+$curmonth19;
		$t_curyear19   = $t_curyear19+$curyear19;
	}		
	$t8 =$t6 - $t_curmonth19; 
	return $t8;
}
function insert_history($user_id,$date,$time,$activity){
	mysql_query("INSERT INTO user_activity SET user_id='$user_id', user_act='$activity', user_date='$date', user_time='$time'");	
}
function get_valpo_upl($po_no,$inv){
	$html ="";
	$qr=mysql_query("SELECT * FROM purchase_order_temp WHERE po_no='$po_no' AND pod_status=20",$inv);
    while($d=mysql_fetch_array($qr)){
		#kode supplier
		$vsup = mysql_fetch_array(mysql_query("SELECT id FROM supplier WHERE sup_code='$d[po_suppcode]' AND sup_status<>'100'",$inv));
		if(empty($vsup['id'])){
			$colorvsup ="#FF0000";
		}else{
			$colorvsup ="#000000";
		}
		#tanggal PO	
		$vdate = get_validationdate2($d['po_date'],$inv);
		if($vdate=="NO"){
			$colorvdate ="#FF0000";
		}else{
			$colorvdate ="#000000";
		}
		#tipe pembayaran	
		$vpay = mysql_fetch_array(mysql_query("select id from buku_besar_tipe where bbt_status=20 and bbt_tipe='$d[po_paytype]'",$inv));
		if(empty($vpay['id'])){
			$colorvpay ="#FF0000";
		}else{
			$colorvpay ="#000000";
		}
		#kode barang	
		$vprod = mysql_fetch_array(mysql_query("SELECT id FROM product WHERE prod_code='$d[po_prodcode]' AND prod_cat<>'100'",$inv));
		if(empty($vprod['id'])){
			$colorvprod ="#FF0000";
		}else{
			$colorvprod ="#000000";
		}
		if((empty($vsup['id'])) || ($vdate=="NO") || (empty($vpay['id'])) || (empty($vprod['id'])) ){
			$html = $html."".$d['po_no']."XX";
		}
	}
	$htm = substr($html, 0,-2);	
	if($htm==""){$status = "OK";}
	else{$status = "NO";}	
	return $status;			
}
function get_valpos_upl($po_no,$inv){
	$html ="";
	$qr=mysql_query("SELECT * FROM purchase_order_temp WHERE po_no='$po_no' AND pod_status=30",$inv);
    while($d=mysql_fetch_array($qr)){
		#kode supplier
		$vsup = mysql_fetch_array(mysql_query("SELECT id FROM customer WHERE cust_code='$d[po_suppcode]' AND cust_status<>'100'",$inv));
		if(empty($vsup['id'])){
			$colorvsup ="#FF0000";
		}else{
			$colorvsup ="#000000";
		}
		#tanggal PO	
		$vdate = get_validationdate2($d['po_date'],$inv);
		if($vdate=="NO"){
			$colorvdate ="#FF0000";
		}else{
			$colorvdate ="#000000";
		}
		#tipe pembayaran	
		$vpay = mysql_fetch_array(mysql_query("select id from buku_besar_tipe where bbt_status=20 and bbt_tipe='$d[po_paytype]'",$inv));
		if(empty($vpay['id'])){
			$colorvpay ="#FF0000";
		}else{
			$colorvpay ="#000000";
		}
		#kode barang	
		$vprod = mysql_fetch_array(mysql_query("SELECT id FROM product WHERE prod_code='$d[po_prodcode]' AND prod_cat<>'100'",$inv));
		if(empty($vprod['id'])){
			$colorvprod ="#FF0000";
		}else{
			$colorvprod ="#000000";
		}
		if((empty($vsup['id'])) || ($vdate=="NO") || (empty($vpay['id'])) || (empty($vprod['id'])) ){
			$html = $html."".$d['po_no']."XX";
		}
	}
	$htm = substr($html, 0,-2);	
	if($htm==""){$status = "OK";}
	else{$status = "NO";}	
	return $status;			
}
function get_stock_produksi($prod_code,$fromdate,$todate,$inv){
	$stock=0;
	$start_date_m2year = start_date_m2year($fromdate);
	$qd = mysql_query("SELECT a.id, a.prod_code, a.prod_name, a.prod_sat, a.prod_cat, b.stock_qty, c.trfgd_qty, d.rtngd_qty, e.wobb_qty, f.wobj_qty, g.fgd_qty, h.trfqc_qty, i.rtnqc_qty,
							  j.scr_qty, k.kvp_qty, l.kvm_qty, m.wobbbj_qty, n.kvp2_qty, o.kvm2_qty
						FROM product a
						LEFT JOIN (SELECT stock_prodid, SUM(stock_qty) stock_qty FROM stock WHERE stock_date='$fromdate' AND stock_status=10 
										   AND stock_loc=2 GROUP BY stock_prodid) as b ON b.stock_prodid=a.id
						LEFT JOIN (SELECT a.trfd_prodid, SUM(a.trfd_qty) trfgd_qty FROM transfer_detailexbc a INNER JOIN transfer b ON b.id=a.trfd_hid
									   WHERE b.trf_spbdate BETWEEN '$fromdate' AND '$todate' AND b.trf_status=20 AND b.trf_category=1 GROUP BY a.trfd_prodid) as c ON c.trfd_prodid=a.id
						LEFT JOIN (SELECT a.trfd_prodid, SUM(a.trfd_qty) rtngd_qty FROM transfer_detailexbc a INNER JOIN transfer b ON b.id=a.trfd_hid
									   WHERE b.trf_spbdate BETWEEN '$fromdate' AND '$todate' AND b.trf_status=20 AND b.trf_category=3 GROUP BY a.trfd_prodid) as d ON d.trfd_prodid=a.id  
						LEFT JOIN (SELECT a.wod_prodid, SUM(a.wod_qty) wobb_qty FROM workorder_detail a INNER JOIN workorder b ON b.id=a.wod_hid
									   WHERE b.wo_date BETWEEN '$fromdate' AND '$todate' AND b.wo_status=20 AND b.wo_level<>'Level FG' GROUP BY a.wod_prodid) as e ON e.wod_prodid=a.id           
						LEFT JOIN (SELECT wo_prodid, SUM(wo_qty) wobj_qty FROM workorder WHERE wo_date BETWEEN '$fromdate' AND '$todate'
								   AND wo_status=20 AND wo_level<>'Level FG' GROUP BY wo_prodid) as f ON f.wo_prodid=a.id           
						LEFT JOIN (SELECT c.id, SUM(b.fgd_qty) fgd_qty FROM finishgoods a INNER JOIN finishgoods_detail b ON b.fgd_hid=a.id
										   INNER JOIN product c ON c.id=b.fgd_prodid WHERE a.fgh_status=20 AND a.fgh_lpdate BETWEEN '$fromdate' AND '$todate' GROUP BY c.id) as g ON g.id=a.id
						LEFT JOIN (SELECT a.trfd_prodid, SUM(a.trfd_qty) trfqc_qty FROM transfer_detail a INNER JOIN transfer b ON b.id=a.trfd_hid
									   WHERE b.trf_spbdate BETWEEN '$fromdate' AND '$todate' AND b.trf_status=20 AND b.trf_category=4 GROUP BY a.trfd_prodid) as h ON h.trfd_prodid=a.id
						LEFT JOIN (SELECT a.trfd_prodid, SUM(a.trfd_qty) rtnqc_qty FROM transfer_detail a INNER JOIN transfer b ON b.id=a.trfd_hid
									   WHERE b.trf_spbdate BETWEEN '$fromdate' AND '$todate' AND b.trf_status=20 AND b.trf_category=6 GROUP BY a.trfd_prodid) as i ON i.trfd_prodid=a.id
						LEFT JOIN (SELECT c.id, SUM(b.scrd_qty) scr_qty FROM scrap a INNER JOIN scrap_detail b ON b.scrd_hid=a.id
										   INNER JOIN product c ON c.id=b.scrd_prodid WHERE a.scrh_status=20 AND a.scrh_lpdate BETWEEN '$fromdate' AND '$todate' GROUP BY c.id) as j ON j.id=a.id
						LEFT JOIN (SELECT kv_prodid, SUM(kv_qty) kvp_qty FROM konversi WHERE kv_status=20 AND kv_date BETWEEN '$fromdate' AND '$todate' GROUP BY kv_prodid) as k ON k.kv_prodid=a.id
						LEFT JOIN (SELECT a.kvd_prodid, SUM(a.kvd_qty) kvm_qty FROM konversi_detail a INNER JOIN konversi b ON b.id=a.kvd_hid 
										 WHERE b.kv_status=20 AND b.kv_date BETWEEN '$fromdate' AND '$todate'	GROUP BY a.kvd_prodid) as l ON l.kvd_prodid=a.id
						LEFT JOIN (SELECT a.wod_prodid, SUM(a.wod_qty) wobbbj_qty FROM workorder_detail a INNER JOIN workorder b ON b.id=a.wod_hid
										 WHERE b.wo_date BETWEEN '$fromdate' AND '$todate' AND b.wo_status=20 AND b.wo_level='Level FG' GROUP BY a.wod_prodid) as m ON m.wod_prodid=a.id
						LEFT JOIN (SELECT kv_prodid, SUM(kv_qty) kvp2_qty FROM konversi2 WHERE kv_status=20 AND kv_date BETWEEN '$fromdate' AND '$todate' GROUP BY kv_prodid) as n ON n.kv_prodid=a.id
						LEFT JOIN (SELECT a.kvd_prodid, SUM(a.kvd_qty) kvm2_qty FROM konversi_detail2 a INNER JOIN konversi2 b ON b.id=a.kvd_hid 
										   WHERE b.kv_status=20 AND b.kv_date BETWEEN '$fromdate' AND '$todate'	GROUP BY a.kvd_prodid) as o ON o.kvd_prodid=a.id                              
						WHERE a.prod_code='$prod_code' AND (b.stock_qty > 0 OR c.trfgd_qty > 0 OR d.rtngd_qty > 0 OR e.wobb_qty > 0 OR f.wobj_qty > 0 OR g.fgd_qty > 0 OR h.trfqc_qty > 0 OR i.rtnqc_qty > 0 OR j.scr_qty > 0 OR
							  k.kvp_qty > 0 OR l.kvm_qty > 0 OR m.wobbbj_qty > 0 OR n.kvp2_qty > 0 OR o.kvm2_qty > 0) 
						ORDER BY a.prod_code ASC");
	while($dd=mysql_fetch_array($qd)){		
		#Summary	   
		if($dd['prod_cat']==1 || $dd['prod_cat']==6 || $dd['prod_cat']==7 ){			
			$stk    = $dd['stock_qty']; 
			$in     = $dd['trfgd_qty']+$dd['kvp_qty']+$dd['kvm2_qty']; 
			$out    = $dd['rtngd_qty']+$dd['wobb_qty']+$dd['kvm_qty']+$dd['wobbbj_qty']+$dd['kvp2_qty'];	
			$stock  = ($stk+$in)-$out;
			$price  = get_unitprice_bb($start_date_m2year,$todate,$dd['id']); 
			$tprice = $stock*$price;
		}elseif($dd['prod_cat']==3){
			$stk    = $dd['stock_qty'];
			$in     = $dd['trfgd_qty']+$dd['wobj_qty']+$dd['rtnqc_qty']+$dd['fgd_qty']+$dd['kvp_qty']+$dd['kvm2_qty'];
			$out    = $dd['trfqc_qty']+$dd['wobb_qty']+$dd['kvm_qty']+$dd['wobbbj_qty']+$dd['kvp2_qty'];	
			$stock  = ($stk+$in)-$out;
			$price  = get_unitprice_bj($start_date_m2year,$todate,$dd['id']); 
			$tprice = $stock*$price;
		}elseif($dd['prod_cat']==5){
			$stk    = $dd['stock_qty'];
			$in     = $dd['trfgd_qty']+$dd['wobj_qty']+$dd['rtnqc_qty']+$dd['kvp_qty']+$dd['kvm2_qty'];
			$out    = $dd['trfqc_qty']+$dd['wobb_qty']+$dd['kvm_qty']+$dd['wobbbj_qty']+$dd['kvp2_qty'];	
			$stock  = ($stk+$in)-$out;
			$price  = get_unitprice_wip($start_date_m2year,$todate,$dd['id']); 
			$tprice = $stock*$price;
		}else{
			$stk    = $dd['stock_qty'];
			$in     = $dd['scr_qty']+$dd['wobj_qty']+$dd['rtnqc_qty']+$dd['kvp_qty']+$dd['kvm2_qty'];
			$out    = $dd['trfqc_qty']+$dd['kvm_qty']+$dd['kvp2_qty'];	
			$stock  = ($stk+$in)-$out;
			$price  = 0;
			$tprice = $stock*$price;
		}
	}
	if($stock > 0){ return $stock; }else{return 0;}
}
function get_stock_produksi_hansae($prod_code,$fromdate,$todate,$inv){
	$stock=0;
	$start_date_m2year = start_date_m2year($fromdate);
	$qd = mysql_query("SELECT id,prod_cat FROM product WHERE prod_code='$prod_code' ORDER BY prod_code ASC");
	while($dd=mysql_fetch_array($qd)){		
		#Bahan Baku & penolong
		$stk = mysql_fetch_array(mysql_query("SELECT stock_prodid, SUM(stock_qty) qty, SUM(stock_price) price FROM stock WHERE stock_date='$fromdate' AND stock_status=10 AND stock_loc=2 AND stock_prodid='$dd[id]' 
			   GROUP BY stock_prodid"));
		#Transfer BB
		$trf = mysql_fetch_array(mysql_query("SELECT a.trfd_prodid, SUM(a.trfd_qty) qty, SUM(trfd_price) price FROM transfer_detailexbc a INNER JOIN transfer b ON b.id=a.trfd_hid
		       WHERE b.trf_spbdate BETWEEN '$fromdate' AND '$todate' AND b.trf_status=20 AND b.trf_category=1 AND a.trfd_prodid='$dd[id]' GROUP BY a.trfd_prodid"));
		#Return BB
		$rtn = mysql_fetch_array(mysql_query("SELECT a.trfd_prodid, SUM(a.trfd_qty) qty FROM transfer_detailexbc a INNER JOIN transfer b ON b.id=a.trfd_hid
		       WHERE b.trf_spbdate BETWEEN '$fromdate' AND '$todate' AND b.trf_status=20 AND b.trf_category=3 AND a.trfd_prodid='$dd[id]' GROUP BY a.trfd_prodid"));
		#Work Order BB
		$wo = mysql_fetch_array(mysql_query("SELECT a.wod_prodid, SUM(a.wod_qty) qty FROM workorder_detail a INNER JOIN workorder b ON b.id=a.wod_hid
		       WHERE b.wo_date BETWEEN '$fromdate' AND '$todate' AND b.wo_status=20 AND a.wod_prodid='$dd[id]' AND b.wo_level<>'Level FG' GROUP BY a.wod_prodid"));	
		#Barang Jadi & Scrap
		#Work Order BJ BOM
		$wprod = mysql_fetch_array(mysql_query("SELECT wo_prodid, SUM(wo_qty) qty, SUM(wo_price) price FROM workorder WHERE wo_date BETWEEN '$fromdate' AND '$todate' 
			   AND wo_status=20 AND wo_prodid='$dd[id]' AND wo_level<>'Level FG' GROUP BY wo_prodid"));	
		#Work Order BJ Manual
		$mlprod = mysql_fetch_array(mysql_query("SELECT c.id, SUM(b.fgd_qty) qty FROM finishgoods a INNER JOIN finishgoods_detail b ON b.fgd_hid=a.id
		INNER JOIN product c ON c.id=b.fgd_prodid WHERE a.fgh_status=20 AND a.fgh_lpdate BETWEEN '$fromdate' AND '$todate' AND b.fgd_prodid='$dd[id]'
		GROUP BY c.id"));	
		#Transfer BJ
		$trfprod = mysql_fetch_array(mysql_query("SELECT a.trfd_prodid, SUM(a.trfd_qty) qty FROM transfer_detail a INNER JOIN transfer b ON b.id=a.trfd_hid
		       WHERE b.trf_spbdate BETWEEN '$fromdate' AND '$todate' AND b.trf_status=20 AND b.trf_category=4 AND a.trfd_prodid='$dd[id]' GROUP BY a.trfd_prodid"));
		#Return BJ
		$rtnprod = mysql_fetch_array(mysql_query("SELECT a.trfd_prodid, SUM(a.trfd_qty) qty FROM transfer_detail a INNER JOIN transfer b ON b.id=a.trfd_hid
		       WHERE b.trf_spbdate BETWEEN '$fromdate' AND '$todate' AND b.trf_status=20 AND b.trf_category=6 AND a.trfd_prodid='$dd[id]' GROUP BY a.trfd_prodid"));      	   
		#Laporan Scrap
		$scrp = mysql_fetch_array(mysql_query("SELECT c.id, SUM(b.scrd_qty) qty FROM scrap a INNER JOIN scrap_detail b ON b.scrd_hid=a.id
		INNER JOIN product c ON c.id=b.scrd_prodid WHERE a.scrh_status=20 AND a.scrh_lpdate BETWEEN '$fromdate' AND '$todate' AND b.scrd_prodid='$dd[id]'
		GROUP BY c.id"));
		#Konversi +
		$kvp = mysql_fetch_array(mysql_query("SELECT kv_prodid, SUM(kv_qty) qty FROM konversi WHERE kv_status=20 AND kv_date BETWEEN '$fromdate' AND '$todate' 
		AND kv_prodid='$dd[id]'	GROUP BY kv_prodid"));
		#Konversi -
		$kvm = mysql_fetch_array(mysql_query("SELECT a.kvd_prodid, SUM(a.kvd_qty) qty FROM konversi_detail a INNER JOIN konversi b ON b.id=a.kvd_hid 
		WHERE b.kv_status=20 AND b.kv_date BETWEEN '$fromdate' AND '$todate' 	AND a.kvd_prodid='$dd[id]'	GROUP BY a.kvd_prodid"));
		#Work Order BB-FG
		$wofg = mysql_fetch_array(mysql_query("SELECT a.wod_prodid, SUM(a.wod_qty) qty FROM workorder_detail a INNER JOIN workorder b ON b.id=a.wod_hid
			   WHERE b.wo_date BETWEEN '$fromdate' AND '$todate' AND b.wo_status=20 AND a.wod_prodid='$dd[id]' AND b.wo_level='Level FG' 
			   GROUP BY a.wod_prodid"));
		#khusus hansae (masuk dari incoming)
		$inc = mysql_fetch_array(mysql_query("SELECT b.incd_prodid, SUM(b.incd_qty) qty FROM incoming a INNER JOIN incoming_detail b ON b.incd_hid=a.id
			   WHERE a.inch_status=20 AND b.incd_prodid='$dd[id]' AND a.inch_docdate BETWEEN '$fromdate' AND '$todate' GROUP BY b.incd_prodid"));
		#Summary	   
		if($dd['prod_cat']==1 || $dd['prod_cat']==6 ){
			$stk    = $stk['qty'];
			$in     = $trf['qty']+$kvp['qty'];
			$out    = $rtn['qty']+$wo['qty']+$kvm['qty']+$wofg['qty'];	
			$stock  = ($stk+$in)-$out;
			$price  = get_unitprice_bb($start_date_m2year,$todate,$dd['id']); #get_pricewip1($dd['id'],$fromdate,$todate,$inv);
			$tprice = $stock*$price;
		}elseif($dd['prod_cat']==3){
			$stk    = $stk['qty'];
			$in     = $trf['qty']+$wprod['qty']+$rtnprod['qty']+$mlprod['qty']+$kvp['qty'];
			$out    = $trfprod['qty']+$wo['qty']+$kvm['qty']+$wofg['qty'];	
			$stock  = ($stk+$in)-$out;
			$price  = get_pricewip2($dd['id'],$fromdate,$todate,$inv);
			$tprice = $stock*$price;
		}elseif($dd['prod_cat']==5){
			$stk    = $stk['qty'];
			$in     = $inc['qty']+$trf['qty']+$wprod['qty']+$rtnprod['qty']+$kvp['qty'];
			$out    = $trfprod['qty']+$wo['qty']+$kvm['qty']+$wofg['qty'];	
			$stock  = ($stk+$in)-$out;
			$price  = get_pricewip2($dd['id'],$fromdate,$todate,$inv);
			$tprice = $stock*$price;
		}else{
			$stk    = $stk['qty'];
			$in     = $scrp['qty']+$rtnprod['qty']+$kvp['qty'];
			$out    = $trfprod['qty']+$kvm['qty'];	
			$stock  = ($stk+$in)-$out;
			$price  = 0;
			$tprice = $stock*$price;
		}
		
	}
	if($stock > 0){ return $stock; }else{return 0;}
}
function get_valwo_upl($wo_no,$start_date2,$end_date2,$inv){
	$html ="";
	$qr=mysql_query("SELECT * FROM workorder_temp WHERE wo_no='$wo_no' AND wod_status=20",$inv);
    while($d=mysql_fetch_array($qr)){
		$vdate = get_validationdate2($d['wo_date'],$inv);
		if($vdate=="NO"){
			$colorvdate ="#FF0000";
		}else{
			$colorvdate ="#000000";
		}
		#kode barang jadi	
		$vprod1 = mysql_fetch_array(mysql_query("SELECT id FROM product WHERE prod_code='$d[wo_prodcode]' AND prod_cat<>'100'",$inv));
		if(empty($vprod1['id'])){
			$colorvprod1 ="#FF0000";
		}else{
			$colorvprod1 ="#000000";
		}	
		#kode bahan baku
		$vprod2 = mysql_fetch_array(mysql_query("SELECT id FROM product WHERE prod_code='$d[wod_prodcode]' AND prod_cat<>'100'",$inv));
		if(empty($vprod2['id'])){
			$colorvprod2 ="#FF0000";
		}else{
			$colorvprod2 ="#000000";
		}
		$stock = get_stock_produksi($d['wod_prodcode'],$start_date2,$end_date2,$inv);
		if($stock < $d['wod_qty']){
			$colorvstock ="#FF0000";
		}else{
			$colorvstock ="#000000";
		}
		if(($vdate=="NO") || (empty($vprod1['id'])) || (empty($vprod2['id'])) || ($stock < $d['wod_qty']) ){
			$html = $html."".$d['wo_no']."XX";
		}
	}
	$htm = substr($html, 0,-2);	
	if($htm==""){$status = "OK";}
	else{$status = "NO";}	
	return $status;			
}
function get_tahunacct(){
	$q = mysql_fetch_array(mysql_query("SELECT p_tahun FROM periode WHERE p_status=90"));
	return $q['p_tahun'];
}

function get_valjurnal_upl($jurnal_no,$inv){
	$html ="";
	$qr=mysql_query("SELECT * FROM jurnal_temp WHERE jurnal_no='$jurnal_no' AND jurnald_status=50",$inv);
    while($d=mysql_fetch_array($qr)){
		#jurnal tipe	
		if(strtoupper($d['jurnal_type']) !="AR" && strtoupper($d['jurnal_type']) !="AP" && strtoupper($d['jurnal_type']) !="OT" &&
		   strtoupper($d['jurnal_type']) !="PT" && strtoupper($d['jurnal_type']) !="HT"){
			$colorvtype ="#FF0000";
		}else{
			$colorvtype ="#000000";
		}	
		#sup/cust
		$vsup = mysql_fetch_array(mysql_query("SELECT a.id, a.kode, a.nama, snpwp, CONCAT_WS(' - ', a.kode, a.nama) codename, a.tipe FROM
			(SELECT id, sup_code kode, sup_name nama, sup_npwp snpwp, 'SUP' tipe FROM supplier WHERE sup_status=10
			UNION
			SELECT id, cust_code kode, cust_name nama, cust_npwp snpwp, 'CUS' tipe FROM customer WHERE cust_status=10) a
			WHERE a.kode='$d[jurnal_suppcode]' ORDER BY a.nama ASC ",$inv));
		if(empty($vsup['id'])){
			$colorvsup ="#FF0000";
		}else{
			$colorvsup ="#000000";
		}	
		#paytype	
		if(strtoupper($d['jurnal_ptype']) !="KAS" && strtoupper($d['jurnal_ptype']) !="BANK" && strtoupper($d['jurnal_ptype']) !="HUTANG" && 
		   strtoupper($d['jurnal_ptype']) !="PIUTANG" && strtoupper($d['jurnal_ptype']) !="LAIN-LAIN"){
			$colorvptype ="#FF0000";
		}else{
			$colorvptype ="#000000";
		}	
		#pos data	
		if(strtoupper($d['jurnal_post']) !="DEBIT" && strtoupper($d['jurnal_post']) !="KREDIT"){
			$colorvpos ="#FF0000";
		}else{
			$colorvpos ="#000000";
		}
		#jurnal account
		$vacctno = mysql_fetch_array(mysql_query("SELECT j_acctno FROM jurnal_master WHERE j_acctno='$d[jurnald_acctno]' ORDER BY j_acctno ASC",$inv));
		if(empty($vacctno['j_acctno'])){
			$colorvacctno ="#FF0000";
		}else{
			$colorvacctno ="#000000";
		}
		#jurnal no
		$vjrlno = mysql_fetch_array(mysql_query("SELECT jurnal_no FROM jurnal WHERE jurnal_no='$jurnal_no'",$inv));
		if(!empty($vjrlno['jurnal_no'])){
			$colorvjrlno ="#FF0000";
		}else{
			$colorvjrlno ="#000000";
		}
		#periode aktif
		if(substr($d['jurnal_date'],0,4) != get_tahunacct()){
			$colorvjrldt ="#FF0000";
		}else{
			$colorvjrldt ="#000000";
		}
		if($colorvtype =="#FF0000" || $colorvsup =="#FF0000" || $colorvptype =="#FF0000" || $colorvpos =="#FF0000" || $colorvacctno =="#FF0000" || $colorvjrlno =="#FF0000" || $colorvjrldt =="#FF0000"){
			$html = $html."".$d['jurnal_no']."XX";
		}
	}
	$htm = substr($html, 0,-2);	
	if($htm==""){$status = "OK";}
	else{$status = "NO";}	
	return $status;			
}
#Get Unit Price Bahan baku & penolong
function get_unitprice_bbinc($sdate,$edate,$prodid,$docdate,$docno,$doctype){
	/*$q = mysql_fetch_array(mysql_query("SELECT CASE WHEN a.incd_currency='IDR' THEN (a.incd_price/a.incd_qty) ELSE (a.incd_price_usd/a.incd_qty) END AS unit_price
		 FROM incoming_detail a INNER JOIN incoming b ON b.id=a.incd_hid
		 WHERE b.inch_docdate BETWEEN '$sdate' AND '$edate' AND b.inch_status=20 AND a.incd_prodid=$prodid 
		 AND ((a.incd_qty>0 AND a.incd_price>0) OR (a.incd_qty>0 AND a.incd_price_usd>0)) ORDER BY a.id DESC LIMIT 0,1"));
	if(empty($q['unit_price'])){
		$qq = mysql_fetch_array(mysql_query("SELECT stock_price FROM stock WHERE stock_date BETWEEN '$sdate' AND '$edate' AND stock_loc=1 AND stock_prodid=$prodid 
			  AND stock_price>0 ORDER BY id DESC LIMIT 0,1"));
		return $qq['stock_price'];
	}else{
		return $q['unit_price'];
	}*/
	$q = mysql_fetch_array(mysql_query("SELECT CASE WHEN a.incd_currency='IDR' THEN (a.incd_price/a.incd_qty) ELSE (a.incd_price_usd/a.incd_qty) END AS unit_price
		 FROM incoming_detail a INNER JOIN incoming b ON b.id=a.incd_hid
		 WHERE b.inch_docdate='$docdate' AND b.inch_docno='$docno' AND b.inch_doctype='$doctype' AND a.incd_prodid=$prodid AND b.inch_status=20
		 AND ((a.incd_qty>0 AND a.incd_price>0) OR (a.incd_qty>0 AND a.incd_price_usd>0)) ORDER BY a.id DESC LIMIT 0,1"));
	if(empty($q['unit_price'])){
		$qq = mysql_fetch_array(mysql_query("SELECT stock_price FROM stock WHERE stock_date BETWEEN '$sdate' AND '$edate' AND stock_loc=1 AND stock_prodid=$prodid 
			  AND stock_docdate='$docdate' AND stock_docno='$docno' AND stock_doctype='$doctype' AND stock_price>0 ORDER BY id DESC LIMIT 0,1"));
		return $qq['stock_price'];
	}else{
		return $q['unit_price'];
	}	
}
#Get Unit Price Bahan baku & penolong untuk produksi
function get_unitprice_bb($sdate,$edate,$prodid){
	$q = mysql_fetch_array(mysql_query("SELECT CASE WHEN a.incd_currency='IDR' THEN (a.incd_price/a.incd_qty) ELSE (a.incd_price_usd/a.incd_qty) END AS unit_price
		 FROM incoming_detail a INNER JOIN incoming b ON b.id=a.incd_hid
		 WHERE b.inch_docdate BETWEEN '$sdate' AND '$edate' AND b.inch_status=20 AND a.incd_prodid=$prodid 
		 AND ((a.incd_qty>0 AND a.incd_price>0) OR (a.incd_qty>0 AND a.incd_price_usd>0)) ORDER BY a.id DESC LIMIT 0,1"));
	if(empty($q['unit_price'])){
		$qq = mysql_fetch_array(mysql_query("SELECT stock_price FROM stock WHERE stock_date BETWEEN '$sdate' AND '$edate' AND stock_loc=1 AND stock_prodid=$prodid 
			  AND stock_price>0 ORDER BY id DESC LIMIT 0,1"));
		return $qq['stock_price'];
	}else{
		return $q['unit_price'];
	}	
}
#Get Unit Price WIP
function get_unitprice_wip($sdate,$edate,$prodid){
	$wuprice=0;
	$dh = mysql_fetch_array(mysql_query("SELECT a.id, a.wo_qty FROM workorder a	INNER JOIN product b ON b.id=a.wo_prodid
		WHERE a.wo_date BETWEEN '$sdate' AND '$edate' AND a.wo_status=20 AND b.prod_cat=5 AND a.wo_prodid='$prodid' AND a.wo_qty>0 
		ORDER BY a.id DESC LIMIT 0,1"));
	$tiprice=0;	
	$qd = mysql_query("SELECT wod_prodid, wod_qty FROM workorder_detail WHERE wod_hid='$dh[id]'");
	while($dd=mysql_fetch_array($qd)){
		$iprice  = get_unitprice_bb($sdate,$edate,$dd['wod_prodid']);
		$aiprice = $dd['wod_qty']*$iprice;
		$tiprice = $tiprice+$aiprice;
	}
	if(empty($tiprice)){
		$qq = mysql_fetch_array(mysql_query("SELECT stock_price FROM stock WHERE stock_date BETWEEN '$sdate' AND '$edate' AND stock_loc=2 AND stock_prodid=$prodid 
			  AND stock_price>0 ORDER BY id DESC LIMIT 0,1"));
		$wuprice = $qq['stock_price'];
	}else{
		$wuprice = $tiprice/$dh['wo_qty'];
	}
	return $wuprice;
}
#Get Unit Price BJ
function get_unitprice_bj($sdate,$edate,$prodid){
	$wuprice=0;
	$dh = mysql_fetch_array(mysql_query("SELECT a.id, a.wo_qty FROM workorder a	INNER JOIN product b ON b.id=a.wo_prodid
		WHERE a.wo_date BETWEEN '$sdate' AND '$edate' AND a.wo_status=20 AND b.prod_cat=3 AND a.wo_prodid='$prodid' AND a.wo_qty>0 
		ORDER BY a.id DESC LIMIT 0,1"));
	$tiprice=0;	
	$qd = mysql_query("SELECT a.wod_prodid, a.wod_qty, b.prod_cat FROM workorder_detail a INNER JOIN product b ON b.id=a.wod_prodid WHERE a.wod_hid='$dh[id]'");
	while($dd=mysql_fetch_array($qd)){
		if($dd['prod_cat']==1 || $dd['prod_cat']==6){
			$iprice  = get_unitprice_bb($sdate,$edate,$dd['wod_prodid']);
		}elseif($dd['prod_cat']==5){
			$iprice  = get_unitprice_wip($sdate,$edate,$dd['wod_prodid']);
		}else{
			$iprice  = 0;
		}
		$aiprice = $dd['wod_qty']*$iprice;
		$tiprice = $tiprice+$aiprice;
	}
	if(empty($tiprice)){
		$qq = mysql_fetch_array(mysql_query("SELECT stock_price FROM stock WHERE stock_date BETWEEN '$sdate' AND '$edate' AND stock_loc=3 AND stock_prodid=$prodid 
			  AND stock_price>0 ORDER BY id DESC LIMIT 0,1"));
		$wuprice = $qq['stock_price'];
	}else{
		$wuprice = $tiprice/$dh['wo_qty'];
	}
	return $wuprice;
}
function get_invno($inv_date,$type){
	if($type=="REGULER"){$dtype="";}elseif($type=="SUBKON"){$dtype="/SUB";}elseif($type=="TRIAL"){$dtype="/T";}
	elseif($type=="SAMPLE"){$dtype="/S";}elseif($type=="REPAIR"){$dtype="/REP";}elseif($type=="DIKEMBALIKAN"){$dtype="/RET";}
	$year   = substr($inv_date,0,4);
	$month  = substr($inv_date,5,2);
	$lno    = mysql_fetch_array(mysql_query("SELECT RIGHT(dlv_invno,4) nomor FROM delivery WHERE dlv_status=10 ORDER BY id DESC LIMIT 0,1"));
	$lmonth = mysql_fetch_array(mysql_query("SELECT MONTH(dlv_invdate) bulan FROM delivery WHERE dlv_status=10 ORDER BY id DESC LIMIT 0,1"));
	$lastno = $lno['nomor']; $lastmonth = $lmonth['bulan'];
	if(empty($lastno)){$seq = 1;}else{$seq   = (int)$lastno+1;}
	if($seq < 10){$seq = "000".$seq;}elseif($seq < 100){$seq = "00".$seq;}elseif($seq < 1000){$seq = "0".$seq;}else{$seq;}
	if($lastmonth == $month){
		$regno = "HDI".substr($year,2,2)."".$month."".$dtype."/".$seq;
    }else{
		$regno = "HDI".substr($year,2,2)."".$month."".$dtype."/0001";
    }
	return $regno;
}
function get_sjno2($dlv_sjdate){
	$year   = substr($dlv_sjdate,0,4);
	$month  = substr($dlv_sjdate,5,2);
	$romawi = get_romawi($month);
	$lno    = mysql_fetch_array(mysql_query("SELECT LEFT(dlv_nosj,5) nomor FROM delivery WHERE dlv_status=10 ORDER BY id DESC LIMIT 0,1"));
	$lyear = mysql_fetch_array(mysql_query("SELECT YEAR(dlv_sjdate) tahun, MONTH(dlv_sjdate) bulan FROM delivery WHERE dlv_status=10 ORDER BY id DESC LIMIT 0,1"));
	$lastno = $lno['nomor']; $lastyear = $lyear['tahun']; $lastmonth = $lyear['bulan'];
	if(empty($lastno)){$seq = 1;}else{$seq   = (int)$lastno+1;}
	if($seq < 10){$seq = "0000".$seq;}elseif($seq < 100){$seq = "000".$seq;}elseif($seq < 1000){$seq = "00".$seq;}elseif($seq < 10000){$seq = "0".$seq;}else{$seq;}
	if($lastmonth == $month){
		$regno = $seq."/HDI/DEL/".$romawi."/".$year;
    }else{
		$regno = "00001/HDI/DEL/".$romawi."/".$year;
    }
	return $regno;
}
function get_pono_hdi($po_date){
	$year   = substr($po_date,0,4);
	$month  = substr($po_date,5,2); #date('m');
	$lno = mysql_fetch_array(mysql_query("SELECT LEFT(po_no,3) regno FROM purchase_order WHERE po_type='Purchasing' AND po_status<>100 ORDER BY id DESC LIMIT 0,1"));
	$lmonth = mysql_fetch_array(mysql_query("SELECT MONTH(po_date) bln FROM purchase_order WHERE po_type='Purchasing' AND po_status<>100 ORDER BY id DESC LIMIT 0,1"));
	$lastno = $lno['regno'];
	$lastmonth = $lmonth['bln'];
	if(empty($lastno)){
		$seq = 1;
	}else{
		$seq   = (int)$lastno+1;
	}
	if ($seq < 10)          $seq = "00".$seq;
    elseif($seq < 100)      $seq = "0".$seq;
    else $seq;
	if($lastmonth == $month){
		$regno = $seq."/HDI/PC/".$month."/".$year;
    }else{
		$regno = "001/HDI/PC/".$month."/".$year;
    }
	return $regno;
}
function get_kurspajak($bdate,$fcurr){
	$cr = mysql_fetch_array(mysql_query("SELECT rate_nto FROM rate WHERE rate_status=10 AND rate_dfrom <='$bdate' AND rate_dto >='$bdate' AND rate_tfrom='$fcurr'"));
	if(!empty($cr['rate_nto'])){
		return $cr['rate_nto'];
	}else{
		return 1;
	}
}
function get_currency($sdate,$edate,$prodid,$docdate,$docno,$doctype){
	$q = mysql_fetch_array(mysql_query("SELECT CASE WHEN b.valuta is null THEN a.incd_currency ELSE b.valuta END as curr FROM incoming_detail a INNER JOIN incoming b ON b.id=a.incd_hid
		 WHERE b.inch_docdate='$docdate' AND b.inch_docno='$docno' AND b.inch_doctype='$doctype' AND a.incd_prodid=$prodid AND b.inch_status=20
		 AND ((a.incd_qty>0 AND a.incd_price>0) OR (a.incd_qty>0 AND a.incd_price_usd>0)) ORDER BY a.id DESC LIMIT 0,1"));
	if(empty($q['curr'])){
		$qq = mysql_fetch_array(mysql_query("SELECT stock_currency FROM stock WHERE stock_date BETWEEN '$sdate' AND '$edate' AND stock_loc=1 AND stock_prodid=$prodid 
			  AND stock_docdate='$docdate' AND stock_docno='$docno' AND stock_doctype='$doctype' AND stock_price>0 ORDER BY id DESC LIMIT 0,1"));
		return $qq['stock_currency'];
	}else{
		return $q['curr'];
	}	
}
function get_kvno2(){
	$year   = date('Y');
	$month  = date('m');
	$lno = mysql_fetch_array(mysql_query("SELECT SUBSTR(kv_no,7,6) regno FROM konversi2 ORDER BY id DESC LIMIT 0,1"));
	$lmonth = mysql_fetch_array(mysql_query("SELECT MONTH(kv_date) bln FROM konversi2 ORDER BY id DESC LIMIT 0,1"));
	$lastno = $lno['regno'];
	$lastmonth = $lmonth['bln'];
	if(empty($lastno)){
		$seq = 1;
	}else{
		$seq   = (int)$lastno+1;
	}
	if ($seq < 10)          $seq = "00000".$seq;
    elseif($seq < 100)      $seq = "0000".$seq;
    elseif($seq < 1000)     $seq = "000".$seq;
	elseif($seq < 10000)    $seq = "00".$seq;
	elseif($seq < 100000)   $seq = "0".$seq;
    else $seq;
	if($lastmonth == $month){	  
		$regno = "KV".substr($year,2,2)."".$month."".$seq;
    }else{
		$regno = "KV".substr($year,2,2)."".$month."000001";
    }
	return $regno;
}
function get_kvextno(){
	$year   = date('Y');
	$month  = date('m');
	$lno = mysql_fetch_array(mysql_query("SELECT SUBSTR(kv_no,7,6) regno FROM konversi_external ORDER BY id DESC LIMIT 0,1"));
	$lmonth = mysql_fetch_array(mysql_query("SELECT MONTH(kv_date) bln FROM konversi_external ORDER BY id DESC LIMIT 0,1"));
	$lastno = $lno['regno'];
	$lastmonth = $lmonth['bln'];
	if(empty($lastno)){
		$seq = 1;
	}else{
		$seq   = (int)$lastno+1;
	}
	if ($seq < 10)          $seq = "00000".$seq;
    elseif($seq < 100)      $seq = "0000".$seq;
    elseif($seq < 1000)     $seq = "000".$seq;
	elseif($seq < 10000)    $seq = "00".$seq;
	elseif($seq < 100000)   $seq = "0".$seq;
    else $seq;
	if($lastmonth == $month){	  
		$regno = "KE".substr($year,2,2)."".$month."".$seq;
    }else{
		$regno = "KE".substr($year,2,2)."".$month."000001";
    }
	return $regno;
}
function get_stockexbc($start_date2,$end_date2,$prdid,$inv){
	$q = mysql_fetch_array(mysql_query("SELECT SUM(z.endqty) stock FROM (SELECT  (b.inqty-b.outqty) endqty FROM
						(SELECT a.prodid, a.doctype, a.docno, a.docdate, SUM(a.inqty) inqty, SUM(a.outqty) outqty, a.price, b.prod_code, b.prod_name, b.prod_sat, a.curr  FROM
						(SELECT stock_prodid prodid, ifnull(stock_doctype,0) doctype, ifnull(stock_docno,0) docno, stock_docdate docdate, stock_qty inqty, 0 outqty, (stock_price/stock_qty) price, stock_currency curr 
						FROM stock WHERE stock_status=10 AND stock_date='$start_date2' AND stock_loc IN (1,4)
						UNION ALL
						SELECT b.incd_prodid prodid, ifnull(a.inch_doctype,0) doctype , ifnull(a.inch_docno,0) docno, a.inch_docdate docdate, b.incd_qty inqty, 0 outqty, 
						(b.incd_price/b.incd_qty) price, b.incd_currency curr 
						FROM incoming a
						INNER JOIN incoming_detail b ON b.incd_hid=a.id
						WHERE a.inch_status=20 AND a.inch_docdate BETWEEN '$start_date2' AND '$end_date2'
						UNION ALL
						SELECT a.trfd_prodid prodid, ifnull(a.trfd_doctype,0) doctype , ifnull(a.trfd_docno,0) docno, a.trfd_docdate docdate, 0 inqty, a.trfd_qty outqty, 0 price, '' curr 
						FROM transfer_detailexbc a
						INNER JOIN transfer b ON b.id=a.trfd_hid
						WHERE b.trf_status=20 AND a.trfd_status=10 AND b.trf_category=1 AND b.trf_spbdate BETWEEN '$start_date2' AND '$end_date2'
						UNION ALL
						SELECT a.trfd_prodid prodid, ifnull(a.trfd_doctype,0) doctype , ifnull(a.trfd_docno,0) docno, a.trfd_docdate docdate, a.trfd_qty inqty, 0 outqty, 0 price, '' curr 
						FROM transfer_detailexbc a
						INNER JOIN transfer b ON b.id=a.trfd_hid
						WHERE b.trf_status=20 AND a.trfd_status=10 AND b.trf_category=3 AND b.trf_spbdate BETWEEN '$start_date2' AND '$end_date2'
						UNION ALL
						SELECT a.prodid, ifnull(a.jenis_dokpab_asal,0) doctype, ifnull(a.no_dokpab_asal,0) docno, a.tgl_dokpab_asal docdate, 0 inqty, a.jumlah outqty, 0 price, '' curr 
						FROM outgoing_dokasal a
						INNER JOIN outgoing b ON b.id=a.outh_hid            
						WHERE b.outh_status=20 AND a.log_status=20 AND b.outh_docdate BETWEEN '$start_date2' AND '$end_date2') a
						INNER JOIN product b ON b.id=a.prodid
						WHERE b.id='$prdid' GROUP BY a.prodid, a.doctype, a.docno) b
						WHERE (b.inqty-b.outqty) > 0 ) as z",$inv));
	if(!empty($q['stock'])){
		return $q['stock'];
	}else{	
		return 0;
	}	
}
#Get Transfer antar lokasi
function get_tllastno()
{
	$max_id = mysql_fetch_row(mysql_query("SELECT SUBSTR((tlh_no),12,4) FROM transfer_lokasi ORDER BY id DESC LIMIT 1"));
	$max_id = $max_id[0];
	return $max_id;	
}	
#Get TL Month
function get_tlmonth()
{
	$max_id = mysql_fetch_row(mysql_query("SELECT SUBSTR((tlh_no),9,2) FROM transfer_lokasi ORDER BY id DESC LIMIT 1"));
	$max_id = $max_id[0];
	return $max_id;	
}	
#Get TL No
function gettlno()
{
	$year  = date('Y');
	$month = date('m');
	$seq   = (int)get_tllastno()+1;
	 if ($seq < 10)   $seq = "000".$seq;
	elseif($seq < 100)  $seq = "00".$seq;
	elseif($seq < 1000) $seq = "0".$seq;
	else $seq;
	$last_month = get_tlmonth();
	$getcode = get_code();
	if($last_month == $month){
		$adj_no = "TAL-$year$month-$seq/$getcode";
	}else{
		$adj_no = "TAL-$year$month-0001/$getcode";
	}	
	return $adj_no;
}
function get_labarugi_sgi($fromdate,$todate){
	$no1=1; $t_lastmonth1=0; $t_curmonth1=0; $t_curyear1=0;
	$qd1 = mysql_query("SELECT a.j_acctno, a.j_name1 FROM jurnal_master a			
			WHERE a.j_status=10 AND a.j_lr='Y' AND j_rumus=1 ORDER BY a.j_acctno ASC");
	while($dd1 = mysql_fetch_array($qd1)){
		$curmonth1   = get_nr_curmonth($fromdate,$todate,$dd1['j_acctno']);
		$t_curmonth1 = $t_curmonth1+$curmonth1;
		$lastmonth1   = get_nr_lastmonth($fromdate,$todate,$dd1['j_acctno']);
		$t_lastmonth1 = $t_lastmonth1+$lastmonth1;
		$curyear1     = $lastmonth1+$curmonth1;
		$t_curyear1   = $t_curyear1+$curyear1;
	}	
  
	$no2=1; $t_lastmonth2=0; $t_curmonth2=0; $t_curyear2=0;
	$qd2 = mysql_query("SELECT a.j_acctno, a.j_name1 FROM jurnal_master a			
			WHERE a.j_status=10 AND a.j_lr='Y' AND j_rumus=2 ORDER BY a.j_acctno ASC");
	while($dd2 = mysql_fetch_array($qd2)){
		$curmonth2   = get_nr_curmonth($fromdate,$todate,$dd2['j_acctno']);
		$t_curmonth2 = $t_curmonth2+$curmonth2;
		$lastmonth2   = get_nr_lastmonth($fromdate,$todate,$dd2['j_acctno']);
		$t_lastmonth2 = $t_lastmonth2+$lastmonth2;
		$curyear2     = $lastmonth2+$curmonth2;
		$t_curyear2   = $t_curyear2+$curyear2;
  	}
	
	$no3=1; $t_lastmonth3=0; $t_curmonth3=0; $t_curyear3=0;
	$qd3 = mysql_query("SELECT a.j_acctno, a.j_name1 FROM jurnal_master a			
			WHERE a.j_status=10 AND a.j_lr='Y' AND j_rumus=3 ORDER BY a.j_acctno ASC");
	while($dd3 = mysql_fetch_array($qd3)){
		$curmonth3   = get_nr_curmonth($fromdate,$todate,$dd3['j_acctno']);
		$t_curmonth3 = $t_curmonth3+$curmonth3;
		$lastmonth3   = get_nr_lastmonth($fromdate,$todate,$dd3['j_acctno']);
		$t_lastmonth3 = $t_lastmonth3+$lastmonth3;
		$curyear3     = $lastmonth3+$curmonth3;
		$t_curyear3   = $t_curyear3+$curyear3;
   	}
	
	$no4=1; $t_lastmonth4=0; $t_curmonth4=0; $t_curyear4=0;
	$qd4 = mysql_query("SELECT a.j_acctno, a.j_name1 FROM jurnal_master a			
			WHERE a.j_status=10 AND a.j_lr='Y' AND j_rumus=4 ORDER BY a.j_acctno ASC");
	while($dd4 = mysql_fetch_array($qd4)){
		$curmonth4   = get_nr_curmonth($fromdate,$todate,$dd4['j_acctno']);
		$t_curmonth4 = $t_curmonth4+$curmonth4;
		$lastmonth4   = get_nr_lastmonth($fromdate,$todate,$dd4['j_acctno']);
		$t_lastmonth4 = $t_lastmonth4+$lastmonth4;
		$curyear4     = $lastmonth4+$curmonth4;
		$t_curyear4   = $t_curyear4+$curyear4;
    }
	
	$no5=1; $t_lastmonth5=0; $t_curmonth5=0; $t_curyear5=0;
	$qd5 = mysql_query("SELECT a.j_acctno, a.j_name1 FROM jurnal_master a			
			WHERE a.j_status=10 AND a.j_lr='Y' AND j_rumus=5 ORDER BY a.j_acctno ASC");
	while($dd5 = mysql_fetch_array($qd5)){
		$curmonth5   = get_nr_curmonth($fromdate,$todate,$dd5['j_acctno']);
		$t_curmonth5 = $t_curmonth5+$curmonth5;
		$lastmonth5   = get_nr_lastmonth($fromdate,$todate,$dd5['j_acctno']);
		$t_lastmonth5 = $t_lastmonth5+$lastmonth5;
		$curyear5     = $lastmonth5+$curmonth5;
		$t_curyear5   = $t_curyear5+$curyear5;
    }
	
	$no6=1; $t_lastmonth6=0; $t_curmonth6=0; $t_curyear6=0;
	$qd6 = mysql_query("SELECT a.j_acctno, a.j_name1 FROM jurnal_master a			
			WHERE a.j_status=10 AND a.j_lr='Y' AND j_rumus=6 ORDER BY a.j_acctno ASC");
	while($dd6 = mysql_fetch_array($qd6)){
		$curmonth6   = get_nr_curmonth($fromdate,$todate,$dd6['j_acctno']);
		$t_curmonth6 = $t_curmonth6+$curmonth6;
		$lastmonth6   = get_nr_lastmonth($fromdate,$todate,$dd6['j_acctno']);
		$t_lastmonth6 = $t_lastmonth6+$lastmonth6;
		$curyear6     = $lastmonth6+$curmonth6;
		$t_curyear6   = $t_curyear6+$curyear6;
    }
	
	$no7=1; $t_lastmonth7=0; $t_curmonth7=0; $t_curyear7=0;
	$qd7 = mysql_query("SELECT a.j_acctno, a.j_name1 FROM jurnal_master a			
			WHERE a.j_status=10 AND a.j_lr='Y' AND j_rumus=7 ORDER BY a.j_acctno ASC");
	while($dd7 = mysql_fetch_array($qd7)){
		$curmonth7   = get_nr_curmonth($fromdate,$todate,$dd7['j_acctno']);
		$t_curmonth7 = $t_curmonth7+$curmonth7;
		$lastmonth7   = get_nr_lastmonth($fromdate,$todate,$dd7['j_acctno']);
		$t_lastmonth7 = $t_lastmonth7+$lastmonth7;
		$curyear7     = $lastmonth7+$curmonth7;
		$t_curyear7   = $t_curyear7+$curyear7;
    }
  
	$t_cgs_lastmonth=$t_lastmonth2+$t_lastmonth3+$t_lastmonth4+$t_lastmonth5+$t_lastmonth6+$t_lastmonth7;
	$t_cgs_curmonth=$t_curmonth2+$t_curmonth3+$t_curmonth4+$t_curmonth5+$t_curmonth6+$t_curmonth7;
	$t_cgs_curyear=$t_curyear2+$t_curyear3+$t_curyear4+$t_curyear5+$t_curyear6+$t_curyear7;
  
	$t_grp_lastmonth=$t_lastmonth1+$t_cgs_lastmonth;
	$t_grp_curmonth=$t_curmonth1+$t_cgs_curmonth;
	$t_grp_curyear=$t_curyear1+$t_cgs_curyear;
    
	$no8=1; $t_lastmonth8=0; $t_curmonth8=0; $t_curyear8=0;
	$qd8 = mysql_query("SELECT a.j_acctno, a.j_name1 FROM jurnal_master a			
			WHERE a.j_status=10 AND a.j_lr='Y' AND j_rumus=8 ORDER BY a.j_acctno ASC");
	while($dd8 = mysql_fetch_array($qd8)){
		$curmonth8   = get_nr_curmonth($fromdate,$todate,$dd8['j_acctno']);
		$t_curmonth8 = $t_curmonth8+$curmonth8;
		$lastmonth8   = get_nr_lastmonth($fromdate,$todate,$dd8['j_acctno']);
		$t_lastmonth8 = $t_lastmonth8+$lastmonth8;
		$curyear8     = $lastmonth8+$curmonth8;
		$t_curyear8   = $t_curyear8+$curyear8;
    }
	
	$no9=1; $t_lastmonth9=0; $t_curmonth9=0; $t_curyear9=0;
	$qd9 = mysql_query("SELECT a.j_acctno, a.j_name1 FROM jurnal_master a			
			WHERE a.j_status=10 AND a.j_lr='Y' AND j_rumus=9 ORDER BY a.j_acctno ASC");
	while($dd9 = mysql_fetch_array($qd9)){
		$curmonth9   = get_nr_curmonth($fromdate,$todate,$dd9['j_acctno']);
		$t_curmonth9 = $t_curmonth9+$curmonth9;
		$lastmonth9   = get_nr_lastmonth($fromdate,$todate,$dd9['j_acctno']);
		$t_lastmonth9 = $t_lastmonth9+$lastmonth9;
		$curyear9     = $lastmonth9+$curmonth9;
		$t_curyear9   = $t_curyear9+$curyear9;
    }
	
	$no10=1; $t_lastmonth10=0; $t_curmonth10=0; $t_curyear10=0;
	$qd10 = mysql_query("SELECT a.j_acctno, a.j_name1 FROM jurnal_master a			
			WHERE a.j_status=10 AND a.j_lr='Y' AND j_rumus=10 ORDER BY a.j_acctno ASC");
	while($dd10 = mysql_fetch_array($qd10)){
		$curmonth10   = get_nr_curmonth($fromdate,$todate,$dd10['j_acctno']);
		$t_curmonth10 = $t_curmonth10+$curmonth10;
		$lastmonth10   = get_nr_lastmonth($fromdate,$todate,$dd10['j_acctno']);
		$t_lastmonth10 = $t_lastmonth10+$lastmonth10;
		$curyear10     = $lastmonth10+$curmonth10;
		$t_curyear10   = $t_curyear10+$curyear10;
  	}
  	
	$no11=1; $t_lastmonth11=0; $t_curmonth11=0; $t_curyear11=0;
	$qd11 = mysql_query("SELECT a.j_acctno, a.j_name1 FROM jurnal_master a			
			WHERE a.j_status=10 AND a.j_lr='Y' AND j_rumus=11 ORDER BY a.j_acctno ASC");
	while($dd11 = mysql_fetch_array($qd11)){
		$curmonth11   = get_nr_curmonth($fromdate,$todate,$dd11['j_acctno']);
		$t_curmonth11 = $t_curmonth11+$curmonth11;
		$lastmonth11   = get_nr_lastmonth($fromdate,$todate,$dd11['j_acctno']);
		$t_lastmonth11 = $t_lastmonth11+$lastmonth11;
		$curyear11     = $lastmonth11+$curmonth11;
		$t_curyear11   = $t_curyear11+$curyear11;
  	}
	
	$t_opex_lastmonth=$t_lastmonth8+$t_lastmonth9+$t_lastmonth10+$t_lastmonth11;
	$t_opex_curmonth=$t_curmonth8+$t_curmonth9+$t_curmonth10+$t_curmonth11;
	$t_opex_curyear=$t_curyear8+$t_curyear9+$t_curyear10+$t_curyear11;
  	
	$t_ifo_lastmonth	=$t_grp_lastmonth-$t_opex_lastmonth;
	$t_ifo_curmonth		=$t_grp_curmonth-$t_opex_curmonth;
	$t_ifo_curyear		=$t_grp_curyear-$t_opex_curyear;
  	
	$no12=1; $t_lastmonth12=0; $t_curmonth12=0; $t_curyear12=0;
	$qd12 = mysql_query("SELECT a.j_acctno, a.j_name1 FROM jurnal_master a			
			WHERE a.j_status=10 AND a.j_lr='Y' AND j_rumus=12 ORDER BY a.j_acctno ASC");
	while($dd12 = mysql_fetch_array($qd12)){
		$curmonth12   = get_nr_curmonth($fromdate,$todate,$dd12['j_acctno']);
		$t_curmonth12 = $t_curmonth12+$curmonth12;
		$lastmonth12   = get_nr_lastmonth($fromdate,$todate,$dd12['j_acctno']);
		$t_lastmonth12 = $t_lastmonth12+$lastmonth12;
		$curyear12     = $lastmonth12+$curmonth12;
		$t_curyear12   = $t_curyear12+$curyear12;
  	}
	
	$no13=1; $t_lastmonth13=0; $t_curmonth13=0; $t_curyear13=0;
	$qd13 = mysql_query("SELECT a.j_acctno, a.j_name1 FROM jurnal_master a			
			WHERE a.j_status=10 AND a.j_lr='Y' AND j_rumus=13 ORDER BY a.j_acctno ASC");
	while($dd13 = mysql_fetch_array($qd13)){
		$curmonth13   = get_nr_curmonth($fromdate,$todate,$dd13['j_acctno']);
		$t_curmonth13 = $t_curmonth13+$curmonth13;
		$lastmonth13   = get_nr_lastmonth($fromdate,$todate,$dd13['j_acctno']);
		$t_lastmonth13 = $t_lastmonth13+$lastmonth13;
		$curyear13     = $lastmonth13+$curmonth13;
		$t_curyear13   = $t_curyear13+$curyear13;
  	}
	
	$no14=1; $t_lastmonth14=0; $t_curmonth14=0; $t_curyear14=0;
	$qd14 = mysql_query("SELECT a.j_acctno, a.j_name1 FROM jurnal_master a			
			WHERE a.j_status=10 AND a.j_lr='Y' AND j_rumus=14 ORDER BY a.j_acctno ASC");
	while($dd14 = mysql_fetch_array($qd14)){
		$curmonth14   = get_nr_curmonth($fromdate,$todate,$dd14['j_acctno']);
		$t_curmonth14 = $t_curmonth14+$curmonth14;
		$lastmonth14   = get_nr_lastmonth($fromdate,$todate,$dd14['j_acctno']);
		$t_lastmonth14 = $t_lastmonth14+$lastmonth14;
		$curyear14     = $lastmonth14+$curmonth14;
		$t_curyear14   = $t_curyear14+$curyear14;
  	}
	
	$t_otex_lastmonth=$t_lastmonth13+$t_lastmonth14;
	$t_otex_curmonth=$t_curmonth13+$t_curmonth14;
	$t_otex_curyear=$t_curyear13+$t_curyear14;
  
	$t_oie_lastmonth	=$t_lastmonth12-$t_otex_lastmonth;
	$t_oie_curmonth		=$t_curmonth12-$t_otex_curmonth;
	$t_oie_curyear		=$t_curyear12-$t_otex_curyear;
  
	$t_npl_bt_lastmonth		=$t_ifo_lastmonth+$t_oie_lastmonth;
	$t_npl_bt_curmonth		=$t_ifo_curmonth+$t_oie_curmonth;
	$t_npl_bt_curyear		=$t_ifo_curyear+$t_oie_curyear;
  	
	$no15=1; $t_lastmonth15=0; $t_curmonth15=0; $t_curyear15=0;
	$qd15 = mysql_query("SELECT a.j_acctno, a.j_name1 FROM jurnal_master a			
			WHERE a.j_status=10 AND a.j_lr='Y' AND j_rumus=15 ORDER BY a.j_acctno ASC");
	while($dd15 = mysql_fetch_array($qd15)){
		$curmonth15   = get_nr_curmonth($fromdate,$todate,$dd15['j_acctno']);
		$t_curmonth15 = $t_curmonth15+$curmonth15;
		$lastmonth15   = get_nr_lastmonth($fromdate,$todate,$dd15['j_acctno']);
		$t_lastmonth15 = $t_lastmonth15+$lastmonth15;
		$curyear15     = $lastmonth15+$curmonth15;
		$t_curyear15   = $t_curyear15+$curyear15;
  	}
	
	$t_npl_at_lastmonth=$t_npl_bt_lastmonth-$t_lastmonth15;
	$t_npl_at_curmonth=$t_npl_bt_curmonth-$t_curmonth15;
	$t_npl_at_curyear=$t_npl_bt_curyear-$t_curyear15;
  	
	$no16=1; $t_lastmonth16=0; $t_curmonth16=0; $t_curyear16=0;
	$qd16 = mysql_query("SELECT a.j_acctno, a.j_name1 FROM jurnal_master a			
			WHERE a.j_status=10 AND a.j_lr='Y' AND j_rumus=16 ORDER BY a.j_acctno ASC");
	while($dd16 = mysql_fetch_array($qd16)){
		$curmonth16   = get_nr_curmonth($fromdate,$todate,$dd16['j_acctno']);
		$t_curmonth16 = $t_curmonth16+$curmonth16;
		$lastmonth16   = get_nr_lastmonth($fromdate,$todate,$dd16['j_acctno']);
		$t_lastmonth16 = $t_lastmonth16+$lastmonth16;
		$curyear16     = $lastmonth16+$curmonth16;
		$t_curyear16   = $t_curyear16+$curyear16;
  	}
	
	$t_pl_by_lastmonth=$t_npl_at_lastmonth-$t_lastmonth16;
	$t_pl_by_curmonth=$t_npl_at_curmonth-$t_curmonth16;
	$t_pl_by_curyear=$t_npl_at_curyear-$t_curyear16;
  	
	return $t_pl_by_curmonth;
}
function get_nr_lastmonth_usd($fromdate,$todate,$acctno){
	$cek = mysql_fetch_array( mysql_query("SELECT j_bbnrstatus FROM jurnal_master WHERE j_status=10 AND j_acctno='$acctno'"));
	$sa=mysql_fetch_array(mysql_query("SELECT sa_price_usd FROM stock_acct WHERE sa_date='$fromdate' AND sa_status=10 AND sa_acctno='$acctno' ORDER BY id DESC LIMIT 0,1"));	
	if($cek['j_bbnrstatus']=="NO"){
		return $sa['sa_price_usd'];
	}else{
		return $sa['sa_price_usd']*-1;
	}	
}
function get_nr_curmonth_usd($fromdate,$todate,$acctno){
	$qcoa = mysql_query("SELECT j_acctno, j_bbnrstatus, j_rumusbb FROM jurnal_master WHERE j_status=10 AND j_acctno='$acctno' ORDER BY j_acctno ASC");
	while($dcoa=mysql_fetch_array($qcoa)){
		$no=1;
		$saldo =0; $t_amountin=0; $t_amountout=0; $saldo_usd =0; $t_amountin_usd=0; $t_amountout_usd=0;
		$qd=mysql_query("SELECT a.tanggal, a.noreferensi, a.keterangan, a.amountin, a.amountin_usd, a.amountout, a.amountout_usd, a.flag FROM
				(SELECT a.jurnal_date tanggal, a.jurnal_invno noreferensi, b.jurnald_desc keterangan, b.jurnald_amount amountin, b.jurnald_amount_usd amountin_usd, 
				0 amountout, 0 amountout_usd, 1 flag FROM jurnal a INNER JOIN jurnal_detail b ON b.jurnal_no=a.jurnal_no
				WHERE a.jurnal_status=20 AND a.jurnal_date BETWEEN '$fromdate' AND '$todate' AND jurnald_acctno='$dcoa[j_acctno]' AND b.jurnald_status=10		
				UNION ALL
				SELECT a.jurnal_date tanggal, a.jurnal_invno noreferensi, b.jurnald_desc keterangan, 0 amountin, 0 amountin_usd,
				b.jurnald_amount amountout, b.jurnald_amount_usd amountout_usd, 2 flag FROM jurnal a INNER JOIN jurnal_detail b ON b.jurnal_no=a.jurnal_no
				WHERE a.jurnal_status=20 AND a.jurnal_date BETWEEN '$fromdate' AND '$todate' AND jurnald_acctno='$dcoa[j_acctno]' AND b.jurnald_status=20) a
				ORDER BY a.tanggal ASC");
		$num = mysql_num_rows($qd);		
		if($num==0){
			$t_saldo_usd = $saldo_usd;
			$t_saldo     = $saldo;
		}else{		
			while($dd=mysql_fetch_array($qd)){	
				if($dcoa['j_rumusbb']=='A'){	
					$saldo_usd 	= $saldo_usd+$dd['amountin_usd']-$dd['amountout_usd'];
					$saldo 		= $saldo+$dd['amountin']-$dd['amountout'];
				}else{
					$saldo_usd 	= $saldo_usd-$dd['amountin_usd']+$dd['amountout_usd'];
					$saldo 		= $saldo-$dd['amountin']+$dd['amountout'];
				}
				$t_saldo_usd = $saldo_usd;
				$t_saldo	 = $saldo;
				$t_amountin_usd  = $t_amountin_usd+$dd['amountin_usd'];
				$t_amountin  	 = $t_amountin+$dd['amountin'];
				$t_amountout_usd = $t_amountout_usd+$dd['amountout_usd'];
				$t_amountout 	 = $t_amountout+$dd['amountout'];
			}
		}
		if($dcoa['j_bbnrstatus']=="NO"){
			return $t_saldo_usd;
		}else{
			return $t_saldo_usd*-1;
		}	
	}
}
function get_valwo_upl_wip($wo_no,$start_date2,$end_date2,$inv){
	$html ="";
	$qr=mysql_query("SELECT * FROM workorder_temp WHERE wo_no='$wo_no' AND wod_status=30",$inv);
    while($d=mysql_fetch_array($qr)){
		$vdate = get_validationdate2($d['wo_date'],$inv);
		if($vdate=="NO"){
			$colorvdate ="#FF0000";
		}else{
			$colorvdate ="#000000";
		}
		#kode barang jadi	
		$vprod1 = mysql_fetch_array(mysql_query("SELECT id FROM product WHERE prod_code='$d[wo_prodcode]' AND prod_cat<>'100'",$inv));
		if(empty($vprod1['id'])){
			$colorvprod1 ="#FF0000";
		}else{
			$colorvprod1 ="#000000";
		}	
		#kode bahan baku
		$vprod2 = mysql_fetch_array(mysql_query("SELECT id FROM product WHERE prod_code='$d[wod_prodcode]' AND prod_cat<>'100'",$inv));
		if(empty($vprod2['id'])){
			$colorvprod2 ="#FF0000";
		}else{
			$colorvprod2 ="#000000";
		}
		$stock = get_stock_produksi($d['wod_prodcode'],$start_date2,$end_date2,$inv);
		if($stock < $d['wod_qty']){
			$colorvstock ="#FF0000";
		}else{
			$colorvstock ="#000000";
		}
		if(($vdate=="NO") || (empty($vprod1['id'])) || (empty($vprod2['id'])) || ($stock < $d['wod_qty']) ){
			$html = $html."".$d['wo_no']."XX";
		}
	}
	$htm = substr($html, 0,-2);	
	if($htm==""){$status = "OK";}
	else{$status = "NO";}	
	return $status;			
}

#Get PB No
function getpbno(){
	$year  = date('Y');
	$month = date('m');
	$pbno = mysql_fetch_row(mysql_query("SELECT SUBSTR((pb_no),12,4) FROM permintaan_barang ORDER BY id DESC LIMIT 1"));
	$seq   = (int)$pbno[0]+1;
	 if ($seq < 10)   $seq = "000".$seq;
	elseif($seq < 100)  $seq = "00".$seq;
	elseif($seq < 1000) $seq = "0".$seq;
	else $seq;
	$lm = mysql_fetch_row(mysql_query("SELECT SUBSTR((pb_no),9,2) FROM permintaan_barang ORDER BY id DESC LIMIT 1"));
	$last_month = $lm[0];
	$getcode = get_code();
	if($last_month == $month){
		$lps_no = "PBR-$year$month-$seq/$getcode";
	}else{
		$lps_no = "PBR-$year$month-0001/$getcode";
	}	
	return $lps_no;
}
function get_pono_chinli($tipe){
	if($tipe=="Sales"){
		$tipes = "SO";
	}else{
		$tipes = "PO";
	}
	$year   = date('Y');
	$month  = date('m');
	$lno = mysql_fetch_array(mysql_query("SELECT SUBSTR(po_no,7,6) regno FROM purchase_order WHERE po_type='$tipe' ORDER BY id DESC LIMIT 0,1"));
	$lyear = mysql_fetch_array(mysql_query("SELECT YEAR(po_date) thn FROM purchase_order WHERE po_type='$tipe' ORDER BY id DESC LIMIT 0,1"));
	$lastno = $lno['regno'];
	$lastyear = $lyear['thn'];
	if(empty($lastno)){
		$seq = 1;
	}else{
		$seq   = (int)$lastno+1;
	}
	if ($seq < 10)          $seq = "00000".$seq;
    elseif($seq < 100)      $seq = "0000".$seq;
    elseif($seq < 1000)     $seq = "000".$seq;
	elseif($seq < 10000)    $seq = "00".$seq;
	elseif($seq < 100000)   $seq = "0".$seq;
    else $seq;
	if($lastyear == $year){
		$regno = "".substr($year,2,2)."".$month."".$seq;
    }else{
		$regno = "".substr($year,2,2)."".$month."000001";
    }
	return $regno;
}
function get_wonb($month,$year){
	#$year   = date('Y');
	#$month  = date('m');
	$lno = mysql_fetch_array(mysql_query("SELECT SUBSTR(wonb_no,7,6) regno FROM workorder_nonbom WHERE MONTH(wonb_date)='$month' AND YEAR(wonb_date)='$year' ORDER BY SUBSTR(wonb_no,7,6) DESC LIMIT 0,1"));
	$lmonth = mysql_fetch_array(mysql_query("SELECT MONTH(wonb_date) bln FROM workorder_nonbom WHERE MONTH(wonb_date)='$month' AND YEAR(wonb_date)='$year' ORDER BY SUBSTR(wonb_no,7,6) DESC LIMIT 0,1"));
	$lastno = $lno['regno'];
	$lastmonth = $lmonth['bln'];
	if(empty($lastno)){
		$seq = 1;
	}else{
		$seq   = (int)$lastno+1;
	}
	if ($seq < 10)          $seq = "00000".$seq;
    elseif($seq < 100)      $seq = "0000".$seq;
    elseif($seq < 1000)     $seq = "000".$seq;
	elseif($seq < 10000)    $seq = "00".$seq;
	elseif($seq < 100000)   $seq = "0".$seq;
    else $seq;
	if(!empty($lno['regno'])){
		$regno = "WON".substr($year,2,2)."".$month."".$seq;
    }else{
		$regno = "WON".substr($year,2,2)."".$month."000001";
    }
	return $regno;
}
?>