public override CustomPacket Commands(CustomPacket packet, TcpClient client) { CustomPacket responsePacket = null; var watch = Stopwatch.StartNew(); switch(packet.Command) { case "scan": _log.Information($"Incoming scan from: {client.Client.RemoteEndPoint}"); responsePacket = Scan(packet); break; case "getBIN": responsePacket = getBin(packet); break; case "showMissing": responsePacket = showMissing(packet); break; case "disposeCache": using (var _db = new ZEMDBContext()) { var stray = _db.ScanCache.Find((Guid)packet.Payload); if (stray != null) _db.Remove(stray); _db.SaveChanges(); responsePacket = new CustomPacket(FlagType.basic, HeaderTypes.basic, "disposeCache", null, null); } break; } watch.Stop(); _log.Information($"Command {packet.Command} execution time [{watch.ElapsedMilliseconds}]"); return responsePacket; }
protected CustomPacket Scan(CustomPacket packet) { using (var _db = new ZEMDBContext()) { ScannedCode sc = (ScannedCode)packet.Payload; var scan = _db.ScanCache.Find(sc.SessionGUID); if (scan == null) { _db.ScanCache.Add(new ScanCache { ScanCacheId = sc.SessionGUID }); _db.SaveChanges(); scan = _db.ScanCache.Find(sc.SessionGUID); } VTInsertFunctions VTFuncs = new VTInsertFunctions(_db, _log, scan); ScannedResponse sr = new ScannedResponse(); CustomPacket response = new CustomPacket(FlagType.basic, HeaderTypes.basic, "scan", new List<string>(), sr); // 23102720A sc.Declared = false; sc.complete = false; sc.isFullSet = false; sc.addedBefore = false; sc.dataDostawyOld = DateTime.MinValue; sc.dataDoskanowania = DateTime.Now; sc.dataUtworzenia = DateTime.Now; sr.missingEntries = new List<MissingBackwards>(); var techEntry = _db.Technical.IgnoreQueryFilters().FirstOrDefault(c => c.PrzewodCiety == sc.kodCiety); if (techEntry == null) { if (_db.MissingFromTech.Find(sc.kodCiety) == null) { _db.MissingFromTech.Add(new MissingFromTech { DataDodania = sc.dataDoskanowania, Kod = sc.kodCiety, User = sc.User }); _db.SaveChanges(); } response.Header = HeaderTypes.error; response.Flag = FlagType.notInTech; return response; } else if (techEntry.Deleted == true) { response.Header = HeaderTypes.error; response.Flag = FlagType.isDeleted; return response; } else if (techEntry.KanBan == true) { if (_db.ScannedKanbans.FirstOrDefault(c => c.Kod == sc.kodCiety && c.Wiazka == techEntry.Wiazka) == null) { _db.ScannedKanbans.Add(new ScannedKanban { DataDodania = sc.dataDoskanowania, Kod = sc.kodCiety, Wiazka = techEntry.Wiazka, User = sc.User }); _db.SaveChanges(); } response.Header = HeaderTypes.error; response.Flag = FlagType.isKanban; return response; } sc.Wiazka = techEntry.Wiazka; sc.Rodzina = techEntry.Rodzina; sc.BIN = techEntry.BIN; sr.PrzewodCiety = techEntry.PrzewodCiety; sr.BIN = techEntry.BIN; sr.KodWiazki = techEntry.KodWiazki; sr.LiteraRodziny = techEntry.LiterRodziny; sr.IlePrzewodow = techEntry.IlePrzewodow; var dostawaEntry = _db.Dostawa.FirstOrDefault(c => c.Data.Date == sc.dataDostawy.Date && c.Kod == "PLC" + sc.kodCietyFull); if (dostawaEntry != null) { sc.dataDostawy = dostawaEntry.Data; sc.dataDostawyOld = dostawaEntry.Data; sc.sztukiDeklarowane = dostawaEntry.Ilosc; sc.Declared = true; if (sc.sztukiSkanowane == sc.sztukiDeklarowane) { var sets = _db.VTMagazyn.Where(c => c.Wiazka == sc.Wiazka && c.DataDostawy.Date == sc.dataDostawyOld.Date).ToList(); var deliveries = _db.Dostawa.Include(c => c.Technical).Where(c => c.Technical.Wiazka == sc.Wiazka && c.Data.Date == sc.dataDostawyOld.Date).ToList(); int declared = VTFuncs.GetPossibleDeclaredValue(sc, sets, deliveries, sc.NumerKompletu); if (sc.sztukiSkanowane != declared && !sc.isForcedOverDeclared) { response.Header = HeaderTypes.error; response.Flag = FlagType.quantityOverDeclated; response.Args.Add(sc.sztukiDeklarowane.ToString()); response.Args.Add(VTFuncs.GetScannedForDay(sc, sets).ToString()); response.Args.Add(declared.ToString()); response.Args.Add($"{declared - sc.sztukiSkanowane}"); return response; } else { if (!VTFuncs.CheckBackOrAdd(response, techEntry, sc, dostawaEntry)) return response; } } else if (sc.sztukiSkanowane != sc.sztukiDeklarowane) { if (!sc.isForcedQuantity) { response.Header = HeaderTypes.error; response.Flag = FlagType.quantityIncorrect; response.Args.Add(sc.sztukiDeklarowane.ToString()); var sets = _db.VTMagazyn.Where(c => c.Wiazka == sc.Wiazka && c.DataDostawy.Date == sc.dataDostawyOld.Date).ToList(); var deliveries = _db.Dostawa.Include(c => c.Technical).Where(c => c.Technical.Wiazka == sc.Wiazka && c.Data.Date == sc.dataDostawyOld.Date).ToList(); int declared = VTFuncs.GetPossibleDeclaredValue(sc, sets, deliveries, sc.NumerKompletu); response.Args.Add(VTFuncs.GetScannedForDay(sc, sets).ToString()); response.Args.Add(declared.ToString()); response.Args.Add($"{declared - sc.sztukiSkanowane}"); return response; } else { if (!VTFuncs.CheckBackOrAddQuantityIncorrect(response, techEntry, sc, dostawaEntry)) return response; } } } else { sc.dataDostawy = sc.dataDostawy.Date; sc.dataDostawyOld = sc.dataDostawy.Date; if (!sc.isForcedUndeclared) { response.Header = HeaderTypes.error; response.Flag = FlagType.notInDeclared; return response; } else { if (!sc.isForcedQuantity) { response.Header = HeaderTypes.error; response.Flag = FlagType.quantityIncorrect; response.Args.Add("0"); var sets = _db.VTMagazyn.Where(c => c.Wiazka == sc.Wiazka && c.DataDostawy.Date == sc.dataDostawyOld.Date).ToList(); var deliveries = _db.Dostawa.Include(c => c.Technical).Where(c => c.Technical.Wiazka == sc.Wiazka && c.Data.Date == sc.dataDostawyOld.Date).ToList(); int declared = VTFuncs.GetPossibleDeclaredValue(sc, sets, deliveries, sc.NumerKompletu); response.Args.Add(VTFuncs.GetScannedForDay(sc, sets).ToString()); response.Args.Add(declared.ToString()); response.Args.Add($"{declared - sc.sztukiSkanowane}"); return response; } else { if (!VTFuncs.CheckBackOrAddQuantityIncorrect(response, techEntry, sc, dostawaEntry)) return response; } } } _db.SaveChanges(); bool isComplete = VTFuncs.checkComplete(sc, out int numToComplete, out int numScanned, out int numScannedToComplete); sr.DataDostawy = sc.dataDostawy; sr.DataDostawyOld = sc.dataDostawyOld; sr.numToComplete = numToComplete; sr.numScanned = numScanned; sr.numScannedToComplete = numScannedToComplete; sr.isComplete = isComplete; sr.sztukiDeklatowane = sc.sztukiDeklarowane; sr.numerKompletu = sc.NumerKompletu; sr.Wiazka = sc.Wiazka; sr.Rodzina = sc.Rodzina; sr.sztukiSkanowane = sc.sztukiSkanowane; if (numScanned == 1) { sr.Print = true; sr.isSpecialColor = false; } if (VTFuncs.shouldPrintSpecial(sc)) { sr.Print = true; sr.isSpecialColor = true; } _log.Information("Scan details: {@sc}", sc); _log.Information("Response details: {@sr}", sr); return response; } }
/// <summary> /// Browse old scans in hope to find somewhere to add current scan to /// </summary> /// <param name="technical"></param> /// <param name="scanned"></param> /// <param name="dostawa"></param> /// <returns></returns> public bool SearchBack(Technical technical, ScannedCode scanned, Dostawa dostawa) { if (!scanned.isLookingBack || _scanCache.LookedBack == true) { return(false); } // Find all scans between dates var pastScans = _db.VTMagazyn.Include(c => c.Dostawy).Where(c => c.Wiazka == scanned.Wiazka && c.DataDostawy.Date < scanned.dataDostawy.Date && c.autocompleteEnabled == true && c.DataDostawy.Date >= scanned.dataDostawy.Date.AddDays(-7)) .OrderBy(c => c.DataDostawy).ToList().GroupBy(c => c.DataDostawy).Select(g => g.ToList()).ToList(); var pastDeliveries = _db.Dostawa.AsNoTracking().Include(c => c.Technical) .Where(c => c.Technical.Wiazka == scanned.Wiazka && c.Data.Date <scanned.dataDostawy.Date && c.Data.Date> scanned.dataDostawy.Date.AddDays(-7)).ToList(); foreach (var scanPerDate in pastScans) { // Look if there are any duplicate scans (multiple sets) var duplicateScans = scanPerDate.Where(c => c.KodCiety == scanned.kodCiety).OrderBy(c => c.NumerKompletu).ToList(); int mostFrequentCount = 0; if (duplicateScans.Count > 0) { foreach (var scan in duplicateScans) { mostFrequentCount = GetPossibleDeclaredValue(new ScannedCode { Wiazka = scan.Wiazka, dataDostawyOld = scan.DataDostawy }, scanPerDate, pastDeliveries, scan.NumerKompletu); // If this scan have already been added set flags and notify user if (scan.DataDopisu != null && ((DateTime)scan.DataDopisu).Date == scanned.dataDostawy.Date && !scanned.isForcedInsert) { scanned.addedBefore = true; return(false); } // Check if we can add current scan to previous one if (scan.SztukiZeskanowane + scanned.sztukiSkanowane == mostFrequentCount && scan.SztukiZeskanowane != mostFrequentCount) { scan.SztukiZeskanowane += scanned.sztukiSkanowane; scan.DataDopisu = scanned.dataDostawy; scan.DopisanaIlosc = scanned.sztukiSkanowane; scan.DostawaDopis = scanned.DokDostawy; scanned.dataDostawyOld = scan.DataDostawy; if (dostawa != null) { UpdateVT(dostawa, scan); } _db.Update(scan); _db.SaveChanges(); return(true); } } // Check if any set is missing current scan int[] setIDs = scanPerDate.Where(c => c.Komplet == false).Select(c => c.NumerKompletu).Distinct().ToArray(); foreach (int setID in setIDs) { mostFrequentCount = GetPossibleDeclaredValue(new ScannedCode { Wiazka = scanned.Wiazka, dataDostawyOld = scanPerDate[0].DataDostawy }, scanPerDate, pastDeliveries, setID); // Only add on the previous date if "declared" value is the same as scanned if (scanned.sztukiSkanowane == mostFrequentCount && duplicateScans.Where(c => c.NumerKompletu == setID).Select(c => c.KodCiety).FirstOrDefault() != scanned.kodCiety) { scanned.DataDopisu = scanned.dataDostawy; scanned.DopisanaIlosc = scanned.sztukiSkanowane; scanned.dataDostawyOld = scanPerDate[0].DataDostawy; scanned.DostawaDopis = scanned.DokDostawy; AddToVT(technical, scanned, dostawa, true); return(true); } } } else { mostFrequentCount = GetPossibleDeclaredValue(new ScannedCode { Wiazka = scanned.Wiazka, dataDostawyOld = scanPerDate[0].DataDostawy }, scanPerDate, pastDeliveries, 0); // Only add on the previous date if "declared" value is the same as scanned if (scanned.sztukiSkanowane == mostFrequentCount) { scanned.DataDopisu = scanned.dataDostawy; scanned.DopisanaIlosc = scanned.sztukiSkanowane; scanned.dataDostawyOld = scanPerDate[0].DataDostawy; scanned.DostawaDopis = scanned.DokDostawy; AddToVT(technical, scanned, dostawa); return(true); } } } _scanCache.LookedBack = true; _db.Update(_scanCache); _db.SaveChanges(); return(false); }