<?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")) {
  $insertSQL = sprintf("INSERT INTO mfin_recebimentos (Cliente_Id, Pasta_Id, Tipo, Forma, Banco_Id, NumeroNota, Emissao, Vencimento, Juros, Multa, Desconto, pis, cofins, csll, ir, Valor, Cheque, Recebimento, Baixadoem, Detalhe) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['Cliente_Id'], "int"),
                       GetSQLValueString($_POST['Pasta_Id'], "int"),
                       GetSQLValueString($_POST['Tipo'], "text"),
                       GetSQLValueString($_POST['Forma'], "int"),
                       GetSQLValueString($_POST['Banco_Id'], "text"),
                       GetSQLValueString($_POST['NumeroNota'], "text"),
                       GetSQLValueString(convertdata($_POST['Emissao']), "date"),
                       GetSQLValueString(convertdata($_POST['Emissao']), "date"),
                       GetSQLValueString(numerobrasil($_POST['Juros']), "double"),
                       GetSQLValueString(numerobrasil($_POST['Multa']), "double"),
                       GetSQLValueString(numerobrasil($_POST['Desconto']), "double"),
                       GetSQLValueString(numerobrasil($_POST['pis']), "double"),
                       GetSQLValueString(numerobrasil($_POST['cofins']), "double"),
                       GetSQLValueString(numerobrasil($_POST['csll']), "double"),
                       GetSQLValueString(numerobrasil($_POST['ir']), "double"),
                       GetSQLValueString(numerobrasil($_POST['Valor']), "double"),
                       GetSQLValueString($_POST['Cheque'], "text"),
					   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());


mysql_select_db($database_paivaconex, $paivaconex);
$query_teste = "SELECT * FROM cli_pasta WHERE id_cli = ".$_POST['Cliente_Id']." 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['Cliente_Id'].", ".$_POST['Pasta_Id'].")";
$teste = mysql_query($query_teste, $paivaconex) or die(mysql_error());


}








}

if ((isset($_GET['del'])) && ($_GET['del'] != "")) {
  $deleteSQL = sprintf("DELETE FROM mfin_recebimentos WHERE Id=%s",
                       GetSQLValueString($_GET['del'], "int"));

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

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_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_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_forma = "SELECT * FROM mfin_formaspagamentos ORDER BY Id ASC";
$forma = mysql_query($query_forma, $paivaconex) or die(mysql_error());
$row_forma = mysql_fetch_assoc($forma);
$totalRows_forma = mysql_num_rows($forma);

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


//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>
<script src="js/jquery-1.8.3.min.js" type="text/javascript"></script>
<script src="SpryAssets/SpryValidationSelect.js" type="text/javascript"></script>
<script src="SpryAssets/SpryValidationTextField.js" type="text/javascript"></script>

<script type="text/javascript">

$(document).ready(function() {
	alert("teste");
	$("select[name=Pasta_Id]").change(function(){
		$.post("clientes_lista.php",{pasta:$(this).val()},function(data){
			$("select[name=Cliente_Id]").html(data);
		})
	})
	
	
	$("#listar").click(function(){
		$.post("clientes_lista_todos.php",{pasta:$(this).val()},function(data){
			$("select[name=Cliente_Id]").html(data);
		})
	})
})
</script>

<link href="SpryAssets/SpryValidationSelect.css" rel="stylesheet" type="text/css" />
<link href="SpryAssets/SpryValidationTextField.css" rel="stylesheet" type="text/css" />
</head>

<body>
<?php include_once('controls/import.php'); ?>
<?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</div>
<p>&nbsp;</p>
<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
  <table align="center">
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Pasta:</td>
      <td><span id="spryselect1">
        <select name="Pasta_Id">
          <option value="433">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="selectRequiredMsg">Selecione um item.</span></span></td>
    </tr>
	    <tr> </tr>    
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Cliente:</td>
      <td><span id="spryselect2">
        <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>
        <span class="selectRequiredMsg">Selecione um item.</span></span>        <input type="button" name="listar" id="listar" value="Listar Todos os Clientes" /></td>
    </tr>

    
    <tr> </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Tipo:</td>
      <td><span id="spryselect3">
        <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));
?>
        </select>
        <span class="selectRequiredMsg">Selecione um item.</span></span></td>
    </tr>
    <tr> </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Forma:</td>
      <td><span id="spryselect4">
        <select name="Forma">
          <?php 
do {  
?>
          <option value="<?php echo $row_forma['Id']?>" ><?php echo $row_forma['Nome']?></option>
          <?php
} while ($row_forma = mysql_fetch_assoc($forma));
?>
        </select>
        <span class="selectRequiredMsg">Selecione um item.</span></span></td>
    </tr>
    <tr> </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Banco:</td>
      <td><span id="spryselect5">
        <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>
        <span class="selectRequiredMsg">Selecione um item.</span></span></td>
    </tr>
    <tr> </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Numero Nota:</td>
      <td><input type="text" name="NumeroNota" value="0" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Data do Recebimento:</td>
      <td><span id="sprytextfield1">
      <input type="text" name="Emissao" value="" size="32" id="Emissao" />
      <span class="textfieldRequiredMsg">Um valor é necessário.</span><span class="textfieldInvalidFormatMsg">Formato inválido.</span></span></td>
    </tr>
    
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Juros:</td>
      <td><span id="sprytextfield2">
      <input type="text" name="Juros" value="0" size="32" />
      <span class="textfieldRequiredMsg">Um valor é necessário.</span><span class="textfieldInvalidFormatMsg">Formato inválido.</span></span></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Multa:</td>
      <td><span id="sprytextfield3">
      <input type="text" name="Multa" value="0" size="32" />
      <span class="textfieldRequiredMsg">Um valor é necessário.</span><span class="textfieldInvalidFormatMsg">Formato inválido.</span></span></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Desconto:</td>
      <td><span id="sprytextfield4">
      <input type="text" name="Desconto" value="0" size="32" />
      <span class="textfieldRequiredMsg">Um valor é necessário.</span><span class="textfieldInvalidFormatMsg">Formato inválido.</span></span></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Pis:</td>
      <td><span id="sprytextfield5">
      <input type="text" name="pis" value="0" size="32" />
      <span class="textfieldRequiredMsg">Um valor é necessário.</span><span class="textfieldInvalidFormatMsg">Formato inválido.</span></span></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Cofins:</td>
      <td><span id="sprytextfield6">
      <input type="text" name="cofins" value="0" size="32" />
      <span class="textfieldRequiredMsg">Um valor é necessário.</span><span class="textfieldInvalidFormatMsg">Formato inválido.</span></span></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Csll:</td>
      <td><span id="sprytextfield7">
      <input type="text" name="csll" value="0" size="32" />
      <span class="textfieldRequiredMsg">Um valor é necessário.</span><span class="textfieldInvalidFormatMsg">Formato inválido.</span></span></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Ir:</td>
      <td><span id="sprytextfield8">
      <input type="text" name="ir" value="0" size="32" />
      <span class="textfieldRequiredMsg">Um valor é necessário.</span><span class="textfieldInvalidFormatMsg">Formato inválido.</span></span></td>
    </tr>
    <tr valign="baseline">
      <td nowrap="nowrap" align="right">Valor:</td>
      <td><span id="sprytextfield9">
      <input type="text" name="Valor" value="0" size="32" />
      <span class="textfieldRequiredMsg">Um valor é necessário.</span><span class="textfieldInvalidFormatMsg">Formato inválido.</span></span></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" valign="top">Detalhe:</td>
      <td>
        <textarea name="Detalhe" cols="50" rows="5"></textarea>
        </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="MM_insert" value="form1" />
</form>
<p>&nbsp;</p>
</div>

</div>

</div>

<?php include_once('controls/footer.php'); ?>
<script type="text/javascript">
var spryselect1 = new Spry.Widget.ValidationSelect("spryselect1");
var spryselect2 = new Spry.Widget.ValidationSelect("spryselect2");
var spryselect3 = new Spry.Widget.ValidationSelect("spryselect3");
var spryselect4 = new Spry.Widget.ValidationSelect("spryselect4");
var spryselect5 = new Spry.Widget.ValidationSelect("spryselect5");
var sprytextfield1 = new Spry.Widget.ValidationTextField("sprytextfield1", "date", {format:"dd/mm/yyyy"});
var sprytextfield2 = new Spry.Widget.ValidationTextField("sprytextfield2", "currency", {format:"dot_comma"});
var sprytextfield3 = new Spry.Widget.ValidationTextField("sprytextfield3", "currency", {format:"dot_comma"});
var sprytextfield4 = new Spry.Widget.ValidationTextField("sprytextfield4", "currency", {format:"dot_comma"});
var sprytextfield5 = new Spry.Widget.ValidationTextField("sprytextfield5", "currency", {format:"dot_comma"});
var sprytextfield6 = new Spry.Widget.ValidationTextField("sprytextfield6", "currency", {format:"dot_comma"});
var sprytextfield7 = new Spry.Widget.ValidationTextField("sprytextfield7", "currency", {format:"dot_comma"});
var sprytextfield8 = new Spry.Widget.ValidationTextField("sprytextfield8", "currency", {format:"dot_comma"});
var sprytextfield9 = new Spry.Widget.ValidationTextField("sprytextfield9", "currency", {format:"dot_comma"});
</script>
</body>
</html>
<?php
mysql_free_result($tipo);

mysql_free_result($pasta);

mysql_free_result($cliente);

mysql_free_result($forma);

mysql_free_result($banco);
?>
