public void UpdateAccountsTable()
 {
     using (var dbContext = new CalculationSystemDbContext())
     {
         AccountsGrid.ItemsSource = dbContext.Accounts.Include("House").ToList();
     }
 }
예제 #2
0
 private void SetOpenedPerion()
 {
     using (var db = new CalculationSystemDbContext())
     {
         OpenedPeriod = db.Periods.SingleOrDefault(p => p.IsOpened);
     }
 }
 private void LoadDevices()
 {
     using (var db = new CalculationSystemDbContext())
     {
         devices = new ObservableCollection <MeteringDevice>(db.MeteringDevices.Include("House").ToList());
     }
 }
 public void UpdateOrRefresh()
 {
     using (var dbContext = new CalculationSystemDbContext())
     {
         housingRegistryDataGrid.ItemsSource = dbContext.Houses.ToList();
     }
 }
예제 #5
0
 private void SavePeriod(Period p)
 {
     using (var db = new CalculationSystemDbContext())
     {
         db.Periods.Add(p);
         db.SaveChanges();
     }
 }
예제 #6
0
 private void ClosePeriod()
 {
     using (var db = new CalculationSystemDbContext())
     {
         Period period = db.Periods.Single(p => p.Id == OpenedPeriod.Id);
         period.IsOpened = false;
         db.SaveChanges();
     }
 }
 private void RemoveDevice(int deviceId)
 {
     using (var dbContext = new CalculationSystemDbContext())
     {
         MeteringDevice device = dbContext.MeteringDevices.Single(d => d.Id == deviceId);
         dbContext.MeteringDevices.Remove(device);
         dbContext.SaveChanges();
     }
 }
예제 #8
0
 private Period GetLastOpenedPeriod()
 {
     using (var db = new CalculationSystemDbContext())
     {
         return(db.Periods
                .Where(p => p.Year == DateTime.Now.Year)
                .OrderByDescending(p => p.Id)
                .FirstOrDefault());
     }
 }
 private MeteringDevice UpdateDevice(int deviceId, double readings)
 {
     using (var dbContext = new CalculationSystemDbContext())
     {
         MeteringDevice device = dbContext.MeteringDevices.Single(d => d.Id == deviceId);
         SaveInitialReadingForCurrentPeriod(dbContext, device.Id, device.Readings);
         device.Readings = readings;
         dbContext.SaveChanges();
         return(dbContext.MeteringDevices.Include("House").Single(d => d.Id == deviceId));
     }
 }
        public HomeSelectionWindow()
        {
            InitializeComponent();
            WindowStartupLocation = WindowStartupLocation.CenterOwner;
            Owner = Application.Current.MainWindow;

            using (var newDbContext = new CalculationSystemDbContext())
            {
                homeSelectionDataGrid.ItemsSource = newDbContext.Houses.ToList();
            }
        }
 private IEnumerable <Account> GetAllAccounts()
 {
     using (var context = new CalculationSystemDbContext())
     {
         return(new ObservableCollection <Account>(
                    context.Accounts
                    .Include("House")
                    .Include("Services")
                    .Include("Services.Prices")));
     }
 }
예제 #12
0
 private IEnumerable <PeriodModel> GetClosedPeriods()
 {
     using (var db = new CalculationSystemDbContext())
     {
         return(db.Periods
                .Where(p => p.Year == DateTime.Now.Year && !p.IsOpened)
                .OrderBy(p => p.Id)
                .ToList()
                .Select(p => new PeriodModel
         {
             Id = p.Id,
             Name = $"{DateTimeFormatInfo.CurrentInfo.GetMonthName(p.Month)} {p.Year}"
         }));
     }
 }
예제 #13
0
        private void SaveAccrual(int accountId, Calculation calculation, CalculationSystemDbContext db)
        {
            var accrual = db.Accruals.SingleOrDefault(a => a.PeriodId == currentPeriod.Id && a.AccountId == accountId);

            if (accrual == null)
            {
                db.Accruals.Add(new Accrual {
                    AccountId = accountId, PeriodId = currentPeriod.Id, Value = calculation.Total.Value
                });
            }
            else
            {
                accrual.Value = calculation.Total.Value;
            }
        }
        private MeteringDevice CreateDevice(double initialReadings, House house)
        {
            using (var dbContext = new CalculationSystemDbContext())
            {
                var device = new MeteringDevice
                {
                    Id       = house.Id,
                    Readings = initialReadings
                };

                dbContext.MeteringDevices.Add(device);
                dbContext.SaveChanges();

                return(dbContext.MeteringDevices.Include("House").Single(d => d.Id == house.Id));
            }
        }
 private void EditHouse_Clicked(object sender, RoutedEventArgs e)
 {
     if (housingRegistryDataGrid.SelectedIndex > -1)
     {
         EditHouseWindow editWindow    = new EditHouseWindow();
         House           editableHouse = housingRegistryDataGrid.SelectedItem as House;
         editWindow.cbCity.Text            = editableHouse.City;
         editWindow.cbStreet.Text          = editableHouse.Street;
         editWindow.tbHouseNumber.Text     = editableHouse.HouseNumber.ToString();
         editWindow.tbCaseNumber.Text      = editableHouse.CaseNumber.ToString();
         editWindow.tbHeatingStandart.Text = editableHouse.HeatingStandart.ToString();
         var result = editWindow.ShowDialog();
         if (result == false)
         {
             editWindow.Close();
         }
         else
         {
             try
             {
                 using (var dbContext = new CalculationSystemDbContext())
                 {
                     House house = dbContext.Houses.Single(x => x.Id == editableHouse.Id);
                     house.City            = editWindow.cbCity.SelectedItem.ToString();
                     house.Street          = editWindow.cbStreet.SelectedItem.ToString();
                     house.HouseNumber     = int.Parse(editWindow.tbHouseNumber.Text);
                     house.HeatingStandart = double.Parse(editWindow.tbHeatingStandart.Text);
                     if (!String.IsNullOrEmpty(editWindow.tbCaseNumber.Text))
                     {
                         house.CaseNumber = editWindow.tbCaseNumber.Text[0];
                     }
                     else
                     {
                         house.CaseNumber = null;
                     }
                     dbContext.SaveChanges();
                     UpdateOrRefresh();
                     MessageBox.Show("House edited");
                 }
             }
             catch (Exception ex)
             {
                 MessageBox.Show($"Impossible! Reason: -{ex.Message}");
             }
         }
     }
 }
예제 #16
0
        private double GetInitialReadings(Account currAccount)
        {
            using (var db = new CalculationSystemDbContext())
            {
                var initialReadings = db.InitialHouseDeviceReadings
                                      .SingleOrDefault(r => r.PeriodId == currentPeriod.Id && r.HouseId == currAccount.HouseId);

                if (initialReadings == null)
                {
                    return(currAccount.House.GroupMeteringDevice.Readings);
                }
                else
                {
                    return(initialReadings.Readings);
                }
            }
        }
예제 #17
0
        private void btCalculate_Clicked(object sender, RoutedEventArgs e)
        {
            using (var db = new CalculationSystemDbContext())
            {
                Account currAccount = db.Accounts
                                      .Include("House")
                                      .Single(a => a.Id == account.Id);

                foreach (var c in Calculations)
                {
                    c.ServiceQuantity = GetStandard(currAccount) * account.LivingSpace;
                    c.Total           = c.ServiceQuantity * c.ServiceRate;
                    SaveAccrual(account.Id, c, db);
                }

                db.SaveChanges();
            }
        }
        private void SaveInitialReadingForCurrentPeriod(CalculationSystemDbContext dbContext, int houseId, double readings)
        {
            Period currentPeriod = (DataContext as MeteringDeviceRegistryViewModel).OpenedPeriod;

            InitialHouseDeviceReadingInPeriod periodDataForHouse = dbContext
                                                                   .InitialHouseDeviceReadings
                                                                   .SingleOrDefault(r => r.PeriodId == currentPeriod.Id && r.HouseId == houseId);

            if (periodDataForHouse == null)
            {
                dbContext.InitialHouseDeviceReadings.Add(new InitialHouseDeviceReadingInPeriod
                {
                    PeriodId = currentPeriod.Id,
                    HouseId  = houseId,
                    Readings = readings
                });
            }
        }
 private void Delete_Clicked(object sender, RoutedEventArgs e)
 {
     if (housingRegistryDataGrid.SelectedIndex > -1)
     {
         var result = MessageBox.Show("Are you sure?", "Delete this house?", MessageBoxButton.YesNo);
         if (result == MessageBoxResult.Yes)
         {
             using (var dbContext = new CalculationSystemDbContext())
             {
                 House delHouse = housingRegistryDataGrid.SelectedItem as House;
                 dbContext.Houses.Remove(dbContext.Houses.Single(h => h.Id == delHouse.Id));
                 dbContext.SaveChanges();
                 UpdateOrRefresh();
                 MessageBox.Show("Selected house was deleted!");
             }
         }
     }
 }
        private void ButtonFind_Click(object sender, RoutedEventArgs e)                     //Search button
        {
            var dbContext = new CalculationSystemDbContext();

            if (String.IsNullOrEmpty(tbAccountId.Text) && String.IsNullOrEmpty(tbOwner.Text))
            {
                AccountsGrid.ItemsSource = dbContext.Accounts.Include("House").ToList();
            }
            else
            {
                if (String.IsNullOrEmpty(tbOwner.Text))
                {
                    int            accountId = int.Parse(tbAccountId.Text);
                    var            account   = dbContext.Accounts.Find(accountId);
                    List <Account> tbl       = new List <Account>();
                    tbl.Add(account);
                    if (account == null)
                    {
                        AccountsGrid.ItemsSource = new List <Account>();
                        MessageBox.Show("Nothing found");
                    }
                    else
                    {
                        AccountsGrid.ItemsSource = tbl;
                        MessageBox.Show("Search completed succesfully by the Id");
                    }
                }
                if (String.IsNullOrEmpty(tbAccountId.Text))
                {
                    string         owner = tbOwner.Text.ToString();
                    List <Account> tbl   = dbContext.Accounts.Where(x => x.Owner == owner).ToList();
                    if (tbl.Count < 1)
                    {
                        AccountsGrid.ItemsSource = new List <Account>();
                        MessageBox.Show("Nothing found");
                    }
                    else
                    {
                        AccountsGrid.ItemsSource = tbl;
                        MessageBox.Show("Search completed succesfully by the owner");
                    }
                }
            }
        }
        private void SaveAccount(string owner, string space, int houseId, string aptNumber)
        {
            using (var dbContext = new CalculationSystemDbContext())
            {
                Account newAccount = new Account
                {
                    Owner           = owner,
                    LivingSpace     = double.Parse(space),
                    HouseId         = houseId,
                    ApartmentNumber = int.Parse(aptNumber),
                    Services        = new List <Service> {
                        Service.DefaultService()
                    }
                };

                dbContext.Accounts.Add(newAccount);
                dbContext.SaveChanges();
            }
        }
 private void EditAccount_Clicked(object sender, RoutedEventArgs e)
 {
     if (AccountsGrid.SelectedIndex > -1)
     {
         EditAccountWindow newEditAccountWindow = new EditAccountWindow();
         Account           editAccount          = AccountsGrid.SelectedItem as Account;
         newEditAccountWindow.tbOwner.Text           = editAccount.Owner;
         newEditAccountWindow.tbApartmentNumber.Text = editAccount.ApartmentNumber.ToString();
         newEditAccountWindow.tbLivingSpace.Text     = editAccount.LivingSpace.ToString();
         var result = newEditAccountWindow.ShowDialog();
         if (result == false)
         {
             newEditAccountWindow.Close();
         }
         else
         {
             try
             {
                 using (var dbContext = new CalculationSystemDbContext())
                 {
                     Account account = dbContext.Accounts.Single(x => x.Id == editAccount.Id);
                     account.Owner           = newEditAccountWindow.tbOwner.Text;
                     account.ApartmentNumber = int.Parse(newEditAccountWindow.tbApartmentNumber.Text);
                     account.LivingSpace     = double.Parse(newEditAccountWindow.tbLivingSpace.Text);
                     dbContext.SaveChanges();
                     UpdateAccountsTable();
                     MessageBox.Show("Account edited");
                 }
             }
             catch (Exception ex)
             {
                 MessageBox.Show($"Impossible! Reason: -{ex.Message}");
             }
         }
     }
     else
     {
         MessageBox.Show("Select account before");
     }
 }
예제 #23
0
        private IEnumerable <HouseReport> GetReportData(int periodId)
        {
            using (var db = new CalculationSystemDbContext())
            {
                var query = from house in db.Houses
                            join accrual in db.Accruals.Include("Account").Where(a => a.PeriodId == periodId)
                            on house.Id equals accrual.Account.HouseId
                            into accruals
                            where accruals.Any()
                            select new HouseReport
                {
                    House   = house,
                    Entries = accruals.Select(a => new AccountReportEntry
                    {
                        Account  = a.Account,
                        Accruals = a.Value
                    })
                };

                return(query.ToList());
            }
        }
 private void DeleteSelectedAccount_Clicked(object sender, RoutedEventArgs e)
 {
     if (AccountsGrid.SelectedIndex > -1)
     {
         var result = MessageBox.Show("Are you sure?", "Delete this account?", MessageBoxButton.YesNo);
         if (result == MessageBoxResult.Yes)
         {
             try
             {
                 using (var dbContext = new CalculationSystemDbContext())
                 {
                     Account delAccount = AccountsGrid.SelectedItem as Account;
                     dbContext.Accounts.Remove(dbContext.Accounts.Single(h => h.Id == delAccount.Id));
                     dbContext.SaveChanges();
                     MessageBox.Show("Selected account was deleted!");
                 }
             }
             catch (Exception ex)
             {
                 MessageBox.Show($"Impossible? Reason -{ex.Message}");
             }
         }
     }
 }
        private void HouseAddition_Clicked(object sender, RoutedEventArgs e)
        {
            HouseAdditionWindow newHouseWindow = new HouseAdditionWindow();
            var result = newHouseWindow.ShowDialog();

            if (result == false)
            {
                newHouseWindow.Close();
            }
            else
            {
                try
                {
                    var   dbContext = new CalculationSystemDbContext();
                    House house     = new House();
                    house.City            = newHouseWindow.cbCity.SelectedItem.ToString();
                    house.Street          = newHouseWindow.cbStreet.SelectedItem.ToString();
                    house.HouseNumber     = int.Parse(newHouseWindow.tbHouseNumber.Text);
                    house.HeatingStandart = double.Parse(newHouseWindow.tbHeatingStandart.Text);
                    if (!String.IsNullOrEmpty(newHouseWindow.tbCaseNumber.Text))
                    {
                        house.CaseNumber = newHouseWindow.tbCaseNumber.Text[0];
                    }
                    else
                    {
                        house.CaseNumber = null;
                    }
                    dbContext.Houses.Add(house);
                    dbContext.SaveChanges();
                }
                catch (Exception exception)
                {
                    MessageBox.Show($"Impossible! Reason: -{exception.Message}");
                }
            }
        }