<?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_pagamentos_2 (Banco, Fornecedor, Emissao, Vencimento, Pasta, Cliente, NumFatura, Valor, Ir, Pis, Cofins, Csll, Inss, Iss, Juros, Multa, Desconto, diferenca, outros, DtPagamento, FormaPagamento, Cheque, Detalhe) VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['Banco'], "int"),
                       GetSQLValueString($_POST['Fornecedor'], "int"),
                       GetSQLValueString(convertdata($_POST['Emissao']), "date"),
                       GetSQLValueString(convertdata($_POST['DtPagamento']), "date"),
                       GetSQLValueString($_POST['pastas'], "int"),
                       GetSQLValueString($_POST['cliente'], "int"),
                       GetSQLValueString($_POST['NumFatura'], "text"),
                       GetSQLValueString(numerobrasil($_POST['Valor']), "double"),
                       GetSQLValueString(numerobrasil($_POST['Ir']), "double"),
                       GetSQLValueString(numerobrasil($_POST['Pis']), "double"),
                       GetSQLValueString(numerobrasil($_POST['Cofins']), "double"),
                       GetSQLValueString(numerobrasil($_POST['Csll']), "double"),
                       GetSQLValueString(numerobrasil($_POST['Inss']), "double"),
                       GetSQLValueString(numerobrasil($_POST['Iss']), "double"),
                       GetSQLValueString(numerobrasil($_POST['Juros']), "double"),
                       GetSQLValueString(numerobrasil($_POST['Multa']), "double"),
                       GetSQLValueString(numerobrasil($_POST['Desconto']), "double"),
                       GetSQLValueString(numerobrasil($_POST['diferenca']), "double"),
                       GetSQLValueString(numerobrasil($_POST['outros']), "double"),
                       GetSQLValueString(convertdata($_POST['DtPagamento']), "date"),
                       GetSQLValueString($_POST['FormaPagamento'], "int"),
                       GetSQLValueString($_POST['Cheque'], "text"),
                       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']." AND id_pasta = ".$_POST['pastas'];
$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'].", ".$_POST['pastas'].")";
$teste = mysql_query($query_teste, $paivaconex) or die(mysql_error());


}






}

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

mysql_select_db($database_paivaconex, $paivaconex);
$query_fornecedor = "SELECT * FROM mfin_fornecedores ORDER BY NomeFantasia ASC";
$fornecedor = mysql_query($query_fornecedor, $paivaconex) or die(mysql_error());
$row_fornecedor = mysql_fetch_assoc($fornecedor);
$totalRows_fornecedor = mysql_num_rows($fornecedor);


//USING
require_once 'controls/verifylogin.php';
require_once '../Class/model/PagamentosModel.php';
require_once '../Class/controller/PagamentosControl.php';
require_once '../Class/model/BancosModel.php';
require_once '../Class/model/FornecedoresModel.php';
require_once '../Class/model/ClientesModel.php';
require_once '../Class/model/PastasModel.php';
require_once '../Class/model/FormasPagamentosModel.php';
include_once('../Database/MySQL.Aux.php');

$control = new PagamentosControl();
$model = new PagamentosModel();
$aux = new MySQLAux();



?>
<!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=windows-1252">

<title>Módulo Financeiro</title>
<link href="../SpryAssets/SpryValidationSelect.css" rel="stylesheet" type="text/css" />
<link href="../SpryAssets/SpryValidationTextField.css" rel="stylesheet" type="text/css" />
<?php include_once('controls/import.php'); ?>
<script src="js/pagamentos.js" type="text/javascript"></script>
<script src="js/jquery-1.8.3.min.js" type="text/javascript"></script>
<script src="js/jquery.mask.js" type="text/javascript"></script>
<script src="../SpryAssets/SpryValidationSelect.js" type="text/javascript"></script>
<script src="../SpryAssets/SpryValidationTextField.js" type="text/javascript"></script>



<script>
        jQuery(function($){
		   $("#Emissao").mask("99/99/9999");
		   $("#Vencimento").mask("99/99/9999");
		   $("#DtPagamento").mask("99/99/9999");
                   $(".numeralbrasil").mask('000.000.000.000,00', {reverse: true});
		 });
        
</script>





<script type="text/javascript">




$(document).ready(function() {
	
	$("select[name=pastas]").change(function(){
		$.post("clientes_lista.php",{pasta:$(this).val()},function(data){
			$("select[name=cliente]").html(data);
		})
	})
	
	
	$("#listar").click(function(){
		$.post("clientes_lista_todos.php",{pasta:$(this).val()},function(data){
			$("select[name=cliente]").html(data);
		})
	})
});

</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 Pagamentos</div>
<form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
  <table align="left">
    <tr valign="baseline">
      <td align="right" nowrap="nowrap" style="color: #60F">Banco:</td>
      <td><span id="spryselect1">
        <select name="Banco" tabindex="1">
          <?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 valign="baseline">
      <td align="right" nowrap="nowrap" style="color: #60F">Fornecedor:</td>
      <td><span id="spryselect2">
        <select name="Fornecedor" tabindex="2">
          <?php 
do {  
?>
          <option value="<?php echo $row_fornecedor['Id']?>" ><?php echo $row_fornecedor['NomeFantasia']?></option>
          <?php
} while ($row_fornecedor = mysql_fetch_assoc($fornecedor));
?>
          </select>
        <span class="selectRequiredMsg">Selecione um item.</span></span></td>
    </tr>
    <tr valign="baseline">
      <td align="right" nowrap="nowrap" style="color: #60F">Emissao:</td>
      <td><span id="sprytextfield1">
        <input type="text" name="Emissao" id="Emissao" value="" size="32" tabindex="3"/>
        <span class="textfieldRequiredMsg">Um valor e necessario.</span><span class="textfieldInvalidFormatMsg">Formato invalido.</span></span></td>
    </tr>
    <tr valign="baseline">
      <td align="right" nowrap="nowrap" style="color: #60F">Pasta:</td>
      <td><select name="pastas" id="pastas" tabindex="4">
        <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></td>
    </tr>
    <tr valign="baseline">
      <td align="right" nowrap="nowrap" style="color: #60F">Referencia:</td>
      <td><select name="cliente" id="cliente" tabindex="5">
        <?php 
do {  
?>
        <option value="<?php echo $row_cliente['Id']?>" ><?php echo $row_cliente['NomeFantasia']?></option>
        <?php
} while ($row_cliente = mysql_fetch_assoc($cliente));
?>
        </select>
        <input type="button" name="listar" id="listar" value="Listar Todos os Clientes" />
        
        </td>
    </tr>
    <tr valign="baseline">
      <td align="right" nowrap="nowrap" style="color: #60F">Numero da Fatura:</td>
      <td><input type="text" name="NumFatura" value="" size="32" tabindex="6"/></td>
    </tr>
    <tr valign="baseline">
      <td align="right" nowrap="nowrap" style="color: #60F">Valor:</td>
      <td><span id="sprytextfield4">
      <input type="text" name="Valor" value="0,00" size="32" class="numeralbrasil" tabindex="7"/>
      <span class="textfieldRequiredMsg">Um valor e necessario.</span><span class="textfieldInvalidFormatMsg">Formato invalido.</span></span></td>
    </tr>
    <tr valign="baseline">
      <td align="right" nowrap="nowrap" style="color: #60F">I.R:</td>
      <td><input type="text" name="Ir" value="0,00" size="32" class="numeralbrasil"/></td>
    </tr>
    <tr valign="baseline">
      <td align="right" nowrap="nowrap" style="color: #60F">PIS:</td>
      <td><input type="text" name="Pis" value="0,00" size="32" class="numeralbrasil" /></td>
    </tr>
    <tr valign="baseline">
      <td align="right" nowrap="nowrap" style="color: #60F">COFINS:</td>
      <td><input type="text" name="Cofins" value="0,00" size="32" class="numeralbrasil" /></td>
    </tr>
    <tr valign="baseline">
      <td align="right" nowrap="nowrap" style="color: #60F">CSLL:</td>
      <td><input type="text" name="Csll" value="0,00" size="32" class="numeralbrasil"/></td>
    </tr>
    <tr valign="baseline">
      <td align="right" nowrap="nowrap" style="color: #60F">INSS:</td>
      <td><input type="text" name="Inss" value="0,00" size="32" class="numeralbrasil"/></td>
    </tr>
    <tr valign="baseline">
      <td align="right" nowrap="nowrap" style="color: #60F">ISS:</td>
      <td><input type="text" name="Iss" value="0,00" size="32" class="numeralbrasil"/></td>
    </tr>
    <tr valign="baseline">
      <td align="right" nowrap="nowrap" style="color: #60F">Juros:</td>
      <td><input type="text" name="Juros" value="0,00" size="32" class="numeralbrasil"/></td>
    </tr>
    <tr valign="baseline">
      <td align="right" nowrap="nowrap" style="color: #60F">Multa:</td>
      <td><input type="text" name="Multa" value="0,00" size="32" class="numeralbrasil"/></td>
    </tr>
    <tr valign="baseline">
      <td align="right" nowrap="nowrap" style="color: #60F">Desconto:</td>
      <td><input type="text" name="Desconto" value="0,00" size="32" class="numeralbrasil"/></td>
    </tr>
    <tr valign="baseline">
      <td align="right" nowrap="nowrap" style="color: #60F">Data de Pagamento:</td>
      <td><span id="sprytextfield3">
        <input type="text" name="DtPagamento" id="DtPagamento" value="" size="32" tabindex="8"/>
        <span class="textfieldInvalidFormatMsg">Formato invalido.</span></span></td>
    </tr>
    <tr valign="baseline">
      <td align="right" nowrap="nowrap" style="color: #60F">Forma de Pagamento:</td>
      <td><span id="spryselect3">
        <select name="FormaPagamento" tabindex="9">
          <?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 valign="baseline">
      <td align="right" nowrap="nowrap" style="color: #60F">Cheque:</td>
      <td><input type="text" name="Cheque" value="0" size="32" /></td>
    </tr>
    <tr valign="baseline">
      <td align="right" valign="top" nowrap="nowrap" style="color: #60F">Detalhe:</td>
      <td><textarea name="Detalhe" cols="50" rows="5" tabindex="10"></textarea></td>
    </tr>
    <tr valign="baseline">
      <td align="right" nowrap="nowrap" style="color: #60F">&nbsp;</td>
      <td><input type="submit" value="Inserir registro" tabindex="11"/></td>
    </tr>
  </table>
  <input type="hidden" name="MM_insert" value="form1" />
  <input type="hidden" name="diferenca" value="0,00"  />
  <input type="hidden" name="outros" value="0,00"  />
</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 sprytextfield1 = new Spry.Widget.ValidationTextField("sprytextfield1", "date", {format:"dd/mm/yyyy"});
var sprytextfield3 = new Spry.Widget.ValidationTextField("sprytextfield3", "date", {isRequired:false, format:"dd/mm/yyyy"});
var spryselect3 = new Spry.Widget.ValidationSelect("spryselect3");
var sprytextfield4 = new Spry.Widget.ValidationTextField("sprytextfield4", "currency", {format:"dot_comma"});
</script>
</body>
</html>
<?php
mysql_free_result($pasta);

mysql_free_result($cliente);

mysql_free_result($forma);

mysql_free_result($banco);

mysql_free_result($fornecedor);
?>
