Пример #1
0
        public MainViewModel()
        {
            Application.Current.MainWindow.Closing += MainWindow_Closing;
            Application.Current.MainWindow.Closed += MainWindow_Closed;

            try
            {
                _ctx = new UnitOfWork(new IsisContext());
                Workspaces = new ObservableCollection<WorkspaceViewModel>
                {
                    new BerekenModuleViewModel(_ctx),
                    new PrestatieBeheerViewModel(_ctx),
                    new KlantenBeheerViewModel(_ctx),
                    new PersoneelBeheerViewModel(_ctx),
                    new ParameterBeheerViewModel(_ctx)
                };
                SelectedWorkspace = Workspaces.First();
            }
            catch (Exception ex)
            {
                MessageBoxService messageService = new MessageBoxService();
                messageService.ShowMessageBox("ERROR");      //Hack: The first Messagebox closes automatically
                //TODO: Add possiblity to restore database from backup!
                messageService.ShowErrorBox("Er heeft zich een probleem voorgedaan bij het ophalen van de data \n\nError: " + ex.Message);
                logger.Error("Loading Database (startup)", ex);
                Application.Current.Shutdown(-1);
            }
        }
        public void Save()
        {
            if (!DatumViewModel.HasDates)
            {
                MessageBoxService messageService = new MessageBoxService();
                messageService.ShowMessageBox("Je hebt nog geen datum gekozen!");
                return;
            }

            try
            {
                //Add prestatie + dates to DB
                if (ButtonToevoegenContent == "Toevoegen")
                {
                    //Add date(s) to DB
                    Ctx.Datums.AddRange(DatumViewModel.NewDates);
                    //Add prestatie to DB (parameter needs to be last date)
                    CurrentView.Save(DatumViewModel.NewDates.Last().Date);

                    #region Legacy code

                    ////Add Dates to DB
                    //foreach (var d in DatumViewModel.NewDates)
                    //{
                    //    d.Id = CurrentView.AddPrestatie.Id;
                    //    context.Datum.Add(d);

                    //    //EF is retarded and thinks that I readded the Strijkers to the db, while I didn't.
                    //    //I Just use them as foreign relantionschip, I can't just use the id, because I need the name
                    //    //Manually said this is not true
                    //    //https://msdn.microsoft.com/en-us/magazine/dn166926.aspx
                    //    //This link explains it!

                    //    if (d.Strijker1 != null)
                    //        context.Entry(d.Strijker1).State = EntityState.Unchanged;
                    //    if (d.Strijker2 != null)
                    //        context.Entry(d.Strijker2).State = EntityState.Unchanged;
                    //    if (d.Strijker3 != null)
                    //        context.Entry(d.Strijker3).State = EntityState.Unchanged;
                    //    if (d.Strijker4 != null)
                    //        context.Entry(d.Strijker4).State = EntityState.Unchanged;
                    //    if (d.Strijker5 != null)
                    //        context.Entry(d.Strijker5).State = EntityState.Unchanged;
                    //}

                    #endregion
                }
                //Update (edit) last prestatie + dates
                else
                {
                    CurrentView.UpdateLast();

                    ButtonBerekenContent = "Berekenen";
                    ButtonToevoegenContent = "Toevoegen";
                    ButtonChangeContent = "Laatste prestatie aanpassen";
                }

                //Save changes to DB
                Ctx.Complete();
            }
            catch (Exception ex)
            {
                Ctx.DiscardChanges();
                MessageBoxService messageService = new MessageBoxService();
                messageService.ShowErrorBox("Er heeft zich een probleem voorgedaan bij het opslaan van een prestatie \n\nError: " + ex.Message);
            }

            //Re init CurrentView => Make new Prestatie, ...
            CurrentView.Init();
            DatumViewModel.Init();
        }
        public void Save()
        {
            if (ButtonToevoegenContent == "Annuleren")
            {
                Ctx.Strijkers.Add(SelectedPersoneel);
            }

            try
            {
                Ctx.Complete();
            }
            catch (Exception ex)
            {
                MessageBoxService messageService = new MessageBoxService();
                messageService.ShowErrorBox("Er heeft zich een probleem voorgedaan bij het opslaan van de strijksters \n\nError: " + ex.Message);
            }

            ButtonToevoegenContent = "Toevoegen";
        }
        private void SelectedTypePropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            var changed = sender as KlantType;

            try
            {
                Ctx.Complete();
            }
            catch (Exception ex)
            {
                MessageBoxService messageService = new MessageBoxService();
                messageService.ShowErrorBox("Er heeft zich een probleem voorgedaan bij het opslaan van een bestaande 'Klant Type' ("+ changed.Type + " " + changed.Naam + ")\n\nError: " + ex.Message);
            }
        }
        public void Delete()
        {
            try
            {
                Ctx.KlantTypes.Remove(SelectedType);
                Ctx.Complete();
            }
            catch (Exception ex)
            {
                Ctx.DiscardChanges();
                MessageBoxService messageService = new MessageBoxService();
                messageService.ShowErrorBox("Er heeft zich een probleem voorgedaan bij het verwijderen van een bestaande 'Klant Type' (" + SelectedType.Type + " " + SelectedType.Naam + ")\n\nError: " + ex.Message);
            }

            GetData();
        }
        public virtual void Add()
        {
            try
            {
                Ctx.KlantTypes.Add(AddType);
                Ctx.Complete();
            }
            catch (Exception ex)
            {
                Ctx.DiscardChanges();
                MessageBoxService messageService = new MessageBoxService();
                messageService.ShowErrorBox("Er heeft zich een probleem voorgedaan bij het toevoegen van een nieuwe 'Klant Type' (" + AddType.Type + " " + AddType.Naam + ")\n\nError: " + ex.Message);
            }

            GetData();
            AddType = new KlantType();
        }