<?php
session_start();

require_once 'gerador/SqlServer.php';
require_once 'script_php/ConexaoSql.php';
require_once 'script_php/RemotoInterno.php';
require_once 'script_php/SisNotasSQL.php';

$remoto = new  RemotoInterno();
$sqlServer = new SqlServer();

$datainicio = $_REQUEST['datainicio'];
$datafinal = $_REQUEST['datafinal'];

$data_obj2 = new DateTime($datainicio);
$data_formatada1 = $data_obj2->format('Y-d-m');

$data_obj3 = new DateTime($datafinal);
$data_formatada2 = $data_obj3->format('Y-d-m');

$dados = $sqlServer->buscarCompra($data_formatada1,$data_formatada2);

?>

<!DOCTYPE html>
<html>
<head>

<?php include_once 'head.html' ?>

<title></title>
</head>

<body>

<?php include_once 'header.php'; ?>

<h2>

<svg xmlns="http://www.w3.org/2000/svg" width="30" height="30" fill="currentColor" class="bi bi-database-fill-gear" viewBox="0 0 16 16">
  <path d="M8 1c-1.573 0-3.022.289-4.096.777C2.875 2.245 2 2.993 2 4s.875 1.755 1.904 2.223C4.978 6.711 6.427 7 8 7s3.022-.289 4.096-.777C13.125 5.755 14 5.007 14 4s-.875-1.755-1.904-2.223C11.022 1.289 9.573 1 8 1"/>
  <path d="M2 7v-.839c.457.432 1.004.751 1.49.972C4.722 7.693 6.318 8 8 8s3.278-.307 4.51-.867c.486-.22 1.033-.54 1.49-.972V7c0 .424-.155.802-.411 1.133a4.51 4.51 0 0 0-4.815 1.843A12 12 0 0 1 8 10c-1.573 0-3.022-.289-4.096-.777C2.875 8.755 2 8.007 2 7m6.257 3.998L8 11c-1.682 0-3.278-.307-4.51-.867-.486-.22-1.033-.54-1.49-.972V10c0 1.007.875 1.755 1.904 2.223C4.978 12.711 6.427 13 8 13h.027a4.55 4.55 0 0 1 .23-2.002m-.002 3L8 14c-1.682 0-3.278-.307-4.51-.867-.486-.22-1.033-.54-1.49-.972V13c0 1.007.875 1.755 1.904 2.223C4.978 15.711 6.427 16 8 16c.536 0 1.058-.034 1.555-.097a4.5 4.5 0 0 1-1.3-1.905m3.631-4.538c.18-.613 1.048-.613 1.229 0l.043.148a.64.64 0 0 0 .921.382l.136-.074c.561-.306 1.175.308.87.869l-.075.136a.64.64 0 0 0 .382.92l.149.045c.612.18.612 1.048 0 1.229l-.15.043a.64.64 0 0 0-.38.921l.074.136c.305.561-.309 1.175-.87.87l-.136-.075a.64.64 0 0 0-.92.382l-.045.149c-.18.612-1.048.612-1.229 0l-.043-.15a.64.64 0 0 0-.921-.38l-.136.074c-.561.305-1.175-.309-.87-.87l.075-.136a.64.64 0 0 0-.382-.92l-.148-.045c-.613-.18-.613-1.048 0-1.229l.148-.043a.64.64 0 0 0 .382-.921l-.074-.136c-.306-.561.308-1.175.869-.87l.136.075a.64.64 0 0 0 .92-.382zM14 12.5a1.5 1.5 0 1 0-3 0 1.5 1.5 0 0 0 3 0"/>
</svg>

Pedidos encontrados no Softcom</h2>
<p></p>

<table class="table table-striped">
    
    <thead class="table-dark" align="center">

        <th >ID_SOFT</th>
        <th>DATA EMISSÃO</th>
        <th>FABRICANTE</th>
        <th>AVISO ENTREGA</th>
        <th>FORNECEDOR</th>
        <th>LOJA</th>
        <th>OBSERVAÇÃO</th>
        <th>VALOR</th>
        <th>IMPORTAR</td>

    </thead>

    <?php



    $mysql = new SisNotasSQL();
    
    foreach($dados as $dados2){

        $data = $remoto->converterData($dados2[1]);

        $pedido = $mysql->BuscarPedido($conexao,$dados2[0]);
        
        if ($dados2[0] <> $pedido) {

            $valorpedido = 0;
            $valor =  $sqlServer->PrecoPedido($dados2[0]);
            foreach ($valor as $dados3){ $valorpedido = $dados3[0] + $valorpedido;
                $fornecedor = $dados3[1];
            }
        ?>








    <tr>
        <td align="center"><?php echo $dados2[0]; ?></td>
        <td align="center"><?php echo $data; ?></td>
        <td align="center"><?php echo $dados2[2]; ?></td>
        


<form action="script_php/importarPedidosSoftcom.php" method="GET">



<td align="center">

    <input type="checkbox" name="mensagem" value="sim">
</td>

<td align="left">
<select class=" form_forn" name="fornecedor">



<?php
      //buscar fornecedor no softcom
      $listagem = $sqlServer->BuscarFornecedor();
    
     foreach ($listagem AS $linha)
      {
        $selected = ($linha[1] == $fornecedor) ? 'selected' : '';

          $fornecedorNovo = $linha[1];
          $cnpj_fornecedor = $linha[1];
      ?>

        <option value="<?= $linha[1]; ?>" <?php echo $selected;?>>
        <?php echo htmlspecialchars($linha[1], ENT_QUOTES, 'UTF-8'); } ?>

    </option>

    </select>
    </td>

    <td align="center"><?php echo $dados2[4]; ?></td>
    <td align="center"><?php echo $dados2[5]; ?></td>
    <td align="center">R$ <?php echo number_format($valorpedido, 2, ",", "."); ?></td>
    <td align="center">

    <a href="script_php/importarPedidosSoftcom.php?id=<?= $dados2[0]?>&data=<?= $dados2[1]?>&fabricante=<?= $dados2[2]?>&fornecedor=<?= $fornecedor?>&loja=<?= $dados2[4]?>&observacao=<?= $dados2[5]?>&valor=<?= $valorpedido?>&inicio=<?= $datainicio?>&final=<?= $datafinal?>">

    <input type="hidden" name="id" value="<?= $dados2[0] ?>">
    <input type="hidden" name="data" value="<?= $dados2[1] ?>">
    <input type="hidden" name="fabricante" value="<?= $dados2[2] ?>">
    <input type="hidden" name="loja" value="<?= $dados2[4] ?>">
    <input type="hidden" name="observacao" value="<?= $dados2[5] ?>">
    <input type="hidden" name="valor" value="<?= $valorpedido ?>">
    <input type="hidden" name="inicio" value="<?= $datainicio ?>">
    <input type="hidden" name="final" value="<?= $datafinal ?>">
  

    <input type="submit" name="" value="Importar Pedido" class="btn btn-warning">
    
    </td>

</tr>

 </form>


<?php } }?>



</table>

<?php include_once 'footer.php'; ?>