<?php
	require_once "conexao.php";
	try{
		$num_loja = $_SESSION['loja'];

		$__FROM   = '';
		$__INNER2 = '';
		if($num_loja == 1){
			$__FROM   = '[UNI].[dbo].[ITEQV_GIM]'; // voltar para o banco UNI
			$__INNER2 = '[SOF].[dbo].[ITENS_GIM]';
		} else if ($num_loja == 6){
			$__FROM   = '[SOF].[dbo].[ITEQV08_GIM]';
			$__INNER2 = '[SOF].[dbo].[ITENS08_GIM]';
		} else {
			$__FROM   = '[SOF].[dbo].[ITEQV0'.$num_loja.'_GIM]';
			$__INNER2 = '[SOF].[dbo].[ITENS0'.$num_loja.'_GIM]';
		}
		$query_sql = "SELECT
						'$num_loja' AS SQL_LOJA,
						I.SQL_IQCODBUS AS SQL_IQCODBUS,
						SQL_IDESCRI
					FROM $__FROM I
					INNER JOIN (
						SELECT MAX(sql_iqanomes) as REF_ANOMES, SQL_IQCODBUS AS REF_CODIGO FROM $__FROM GROUP BY SQL_IQCODBUS
					) R ON I.SQL_IQCODBUS = R.REF_CODIGO AND I.SQL_IQANOMES = R.REF_ANOMES
					INNER JOIN $__INNER2 M ON R.REF_CODIGO = M.SQL_ICODBUS
					WHERE (SQL_IDESCRI LIKE '%".$_POST['valor']."%' OR SQL_ICODBUS LIKE '%".$_POST['valor']."%')
		";

		$Conexao  = Conexao::getConnection($num_loja);
		$query    = $Conexao->query($query_sql);
		$response_produtos = $query->fetchAll();
		
	}catch(Exception $e){

		$response_produtos = false;
		exit;

	}