Example #1
0
        private void SaveDialog(object param)
        {
            if (!IsValid(this as DependencyObject) || SelectedBuilding == null || ChargeDate == null)
            {
                return;
            }

            var charges = new List <Charge>();

            using (var db = new DB.DomenaDBContext())
            {
                var apartments = db.Apartments.Where(x => !x.IsDeleted && x.SoldDate == null && x.BuildingId == SelectedBuilding.BuildingId).ToList();

                foreach (var ap in apartments)
                {
                    var newCharge = new LibDataModel.Charge();
                    newCharge.SettlementId = Guid.Empty;
                    newCharge.ChargeId     = Guid.NewGuid();
                    newCharge.ApartmentId  = ap.ApartmentId;
                    newCharge.CreatedDate  = DateTime.Today;
                    newCharge.ChargeDate   = ChargeDate;
                    newCharge.IsClosed     = false;
                    newCharge.IsDeleted    = false;
                    newCharge.OwnerId      = ap.OwnerId;

                    db.Charges.Add(newCharge);
                    charges.Add(newCharge);
                }
                db.SaveChanges();
            }

            foreach (var c in charges)
            {
                ChargesOperations.RecalculateCharge(c);
            }

            SwitchPage.SwitchMainPage(new Pages.ChargesPage(), this);
        }
Example #2
0
        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();
                }
            }
        }