Пример #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);
        }
Пример #2
0
        /// <summary>
        /// Gera o aquivo baseado nas coleção de todas informações passada até o momento
        /// </summary>
        /// <returns></returns>
        public override string Remessa()
        {
            // Existe algum BUG na executação de template e reflection com ActiveX que  dentro do doreach de conteudo da classe e layout acaba por não gerar o arquivo final.
            // Então, cada linha já será gerada imediatamente em uma String Builder, liberando assim a necessidade de memorizar os itens que iriam conter no arquivo.

            string[] cBanco        = Cedente.Banco.Split('-');
            string[] cAgDig        = Cedente.Agencia.Split('-');
            string   cContaDigitos = CobUtil.SoNumeros(Cedente.Conta);

            Bancos banco = (Bancos)CobUtil.GetInt(cBanco[0]);

            if (!(banco == Bancos.SANTANDER || banco == Bancos.BANESPA_SANTANDER))
            {
                throw new Exception("Esta classe é valida apenas para o Santander");
            }
            else if (cContaDigitos.Length != 10)
            {
                throw new Exception("Erro na Conta não tem 10 dígitos");
            }
            else if (Cedente.CodCedente.Length != 7)
            {
                throw new Exception("Erro no CodCedente não tem 7 dígitos");
            }
            else if (Cedente.CedenteCOD.Length != 20)
            {
                throw new Exception("Erro no CedenteCOD não tem 20 dígitos");
            }
            else if (Cedente.Convenio.Length != 25)
            {
                throw new Exception("Erro no Convenio não tem 25 dígitos");
            }

            regArqHeader[CNAB400SantanderHeader.Empresa_Codigo] = Cedente.CedenteCOD;
            regArqHeader[CNAB400SantanderHeader.Empresa_Nome]   = Cedente.Cedente;
            regArqHeader[CNAB400SantanderHeader.Banco_Codigo]   = (int)banco;
            regArqHeader[CNAB400SantanderHeader.Data]           = DataHoje;

            // Limpa os objetos de saida/entrada
            Data.Clear();
            Clear();

            // Inicia o contador Sequencial
            regArqHeader[CNAB400SantanderHeader.Sequencia] = SequencialRegistro = 1;

            // o sequencial do header é sempre 1 (FIXO)
            Add(regArqHeader);

            // Proximo item
            SequencialRegistro = 2;

            BoletoInfo boleto;
            SacadoInfo sacado;
            Reg <CNAB400SantanderRemessa1> regBoleto;

            double nValor = 0;

#if TEST_LOG
            Log += "Remessa " + Boletos.Count + "!\r\n";
#endif
            foreach (string n in Boletos.NossoNumeros)
            {
                boleto  = Boletos[n];
                sacado  = boleto.Sacado;
                nValor += boleto.ValorDocumento;
#if TEST_LOG
                Log += string.Format("{0} {1} {2:C}", n, boleto.NossoNumero, boleto.ValorDocumento);
#endif
                regBoleto = new Reg <CNAB400SantanderRemessa1>();

                regBoleto[CNAB400SantanderRemessa1.CedenteTipo]     = Cedente.Tipo;
                regBoleto[CNAB400SantanderRemessa1.CedenteCNPJ]     = Cedente.DocumentoNumeros;
                regBoleto[CNAB400SantanderRemessa1.CedenteCOD]      = Cedente.CedenteCOD;
                regBoleto[CNAB400SantanderRemessa1.CedenteControle] = Cedente.Convenio;
                regBoleto[CNAB400SantanderRemessa1.Banco]           = 33;
                regBoleto[CNAB400SantanderRemessa1.CarteiraTipo]    = Cedente.CarteiraTipo;
                regBoleto[CNAB400SantanderRemessa1.Agencia]         = Cedente.CarteiraTipo == "5" ? cAgDig[0] : "0";


                regBoleto[CNAB400SantanderRemessa1.NossoNumero] = boleto.NossoNumero;
#if TEST_LOG
                Log += string.Format(" ? {0} ? {1}\r\n", regBoleto[CNAB400SantanderRemessa1.NossoNumero], boleto.NossoNumero);
#endif
                regBoleto[CNAB400SantanderRemessa1.NossoNumeroDig]  = CobUtil.Modulo11Especial(boleto.NossoNumero, 9);
                regBoleto[CNAB400SantanderRemessa1.MultaTipo]       = boleto.PercentualMulta == 0 ? 0 : 4;
                regBoleto[CNAB400SantanderRemessa1.MultaPercentual] = boleto.PercentualMulta;
                regBoleto[CNAB400SantanderRemessa1.Ocorrencia]      = (int)boleto.Ocorrencia;
                regBoleto[CNAB400SantanderRemessa1.SeuNumero]       = boleto.BoletoID;
                regBoleto[CNAB400SantanderRemessa1.DataVencimento]  = boleto.DataVencimento;
                regBoleto[CNAB400SantanderRemessa1.Valor]           = boleto.ValorDocumento;
                regBoleto[CNAB400SantanderRemessa1.Especie]         = (int)boleto.Especie;
                regBoleto[CNAB400SantanderRemessa1.Aceite]          = boleto.Aceite;
                regBoleto[CNAB400SantanderRemessa1.DataDocumento]   = boleto.DataDocumento;

                int nInstrucao1 = boleto.Instrucao1;
                int nInstrucao2 = boleto.Instrucao2;

                if (nInstrucao1 == 0 && nInstrucao2 > 0)
                {
                    nInstrucao1 = nInstrucao2;
                    nInstrucao2 = 0;
                }

                // Prioriza protesto na instrução 1
                if (nInstrucao1 > 0)
                {
                    regBoleto[CNAB400SantanderRemessa1.Instrucao1] = nInstrucao1;
                    if (nInstrucao1 == 6)
                    {
                        if (boleto.DiasProtesto == 0)
                        {
                            throw new Exception("Não é possivel dar instrução de protesto sem 'DiasProtesto'");
                        }

                        regBoleto[CNAB400SantanderRemessa1.DiasProtesto] = boleto.DiasProtesto;
                    }
                }

                // Trata a mora na segunda instrução (Se tem Mora maior de 1 centavo adiciona a mora)
                if (boleto.ValorMora > 0.01)
                {
                    regBoleto[CNAB400SantanderRemessa1.Mora] = boleto.ValorMora;
                }
                else // Caso contrario não cobra
                {
                    if (nInstrucao1 == 0)
                    {
                        regBoleto[CNAB400SantanderRemessa1.Instrucao1] = 8;
                    }
                    else
                    {
                        regBoleto[CNAB400SantanderRemessa1.Instrucao2] = 8;
                    }
                }

                // Mas se a instrução 2 algo diferente adicona também, talvez sobrescrevendo...
                if (nInstrucao2 != 0)
                {
                    regBoleto[CNAB400SantanderRemessa1.Instrucao2] = nInstrucao2;
                }

                regBoleto[CNAB400SantanderRemessa1.DataDesconto]     = boleto.DataDesconto;
                regBoleto[CNAB400SantanderRemessa1.ValorIOF]         = boleto.ValorIOF;
                regBoleto[CNAB400SantanderRemessa1.ValorDesconto]    = boleto.ValorDesconto;
                regBoleto[CNAB400SantanderRemessa1.ValorAbatimento]  = boleto.ValorOutras < 0 ? -boleto.ValorOutras : 0;
                regBoleto[CNAB400SantanderRemessa1.Sacado_Tipo]      = sacado.Tipo;
                regBoleto[CNAB400SantanderRemessa1.Sacado_Inscricao] = sacado.DocumentoNumeros;
                regBoleto[CNAB400SantanderRemessa1.Nome]             = sacado.Sacado;
                regBoleto[CNAB400SantanderRemessa1.Endereco]         = sacado.Endereco;
                regBoleto[CNAB400SantanderRemessa1.Bairro]           = sacado.Bairro;
                regBoleto[CNAB400SantanderRemessa1.Cidade]           = sacado.Cidade;
                regBoleto[CNAB400SantanderRemessa1.UF]       = sacado.UF;
                regBoleto[CNAB400SantanderRemessa1.CEP]      = sacado.CepNumeros;
                regBoleto[CNAB400SantanderRemessa1.Avalista] = sacado.Avalista;

                regBoleto[CNAB400SantanderRemessa1.Complemento] = cContaDigitos.Substring(8, 2);
                regBoleto[CNAB400SantanderRemessa1.Sequencia]   = SequencialRegistro++;

                // adiciona o boleto convertido em registro
                AddBoleto(regBoleto, boleto);

                AddOpcionais(boleto);
            }

            regArqTrailer[CNAB400SantanderTrailer.Quantidade] = Boletos.NossoNumeros.Count;
            //regArqTrailer[CNAB400SantanderTrailer.Quantidade] = Boletos.NossoNumeros.Count + 2; // adicionando o proprio header e trailer
            regArqTrailer[CNAB400SantanderTrailer.Valor]     = nValor;
            regArqTrailer[CNAB400SantanderTrailer.Sequencia] = SequencialRegistro;

            Add(regArqTrailer);

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