<?php
require 'acesso.php';
	$res = array();
	
	$id_firma    = $_POST['id_firma'];
	$id_device = $_POST['id_device'];
	
	/*$ret = $con->prepare("Select COALESCE(a.arcodgru, 'INEXISTENTE') AS arcodgru, COALESCE(b.arnomgru, 'INEXISTENTE') AS arnomgru FROM estoque a LEFT JOIN grupos b ON a.arcodgru = b.arcodgru 
			WHERE CAST( MID( arvratad, (artabatu-1)*16+1, 16 ) * 1 AS DECIMAL( 16,2 ) ) > 0 AND a.arcodgru IS NOT NULL AND 
			b.arnomgru IS NOT NULL GROUP BY a.arcodgru");*/
	$ret = $con->prepare(
		"SELECT
			COALESCE(a.arcodgru, 'INEXISTENTE') AS arcodgru, 
			COALESCE(b.arnomgru, 'INEXISTENTE') AS arnomgru 
		FROM 
			estoque a 
		LEFT JOIN grupos b ON a.arcodgru = b.arcodgru 
		LEFT JOIN (SELECT COUNT(*) AS qtd, arcodigo FROM tabela_precos_itens GROUP BY arcodigo) t ON a.arcodigo = t.arcodigo 
		WHERE 
			(CAST( MID( arvratad, (artabatu-1)*16+1, 16 ) * 1 AS DECIMAL( 16,2 ) ) > 0 OR t.qtd > 0) AND 
			a.arcodgru IS NOT NULL AND
			b.arnomgru IS NOT NULL GROUP BY a.arcodgru"
	);
	
	$ret->execute();
	$mensagem = $ret->errorInfo();
	if ($ret->rowCount() > 0) {
	    $res["grupos"] = array();
	    while ($row = $ret->fetch(PDO::FETCH_ASSOC)) {
	    	array_push($res["grupos"], $row);
	    }
	    $res["sucesso"] = 1;
	} else {
	    $res["sucesso"] = 0;
	    $res["mensagem"] = $mensagem[2];
	}
	echo json_encode($res);
	$con = null;
?>