Пример #1
0
        /// <summary>
        /// Retorna no layout CNAB240
        /// </summary>
        public override string Remessa()
        {
            // Limpa os objetos de saida/entrada
            Data.Clear();
            Clear();

            string[] cAgenciaDig = Cedente.Agencia.Split('-');

            // Header do Arquivo
            regHeaderArquivo[CNAB240HeaderArquivoCaixa.InscricaoTipo]    = Cedente.Tipo;
            regHeaderArquivo[CNAB240HeaderArquivoCaixa.InscricaoNumero]  = Cedente.DocumentoNumeros;
            regHeaderArquivo[CNAB240HeaderArquivoCaixa.Agencia]          = cAgenciaDig[0];
            regHeaderArquivo[CNAB240HeaderArquivoCaixa.AgenciaDAC]       = cAgenciaDig[1];
            regHeaderArquivo[CNAB240HeaderArquivoCaixa.CodigoCedente]    = Cedente.CodCedente;
            regHeaderArquivo[CNAB240HeaderArquivoCaixa.EmpresaNome]      = Cedente.Cedente;
            regHeaderArquivo[CNAB240HeaderArquivoCaixa.Data]             = DataHoje;
            regHeaderArquivo[CNAB240HeaderArquivoCaixa.NumeroLote]       = NumeroLote;
            regHeaderArquivo[CNAB240HeaderArquivoCaixa.ReservadoEmpresa] = Producao ? "REMESSA-PRODUÇÃO" : "REMESSA-TESTE";
            Add(regHeaderArquivo);

            // Header do lote
            regHeaderLote[CNAB240HeaderLoteCaixa.Lote]                 = SequencialLote;
            regHeaderLote[CNAB240HeaderLoteCaixa.InscricaoTipo]        = Cedente.Tipo;
            regHeaderLote[CNAB240HeaderLoteCaixa.CodigoConvenio]       = Cedente.Convenio;
            regHeaderLote[CNAB240HeaderLoteCaixa.InscricaoNumero]      = Cedente.DocumentoNumeros;
            regHeaderLote[CNAB240HeaderLoteCaixa.Agencia]              = cAgenciaDig[0];
            regHeaderLote[CNAB240HeaderLoteCaixa.AgenciaDAC]           = cAgenciaDig[1];
            regHeaderLote[CNAB240HeaderLoteCaixa.CodigoCedente]        = Cedente.CodCedente;
            regHeaderLote[CNAB240HeaderLoteCaixa.EmpresaNome]          = Cedente.Cedente;
            regHeaderLote[CNAB240HeaderLoteCaixa.NumeroRemessaRetorno] = NumeroLote;
            regHeaderLote[CNAB240HeaderLoteCaixa.Data]                 = DataHoje;
            Add(regHeaderLote);

            BoletoInfo boleto;
            SacadoInfo sacado;
            Reg <CNAB240SegmentoPCaixa> regP;
            Reg <CNAB240SegmentoQCaixa> regQ;

            SequencialRegistro = 1;
            double ValorTotal = 0;

            foreach (string n in this.Boletos.NossoNumeros)
            {
                boleto = Boletos[n];
                sacado = boleto.Sacado;

                // Define as informações do segmento P
                regP = new Reg <CNAB240SegmentoPCaixa>();

                regP[CNAB240SegmentoPCaixa.Lote]               = SequencialLote;
                regP[CNAB240SegmentoPCaixa.Nregistro]          = SequencialRegistro++;
                regP[CNAB240SegmentoPCaixa.Agencia]            = cAgenciaDig[0];
                regP[CNAB240SegmentoPCaixa.AgenciaDAC]         = cAgenciaDig[1];
                regP[CNAB240SegmentoPCaixa.CodigoCedente]      = Cedente.CodCedente;
                regP[CNAB240SegmentoPCaixa.CarteiraModalidade] = Cedente.Modalidade;
                regP[CNAB240SegmentoPCaixa.NossoNumero]        = boleto.NossoNumero;
                regP[CNAB240SegmentoPCaixa.NumeroDocumento]    = boleto.NumeroDocumento;
                regP[CNAB240SegmentoPCaixa.Vencimento]         = boleto.DataVencimento;
                regP[CNAB240SegmentoPCaixa.ValorDocumento]     = boleto.ValorDocumento;
                if (boleto.ValorMora >= 0.01)
                {
                    regP[CNAB240SegmentoPCaixa.Juros]     = 1;
                    regP[CNAB240SegmentoPCaixa.JurosData] = boleto.DataVencimento.AddDays(1);
                    regP[CNAB240SegmentoPCaixa.JurosMora] = boleto.ValorMora;
                }
                else
                {
                    regP[CNAB240SegmentoPCaixa.Juros] = 3; // isento
                }
                regP[CNAB240SegmentoPCaixa.Especie]           = (int)boleto.Especie;
                regP[CNAB240SegmentoPCaixa.Aceite]            = boleto.Aceite;
                regP[CNAB240SegmentoPCaixa.Emissao]           = boleto.DataDocumento;
                regP[CNAB240SegmentoPCaixa.UsoEmpresaCedente] = boleto.NossoNumero;
                int nDiasBaixa;
                if (boleto.DiasProtesto > 1)
                {
                    regP[CNAB240SegmentoPCaixa.ProtestoCodigo]       = 1;
                    regP[CNAB240SegmentoPCaixa.ProtestoPrazo]        = boleto.DiasProtesto;
                    regP[CNAB240SegmentoPCaixa.BaixaDevolucaoCodigo] = 2; // Não Baixar / Não Devolver
                    // Baixar em no minimo 8 dias apos o protesto baseado na data de geração do arquivo
                    nDiasBaixa = 0;                                       // boleto.DataVencimento.AddDays(boleto.DiasProtesto + 8).Subtract(DateTime.Now).Days;
                }
                else
                {
                    regP[CNAB240SegmentoPCaixa.ProtestoCodigo]       = 3;                            // não protestar
                    regP[CNAB240SegmentoPCaixa.BaixaDevolucaoCodigo] = boleto.DiasBaixa > 0 ? 1 : 2; // 1 => Baixar / Devolver
                    if (boleto.DiasBaixa > 0)
                    {
                        nDiasBaixa = boleto.DataVencimento.AddDays(boleto.DiasBaixa).Subtract(DataHoje).Days;
                    }
                    else
                    {
                        nDiasBaixa = 0;
                    }
                }
                if (nDiasBaixa <= 0)
                {
                    nDiasBaixa = 0;
                }
                else if (nDiasBaixa < 5)
                {
                    nDiasBaixa = 5;
                }
                else if (nDiasBaixa > 120)
                {
                    nDiasBaixa = 120;
                }

                regP[CNAB240SegmentoPCaixa.BaixaDevolucaoPrazo] = nDiasBaixa;
                ValorTotal += boleto.ValorDocumento;

                // Define as informações do segmento Q
                regQ = new Reg <CNAB240SegmentoQCaixa>();
                regQ[CNAB240SegmentoQCaixa.Lote]          = SequencialLote;
                regQ[CNAB240SegmentoQCaixa.Nregistro]     = SequencialRegistro++;
                regQ[CNAB240SegmentoQCaixa.Sacado_Tipo]   = boleto.Sacado.Tipo;
                regQ[CNAB240SegmentoQCaixa.Sacado_Numero] = CobUtil.GetLong(CobUtil.SoNumeros(boleto.Sacado.DocumentoNumeros));
                regQ[CNAB240SegmentoQCaixa.Nome]          = boleto.Sacado.Sacado;
                regQ[CNAB240SegmentoQCaixa.Endereco]      = boleto.Sacado.Endereco;
                regQ[CNAB240SegmentoQCaixa.Bairro]        = boleto.Sacado.Bairro;
                regQ[CNAB240SegmentoQCaixa.CEP]           = boleto.Sacado.CepNumeros;
                regQ[CNAB240SegmentoQCaixa.Cidade]        = boleto.Sacado.Cidade;
                regQ[CNAB240SegmentoQCaixa.UF]            = boleto.Sacado.UF;
                long avalista = CobUtil.GetLong(boleto.Sacado.AvalistaNumeros);
                if (avalista > 0)
                {
                    regQ[CNAB240SegmentoQCaixa.Avalista_Tipo]   = boleto.Sacado.AvalistaTipo;
                    regQ[CNAB240SegmentoQCaixa.Avalista_Numero] = avalista;
                    regQ[CNAB240SegmentoQCaixa.Avalista_Nome]   = boleto.Sacado.Avalista;
                }

                // adiciona o boleto convertido em registro
                // AddSegmentoPQ(regP, regQ);
                AddBoleto(regP, boleto);
                AddBoleto(regQ, boleto);

                AddOpcionais(boleto);
            }

            regTrailerLote[CNAB240TrailerLoteCaixa.Lote]          = SequencialLote;
            regTrailerLote[CNAB240TrailerLoteCaixa.QTD]           = SequencialRegistro + 1; // tem que incluir o header
            regTrailerLote[CNAB240TrailerLoteCaixa.CobrancaQTD]   = this.Boletos.Count;
            regTrailerLote[CNAB240TrailerLoteCaixa.CobrancaValor] = ValorTotal;
            Add(regTrailerLote);

            regTrailerArquivo[CNAB240TrailerArquivoCaixa.LotesQTD]     = 1;
            regTrailerArquivo[CNAB240TrailerArquivoCaixa.RegistrosQTD] = itens.Count + 1;
            Add(regTrailerArquivo);

            // Gera o Texto de saida da forma padrão
            return(this.Conteudo);
        }