private void CreateLoad(ref bool newLoad) { _loadCreated = true; timer.Stop(); autoSaveTimer.Stop(); using (var dp = SimpleIoc.Default.GetInstance <IUnitOfWorkFactory>().CreateUnitOfWork()) { Logging.Logger.Log("INFO", "CREATING LOAD FROM WEIGH IN: LOAD " + GinTicketLoadNumber); //find module ownerships var bridgeID = dp.SettingsRepository.GetSettingWithDefault(BridgeSettingKeys.BRIDGE_ID, ""); var ownerships = dp.ModuleOwnershipRepository.FindMatching(m => m.GinTagLoadNumber == GinTicketLoadNumber).ToList().OrderBy(t => t.LastCreatedOrUpdated); var lastOwnership = ownerships.LastOrDefault(); var lastLoadScan = dp.LoadScanRepository.FindMatching(l => l.GinTagLoadNumber == GinTicketLoadNumber).OrderBy(t => t.Created).LastOrDefault(); var lastLoadScanTime = (lastLoadScan != null) ? lastLoadScan.LastCreatedOrUpdated : DateTime.Now.AddYears(-1); ModuleScanData scanData = new ModuleScanData(); scanData.Scans = new List <LoadModuleScan>(); Logging.Logger.Log("INFO", "GET TAGS SEEN BETWEEN " + _readerWindowStartTime.ToLocalTime().ToString() + " AND " + DateTime.Now.ToString()); var tags = TagDataProvider.GetTagsFirstSeenInTimeRange(_readerWindowStartTime, DateTime.UtcNow); var tagSerials = tags.Select(t => t.SerialNumber).ToArray(); var ginnedTags = dp.ModuleOwnershipRepository.FindMatching(m => tagSerials.Contains(m.Name) && (m.Status == "Ginned" || m.Status == "On feeder")); if (ginnedTags.Count() > 0) { Logging.Logger.Log("WARNING", "ONE OR MORE TAGS READ WITH GINNED STATUS. GINNED COUNT: " + ginnedTags.Count().ToString()); } var trucks = dp.TruckRepository.GetAll(); foreach (var t in tags) { if (!ginnedTags.Any(g => g.Name == t.SerialNumber) && !trucks.Any(x => x.RFIDTagId == t.SerialNumber || x.RFIDTagId == t.Epc)) { Logging.Logger.Log("INFO", "RECORDING MODULE SN " + t.SerialNumber + " FIRST SEEN AT LOCAL TIME" + t.Firstseen.ToLocalTime().ToString()); scanData.Scans.Add(new LoadModuleScan { EPC = t.Epc, SerialNumber = t.SerialNumber, ScanTime = t.Firstseen }); } } var serialNumbers = tags.Select(t => t.SerialNumber).ToArray(); var moduleOwnershipsForTags = dp.ModuleOwnershipRepository.FindMatching(m => serialNumbers.Contains(m.Name)).ToList(); if (moduleOwnershipsForTags.Count() > 0 && lastOwnership == null) { lastOwnership = moduleOwnershipsForTags[0]; Logging.Logger.Log("INFO", "Found ownership from module."); } if (lastOwnership != null) { Logging.Logger.Log("INFO", "Using ownership " + lastOwnership.Client + "/" + lastOwnership.Farm + "/" + lastOwnership.Field); } bool populateFromOwnership = (lastOwnership != null && lastLoadScan == null); //if ((lastOwnership != null && lastLoadScan == null)/* || (lastOwnership != null && lastOwnership.LastCreatedOrUpdated > lastLoadScan.LastCreatedOrUpdated)*/) // populateFromOwnership = true; if (lastLoadScan == null) { Logging.Logger.Log("INFO", "CREATING NEW LOAD SCAN"); newLoad = true; lastLoadScan = new LoadScanEntity(); lastLoadScan.SubmittedBy = "attendant"; //always use a new load number otherwise a previous load //could get overwritten lastLoadScan.BridgeLoadNumber = dp.LoadScanRepository.LastLoadNumber() + 1; /*if (populateFromOwnership) * if (lastOwnership.BridgeLoadNumber > 0) lastLoadScan.BridgeLoadNumber = lastOwnership.BridgeLoadNumber; * else lastLoadScan.BridgeLoadNumber = dp.LoadScanRepository.LastLoadNumber() + 1; * else * lastLoadScan.BridgeLoadNumber = dp.LoadScanRepository.LastLoadNumber() + 1; */ } lastLoadScan.BridgeID = bridgeID; lastLoadScan.TruckID = _activeTruck.Name; lastLoadScan.GinTagLoadNumber = GinTicketLoadNumber; lastLoadScan.GrossWeight = GrossWeight; lastLoadScan.NetWeight = GrossWeight - _activeTruck.TareWeight; lastLoadScan.SplitWeight1 = Weight1; lastLoadScan.SplitWeight2 = Weight2; lastLoadScan.Latitude = dp.SettingsRepository.GetSettingDoubleValue(BridgeSettingKeys.LATITUDE); lastLoadScan.Longitude = dp.SettingsRepository.GetSettingDoubleValue(BridgeSettingKeys.LONGITUDE); if (populateFromOwnership && lastOwnership != null) { lastLoadScan.TrailerNumber = lastOwnership.TrailerNumber; lastLoadScan.Variety = lastOwnership.Variety; lastLoadScan.YardRow = lastOwnership.Location; lastLoadScan.PickedBy = lastOwnership.PickedBy; } if (populateFromOwnership && lastOwnership != null) { lastLoadScan.Client = lastOwnership.Client; lastLoadScan.Farm = lastOwnership.Farm; lastLoadScan.Field = lastOwnership.Field; } var status = dp.SettingsRepository.GetSettingWithDefault(BridgeSettingKeys.TARGET_STATUS, "AT GIN"); if (status == "AT GIN") { lastLoadScan.TargetStatus = ModuleStatus.AT_GIN; } else { lastLoadScan.TargetStatus = ModuleStatus.ON_FEEDER; } lastLoadScan.SubmittedBy = "attendant"; lastLoadScan.SetSerializedModuleScanData(scanData); dp.LoadScanRepository.Save(lastLoadScan); dp.SaveChanges(); Logging.Logger.WriteBuffer(); } }