<?php
global $con;
include_once('functions.php');
if(stripos($_SERVER['SCRIPT_NAME'],"/admin/")===false?false:true){
	include_once("../connect.php");
	
}else{
	include_once("connect.php");
	
}

$function = $_REQUEST['f'];
if($function == "getZeroedOutDetail"){	
	getZeroedOutDetail($_REQUEST['line'], $_REQUEST['group']);
}
function getZeroedOutDetail($Line, $Group){
	global $con;
	$query = "SELECT count(*) Count,  MAX(ORDER_HEAD.OrderDate) OrderDate,ORDER_DETAIL.`Group`, ORDER_DETAIL.Line, ORDER_DETAIL.SKU, ORDER_DETAIL.Description, ORDER_DETAIL.Notes, Qty, SUM(OrderQty) Ordered FROM ORDER_HEAD JOIN ORDER_DETAIL USING(OrderID) JOIN INVENTORY ON(INVENTORY.Group = ORDER_DETAIL.Group AND INVENTORY.Line = ORDER_DETAIL.Line AND INVENTORY.SKU = ORDER_DETAIL.SKU) WHERE OrderDate BETWEEN DATE_ADD(NOW(), INTERVAL -1 YEAR) AND NOW() AND ORDER_DETAIL.Line NOT LIKE 'MIS' AND ORDER_DETAIL.Line = '$Line' AND ORDER_DETAIL.Group = '$Group' and INVENTORY.Qty <=0 GROUP BY `Group`, Line, SKU  ORDER BY Count DESC, `Group`, Line, SKU";
	$result = mysqli_query($con, $query);
	$detailArray = [];
	while($row = mysqli_fetch_array($result)){
		$row['OrderDate'] = date("m/d/Y h:i a", strtotime($row['OrderDate']));
		array_push($detailArray,$row);
	}
	echo json_encode($detailArray);
}