public bool Delete(tblSample entity) { _context.tblSamples.Attach(entity as tblSample); _context.tblSamples.Remove(entity as tblSample); return(true); }
public bool Update(tblSample entity) { _context.tblSamples.Attach(entity as tblSample); _context.Entry(entity as tblSample).State = EntityState.Modified; return(true); }
private void SetTblSampleNotes(tblSample tblSample, SampleOrder sampleOrder, ref bool commitNewContext) { var existingNotes = tblSample.tblSampleNotes.ToDictionary(n => n.SamNoteID); var samNoteID = OldContext.tblSampleNotes.Select(n => n.SamNoteID).DefaultIfEmpty(DateTime.Now.RoundMillisecondsForSQL()).Max(); foreach(var entry in sampleOrder.JournalEntries) { tblSampleNote note = null; if(entry.SamNoteID != null && existingNotes.TryGetValue(entry.SamNoteID.Value, out note)) { existingNotes.Remove(note.SamNoteID); } if(note == null) { commitNewContext = true; samNoteID = samNoteID.AddSeconds(1); note = new tblSampleNote { SamNoteID = samNoteID, SampleID = tblSample.SampleID, s_GUID = Guid.NewGuid() }; OldContext.tblSampleNotes.AddObject(note); entry.SamNoteID = note.SamNoteID; } note.EmployeeID = entry.EmployeeId; note.SampleJnlDate = entry.Date; note.SampleNote = entry.Text; } foreach(var tblSampleNote in existingNotes) { OldContext.tblSampleNotes.DeleteObject(tblSampleNote.Value); } }
public bool Add(tblSample entity) { _context.tblSamples.Add(entity as tblSample); return(true); }
private tblSample Sync(SampleOrderKey sampleOrderKey, out bool commitNewContext) { commitNewContext = false; var sampleOrder = UnitOfWork.SampleOrderRepository.FindByKey(sampleOrderKey, s => s.Broker, s => s.RequestCustomer.Company, s => s.JournalEntries, s => s.Items.Select(i => i.Product), s => s.Items.Select(i => i.Lot), s => s.Items.Select(i => i.Spec), s => s.Items.Select(i => i.Match)); if(sampleOrder == null) { throw new Exception(string.Format("Could not load SampleOrder[{0}]", sampleOrderKey)); } var tblSample = GetTblSampleRecords(sampleOrder.SampleID); if(tblSample == null) { commitNewContext = true; var minSampleId = sampleOrder.Year * 1000; var sampleId = OldContext.tblSamples.Select(s => s.SampleID).Where(i => i > minSampleId).DefaultIfEmpty(minSampleId).Max() + 1; tblSample = new tblSample { SampleID = sampleId, tblSampleNotes = new EntityCollection<tblSampleNote>(), tblSampleDetails = new EntityCollection<tblSampleDetail>(), s_GUID = Guid.NewGuid() }; OldContext.tblSamples.AddObject(tblSample); sampleOrder.SampleID = tblSample.SampleID; } tblSample.EmployeeID = sampleOrder.EmployeeId; tblSample.EntryDate = sampleOrder.TimeStamp.ConvertUTCToLocal(); tblSample.Comments = sampleOrder.Comments; tblSample.Notes2Print = sampleOrder.PrintNotes; tblSample.Volume = (decimal?) sampleOrder.Volume; tblSample.SampleDate = sampleOrder.DateDue; tblSample.DateRecd = sampleOrder.DateReceived; tblSample.DateCompleted = sampleOrder.DateCompleted; tblSample.Completed = sampleOrder.DateCompleted.HasValue; tblSample.Priority = sampleOrder.ShipmentMethod; tblSample.FOB = sampleOrder.FOB; tblSample.Status = sampleOrder.Status.ToDisplayString(); tblSample.Active = sampleOrder.Active; tblSample.Company_IA = sampleOrder.RequestCustomer == null ? null : sampleOrder.RequestCustomer.Company.Name; tblSample.Broker = sampleOrder.Broker == null ? null : sampleOrder.Broker.Name; tblSample.Contact_IA = sampleOrder.Request.Name; tblSample.Address1_IA = sampleOrder.Request.Address.AddressLine1; tblSample.Address2_IA = sampleOrder.Request.Address.AddressLine2; tblSample.Address3_IA = sampleOrder.Request.Address.AddressLine3; tblSample.City_IA = sampleOrder.Request.Address.City; tblSample.State_IA = sampleOrder.Request.Address.State; tblSample.Zip_IA = sampleOrder.Request.Address.PostalCode; tblSample.Country_IA = sampleOrder.Request.Address.Country; tblSample.SCompany = sampleOrder.ShipToCompany; tblSample.SContact = sampleOrder.ShipTo.Name; tblSample.SPhone = sampleOrder.ShipTo.Phone; tblSample.SAddress1 = sampleOrder.ShipTo.Address.AddressLine1; tblSample.SAddress2 = sampleOrder.ShipTo.Address.AddressLine2; tblSample.SAddress3 = sampleOrder.ShipTo.Address.AddressLine3; tblSample.SCity = sampleOrder.ShipTo.Address.City; tblSample.SState = sampleOrder.ShipTo.Address.State; tblSample.SZip = sampleOrder.ShipTo.Address.PostalCode; tblSample.SCountry = sampleOrder.ShipTo.Address.Country; SetTblSampleNotes(tblSample, sampleOrder, ref commitNewContext); SetTblSampleDetails(tblSample, sampleOrder, ref commitNewContext); return tblSample; }
private void SetTblSampleDetails(tblSample tblSample, SampleOrder sampleOrder, ref bool commitNewContext) { var existingDetails = tblSample.tblSampleDetails.ToDictionary(n => n.SampleDetailID); var sampleDetailId = OldContext.tblSampleDetails.Select(n => n.SampleDetailID).DefaultIfEmpty(DateTime.Now.RoundMillisecondsForSQL()).Max(); var custSpecId = OldContext.tblSampleCustSpecs.Select(n => n.CustSpecID).DefaultIfEmpty(DateTime.Now.RoundMillisecondsForSQL()).Max(); var rvcMatchId = OldContext.tblSampleRVCMatches.Select(n => n.RVCMatchID).DefaultIfEmpty(DateTime.Now.RoundMillisecondsForSQL()).Max(); foreach(var item in sampleOrder.Items) { tblSampleDetail detail = null; if(item.SampleDetailID != null && existingDetails.TryGetValue(item.SampleDetailID.Value, out detail)) { existingDetails.Remove(detail.SampleDetailID); } if(detail == null) { commitNewContext = true; sampleDetailId = sampleDetailId.AddSeconds(1); detail = new tblSampleDetail { SampleDetailID = sampleDetailId, SampleID = tblSample.SampleID, s_GUID = Guid.NewGuid() }; OldContext.tblSampleDetails.AddObject(detail); item.SampleDetailID = detail.SampleDetailID; } detail.ProdID = item.Product == null ? (int?) null : int.Parse(item.Product.ProductCode); detail.Lot = item.Lot == null ? (int?) null : LotNumberParser.BuildLotNumber(item.Lot); detail.Qty = item.Quantity; detail.Desc = item.Description; detail.SampleMatch = item.CustomerProductName; if(item.Spec == null) { DeleteTblSampleCustSpecs(detail); } else { var spec = detail.tblSampleCustSpecs.FirstOrDefault(s => s.CustSpecID == item.Spec.CustSpecID); if(spec == null) { commitNewContext = true; custSpecId = custSpecId.AddSeconds(1); spec = new tblSampleCustSpec { CustSpecID = custSpecId, SampleDetailID = detail.SampleDetailID }; OldContext.tblSampleCustSpecs.AddObject(spec); item.Spec.CustSpecID = spec.CustSpecID; } SetSpec(spec, item.Spec); } if(item.Match == null) { DeleteTblSampleRVCMatches(detail); } else { var match = detail.tblSampleRVCMatches.FirstOrDefault(s => s.RVCMatchID == item.Match.RVCMatchID); if(match == null) { commitNewContext = true; rvcMatchId = rvcMatchId.AddSeconds(1); match = new tblSampleRVCMatch { RVCMatchID = rvcMatchId, SampleDetailID = detail.SampleDetailID }; OldContext.tblSampleRVCMatches.AddObject(match); item.Match.RVCMatchID = match.RVCMatchID; } SetMatch(match, item.Match); } } foreach(var detail in existingDetails) { DeleteTblSampleDetail(detail.Value); } }