public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { DETAILS_TBL detail = (DETAILS_TBL)value; return((detail.HOURS - this.Context.SUBDETAILS_TBL.ToList().Where(p => p.DETAIL_ID == detail.DETAIL_ID).DefaultIfEmpty(new SUBDETAILS_TBL() { HOURS = (decimal)0.0 }).Select(x => x.HOURS).Sum()).ToString()); }
public WorkLoadEdit() { InitializeComponent(); this.Tab = null; this.Window = null; //this.SubdetailsGrid.Items.Clear(); this.MainParametersChoose = new MainComplexEdit.MainParametersChoose(); this.ParametersFrame.Content = this.MainParametersChoose; Action <Type, System.Windows.Controls.ItemsControl> UpdateAssing = ((App)Application.Current).AssignRefresh; SelectionChangedEventHandler selectionChanged = new SelectionChangedEventHandler((object obj, SelectionChangedEventArgs args) => this.PreparePlan()); this.MainParametersChoose.EduFormsList.SelectionChanged += selectionChanged; this.MainParametersChoose.EduTypesList.SelectionChanged += selectionChanged; this.MainParametersChoose.CourseChoose.SelectionChanged += selectionChanged; this.MainParametersChoose.SemesterChoose.SelectionChanged += selectionChanged; try { this.MainContext.MAIN_TBL.Local.CollectionChanged += new System.Collections.Specialized.NotifyCollectionChangedEventHandler((object obj, NotifyCollectionChangedEventArgs args) => this.PreparePlan()); this.MainContext.MAIN_TBL.Load(); } catch (Exception ex) { MessageBox.Show("Unable to load plans.\nError: " + ex.Message + ".\n" + ex.StackTrace); } this.MainsGrid.SelectionChanged += new SelectionChangedEventHandler((object obj, SelectionChangedEventArgs arg) => { this.UpdateDetails(); this.MainsGrid.Focus(); }); this.MainContext.DETAILS_TBL.Local.CollectionChanged += new NotifyCollectionChangedEventHandler((object obj, NotifyCollectionChangedEventArgs args) => { DETAILS_TBL detail = (DETAILS_TBL)this.DetailsGrid.SelectedItem; this.UpdateDetails(); if (detail != null) { this.DetailsGrid.SelectedItem = this.DetailsGrid.Items.OfType <DETAILS_TBL>().FirstOrDefault(p => p.DETAIL_ID == detail.DETAIL_ID); } }); try { this.MainContext.DETAILS_TBL.Load(); } catch (Exception ex) { MessageBox.Show("Unable to load plans details.\nError: " + ex.Message + "\n" + ex.StackTrace); } this.MainContext.SUBDETAILS_TBL.Local.CollectionChanged += new NotifyCollectionChangedEventHandler((object obj, NotifyCollectionChangedEventArgs args) => { DETAILS_TBL detail = (DETAILS_TBL)this.DetailsGrid.SelectedItem; if (detail != null) { if (args.Action == NotifyCollectionChangedAction.Reset) { try { this.SubDetailCol.Clear(); } catch { } } switch (args.Action) { case NotifyCollectionChangedAction.Reset: case NotifyCollectionChangedAction.Replace: case NotifyCollectionChangedAction.Add: foreach (SUBDETAILS_TBL subdetail in args.NewItems) { if (subdetail.DETAIL_ID == detail.DETAIL_ID && !this.SubDetailCol.Any(p => p.SUBDETAIL_ID == subdetail.SUBDETAIL_ID)) { this.SubDetailCol.Add(this.MainContext.SUBDETAILS_TBL.Where(p => p.SUBDETAIL_ID == subdetail.SUBDETAIL_ID).AsNoTracking().FirstOrDefault()); } } this.SubdetailsGrid.IsEnabled = true; this.UserAbleAdd(); break; case NotifyCollectionChangedAction.Remove: foreach (SUBDETAILS_TBL subdetail in args.OldItems) { if (subdetail.DETAIL_ID == detail.DETAIL_ID && !this.SubDetailCol.Any(p => p.SUBDETAIL_ID == subdetail.SUBDETAIL_ID)) { SUBDETAILS_TBL s = this.SubDetailCol.FirstOrDefault(p => p.SUBDETAIL_ID == subdetail.SUBDETAIL_ID); if (s != null) { this.SubDetailCol.Remove(s); } } } break; } } else { this.SubdetailsGrid.IsEnabled = false; } }); this.DetailsGrid.SelectionChanged += new SelectionChangedEventHandler((object obj, SelectionChangedEventArgs arg) => { try { DETAILS_TBL detail = (DETAILS_TBL)this.DetailsGrid.SelectedItem; try { this.SubDetailCol.Clear(); } catch { } if (detail != null) { foreach (SUBDETAILS_TBL subdetail in this.MainContext.SUBDETAILS_TBL.Where(p => p.DETAIL_ID == detail.DETAIL_ID).AsNoTracking()) { this.SubDetailCol.Add(subdetail); } this.SubdetailsGrid.IsEnabled = true; } else { this.SubdetailsGrid.IsEnabled = false; } this.UserAbleAdd(); this.DetailsGrid.Focus(); } catch (Exception ex) { MessageBox.Show("Unable to load loads details while switchiing selection of plan details.\nError: " + ex.Message + "\n" + ex.StackTrace); } }); this.MainContext.SUBDETAILS_TBL.Load(); this.SubDetailCol = new ObservableCollection <SUBDETAILS_TBL>(); this.SubDetailCol.CollectionChanged += new NotifyCollectionChangedEventHandler((object sender, NotifyCollectionChangedEventArgs args) => { if (args.Action == NotifyCollectionChangedAction.Remove) { foreach (SUBDETAILS_TBL subdetail in args.OldItems) { try { if (((DETAILS_TBL)this.DetailsGrid.SelectedItem).SUBDETAILS_TBL.Any(p => p.SUBDETAIL_ID == subdetail.SUBDETAIL_ID)) { using (Entities context = new Entities()) { SUBDETAILS_TBL _subdetail = context.SUBDETAILS_TBL.Find(subdetail.SUBDETAIL_ID); if (_subdetail != null) { context.SUBDETAILS_TBL.Remove(_subdetail); SUBDETAILS_TBL local = this.MainContext.SUBDETAILS_TBL.Local.FirstOrDefault(p => p.SUBDETAIL_ID == subdetail.SUBDETAIL_ID); if (local != null) { this.MainContext.SUBDETAILS_TBL.Local.Remove(local); } context.SaveChanges(); context.Database.Connection.Open(); DbCommand command = context.Database.Connection.CreateCommand(); command.CommandText = "DELETE FROM GPRELATIONS_TBL WHERE SUBDETAIL_ID=" + _subdetail.SUBDETAIL_ID.ToString(); command.ExecuteNonQuery(); context.Database.Connection.Close(); this.MainContext.SUBDETAILS_TBL.Load(); } } } } catch (Exception ex) { MessageBox.Show("Unable to delete load detail.\nError: " + ex.Message + "\n" + ex.StackTrace); } } } this.UserAbleAdd(); this.DetailsGrid.Items.Refresh(); }); this.SubdetailsGrid.Items.Clear(); this.SubdetailsGrid.ItemsSource = this.SubDetailCol; UpdateAssing(typeof(MAIN_TBL), this.MainsGrid); UpdateAssing(typeof(SUBDETAILS_TBL), this.SubdetailsGrid); UpdateAssing(typeof(DETAILS_TBL), this.DetailsGrid); UpdateAssing(typeof(TEACHERS_TBL), this.AvaliebleTutors); UpdateAssing(typeof(SUBDETAILS_TBL), this.AvaliebleTutors); RoutedEventHandler HourCellLostFocusHandler = new RoutedEventHandler((object sender, RoutedEventArgs args) => this.SubdetailsGrid.Items.Refresh()); this.SubdetailsGrid.CellEditEnding += new EventHandler <DataGridCellEditEndingEventArgs>((object sender, DataGridCellEditEndingEventArgs args) => { Validation.ClearInvalid(((TextBox)args.EditingElement).GetBindingExpression(TextBox.TextProperty)); if (args.EditAction == DataGridEditAction.Cancel) { return; } try { SUBDETAILS_TBL subdetail = (SUBDETAILS_TBL)args.Row.DataContext; if (args.Column == this.SubHourCol && this.DetailsGrid.SelectedItem != null) { decimal detailHours = ((DETAILS_TBL)DetailsGrid.SelectedItem).HOURS, newHours = (decimal)0.0; System.String error = System.String.Empty; if (!decimal.TryParse(((TextBox)args.EditingElement).Text ?? System.String.Empty, out newHours)) { error = "На жаль, неможливо розпізнати введений текст як число.\nБудь ласка, переконайтеся що введені вами дані не містять постороніх символів (усе окрім цифр та коми)."; } else if (newHours <= (decimal)0.0) { error = "На жаль, кількість годин не може бути від'ємною."; } else if (newHours > (decimal)999.99) { error = "На жаль, кількість годин не може бути більше ніж 999,99."; } else if (detailHours < newHours + this.SubDetailCol.DefaultIfEmpty(new SUBDETAILS_TBL()).Select(p => p.HOURS).Sum() - (this.SubdetailsGrid.SelectedItem != null && (this.SubdetailsGrid.SelectedItem ?? new object()).GetType().Name.Contains("SUBDETAILS_TBL") ? ((SUBDETAILS_TBL)this.SubdetailsGrid.SelectedItem).HOURS : (decimal)0.0)) { error = "На жаль така кількість годин наразі недоступна."; } else { subdetail.HOURS = newHours; this.UpdateLoad(); this.SubdetailsGrid.ItemsSource = null; this.SubdetailsGrid.ItemsSource = this.SubDetailCol; this.UserAbleAdd(); } if (error != System.String.Empty) { ValidationError validationError = new ValidationError(new DataErrorValidationRule(), ((TextBox)args.EditingElement).GetBindingExpression(TextBox.TextProperty)); validationError.ErrorContent = error; Validation.MarkInvalid(((TextBox)args.EditingElement).GetBindingExpression(TextBox.TextProperty), validationError); args.Cancel = true; } } } catch (Exception ex) { MessageBox.Show("Unable to edit the record.\nError: " + ex.Message + ".\n" + ex.StackTrace); } }); this.SubdetailsGrid.InitializingNewItem += new InitializingNewItemEventHandler((object sender, InitializingNewItemEventArgs args) => ((SUBDETAILS_TBL)args.NewItem).DETAIL_ID = ((DETAILS_TBL)this.DetailsGrid.SelectedItem).DETAIL_ID); this.GroupSelectPage = new LoadComplexEdit.GroupSelect(); this.GroupSelectFrame.Content = this.GroupSelectPage; //this.GroupSelectPage.CloseBut.Click += new RoutedEventHandler((object obj, RoutedEventArgs args) => this.GroupSelectPopup.IsOpen = false); SubdetailsGrid.SelectionChanged += new SelectionChangedEventHandler((object sender, SelectionChangedEventArgs args) => { if (this.SubdetailsGrid.SelectedItem == null) { this.AvaliebleTutors.IsEnabled = false; this.GroupSelectFrame.IsEnabled = false; } else { try { this.AvaliebleTutors.IsEnabled = true; this.GroupSelectFrame.IsEnabled = true; this.SkipUpdateLoad = true; SUBDETAILS_TBL subdetail = this.SubdetailsGrid.SelectedItem.GetType().Name.Contains("SUBDETAILS_TBL") ? ((SUBDETAILS_TBL)this.SubdetailsGrid.SelectedItem) : new SUBDETAILS_TBL() { DETAIL_ID = ((DETAILS_TBL)this.DetailsGrid.SelectedItem).DETAIL_ID, TEACHER_ID = 0 }; this.AvaliebleTutors.ItemsSource = this.MainContext.TEACHERS_TBL.ToList() .Where(p => (this.SubdetailsGrid.SelectedItem.GetType().Name.Contains("SUBDETAILS_TBL") ? ((SUBDETAILS_TBL)this.SubdetailsGrid.SelectedItem).TEACHER_ID == p.TEACHER_ID : false) || !this.SubDetailCol.Any(g => g.TEACHER_ID == p.TEACHER_ID)); this.GroupSelectPage.AvGroups = this.MainContext.GROUPS_TBL.ToList().Where(p => p.COURSE_NO == ((DETAILS_TBL)this.DetailsGrid.SelectedItem).MAIN_TBL.COURSE_NO && p.EDUFORM_ID == ((DETAILS_TBL)this.DetailsGrid.SelectedItem).MAIN_TBL.EDUFORM_ID) .ToList(); this.GroupSelectPage.SubDetail = subdetail; this.AvaliebleTutors.SelectedItem = this.AvaliebleTutors.Items.OfType <TEACHERS_TBL>().FirstOrDefault(p => this.SubdetailsGrid.SelectedItem.GetType().Name.Contains("SUBDETAILS_TBL") ? ((SUBDETAILS_TBL)this.SubdetailsGrid.SelectedItem).TEACHER_ID == p.TEACHER_ID : false); this.SkipUpdateLoad = false; } catch (Exception ex) { MessageBox.Show("Unable to fill edit fields while switching selection of load plan.\nError: " + ex.Message + ".\n" + ex.StackTrace); } this.SubdetailsGrid.Focus(); } }); this.AvaliebleTutors.SelectionChanged += new SelectionChangedEventHandler((object sender, SelectionChangedEventArgs args) => { try { bool goToNextRow = !(this.SubdetailsGrid.SelectedItem?.GetType() ?? typeof(SUBDETAILS_TBL)).Name.Contains("SUBDETAILS_TBL") && this.SubdetailsGrid.SelectedItem != null && this.AvaliebleTutors.SelectedItem != null; if (this.AvaliebleTutors.SelectedItem != null) { this.AddSubdetail(new Action <SUBDETAILS_TBL>((SUBDETAILS_TBL subdetail) => { subdetail.TEACHERS_TBL = (TEACHERS_TBL)this.AvaliebleTutors.SelectedItem; subdetail.TEACHER_ID = ((TEACHERS_TBL)this.AvaliebleTutors.SelectedItem).TEACHER_ID; })); } if ((this.SubdetailsGrid.SelectedItem?.GetType() ?? typeof(object)).Name.Contains("SUBDETAILS_TBL") && this.SubdetailsGrid.SelectedItem != null && this.AvaliebleTutors.SelectedItem != null) { ((SUBDETAILS_TBL)this.SubdetailsGrid.SelectedItem).TEACHERS_TBL = (TEACHERS_TBL)this.AvaliebleTutors.SelectedItem; ((SUBDETAILS_TBL)this.SubdetailsGrid.SelectedItem).TEACHER_ID = ((TEACHERS_TBL)this.AvaliebleTutors.SelectedItem).TEACHER_ID; } this.SubdetailsGrid.Items.Refresh(); this.UpdateLoad(); this.AvaliebleTutors.Items.Refresh(); if (goToNextRow && this.SubdetailsGrid.CanUserAddRows) { this.SubdetailsGrid.SelectedIndex = this.SubdetailsGrid.Items.Count - 1; this.SubdetailsGrid.Focus(); } else { this.AvaliebleTutors.Focus(); } } catch (Exception ex) { MessageBox.Show("Unable to change selection of tutor.\nError: " + ex.Message + ".\n" + ex.StackTrace); } }); this.GroupSelectPage.GroupsInColl.CollectionChanged += new NotifyCollectionChangedEventHandler((object sender, NotifyCollectionChangedEventArgs args) => { if (args.Action == NotifyCollectionChangedAction.Add || args.Action == NotifyCollectionChangedAction.Remove) { try { if (this.GroupSelectPage.GroupsInColl.Count > 0) { this.AddSubdetail(new Action <SUBDETAILS_TBL>((SUBDETAILS_TBL subdetail) => { foreach (GROUPS_TBL group in args.NewItems) { subdetail.GROUPS_TBL.Add(group); } })); } } catch (Exception ex) { MessageBox.Show("Unable to update group affiliating.\nError: " + ex.Message + ".\n" + ex.StackTrace); } this.SubdetailsGrid.Items.Refresh(); this.UpdateLoad(); } }); this.MainContext.TEACHERS_TBL.Local.CollectionChanged += new NotifyCollectionChangedEventHandler((object sender, NotifyCollectionChangedEventArgs args) => { try { this.SubdetailsGrid.Items.Refresh(); if (this.AvaliebleTutors.IsEnabled && this.SubdetailsGrid.SelectedItem != null) { this.AvaliebleTutors.ItemsSource = this.MainContext.TEACHERS_TBL.ToList() .Where(p => (this.SubdetailsGrid.SelectedItem.GetType().Name.Contains("SUBDETAILS_TBL") ? ((SUBDETAILS_TBL)this.SubdetailsGrid.SelectedItem).TEACHER_ID == p.TEACHER_ID : false) || !this.SubDetailCol.Any(g => g.TEACHER_ID == p.TEACHER_ID)); this.AvaliebleTutors.Items.Refresh(); } } catch (Exception ex) { MessageBox.Show("Неможливо оновити список доступних викладачів.\nПричина:\n" + ex.Message + "\n" + ex.StackTrace); } }); this.Loaded += new RoutedEventHandler((object sender, RoutedEventArgs args) => PreparePlan()); }