 <?php  
  function item_qty($get_itemid,$enddate=false,$fromdate=false) {
    $coid = $GLOBALS['coid'];
  	if ($enddate===false) {
  		$enddate = $GLOBALS['date'];
  	}
  	
  	if ($fromdate!==false) {
  		$item = mysqli_query($GLOBALS['conn'],"SELECT 
  			SUM(COALESCE(CASE WHEN plus = '+' AND date(datetime)<'$fromdate' THEN qty END,0)) pr_in_qty,
  			SUM(COALESCE(CASE WHEN plus = '-' AND date(datetime)<'$fromdate' THEN qty END,0)) pr_out_qty,
  			SUM(COALESCE(CASE WHEN plus = '+' AND date(datetime)<'$fromdate' THEN qty END,0))-SUM(COALESCE(CASE WHEN plus = '-' AND date(datetime)<'$fromdate' THEN qty END,0)) pr_stock_qty,
  			SUM(COALESCE(CASE WHEN plus = '+' THEN qty END,0)) in_qty,
  			SUM(COALESCE(CASE WHEN plus = '-' THEN qty END,0)) out_qty,
  			SUM(COALESCE(CASE WHEN plus = '+' THEN qty END,0))-SUM(COALESCE(CASE WHEN plus = '-' THEN qty END,0)) stock_qty
  		    FROM order_list WHERE coid='$coid' AND active=1 AND pro_id = '$get_itemid' AND date(datetime) <='$enddate' GROUP BY pro_id") or die(mysqli_error($GLOBALS['conn']));
  	} else {

      $item = mysqli_query($GLOBALS['conn'],"SELECT 
        SUM(COALESCE(CASE WHEN plus = '+' AND date(datetime)<'$enddate' THEN qty END,0)) pr_in_qty,
        SUM(COALESCE(CASE WHEN plus = '-' AND date(datetime)<'$enddate' THEN qty END,0)) pr_out_qty,
        SUM(COALESCE(CASE WHEN plus = '+' AND date(datetime)<'$enddate' THEN qty END,0))-SUM(COALESCE(CASE WHEN plus = '-' AND date(datetime)<'$enddate' THEN qty END,0)) pr_stock_qty,
        SUM(COALESCE(CASE WHEN plus = '+' THEN qty END,0)) in_qty,
        SUM(COALESCE(CASE WHEN plus = '-' THEN qty END,0)) out_qty,
        SUM(COALESCE(CASE WHEN plus = '+' THEN qty END,0))-SUM(COALESCE(CASE WHEN plus = '-' THEN qty END,0)) stock_qty
          FROM order_list WHERE coid='$coid' AND active=1 AND pro_id = '$get_itemid' AND date(datetime) <='$enddate' GROUP BY pro_id") or die(mysqli_error($GLOBALS['conn']));      
  	}
     return mysqli_fetch_assoc($item);
   }

?>