private void AddDoc(Parcel parcel) { var parcelDetailsViewModel = (ParcelDetailsViewModel)DataContext; Document doc = new Document(); doc.exposure = true; doc.quantity = 1; doc.idWarehouse = Properties.Settings.Default.Warehouse; doc.idEmployee = Properties.Settings.Default.IdUser; doc.summary = parcel.amount; doc.code = $"/{DateTime.Now.Month}/{DateTime.Now.Year}"; int idType = 0; switch (parcelDetailsViewModel.StatusSelected.id) { case (int)EnumParcelStatus.acceptedSender: idType = CompanyEntities.TypeOfDocument.FirstOrDefault(p => p.type.Equals("PZ")).id; break; case (int)EnumParcelStatus.beetwen: idType = CompanyEntities.TypeOfDocument.FirstOrDefault(p => p.type.Equals("WZ")).id; break; case (int)EnumParcelStatus.acceptedReciver: idType = CompanyEntities.TypeOfDocument.FirstOrDefault(p => p.type.Equals("PZ")).id; break; case (int)EnumParcelStatus.handed: idType = CompanyEntities.TypeOfDocument.FirstOrDefault(p => p.type.Equals("WZ")).id; break; } if (idType == 0) { return; } doc.idTypeOfDocument = idType; CompanyEntities.Document.Add(doc); CompanyEntities.SaveChanges(); ParcelMoving parcelMoving = new ParcelMoving(); if (idType == (int)EnumTypeOfDocument.PZ) { parcelMoving.idDocPZ = doc.id; parcelMoving.readingPZ = true; parcelMoving.readingWZ = false; } else { parcelMoving.idDocWZ = doc.id; parcelMoving.readingWZ = true; parcelMoving.readingPZ = false; } parcelMoving.idParcel = parcel.id; doc.code = doc.id + doc.code; CompanyEntities.ParcelMoving.Add(parcelMoving); CompanyEntities.SaveChanges(); }
private Document UpdateBuffer() { var addViewModel = (WarehouseAddViewModel)DataContext; var document = addViewModel.Document; var info = new InfoWindow(); if (document == null) { if (Parcel.Count == 0) { return(null); } document = new Document(); document.idTypeOfDocument = companyEntities.TypeOfDocument.FirstOrDefault(p => p.type.Equals(documentType.ToString())).id; document.idWarehouse = Properties.Settings.Default.Warehouse; document.code = $"/{DateTime.Now.Month}/{DateTime.Now.Year}"; document.quantity = Parcel.Count; document.exposure = false; companyEntities.Document.Add(document); companyEntities.SaveChanges(); document.code = document.id + document.code; foreach (var p in Parcel) { ParcelMoving temp = null; if (documentType == EnumTypeOfDocument.WZ) { temp = companyEntities.ParcelMoving.FirstOrDefault(pm => pm.idParcel == p.id && !pm.readingWZ); } bool isAdd = false; if (temp == null) { isAdd = true; temp = new ParcelMoving(); } if (documentType == EnumTypeOfDocument.PZ) { temp.idDocPZ = document.id; } else { temp.idDocWZ = document.id; } temp.idParcel = p.id; if (documentType == EnumTypeOfDocument.PZ) { temp.readingPZ = true; } else if (documentType == EnumTypeOfDocument.WZ) { temp.readingWZ = true; } if (isAdd) { companyEntities.ParcelMoving.Add(temp); } } companyEntities.SaveChanges(); } else { var actuallyList = addViewModel.actuallyParcelList; if (Parcel.Count == 0 && actuallyList.Count == 0) { return(null); } foreach (var rem in actuallyList) { if (Parcel.Any(p => p.id == rem.id)) { continue; } var temp = companyEntities.ParcelMoving.FirstOrDefault(p => p.idParcel == rem.id); if (temp != null) { companyEntities.ParcelMoving.Remove(temp); } } foreach (var p in Parcel) { if (actuallyList.Any(parcel => parcel.id == p.id)) { continue; } ParcelMoving temp = null; if (documentType == EnumTypeOfDocument.WZ) { temp = companyEntities.ParcelMoving.FirstOrDefault(pm => pm.idParcel == p.id && !pm.readingWZ); } bool isAdd = false; if (temp == null) { isAdd = true; temp = new ParcelMoving(); } if (documentType == EnumTypeOfDocument.PZ) { temp.idDocPZ = document.id; } else { temp.idDocWZ = document.id; } temp.idParcel = p.id; if (documentType == EnumTypeOfDocument.PZ) { temp.readingPZ = true; } else if (documentType == EnumTypeOfDocument.WZ) { temp.readingWZ = true; } if (isAdd) { companyEntities.ParcelMoving.Add(temp); } } companyEntities.SaveChanges(); document = companyEntities.Document.FirstOrDefault(d => d.id == document.id); document.quantity = companyEntities.ParcelMoving.Where(p => documentType == EnumTypeOfDocument.PZ ? p.idDocPZ == document.id : p.idDocWZ == document.id).ToList().Count; companyEntities.SaveChanges(); } return(document); }