<?php require_once('../Connections/paivaconex.php');
require_once('funcoes.php'); 
session_start();

?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "") 
{
  if (PHP_VERSION < 6) {
    $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
  }

  $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

  switch ($theType) {
    case "text":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;    
    case "long":
    case "int":
      $theValue = ($theValue != "") ? intval($theValue) : "NULL";
      break;
    case "double":
      $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
      break;
    case "date":
      $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
      break;
    case "defined":
      $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
      break;
  }
  return $theValue;
}
}

mysql_select_db($database_paivaconex, $paivaconex);
$query_empresa = "SELECT * FROM mfin_empresas INNER JOIN mfin_cidades ON mfin_empresas.Cidade = mfin_cidades.Id INNER JOIN mfin_estado ON mfin_cidades.Uf = mfin_estado.Id";
$empresa = mysql_query($query_empresa, $paivaconex) or die(mysql_error());
$row_empresa = mysql_fetch_assoc($empresa);
$totalRows_empresa = mysql_num_rows($empresa);

mysql_select_db($database_paivaconex, $paivaconex);
$query_Fatura = "Select Sum(Valor) as valor,DATE_FORMAT(DtEmissao, '%d/%m/%Y') AS Dt_Emissao, DATE_FORMAT(DtVencimento, '%d/%m/%Y') AS Dt_Vencimento, NumNota  FROm mfin_faturas Inner join mfin_faturasdescricao ON mfin_faturas.Id = Fatura WHERE mfin_faturas.Id=".$_GET['id']." Group by Fatura";
$Fatura = mysql_query($query_Fatura, $paivaconex) or die(mysql_error());
$row_Fatura = mysql_fetch_assoc($Fatura);
$totalRows_Fatura = mysql_num_rows($Fatura);

mysql_select_db($database_paivaconex, $paivaconex);
$query_cliente = "Select RazaoSocial, Endereco, Numero, Complemento, mfin_cidades.Cidade AS City , Sigla, IF(Cnpj='', Cpf, Cnpj) as doc FROM mfin_faturas Inner join mfin_clientes ON mfin_faturas.Cliente = mfin_clientes.Id INNER JOIN mfin_cidades ON mfin_clientes.Cidade = mfin_cidades.Id INNER JOIN mfin_estado ON mfin_cidades.Uf = mfin_estado.Id WHERE mfin_faturas.Id=".$_GET['id'];
$cliente = mysql_query($query_cliente, $paivaconex) or die(mysql_error());
$row_cliente = mysql_fetch_assoc($cliente);
$totalRows_cliente = mysql_num_rows($cliente);

mysql_select_db($database_paivaconex, $paivaconex);
$query_descricao = "Select * FROM mfin_faturasdescricao  INNEr join mfin_tiposrecebimentos ON TipoRecebimento = mfin_tiposrecebimentos.Id WHERE Fatura=".$_GET['id'];
$descricao = mysql_query($query_descricao, $paivaconex) or die(mysql_error());
$row_descricao = mysql_fetch_assoc($descricao);
$totalRows_descricao = mysql_num_rows($descricao);

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

<body>
<table width="936" height="337" border="0">
  <tr>
    <td width="930" align="left" valign="top"><table width="893" border="1">
      <tr align="center">
        <td width="360" rowspan="5" valign="top"><img src="Imagens/Logo.fw.png" width="345" height="167" /></td>
        <td width="557" height="23" align="left"><?php echo $row_empresa['Endereco'].' '.$row_empresa['Numero'].' '.$row_empresa['Complemento']; ?></td>
      </tr>
      <tr>
        <td height="39">CEP.: <?php echo $row_empresa['Cep']; ?></td>
      </tr>
      <tr>
        <td height="33"><?php echo $row_empresa['Cidade'].' - '.$row_empresa['Sigla']; ?></td>
      </tr>
      <tr>
        <td height="30">CNPJ/MF.: n&ordm; <?php echo mascara_string("##.###.###/####.##",str_pad($row_empresa['Cnpj'],14,"0", STR_PAD_LEFT)); ?></td>
      </tr>
      <tr>
        <td height="23">Inscri&ccedil;&atilde;o Municipal: n&ordm;  <?php echo mascara_string("#.###.###.#",str_pad($row_empresa['InscricaoMunicipal'],8,"0", STR_PAD_LEFT)); ?></td>
      </tr>
      <tr>
        <td height="34" align="center">Fone/Fax: <?php echo $row_empresa['Telefone']; ?></td>
        <td>&nbsp;</td>
      </tr>
    </table>
    <p>&nbsp;</p>
    <table width="889" border="0">
      <tr align="center">
        <td>FATURA</td>
      </tr>
    </table>
    <p>&nbsp;</p>
    <table width="895" border="1">
      <tr align="center">
        <td><strong>Data de Emiss&atilde;o</strong></td>
        <td><strong>N&uacute;mero da Fatura</strong></td>
        <td><strong>Valor em R$</strong></td>
        <td><strong>Vencimento</strong></td>
      </tr>
      <tr align="center">
        <td><?php echo $row_Fatura['Dt_Emissao']; ?></td>
        <td><?php echo number_format($row_Fatura['NumNota'],0,',','.'); ?></td>
        <td><?php echo number_format($row_Fatura['valor'],2,',','.'); ?></td>
        <td><?php echo $row_Fatura['Dt_Vencimento']; ?></td>
      </tr>
    </table>
    <p>&nbsp;</p>
    <table width="893" border="1">
      <tr>
        <td width="223">Cliente</td>
        <td width="654"><?php echo $row_cliente['RazaoSocial']; ?></td>
      </tr>
      <tr>
        <td>Endere&ccedil;o</td>
        <td><?php echo $row_cliente['Endereco'].' '.$row_cliente['Numero'].' '.$row_cliente['Complemento']; ?></td>
      </tr>
      <tr>
        <td>Cidade/Estado</td>
        <td><?php echo $row_cliente['City'].' / '.$row_cliente['Sigla']; ?></td>
      </tr>
      <tr>
        <td>CNPJ / CPF</td>
        <td><?php 
		if(strlen($row_cliente['doc'])>11){
			echo mascara_string("##.###.###/####-##",str_pad($row_cliente['doc'],14,"0", STR_PAD_LEFT)); 
		}else{
			echo mascara_string("###.###.###-##",str_pad($row_cliente['doc'],11,"0", STR_PAD_LEFT)); 
		}
		?>
        </td>
      </tr>
    </table>
    <p>&nbsp;</p>
    <table width="892" border="1">
      <tr>
        <td width="221">Valor por Extenso</td>
        <td width="655"><?php echo extenso($row_Fatura['valor']); ?></td>
      </tr>
    </table>
    <p>&nbsp;</p>
    <table width="892" border="1">
      <tr>
        <td width="622"><strong>Discrimina&ccedil;&atilde;o dos Despesas</strong></td>
        <td width="101"><strong>Tipo</strong></td>
        <td width="147"><strong>Valores em R$</strong></td>
      </tr>
      <?php do { ?>
        <tr>
          <td><?php echo $row_descricao['Descricao']; ?></td>
          <td><?php echo $row_descricao['Nome']; ?></td>
          <td><?php echo number_format($row_descricao['Valor'],2,',','.'); ?></td>
        </tr>
        <?php } while ($row_descricao = mysql_fetch_assoc($descricao)); ?>
      <tr>
        <td colspan="2"><strong>Total Liquido</strong></td>
        <td><strong><?php echo number_format($row_Fatura['valor'],2,',','.'); ?></strong></td>
    </tr>
    </table>
    <p>&nbsp;</p></td>
  </tr>
</table>
</body>
</html>
<?php
mysql_free_result($empresa);

mysql_free_result($Fatura);

mysql_free_result($cliente);

mysql_free_result($descricao);
?>
