<?php
    include_once('../../../conexao.php');
    include_once('../../../DAO/DAOvenda.php');
	$DAOvenda  = new DAOvenda();

    if (!isset($_SESSION)) {
           session_start();
    }
    $conn = new Conexao();

    $response = $DAOvenda->empresaDefault();
    foreach($response as $row){
        $emp_razao_social = $row['emp_razao_social'];
        $emp_email        = $row['emp_email'];
        $emp_end          = $row['emp_end'];
        $emp_numero       = $row['emp_numero'];
        $emp_cep          = $row['emp_cep'];
        $emp_bairro       = $row['emp_bairro'];
        $emp_fone         = $row['emp_fone'];
    }

    $responseVenda = $DAOvenda->imprimirDAV($_POST['venda-impressao-ven-id']);


    $sql = " SHOW TABLE STATUS WHERE Name='servico_aplicacao';";
    $stmt = $conn->conectar()->prepare($sql);
    $stmt->execute();  
    $result = $stmt->fetchAll();
    

    foreach($result as $row3) {		
    // echo' <h1>Nome da tabela'.$row3->Comment.'</h1>' .'<br>';
    $title = $row3->Comment;

    }
    

?>   
<!-- <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />-->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.0.0-beta1/dist/js/bootstrap.bundle.min.js" integrity="sha384-ygbV9kiqUc6oa4msXn9868pTtWMgiQaeYH7/t7LECLbyPA2x65Kgf80OJFdroafW" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/paper-css/0.4.1/paper.min.css" integrity="sha512-V+r0ugxPpGraMA2ubEWvqpfcZZAfMAbnSDPeu/6gYFfdxlgJGfCuNTNrZkrjQ0sJBoySdN6uoTVwJ+4P/BeTkg==" crossorigin="anonymous" />
 
 <link rel="preconnect" href="https://fonts.gstatic.com">
<link href="https://fonts.googleapis.com/css2?family=Open+Sans:wght@300;400;600&display=swap" rel="stylesheet"> 
  <style>
    body   { font-family: calibri }
   /* body   { font-family: 'Open Sans', sans-serif; }*/
	
    h1     {font-size: 40pt; line-height: 18mm}
    h2, h3 { font-size: 20pt; line-height: 7mm }
    h4     { font-size: 32pt; line-height: 14mm }
    h2 + p { font-size: 18pt; line-height: 7mm;font-weight: normal; }
    h3 + p { font-size: 14pt; line-height: 7mm }
    li     { font-size: 11pt; line-height: 5mm; text-transform:uppercase; font-weight:bold;     list-style-type: none; }

    h1      { margin: 0 }
    h1 + ul { margin: 2mm 0 5mm }
    h2, h3  { margin: 0 3mm 3mm 0; float: left }
    h2 + p,
    h3 + p  { margin: 0 0 3mm 50mm }
    h4      { margin: 2mm 0 0 50mm; border-bottom: 2px solid black }
    h4 + ul { margin: 5mm 0 0 50mm }
    article { border: 4px double black; padding: 5mm 10mm; border-radius: 3mm }
    th, td {
  text-align: left;
  padding: 6px;
}

tr:nth-child(even) {
  background-color: #f2f2f2;
}
 
  @page {
    size: A4
  }

.table-responsive:{
height:95%;
}

.sheet {
  overflow: visible;
  height: auto !important;
}



/* styilo table */
.lu-table-dav {
    width: 100%;
    padding-bottom: 0;
    border-spacing: 0;
    border-top: 1px solid #000;
    border-bottom: 1px solid #000;
    border-left: 1px solid #000;
}

.lu-table-dav th.right {
    text-align: right;
}
.lu-table-dav th {
    font-weight: 700;
    height: 25px;
    padding-top: 5px;
    text-align: left;
    border-right: 1px solid #000;
    background-color: #f0f0f0;
/*     font-size: 9pt; */
    padding-left: 5px;
    padding-right: 5px;
}
.lu-table-dav {
    border-spacing: 0;
}

.divBorda {
    padding: 8px;
    border-spacing: 0;
    border: 1px solid #000;
}

/* Css da tabela dos dados da dav */
.lu-table-dav td, .lu-table-dav-detais-dav td {
    text-align: left;
    border-top: 1px solid #000;
    border-right: 1px solid #000;
    vertical-align: middle;
    padding: 3px 0;
        padding-right: 0px;
        padding-left: 0px;
/*     font-size: 9pt; */
    padding-left: 5px;
    padding-right: 5px;
}

.lu-table-dav-detais-dav {
    width: 100%;
    min-height: 100%;
    padding-bottom: 0;
    border-spacing: 0;
    border-bottom: 1px solid #000;
    border-left: 1px solid #000;
}

.lu-table-dav-detais-dav th {
    font-weight: 700;
    height: 30px;
    padding-top: 5px;
    padding-bottom: 5px;
    text-align: left;
    border-right: 1px solid #000;
    border-top: 1px solid #000;
    background-color: #f0f0f0;
/*     font-size: 9pt; */
    padding-left: 5px;
    padding-right: 5px;
}

.right {
    text-align: right !important;
}
</style>

<body class="A4">
 
  <section class="sheet padding-10mm">
  <h1 style="   font-size:10pt;margin-top: -38px;text-align: right;float: right;"><?php echo date('d/m/Y') ?></h1>
  <ul style="padding-left: 0px;margin-top: -4mm;">
      <li><?php echo $emp_razao_social; ?> <li>
      <li> Mossoró - Rio Grande do Norte </li>
      <li><?php echo $emp_end; ?> , <?php echo $emp_numero; ?>,  <?php echo $emp_bairro; ?>  – CEP: <?php echo $emp_cep; ?>  </li>
      <li>Telefone: <?php echo $emp_fone; ?> </li>
      <li><?php echo $emp_email; ?></li>
 
    </ul>
  
    <h1 style="margin:0 auto; font-weight:bold; text-align:center; font-size:22pt;margin-top: -30px;">DAV - DOCUMENTO AUXILIAR DE VENDA</h1>
	
	
	
	<h1 style="font-size: 16pt;margin-bottom: -27px;margin-top: -26px;color: #0c0c0c;letter-spacing: 3px;">CLIENTE</h1>

    <?php
        foreach($responseVenda as $row){
            /* dados do cliente */
            $cli_nome     = $row['cli_nome'];
            $cli_cpf_cnpj = $row['cli_cpf_cnpj'];
            $cli_celular  = $row['cli_celular'];
            $cli_endereco = $row['cli_endereco'].', '.$row['cli_bairro'].', '.$row['cli_numero'].
            ' - '.$row['cid_nm'].'/'.$row['cli_uf'];
            
            /* dados da venda */   
            $ven_dt_abertura = date('d/m/Y', strtotime($row['ven_dt_abertura']));
            $ven_situacao    = $row['ven_situacao'];
        }
    ?>

	<!-- Tabela de cliente -->
	<table style="text-align: left;">
    <tbody>
        <tr>
            <td width="70%" style="padding-left: 0px;">
                <div class="divBorda"><b><?php echo $cli_nome; ?></b>
					<br><b>CPF/CNPJ:</b> <?php echo $cli_cpf_cnpj; ?><br>
					<b>TELEFONE: </b><?php echo $cli_celular; ?><br>
					<b>ENDEREÇO: </b><?php echo $cli_endereco; ?><br>
				</div>
            </td>
            <td width="20px"></td>
            <td width="30%" style="padding-right: 0PX;">
                <table class="lu-table-dav-detais-dav">
                    <tbody>
                        <tr>
                            <th style="height:17px;">DAV</th> 
                            <td><?php echo $_POST['venda-impressao-ven-id']?></td>
                        </tr>
                        <tr>
                            <th style="height:17px;">DT. EMISSÃO</th>
                            <td><?php echo $ven_dt_abertura; ?></td>
                        </tr>
                        <tr>
                            <th style="height:17px;">STATUS</th>
                            <td><?php echo $ven_situacao; ?></td>
                        </tr>
                    </tbody>
                </table>
            </td>
        </tr>
    </tbody>
</table>

	<!-- Tabela de cliente -->

    <?php   
        $isThereProduct = false;
        $isThereServices = false;
        $isThereThing    = false;
        foreach($responseVenda as $row){
            if ($row['vit_tipo'] == 'S')
               $isThereServices = true;

            if ($row['vit_tipo'] == 'P')
               $isThereProduct = true;
             
            if ($row['sap_id'] != '')
               $isThereThing = true;   

        }
    ?>
<?php if ($isThereThing == true) {?>
<h1 style="font-size: 16pt;margin-bottom: -27px;margin-top: -19px;color: #0c0c0c;letter-spacing: 3px;">IDENTIFICAÇÃO DO(A) <?php echo strtoupper($title)?> </h1>
<table style="text-align: left;width: 100%;">
    <tbody>
        <tr>
            <td style="padding-left: 0px;" >
                <div class="divBorda"><b><?php echo $row['sap_id'].' - '.$row['sap_descricao1'] ?> </b>
					 
				</div>
            </td>
            
        </tr>
    </tbody>
</table>
<?php }?>


	
<!-- Table Serviços -->
<?php if ($isThereServices == true) {?>
<h1 style="font-size: 16pt;margin-bottom: -22px;margin-top: -20px;color: #0c0c0c;letter-spacing: 3px;">SERVIÇO</h1>
<table class="lu-table-dav">
    <tbody>
        <!-- <div style="background: #262424;color: white;padding: 5px;">APLICAÇÃO: <?php echo $row['sap_id'].' - '.$row['sap_descricao1'] ?> </div> -->
        <tr>
			<th>Código</th>
            <th>Descrição do Serviço</th>
            <th>Qtd.</th>
            <th class="right" style="white-space: nowrap;">Valor unit.</th>
            <th class="right">Subtotal</th>
            <th class="right" style="white-space: nowrap;">Desc R$</th>
            <th class="right">Valor liq.</th>
          
        </tr>

        <?php 
            $numberItens      = 0;
            $discountServices = 0;
            $amount           = 0;
            $totalServicesLiq    = 0;
            $totalServices    = 0;
            foreach($responseVenda as $row){
                if ($row['vit_tipo'] == 'S'){
                    echo'<tr>
                        <td style="vertical-align: top; word-break: break-all;" width="10%">'.$row['vit_pro_id'].'</td>
                        <td style="text-align: center" width="29%">'.$row['vit_pro_descricao'].'</td>
                        
                        <td class="right" width="10%">'. number_format($row['vit_qtd'],2,',','.').'</td>
                        <td class="right" width="10%">'. number_format($row['vit_vl_unitario'],2,',','.').'</td>
                        <td class="right">'.number_format($row['vit_vl_bruto'],2,',','.').'</td>
                        <td class="right" width="7%">'. number_format($row['vit_vl_desconto'],2,',','.').'</td>
                        <td class="right">'.number_format($row['vit_vl_liquido'],2,',','.').'</td>
                   
                    </tr>';
                    $numberItens      = $numberItens + 1;
                    $discountServices = $discountServices  + $row['vit_vl_desconto'];
                    $amount           = $amount + $row['vit_qtd'];
                    $totalServicesLiq    = $totalServicesLiq + $row['vit_vl_bruto'] -  $row['vit_vl_desconto'];
                    $totalServices    = $totalServices + $row['vit_vl_bruto'];
                
                }
            }
        ?>
        
        <tr>
            <td colspan="6" style="text-align: right; padding: 2px 2px; font-weight:bold; border-bottom:0">
				N° de itens <br>
				Desconto dos itens<br>
				Soma das quantidades<br>
				Total de Serviços<br>
			</td>
            <td class="right">
                <?php echo $numberItens;?><br>
				<?php echo number_format($discountServices,2,',','.')?><br>
				<?php echo $amount;?><br>
				<?php echo number_format($totalServicesLiq ,2,',','.')?>
			</td>
        </tr>
    </tbody>
</table>
<?php }?>
<!-- table Serviços -->


<?php if ($isThereProduct == true) {?>
<!-- Table Produtos -->
<h1 style="font-size: 16pt;margin-bottom: -22px;margin-top: -11px;color: #0c0c0c;letter-spacing: 3px;">PRODUTO</h1>
<table class="lu-table-dav">
    <tbody>
        <tr>
			<th>Código</th>
            <th>Descrição do Produto</th>
            <th>Ref.</th>
            <th>Qtd.</th>
            <th class="right" style="white-space: nowrap;">Valor unit.</th>
            <th class="right">Subtotal</th>
            <th class="right" style="white-space: nowrap;">Desc R$</th>
            <th class="right" style="white-space: nowrap;">Valor liq.</th>
          
        </tr>


        <?php 
            $numberItens      = 0;
            $discountProducts = 0;
            $amount           = 0;
            $totalProductsLiq = 0;
            $totalProducts    = 0;
            foreach($responseVenda as $row){
                if ($row['vit_tipo'] == 'P'){
                    echo'<tr>
                        <td style="vertical-align: top; word-break: break-all;" width="10%">'.$row['vit_pro_id'].'</td>
                        <td style="text-align: center" width="29%">'.$row['vit_pro_descricao'].'</td>
                        <td width="10%">'.$row['vit_pro_referencia'].'</td>
                        <td class="right" width="10%">'. number_format($row['vit_qtd'],2,',','.').'</td>
                        <td class="right" width="10%">'. number_format($row['vit_vl_unitario'],2,',','.').'</td>
                        <td class="right">'.number_format($row['vit_vl_bruto'],2,',','.').'</td>
                        <td class="right" width="7%">'. number_format($row['vit_vl_desconto'],2,',','.').'</td>
                        <td class="right">'.number_format($row['vit_vl_liquido'],2,',','.').'</td>
                        
                    </tr>';
                    $numberItens      = $numberItens + 1;
                    $discountProducts = $discountProducts  + $row['vit_vl_desconto'];
                    $amount           = $amount + $row['vit_qtd'];
                    $totalProductsLiq    = $totalProductsLiq + $row['vit_vl_bruto']-$row['vit_vl_desconto'];
                    $totalProducts    = $totalProducts + $row['vit_vl_bruto'];
                }
            }
        ?>


       
        <tr>
            <td colspan="7" style="text-align: right; padding: 2px 2px; font-weight:bold; border-bottom:0">
					N° de itens <br>
					Desconto dos itens<br>
					Soma das quantidades<br>
					Total de Produtos
			</td>
			<td class="right">
				<?php echo $numberItens;?><br>
				<?php echo number_format($discountProducts,2,',','.')?><br>
				<?php echo $amount;?><br>
				<?php echo number_format($totalProductsLiq,2,',','.')?>
			</td>
        </tr>
    </tbody>
</table>
<?php }?>
<!-- table Produtos -->



<h1 style="font-size: 16pt;margin-bottom: -22px;margin-top: -11px;color: #0c0c0c;letter-spacing: 3px;">FORMA DE PAGAMENTO</h1>

<!-- Table totalizadores -->
<table class="lu-table-dav" style="width: 68%;">
    <tbody>
        <tr>
			<th>SEQ</th>
            <th>DESCRIÇÃO</th>
			<th>VENCIMENTO</th>
            <th class="right">VALOR R$ </th>
        </tr>


        <?php 
            $entryValueBegin = 0;
            $responsePagamento = $DAOvenda->imprimirDAVPagamento($_POST['venda-impressao-ven-id']);
            foreach($responsePagamento as $row){
                echo'<tr>
                    <td style="vertical-align: top; word-break: break-all;" width="5%">'.$row['pag_sequencia'].'</td>
                    
                    <td style="text-align: center" width="30%">'.$row['pag_fpa_descricao'].' '.$row['pag_numero'] .'</td>
                    <td   width="5%">'.date('d/m/Y',strtotime( $row['pag_vencimento']) ).'</td>
                    <td class="right" width="10%">'.number_format($row['pag_valor'],2,',','.').'</td>
                </tr>';
            }
        ?>
      
        <tr>
 
            <td colspan="3" style="text-align: right; padding: 2px 2px; font-weight:bold; border-bottom:0">
					
					TOTAL BRUTO<br>
					DESCONTO<br>
					TOTAL LÍQUIDO
			</td>
			<td class="right">
                <?php echo number_format( ($totalProducts + $totalServices ) ,2,',','.')?><br>
				<?php echo number_format(  ($discountProducts + $discountServices) ,2,',','.')?><br>
				<?php echo number_format(  ( ($totalProducts-$discountProducts)+($totalServices-$discountServices)) ,2,',','.')?>
			</td>
            
        </tr>
    </tbody>
</table>
<!-- table totalizadores -->

  </section>

</body> 
 