Public function PesquisaCentroCustoEmpresa($pesquisa, $Cod) {

	$option = "";
	$CentroaAlteracao = false;
	$String = new CString();
	$ObjModel = new ModellancaCrBd();
	$resultado = parent::ListaCentroCusto(trim($pesquisa));
	if ($Cod)
	    $CentroaAlteracao = $ObjModel->RecuperaCampo("CENTRO_CUSTO_ID", "F_LANCA_CR", "F_LANCA_CR_ID", $String->descriptografa($Cod));


	if ($resultado) {
	    foreach ($resultado as $ListaCentro){
		$select = "";
		if ($CentroaAlteracao == $ListaCentro->getIdCentroCusto()) {
		    $select = ' selected="selected" ';
		}

		$option .= '<option' . $select . ' value="' . $ListaCentro->getIdCentroCusto() . '">' . $ListaCentro->getNomeCentroCusto() . '</option>';
	    }
	    $Retorno = '<label class="col-sm-2 control-label" for="combobox">Centro Custo *</label>
			<div class="col-sm-3">
			    <select  name="centro_custo" id="centro_custo"  class="form-control input-sm" required="required">
				<option value=""></option>' .
		    $option . '
		    </select>
			</div>
		    </div>';
	    echo $Retorno;
	    exit();
	} else {
	    echo 0;
	    exit();
	}
    }
      public function InformacoesRecebimento($IdCr) {

	$Sql = "SELECT
		EMP.EMPRESA_ID,
		CR.F_LANCA_CR_ID,			
		F_LANCA_CR2.VALOR,	
		RAZAO_SOCIAL,		
		DATA_VENCIMENTO,				
		NUM_PARCELA,
		COD_PARCELAMENTO_PAI,
		F_LANCA_CR2_ID,
		(SELECT COUNT(COD_PARCELAMENTO) FROM F_LANCA_CR WHERE COD_PARCELAMENTO = CR.COD_PARCELAMENTO) AS TOTAL_PARCELAS,
		BANCO.BANCO_ID AS COD_CC, 
		BANCO.VALOR_MULTA,
		BANCO.VALOR_JURO,
		NUMERO_NF
		FROM  F_LANCA_CR CR
		INNER JOIN F_LANCA_CR2  ON (F_LANCA_CR2.F_LANCA_CR2_ID = (SELECT MAX(CR2.F_LANCA_CR2_ID) FROM F_LANCA_CR2 CR2 WHERE CR2.F_LANCA_CR_ID = CR.F_LANCA_CR_ID))
		LEFT JOIN EMPRESA  EMP ON (CR.EMPRESA_ID = EMP.EMPRESA_ID)
		INNER JOIN BANCO ON (CR.BANCO_ID = BANCO.BANCO_ID)
		WHERE CR.F_LANCA_CR_ID = $IdCr";

	$lista = array();

	$Result = parent::execute_query($Sql);

	while ($linha = parent::criaObjetoConsulta($Result)){
	    $contaReceber = new ModellancaCrBd();
	    $contaReceber->setId_cliente($linha->EMPRESA_ID);
	    $contaReceber->setConta_receber_id($linha->F_LANCA_CR_ID);
	    $contaReceber->setValor($linha->VALOR);
	    $contaReceber->setRazao_social_cli($linha->RAZAO_SOCIAL);
	    $contaReceber->setData_vencimento($linha->DATA_VENCIMENTO);
	    $contaReceber->setCodFilho($linha->F_LANCA_CR2_ID);
	    $contaReceber->setCodParcela($linha->NUM_PARCELA);
	    $contaReceber->setTotalParcelas($linha->TOTAL_PARCELAS);
	    $contaReceber->setVlrMora($linha->VALOR_JURO);
	    $contaReceber->setVlrMulta($linha->VALOR_MULTA);
	    $contaReceber->setCodContaCorrente($linha->COD_CC);
	    $contaReceber->setNumeroNF($linha->NUMERO_NF);

	    array_push($lista, $contaReceber);
	}
	return $lista;
    }
    public function EfetuaRecebimentoBoletos($boleto, $acrescimo, $vlrRecebido, $valor_desconto, $id_usu_recebimento, $data_recebimento, $obs, $vencimento, $commit, $transacao, $codigo_retorno) {
	$ModeloCr = new ModellancaCrBd();
	if ($ModeloCr->EfetuaRecebimento($boleto, $acrescimo, $vlrRecebido, $valor_desconto, $id_usu_recebimento, $data_recebimento, $obs, '2014-01-01', $commit, $transacao, $codigo_retorno)) {
	    return true;
	} else {
	    return false;
	}
    }
    Public Function BuscaDetalhesFechamento($dia) {

	//Busca os fechamentos realizados para saque no dia em questão
	$sql = "SELECT CR.COD_PARCELAMENTO"
		. " FROM F_CONSOLIDACAO_DIA CON"
		. " INNER JOIN F_LANCA_CR CR ON (CON.F_LANCA_CR_ID = CR.F_LANCA_CR_ID)"
		. " INNER JOIN F_LANCA_CR2  ON (F_LANCA_CR2.F_LANCA_CR2_ID = (SELECT MAX(F_LANCA_CR2.F_LANCA_CR2_ID) FROM F_LANCA_CR2 WHERE F_LANCA_CR2.F_LANCA_CR_ID = CR.F_LANCA_CR_ID))"
		. " WHERE CON.DIA_MOVIMENTACAO = '$dia'";


	$result = parent::execute_query($sql);
	if ($result) {
	    $StringIN = "";

	    while ($linha = parent::criaObjetoConsulta($result)){
		$StringIN .= $linha->COD_PARCELAMENTO . " ,";
	    }
	    $StringIN = substr($StringIN, 0, -1);

	    $ModelCr = new ModellancaCrBd();
	    if ($ListaOperacoes = $ModelCr->DadosLancamento($id_cr = false, $StringIN)) {
		return $ListaOperacoes;
	    } else {
		return false;
	    }
	}
	return false;
    }
    public function BaixaCr() {
	$CR = new ModellancaCrBd();
	$Data = new Data();
	$String = new CString();

	$sql = "SELECT * FROM CONTA_RECEBER WHERE VALOR IS NOT NULL AND DT_VENCIMENTO IS NOT NULL";

	if ($result = parent::execute_query($sql)) {

	    $CR = new ModellancaCrBd();
	    $Data = new Data();
	    $String = new CString();

	    while ($linha = parent::criaObjetoConsulta($result)){

		$linha->CONTA_RECEBER_ID;
		$linha->GRUPO_ID;
		$linha->NOME_CONTA;
		$linha->EMPRESA_ID;
		$linha->TIPO_DOCUMENTO;
		$linha->NUMERO_DOCUMENTO;
		$linha->DT_VENCIMENTO;
		$linha->VALOR;
		$linha->REPLICAR_MESES;
		$linha->LANCAMENTO_AUTOMATICO;
		$linha->STATUS;
		$linha->OBSERVACAO;
		$linha->DATA_INCLUSAO;
		$linha->DESCONTO;
		$linha->ACRESCIMO;
		$linha->NUMERO_DOCUMENTO_RECEBIMENTO;
		$linha->OBSERVACAO_RECEBIMENTO;
		$linha->VALOR_RECEBIDO;
		$linha->ORIGEM_CONTA_RECEBER_ID;
		$linha->SYNC;
		$linha->CONTA_BAIXA;
		$linha->DT_RECEBIMENTO;
		$linha->FORMA_PAGAMENTO;
		$linha->NUMERO_FATURA;
		$linha->FATURADO;
		$linha->PAGO;
		$linha->BANCO_ID;
		$linha->FILIAL_ID;
		$linha->CENTRO_CUSTO_ID;
		$linha->NUMERO_PARCELA;
		$linha->UCIDUSER;
		$linha->UCIDUSER_BAIXA;
		$linha->DATA_BAIXA;

		$linha->CONTA_RECEBER_ID;

		if ((trim($linha->PAGO)) == "S") {
		    $id_cr = parent::RecuperaCampo("F_LANCA_CR_ID", "F_LANCA_CR", "ID_INTEGRACAO", $linha->CONTA_RECEBER_ID);

		    if ($CR->EfetuaRecebimento(2, $id_cr, 0.00, $linha->VALOR_RECEBIDO, 0.00, $linha->UCIDUSER_BAIXA, $linha->DT_RECEBIMENTO, $linha->OBSERVACAO_RECEBIMENTO, $linha->DT_VENCIMENTO, false, false, false)) {
			break;
			return false;
		    }
		}
	    }
	}
    }
    public function CalculaValorFaturaMenosImposto($Imposto, $idCr, $trans = false) {

	$ModelCr = new ModellancaCrBd();

	$Valor = $ValorFatura = $ModelCr->ValorReferenciaEncargos($idCr, $trans);

	return (float) number_format(($Valor - $Imposto), 2, '.', '');
    }
    /**
     * @return string
     */
    Public function ImpressaoComprovanteFaturamento() {

	$Dados = new ModellancaCrBd();

	$StatusLancamento = $Dados->RecuperaCampo("F_STATUS_FINANCEIRO_ID", "F_LANCA_CR", "F_LANCA_CR_ID", $this->getCodLancamento());

	/* if ($StatusLancamento != 2) {
	  $this->setMotivoErro("Lançamento não encontra-se faturado");
	  return false;
	  } */

	$ManipulaBanco = new ManipulaBanco();
	$AssinaturaImagem = "../../include/img/" . $ManipulaBanco->RecuperaCampo("ASSINATURA_RECIBO_IMAGEM", "PARAMETRO", 1, 1);
	$AssinaturaNome = $ManipulaBanco->RecuperaCampo("ASSINATURA_RECIBO_NOME", "PARAMETRO", 1, 1);


	$Dados = $InformacoesImpressao = $Dados->DadosLancamento($this->getCodLancamento(), false, false, 2)[0];

	$Mora = (float) $Dados->getVlrMora();
	$Multa = (float) $Dados->getVlrMulta();
	$VlrImposto = (float) $Dados->getVlrImposto();
	$ValorFatura = (float) $Dados->getValor();
	$ValorDesconto = (float) $Dados->getVlrDescontoFatura();
	$Encargos = number_format(($Mora + $Multa), 2);

	$modelImposto = new ModelImposto();

	$DadosImposto = $modelImposto->DadosImpostoFromCR($Dados->getConta_receber_id());

	$ISS = 0.00;
	$INSS = 0.00;
	$PIS = 0.00;
	$COFINS = 0.00;
	$CSLL = 0.00;
	$IRPJ = 0.00;
	$totalImposto = 0.00;
	$PISS = 0.00;
	$PINSS = 0.00;
	$PPIS = 0.00;
	$PCOFINS = 0.00;
	$PCSLL = 0.00;
	$PIRPJ = 0.00;

	$ValorDesconto = (float) !empty($ValorDesconto) ? $ValorDesconto : 0.00;

	$DadosImposto = $DadosImposto[0];
	//Calculo Imposto
	$Dados->getValor();
	$ValorAnterior = (float) (($ValorFatura + ($VlrImposto + $ValorDesconto) - ($Mora + $Multa)));
	$Outros = 0.00;
	$ISS = number_format(($DadosImposto['ISS'] * $ValorAnterior / 100), 2);
	$INSS = number_format(($DadosImposto['INSS'] * $ValorAnterior / 100), 2);
	$PIS = number_format(($DadosImposto['PIS'] * $ValorAnterior / 100), 2);
	$COFINS = number_format(($DadosImposto['COFINS'] * $ValorAnterior / 100), 2);
	$CSLL = number_format(($DadosImposto['CSLL'] * $ValorAnterior / 100), 2);
	$IRPJ = number_format(($DadosImposto['IRPJ'] * $ValorAnterior / 100), 2);
	$totalImposto = number_format(($ISS + $INSS + $PIS + $COFINS + $CSLL + $IRPJ), 2);
	$PISS = $DadosImposto['ISS'];
	$PINSS = $DadosImposto['INSS'];
	$PPIS = $DadosImposto['PIS'];
	$PCOFINS = $DadosImposto['COFINS'];
	$PCSLL = $DadosImposto['CSLL'];
	$PIRPJ = $DadosImposto['IRPJ'];

	if ($totalImposto == 0) {
	    $totalImposto = number_format($Dados->getVlrImposto(), 2);
	    $Outros = $Dados->getVlrImposto();
	}
	$Outros = number_format($Outros, 2);

	$VlrLiquido = (float) ($ValorAnterior - $totalImposto - $ValorDesconto + ($Mora + $Multa));

	$StrDescricao = "<br><br><br><b>RECEBI DE </b>" . $Dados->getRazao_social_cli() . " </b><br><br>"
		. "<b>A IMPORTÂNCIA DE </b> " . parent::escreverValorMoeda($Dados->getValor()) . "</b><br><br>"
		. "<b>REFERENTE A </b>" . $Dados->getCentroReceita() . "<br><br><br>"
		. "";
	$impostos = "
				<tr >
					<td width='33%' style='font-size: 08pt'>ISS</td>
					<td width='33%' align='center' style='font-size: 08pt'>" . $PISS . "%</td>
					<td width='33%' align='right' style='font-size: 08pt'>" . $ISS . "</td>
				</tr>
				<tr>
					<td width='33%' style='font-size: 08pt'>INSS</td>
					<td width='33%' style='font-size: 08pt' align='center'>" . $PINSS . "%</td>
					<td width='33%' style='font-size: 08pt' align='right'>$INSS</td>
				</tr>
				<tr>
					<td width='33%' style='font-size: 08pt'>PIS</td>
					<td width='33%' style='font-size: 08pt'align='center'>" . $PPIS . "%</td>
					<td width='33%' style='font-size: 08pt' align='right'>$PIS</td>
				</tr>
				<tr>
					<td width='33%' style='font-size: 08pt'>COFINS</td>
					<td width='33%' align='center' style='font-size: 08pt'>" . $PCOFINS . "%</td>
					<td width='33%' align='right' style='font-size: 08pt'>$COFINS</td>
				</tr>
				<tr>
					<td width='33%' style='font-size: 08pt'>CSLL</td>
					<td width='33%' style='font-size: 08pt' align='center' >" . $PCSLL . "%</td>
					<td width='33%' style='font-size: 08pt' align='right'>$CSLL</td>
				</tr>
				<tr>
					<td width='33%' style='font-size: 08pt'>IRPJ</td>
					<td width='33%' align='center' style='font-size: 08pt'>" . $PIRPJ . "%</td>
					<td width='33%' align='right' style='font-size: 08pt'>$IRPJ</td>
				</tr>			
				<tr>
					<td width='33%' style='font-size: 08pt'>Outros</td>
					<td width='33%' align='center' style='font-size: 08pt'>-</td>
					<td width='33%' align='right' style='font-size: 08pt'>$Outros</td>
				</tr>";

	$HtmlPrimeiraVia = '
    <head>
	<meta charset="utf-8">
	<title>COMPROVANTE DE FATURAMENTO</title>
	<style>
		td{
			font-size: 9pt;
		};
		
		
	</style>
    </head>
    <body>
	<table cellspacing="0" cellpadding="0"  width="657"  border=1 style="border-collapse: collapse; font-size:9pt">
	    <tr>
		<td align=center> 
			<img src="' . $this->getLogoClinica() . '" width="200" height="80">
		</td>
		<td colspan=2> 
			
			
			<div align="center">
			<span style="font-size:9pt;" align="center" >
			  ' . $this->getCabecalho() . '
			</span>
			</div>	
		</td>
	    <tr>
	    <tr>
		<td align="center" bgcolor="#B7B6B6" colspan=3 style="font-size:9pt;">COMPROVANTE DE FATURAMENTO  - 1º VIA - VALOR(R$) ' . number_format($Dados->getValor(), 2) . '</td>
	    <tr>

	   
	    </tr>
	    <tr>
		<td colspan=3 align="left" style="font-size:9pt">
		<div style="float: left; width: 30%;margin-bottom: 10px;margin-top: 10px">

			<table  width="10%" style="border-style: ridge;border-color: #080808;border-width: 1px">
				<tbody>
					<tr>
						<td colspan="2" bgcolor="#B7B6B6"><b>Valor Bruto</b></td>
						<td colspan="1" bgcolor="#B7B6B6">R$' . number_format($ValorAnterior, 2) . '</td>
					</tr>
					<tr>
						<td><b>Imposto</b></td>
						<td><b>%</b></td>
						<td align="right"><b>Valor</b></td>
					</tr>
					' . $impostos . '
					<tr>
						<td colspan="2" bgcolor="#ffb6c1">Impostos</td>
						<td colspan="1" align="right" bgcolor="#ffb6c1">R$ ' . number_format($totalImposto, 2) . '</td>
					</tr>
					<tr>
						<td colspan="2" bgcolor="#f5f5dc"><b>Decontos</b></td>
						<td colspan="1" align="right" bgcolor="#f5f5dc">R$ ' . number_format($ValorDesconto, 2) . '</td>
					</tr>
					<tr>
						<td colspan="2" bgcolor="#F08080"><b>Encargos</b></td>
						<td colspan="1" align="right" bgcolor="#F08080">R$ ' . $Encargos . '</td>
					</tr>
					<tr>
						<td colspan="2" bgcolor="#7fffd4"><b>Vlr líquido </b></td>
						<td colspan="1" bgcolor="#7fffd4" align="right">R$' . number_format($VlrLiquido, 2) . '</td>
					</tr>
				</tbody>
			</table>
		</div>
		<div style="float: right; width: 70%;">
			' . $StrDescricao . '
		</div>
		</td>
	    </tr>	
	       <tr style="height:27.25pt" >
		<td colspan=1 align=center>	
			<strong><span style="font-size:08pt; ">CIDADE</span></strong>
		    <p style="margin-bottom:0pt; line-height:normal;">
		
			<span style="font-size:08pt">' . $this->getCidadeClinica() . ' / ' . $this->getUfClinica() . '-' . date('d/m/Y') . '</span>
		    </p>
		</td>
		<td colspan=2 align=center>
			<img src="' . $AssinaturaImagem . '" width="140" height="40">
		    <hr align="center" width="350" style="line-height:1">
		    <p style="line-height:0" align="center">  ' . $AssinaturaNome . '</p>
		</td>
	    </tr>
	</table>
	<div align=center  style="font-size:09pt";>
	</div>
    </body>';

	$HtmlSegundaVia = '

    <head>
	<meta charset="utf-8">
	<title>COMPROVANTE DE FATURAMENTO</title>
    </head>
    <body>
	<table cellspacing="0" cellpadding="0"  width="657"  border=1 style="border-collapse: collapse; font-size:10pt">
	    <tr>
		<td align=center> 
			<img src="' . $this->getLogoClinica() . '" width="200" height="80">
		</td>
		<td colspan=2> 
			
			
			<div align="center">
			<span style="font-size:9pt;" align="center" >
			   ' . $this->getCabecalho() . '
			</span>
			</div>	
		</td>
	    <tr>
	    <tr>
		<td align="center" bgcolor="#B7B6B6" colspan=3 style="font-size:9pt;">COMPROVANTE DE FATURAMENTO - 2º VIA  VALOR(R$) ' . number_format($Dados->getValor(), 2) . '</td>
	    <tr>
	   
	    </tr>
	    <tr>
		<td colspan=2 align="left" style="font-size:9pt">
		<div style="float: left; width: 30%;margin-bottom: 10px;margin-top: 10px">
			<table  width="10%" style="border-style: ridge;border-color: #080808;border-width: 1px">
				<tbody>
					<tr>
						<td colspan="2" bgcolor="#B7B6B6"><b>Valor Bruto</b></td>
						<td colspan="1" bgcolor="#B7B6B6">R$' . number_format($ValorAnterior, 2) . '</td>
					</tr>
					<tr>
						<td><b>Imposto</b></td>
						<td><b>%</b></td>
						<td align="right"><b>Valor</b></td>
					</tr>
					' . $impostos . '
					<tr>
						<td colspan="2" bgcolor="#ffb6c1">Impostos</td>
						<td colspan="1" align="right" bgcolor="#ffb6c1">R$ ' . number_format($totalImposto, 2) . '</td>
					</tr>
					<tr>
						<td colspan="2" bgcolor="#f5f5dc"><b>Decontos</b></td>
						<td colspan="1" align="right" bgcolor="#f5f5dc">R$ ' . number_format($ValorDesconto, 2) . '</td>
					</tr>
					
					<tr>
						<td colspan="2" bgcolor="#F08080"><b>Encargos</b></td>
						<td colspan="1" align="right" bgcolor="#F08080">R$ ' . $Encargos . '</td>
					</tr>
					<tr>
						<td colspan="2" bgcolor="#7fffd4"><b>Vlr líquido </b></td>
						<td colspan="1" bgcolor="#7fffd4" align="right">R$' . number_format($VlrLiquido, 2) . '</td>
					</tr>
				</tbody>
			</table>
		</div>
		<div style="float: right; width: 70%;">
			' . $StrDescricao . '
		</div>
		</td>
	    </tr>
	       <tr style="height:27.25pt" >
		<td colspan=1 align=center>	
			<strong><span style="font-size:08pt; ">CIDADE</span></strong>
		    <p style="margin-bottom:0pt; line-height:normal;">
		
			<span style="font-size:08pt">' . $this->getCidadeClinica() . ' / ' . $this->getUfClinica() . '-' . date('d/m/Y') . '</span>
		    </p>
		</td>
		<td colspan=2 align=center>
			<img src="' . $AssinaturaImagem . '" width="140" height="40">
		    <hr align="center" width="350" style="line-height:1">
		    <p style="line-height:0" align="center">  ' . $AssinaturaNome . '</p>
		</td>
	    </tr>
	</table>
	<div align=center  style="font-size:09pt";>
	</div>
    </body>';



	$Html = $HtmlPrimeiraVia . "<br>--------------------------------------------------------------------------------------------------------------------------<br>" . $HtmlSegundaVia;

	$this->setHtmlRecibo($Html);
	return $Html;
    }
    private function FaturaLancamento() {
	$CData = New Data();
	$ModelCr = new ModellancaCrBd();
	$data = date("Y-m-d H:i:s");

	$codCr2 = $ModelCr->UltimoFilho($this->getCodLancamentoParaFaturar(), $this->getTransacao());
	$DataVencimento = $CData->convertDataSql($this->DiaVencimentoTabela());

	//Imposto
	$Imposto = $this->CalculaValorImposto();

	if (!$ModelCr->FaturaLancamento($codCr2, "0.00", "0.00", "0.00", $this->getValor(), "0", $data, $DataVencimento, "", "", $_SESSION["id_usuario"], "0.00", "0.00", "0.00", $Imposto, null, $this->getTransacao(), false)) {
	    $this->setMsg($ModelCr->getMotivoErro() . "\n\nNão foi possível faturar o lançamento de " . $this->getTipoFechamento() . " referente ao fechamento " . $this->getCodFechamentoSigmo() . " ID: " . $this->getIdFechamento() . "\n\n" . $ModelCr->getMotivoErro());
	    $this->setStatusOperacao(true);
	    $erroGravar = true;
	}
	return true;
    }
    Public function ParcelaAtualTotalParcelas($idCr) {

	$Model = new ModellancaCrBd();

	$Model->StringTotalParcelasParcelaAtual($idCr);

	return $Model->getCodParcela() . " / " . $Model->getTotalParcelas();
    }