<?php
    include_once('../../../conexao.php');
    include_once('../../../DAO/relatorios/DAORelatorioContasReceber.php');
	$DAORelatorioContasReceber  = new DAORelatorioContasReceber();
    if (!isset($_SESSION)) {
           session_start();
       }
    $conn = new Conexao();

   $response = $DAORelatorioContasReceber->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'];
   }
?>   
<!-- <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 */
.grid {
    width: 100%;
    padding-bottom: 0;
    border-spacing: 0;
    border-top: 1px solid #000;
    border-bottom: 1px solid #000;
    border-left: 1px solid #000;
}

.grid th.right {
    text-align: right;
}
.grid 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;
}
.grid {
    border-spacing: 0;
}

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

.grid td, .gridH 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;
}

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

.gridH 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;
}
</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>

	<!-- Tabela de cliente -->
	<table style="text-align: left;">
    <tbody>
        <tr>
            <td width="70%" style="padding-left: 0px;">
                <div class="divBorda"><b>JOAO LENO ALFREDO SOUZA</b>
					<br><b>CPF/CNPJ:</b> 094.367.194-93<br>
					<b>TELEFONE:</b> (84) 98135-9585<br>
					<b>ENDEREÇO:</b> AVENIDAD CENTENÁRIA, AEROPORTO, 50 - MOSSORÓ/RN<br>
				</div>
            </td>
            <td width="20px"></td>
            <td width="30%" style="padding-right: 0PX;">
                <table class="gridH">
                    <tbody>
                        <tr>
                            <th style="height:17px;">DAV</th>
                            <td>0000002</td>
                        </tr>
                        <tr>
                            <th style="height:17px;">DT. EMISSÃO</th>
                            <td>09/02/2021</td>
                        </tr>
                        <tr>
                            <th style="height:17px;">STATUS</th>
                            <td>ENCERRADA7</td>
                        </tr>
                    </tbody>
                </table>
            </td>
        </tr>
    </tbody>
</table>

	<!-- Tabela de cliente -->


	<h1 style="font-size: 16pt;margin-bottom: -22px;margin-top: -20px;color: #0c0c0c;letter-spacing: 3px;">SERVIÇO</h1>
	
<!-- Table Serviços -->
<table class="grid">
    <tbody>
        <tr>
			<th>Código</th>
            <th>Descrição do Serviço</th>
            <th>Qtd.</th>
            <th class="right">Valor unit.</th>
            <th class="right">Desc R$</th>
            <th class="right">Valor liq.</th>
            <th class="right">Subtotal</th>
        </tr>
        <tr>
            <td style="vertical-align: top; word-break: break-all;" width="10%">0000041</td>
            <td style="text-align: center" width="29%">TROCA DE DISPLAY</td>
            <td class="right" width="10%">1,00</td>
            <td class="right" width="10%">100,00</td>
            <td class="right" width="6%">0,00</td>
            <td class="right">100,00</td>
            <td class="right">100,00</td>
        </tr>
        <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">
				1,00<br>
				0,00<br>
				1,00<br>
				100,00 
			</td>
        </tr>
    </tbody>
</table>
<!-- table Serviços -->

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

<!-- Table Produtos -->
<table class="grid">
    <tbody>
        <tr>
			<th>Código</th>
            <th>Descrição do Serviço</th>
 
            <th>Referência</th>
            <th>Qtd.</th>
            <th class="right">Valor unit.</th>
            <th class="right">Desc R$</th>
            <th class="right">Valor liquído</th>
            <th class="right">Subtotal</th>
        </tr>
        <tr>
            <td style="vertical-align: top; word-break: break-all;" width="10%">0000041</td>
            <td style="text-align: center" width="29%">BATERIA DE IPHONE</td>
 
            <td width="10%"></td>
            <td class="right" width="10%">1,00</td>
            <td class="right" width="10%">100,00</td>
            <td class="right" width="7%">0,00</td>
            <td class="right">100,00</td>
            <td class="right">100,00</td>
        </tr>
        <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">
				1,00<br>
				0,00<br>
				1,00<br>
				100,00 
			</td>
        </tr>
    </tbody>
</table>
<!-- table Produtos -->


<br><br><br>
<table  class="table table-striped table-responsive" style="width: 100%;margin-top: -13px;border-collapse: collapse;border-spacing: 0;display:none">
  <thead style="text-align: left;">
    <tr>
      <th>Parcela</th>
      

      <th>For. Pagto</th>
      <th>Data de Venc.</th>
      <th>Juros</th>
      <th>Desconto</th>
      <th>Vl. Parcela</th>
      <th>Vl. Recebido</th>
      <th>Situação</th>
         </tr>
  </thead>
  <tbody>

  <?php 
	$saldo   				 = 0;
	$entrada 			     = 0;
    $saida   				 = 0;
	$showTableTotalPaga 	 = 'display:none;';
	$showTableTotalEmAberto  = 'display:none;';
	$showTableTotalCancelada = 'display:none;';
  	$venda = '';
    $result = $DAORelatorioContasReceber->relatorioContasReceber($_POST['filtro-relatorio-contas-receber-cli_id'], $_POST['filtro-relatorio-contas-receber-cre_situacao'], $_POST['filtro-relatorio-contas-receber-dt-inicial'], $_POST['filtro-relatorio-contas-receber-dt-final']);  
    foreach($result as $row) {

      if ($venda != $row['cre_ven_id']){
        echo'<tr style="background-color: #FDF4C9 !important;">
          <td colspan="8" style="font-weight: bold;text-transform: uppercase;padding-bottom: 0px;">'.$row['cli_nome']. '<span style="float: right;">VENDA N°: '.$row['cre_ven_id'].'</span> </td>
        </tr>';

        echo'<tr style="background-color: #FDF4C9 !important;">
          <td colspan="8" style="font-weight: bold;text-transform: uppercase;padding-top: 0px;text-align: right;">Valor: '.'R$ '.number_format($row['cre_vl_total'],2,",","."). ' </td>
        </tr>';
        $venda = $row['cre_ven_id'];  
      }  
        
      switch ($row['cre_situacao']) {
        case "EM ABERTO":
            $classColor = "green";
            break;
        case "CANCELADA":  
            $classColor = "#dc3545";
            break;
        case "PAGA":
            $classColor = "#007bff";
            break;    
     				 
    }
        
 
     echo' <tr>
           <td>'.$row['cre_id'].'</td>
           
            <td style="white-space: nowrap;">'.$row['fpa_descricao'].'</td>
            <td>'.date('d/m/Y', strtotime($row['cre_dt_vencimento']) ).'</td>
            <td  style="white-space: nowrap;">'.'R$ '.number_format($row['cre_vl_juros'],2,",",".").'</td>
            <td  style="white-space: nowrap;">'.'R$ '.number_format($row['cre_vl_desconto'],2,",",".").'</td>
            <td  style="white-space: nowrap;">'.'R$ '.number_format($row['cre_vl_parcela'],2,",",".").'</td>
            <td  style="white-space: nowrap;">'.'R$ '.number_format($row['cre_vl_pago'],2,",",".").'</td>
            <td  style="color:'.$classColor.';white-space: nowrap;">'. $row['cre_situacao'] .'</td>
            
        </tr>';
	}
	
		$saldo = $entrada - $saida ;  


//		echo '---> '.$_POST['filtro-relatorio-contas-receber-cre_situacao'] ;

	/* 	if ($_POST['filtro-relatorio-contas-receber-cre_situacao'] == 'PAGA')
		    $showTableTotalPaga = 'display:inline-table;';
		if ($_POST['filtro-relatorio-contas-receber-cre_situacao'] == 'EM ABERTO')
			$showTableTotalEmAberto = 'display:inline-table;';   
		if ($_POST['filtro-relatorio-contas-receber-cre_situacao'] == 'CANCELADA')
			$showTableTotalCancelada = 'display:inline-table;';  
		if ($_POST['filtro-relatorio-contas-receber-cre_situacao'] == ''){
			$showTableTotalPaga = 'display:inline-table;';
			$showTableTotalEmAberto = 'display:inline-table;';
			$showTableTotalCancelada = 'display:inline-table;';  
		} */
		
    ?>
    
  </tbody>
</table>





  <!-- Totalizadores agrupados por situacao PAGA -->
  <table  class="table table-striped table-responsive" style="border-top-width: 2px;border-top-style: solid;width: 50%;margin-top: 25px;border-collapse: collapse;border-spacing: 0;text-align: center;position: relative;left: 50%;<?php echo $showTableTotalPaga; ?> ">
		<thead style="text-align: left;">
	   		 <tr style="font-weight: bold;text-transform: uppercase;background-color: #007bff ;border-bottom-width: 2px;
border-bottom-style: double;">
				<td colspan="2" style="font-weight: bold;text-transform: uppercase;padding-bottom: 6px;">TOTAL DE PARCELAS PAGAS </td>
			  </tr> 

			 
			<tr style="background-color: white;">
				<th>DESCRIÇÃO</th>
				
				<th  style="text-align: right;">VALOR</th>
				<!--<th>SITUAÇÃO</th>-->
			</tr>
		</thead>
		<tbody>
			<?php
				$result = $DAORelatorioContasReceber->relatorioContasReceberAgrupadosPorSituacao($_POST['filtro-relatorio-contas-receber-cli_id'], 'PAGA', $_POST['filtro-relatorio-contas-receber-dt-inicial'], $_POST['filtro-relatorio-contas-receber-dt-final']);  
				$totalEmAberto = 0;
				foreach($result as $row) {


					switch ($row['cre_situacao']) {
						case "EM ABERTO":
							$classColor = "green";
							break;
						case "CANCELADA":  
							$classColor = "#dc3545";
							break;
						case "PAGA":
							$classColor = "#007bff";
							break;    		  
					}
					$totalEmAberto += $row['total'];

				echo   '<tr style="background-color: #fff;">
							<td style="white-space: nowrap;">'.$row['descricao'].'</td>
							<td  style="white-space: nowrap;color:'.$classColor.';text-align: right;">'.'R$ '.number_format($row['total'],2,",",".").'</td>
							<!--<td style="white-space: nowrap;color:'.$classColor.';">'.$row['cre_situacao'].'</td>-->
					
						</tr>';
				}
			  echo'	<tr style="font-weight: bold;text-transform: uppercase;border-top-width: 2px;
			  border-top-style: solid;">
			  			<td style="white-space: nowrap;padding-top: 7px;padding-bottom: 7px;">TOTAL</td>
						<td colspan="1"style="text-align: right;font-weight: bold;text-transform: uppercase;padding-bottom: 0px;padding-top: 0px;color:'.$classColor.';">'.
						'R$ '.number_format($totalEmAberto,2,",",".").'</td>
			  	  	</tr> ';
			?>	
		</tbody>
	</table>
  	<!-- Totalizadores agrupados por situacao PAGA-->


	  

  <!-- Totalizadores agrupados por situacao EM ABERTO -->
  <table  class="table table-striped table-responsive" style=" border-top-width: 2px;border-top-style: solid;width: 50%;margin-top: 25px;border-collapse: collapse;border-spacing: 0;text-align: center;position: relative;left: 50%;<?php echo $showTableTotalEmAberto; ?>">
		<thead style="text-align: left;">
	   		 <tr style="font-weight: bold;text-transform: uppercase;background-color: #28a745 ;border-bottom-width: 2px;
border-bottom-style: double;">
				<td colspan="2" style="font-weight: bold;text-transform: uppercase;padding-bottom: 6px;">TOTAL DE PARCELAS EM ABERTO </td>
			  </tr> 

			 
			<tr style="background-color: white;">
				<th>DESCRIÇÃO</th>
				
				<th  style="text-align: right;">VALOR</th>
				<!--<th>SITUAÇÃO</th>-->
			</tr>
		</thead>
		<tbody>
			<?php
				$result = $DAORelatorioContasReceber->relatorioContasReceberAgrupadosPorSituacao($_POST['filtro-relatorio-contas-receber-cli_id'], 'EM ABERTO', $_POST['filtro-relatorio-contas-receber-dt-inicial'], $_POST['filtro-relatorio-contas-receber-dt-final']);  
				$totalEmAberto = 0;
				foreach($result as $row) {


					switch ($row['cre_situacao']) {
						case "EM ABERTO":
							$classColor = "green";
							break;
						case "CANCELADA":  
							$classColor = "#dc3545";
							break;
						case "PAGA":
							$classColor = "#007bff";
							break;    		  
					}
					$totalEmAberto += $row['total'];

				echo   '<tr style="background-color: #fff;">
							<td style="white-space: nowrap;">'.$row['descricao'].'</td>
							<td  style="white-space: nowrap;color:'.$classColor.';text-align: right;">'.'R$ '.number_format($row['total'],2,",",".").'</td>
							<!--<td style="white-space: nowrap;color:'.$classColor.';">'.$row['cre_situacao'].'</td>-->
					
						</tr>';
				}
			  echo'	<tr style="font-weight: bold;text-transform: uppercase;border-top-width: 2px;
			  border-top-style: solid;">
			  			<td style="white-space: nowrap;padding-top: 7px;padding-bottom: 7px;">TOTAL</td>
						<td colspan="1"style="text-align: right;font-weight: bold;text-transform: uppercase;padding-bottom: 0px;padding-top: 0px;color:'.$classColor.';">'.
						'R$ '.number_format($totalEmAberto,2,",",".").'</td>
			  	  	</tr> ';
			?>	
		</tbody>
	</table>
  	<!-- Totalizadores agrupados por situacao  EM ABERTO -->




  <!-- Totalizadores agrupados por situacao CANCELADA -->
  <table  class="table table-striped table-responsive" style="border-top-width: 2px;border-top-style: solid;width: 50%;margin-top: 25px;border-collapse: collapse;border-spacing: 0;text-align: center;position: relative;left: 50%;<?php echo $showTableTotalCancelada; ?>">
		<thead style="text-align: left;">
	   		 <tr style="font-weight: bold;text-transform: uppercase;background-color: #dc3545 ;border-bottom-width: 2px;
border-bottom-style: double;">
				<td colspan="2" style="font-weight: bold;text-transform: uppercase;padding-bottom: 6px;">TOTAL DE PARCELAS CANCELADAS </td>
			  </tr> 

			 
			<tr style="background-color: white;">
				<th>DESCRIÇÃO</th>
				
				<th style="text-align: right;">VALOR</th>
				<!--<th>SITUAÇÃO</th>-->
			</tr>
		</thead>
		<tbody>
			<?php
				$result = $DAORelatorioContasReceber->relatorioContasReceberAgrupadosPorSituacao($_POST['filtro-relatorio-contas-receber-cli_id'], 'CANCELADA', $_POST['filtro-relatorio-contas-receber-dt-inicial'], $_POST['filtro-relatorio-contas-receber-dt-final']);  
				$totalEmAberto = 0;
				foreach($result as $row) {


					switch ($row['cre_situacao']) {
						case "EM ABERTO":
							$classColor = "green";
							break;
						case "CANCELADA":  
							$classColor = "#dc3545";
							break;
						case "PAGA":
							$classColor = "#007bff";
							break;    		  
					}
					$totalEmAberto += $row['total'];

				echo   '<tr style="background-color: #fff;">
							<td style="white-space: nowrap;">'.$row['descricao'].'</td>
							<td  style="white-space: nowrap;color:'.$classColor.';text-align: right;">'.'R$ '.number_format($row['total'],2,",",".").'</td>
							<!--<td style="white-space: nowrap;color:'.$classColor.';">'.$row['cre_situacao'].'</td>-->
					
						</tr>';
				}
			  echo'	<tr style="font-weight: bold;text-transform: uppercase; border-top-width: 2px;
			  border-top-style: solid;">
			  			<td style="white-space: nowrap;padding-top: 7px;padding-bottom: 7px;">TOTAL</td>
						<td colspan="1"style="text-align: right;font-weight: bold;text-transform: uppercase;padding-bottom: 0px;padding-top: 0px;color:'.$classColor.';">'.
						'R$ '.number_format($totalEmAberto,2,",",".").'</td>
			  	  	</tr> ';
			?>	
		</tbody>
	</table>
  	<!-- Totalizadores agrupados por situacao  CANCELADA -->


 

  </section>
  
 

</body> 