Esempio n. 1
0
        private async void ExtendedClosingEventHandler(object sender, DialogClosingEventArgs eventArgs)
        {
            if ((bool)eventArgs.Parameter)
            {
                var dc = (eventArgs.Session.Content as Wizards.EditApartmentWizard);
                //Accept
                if (dc._apartmentLocalCopy == null)
                {
                    if (!IsValid(dc as DependencyObject) || (string.IsNullOrEmpty(dc.SelectedBuildingAddress) || string.IsNullOrEmpty(dc.SelectedOwnerMailAddress) || dc.ApartmentNumber <= 0 || double.Parse(dc.AdditionalArea) < 0 || double.Parse(dc.ApartmentArea) <= 0))
                    {
                        eventArgs.Cancel();
                        return;
                    }
                    //Add new apartment
                    using (var db = new DB.DomenaDBContext())
                    {
                        var newApartment = new LibDataModel.Apartment {
                            BoughtDate = dc.BoughtDate.Date, ApartmentId = Guid.NewGuid(), BuildingId = dc.SelectedBuildingName.BuildingId, AdditionalArea = double.Parse(dc.AdditionalArea), ApartmentArea = double.Parse(dc.ApartmentArea), IsDeleted = false, OwnerId = dc.SelectedOwnerName.OwnerId, CreatedDate = DateTime.Now, ApartmentNumber = dc.ApartmentNumber, MeterCollection = new List <ApartmentMeter>(), Locators = dc.LocatorsAmount
                        };
                        if (!dc.SelectedOwnerMailAddress.Equals(db.Owners.Where(x => x.OwnerId == dc._apartmentLocalCopy.OwnerId).Select(x => x.MailAddress)))
                        {
                            newApartment.CorrespondenceAddress = dc.SelectedOwnerMailAddress;
                        }
                        else
                        {
                            newApartment.CorrespondenceAddress = null;
                        }
                        var q = dc.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.Apartments.Add(newApartment);
                        db.SaveChanges();
                    }
                }
                else
                {
                    if (!IsValid(dc as DependencyObject) || (string.IsNullOrEmpty(dc.SelectedBuildingAddress) || string.IsNullOrEmpty(dc.SelectedOwnerMailAddress) || dc.ApartmentNumber <= 0 || double.Parse(dc.AdditionalArea) < 0 || double.Parse(dc.ApartmentArea) <= 0))
                    {
                        eventArgs.Cancel();
                        return;
                    }
                    //Edit Apartment
                    using (var db = new DB.DomenaDBContext())
                    {
                        var q = db.Apartments.Include(x => x.MeterCollection).Where(x => x.ApartmentId.Equals(dc._apartmentLocalCopy.ApartmentId)).FirstOrDefault();
                        q.BoughtDate      = dc.BoughtDate.Date;
                        q.AdditionalArea  = double.Parse(dc.AdditionalArea);
                        q.ApartmentArea   = double.Parse(dc.ApartmentArea);
                        q.ApartmentNumber = dc.ApartmentNumber;
                        q.BuildingId      = dc.SelectedBuildingName.BuildingId;
                        q.CreatedDate     = DateTime.Now;
                        //q.HasWaterMeter = dc.HasWaterMeter == 0;
                        //q.WaterMeterExp = dc.WaterMeterExp.Date;
                        q.OwnerId  = dc.SelectedOwnerName.OwnerId;
                        q.Locators = dc.LocatorsAmount;

                        if (!dc.SelectedOwnerMailAddress.Equals(db.Owners.Where(x => x.OwnerId == dc._apartmentLocalCopy.OwnerId).Select(x => x.MailAddress)))
                        {
                            q.CorrespondenceAddress = dc.SelectedOwnerMailAddress;
                        }
                        else
                        {
                            q.CorrespondenceAddress = null;
                        }

                        var meters = dc.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;
                                s.LastMeasure      = m.LastMeasure;
                            }
                        }
                        foreach (var m in q.MeterCollection)
                        {
                            if (!meters.Any(x => x.MeterId.Equals(m.MeterId)))
                            {
                                m.IsDeleted = true;
                            }
                        }

                        db.SaveChanges();
                    }
                }
            }
            else if (!(bool)eventArgs.Parameter)
            {
                bool ynResult = await Helpers.YNMsg.Show("Czy chcesz anulować?");

                if (!ynResult)
                {
                    //eventArgs.Cancel();
                    var dc     = (eventArgs.Session.Content as Wizards.EditApartmentWizard);
                    var result = await DialogHost.Show(dc, "RootDialog", ExtendedOpenedEventHandler, ExtendedClosingEventHandler);
                }
            }
            InitializeCollection();
            DrawerHost.CloseDrawerCommand.Execute(Dock.Bottom, this.DH);
        }
        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();
                }
            }
        }