示例#1
0
        public bool InsertOrUpdate(DebitNote DebitNote)
        {
            var notaDebito = DebitNote;
            var contratada = _contractorApp.Search(c => c.Id == notaDebito.ContractorId).Include(c => c.DebitNotes).FirstOrDefault();

            // Verifico se a nota de débito correponde a empresa selecionada.
            if (contratada.DebitNotes.Any(c => c.NumberDebitNote == notaDebito.NumberDebitNote))
            {
                return(_epr.InsertOrUpdate(DebitNote));
            }
            var numeroNd = "";

            // Pesquiso a última nota de débito do mês recorrente da empresa selecionada.
            // Cano não exista retorna 000000000, para iniciar a contagem.
            var oldNd =
                contratada.DebitNotes.LastOrDefault(
                    c => c.DateRegistration != null && c.DateRegistration.Value.Month == DateTime.Now.Month)?
                .NumberDebitNote ?? "000000000";

            // Pego o cnpj da empresa selecionada
            var cnpj = new Cnpj(contratada.Cnpj).Codigo;

            // Faço este loop, pois pode ocorrer situação de o número ser repetido.
            while (true)
            {
                // Pego o último contador e incremento mais 1 (um).
                var nd = (Convert.ToInt32(oldNd.Remove(0, 6)) + 1).ToString();

                // Crio a ND seguindo a regra: CNPJ (apenas dígitos) + ANO + MÊS + Contador.
                numeroNd = cnpj.Substring(12, 2) + DateTime.Now.Year.ToString().Substring(2, 2) + DateTime.Now.Month.ToString().PadLeft(2, '0') + nd.PadLeft(3, '0');

                notaDebito.NumberDebitNote = numeroNd;

                if (!_epr.Search(c => c.NumberDebitNote == notaDebito.NumberDebitNote).Any())
                {
                    break;
                }

                oldNd = numeroNd;
            }

            return(_epr.InsertOrUpdate(DebitNote));
        }
示例#2
0
        public override bool ValidadorDocumento()
        {
            int[] multiplicador1 = new int[12] {
                5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2
            };
            int[] multiplicador2 = new int[13] {
                6, 5, 4, 3, 2, 9, 8, 7, 6, 5, 4, 3, 2
            };
            int    soma;
            int    resto;
            string digito;
            string tempCnpj;

            Cnpj = Cnpj.Trim();
            Cnpj = Cnpj.Replace(".", "").Replace("-", "").Replace("/", "");

            if (Cnpj.Length != 14)
            {
                return(false);
            }

            tempCnpj = Cnpj.Substring(0, 12);

            soma = 0;
            for (int i = 0; i < 12; i++)
            {
                soma += int.Parse(tempCnpj[i].ToString()) * multiplicador1[i];
            }

            resto = (soma % 11);
            if (resto < 2)
            {
                resto = 0;
            }
            else
            {
                resto = 11 - resto;
            }

            digito = resto.ToString();

            tempCnpj = tempCnpj + digito;
            soma     = 0;
            for (int i = 0; i < 13; i++)
            {
                soma += int.Parse(tempCnpj[i].ToString()) * multiplicador2[i];
            }

            resto = (soma % 11);
            if (resto < 2)
            {
                resto = 0;
            }
            else
            {
                resto = 11 - resto;
            }

            digito = digito + resto.ToString();

            return(Cnpj.EndsWith(digito));
        }