<?php
require '../conexao/conexao.php';

//$mes = '09';
//$ano = '2023';
//$filial = '2,4';
//$vendedor = '51';
$ano = $_GET['ano'] ?? null;
$mes = $_GET['mes'] ?? null;
$filial = $_GET['filial'] ?? null;

//Todal QTD Requisições
  $qry_qtdrequis = "select count(Distinct(rm.cdfil||'-'||rm.nrrqu))soma from fc17100 rm, fc17200 tar, fc08000 fun
                    where rm.cdfil = tar.cdfil and
                    rm.nrrqu = tar.nrrqu and
                    tar.cdfun = fun.cdfun and
                    rm.cdfil = fun.cdcon and
                    tar.tptar = 'R' and
                    rm.vrrcb <> '0'"; 
                    
                    
if ($ano && $ano != "Todos") {
  $qry_qtdrequis .= " AND EXTRACT(YEAR FROM rm.dtope) = " . intval($ano);
}
if ($mes && $mes != "Todos") {
  $qry_qtdrequis .= " AND EXTRACT(MONTH FROM rm.dtope) = " . intval($mes);
}


if ($filial && $filial != "Todos") {
  $qry_qtdrequis .= " AND tar.cdcon in (" . ($filial). ")";
}

if ($vendedor !== null) {
  $qry_qtdrequis .= " AND fun.cdfun = '" . $vendedor . "'";
}


  $qryqtdrequis = ibase_query ($connection, $qry_qtdrequis);
  $row100 = ibase_fetch_assoc($qryqtdrequis);
  $qtdrequis = $row100["SOMA"];
  //echo $qry_qtdrequis;
  //QTD Unids Marca Propria
  $qry_qtdmarca = "select  sum(i.quant) as soma from fc14100 i, fc03000 p, fc14000 c ";
  if ($vendedor !== null) {
    $qry_qtdmarca .= "JOIN fc08000 fun ON c.cdconre = fun.cdcon AND c.cdfunre = fun.cdfun";
  } 
  $qry_qtdmarca .=   " where i.cdpro = p.cdpro and
                   c.nrcpm = i.nrcpm and
                   c.cdfil = i.cdfil and
				          p.cdpro not in (24663,20265) and
                   p.setor in (276,330)";


if ($ano && $ano != "Todos") {
$qry_qtdmarca .= " AND EXTRACT(YEAR FROM c.dtpagefe) = " . intval($ano);
}
if ($mes && $mes != "Todos") {
$qry_qtdmarca .= " AND EXTRACT(MONTH FROM c.dtpagefe) = " . intval($mes);
}

if ($filial && $filial != "Todos") {
$qry_qtdmarca .= " AND c.cdconre in (" . ($filial). ")";
}

if ($vendedor !== null) {
  $qry_qtdmarca .= " AND fun.cdfun = '" . $vendedor . "'";
}


//echo $qry_qtdmarca;

  $qryqtdmarca = ibase_query ($connection, $qry_qtdmarca);
  $row101 = ibase_fetch_assoc($qryqtdmarca);
  $qtdmarca = $row101["SOMA"];
      
  //QTD Unids Marca Propria + Revenda
  $qry_qtdmarcarevendaproria = "select sum(i.quant) as soma from fc14100 i, fc03000 p, fc14000 c ";
  if ($vendedor !== null) {
    $qry_qtdmarcarevendaproria .= "JOIN fc08000 fun ON c.cdconre = fun.cdcon AND c.cdfunre = fun.cdfun";
  } 
  $qry_qtdmarcarevendaproria .=   " where i.cdpro = p.cdpro and
                          c.nrcpm = i.nrcpm and
                          c.cdfil = i.cdfil and
						  p.cdpro not in (24663,20265)";

if ($ano && $ano != "Todos") {
$qry_qtdmarcarevendaproria .= " AND EXTRACT(YEAR FROM c.dtpagefe) = " . intval($ano);
}
if ($mes && $mes != "Todos") {
$qry_qtdmarcarevendaproria .= " AND EXTRACT(MONTH FROM c.dtpagefe) = " . intval($mes);
}

if ($filial && $filial != "Todos") {
  $qry_qtdmarcarevendaproria .= " AND c.cdconre in (" . ($filial). ")";
}


if ($vendedor !== null) {
  $qry_qtdmarcarevendaproria .= " AND fun.cdfun = '" . $vendedor . "'";
}




  $qryqtdmarcarevenda = ibase_query ($connection, $qry_qtdmarcarevendaproria);
  $row102 = ibase_fetch_assoc($qryqtdmarcarevenda);
  $qtdmarcarevenda = $row102["SOMA"];
  
  
  //QTD Unids Marca Propria + Revenda - ALGUNS PRODUTOS
  $qry_qtdmarcarevenda = "select sum(i.quant) as soma from fc14100 i, fc03000 p, fc14000 c ";
  if ($vendedor !== null) {
    $qry_qtdmarcarevenda .= "JOIN fc08000 fun ON c.cdconre = fun.cdcon AND c.cdfunre = fun.cdfun";
  } 
  $qry_qtdmarcarevenda .= " where i.cdpro = p.cdpro and
                          c.nrcpm = i.nrcpm and
                          c.cdfil = i.cdfil and
						  p.cdpro not in (24663,20265)"
							;

if ($ano && $ano != "Todos") {
  $qry_qtdmarcarevenda .= " AND EXTRACT(YEAR FROM c.dtpagefe) = " . intval($ano);
  }
  if ($mes && $mes != "Todos") {
  $qry_qtdmarcarevenda .= " AND EXTRACT(MONTH FROM c.dtpagefe) = " . intval($mes);
  }
        
  if ($filial && $filial != "Todos") {
    $qry_qtdmarcarevenda .= " AND c.cdconre in (" . ($filial). ")";
}

if ($vendedor !== null) {
  $qry_qtdmarcarevenda .= " AND fun.cdfun = '" . $vendedor . "'";
}


  $qryqtdmarcarevenda = ibase_query ($connection, $qry_qtdmarcarevenda);
  $row104 = ibase_fetch_assoc($qryqtdmarcarevenda);
  $qtdmarcarevendamenosprod = $row104["SOMA"];
  
  //Calculo aproveitamento...
  
  //$geral = number_format((($qtdmarcarevendamenosprod/$qtdrequis)*100), 2, ',', '.');
  //$marca = number_format((($qtdmarca/$qtdrequis)*100), 2, ',', '.');
  $geral = ($qtdrequis != 0) ? number_format((($qtdmarcarevendamenosprod / $qtdrequis) * 100), 2, ',', '.') : 0;
$marca = ($qtdrequis != 0) ? number_format((($qtdmarca / $qtdrequis) * 100), 2, ',', '.') : 0;

// Agora você pode usar $geral e $marca em sua lógica ou impressão.


  //echo 'geral '. $geral;
 // echo ' marca '. $marca;







  
  ?>