<?php
if (!isset($_SERVER["HTTP_HOST"])) {
  //parse_str($argv[1], $_GET);
  parse_str($argv[1], $_POST);
}

require 'acesso.php';
	$res = array();

	//$tipo_venda = $_POST['tipo_venda'];
	$tipo_venda = 0;
	$id_firma    = $_POST['id_firma'];
	$id_device = $_POST['id_device'];

	/*
	 * arvrcomp = vr_custo
	 * arvratad = vr_atacado
	 * arvrvend = vr_varejo
	 * arqtdest = qtd_loja
	 * arqtdepo = qtd_deposito
	 * 
	 */
	
	// SEM TABELA DE PREÇOS
	$query = "SELECT a.arcodigo, a.arunidad, a.arcodmar, b.arnommar, a.arcodgru, c.arnomgru, a.arrefere, a.arcdbar2, 
					a.ardesnot, a.ardescri, a.arvrlemb, a.ardesconto_prod, a.ardesemb, a.aruniemb, COALESCE( ardesnot, ardescri ) AS descricao, 
					CAST( MID( a.arvrcomp, ( a.artabatu - 1) * 16 + 1, 16 ) * 1 AS DECIMAL( 16,2 ) ) AS arvrcomp,
					CAST( MID( a.arvratad, ( a.artabatu - 1) * 16 + 1, 16 ) * 1 AS DECIMAL( 16,2 ) ) AS arvratad,
					CAST( MID( a.arvrvend, ( a.artabatu - 1) * 16 + 1, 16 ) * 1 AS DECIMAL( 16,2 ) ) AS arvrvend,
					CAST( MID( a.arqtdest, ( a.artabatu - 1) * 9 + 1 , 9 ) * 1 AS DECIMAL( 9,3 ) ) AS arqtdest, IFNULL(ar_data_venc, '') AS ardtvenc,
					CAST( MID( a.arqtdepo, ( a.artabatu - 1) * 9 + 1 , 9 ) * 1 AS DECIMAL( 9,3 ) ) AS arqtdepo,
					a.arstqmin, a.arfracio, a.ardtinia, a.ardtinic, a.arcodbar, a.arperdesc_atacado, a.ardtfina, a.ardtfinc, a.arvrproa, 
					a.arvrproc, a.arvrproc_emb FROM estoque a JOIN marcas b ON a.arcodmar = b.arcodmar LEFT JOIN grupos c ON a.arcodgru = c.arcodgru ";
	$query = "SELECT a.arcodigo, a.arunidad, a.arcodmar, b.arnommar, a.arcodgru, c.arnomgru, a.arrefere, a.arcdbar2, 
					a.ardesnot, a.ardescri, a.arvrlemb, a.ardesconto_prod, a.ardesemb, a.aruniemb, COALESCE( ardesnot, ardescri ) AS descricao, 
					CAST( MID( a.arvrcomp, ( a.artabatu - 1) * 16 + 1, 16 ) * 1 AS DECIMAL( 16,2 ) ) AS arvrcomp,
					CAST( MID( a.arvratad, ( a.artabatu - 1) * 16 + 1, 16 ) * 1 AS DECIMAL( 16,2 ) ) AS arvratad,
					CAST( t.arvalor_tabela AS DECIMAL( 16,2 ) ) AS arvrvend,
					CAST( MID( a.arqtdest, ( a.artabatu - 1) * 9 + 1 , 9 ) * 1 AS DECIMAL( 9,3 ) ) AS arqtdest, IFNULL(ar_data_venc, '') AS ardtvenc,
					CAST( MID( a.arqtdepo, ( a.artabatu - 1) * 9 + 1 , 9 ) * 1 AS DECIMAL( 9,3 ) ) AS arqtdepo,
					a.arstqmin, a.arfracio, a.ardtinia, a.ardtinic, a.arcodbar, a.arperdesc_atacado, a.ardtfina, a.ardtfinc, a.arvrproa, 
					a.arvrproc, a.arvrproc_emb FROM estoque a LEFT JOIN marcas b ON a.arcodmar = b.arcodmar LEFT JOIN grupos c ON a.arcodgru = c.arcodgru LEFT JOIN tabela_precos_itens t ON t.arcodigo = a.arcodigo AND t.arcodigo_tabela = '0001' ";
	
	if (isset($_POST['contagem'])) {
		if ($tipo_venda == 1) {
			$ret = $con->prepare("SELECT COUNT(arcodigo) AS contagem FROM estoque WHERE arlataca = 0 AND sr_deleted != 'T'
					AND ardesativa_produ = 0 AND (CAST( MID( arvratad, ( artabatu - 1) * 16 + 1, 16 ) * 1 AS DECIMAL( 16,2 ) ) > 0)");
			
		} else {
			$ret = $con->prepare("SELECT COUNT(arcodigo) AS contagem FROM estoque WHERE sr_deleted != 'T' 
					AND ardesativa_produ = 0 AND (ardesnot IS NOT NULL or ardescri IS NOT NULL)");
		}
	} else if (isset($_POST['linha'])) {
		$linha = $_POST['linha'];
		if ($linha == -1) {
			$ret = $con->prepare($query . "WHERE a.arlataca = 0 AND a.sr_deleted != 'T' AND a.ardesativa_produ = 0 HAVING descricao
					IS NOT NULL ORDER BY descricao ");
			/*$ret = $con->prepare($query . "WHERE a.arlataca = 0 AND a.sr_deleted != 'T' AND a.ardesativa_produ = 0 AND
					(CAST( MID( a.arvratad, ( a.artabatu - 1) * 16 + 1, 16 ) * 1 AS DECIMAL( 16,2 ) ) > 0) HAVING descricao
					IS NOT NULL ORDER BY descricao ");*/
		} else {
			if ($tipo_venda == 1) {
				$ret = $con->prepare($query . "WHERE a.arlataca = 0 AND a.sr_deleted != 'T' AND a.ardesativa_produ = 0 HAVING descricao
						IS NOT NULL ORDER BY descricao LIMIT $linha, 100");
				/*$ret = $con->prepare($query . "WHERE a.arlataca = 0 AND a.sr_deleted != 'T' AND a.ardesativa_produ = 0 AND
						(CAST( MID( a.arvratad, ( a.artabatu - 1) * 16 + 1, 16 ) * 1 AS DECIMAL( 16,2 ) ) > 0) HAVING descricao
						IS NOT NULL ORDER BY descricao LIMIT $linha, 100");*/
			} else {
				$ret = $con->prepare($query . "WHERE a.sr_deleted != 'T' AND a.ardesativa_produ = 0 HAVING descricao IS NOT NULL ORDER BY descricao LIMIT $linha, 100
				");
			}
		}
	}
	$ret->execute();
	$mensagem = $ret->errorInfo();
	if($ret->rowCount() > 0) {
		$res["estoque"] = array();
		while ($row = $ret->fetch(PDO::FETCH_ASSOC)) {
			array_push($res["estoque"], $row);
		}
		$res["sucesso"] = 1;
		$res["query"] = $query;
		echo json_encode($res);
	} else {
	    $res["sucesso"] = 0;
	    $res["mensagem"] = $mensagem[2];
		echo json_encode($res);
	}
	
	$con = null;
?>

