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

  $lojas = '2,4';
  $mes = '09';
  $ano = '2023';
  $ultimo_dia = date("t", mktime(0,0,0,$mes,'01',$ano));
 $ultimo_dia = $ano."-".$mes."-".$ultimo_dia;
  $primeiro_dia = $ano.'-'.$mes.'-'.'01';
  $ultimodia = cal_days_in_month(CAL_GREGORIAN, $mes , $ano);  
  $vendedor = '55';

  
  //Venda Vendedores Manipulado
  
  $qry_VenManiVendedor = "select fun.nomefun, fun.cdfun from fc08000 fun
                  where 
                  fun.cdcon in (".$lojas.") and
                  fun.cdfun = '".$vendedor."'
                  Group by fun.nomefun, fun.cdfun
                  order by fun.nomefun, fun.cdfun";
  $qryVenManiVendedor = ibase_query ($connection, $qry_VenManiVendedor);
  $row = ibase_fetch_assoc($qryVenManiVendedor);
  //$manipulado = $row["SOMA"];
  $nome = $row["NOMEFUN"]; 
  
  /// TROQUEI rm.cdfil = fun.cdcon and
	  
  
   
  

?>

<html>
<head>
<title>Painel</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>

  <font size="2" face="Geneva, Arial, Helvetica, sans-serif"> <?php echo $mes."/".$ano; ?> 
  <br>
  <br>
  </font></p>
<table width="700" border="0" align="center">
  <tr>
    <td><div align="center"><strong>Vendedor:  <?php echo $nome;?> </strong></div></td>
  </tr>
  
  
<div align="center"> 
  <?php 

  $ultimo_dia = date("t", mktime(0,0,0,$mes,'01',$ano));
  $ultimo_dia = $ano."-".$mes."-".$ultimo_dia;
  $primeiro_dia = $ano.'-'.$mes.'-'.'01';
  $ultimodia = cal_days_in_month(CAL_GREGORIAN, $mes , $ano);  

  //Manipulado
  $i = 1;
  while ($i <= $ultimodia) {
    //Quantidade de Formulas...
    $qry_manipulado = "select SUM(rm.vrrcb) as valor from fc17100 rm, fc17200 tar, fc08000 fun
                  where rm.cdfil = tar.cdfil and
                  rm.nrrqu = tar.nrrqu and
                  rm.dtope >= '".$ano."-".$mes."-".$i."'  and
                  rm.dtope <= '".$ano."-".$mes."-".$i."' and
                  tar.cdcon in (".$lojas.") and
                  tar.cdfun = fun.cdfun and
                  rm.cdfil = fun.cdcon and
                  tar.tptar = 'R' and
				  rm.vrrcb <> '0' and
				  fun.cdfun = '".$vendedor."'";
    $qrymanipulado = ibase_query ($connection, $qry_manipulado); 
    $row0 = ibase_fetch_assoc($qrymanipulado);
	$matriz[$i][0] = $i."-".$mes."-".$ano;
	if ($row0["VALOR"] == ""){
	$matriz[$i][1] = "0";
	} else{
	$matriz[$i][1] = $row0["VALOR"];
	}
	$i = $i+1;	
  }
  
  //Revenda
  $r = 1;
  while ($r <= $ultimodia) {
    //Quantidade de Formulas...
    $qry_revenda = "select SUM(p.vrtot - p.vrdsc - p.vrdscv + p.vrtxav) as valor  from fc14100 p, fc14000 cp, FC03000 pr, fc08000 fun
                   where p.cdfil = cp.cdfil and
                   p.nrcpm = cp.nrcpm and
                   p.cdpro = pr.cdpro and
                   cp.dtpagefe >= '".$ano."-".$mes."-".$r."' and
                   cp.dtpagefe <= '".$ano."-".$mes."-".$r."' and
                   pr.setor <> '330' and
                   cp.cdconre in (".$lojas.") and
				   cp.cdconre = fun.cdcon and
                   cp.cdfunre = fun.cdfun and
				   fun.cdfun = '".$vendedor."'";
    $qryrevenda = ibase_query ($connection, $qry_revenda); 
    $row1 = ibase_fetch_assoc($qryrevenda);
	$matriz1[$r][0] = $r."-".$mes."-".$ano;
	if ($row1["VALOR"] == ""){
	$matriz1[$r][1] = "0";
	} else{
	$matriz1[$r][1] = $row1["VALOR"];
	}
	$r = $r+1;	
  }
  
  //marca
  $m = 1;
  while ($m <= $ultimodia) {
    //Quantidade de Formulas...
    $qry_marca = "select SUM(p.vrtot - p.vrdsc - p.vrdscv + p.vrtxav) as valor  from fc14100 p, fc14000 cp, FC03000 pr, fc08000 fun
                  where p.cdfil = cp.cdfil and
                  p.nrcpm = cp.nrcpm and
                  p.cdpro = pr.cdpro and
                  cp.dtpagefe >= '".$ano."-".$mes."-".$m."' and
                  cp.dtpagefe <= '".$ano."-".$mes."-".$m."' and
                  pr.setor = '330' and
                  cp.cdconre in (".$lojas.") and
				  cp.cdconre = fun.cdcon and
                  cp.cdfunre = fun.cdfun and
				  fun.cdfun = '".$vendedor."'";	
    $qrymarca = ibase_query ($connection, $qry_marca); 
    $row2 = ibase_fetch_assoc($qrymarca);
	$matriz2[$m][0] = $m."-".$mes."-".$ano;
	if ($row2["VALOR"] == ""){
	$matriz2[$m][1] = "0";
	} else{
	$matriz2[$m][1] = $row2["VALOR"];
	}
	$m = $m+1;	
  }
      
  //Fecho as conexões
  ibase_free_result($qrymanipulado);
?>
  <p><br>
    
  </p>
  <table width="579" border="0" align="center">
    <tr> 
      <?php
echo "<div align='center'>APURAÇÃO DIÁRIA</div>";
?>
          <br>
          </strong></div></td>
    </tr>
    <tr> 
      <td width="273"><table width="273" border="0" align="center">
          <tr> 
            <td width="267" colspan="2"><div align="right"><strong>Manipulado</strong></div></td>
          </tr>
          <tr> 
            <td colspan="2"><table width="250" border="0" align="right">
                <tr> 
                  <td width="150"><strong>Data</strong></td>
                  <td width="100"><div align="right"><strong>Valor</strong></div></td>
                </tr>
                <?php
  $l = 1;
  $val = 0;
  while ($l <= $ultimodia) {
    echo"<tr> 
      <td width=\"87\">".$matriz[$l][0]."</td>
      <td width=\"89\"><div align=\"right\">".number_format($matriz[$l][1], 2, ',', '.')."</div></td>
    </tr>";
  $val = $val + $matriz[$l][1]; 	
  $l = $l+1;
  }
?>
                <tr> 
                  <td width="150">&nbsp;</td>
                  <td width="150"><div align="right"></div></td>
                </tr>
              </table></td>
          </tr>
        </table></td>
      <td width="149"><table width="125" border="0" align="center">
          <tr> 
            <td width="216" colspan="2"><div align="right"><strong>Revenda</strong></div></td>
          </tr>
          <tr> 
            <td colspan="2"><table width="120" border="0" align="center">
                <tr> 
                  <td width="100"><div align="right"><strong>Valor</strong></div></td>
                </tr>
                <?php
  $r = 1;
  $val1 = 0;
  while ($r <= $ultimodia) {
    echo"<tr> 
      <td width=\"89\"><div align=\"right\">".number_format($matriz1[$r][1], 2, ',', '.')."</div></td>
    </tr>";
  $val1 = $val1 + $matriz1[$r][1]; 	
  $r = $r+1;
  }
?>
                <tr> 
                  <td width="150"><div align="right"></div></td>
                </tr>
              </table></td>
          </tr>
        </table></td>
      <td width="143" colspan="2"><table width="125" border="0" align="center">
          <tr> 
            <td width="233" colspan="2"><div align="right"><strong>Marca Pr&oacute;pria</strong></div></td>
          </tr>
          <tr> 
            <td colspan="2"><table width="120" border="0" align="center">
                <tr> 
                  <td width="100"><div align="right"><strong>Valor</strong></div></td>
                </tr>
                <?php
  $m = 1;
  $val2 = 0;
  while ($m <= $ultimodia) {
    echo"<tr> 
      <td width=\"89\"><div align=\"right\">".number_format($matriz2[$m][1], 2, ',', '.')."</div></td>
    </tr>";
  $val2 = $val2 + $matriz2[$m][1]; 	
  $m = $m+1;
  }
?>
                <tr> 
                  <td width="150"><div align="right"></div></td>
                </tr>
              </table></td>
          </tr>
        </table></td>
    </tr>
  </table>
  
  </div>
</body>
</html>
