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

//Soma do Valor Total
for ($i = 0; $i < count($_POST['cli_valor']); $i++) {
	$somatorio += numerobrasil($_POST['cli_valor'][$i]);
}

//Lancamento de Honorarios  + Juros
  for ($i = 0; $i < count($_POST['cli_valor']); $i++) {
	  
	//Calculo de Porcentagem 
   	 $percentual = numerobrasil($_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, outros, outros2, outros3, outros4, honorarios) 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($_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($valor_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"),
					   GetSQLValueString(numerobrasil($_POST['outros'][$i]), "double"),
					   GetSQLValueString(numerobrasil($_POST['outros2'][$i]), "double"),
					   GetSQLValueString(numerobrasil($_POST['honorarios'][$i]), "double"),
					   GetSQLValueString(numerobrasil($_POST['repass'][$i]), "double"),
					   GetSQLValueString(numerobrasil($_POST['honorarios2'][$i]), "double"));

  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(numerobrasil($_POST['juros_repass'][$i]), "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['cli_valor'][$i])-numerobrasil($_POST['repass'][$i])-numerobrasil($_POST['outros'][$i])-numerobrasil($_POST['outros2'][$i])-numerobrasil($_POST['honorarios'][$i])-numerobrasil($_POST['honorarios2'][$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_teste = "SELECT * FROM cli_pasta WHERE id_cli = ".$_POST['cli_id'][$i]." AND id_pasta = ".$_POST['Pasta_Id'];
$teste = mysql_query($query_teste, $paivaconex) or die(mysql_error());
$totalRows_teste = mysql_num_rows($teste);
  

if($totalRows_teste == 0){

mysql_select_db($database_paivaconex, $paivaconex);
$query_teste = "INSERT INTO cli_pasta (id_cli, id_pasta) VALUES (".$_POST['cli_id'][$i].", ".$_POST['Pasta_Id'].")";
$teste = mysql_query($query_teste, $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 ORDER BY Id ASC";
$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/SpryValidationSelect.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/SpryValidationSelect.js" type="text/javascript"></script>
<script>
        jQuery(function($){
		   $("#Emissao").mask("99/99/9999");
		 });
        
</script>
<script language="JavaScript"><!--
document.onkeydown = mykeyhandler;

function mykeyhandler() 
{
    if (window.event && window.event.keyCode == 8) 
    {
        // try to cancel the backspace
        window.event.cancelBubble = true;
        window.event.returnValue = false;
        return false;
    }
}
//--></script>


</head>
<body>

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

<div class="content">

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

<div id="conteudo-sit">

<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="778" align="center">
    <tr valign="baseline">
      <td width="205" align="right" nowrap="nowrap">Pasta</td>
      <td width="712"><span id="spryselect1">
        <select name="Pasta_Id">
          <option value="0">Selecione</option>
          <?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>
        <span class="selectInvalidMsg">Selecione um item valido.</span></span></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>
        </td>
    </tr>
    
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Juros R$ </td>
      
      <td><input name="juros" type="text" id="juros" value="0" size="32"  />
      <p>&nbsp;</p>
        <p>&nbsp; </p></td>
    </tr>
    <tr>
    <td colspan="2" align="center"><input name="listar" type="button" value="Listar Todos os Clientes" id="listar" /></td>	
    </tr>
    <tr style="font-size: 9px">
    <td colspan="2" align="right">
    <table width="672" align="right">
    <tr align="center" valign="baseline" style="font-size: 10px">
      <td width="38" nowrap="nowrap" style="font-size: 10px">Cliente</td>
      <td width="130" style="font-size: 10px">Valor R$</td>
      <td width="130" nowrap="nowrap" style="font-size: 10px">Desconto 1  R$:</td>
      <td width="130" style="font-size: 10px">Desconto 2 R$</td>
      <td width="130" style="font-size: 10px">Desconto 3 R$</td>
      
      

    </tr>
    <tr valign="baseline" style="font-size: 10px">
      <td width="38" align="right" nowrap="nowrap" style="font-size: 10px"><span style="font-size: 10px"></span>        <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="130" style="font-size: 10px"><input type="text" name="Valor" value="0" class="numeralbrasil"  />
</td>
      <td width="130" align="right" nowrap="nowrap"><input name="outross" type="text" id="outross" value="0"  class="numeralbrasil"/></td>
      <td width="130"><input name="outros2s" type="text" id="outros2s" value="0"  class="numeralbrasil"/></td>
      <td width="130" align="right"><input name="honorarioss" type="text" id="honorarioss" value="0"  class="numeralbrasil"/></td>
      
      

    </tr>
    <tr align="center">
    <td>
    Honorarios
    </td>
    <td width="130" style="font-size: 10px">Outros R$</td>
    <td width="130" style="font-size: 10px">Juros Cliente R$</td>
    <td style="font-size: 10px">=</td>
    <td width="130" style="font-size: 10px">Repasse R$</td>
    </tr>
    <tr>
    <td><input name="honorarios22" type="text" class="numeralbrasil" id="honorarios22" value="0" /></td>
    <td width="126" align="right"><input name="repasse" type="text" id="repasse" value="0"  class="numeralbrasil"/></td>
    <td width="130">
        <input name="juros_repasse" type="text" id="juros_repasse" value="0" class="numeralbrasil"/>
      </td>
      <td align="center" style="font-size: 10px">=</td>
      <td width="130">
        <input name="resultado" type="text" id="resultado" value="0" class="numeralbrasil"/>
      </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 align="right" valign="bottom" nowrap="nowrap"><p>&nbsp;</p>
        <p>&nbsp; </p>
        Total:</td>
      
      <td>
      <p>&nbsp;</p>
        <p>&nbsp; </p><input name="total" type="text" id="total" value="" size="32" readonly="readonly" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Detalhe:</td>
      <td><span id="sprytextfield1"><span class="textfieldRequiredMsg">Um valor e necessario.</span><span id="sprytextfield3">
        <input type="text" name="Detalhe" id="Detalhe" />
        <span class="textfieldRequiredMsg">Um valor e necessario.</span></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>

</div>

</div>

</div>

<?php include_once('controls/footer.php'); ?>
<script type="text/javascript">
var sprytextfield2 = new Spry.Widget.ValidationTextField("sprytextfield2");
var sprytextfield3 = new Spry.Widget.ValidationTextField("sprytextfield3");
var spryselect1 = new Spry.Widget.ValidationSelect("spryselect1", {isRequired:false, invalidValue:"0"});
</script>
</body>
</html>
<?php
mysql_free_result($cliente);

mysql_free_result($pasta);

mysql_free_result($tipo);

mysql_free_result($banco);

mysql_free_result($formas);
?>
