<?php

Include("includes/con_cliente.php");
require_once('connection.php');




$P_nota=$_GET['id'];
$P_empresa=$_GET['empresa'];

$result_nota = "SELECT * FROM tbnf WHERE nota = '".$P_nota."' AND empresa = '".$P_empresa."'";
//$result_nota = "SELECT * FROM tbnf WHERE nota = '".$P_nota."'";
$resultado_nota = mysqli_query($con,$result_nota);
$row_nota = mysqli_fetch_assoc($resultado_nota);

$result_custo = "SELECT Sum(tbitensnf.customedtot) AS custo FROM tbitensnf WHERE nota = '".$P_nota."' and empresa = '".$P_empresa."' ";
//$result_custo = "SELECT Sum(tbitensnf.customedtot) AS custo FROM tbnf INNER JOIN tbitensnf ON tbnf.nota = tbitensnf.nota WHERE tbnf.nota = '".$P_nota."' and tbnf.empresa = '".$P_empresa."' ";
//$result_custo = "SELECT Sum(tbitensnf.customedtot) AS custo FROM tbnf INNER JOIN tbitensnf ON tbnf.nota = tbitensnf.nota WHERE tbnf.nota = '".$P_nota."'";
$resultado_custo = mysqli_query($con,$result_custo);
$row_custo = mysqli_fetch_assoc($resultado_custo);


?>


<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>Sistema SCA - Grupo BRO</title>
    <link rel="stylesheet" href="node_modules/bootstrap/compiler/bootstrap.css">
    <link rel="stylesheet" href="node_modules/bootstrap/compiler/styler.css">
    <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery.mask/1.14.15/jquery.mask.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
</head>
<body>
    <nav class="navbar fixed-top navbar-expand-lg navbar-dark" style="background-color: #3333ff;">
        <a class="navbar-brand" href="menu.php">
            <img src="img/logomarca.png" width="50" height="40" class="d-inline-block align-center" alt="">
            SCA - Orçamentos - Apuração das Margens de Comerciais
        </a>
        <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarTogglerDemo02" aria-controls="navbarTogglerDemo02" aria-expanded="false" aria-label="Toggle navigation">
            <span class="navbar-toggler-icon"></span>
        </button>
        <div class="collapse navbar-collapse" id="navbarTogglerDemo02">
            <ul class="navbar-nav mr-auto mt-2 mt-lg-0">
            </ul>
            <form class="form-inline my-2 my-lg-0">
                <button class="btn btn-primary btn-sm my-2 my-sm-0" type="submit">
                    <a href="pesquisanotas.php" style="color:inherit"> Nova Pesquisa </a></button>
                </button>
                <button class="btn btn-primary btn-sm my-2 my-sm-0" type="submit">
                    <a href="m.php" style="color:inherit"> Sair </a></button>
                </button>
            </form>
        </div>
    </nav>

        <div style="margin-top: 5rem;" >
            <div class="row mt-2 mx-5">
              
                    <div class="card-body border border-dark rounded" style="background-color:#E0FFFF">
                      <h4 class="card-title">Dados da Nota</h4>
                      <form method="POST" action="simulanota.php" enctype="multipart/form-data">
                      <div class="form-row">
                        <div class="col-md-3 mb-3">
                            <label for="txtempresa">Empresa de Faturamento</label>
                            <input type="text" class="text-info form-control" id="txtempresa" name="txtempresa" value="<?php echo $row_nota['empresa']; ?>" readonly>
                        </div>
                        <div class="col-md-1 mb-3">
                            <label for="txtnf">Número da NF:</label>
                            <input type="text" class="text-info form-control" id="txtnf" name="txtnf" value="<?php echo $P_nota;?>" readonly>
                        </div>
                        <div class="col-md-2 mb-3">
                            <label for="txtvlr">Valor NF R$:</label>
                            <input type="text" class="text-info form-control" id="txtvlr" name="txtvlr" value="<?php echo number_format(($row_nota['vlrnota']),2, ',', '.'); ?>" readonly>
                        </div>
                        <div class="col-md-2 mb-3">
                            <label for="txtcusto">Custo NF R$:</label>
                            <input type="text" class="text-info form-control" id="txtcusto" name="txtcusto" value="<?php echo number_format(($row_custo['custo']),2, ',', '.'); ?>" readonly>
                        </div>
                        <div class="col-md-2 mb-3">
                            <label for="txtmkp">Markup NF R$:</label>
                            <input type="text" class="text-info form-control" id="txtmkp" name="txtmkp" value="<?php echo number_format(($row_nota['vlrnota'] - $row_custo['custo']),2, ',', '.');?>" readonly>
                        </div>
                        
                    </div> 
                    
                    <a href="simulanota.php?id=<?php echo $P_nota;?>&empresa=<?php echo $P_empresa;?>" class="btn btn-outline-danger">Simular Nota Fiscal</a>

                    <!--<a href="simulanota.php?id=<?php echo $P_nota; ?>" class="btn btn-outline-danger">Simular Nota Fiscal</a> -->
                    <!--<button class="btn btn-outline-danger" style="margin-bottom: 10px;margin-top: 10px;margin-left: 10px;" type="submit" name="btnReload" >Simular Nota Fiscal</button> -->
                </div>
              </form>
            </div>
        </div>


        <div class="row mt-2 mx-5">
            <div class="card-body border border-dark rounded" style="background-color:#FFFFE0">
              <h4 class="card-title"></h4>
                <div class="form-row">
                    
                    <div class="col-md-3 mb-4">
                        <label for="txtcliente">Cliente:</label>
                        <input type="text" class="text-info form-control" id="txtcliente" name="txtcliente" value="<?php echo $row_nota['cliente']; ?>" readonly>
                    </div>
                    <div class="col-md-2 mb-4">
                        <label for="txtemissao">Emissão:</label>
                        <input type="text" class="text-info form-control" id="txtemissao" name="txtemissao" value="<?php echo date("d/m/y",strtotime($row_nota['emissao']));?>"readonly>
                    </div>
                    <div class="col-md-3 mb-4">
                        <label for="txtpaciente">Paciente:</label>
                        <input type="text" class="text-info form-control" id="txtpaciente" name="txtpaciente" value="<?php echo $row_nota['paciente'];?>" readonly>
                    </div>
                    <div class="col-md-4 mb-4">
                        <label for="txthospital">Hospital:</label>
                        <input type="text" class="text-info form-control" id="txthospital" name="txthospital" value="<?php echo $row_nota['hospital'];?>" readonly>
                    </div>
                    <div class="col-md-2 mb-4">
                        <label for="txtcidade">Cidade:</label>
                        <input type="text" class="text-info form-control" id="txtcidade" name="txtcidade" value="<?php echo $row_nota['cidhosp'];?>" readonly>
                    </div>
                    <div class="col-md-2 mb-4">
                        <label for="txtplano">Plano de Saúde:</label>
                        <input type="text" class="text-info form-control" id="txtplano" name="txtplano" value="<?php echo $row_nota['convenio'];?>" readonly>
                    </div>
                    <div class="col-md-2 mb-4">
                        <label for="txtmedico">Médico:</label>
                        <input type="text" class="text-info form-control" id="txtmedico" name="txtmedico" value="<?php echo $row_nota['medico'];?>" readonly>
                    </div>
                    <div class="col-md-2 mb-4">
                        <label for="txtcirurgia">Cirurgia:</label>
                        <input type="text" class="text-info form-control" id="txtcirurgia" name="txtcirurgia" value="<?php echo date("d/m/y",strtotime($row_nota['dtcirurgia']));?>"readonly>
                    </div>
                    <div class="col-md-2 mb-4">
                        <label for="txtgrupo">Grupo:</label>
                        <input type="text" class="text-info form-control" id="txtgrupo" name="txtgrupo" value="<?php echo $row_nota['grupo'];?>" readonly>
                    </div>
                    <div class="col-md-2 mb-4">
                        <label for="txttipo">Tipo:</label>
                        <input type="text" class="text-info form-control" id="txttipo" name="txttipo" value="<?php echo $row_nota['tipo'];?>" readonly>
                    </div>
                    <div class="col-md-2 mb-4">
                        <label for="txtvendedor">Vendedor:</label>
                        <input type="text" class="text-info form-control" id="txtvendedor" name="txtvendedor" value="<?php echo $row_nota['representante'];?>" readonly>
                    </div>

                </div>
            </div>
        </div>

        <?php
            $connect = mysqli_connect('127.0.0.1', 'root', 'fcf2778@', 'administrativo');
            if(mysqli_connect_errno($connect)){
            echo 'Falha ao conectar com o Banco de Dados !'.mysqli_connect_error();}


            $result= mysqli_query($connect, "SELECT * FROM tbitensnf WHERE nota = '".$P_nota."' AND empresa = '".$P_empresa."'");
            //$result= mysqli_query($connect, "SELECT * FROM tbitensnf WHERE nota = '".$P_nota."'");

            //$result= mysqli_query($connect, "SELECT tbnf.empresa, tbnf.nota, tbnf.cliente, tbitensnf.produto ,tbitensnf.descricao ,tbitensnf.qtditem, tbitensnf.vlrunit ,tbitensnf.vlrtotal ,tbitensnf.toticms, tbitensnf.customeduni, tbitensnf.customedtot from tbnf INNER JOIN tbitensnf ON tbnf.nota = tbitensnf.nota WHERE tbnf.nota = '".$P_nota."'");
        ?>



        <div class="row mt-2 mx-5">
            <div class="card-body border border-dark rounded" style="background-color:#F5FFFA">
              <h4 class="card-title">Produtos da Nota Fiscal</h4>
              <div class="container">
                <div class="table-responsive">
                  <table style="overflow: auto;" class="table table-striped table-responsive-md btn-table table-hover table-bordered">
                      <thead class="table-warning">
                          <tr>
                              
                              <th style="text-align: center;vertical-align:middle">Codigo</th>
                              <th style="text-align: center;vertical-align:middle">Descrição</th>
                              <th style="text-align: center;vertical-align:middle">Qtd</th>
                              <th style="text-align: center;vertical-align:middle">R$ Unit</th>
                              <th style="text-align: center;vertical-align:middle">R$ Total</th>
                              <th style="text-align: center;vertical-align:middle">R$ Icms</th>
                              <th style="text-align: center;vertical-align:middle">Cust.Unit</th>
                              <th style="text-align: center;vertical-align:middle">Cust.Tot</th>

                          </tr>
                      </thead>
                      <?php while($row = mysqli_fetch_array($result)): ?>

                      <tr>
                          
                          <td style="text-align: left;vertical-align:middle"><?php echo $row['produto']; ?></td>
                          <td style="text-align: left;vertical-align:middle"><?php echo $row['descricao']; ?></td>
                          <td style="text-align: center;vertical-align:middle"><?php echo $row['qtditem']; ?></td>
                          <td style="text-align: right;vertical-align:middle"><?php echo number_format(($row['vlrunit']),2, ',', '.'); ?></td>
                          <td style="text-align: right;vertical-align:middle"><?php echo number_format(($row['vlrtotal']),2, ',', '.'); ?></td>
                          <td style="text-align: right;vertical-align:middle"><?php echo number_format(($row['toticms']),2, ',', '.'); ?></td>
                          <td style="text-align: right;vertical-align:middle"><?php echo number_format(($row['customeduni']),2, ',', '.'); ?></td>
                          <td style="text-align: right;vertical-align:middle"><?php echo number_format(($row['customedtot']),2, ',', '.'); ?></td>



                      </tr>
                      <?php endwhile; ?>
                  </table>
                </div>
              </div>
            </div>



    </form>


    
        <link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/morris.js/0.5.1/morris.css">
        <script src="js/jquery.js"></script> 
        <script src="js/bootstrap.min.js"></script>
</body>
</html>