<?php require_once('../Connections/paivaconex.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;
}
}

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


//USING
require_once 'controls/verifylogin.php';
require_once '../Class/model/RecebimentosModel.php';
require_once '../Class/controller/RecebimentosControl.php';
require_once '../Class/controller/TiposRecebimentosControl.php';
require_once '../Class/model/ClientesModel.php';
require_once '../Class/model/PastasModel.php';
require_once '../Class/model/BancosModel.php';
require_once '../Class/model/FormasPagamentosModel.php';
include_once('../Database/MySQL.Aux.php');

$control = new RecebimentosControl();
$model = new RecebimentosModel();
$aux = new MySQLAux();

$control->setAction();
$control->setId();

$action = $control->getAction();
$id = $control->getId();

//LoadDados
if ($id > 0)
{
	$model->load($id);
}

if($action == 'save')
{
	try 
	{
		$control->GetData();
		header('Location: recebimentos.php');
	}
	catch(Exception $err)
	{
		echo "<script>alert('Ocorreu um problema ao salvar o registro.');</script>";
	}
}
?>

<!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>

<?php include_once('controls/import.php'); ?>
<script src="js/recebimentos.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 Recebimentos</div>

<form id="frmRegister" method="post" action=""><input type="hidden"
	id="hdnAction" name="action" value="save" /> <input type="hidden"
	id="hdnId" name="Id" value="<?php echo $model->getId();?>" />

<table border="0" cellpadding="3" cellspacing="0" class="tblGeral"
	width="100%">

	<tr>
		<td><span class="title">Cliente:</span></td>
		<td>
		<select id="cliente" name="cliente" class="content-data">
<?php 
			
				$clientes = new ClientesModel();
				
				foreach ($clientes->listAll() as $item) {
				
					$select = '';
					if ($item->getId() == $model->getCliente())
					{
						$select = "selected='selected'";
					}
					
					echo "<option value='{$item->getId()}' {$select}>{$item->getNomeFantasia()}</option>";
				}
			?>
		
		</select></td>
	</tr>

	<tr>
		<td><span class="title">Pasta:</span></td>
		<td>
		<select id="pasta" name="pasta" class="content-data">
<?php 
			
				$pastas = new PastasModel();
				
				foreach ($pastas->listAll() as $item) {
				
					$select = '';
					if ($item->getId() == $model->getPasta())
					{
						$select = "selected='selected'";
					}
					
					echo "<option value='{$item->getId()}' {$select}>{$item->getNumero()} {$item->getNome()}</option>";
				}
			?>
		
		</select></td>
	</tr>

	<tr>
		<td><span class="title">Tipo:</span></td>
		<td><select id="tipo" name="tipo" class="content-data">
		  <?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>
		<td><span class="title">Forma de Recebimento:</span></td>
		<td>
		<select id="forma" name="forma" class="content-data">
<?php 
			
				$formaPagamento = new FormasPagamentosModel();
				
				foreach ($formaPagamento->listAll() as $item) {
				
					$select = '';
					if ($item->getId() == $model->getForma())
					{
						$select = "selected='selected'";
					}
					
					echo "<option value='{$item->getId()}' {$select}>{$item->getNome()}</option>";
				}
			?>
		
		</select></td>
	</tr>

	<tr>
		<td><span class="title">Banco:</span></td>
		<td>
		<select id="banco" name="banco" class="content-data">
<?php 
			
				$banco = new BancosModel();
				
				foreach ($banco->listAll() as $item) {
				
					$select = '';
					if ($item->getId() == $model->getBanco())
					{
						$select = "selected='selected'";
					}
					
					echo "<option value='{$item->getId()}' {$select}>{$item->getNomConta()}</option>";
				}
			?>
		
		</select></td>
	</tr>

	<tr>
		<td><span class="title">Número Nota:</span></td>
		<td><input type="text" id="numeronota" name="numeronota" class="content-data"
			style="width: 190px;"  value="<?php echo $model->getNumeroNota();?>" /></td>
	</tr>

	<tr>
		<td><span class="title">Emissao:</span></td>
		<td><input type="text" id="emissao" name="emissao" class="content-data"
			style="width: 80px;" onkeyup="formataData(this,event);" value="<?php echo $aux->getDataBR($model->getEmissao());?>" /></td>
	</tr>

	<tr>
		<td><span class="title">Vencimento:</span></td>
		<td><input type="text" id="vencimento" name="vencimento" class="content-data"
			style="width: 80px;" onkeyup="formataData(this,event);" value="<?php echo $aux->getDataBR($model->getVencimento());?>" maxlength="10" /></td>
	</tr>

	<tr>
		<td><span class="title">Juros:</span></td>
		<td><input type="text" id="juros" name="juros" class="content-data"
			style="width: 100px;" onkeyup="formataValor(this,event);" value="<?php echo $aux->getValorBR($model->getJuros());?>" /></td>
	</tr>

	<tr>
		<td><span class="title">Multa:</span></td>
		<td><input type="text" id="multa" name="multa" class="content-data"
			style="width: 100px;" onkeyup="formataValor(this,event);" value="<?php echo $aux->getValorBR($model->getMulta());?>" /></td>
	</tr>

	<tr>
		<td><span class="title">Desconto:</span></td>
		<td><input type="text" id="desconto" name="desconto" class="content-data"
			style="width: 80px;" onkeyup="formataValor(this,event);" value="<?php echo $aux->getValorBR($model->getDesconto());?>" /></td>
	</tr>

	<tr>
		<td><span class="title">Detalhe:</span></td>
		<td><input type="text" id="detalhe" name="detalhe" class="content-data"
			style="width: 300px;" value="<?php echo $model->getDetalhe();?>" /></td>
	</tr>

	<tr>
		<td><span class="title">Valor:</span></td>
		<td><input type="text" id="valor" name="valor" class="content-data"
			style="width: 80px;" onkeyup="formataValor(this,event);" value="<?php echo $aux->getValorBR($model->getValor());?>" /></td>
	</tr>

	<tr>
		<td><span class="title">Data de Recebimento:</span></td>
		<td><input type="text" id="recebimento" name="recebimento" class="content-data"
			style="width: 80px;" onkeyup="formataData(this,event);" maxlength="10" value="<?php echo $aux->getDataBR($model->getDtRecebimento());?>" /></td>
	</tr>
	
	<tr>
		<td><span class="title">Cheque:</span></td>
		<td><input type="text" id="cheque" name="cheque" class="content-data"
			style="width: 80px;" value="<?php echo $model->getCheque(); ?>" /></td>
	</tr>

</table>

<div id="pBotoes" style="text-align: right"><span>&nbsp;</span> <br />
<input type="submit" id="btnSalvar" name="btnSalvar" class="submit" value="Salvar" /> <input
	type="button" name="btnCancelar" class="submit" value="Cancelar"
	onclick="window. location.href='recebimentos.php'" /></div>

</form>

</div>

</div>

</div>

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

</body>
</html>
<?php
mysql_free_result($tipo);
?>
