<?php
include "../../include/config.php";
include "../../function/inc_function.php";
#variable
$return_arr = array();
$fromdate = get_startdate();
$todate   = get_enddate();
$start_date_m2year = start_date_m2year($fromdate);
$qd = mysql_query("SELECT a.id, a.prod_code, a.prod_name, a.prod_type, a.prod_sat, a.prod_cat, 
					ifnull(b.qty,0) as beg_qty, 
					(ifnull(b.qty,0) + ifnull(c.qty,0) + ifnull(h.qty,0) + ifnull(i.qty,0) - ifnull(l.qty,0)) as temp_qty,
					ifnull(k.qty,0) as in_qty,
					(ifnull(d.qty,0) + ifnull(g.qty,0)+ ifnull(j.qty,0)) as out_qty,
					e.qty as adj_qty, 0 as stock_opname, CONCAT_WS(' - ', a.prod_code, a.prod_name) codname	
					FROM product a             

					LEFT JOIN (SELECT stock_prodid, SUM(stock_qty) as qty 
					FROM stock 
					WHERE stock_date='$fromdate' 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 '$fromdate' and '$todate' 
					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 '$fromdate' and '$todate'
					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 '$fromdate' and '$todate'
					group by adjd_prodid) AS e ON e.adjd_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 '$fromdate' and '$todate'
					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
					INNER JOIN product c ON c.id=a.incd_prodid AND prod_cat IN(3,4)
					where b.inch_status=20 and inch_docdate between '$fromdate' and '$todate' 
					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 '$fromdate' and '$todate' 
					group by kv_prodid) AS i ON i.kv_prodid=a.id

					LEFT JOIN (SELECT a.kvd_prodid, sum(a.kvd_qty) as qty 
					from konversi_external_detail a 
					INNER JOIN konversi_external 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 j ON j.kvd_prodid=a.id

					LEFT JOIN (SELECT kv_prodid, sum(kv_qty) as qty 
					from konversi_barangjadi 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) as qty 
					from konversi_barangjadi_detail a 
					INNER JOIN konversi_barangjadi 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

					WHERE CONCAT_WS(' - ', a.prod_code, a.prod_name) LIKE '%" . mysql_real_escape_string($_GET['term']) . "%' AND prod_cat = 3
						 ORDER BY a.prod_code ASC");

while ($dd = mysql_fetch_array($qd)) {
    #Summary   
	$stock = number_format($dd['temp_qty'], 2, '.', ''); #number_format($dd['beg_qty'], 2, '.', '') + number_format($dd['in_qty'], 2, '.', '') - number_format($dd['tpout_qty'], 2, '.', '') + number_format($dd['adj_qty'], 2, '.', '');
    $price  = 0; #get_unitprice_bj($fromdate_m2year, $todate, $dd['id']);
    $tprice = $stock * $price;

    #if($stock>0){
    $row_array["value"]         = $dd['codname'] . "=>" . $stock;
    $row_array["prod_id"]       = $dd['id'];
    $row_array["prod_code"]     = $dd['prod_code'];
    $row_array["prod_sat"]      = $dd['prod_sat'];
    $row_array["prod_stock"]    = $stock;
    $row_array["priceunit"]     = number_format($price, 2, '.', '');
    $row_array["prod_name"]     = $dd['prod_name'];
    array_push($return_arr, $row_array);
    #}
}
echo json_encode($return_arr);
