<?php
session_start();
require 'verifica_cookie.php';
require 'conexao.php';
require 'config.php';

$termo = (isset($_POST['termo'])) ? strtoupper($_POST['termo']) : '';
$obs = (isset($_POST['obs'])) ? $_POST['obs'] : '0';
$valor = (isset($_POST['valor'])) ? $_POST['valor'] : '0';

$pdo  = Conexao::getInstance();
$sql = "SELECT ven_001, ven_027, ven_026 FROM venda ";
$sql .= "WHERE sit_001 IN (8,21) AND ven_024 = 'C' ";

if ($obs == '1'):
  if ($termo != ''):
    $sql.= "AND (ven_026 = '".intval($termo)."' OR ven_027 iLIKE '%".$termo."%')";
  endif;
else:
  if ($termo != '')
    $sql.= "AND ven_026 = '".$termo."' ";
endif;

$sql .= "ORDER BY ven_026";
$stm = $pdo->prepare($sql);
$stm->execute();
$comandas = $stm->fetchAll(PDO::FETCH_OBJ);

foreach($comandas as $comanda):
  $n_comanda = $comanda->ven_026;
  $link = 'pedido.php?tipo=C&comanda='.$comanda->ven_026.'&venda='.$comanda->ven_001;

  if ($comanda->ven_027 != ''):
    $link .= '&cliente='.$comanda->ven_027;
  endif;

  $sql = "SELECT sit_001 FROM venda WHERE sit_001 = 21 AND ven_026 = " . $comanda->ven_026;
  $stm = $pdo->prepare($sql);
  $stm->execute();
  $venda = $stm->fetchAll(PDO::FETCH_OBJ);
  $back  = (empty($venda)) ? '' : 'btn-yellow';
  $link  = (empty($venda)) ? $link : 'javascript:void(0)';
  $n_comanda  = (empty($venda)) ? $n_comanda : '';

  $sql = "SELECT ven_009 as subtotal,  ((ven_009 * 0.10) + ven_009) as total, (ven_009 * 0.10) as servico, ven_001 ";
  $sql .= "FROM venda ";
  $sql .= "WHERE ven_026 = {$n_comanda} AND ven_024 = 'C' AND sit_001 in (8, 21)";
  $stm = $pdo->prepare($sql);
  $stm->execute();
  $totalizadores = $stm->fetch(PDO::FETCH_OBJ);
  ?>
  <a href='<?=$link?>' class="mdl-button mdl-js-button mdl-button--raised mdl-button--colored btn-mesa <?=$back?>" data-upgraded=",MaterialButton">
    <span class="texto-comanda"><?=$comanda->ven_026?></span> 
    <br> 
    <span class="texto-mesa"><?=$comanda->ven_027?></span>
    <?php if($valor == 1):?>
      <span class="text-center text-white valor">
        <?php if (TAXA_COMANDA):?> 
        <?=number_format( $totalizadores->total, 2, ',', '.')?>
        <?php else:?>
        <?=number_format( $totalizadores->subtotal, 2, ',', '.')?>
        <?php endif;?>
      </span>
    <?php endif; ?>
  </a>
<?php endforeach;?>
