Exemplo n.º 1
0
        public async Task TransferContractors(List <Document> documents)
        {
            ContractorService contractorService = new ContractorService(_dbContext);
            var documentsContractors            = AggregateContractorsFromDocuments(documents);
            List <Contractor> newContractors    = new List <Contractor>();

            foreach (var documentContractor in documentsContractors)
            {
                var contractorServiceResponse = contractorService.ContractorExist((int)documentContractor.SourceId, documentContractor.ContractorSourceId);
                var isAddedToNewContractors   = newContractors.FirstOrDefault(c => c.SourceId == documentContractor.SourceId && c.ContractorSourceId == documentContractor.ContractorSourceId);
                if (!contractorServiceResponse.Exist && isAddedToNewContractors == null)
                {
                    newContractors.Add(documentContractor);
                }
            }

            try
            {
                await _dbContext.AddRangeAsync(newContractors).ConfigureAwait(true);

                await _dbContext.SaveChangesAsync().ConfigureAwait(true);

                _logger.LogInformation($"Dodano : {newContractors.Count} nowych kontrahentów.");
            }
            catch (Exception ex)
            {
                _dbContext.DetachAllEntities();
                _logger.LogError(ex.Message);
            }
        }
Exemplo n.º 2
0
        public async Task CheckContractors(int taskTicketId)
        {
            await FvpWebAppUtils.ChangeTicketStatus(_dbContext, taskTicketId, TicketStatus.Pending).ConfigureAwait(false);

            try
            {
                var token = await _apiService.ApiLogin().ConfigureAwait(false);

                var ueCountries  = (await _dbContext.Countries.ToListAsync().ConfigureAwait(false)).Where(u => u.UE).Select(c => c.Symbol).ToList();
                var allCountries = (await _apiService.GetCountriesAsync(token)).Select(c => c.Symbol);

                ContractorService contractorService = new ContractorService(_dbContext);
                var documentsContractors            = await _dbContext.Contractors.Where(s => s.ContractorStatus == ContractorStatus.NotChecked).ToListAsync().ConfigureAwait(false);

                _logger.LogInformation($"Kontrahenci do sprawdzenia: {documentsContractors.Count}");
                foreach (var documentContractor in documentsContractors)
                {
                    if (documentContractor.CountryCode == "PL" || documentContractor.Firm == Firm.FirmaPolska)
                    {
                        var response = await CheckContractorByGusApi(FvpWebAppUtils.GetDigitsFromString(documentContractor.VatId));
                        await ClassificateContractor(documentContractor, response);
                    }
                    else if (ueCountries.Contains(documentContractor.CountryCode))
                    {
                        var response = await CheckContractorByViesApi(documentContractor);
                        await ClassificateContractor(documentContractor, response);
                    }
                    else if (allCountries.Contains(documentContractor.CountryCode))
                    {
                        var response = new ApiResponseContractor
                        {
                            ApiStatus   = ApiStatus.NotSupportedByApi,
                            Contractors = new List <Contractor>()
                        };
                        await ClassificateContractor(documentContractor, response);
                    }
                    else
                    {
                        var response = new ApiResponseContractor
                        {
                            ApiStatus   = ApiStatus.NotValid,
                            Contractors = new List <Contractor>()
                        };
                        await ClassificateContractor(documentContractor, response);
                    }
                }
                await UpdateStatusAndContractorOnDocuments();

                await FvpWebAppUtils.ChangeTicketStatus(_dbContext, taskTicketId, TicketStatus.Done).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                await FvpWebAppUtils.ChangeTicketStatus(_dbContext, taskTicketId, TicketStatus.Failed).ConfigureAwait(false);

                _logger.LogError(ex.Message);
            }
        }