private void LoadDataGridViewService(IReadOnlyCollection <Service> servicesList = null) { using (var db = new ModelsContext()) { dataGridViewService.DataSource = servicesList ?? db.Services.ToList(); } using (var db = new ModelsContext()) { var services = db.Services.ToList(); services.Insert(0, new Service { Access = "", Name = "Все", Description = "", Price = 0, TypeOfService = TypeOfService.Аренда }); comboBoxServiceName.DataSource = services; comboBoxServiceName.DisplayMember = "Name"; comboBoxServiceName.ValueMember = "Id"; } dataGridViewService.Columns[0].Visible = false; dataGridViewService.Columns[1].Width = 200; dataGridViewService.Columns[1].HeaderText = "Название"; dataGridViewService.Columns[2].Width = 120; dataGridViewService.Columns[2].HeaderText = "Тип"; dataGridViewService.Columns[3].Width = 70; dataGridViewService.Columns[3].HeaderText = "Цена"; dataGridViewService.Columns[4].Width = 180; dataGridViewService.Columns[4].HeaderText = "Описание"; dataGridViewService.Columns[5].Width = 220; dataGridViewService.Columns[5].HeaderText = "Доступность"; }
private void buttonServiceFilterApply_Click(object sender, EventArgs e) { List <Service> accountingsList = null; if (!string.IsNullOrEmpty(textBoxServicePriceFrom.Text) && !string.IsNullOrEmpty(textBoxServicePriceTo.Text) && double.Parse(textBoxServicePriceFrom.Text) > double.Parse(textBoxServicePriceTo.Text)) { MessageBox.Show("Начальная сумма должна быть меньше конечной."); return; } using (var db = new ModelsContext()) { IQueryable <Service> dbAccountings = db.Services; CorrectionFilterPrice(textBoxServicePriceFrom); CorrectionFilterPrice(textBoxServicePriceTo); if (!string.IsNullOrEmpty(textBoxName.Text)) { dbAccountings = dbAccountings.Where(x => x.Name.Contains(textBoxName.Text)); } if (!comboBoxServiceType.Text.Contains("Все")) { var valueTypeClientFilter = (TypeOfService)Enum.Parse(typeof(TypeOfService), comboBoxServiceType.Text); dbAccountings = dbAccountings.Where(x => x.TypeOfService == valueTypeClientFilter); } if (!comboBoxServiceAccess.Text.Contains("Все")) { var valueServiceNameFilter = comboBoxServiceAccess.Text; dbAccountings = dbAccountings.Where(x => x.Access.Contains(valueServiceNameFilter)); } if (!string.IsNullOrEmpty(textBoxServicePriceFrom.Text)) { var priceFrom = double.Parse(textBoxServicePriceFrom.Text); dbAccountings = dbAccountings.Where(x => x.Price >= priceFrom); } if (!string.IsNullOrEmpty(textBoxServicePriceTo.Text)) { var priceTo = double.Parse(textBoxServicePriceTo.Text); dbAccountings = dbAccountings.Where(x => x.Price <= priceTo); } if (!string.IsNullOrEmpty(textBoxServiceDescription.Text)) { dbAccountings = dbAccountings.Where(x => x.Description.Contains(textBoxServiceDescription.Text)); } accountingsList = dbAccountings.ToList(); } if (accountingsList.ToList().Count == 0) { MessageBox.Show("Результаты не надены."); return; } LoadDataGridViewService(accountingsList); }
private void LoadDataGridViewAccounting(IReadOnlyCollection <Accounting> accountingsList = null) { dataGridViewAccounting.Rows.Clear(); dataGridViewAccounting.Columns.Clear(); for (var x = 0; x < AmountOfColumns; x++) { var column = new DataGridViewTextBoxColumn(); dataGridViewAccounting.Columns.Add(column); } using (var db = new ModelsContext()) { if (accountingsList == null) { db.Clients.ToList(); db.Services.ToList(); foreach (var accounting in db.Accountings) { Payment payment; using (var _db = new ModelsContext()) { payment = _db.Payments.First(x => x.Id == accounting.PaymentId); } var paid = payment.Paid ? "Оплачено" : "Без оплаты"; double amountCost = 0; using (var _db = new ModelsContext()) { var list = _db.Attachments.Where(x => x.PaymentId == accounting.PaymentId).ToList(); foreach (var attachment in list) { amountCost += attachment.MadeBy; } } if (amountCost > 0 && amountCost < accounting.Service.Price) { paid = "Частичная"; } dataGridViewAccounting.Rows.Add(accounting.Id, accounting.Client.FullName, accounting.Client.TypeClient, accounting.Service.Name, accounting.Service.Price, accounting.Date.ToShortDateString(), paid); } } else { foreach (var accounting in accountingsList) { var payment = db.Payments.First(x => x.Id == accounting.PaymentId); var paid = payment.Paid ? "Оплачено" : "Без оплаты"; dataGridViewAccounting.Rows.Add(accounting.Id, accounting.Client.FullName, accounting.Client.TypeClient, accounting.Service.Name, accounting.Service.Price, accounting.Date.ToShortDateString(), paid); } } } dataGridViewAccounting.Columns[0].Visible = false; dataGridViewAccounting.Columns[1].Width = 200; dataGridViewAccounting.Columns[1].HeaderText = "ФИО клиента"; dataGridViewAccounting.Columns[2].Width = 120; dataGridViewAccounting.Columns[2].HeaderText = "Тип клиента"; dataGridViewAccounting.Columns[3].Width = 170; dataGridViewAccounting.Columns[3].HeaderText = "Название услуги"; dataGridViewAccounting.Columns[4].Width = 80; dataGridViewAccounting.Columns[4].HeaderText = "Цена услуги"; dataGridViewAccounting.Columns[5].Width = 120; dataGridViewAccounting.Columns[5].HeaderText = "Дата"; dataGridViewAccounting.Columns[5].Width = 120; dataGridViewAccounting.Columns[5].HeaderText = "Статус оплаты"; }
private void buttonApplyAccountingFilter_Click(object sender, EventArgs e) { List <Accounting> accountingsList = null; if (!string.IsNullOrEmpty(textBoxServicePriceFrom.Text) && !string.IsNullOrEmpty(textBoxServicePriceTo.Text) && double.Parse(textBoxServicePriceFrom.Text) > double.Parse(textBoxServicePriceTo.Text)) { MessageBox.Show("Начальная сумма должна быть меньше конечной."); return; } dateTimePickerDateFrom.Value = new DateTime(dateTimePickerDateFrom.Value.Year, dateTimePickerDateFrom.Value.Month, dateTimePickerDateFrom.Value.Day); if (checkBoxFilterDate.Checked && dateTimePickerDateFrom.Value > dateTimePickerDateTo.Value) { MessageBox.Show("Начальная дата должна быть меньше конечной."); return; } using (var db = new ModelsContext()) { IQueryable <Accounting> dbAccountings = db.Accountings; db.Clients.ToList(); db.Services.ToList(); CorrectionFilterPrice(textBoxPriceFrom); CorrectionFilterPrice(textBoxPriceTo); if (!string.IsNullOrEmpty(textBoxClientFullName.Text)) { dbAccountings = dbAccountings.Where(x => x.Client.FullName.Contains(textBoxClientFullName.Text)); } if (!comboBoxTypeClient.Text.Contains("Все")) { var valueTypeClientFilter = (TypeClient)Enum.Parse(typeof(TypeClient), comboBoxTypeClient.Text); dbAccountings = dbAccountings.Where(x => x.Client.TypeClient == valueTypeClientFilter); } if (!comboBoxServiceName.Text.Contains("Все")) { var valueServiceNameFilter = comboBoxServiceName.Text; dbAccountings = dbAccountings.Where(x => x.Service.Name == valueServiceNameFilter); } if (!string.IsNullOrEmpty(textBoxPriceFrom.Text)) { var priceFrom = double.Parse(textBoxPriceFrom.Text); dbAccountings = dbAccountings.Where(x => x.Service.Price >= priceFrom); } if (!string.IsNullOrEmpty(textBoxPriceTo.Text)) { var priceTo = double.Parse(textBoxPriceTo.Text); dbAccountings = dbAccountings.Where(x => x.Service.Price <= priceTo); } if (checkBoxFilterDate.Checked) { var dateFrom = DateTime.Parse(dateTimePickerDateFrom.Value.ToShortDateString()); var dateTo = DateTime.Parse(dateTimePickerDateTo.Value.AddDays(1).ToShortDateString()); dbAccountings = dbAccountings.Where(x => x.Date >= dateFrom && x.Date <= dateTo); } accountingsList = dbAccountings.ToList(); } if (accountingsList.ToList().Count == 0) { MessageBox.Show("Результаты не надены."); return; } LoadDataGridViewAccounting(accountingsList); }
private void editToolStripMenuItem_Click(object sender, EventArgs e) { object classObject = null; switch (tabControl.SelectedIndex) { case 1: try { classObject = new Service { Id = Convert.ToInt32(dataGridViewService.CurrentRow.Cells[0].Value.ToString()), Access = dataGridViewService.CurrentRow.Cells[5].Value.ToString(), Description = dataGridViewService.CurrentRow.Cells[4].Value.ToString(), Price = Convert.ToDouble(dataGridViewService.CurrentRow.Cells[3].Value.ToString()), TypeOfService = (TypeOfService)Enum.Parse(typeof(TypeOfService), dataGridViewService.CurrentRow.Cells[2].Value.ToString()), Name = dataGridViewService.CurrentRow.Cells[1].Value.ToString() }; } catch (NullReferenceException) { MessageBox.Show("Не выбрана запись для редактирования"); return; } break; case 0: using (var db = new ModelsContext()) { try { var currentId = Convert.ToInt32(dataGridViewAccounting.CurrentRow.Cells[0].Value.ToString()); var current = db.Accountings.First(x => currentId == x.Id); var currentFullName = dataGridViewAccounting.CurrentRow.Cells[1].Value.ToString(); var currentTypeClient = (TypeClient)Enum.Parse(typeof(TypeClient), dataGridViewAccounting.CurrentRow.Cells[2].Value.ToString()); var accountingClient = db.Clients.First(x => x.FullName == currentFullName && x.TypeClient == currentTypeClient); var currentServiceName = dataGridViewAccounting.CurrentRow.Cells[3].Value.ToString(); var currentServicePrice = double.Parse(dataGridViewAccounting.CurrentRow.Cells[4].Value.ToString()); var accountingService = db.Services.First(x => x.Name == currentServiceName && x.Price == currentServicePrice); var payment = db.Payments.First(x => x.Id == current.PaymentId); classObject = new Accounting { Id = currentId, Client = accountingClient, ClientId = accountingClient.Id, Date = DateTime.Parse(dataGridViewAccounting.CurrentRow.Cells[5].Value.ToString()), Service = accountingService, ServiceId = accountingService.Id, PaymentId = payment.Id }; } catch (NullReferenceException) { MessageBox.Show("Не выбрана запись для редактирования"); return; } } break; } var operationForm = new OperationForm(tabControl.SelectedIndex, TypeOfTransaction.едактирование, classObject); operationForm.ShowDialog(this); switch (tabControl.SelectedIndex) { case 0: LoadDataGridViewAccounting(); break; case 1: LoadDataGridViewService(); LoadDataGridViewAccounting(); break; } }
private void removeToolStripMenuItem_Click(object sender, System.EventArgs e) { switch (tabControl.SelectedIndex) { case 0: try { if (MessageBox.Show( string.Format("Действительно удалить запись: {0} - {1} ({2})", dataGridViewAccounting.CurrentRow.Cells[3].Value, dataGridViewAccounting.CurrentRow.Cells[1].Value, dataGridViewAccounting.CurrentRow.Cells[5].Value), "Подтвердите действие", MessageBoxButtons.OKCancel) == DialogResult.OK) { using (var db = new ModelsContext()) { var currentAccountingId = Convert.ToInt32(dataGridViewAccounting.CurrentRow.Cells[0].Value.ToString()); var currentAccounting = db.Accountings.FirstOrDefault(x => x.Id == currentAccountingId); if (currentAccounting != null) { db.Accountings.Remove(currentAccounting); db.SaveChanges(); LoadDataGridViewAccounting(); return; } MessageBox.Show("Удаление невозможно в силу непредвиденных обстоятельств."); } } } catch (NullReferenceException) { MessageBox.Show("Не выбрана запись для удаления."); } break; case 1: try { if (MessageBox.Show( string.Format("Действительно удалить запись: {0} - {1} ({2})", dataGridViewService.CurrentRow.Cells[1].Value, dataGridViewService.CurrentRow.Cells[3].Value, dataGridViewService.CurrentRow.Cells[5].Value), "Подтвердите действие", MessageBoxButtons.OKCancel) == DialogResult.OK) { using (var db = new ModelsContext()) { var currentSeviceId = Convert.ToInt32(dataGridViewService.CurrentRow.Cells[0].Value.ToString()); var currentService = db.Services.FirstOrDefault(x => x.Id == currentSeviceId); var usesAccounting = db.Accountings.FirstOrDefault(x => x.ServiceId == currentService.Id); if (usesAccounting != null) { MessageBox.Show("Удаление невозможно. Услуга находится в списке учета."); return; } if (currentService != null) { db.Services.Remove(currentService); db.SaveChanges(); LoadDataGridViewService(); LoadDataGridViewAccounting(); return; } MessageBox.Show("Удаление невозможно в силу непредвиденных обстоятельств."); } } } catch (NullReferenceException) { MessageBox.Show("Не выбрана запись для удаления."); } break; } }