private void SaveDialog(object param) { if (!IsValid(this as DependencyObject)) { return; } decimal amount; bool isAmountValid = decimal.TryParse(this.PaymentAmount, out amount); //Accept if (this._lpc == null) { if (!IsValid(this as DependencyObject) || (string.IsNullOrEmpty(this.SelectedBuildingValue) || string.IsNullOrEmpty(this.SelectedApartmentNumberValue) || !isAmountValid)) { return; } //Add new payment using (var db = new DB.DomenaDBContext()) { var newPayment = new Payment() { IsDeleted = false, ApartmentId = this.SelectedApartmentNumber.ApartmentId, PaymentAddDate = DateTime.Today, PaymentAmount = amount, PaymentId = Guid.NewGuid(), PaymentRegistrationDate = this.PaymentRegistrationDate, ChargeGroup = SelectedGroupName }; db.Payments.Add(newPayment); db.Entry(newPayment.ChargeGroup).State = EntityState.Unchanged; db.SaveChanges(); } } else { if (!IsValid(this as DependencyObject) || (string.IsNullOrEmpty(this.SelectedBuildingValue) || string.IsNullOrEmpty(this.SelectedApartmentNumberValue) || !isAmountValid)) { return; } //Edit payment using (var db = new DB.DomenaDBContext()) { var q = db.Payments.Where(x => x.PaymentId.Equals(this._lpc.PaymentId)).FirstOrDefault(); q.ChargeGroup = SelectedGroupName; q.PaymentAddDate = DateTime.Today; q.PaymentRegistrationDate = this.PaymentRegistrationDate; q.PaymentAmount = amount; db.Entry(q.ChargeGroup).State = EntityState.Unchanged; db.SaveChanges(); } } }
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 SaveDialog(object param) { if (_apartmentLocalCopy == null) { if (!IsValid(this as DependencyObject) || (string.IsNullOrEmpty(SelectedBuildingAddress) || string.IsNullOrEmpty(SelectedOwnerMailAddress) || ApartmentNumber <= 0 || double.Parse(AdditionalArea) < 0 || double.Parse(ApartmentArea) <= 0)) { return; } //Add new apartment using (var db = new DB.DomenaDBContext()) { var newApartment = new LibDataModel.Apartment { BoughtDate = BoughtDate.Date, ApartmentId = Guid.NewGuid(), BuildingId = SelectedBuildingName.BuildingId, AdditionalArea = double.Parse(AdditionalArea), ApartmentArea = double.Parse(ApartmentArea), IsDeleted = false, SoldDate = null, OwnerId = SelectedOwnerName.OwnerId, CreatedDate = DateTime.Now, ApartmentNumber = ApartmentNumber, MeterCollection = new List <ApartmentMeter>(), Locators = LocatorsAmount }; if (!SelectedOwnerMailAddress.Equals(db.Owners.Where(x => x.OwnerId == _apartmentLocalCopy.OwnerId).Select(x => x.MailAddress))) { newApartment.CorrespondenceAddress = SelectedOwnerMailAddress; } else { newApartment.CorrespondenceAddress = null; } var q = MeterCollection.Where(x => !x.IsDeleted); foreach (var m in q) { newApartment.MeterCollection.Add(new ApartmentMeter() { IsDeleted = false, LastMeasure = m.LastMeasure, MeterTypeParent = m.MeterTypeParent, LegalizationDate = m.LegalizationDate, MeterId = m.MeterId }); db.Entry(m.MeterTypeParent).State = EntityState.Unchanged; } db.Apartments.Add(newApartment); // Add initial charge var building = db.Buildings.Include(x => x.CostCollection).FirstOrDefault(x => x.BuildingId == newApartment.BuildingId); double percentage = 1 - (BoughtDate.Day / (double)DateTime.DaysInMonth(BoughtDate.Year, BoughtDate.Month)); var chargeDate = BoughtDate; while (chargeDate <= DateTime.Today) { var c = new Charge() { ApartmentId = newApartment.ApartmentId, ChargeId = Guid.NewGuid(), IsClosed = false, ChargeDate = chargeDate, CreatedDate = DateTime.Today, SettlementId = Guid.Empty, AutoChargeId = Guid.Empty, OwnerId = newApartment.OwnerId }; c.Components = new List <ChargeComponent>(); foreach (var costCollection in building.CostCollection) { if (costCollection.BegginingDate > chargeDate || (costCollection.EndingDate.Year > 1901 && costCollection.EndingDate < chargeDate)) { continue; } var group = db.GroupName.FirstOrDefault(x => x.BuildingChargeGroupNameId == costCollection.BuildingChargeGroupNameId); var cc = new ChargeComponent() { ChargeComponentId = Guid.NewGuid(), CostCategoryId = costCollection.BuildingChargeBasisCategoryId, CostDistribution = costCollection.BuildingChargeBasisDistribution, CostPerUnit = costCollection.CostPerUnit, GroupName = group }; double units; switch ((CostDistribution)cc.CostDistribution) { case CostDistribution.PerApartment: units = 1; break; case CostDistribution.PerApartmentTotalArea: units = newApartment.AdditionalArea + newApartment.ApartmentArea; break; case CostDistribution.PerApartmentArea: units = newApartment.ApartmentArea; break; case CostDistribution.PerAdditionalArea: units = newApartment.AdditionalArea; break; case CostDistribution.PerLocators: units = newApartment.Locators; break; default: units = 0; break; } cc.Sum = Math.Round(((Convert.ToDecimal(units) * cc.CostPerUnit) * Convert.ToDecimal(percentage)), 2); c.Components.Add(cc); db.Entry(cc.GroupName).State = EntityState.Unchanged; } if (c.Components != null && c.Components.Count > 0) { db.Charges.Add(c); } chargeDate = (new DateTime(chargeDate.Year, chargeDate.Month, 1)).AddMonths(1); percentage = 1; } db.SaveChanges(); _apartmentLocalCopy = newApartment; } } else { if (!IsValid(this as DependencyObject) || (string.IsNullOrEmpty(SelectedBuildingAddress) || string.IsNullOrEmpty(SelectedOwnerMailAddress) || ApartmentNumber <= 0 || double.Parse(AdditionalArea) < 0 || double.Parse(ApartmentArea) <= 0)) { return; } //Edit Apartment using (var db = new DB.DomenaDBContext()) { var q = db.Apartments.Include(x => x.MeterCollection).Where(x => x.ApartmentId.Equals(_apartmentLocalCopy.ApartmentId)).FirstOrDefault(); q.BoughtDate = BoughtDate.Date; q.AdditionalArea = double.Parse(AdditionalArea); q.ApartmentArea = double.Parse(ApartmentArea); q.ApartmentNumber = ApartmentNumber; q.BuildingId = SelectedBuildingName.BuildingId; q.CreatedDate = DateTime.Now; //q.HasWaterMeter = dc.HasWaterMeter == 0; //q.WaterMeterExp = dc.WaterMeterExp.Date; q.OwnerId = SelectedOwnerName.OwnerId; q.Locators = LocatorsAmount; if (!SelectedOwnerMailAddress.Equals(db.Owners.Where(x => x.OwnerId == _apartmentLocalCopy.OwnerId).Select(x => x.MailAddress))) { q.CorrespondenceAddress = SelectedOwnerMailAddress; } else { q.CorrespondenceAddress = null; } var meters = MeterCollection.Where(x => !x.IsDeleted); foreach (var m in meters) { if (!q.MeterCollection.Any(x => x.MeterId.Equals(m.MeterId))) { q.MeterCollection.Add(m); //var a = db.Buildings.SelectMany(x => x.MeterCollection).FirstOrDefault(x => x.MeterId.Equals(m.MeterTypeParent.MeterId)); db.Entry(m.MeterTypeParent).State = EntityState.Unchanged; } else { var s = q.MeterCollection.FirstOrDefault(x => x.MeterId.Equals(m.MeterId)); s.LegalizationDate = m.LegalizationDate; db.MetersHistories.Add(new MetersHistory { MeterHistoryId = Guid.NewGuid(), Apartment = q, Building = db.Buildings.FirstOrDefault(x => x.BuildingId == q.BuildingId), ApartmentMeter = s, MeterType = s.MeterTypeParent, ModifiedDate = DateTime.Now, NewValue = m.LastMeasure, OldValue = s.LastMeasure }); s.LastMeasure = m.LastMeasure; } } foreach (var m in q.MeterCollection) { if (!meters.Any(x => x.MeterId.Equals(m.MeterId))) { m.IsDeleted = true; } } db.SaveChanges(); } } }
private void SaveDialog(object param) { if (_charge == null) { if (!IsValid(this as DependencyObject) || (string.IsNullOrEmpty(SelectedBuildingValue) || string.IsNullOrEmpty(SelectedApartmentNumberValue))) { return; } //Add new apartment using (var db = new DB.DomenaDBContext()) { var newCharge = new LibDataModel.Charge(); newCharge.SettlementId = Guid.Empty; newCharge.ChargeId = Guid.NewGuid(); newCharge.ApartmentId = db.Apartments.FirstOrDefault(x => x.BuildingId.Equals(SelectedBuilding.BuildingId) && x.ApartmentNumber.Equals(SelectedApartmentNumber)).ApartmentId; newCharge.CreatedDate = DateTime.Today; newCharge.ChargeDate = ChargeDate; newCharge.IsClosed = ChargeStatus == "Otwarte" ? false : true; newCharge.IsDeleted = false; newCharge.OwnerId = db.Apartments.FirstOrDefault(x => x.BuildingId.Equals(SelectedBuilding.BuildingId) && x.ApartmentNumber.Equals(SelectedApartmentNumber)).OwnerId; newCharge.Components = new List <ChargeComponent>(); foreach (var cc in ChargeComponents) { newCharge.Components.Add(cc); db.Entry(cc.GroupName).State = EntityState.Unchanged; } db.Charges.Add(newCharge); db.SaveChanges(); } } else { if (!IsValid(this as DependencyObject) || (string.IsNullOrEmpty(SelectedBuildingValue) || string.IsNullOrEmpty(SelectedApartmentNumberValue))) { return; } //Edit Apartment using (var db = new DB.DomenaDBContext()) { var q = db.Charges.Include(x => x.Components).Where(x => x.ChargeId.Equals(_charge.ChargeId)).FirstOrDefault(); q.IsClosed = ChargeStatus == "Otwarte" ? false : true; q.ChargeDate = ChargeDate; //q.Components = new List<ChargeComponent>(); foreach (var cc in ChargeComponents) { //q.Components.Add(cc); if (q.Components.Any(x => x.ChargeComponentId.Equals(cc.ChargeComponentId))) { var comp = q.Components.FirstOrDefault(x => x.ChargeComponentId.Equals(cc.ChargeComponentId)); comp.CostCategoryId = cc.CostCategoryId; comp.CostDistribution = cc.CostDistribution; comp.CostPerUnit = cc.CostPerUnit; comp.Sum = cc.Sum; } else { q.Components.Add(cc); db.Entry(cc.GroupName).State = EntityState.Unchanged; } } for (int i = q.Components.Count - 1; i >= 0; i--) { if (!ChargeComponents.Any(x => x.ChargeComponentId.Equals(q.Components[i].ChargeComponentId))) { q.Components.RemoveAt(i); } } db.SaveChanges(); } } }