<?php
require '../conexao/conexao.php';
require '../sessao.php';
require '../configuacoes.php';
$records_per_page = 30;
$current_page = 1;
$start_from = ($current_page - 1) * $records_per_page;
$hasResults = false;

 $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
	  
  
  

?>


  <?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);
?>


<!DOCTYPE html>
<html lang="pt-br">
<?php 
        $pageTitle = "Consultar Médico"; // Definir o título da página aqui
        include('../geral/header.php'); 
        ?>
    <body>

        <!-- Navigation Bar-->
        <?php 
         include('../geral/menu.php'); 
        ?>
        <!-- End Navigation Bar-->

        <!-- ============================================================== -->
        <!-- Start Page Content here -->
        <!-- ============================================================== -->

       <div class="wrapper candidato">
    <div class="container-fluid">
	<div class="card-box mt-3">
	<div class="row">
        <!-- Seção que você deseja adaptar -->
        <div class="col-12">
            <h6>Apuração Diária</h6>
            <table id="datatable-buttons" class="table table-striped table-bordered dt-responsive nowrap">
                <thead>
                    <tr class="text-center">
                        <th>Data</th>
                        <th>Manipulado</th>
                        <th>Revenda</th>
                        <th>Marca Própria</th>
                    </tr>
                </thead>
                <tbody>
                    <?php
                    $l = 1;
                    while ($l <= $ultimodia) {
                        echo "<tr class=\"text-center\">
							 <td width=\"87\">" . htmlspecialchars(date("d/m/Y", strtotime($matriz[$l][0]))) . "</td>
							<td width=\"89\"><div align=\"center\">R$ " .htmlspecialchars( number_format($matriz[$l][1], 2, ',', '.')) . "</div></td>
							<td width=\"89\"><div align=\"center\">R$ " . htmlspecialchars(number_format($matriz1[$l][1], 2, ',', '.')) . "</div></td>
							<td width=\"89\"><div align=\"center\">R$ " . htmlspecialchars(number_format($matriz2[$l][1], 2, ',', '.')) . "</div></td>
							</tr>";

                        $l = $l + 1;
                    }
                    ?>
                </tbody>
            </table>
        </div>
		</div>
        <!-- Fim da seção adaptada -->

        <!-- PAGINAÇÃO -->
        <div style="display: flex; justify-content: center;">
            <?php include('paginacao.php'); ?>
        </div>
        <!-- FIM PAGINAÇÃO -->
    </div>
</div>

        <!-- ============================================================== -->
        <!-- End Page content -->
        <!-- ============================================================== -->
     <!-- Footer Start -->
     <?php
            include('../geral/footer.php');  
            ?>
        <!-- end Footer -->

        <?php
        include('../geral/mensagens_modais.php');  
        ?>

        <!-- Vendor js -->
        <script src="../../assets/js/vendor.min.js"></script>

        <!-- knob plugin -->
        <script src="../../assets/libs/jquery-knob/jquery.knob.min.js"></script>
        <!-- App js-->
        <script src="../../assets/js/app.min.js"></script>
        
    </body>
</html>

<!-- No final do seu documento, antes dos scripts, adicione: -->
<script>
    document.querySelector("button[type=submit]").addEventListener("click", function(event) {
        let nomeMedico = document.querySelector("#nomeMedico").value;
        let ano = document.querySelector("#ano").value;
        let mes = document.querySelector("#mes").value;
        let situacao = document.querySelector("#situacao").value;

        if (!nomeMedico && !ano && !mes && !situacao) {
            event.preventDefault();
            alert("Ao menos um campo deve estar preenchido para realizar a consulta.");
        }
    });
</script>

<script src="../geral/javascript.js"></script>