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); } }
private ViesSimpleRequest ContractorViesRequest(Contractor contractor) { var suffix = FvpWebAppUtils.GetDigitsFromString(contractor.VatId); var prefix = contractor.CountryCode; return(new ViesSimpleRequest { ContractorPrefix = prefix, ContractorSuffix = suffix }); }
public async Task MatchContractors(TaskTicket taskTicket, Target target) { await FvpWebAppUtils.ChangeTicketStatus(_dbContext, taskTicket.TaskTicketId, TicketStatus.Pending); var c21ConnectionSettings = new DbConnectionSettings( target.DatabaseAddress, target.DatabaseUsername, target.DatabasePassword, target.DatabaseName); C21ContractorService contractorService = new C21ContractorService(c21ConnectionSettings); var erpContractors = await contractorService.GetC21FvpContractorsAsync(false).ConfigureAwait(false); erpContractors.Where(c => (c.Country == "PL" || string.IsNullOrEmpty(c.Country)) && c.VatId.ToUpper() != "BRAK").ToList().ForEach(c => c.VatId = FvpWebAppUtils.GetDigitsFromString(c.VatId)); var allSourcesFromTarget = await _dbContext.Sources.Where(s => s.TargetId == target.TargetId).Select(i => i.SourceId).ToListAsync(); var contractors = await _dbContext.Contractors.Where( c => //c.ContractorErpId == null && c.SourceId == taskTicket.SourceId //&& //only contractors from specific source //(c.ContractorStatus == ContractorStatus.Valid || c.ContractorStatus == ContractorStatus.Accepted) //all ).ToListAsync().ConfigureAwait(false); contractors.ForEach(c => { c.ContractorErpId = null; c.ContractorErpPosition = null; }); // remove all previous agreements if (contractors != null && contractors.Count > 0) { Console.WriteLine("Matching contractors..."); foreach (var contractor in contractors) { string vatId = contractor.VatId; if (vatId.ToUpper() != "BRAK") { if (contractor.CountryCode == "PL") { vatId = FvpWebAppUtils.GetDigitsFromString(contractor.VatId); } var erpcontractor = erpContractors.FirstOrDefault(c => c.VatId == vatId && c.Active && c.Name == contractor.Name); if (erpcontractor == null) { erpcontractor = erpContractors.FirstOrDefault(c => c.VatId == vatId && c.Active); } if (erpcontractor == null) { erpcontractor = erpContractors.FirstOrDefault(c => c.VatId == vatId && !c.Active && c.Name == contractor.Name); } if (erpcontractor == null) { erpcontractor = erpContractors.FirstOrDefault(c => c.VatId == vatId && !c.Active); } if (erpcontractor != null) { contractor.ContractorErpId = erpcontractor.Id; contractor.ContractorErpPosition = erpcontractor.FkId; } } else { var shortcut = $"FVP-{taskTicket.SourceId}-{contractor.ContractorId}"; var erpcontractor = erpContractors.FirstOrDefault(c => c.Shortcut == shortcut); if (erpcontractor != null) { contractor.ContractorErpId = erpcontractor.Id; contractor.ContractorErpPosition = erpcontractor.FkId; } if (erpcontractor == null) { erpcontractor = erpContractors.FirstOrDefault(c => c.Active && c.Name.ToUpper() == contractor.Name.ToUpper()); } if (erpcontractor != null) { contractor.ContractorErpId = erpcontractor.Id; contractor.ContractorErpPosition = erpcontractor.FkId; } } } try { _dbContext.UpdateRange(contractors); await _dbContext.SaveChangesAsync(); Console.WriteLine("Contactors matched!"); } catch (Exception ex) { await FvpWebAppUtils.ChangeTicketStatus(_dbContext, taskTicket.TaskTicketId, TicketStatus.Failed); _logger.LogError(ex.Message); } } }
private async Task UpdateStatusAndContractorOnDocuments() { var contractors = await _dbContext.Contractors.ToListAsync().ConfigureAwait(false); if (contractors != null && contractors.Count > 0) { try { var documents = await _dbContext.Documents.Where(c => c.ContractorId == null || c.ContractorId == 0).ToListAsync().ConfigureAwait(false); if (documents != null && documents.Count > 0) { foreach (var document in documents) { //If digits present if (FvpWebAppUtils.GetDigitsFromString(document.DocContractorVatId).Length > 3) { //Find by name var matchedContractors = contractors.Where(c => c.Name == document.DocContractorName && FvpWebAppUtils.GetDigitsFromString(c.VatId) == FvpWebAppUtils.GetDigitsFromString(document.DocContractorVatId) && c.SourceId == document.SourceId).ToList(); //If not found by name - find by DocContractorId if (matchedContractors == null || matchedContractors.Count == 0) { matchedContractors = contractors.Where(c => c.ContractorSourceId == document.DocContractorId && c.SourceId == document.SourceId).ToList(); } if (matchedContractors != null && matchedContractors.Count > 0) { document.ContractorId = matchedContractors[0].ContractorId; //set contractor } //Update document status depending on the contractor status if (matchedContractors != null && matchedContractors.Count == 1 && (matchedContractors[0].ContractorStatus == ContractorStatus.Valid || matchedContractors[0].ContractorStatus == ContractorStatus.Accepted)) { document.DocumentStatus = DocumentStatus.Valid; } else if (matchedContractors != null && matchedContractors.Count == 1 && matchedContractors[0].ContractorStatus == ContractorStatus.Invalid) { document.DocumentStatus = DocumentStatus.Invalid; } else if (matchedContractors != null && matchedContractors.Count > 1) { document.DocumentStatus = DocumentStatus.ManyContractors; } } else { //If digits not present find by name only var matchedContractors = contractors.Where(c => c.Name.ToUpper() == document.DocContractorName.ToUpper() && c.SourceId == document.SourceId).ToList(); if (matchedContractors != null && matchedContractors.Count > 0) { document.ContractorId = matchedContractors[0].ContractorId; //set contractor } //Update document status depending on the contractor status if (matchedContractors != null && matchedContractors.Count == 1 && (matchedContractors[0].ContractorStatus == ContractorStatus.Valid || matchedContractors[0].ContractorStatus == ContractorStatus.Accepted)) { document.DocumentStatus = DocumentStatus.Valid; } else if (matchedContractors != null && matchedContractors.Count == 1 && matchedContractors[0].ContractorStatus == ContractorStatus.Invalid) { document.DocumentStatus = DocumentStatus.Invalid; } else if (matchedContractors != null && matchedContractors.Count > 1) { document.DocumentStatus = DocumentStatus.ManyContractors; } if (matchedContractors != null && matchedContractors.Count == 0) { matchedContractors = contractors.Where(c => c.ContractorSourceId == document.DocContractorId && c.SourceId == document.SourceId).ToList(); if (matchedContractors != null && matchedContractors.Count > 0) { document.ContractorId = matchedContractors[0].ContractorId; //set contractor } //Update document status depending on the contractor status if (matchedContractors != null && matchedContractors.Count == 1 && (matchedContractors[0].ContractorStatus == ContractorStatus.Valid || matchedContractors[0].ContractorStatus == ContractorStatus.Accepted)) { document.DocumentStatus = DocumentStatus.Valid; } else if (matchedContractors != null && matchedContractors.Count == 1 && matchedContractors[0].ContractorStatus == ContractorStatus.Invalid) { document.DocumentStatus = DocumentStatus.Invalid; } else if (matchedContractors != null && matchedContractors.Count > 1) { document.DocumentStatus = DocumentStatus.ManyContractors; } } } } _dbContext.UpdateRange(documents); await _dbContext.SaveChangesAsync(); } } catch (Exception ex) { _logger.LogError(ex.Message); Console.WriteLine("Błąd aktualiazcji statusu i kontrahentów na dokumentach"); } } }