private void AddMonthConsumptionBtn_Click(object sender, RoutedEventArgs e) { if (MonthCalendar.SelectedDate.HasValue == false) { MessageBox.Show("Выберите рассчётный месяц"); } else if (float.TryParse(MonthConsumptionTB.Text, out float cons) == false) { MessageBox.Show("Введите расход воды числом"); } else if (cons <= 0) { MessageBox.Show("Расход воды должен быть больше нуля"); } else { string str = MonthCalendar.SelectedDate.Value.ToShortDateString(); str = str.Substring(6, 4) + str.Substring(3, 2); ModelSprintDBContainer container = new ModelSprintDBContainer(connString); List <MonthConsumption> collMonth = container.Set <MonthConsumption>().ToList(); MonthConsumption monthConsumption = new MonthConsumption() { Date = Convert.ToInt32(str), Consumption = Convert.ToDecimal(cons) }; if (collMonth.Contains(monthConsumption) == true) { MessageBox.Show("Такой период уже существует"); } else { container.MonthConsumptionSet.Add(monthConsumption); List <CostOfWater> costOfWater = container.Set <CostOfWater>().ToList(); decimal square = 0; foreach (Cottager cottager in container.CottagerSet) { square += cottager.Square; } foreach (Cottager cottager in container.CottagerSet) { Billing bill = new Billing() { MonthConsumption = monthConsumption, Cottager = cottager, Author = cottager.Author, Date = monthConsumption.Date, Bill = Convert.ToDecimal((cottager.Square / square) * monthConsumption.Consumption * costOfWater[0].Cost), }; container.BillingSet.Add(bill); } container.SaveChanges(); Close(); } } }
private void SetMonthBillingAsItemSource(MonthConsumption consumption, Billing billEx) { ModelSprintDBContainer container = new ModelSprintDBContainer(connString); IQueryable <Billing> lst = null; if (billEx == null) { lst = from bill in container.BillingSet where bill.MonthConsumption.MonthBillingID == consumption.MonthBillingID select bill; } else { lst = from bill in container.BillingSet where bill.Date == billEx.Date select bill; } List <Billing> bills = lst.ToList(); if (bills.Count > 0) { ViewDataGrid.ItemsSource = bills; ViewDataGrid.Columns.RemoveAt(0); ViewDataGrid.Columns.RemoveAt(1); ViewDataGrid.Columns.RemoveAt(2); ViewDataGrid.Columns.RemoveAt(2); ViewDataGrid.Columns[0].Header = "ФИО дачника"; ViewDataGrid.Columns[1].Header = "Счёт за месяц"; int date = (billEx == null ? consumption.Date : billEx.MonthConsumption.Date); currentViewTextBlock.Text = "Отчёт за период: " + (date / 100).ToString() + "/" + (date % 100 < 10 ? "0" + (date % 100).ToString() : (date % 100).ToString()); SortByFirstColumn(); } else if (consumption != null) { MessageBox.Show("Отсутствуют дачники для выбранного периода"); } }
private void FillDBForExampleBtn_Click(object sender, RoutedEventArgs e) { ModelSprintDBContainer container = new ModelSprintDBContainer(connString); ClearDB(); //пример CostOfWater costOfWater = new CostOfWater() { Cost = 1.57M }; container.CostOfWaterSet.Add(costOfWater); container.CottagerSet.Add(new Cottager() { Author = "Шубин Семён Николаевич", Square = 12 }); container.CottagerSet.Add(new Cottager() { Author = "Иванов Пётр Михайлович", Square = 15 }); container.CottagerSet.Add(new Cottager() { Author = "Сидоров Александр Анатольевич", Square = 20 }); container.CottagerSet.Add(new Cottager() { Author = "Машинцев Илья Константинович", Square = 9.5M }); var cottagers = from item in container.CottagerSet.Local select item; MonthConsumption monthConsumption = new MonthConsumption() { Consumption = 300, Date = 201804 }; decimal square = 0; foreach (Cottager cottager in cottagers) { square += cottager.Square; } foreach (Cottager cottager in cottagers) { Billing bill = new Billing() { MonthConsumption = monthConsumption, Cottager = cottager, Author = cottager.Author, Date = monthConsumption.Date, Bill = Convert.ToDecimal((cottager.Square / square) * monthConsumption.Consumption * costOfWater.Cost), }; container.BillingSet.Add(bill); } monthConsumption = new MonthConsumption() { Consumption = 470.5M, Date = 201803 }; foreach (Cottager cottager in cottagers) { Billing bill = new Billing() { MonthConsumption = monthConsumption, Cottager = cottager, Author = cottager.Author, Date = monthConsumption.Date, Bill = Convert.ToDecimal((cottager.Square / square) * monthConsumption.Consumption * costOfWater.Cost), }; container.BillingSet.Add(bill); } container.SaveChanges(); SetCottagersAsItemsSource(); }