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

?>
<?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;
}
}

$editFormAction = $_SERVER['PHP_SELF'];
if (isset($_SERVER['QUERY_STRING'])) {
  $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
}

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
 
$somatorio = 0; 
$somatorio_juros = 0; 
$percentual = 0;
$valor_juros = 0;  


//Lancamento de Honorarios  
  for ($i = 0; $i < count($_POST['cli_valor']); $i++) {
    
  $insertSQL = sprintf("INSERT INTO mfin_recebimentos (Pasta_Id, Tipo, Forma, Cheque, Banco_Id, Cliente_Id, NumeroNota, Emissao, Vencimento, Juros, Multa, Desconto, Valor, Recebimento, Baixadoem, Detalhe) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['Pasta_Id'], "int"),
                       GetSQLValueString($_POST['Tipo'], "text"),
                       GetSQLValueString($_POST['Forma'], "int"),
                       GetSQLValueString($_POST['Cheque'], "text"),
                       GetSQLValueString($_POST['Banco_Id'], "text"),
                       GetSQLValueString($_POST['cli_id'][$i], "int"),
                       GetSQLValueString($_POST['NumeroNota'], "int"),
                       GetSQLValueString(convertdata($_POST['Emissao']), "date"),
                       GetSQLValueString(convertdata($_POST['Emissao']), "date"),
                       GetSQLValueString($_POST['Juros'], "double"),
                       GetSQLValueString($_POST['Multa'], "double"),
                       GetSQLValueString($_POST['Desconto'], "double"),
                       GetSQLValueString(numerobrasil($_POST['cli_valor'][$i]), "double"),
                       GetSQLValueString(convertdata($_POST['Emissao']), "date"),
                       GetSQLValueString(convertdata($_POST['Emissao']), "date"),
                       GetSQLValueString($_POST['Detalhe'], "text"));

  mysql_select_db($database_paivaconex, $paivaconex);
  $Result1 = mysql_query($insertSQL, $paivaconex) or die(mysql_error());
  
  $somatorio +=$_POST['cli_valor'][$i];
  
  }

//Lancamento da Receita Fianceira
  for ($i = 0; $i < count($_POST['cli_valor']); $i++) {
    
	$percentual = $_POST['cli_valor'][$i]/ $somatorio;
	 $valor_juros = round(numerobrasil($_POST['juros']) * $percentual, 2);
	 
	 
	//Se for o ultimo do Loop
	if ($i == count($_POST['cli_valor'])-1){
			$valor_juros = numerobrasil($_POST['juros']) - $somatorio_juros;
		}
	
	$somatorio_juros += $valor_juros;
	
	
  $insertSQL = sprintf("INSERT INTO mfin_recebimentos (Pasta_Id, Tipo, Forma, Cheque, Banco_Id, Cliente_Id, NumeroNota, Emissao, Vencimento, Juros, Multa, Desconto, Valor, Recebimento, Baixadoem, Detalhe) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['Pasta_Id'], "int"),
                       GetSQLValueString(19, "text"),
                       GetSQLValueString($_POST['Forma'], "int"),
                       GetSQLValueString($_POST['Cheque'], "text"),
                       GetSQLValueString($_POST['Banco_Id'], "text"),
                       GetSQLValueString($_POST['cli_id'][$i], "int"),
                       GetSQLValueString($_POST['NumeroNota'], "int"),
                       GetSQLValueString(convertdata($_POST['Emissao']), "date"),
                       GetSQLValueString(convertdata($_POST['Emissao']), "date"),
                       GetSQLValueString($_POST['Juros'], "double"),
                       GetSQLValueString($_POST['Multa'], "double"),
                       GetSQLValueString($_POST['Desconto'], "double"),
                       GetSQLValueString($valor_juros, "double"),
                       GetSQLValueString(convertdata($_POST['Emissao']), "date"),
                       GetSQLValueString(convertdata($_POST['Emissao']), "date"),
                       GetSQLValueString($_POST['Detalhe'], "text"));

  mysql_select_db($database_paivaconex, $paivaconex);
  $Result1 = mysql_query($insertSQL, $paivaconex) or die(mysql_error());

  }


//Lancamento de Repasse
  for ($i = 0; $i < count($_POST['cli_valor']); $i++) {
    
  $insertSQL = sprintf("INSERT INTO mfin_pagamentos (Pasta, Fornecedor, FormaPagamento, Cheque, Banco, Cliente, NumFatura, Emissao, Vencimento, Juros, Multa, Desconto, Ir, Pis, Cofins, Csll, Inss, Iss, Valor, Detalhe) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['Pasta_Id'], "int"),
                       GetSQLValueString(167, "int"),
                       GetSQLValueString($_POST['Forma'], "int"),
                       GetSQLValueString($_POST['Cheque'], "text"),
                       GetSQLValueString($_POST['Banco_Id'], "text"),
                       GetSQLValueString($_POST['cli_id'][$i], "int"),
                       GetSQLValueString($_POST['NumeroNota'], "int"),
                       GetSQLValueString(convertdata($_POST['Emissao']), "date"),
                       GetSQLValueString(convertdata($_POST['Emissao']), "date"),
                       GetSQLValueString($_POST['Juros'], "double"),
                       GetSQLValueString($_POST['Multa'], "double"),
                       GetSQLValueString($_POST['Desconto'], "double"),
					   GetSQLValueString(0, "double"),
					   GetSQLValueString(0, "double"),
					   GetSQLValueString(0, "double"),
					   GetSQLValueString(0, "double"),
					   GetSQLValueString(0, "double"),
					   GetSQLValueString(0, "double"),
                       GetSQLValueString(numerobrasil($_POST['repass'][$i])-numerobrasil($_POST['juros_repass'][$i]), "double"),
                       GetSQLValueString($_POST['Detalhe'], "text"));

  mysql_select_db($database_paivaconex, $paivaconex);
  $Result1 = mysql_query($insertSQL, $paivaconex) or die(mysql_error());
  

  
  }
  
  
  

//Lancamento de Juros
  for ($i = 0; $i < count($_POST['cli_valor']); $i++) {
    
  $insertSQL = sprintf("INSERT INTO mfin_pagamentos (Pasta, Fornecedor, FormaPagamento, FormaPagamento_idok, Cheque, Banco, Cliente, NumFatura, Emissao, Vencimento, Juros, Multa, Desconto, Ir, Pis, Cofins, Csll, Inss, Iss, Valor, Detalhe) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['Pasta_Id'], "int"),
                       GetSQLValueString(167, "int"),
                       GetSQLValueString($_POST['Forma'], "int"),
					   GetSQLValueString(1, "int"),
                       GetSQLValueString($_POST['Cheque'], "text"),
                       GetSQLValueString($_POST['Banco_Id'], "text"),
                       GetSQLValueString($_POST['cli_id'][$i], "int"),
                       GetSQLValueString($_POST['NumeroNota'], "int"),
                       GetSQLValueString(convertdata($_POST['Emissao']), "date"),
                       GetSQLValueString(convertdata($_POST['Emissao']), "date"),
                       GetSQLValueString($_POST['Juros'], "double"),
                       GetSQLValueString($_POST['Multa'], "double"),
                       GetSQLValueString($_POST['Desconto'], "double"),
					   GetSQLValueString(0, "double"),
					   GetSQLValueString(0, "double"),
					   GetSQLValueString(0, "double"),
					   GetSQLValueString(0, "double"),
					   GetSQLValueString(0, "double"),
					   GetSQLValueString(0, "double"),
                       GetSQLValueString(numerobrasil($_POST['juros_repass'][$i]), "double"),
                       GetSQLValueString($_POST['Detalhe'], "text"));

  mysql_select_db($database_paivaconex, $paivaconex);
  $Result1 = mysql_query($insertSQL, $paivaconex) or die(mysql_error());
  

  
  }


}








mysql_select_db($database_paivaconex, $paivaconex);
$query_cliente = "SELECT * FROM mfin_clientes ORDER BY NomeFantasia ASC";
$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_pasta = "SELECT * FROM mfin_pastas ORDER BY Numero ASC";
$pasta = mysql_query($query_pasta, $paivaconex) or die(mysql_error());
$row_pasta = mysql_fetch_assoc($pasta);
$totalRows_pasta = mysql_num_rows($pasta);

mysql_select_db($database_paivaconex, $paivaconex);
$query_tipo = "SELECT * FROM mfin_tiposrecebimentos";
$tipo = mysql_query($query_tipo, $paivaconex) or die(mysql_error());
$row_tipo = mysql_fetch_assoc($tipo);
$totalRows_tipo = mysql_num_rows($tipo);

mysql_select_db($database_paivaconex, $paivaconex);
$query_banco = "SELECT * FROM mfin_bancos";
$banco = mysql_query($query_banco, $paivaconex) or die(mysql_error());
$row_banco = mysql_fetch_assoc($banco);
$totalRows_banco = mysql_num_rows($banco);

mysql_select_db($database_paivaconex, $paivaconex);
$query_formas = "SELECT * FROM mfin_formaspagamentos ORDER BY Id ASC";
$formas = mysql_query($query_formas, $paivaconex) or die(mysql_error());
$row_formas = mysql_fetch_assoc($formas);
$totalRows_formas = mysql_num_rows($formas);


//USING
require_once 'controls/verifylogin.php';
?>
<!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>

<title>Módulo Financeiro</title>

<link href="../SpryAssets/SpryValidationTextField.css" rel="stylesheet" type="text/css" />
<link href="../SpryAssets/SpryValidationTextarea.css" rel="stylesheet" type="text/css" />
<?php include_once('controls/import.php'); ?>
<script src="js/jquery-1.8.3.min.js" type="text/javascript"></script>
<script src="js/jquery.maskedinput-1.3.min.js" type="text/javascript"></script>
<script type="text/javascript" src="js/jquery.price_format.1.7.js"></script>
<script src="js/multiplos2.js" type="text/javascript"></script>
<script src="../SpryAssets/SpryValidationTextField.js" type="text/javascript"></script>
<script src="../SpryAssets/SpryValidationTextarea.js" type="text/javascript"></script>
<script>
        jQuery(function($){
		   $("#Emissao").mask("99/99/9999");
		 });
        
        </script>
</head>
<body>

<?php include_once('controls/header.php'); ?>

<div class="content">

<div class="form"><?php include_once('controls/menu.php'); ?>

<div id="conteudo-site">

<div class="titulo-conteudo-site"><img src="images/applications.png"
	title="" alt="" /> Cadastro de Recebimentos - lan&Ccedil;amento multiplo </div>
<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
  <table width="814" align="center">
    <tr valign="baseline">
      <td width="174" align="right" nowrap="nowrap">Pasta</td>
      <td width="555"><select name="Pasta_Id">
        <?php 
do {  
?>
        <option value="<?php echo $row_pasta['Id']?>" ><?php echo $row_pasta['Numero'].' '.$row_pasta['Nome']?></option>
        <?php
} while ($row_pasta = mysql_fetch_assoc($pasta));
?>
        </select></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Tipo:</td>
      <td><select name="Tipo">
        <?php
do {  
?>
        <option value="<?php echo $row_tipo['Id']?>"><?php echo $row_tipo['Nome']?></option>
        <?php
} while ($row_tipo = mysql_fetch_assoc($tipo));
  $rows = mysql_num_rows($tipo);
  if($rows > 0) {
      mysql_data_seek($tipo, 0);
	  $row_tipo = mysql_fetch_assoc($tipo);
  }
?>
      </select></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Forma:</td>
      <td><select name="Forma">
        <?php 
do {  
?>
        <option value="<?php echo $row_formas['Id']?>" ><?php echo $row_formas['Nome']?></option>
        <?php
} while ($row_formas = mysql_fetch_assoc($formas));
?>
        </select></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Cheque:</td>
      <td><input type="text" name="Cheque" value="0" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Banco</td>
      <td><select name="Banco_Id">
        <?php 
do {  
?>
        <option value="<?php echo $row_banco['Id']?>" ><?php echo $row_banco['NomeConta']?></option>
        <?php
} while ($row_banco = mysql_fetch_assoc($banco));
?>
        </select></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Data:</td>
      <td><p><span id="sprytextfield2">
        <input name="Emissao" type="text" value="" size="10" maxlength="10" id="Emissao" />
        <span class="textfieldRequiredMsg">Um valor e necessario.</span></span></p>
        <p>&nbsp;</p>
        <p>&nbsp; </p></td>
    </tr>
    
    
    <tr>
    <td align="center" colspan="2">
    <table width="779" align="center">
    <tr valign="baseline">
      <td width="37" align="right" nowrap="nowrap">Cliente</td>
      <td width="38"><select name="Cliente_Id">
        <?php 
do {  
?>
        <option value="<?php echo $row_cliente['Id']?>" ><?php echo $row_cliente['NomeFantasia']?></option>
        <?php
} while ($row_cliente = mysql_fetch_assoc($cliente));
?>
      </select></td>
      <td width="29" align="right" nowrap="nowrap">Valor R$:</td>
      <td width="157"><span id="sprytextfield1">
      <input type="text" name="Valor" value="0"  />
<span class="textfieldInvalidFormatMsg">Formato invalido.</span></span></td>
      <td width="68" align="right" style="color: #F00"> Repasse R$</td>
      <td width="133"><span id="sprytextfield3">
      <input name="repasse" type="text" id="repasse" value="0" />
<span class="textfieldInvalidFormatMsg">Formato invalido.</span></span></td>
      <td width="99" align="right" style="color: #F00">Juros Repasse R$</td>
      <td width="161"><span id="sprytextfield4">
      <input name="juros_repasse" type="text" id="juros_repasse" value="0" />
<span class="textfieldInvalidFormatMsg">Formato invalido.</span></span></td>
    </tr>
    </table>
    </td>
    </tr>
    
    <tr>
    <td colspan="2" align="center"><p>
      <input name="adicionar" type="button" id="adicionar" value="Adicionar" />
    </p>
      <p>&nbsp;</p>
      <p>&nbsp; </p></td>
    </tr>
    <tr>
    <td colspan="4" align="center">
    <table border="0" align="center" id="descricao">
  		
	</table>

    </td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Juros Receita Financeira</td>
      
      <td><input name="juros" type="text" id="juros" value="0" size="32"  /></td>
    </tr>
        <tr valign="baseline">
      <td nowrap="nowrap" align="right">Total</td>
      
      <td><input name="total" type="text" id="total" value="" size="32" readonly="readonly" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right" valign="top">Detalhe:</td>
      <td><span id="sprytextarea1">
        <textarea name="Detalhe" cols="50" rows="5"></textarea>
        <span class="textareaRequiredMsg">Um valor e necessario.</span></span></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">&nbsp;</td>
      <td><input type="submit" value="Inserir registro" /></td>
    </tr>
  </table>
  <input type="hidden" name="NumeroNota" value="0" />
  <input type="hidden" name="Vencimento" value="0" />
  <input type="hidden" name="Juros" value="0" />
  <input type="hidden" name="Multa" value="0" />
  <input type="hidden" name="Desconto" value="0" />
  <input type="hidden" name="Recebimento" value="0" />
  <input type="hidden" name="Baixadoem" value="" />
  <input type="hidden" name="MM_insert" value="form1" />
</form>
<p>&nbsp;</p>
</div>

</div>

</div>

<?php include_once('controls/footer.php'); ?>
<script type="text/javascript">
var sprytextfield1 = new Spry.Widget.ValidationTextField("sprytextfield1", "currency", {isRequired:false, format:"dot_comma", validateOn:["change"]});
var sprytextfield2 = new Spry.Widget.ValidationTextField("sprytextfield2");
var sprytextfield3 = new Spry.Widget.ValidationTextField("sprytextfield3", "currency", {format:"dot_comma", isRequired:false, validateOn:["change"]});
var sprytextfield4 = new Spry.Widget.ValidationTextField("sprytextfield4", "currency", {format:"dot_comma", isRequired:false, validateOn:["change"]});
var sprytextarea1 = new Spry.Widget.ValidationTextarea("sprytextarea1");
</script>
</body>
</html>
<?php
mysql_free_result($cliente);

mysql_free_result($pasta);

mysql_free_result($tipo);

mysql_free_result($banco);

mysql_free_result($formas);
?>
