<?php

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

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

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

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

if($action == 'save')
{
	

//Atualizando Cli Pasta
require_once('../Connections/paivaconex.php'); 
require_once('funcoes.php'); 



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


}

	
	
	
	try 
	{
		$control->GetData();
		header( 'Location: pagamentos.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>


<link href="../SpryAssets/SpryValidationSelect.css" rel="stylesheet" type="text/css" />
<?php include_once('controls/import.php'); ?>
<script src="js/pagamentos.js" type="text/javascript"></script>
<script src="../SpryAssets/SpryValidationSelect.js" type="text/javascript"></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 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">Banco:</span></td>
		<td><select id="banco" name="banco" class="content-data">
<?php

			$bancos = new BancosModel();

			foreach ($bancos->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">Fornecedor:</span></td>
		<td><select id="fornecedor" name="fornecedor" class="content-data">
<?php

			$fornecedor = new FornecedorModel();

			foreach ($fornecedor->listAll() as $item) {

				$select = '';
				if ($item->getId() == $model->getFornecedor())
				{
					$select = "selected='selected'";
				}
				echo "<option value='{$item->getId()}' {$select}>{$item->getNomeFantasia()}</option>";
			}

			?>

		</select></td>
	</tr>
	<tr>
		<td><span class="title">Emissão:</span></td>
		<td><input type="text" id="emissao" name="emissao" class="content-data"
			style="width: 80px;" onkeyup="formataData(this,event);" maxlength="10" 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);" maxlength="10" value="<?php echo $aux->getDataBR($model->getVencimento());?>" /></td>
	</tr>
    <tr>
		<td><span class="title">Pasta:</span></td>
		<td><span id="spryselect1">
		  <select id="pastas" name="pastas" class="content-data">
		    <option value="433">Selecione</option>
		    <?php

			$pasta = new PastasModel();

			foreach ($pasta->listAll() as $item) {

				$select = '';
				if ($item->getId() == $model->getCliente())
				{
					$select = "selected='selected'";
				}
					
				echo "<option value='{$item->getId()}' {$select}>{$item->getNumero()}  {$item->getNome()}</option>";
			}

			?>
		    </select>
		  <span class="selectInvalidMsg">Selecione um item valido.</span></span></td>
	</tr>
	<tr>
		<td><span class="title">Referencia:</span></td>
		<td><select id="cliente" name="cliente" class="content-data">
<?php

			$cliente = new ClientesModel();

			foreach ($cliente->listAll() as $item) {

				$select = '';
				if ($item->getId() == $model->getCliente())
				{
					$select = "selected='selected'";
				}
					
				echo "<option value='{$item->getId()}' {$select}>{$item->getNomeFantasia()}</option>";
			}

			?>

		</select>
		  <input type="button" name="listar" id="listar" value="Listar Todos os Clientes" /></td>
	</tr>
	
	<tr>
		<td><span class="title">Número da Fatura:</span></td>
		<td><input type="text" id="numfatura" name="numfatura" class="content-data"
			style="width: 80px;" value="<?php echo $model->getNumFatura();?>" /></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: 100px;" onkeyup="formataValor(this,event);" value="<?php echo $aux->getValorBR($model->getValor());?>" /></td>
	</tr>
	<tr>
		<td><span class="title">I.R:</span></td>
		<td><input type="text" id="ir" name="ir" class="content-data"
			style="width: 90px;" onkeyup="formataValor(this,event);" value="<?php echo $aux->getValorBR($model->getIr());?>" /></td>
	</tr>
	<tr>
		<td><span class="title">PIS:</span></td>
		<td><input type="text" id="pis" name="pis" class="content-data"
			style="width: 90px;" onkeyup="formataValor(this,event);" value="<?php echo $aux->getValorBR($model->getPis());?>" /></td>
	</tr>
	<tr>
		<td><span class="title">COFINS:</span></td>
		<td><input type="text" id="cofins" name="cofins" class="content-data"
			style="width: 90px;" onkeyup="formataValor(this,event);" value="<?php echo $aux->getValorBR($model->getCofins());?>" /></td>
	</tr>
	<tr>
		<td><span class="title">CSLL Nacional:</span></td>
		<td><input type="text" id="csll" name="csll" class="content-data"
			style="width: 90px;" onkeyup="formataValor(this,event);" value="<?php echo $aux->getValorBR($model->getCsll());?>" /></td>
	</tr>
	<tr>
		<td><span class="title">INSS:</span></td>
		<td><input type="text" id="inss" name="inss" class="content-data"
			style="width: 90px;" onkeyup="formataValor(this,event);" value="<?php echo $aux->getValorBR($model->getInss());?>" /></td>
	</tr>
	<tr>
		<td><span class="title">ISS:</span></td>
		<td><input type="text" id="iss" name="iss" class="content-data"
			style="width: 90px;" onkeyup="formataValor(this,event);" value="<?php echo $aux->getValorBR($model->getIss());?>" /></td>
	</tr>
	<tr>
		<td><span class="title">Juros:</span></td>
		<td><input type="text" id="juros" name="juros" class="content-data"
			style="width: 60px;" 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: 60px;" 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: 60px;" onkeyup="formataValor(this,event);" value="<?php echo $aux->getValorBR($model->getDesconto());?>" /></td>
	</tr>
	<tr>
		<td><span class="title">Data de Pagamento:</span></td>
		<td><input type="text" id="dtpagamento" name="dtpagamento" class="content-data"
			style="width: 80px;" onkeyup="formataData(this,event);" maxlength="10" value="<?php echo $aux->getDataBR($model->getDtPagamento());?>" /></td>
	</tr>
	<tr>
		<td><span class="title">Forma de Pagamento:</span></td>
		<td><select id=formapagamento name="formapagamento" class="content-data">
<?php

			$formapagamento = new FormasPagamentosModel();

			foreach ($formapagamento->listAll() as $item) {

				$select = '';
				if ($item->getId() == $model->getFormaPagamento())
				{
					$select = "selected='selected'";
				}
					
				echo "<option value='{$item->getId()}' {$select}>{$item->getNome()}</option>";
			}

			?>

		</select></td>
	</tr>
	<tr>
		<td><span class="title">Cheque:</span></td>
		<td><input type="text" id="cheque" name="cheque" class="content-data"
			style="width: 300px;" 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='pagamentos.php'" /></div>

</form>

</div>

</div>

</div>

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