public async Task ExportContractorsToErp(TaskTicket taskTicket, Target target) { var countries = await _dbContext.Countries.ToListAsync().ConfigureAwait(false); await FvpWebAppUtils.ChangeTicketStatus(_dbContext, taskTicket.TaskTicketId, TicketStatus.Pending); var allSourcesFromTarget = await _dbContext.Sources.Where(s => s.TargetId == target.TargetId).Select(i => i.SourceId).ToListAsync(); var notMatchedContractors = await _dbContext.Contractors.Where( c => c.ContractorErpId == null && c.ContractorErpPosition == null && //c.GusContractorEntriesCount == 1 && (c.ContractorStatus == ContractorStatus.Valid || c.ContractorStatus == ContractorStatus.Accepted) && c.SourceId == taskTicket.SourceId).ToListAsync().ConfigureAwait(false); var c21Contractors = notMatchedContractors.GroupBy(c => new { ContractorId = c.ContractorId, VatId = string.IsNullOrEmpty(c.VatId) || c.VatId.ToUpper() == "BRAK" ? "" : c.VatId, Regon = string.IsNullOrEmpty(c.Regon) ? "" : c.Regon, Name = string.IsNullOrEmpty(c.Name) ? "" : c.Name, Street = string.IsNullOrEmpty(c.Street) ? "" : c.Street, EstateNumber = string.IsNullOrEmpty(c.EstateNumber) ? "" : c.EstateNumber, QuartersNumber = string.IsNullOrEmpty(c.QuartersNumber) ? "" : c.QuartersNumber, City = string.IsNullOrEmpty(c.City) ? "" : c.City, CountryCode = string.IsNullOrEmpty(c.CountryCode) ? "" : c.CountryCode, PostalCode = string.IsNullOrEmpty(c.PostalCode) ? "" : c.PostalCode, Province = string.IsNullOrEmpty(c.Province) ? "" : c.Province, Phone = string.IsNullOrEmpty(c.Phone) ? "" : c.Phone, Email = string.IsNullOrEmpty(c.Email) ? "" : c.Email, }).Select(c => new C21Contractor { nip = c.Key.VatId, Regon = c.Key.Regon, nazwa = FvpWebAppUtils.TruncateToLength(c.Key.Name, 100), ulica = FvpWebAppUtils.TruncateToLength(c.Key.Street.Replace("ul. ", "").Replace("UL. ", ""), 35), numerDomu = c.Key.EstateNumber, numerMieszk = c.Key.QuartersNumber, Miejscowosc = FvpWebAppUtils.TruncateToLength(c.Key.City, 40), rejon = c.Key.Province.ToLower(), Kraj = c.Key.CountryCode, kod = FvpWebAppUtils.TruncateToLength(c.Key.PostalCode, 10), Telefon1 = c.Key.Phone, email = c.Key.Email, aktywny = true, skrot = $"FVP-{taskTicket.SourceId}-{c.Key.ContractorId}", statusUE = FvpWebAppUtils.CheckUeCountry(countries, c.Key.CountryCode), }).ToList(); C21ContractorService c21ContractorService = new C21ContractorService(GetDbSettings(target)); await c21ContractorService.AddContractorsAsync(c21Contractors); var output = await c21ContractorService.ProceedContractorsAsync(); string outputData = ""; foreach (var item in output) { outputData += "; " + item; } Console.WriteLine(outputData); SystemDataService systemDataService = new SystemDataService(_logger, _dbContext); await systemDataService.MatchContractors(taskTicket, target); }
public async Task <C21DocumentAggregate> PrepareDocumentAggregate( List <AccountingRecord> accountingRecords, TargetDocumentSettings targetDocumentSettings, List <Contractor> contractors, List <DocumentVat> allDocumentVats, List <VatRegister> vatRegisters, C21DocumentService c21DocumentService, Document document, Source source) { C21DocumentAggregate c21DocumentAggregate = new C21DocumentAggregate(); var documentVats = allDocumentVats.Where(v => v.DocumentId == document.DocumentId).ToList(); if (accountingRecords == null) { c21DocumentAggregate.IsPrepared = false; c21DocumentAggregate.Messages.Add(new KeyValuePair <string, string>("Zapisy księgowe", "Brak konfiguracji zapisów księgowych")); return(c21DocumentAggregate); } if (targetDocumentSettings == null) { c21DocumentAggregate.IsPrepared = false; c21DocumentAggregate.Messages.Add(new KeyValuePair <string, string>("Konfiguracja dokumentu", $"Nie skonfigurowano typu dokumentu dla tego źródła danych: {source.Description}")); return(c21DocumentAggregate); } var c21documentId = 1000;//await c21DocumentService.GetNextDocumentId(1000); var year = await c21DocumentService.GetYearId(document.SaleDate); if (year == null) { Console.WriteLine($"Brak roku w FK dla dokumentu z tego okresu."); } var docTypDef = await c21DocumentService.GetDocumentDefinition(targetDocumentSettings.DocumentShortcut, year.rokId); if (docTypDef == null) { Console.WriteLine($"Brak definicji dokumentu w FK dla dokumentu z tego okresu. ({document.DocumentNumber})"); } var vatRegisterDef = await c21DocumentService.GetVatRegistersDefs(docTypDef.rejestr); var contractor = contractors.FirstOrDefault(c => c.ContractorId == document.ContractorId); if (year != null && contractor != null && vatRegisterDef != null) { var street = !string.IsNullOrEmpty(contractor.Street) ? contractor.Street.Replace("UL. ", "").Replace("ul. ", "") : ""; c21DocumentAggregate.Document = new C21Document { id = c21documentId, rokId = year.rokId, skrot = targetDocumentSettings.DocumentShortcut, kontrahent = contractor.ContractorErpPosition, nazwa = document.DocumentNumber, tresc = document.DocumentNumber, datawpr = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day), datadok = document.DocumentDate, dataOper = document.SaleDate, kwota = Convert.ToDouble(document.Gross), sygnatura = string.Empty, kontoplatnosci = string.Empty, atrJpkV7 = document.JpkV7, DaneKh = 0, //contractor.ContractorErpPosition == null ? 1 : 0, kh_nazwa = string.IsNullOrEmpty(contractor.Name) ? "" : contractor.Name.Length <= 150 ? contractor.Name : contractor.Name.Substring(0, 150), kh_ulica = string.IsNullOrEmpty(street) ? "" : street.Length <= 50 ? street : street.Substring(0, 50), kh_dom = contractor.EstateNumber, kh_lokal = contractor.QuartersNumber, kh_nip = contractor.VatId, kh_kodPocztowy = FvpWebAppUtils.TruncateToLength(contractor.PostalCode, 10), kh_miejscowosc = string.IsNullOrEmpty(contractor.City) ? "" : contractor.City.Length <= 50 ? contractor.City : contractor.City.Substring(0, 50), kh_kraj = contractor.CountryCode }; var nextAccountingRecordId = 1000; foreach (var accountingRecord in accountingRecords) { if (GetRecordAmmount(document, accountingRecord) != 0) { c21DocumentAggregate.AccountingRecords.Add(new C21AccountingRecord { id = nextAccountingRecordId, dokId = c21documentId, pozycja = 0, rozbicie = (short)(accountingRecord.RecordOrder - 1), strona = !string.IsNullOrEmpty(accountingRecord.Debit) ? (short)0 : (short)1, kwota = GetRecordAmmount(document, accountingRecord), opis = document.DocumentNumber, synt = GetAccountPart(accountingRecord.Account, 0), poz1 = GetAccountPart(accountingRecord.Account, 1), poz2 = GetAccountPart(accountingRecord.Account, 2), poz3 = GetAccountPart(accountingRecord.Account, 3), poz4 = GetAccountPart(accountingRecord.Account, 4), poz5 = GetAccountPart(accountingRecord.Account, 5), }); } nextAccountingRecordId++; } var nextVatRegisterId = 1000; foreach (var documentVat in documentVats) { var vatRegister = GetVatRegisterIdForVat(GetVatValue(documentVat), vatRegisters); c21DocumentAggregate.VatRegisters.Add(new C21VatRegister { id = nextVatRegisterId, dokId = c21documentId, rejId = vatRegister > 0 ? vatRegister : vatRegisterDef.id, okres = document.SaleDate, Oczek = 0, abc = vatRegisterDef.defAbc, nienaliczany = 0, stawka = GetVatValue(documentVat), netto = Convert.ToDouble(documentVat.NetAmount), brutto = Convert.ToDouble(documentVat.GrossAmount), vat = Convert.ToDouble(documentVat.VatAmount), atrJpkV7 = documentVat.VatTags }); nextVatRegisterId++; } } c21DocumentAggregate.IsPrepared = true; return(c21DocumentAggregate); }