public async void C21GetNextDocumentIdTest() { DbConnectionSettings dbConnectionSettings = new DbConnectionSettings("192.168.21.20", "sa", "#sa2015!", "fkf_test_db"); C21DocumentService documentService = new C21DocumentService(dbConnectionSettings, null); var output = await documentService.GetNextDocumentId(10); Console.WriteLine($"C21GetNextDocumentIdTest - Next ID: {output}"); Assert.True(output > 0); }
public async Task <IActionResult> GetDocumentDef(int targetId, string documentShortcut) { var target = await _context.Targets.FirstOrDefaultAsync(t => t.TargetId == targetId); C21DocumentService documentService = new C21DocumentService(GetDbSettings(target), null); var year = await documentService.GetYearId(DateTime.Now); var documentDef = await documentService.GetDocumentDefinition(documentShortcut, year.rokId); return(new JsonResult(new { docExist = documentDef != null ? true : false, documentDef })); }
public async void C21GetYearsTest() { DbConnectionSettings dbConnectionSettings = new DbConnectionSettings("192.168.21.20", "sa", "#sa2015!", "fkf_test_db"); C21DocumentService documentService = new C21DocumentService(dbConnectionSettings, null); var output = await documentService.GetYearId(new DateTime(2020, 5, 20)); Console.WriteLine($"C21GetYearsTest - Year id: {output.rokId}"); Assert.True(output != null); var outputNull = await documentService.GetYearId(new DateTime(2099, 5, 20)); Assert.True(outputNull == null); }
public async void C21GetVatRegisterDefsTest() { DbConnectionSettings dbConnectionSettings = new DbConnectionSettings("192.168.21.20", "sa", "#sa2015!", "fkf_test_db"); C21DocumentService documentService = new C21DocumentService(dbConnectionSettings, null); var output = await documentService.GetVatRegistersDefs(1); Console.WriteLine($"C21GetVatRegisterDefsTest - Vat register name: {output.rNazwa}"); Assert.True(output != null); var outputNull = await documentService.GetVatRegistersDefs(9999); Assert.True(outputNull == null); }
public async Task <IActionResult> GetVatRegisters(int id) { var target = await _context.Targets.FirstOrDefaultAsync(t => t.TargetId == id); C21DocumentService documentService = new C21DocumentService(GetDbSettings(target), null); var vatDefs = await documentService.GetAllVatRegistersDefs(); var defList = from v in vatDefs select new { VatRegisterId = v.id, Name = v.rNazwa }; return(new JsonResult(defList)); }
public async Task <IActionResult> GetFkDuplicatedDocuments(int sourceId, int year, int month) { var target = await _context.Targets.FirstOrDefaultAsync(t => t.Sources.Select(s => s.SourceId).Contains(sourceId)); var docSettings = await _context.TargetDocumentsSettings.FirstOrDefaultAsync(t => t.SourceId == sourceId); DbConnectionSettings dbConnectionSettings = new DbConnectionSettings(target.DatabaseAddress, target.DatabaseUsername, target.DatabasePassword, target.DatabaseName); C21DocumentService c21DocumentService = new C21DocumentService(dbConnectionSettings, null); var fkfDocuments = (await c21DocumentService.GetFKDocuments(year, month, docSettings.DocumentShortcut)); _context.Database.SetCommandTimeout(0); var systemDocuments = await _context.Documents.Where(d => d.SourceId == sourceId && d.DocumentDate.Month == month).ToListAsync(); var duplicates = fkfDocuments.GroupBy(d => new { d.tresc }).Select(d => new { d.Key.tresc, cnt = d.Count() }).ToList(); duplicates = duplicates.Where(d => d.cnt > 1).ToList(); var duplicatedDocs = systemDocuments.Where(d => duplicates.Select(d => d.tresc).Contains(d.DocumentNumber)).ToList(); var sourcesIds = await _context.Sources.Select(s => s.SourceId).ToListAsync(); if (sourceId > 0) { sourcesIds = new List <int> { sourceId } } ; var contractors = await _context.Contractors.ToListAsync(); var sources = await _context.Sources.ToListAsync(); var documents = ( from d in duplicatedDocs from c in contractors from s in sources where d.ContractorId == c.ContractorId && d.SourceId == s.SourceId && sourcesIds.Contains((int)d.SourceId) && d.DocumentDate.Month == month && d.DocumentDate.Year == year orderby d.DocumentDate select new DocumentView(d, s, c)).ToList(); return(Ok(new { data = documents })); }
public async Task InsertDocumentsToTarget(TaskTicket taskTicket, Target target) { await FvpWebAppUtils.ChangeTicketStatus(_dbContext, taskTicket.TaskTicketId, TicketStatus.Pending).ConfigureAwait(false); C21DocumentService c21DocumentService = new C21DocumentService(GetDbSettings(target), _procOutput); //List<C21DocumentAggregate> c21DocumentAggregates = new List<C21DocumentAggregate>(); var documentsToSend = await _dbContext.Documents .Where(d => (d.DocumentStatus == DocumentStatus.Valid || d.DocumentStatus == DocumentStatus.ManyContractors || d.DocumentStatus == DocumentStatus.Accepted) && d.DocumentDate.Month == taskTicket.DateFrom.Month && d.DocumentDate.Year == taskTicket.DateFrom.Year && d.SourceId == taskTicket.SourceId).ToListAsync(); if (documentsToSend != null) { documentsToSend = documentsToSend.OrderBy(d => d.DocumentDate).ToList(); var allDocumentVats = await _dbContext.DocumentVats.Where(v => documentsToSend.Select(i => (int?)i.DocumentId).Contains(v.DocumentId)).ToListAsync(); var contractors = await _dbContext.Contractors.Where(c => c.SourceId == taskTicket.SourceId).ToListAsync(); var targetDocumentSettings = await _dbContext.TargetDocumentsSettings.FirstOrDefaultAsync(t => t.SourceId == taskTicket.SourceId); var accountingRecords = await _dbContext.AccountingRecords.Where(a => a.SourceId == taskTicket.SourceId).ToListAsync(); var source = await _dbContext.Sources.FirstOrDefaultAsync(s => s.SourceId == taskTicket.SourceId); var vatRegisters = await _dbContext.VatRegisters.Where(v => v.TargetDocumentSettingsId == targetDocumentSettings.TargetDocumentSettingsId).ToListAsync(); Console.WriteLine($"Source: {source.Description} Start prep.: {DateTime.Now}"); int docCounter = 0; int insertCounter = 0; foreach (var document in documentsToSend) { var documentAggregate = await PrepareDocumentAggregate(accountingRecords, targetDocumentSettings, contractors, allDocumentVats, vatRegisters, c21DocumentService, document, source); if (documentAggregate.IsPrepared) { //c21DocumentAggregates.Add(documentAggregate); try { await c21DocumentService.AddDocumentAggregate(documentAggregate).ConfigureAwait(false); document.DocumentStatus = DocumentStatus.SentToC2FK; _dbContext.Update(document); await _dbContext.SaveChangesAsync(); insertCounter++; } catch (Exception ex) { _logger.LogError(ex.Message); } } else { foreach (var msg in documentAggregate.Messages) { _logger.LogError($"{msg.Key}: {msg.Value}"); } } docCounter++; if (insertCounter >= 300 || docCounter == documentsToSend.Count) { c21DocumentService.ProceedDocumentsAsync(docCounter, taskTicket.TaskTicketId); Console.WriteLine($"Added documents: {insertCounter} End: {DateTime.Now}"); insertCounter = 0; } } Console.WriteLine($"Source: {source.Description} End prep.: {DateTime.Now} Documents: {docCounter}"); } }
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); }