<?php require_once('../Connections/paivaconex.php'); ?>
<?php require_once('../Connections/paivaconex.php'); ?>
<?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_fornecedores (Tipo, Cnpj, NomeFantasia, Conta, ContaResultado) VALUES (%s, %s, %s, %s, %s)",
                       GetSQLValueString($_POST['Tipo'], "text"),
                       GetSQLValueString($_POST['Cnpj'], "text"),
                       GetSQLValueString($_POST['NomeFantasia'], "text"),
                       GetSQLValueString($_POST['Conta'], "int"),
                       GetSQLValueString($_POST['ContaResultado'], "int"));

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

if ((isset($_POST["MM_update"])) && ($_POST["MM_update"] == "form1")) {
  $updateSQL = sprintf("UPDATE mfin_fornecedores SET Tipo=%s, Cnpj=%s, NomeFantasia=%s, Telefone=%s, Conta=%s, ContaResultado=%s WHERE Id=%s",
                       GetSQLValueString($_POST['Tipo'], "text"),
                       GetSQLValueString($_POST['Cnpj'], "text"),
                       GetSQLValueString($_POST['NomeFantasia'], "text"),
                       GetSQLValueString($_POST['Telefone'], "text"),
                       GetSQLValueString($_POST['Conta'], "int"),
                       GetSQLValueString($_POST['ContaResultado'], "int"),
                       GetSQLValueString($_POST['Id'], "int"));

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

mysql_select_db($database_paivaconex, $paivaconex);
$query_conta = "SELECT * FROM mfin_contas ORDER BY Nome ASC";
$conta = mysql_query($query_conta, $paivaconex) or die(mysql_error());
$row_conta = mysql_fetch_assoc($conta);
$totalRows_conta = mysql_num_rows($conta);
$query_conta = "SELECT * FROM mfin_contas ORDER BY Numero ASC";
$conta = mysql_query($query_conta, $paivaconex) or die(mysql_error());
$row_conta = mysql_fetch_assoc($conta);
$totalRows_conta = mysql_num_rows($conta);

mysql_select_db($database_paivaconex, $paivaconex);
$query_conta2 = "SELECT * FROM mfin_contas ORDER BY Numero ASC";
$conta2 = mysql_query($query_conta2, $paivaconex) or die(mysql_error());
$row_conta2 = mysql_fetch_assoc($conta2);
$totalRows_conta2 = mysql_num_rows($conta2);

$colname_fornecedor = "-1";
if (isset($_GET['id'])) {
  $colname_fornecedor = $_GET['id'];
}
mysql_select_db($database_paivaconex, $paivaconex);
$query_fornecedor = sprintf("SELECT * FROM mfin_fornecedores WHERE Id = %s", GetSQLValueString($colname_fornecedor, "int"));
$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';

?>
<!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>M&#63464;dulo Financeiro</title>

<link href="../SpryAssets/SpryValidationTextField.css" rel="stylesheet" type="text/css" />
<?php include_once('controls/import.php'); ?>
<script src="js/fornecedores.js" type="text/javascript"></script>
<script src="../SpryAssets/SpryValidationTextField.js" type="text/javascript"></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 Fornecedores</div>
  <p>&nbsp;</p>
  <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">Tipo:</td>
        <td><select name="Tipo">
          <option value="pf" <?php if (!(strcmp("pf", htmlentities($row_fornecedor['Tipo'], ENT_COMPAT, 'iso-8859-1')))) {echo "SELECTED";} ?>>Pessoa Fisica</option>
          <option value="pj" <?php if (!(strcmp("pj", htmlentities($row_fornecedor['Tipo'], ENT_COMPAT, 'iso-8859-1')))) {echo "SELECTED";} ?>>Pessoa Jurídica</option>
        </select></td>
      </tr>
      <tr valign="baseline">
        <td nowrap="nowrap" align="right">CPF / CNPJ:</td>
        <td><span id="sprytextfield2">
        <input type="text" name="Cnpj" value="<?php echo htmlentities($row_fornecedor['Cnpj'], ENT_COMPAT, 'iso-8859-1'); ?>" size="32" />
<span class="textfieldInvalidFormatMsg">Formato inválido.</span></span></td>
      </tr>
      <tr valign="baseline">
        <td nowrap="nowrap" align="right">Nome</td>
        <td><span id="sprytextfield1">
          <input type="text" name="NomeFantasia" value="<?php echo htmlentities($row_fornecedor['NomeFantasia'], ENT_COMPAT, 'iso-8859-1'); ?>" size="32" />
          <span class="textfieldRequiredMsg">Um valor é necessário.</span></span></td>
      </tr>
      <tr valign="baseline">
        <td nowrap="nowrap" align="right">Telefone:</td>
        <td><input type="text" name="Telefone" value="<?php echo htmlentities($row_fornecedor['Telefone'], ENT_COMPAT, 'iso-8859-1'); ?>" size="32" /></td>
      </tr>
      <tr valign="baseline">
        <td nowrap="nowrap" align="right">Conta:</td>
        <td><select name="Conta">
          <?php 
do {  
?>
          <option value="<?php echo $row_conta['Id']?>" <?php if (!(strcmp($row_conta['Id'], htmlentities($row_fornecedor['Conta'], ENT_COMPAT, 'iso-8859-1')))) {echo "SELECTED";} ?>><?php echo $row_conta['Numero'].' '.$row_conta['Nome']?></option>
          <?php
} while ($row_conta = mysql_fetch_assoc($conta));
?>
        </select></td>
      </tr>
      <tr> </tr>
      <tr valign="baseline">
        <td nowrap="nowrap" align="right">Conta Resultado</td>
        <td><select name="ContaResultado">
          <?php 
do {  
?>
          <option value="<?php echo $row_conta2['Id']?>" <?php if (!(strcmp($row_conta2['Id'], htmlentities($row_fornecedor['ContaResultado'], ENT_COMPAT, 'iso-8859-1')))) {echo "SELECTED";} ?>><?php echo $row_conta2['Numero'].' '.$row_conta2['Nome']?></option>
          <?php
} while ($row_conta2 = mysql_fetch_assoc($conta2));
?>
        </select></td>
      </tr>
      <tr> </tr>
      <tr valign="baseline">
        <td nowrap="nowrap" align="right">&nbsp;</td>
        <td><input type="submit" value="Atualizar registro" /></td>
      </tr>
    </table>
    <input type="hidden" name="Id" value="<?php echo $row_fornecedor['Id']; ?>" />
    <input type="hidden" name="MM_update" value="form1" />
    <input type="hidden" name="Id" value="<?php echo $row_fornecedor['Id']; ?>" />
  </form>
  <p>&nbsp;</p>
</div>

</div>

</div>

			<?php include_once('controls/footer.php'); ?>
<script type="text/javascript">
var sprytextfield1 = new Spry.Widget.ValidationTextField("sprytextfield1");
var sprytextfield2 = new Spry.Widget.ValidationTextField("sprytextfield2", "integer", {isRequired:false});
</script>
</body>
</html>
<?php
mysql_free_result($conta);

mysql_free_result($conta2);

mysql_free_result($fornecedor);
?>
