<?php
	include "../../include/config.php";
    include "../../function/inc_function.php";    
	#variable
	$return_arr = array();
	$start_date = get_startdate();
	$end_date   = get_enddate();
	$fromdate = get_startdate();
	$todate   = get_enddate();
	$bulan	 	= substr($start_date,5,2);
	$tahun  	= substr($start_date,0,4);
	$year_sto   = substr($end_date,0,4);
	$month_sto  = substr($end_date,5,2);
	/*$data =mysql_query("CALL spvw_cekstockbb('$start_date','$end_date','$bulan','$tahun','%" . mysql_real_escape_string($_GET['term']) . "%','$month_sto','$year_sto')");
	while($line = mysql_fetch_array($data, MYSQL_ASSOC)){
		$row_array["value"]        = $line["prod_code"]." => ".$line["prod_name"]." Stock = ".number_format($line["end_qty"],2,'.','');
		$row_array["prod_id"] 	   = $line["id"];
		$row_array["prod_sat"]     = $line["prod_sat"];
		$row_array["prod_stock"]   = number_format($line["end_qty"],2,'.','');
		array_push($return_arr, $row_array);
	}
	echo json_encode($return_arr);
	$data =mysql_query("SELECT * FROM product WHERE CONCAT_WS(' - ', prod_code, prod_name) LIKE '%" . mysql_real_escape_string($_GET['term']) . "%' AND prod_cat IN(1,6) ORDER BY prod_code ASC");
	while($line = mysql_fetch_array($data, MYSQL_ASSOC)){
		$stock = get_stock($start_date,$end_date,$bulan,$tahun,$line['id'],$line['prod_cat'],$inv);		
		$price = get_pricebb($line['id'],$start_date,$end_date,$inv);		
		$row_array["value"]        = $line["prod_code"]." => ".$line["prod_name"]." Stock = ".number_format($stock,5,'.','')." Price = ".number_format($price,5,'.','');
		$row_array["prod_id"] 	   = $line["id"];
		$row_array["prod_sat"]     = $line["prod_sat"];
		$row_array["prod_stock"]   = number_format($stock,5,'.','');
		$row_array["prod_price"]   = number_format($price,5,'.','');
		array_push($return_arr, $row_array);
	}
	echo json_encode($return_arr);*/
	
	$data =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, CONCAT_WS(' - ', b.prod_code, b.prod_name, b.prod_spec, b.prod_type, b.prod_color) codename 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, b.prod_spec, b.prod_type, b.prod_color 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
					INNER JOIN product c ON c.id=a.prodid AND prod_cat IN(1,6,7)
					INNER JOIN outgoing_detail d ON d.outd_prodid=a.prodid AND d.outd_hid=b.id
					WHERE b.outh_status=20 AND a.log_status=20 AND b.outh_docdate BETWEEN '$fromdate' AND '$todate'
					UNION ALL
					SELECT trfd_prodid prodid, trfd_doctype doctype , trfd_docno docno, trfd_docdate docdate, 0 inqty, trfd_qty outqty, 0 price 
					FROM transfer_temp WHERE trfd_category=1 AND (trfd_inputby<>'' AND trfd_inputby is not null)
					UNION ALL
         			SELECT a.adjd_prodid prodid, a.adjd_doctype doctype , a.adjd_docno docno, a.adjd_docdate docdate, adjd_qty inqty, 0 outqty, 0 price 
					FROM adjusment_detail a
					INNER JOIN adjusment b ON b.id=a.adjd_hid            
					WHERE b.adjh_status=20 AND b.adjh_date BETWEEN '$fromdate' AND '$todate') a
					INNER JOIN product b ON b.id=a.prodid
					WHERE b.prod_cat IN(1,5,6,7) GROUP BY a.prodid, a.doctype, a.docno, a.docdate) b
					WHERE (b.inqty-b.outqty) > 0 AND CONCAT_WS(' - ', b.prod_code, b.prod_name) LIKE '%" . mysql_real_escape_string($_GET['term']) . "%' 
					ORDER BY b.docdate ASC;");
	while($line = mysql_fetch_array($data, MYSQL_ASSOC)){
		#$stock = get_stock($start_date,$end_date,$bulan,$tahun,$line['id'],$line['prod_cat'],$inv);		
		#$price = get_pricebb($line['id'],$start_date,$end_date,$inv);		
		$row_array["value"]        = $line["codename"]." => ".$line["doctype"]." => ".$line["docno"]." => ".$line["docdate"]." Stock = ".number_format($line["endqty"],5,'.','');
		$row_array["prod_id"] 	   = $line["prodid"];
		$row_array["prod_sat"]     = $line["prod_sat"];
		$row_array["prod_stock"]   = number_format($line["endqty"],2,'.','');
		$row_array["prod_price"]   = number_format($line["price"],2,'.','');
		$row_array["prod_doctype"] = $line["doctype"];
		$row_array["prod_docno"]   = $line["docno"];
		$row_array["prod_docdate"] = $line["docdate"];
		array_push($return_arr, $row_array);
	}
	echo json_encode($return_arr);
?>