コード例 #1
0
ファイル: SupplierService.cs プロジェクト: pitersm/Fornecedor
        public async Task ValidateSupplier(SupplierDTO dto, bool isUpdate = false)
        {
            bool cnpjChanged = true, rgChanged = true;

            if (dto.Type == "PF")
            {
                ValidationService.ValidateCpf(dto.CpfCnpj);
            }
            else
            {
                ValidationService.ValidateCnpj(dto.CpfCnpj);
            }

            if (isUpdate)
            {
                var previousState = await Get(dto.Id.Value);

                if (previousState.CpfCnpj == dto.CpfCnpj)
                {
                    cnpjChanged = false;
                }

                if (previousState.Rg == dto.Rg)
                {
                    rgChanged = false;
                }
            }

            if (cnpjChanged)
            {
                var cpfCnpjExists = await CpfCnpjExists(dto.CpfCnpj);

                if (cpfCnpjExists)
                {
                    throw new Exception("This Cpf/Cnpj already exists.");
                }
            }

            if (dto.Type == "PF")
            {
                if (string.IsNullOrEmpty(dto.Rg) || dto.BirthDate == null)
                {
                    throw new Exception("Rg and Birth Date are Mandatory for PF Suppliers");
                }

                var company = await _companyService.Get(dto.CompanyId);

                if (company == null)
                {
                    throw new Exception("Invalid company for supplier.");
                }

                if (company.Uf == "PR" && ValidationService.CalculateAge(dto.BirthDate.Value) < 18)
                {
                    throw new Exception("Supplier must be of age.");
                }

                if (rgChanged)
                {
                    var rgExists = await RgExists(dto.Rg);

                    if (rgExists)
                    {
                        throw new Exception("This RG already exists.");
                    }
                }
            }
        }