private void AddGroupBankAccount(object param) { if (SelectedGroupName != null && BankAccount != null && IsBankAccountValid(BankAccount) && !GroupBankAccounts.Any(x => x.GroupName.BuildingChargeGroupNameId == SelectedGroupName.BuildingChargeGroupNameId)) { GroupBankAccounts.Add(new BuildingChargeGroupBankAccount() { BuildingChargeGroupBankAccountId = Guid.NewGuid(), BankAccount = BankAccount, GroupName = SelectedGroupName, IsDeleted = false }); } }
private void SaveDialog(object param) { if (_buildingLocalCopy == null) { if (!IsValid(this as DependencyObject) || (string.IsNullOrEmpty(BuildingName) || string.IsNullOrEmpty(BuildingCity) || string.IsNullOrEmpty(BuildingZipCode) || string.IsNullOrEmpty(BuildingRoadName) || string.IsNullOrEmpty(BuildingRoadNumber))) { return; } //Add new building using (var db = new DB.DomenaDBContext()) { var newBuilding = new LibDataModel.Building { BuildingId = Guid.NewGuid(), Name = BuildingName, City = BuildingCity, ZipCode = BuildingZipCode, BuildingNumber = BuildingRoadNumber, RoadName = BuildingRoadName, IsDeleted = false }; List <LibDataModel.BuildingChargeBasis> costs = new List <LibDataModel.BuildingChargeBasis>(); foreach (var c in CostCollection) { var catId = db.CostCategories.Where(x => x.CategoryName.Equals(c.CategoryName)).FirstOrDefault().BuildingChargeBasisCategoryId; var cost = new LibDataModel.BuildingChargeBasis { BuildingChargeBasisId = Guid.NewGuid(), BegginingDate = c.BegginingDate.Date, EndingDate = c.EndingDate.Date, CostPerUnit = c.Cost, BuildingChargeBasisDistribution = c.CostUnit.EnumValue, BuildingChargeBasisCategoryId = catId, BuildingChargeGroupNameId = c.CostGroup.BuildingChargeGroupNameId }; costs.Add(cost); } newBuilding.CostCollection = costs; foreach (var m in MetersCollection) { newBuilding.MeterCollection.Add(m); } db.Buildings.Add(newBuilding); foreach (var g in GroupBankAccounts) { g.Building = newBuilding; db.Entry(g.GroupName).State = EntityState.Unchanged; db.BuildingChargeGroupBankAccounts.Add(g); } _buildingLocalCopy = newBuilding; db.SaveChanges(); } } else { if (!IsValid(this as DependencyObject) || (string.IsNullOrEmpty(BuildingName) || string.IsNullOrEmpty(BuildingCity) || string.IsNullOrEmpty(BuildingZipCode) || string.IsNullOrEmpty(BuildingRoadName) || string.IsNullOrEmpty(BuildingRoadNumber))) { return; } //Edit building using (var db = new DB.DomenaDBContext()) { var q = db.Buildings.Include(x => x.CostCollection).Include(x => x.MeterCollection).Where(x => x.BuildingId.Equals(_buildingLocalCopy.BuildingId)).FirstOrDefault(); q.BuildingNumber = BuildingRoadNumber; q.City = BuildingCity; q.Name = BuildingName; q.RoadName = BuildingRoadName; q.ZipCode = BuildingZipCode; //q.CostCollection.RemoveAll(x => true); List <LibDataModel.BuildingChargeBasis> costs = new List <LibDataModel.BuildingChargeBasis>(); foreach (var c in CostCollection) { var catId = db.CostCategories.Where(x => x.CategoryName.Equals(c.CategoryName)).FirstOrDefault().BuildingChargeBasisCategoryId; var cost = new LibDataModel.BuildingChargeBasis { BuildingChargeBasisId = Guid.NewGuid(), BegginingDate = c.BegginingDate.Date, EndingDate = c.EndingDate.Date, CostPerUnit = c.Cost, BuildingChargeBasisDistribution = c.CostUnit.EnumValue, BuildingChargeBasisCategoryId = catId, BuildingChargeGroupNameId = c.CostGroup.BuildingChargeGroupNameId }; costs.Add(cost); } q.CostCollection = costs; //Add new foreach (var m in MetersCollection) { if (!q.MeterCollection.Any(x => x.MeterId.Equals(m.MeterId))) { q.MeterCollection.Add(m); } } //Remove necessary for (int i = q.MeterCollection.Count - 1; i >= 0; i--) { if (!MetersCollection.Any(x => x.MeterId.Equals(q.MeterCollection[i].MeterId))) { q.MeterCollection.RemoveAt(i); } else { // Change names q.MeterCollection[i].Name = MetersCollection.FirstOrDefault(x => x.MeterId.Equals(q.MeterCollection[i].MeterId)).Name; if (q.MeterCollection[i].LastMeasure != MetersCollection.FirstOrDefault(x => x.MeterId.Equals(q.MeterCollection[i].MeterId)).LastMeasure) { var nm = new MetersHistory { MeterHistoryId = Guid.NewGuid(), Apartment = null, ApartmentMeter = null, Building = q, MeterType = q.MeterCollection[i], ModifiedDate = DateTime.Now, NewValue = q.MeterCollection[i].LastMeasure, OldValue = MetersCollection.FirstOrDefault(x => x.MeterId.Equals(q.MeterCollection[i].MeterId)).LastMeasure, }; db.MetersHistories.Add(nm); db.Entry(nm.Building).State = EntityState.Unchanged; q.MeterCollection[i].LastMeasure = MetersCollection.FirstOrDefault(x => x.MeterId.Equals(q.MeterCollection[i].MeterId)).LastMeasure; } q.MeterCollection[i].IsBuilding = MetersCollection.FirstOrDefault(x => x.MeterId.Equals(q.MeterCollection[i].MeterId)).IsBuilding; q.MeterCollection[i].IsApartment = MetersCollection.FirstOrDefault(x => x.MeterId.Equals(q.MeterCollection[i].MeterId)).IsApartment; } } var buildingBankAddresses = db.BuildingChargeGroupBankAccounts.Where(x => x.Building.BuildingId == q.BuildingId).ToList(); //Add new foreach (var bba in GroupBankAccounts) { if (!buildingBankAddresses.Any(x => x.BuildingChargeGroupBankAccountId == bba.BuildingChargeGroupBankAccountId)) { bba.Building = q; db.BuildingChargeGroupBankAccounts.Add(bba); db.Entry(bba.GroupName).State = EntityState.Unchanged; db.Entry(bba.Building).State = EntityState.Unchanged; } } //Remove necessary for (int i = buildingBankAddresses.Count - 1; i >= 0; i--) { if (!GroupBankAccounts.Any(x => x.BuildingChargeGroupBankAccountId.Equals(buildingBankAddresses[i].BuildingChargeGroupBankAccountId))) { buildingBankAddresses[i].IsDeleted = true; } else { // Change names buildingBankAddresses[i].BankAccount = GroupBankAccounts.FirstOrDefault(x => x.BuildingChargeGroupBankAccountId == buildingBankAddresses[i].BuildingChargeGroupBankAccountId).BankAccount; buildingBankAddresses[i].GroupName = GroupBankAccounts.FirstOrDefault(x => x.BuildingChargeGroupBankAccountId == buildingBankAddresses[i].BuildingChargeGroupBankAccountId).GroupName; //db.Entry(buildingBankAddresses[i].Building).State = EntityState.Unchanged; db.Entry(buildingBankAddresses[i].GroupName).State = EntityState.Unchanged; } } db.SaveChanges(); } } }
private void DeleteGroupBankAccount(object param) { GroupBankAccounts.Remove(SelectedGroupBankAccount); }