using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Windows.Threading;

using BeWo.Core;
using BeWo.ServiceProxy;
using BeWo.Validation;
using BeWo.ViewModel;

using BS.Shared;
using BS.Shared.DataContracts;
using BS.Shared.DataContracts.Compact;
using BS.Shared.Services;
using BS.Shared.Core;

using Microsoft.Win32;
using BeWo.Core.Service;
using BeWo.ViewModel.ListViewModel;
using System.Collections.ObjectModel;
using BeWo;
using BeWo.View.Detail;


namespace BeWo.View.Detail
{
    public partial class AccountingTransactionBookingView2
    {
        public static Visibility InsertedOnAndByVisibility
        {
            get
            {
                return BeWoApp.AppSettings.HideServiceRecordInsertedByAndInsertedOn ? Visibility.Collapsed : Visibility.Visible;
            }
        }

        private List<FlatSupportConceptTreeNodeDC> _FlatSupportConceptItems;

        private AccountingBookingVM _ViewModel;

        public List<FlatSupportConceptTreeNodeDC> FlatSupportConceptItems
        {
            get { return _FlatSupportConceptItems; }
            set { _FlatSupportConceptItems = value; }
        }

        public AccountingTransactionBookingView2(AccountingBookingVM pViewModel)
        {
            InitializeComponent();
            InitDateCombos();

            ViewModel = pViewModel;
          
            srGridControl.Visibility = Visibility.Visible;

            DateTime d = DateTime.Now;

            combobox_year.SelectedItem = d.Year;
        }

        internal AccountingBookingVM ViewModel
        {
            get
            {
                return _ViewModel;
            }

            set
            {
                _ViewModel = value;
            }
        }

      
 

        private void ReloadViewModel()
        {
            ViewModel.PrototypeVM.Amount = null;
            ViewModel.PrototypeVM.Notice = string.Empty;
        }

   

        private void InitDateCombos()
        {
    
            var years = new List<int>();
            for (int i = 0; i <= 10; i++)
            {
                years.Add(DateTime.Now.Year - i);
            }

            this.combobox_year.ItemsSource = years;

            InitDayCombos();
        }

        private void InitDayCombos()
        {
            int year = DateTime.Now.Year;

            var yearObj = this.combobox_year.SelectedItem;
            if (yearObj != null)
                year = Int32.Parse(yearObj.ToString());

        }


        private void LoadServiceKlient(int year) { 
            
            DateTimeSpan span = new DateTimeSpan();
            span.StartDate = new DateTime(year, 1, 1);
            span.EndDate = new DateTime(year + 1, 1, 1).AddTicks(-1);

            ServiceFacade.DoOperationsServiceAsync(s => s.GetAccountingTransactions(span, null, null), 
                atlist => 
                    {
                        Cache.GetInstance().GetActiveSupportConceptCostbearer(cb => UpdateGrid(atlist, cb,year));
                    });
        }


        public ObservableCollection<BuchungsJahr> Buchungsjahresliste { 
        get {
            return buchungsjahresliste ?? (buchungsjahresliste = new ObservableCollection<BuchungsJahr>());
        }
            set {
                buchungsjahresliste = value;
            }
        }


        private ObservableCollection<BuchungsJahr> buchungsjahresliste;


        private void UpdateGrid(List<AccountingTransactionDC> atAllList, List<CompactSupportConceptDC> scList, int year)
        {
            Dispatcher.BeginInvoke(
               DispatcherPriority.Normal,
               (Action)delegate
               {
                   buchungsjahresliste = new ObservableCollection<BuchungsJahr>();
 
                foreach (var sc in scList)
                {
                    var bj = new BuchungsJahr();
                    bj.alleVorhandenenDC = atAllList;
                   
                    String Name = sc.FirstName + " " + sc.LastName + "  " + sc.CostBearer;

                    int Sjahr;
                    int Smonat;
                    int Emonat;
                    int Ejahr;

                    if (sc.EndDate == null && sc.StartDate == null)
                    {
                         Sjahr = 0;
                         Smonat = 0;
                         Emonat = 0;
                         Ejahr = 0; 
                    }
                    else
                    {
                         Smonat = sc.StartDate.Value.Date.Month;
                         Sjahr = sc.StartDate.Value.Date.Year;
                         Emonat = sc.EndDate.Value.Date.Month;
                         Ejahr = sc.EndDate.Value.Date.Year;
                    }

                    //#################### Sortieren der Kostenträger für das gewählte Jahr  ######################### 
                    if (Sjahr == year || Sjahr == 0 || Ejahr == year || Ejahr == 0 || Sjahr < year && Ejahr > year )
                    {
                        buchungsjahresliste.Add(bj);

                        bj.Klient = Name;
                        bj.year = year;
          
                        bj.vergleicheHilfeplan.Add(sc);

                        DateTime januar;

                        if (year == DateTime.Now.Year)
                        {
                            januar = new DateTime(year, 1, 1);
                        }else
                            januar = new DateTime(GetYear(), 1, 1);

                        int i = 0;

                        foreach (var at in atAllList)
                        {
                            bj.supportCostRelDC.Add(at);

                            if (at.SupportConceptCostBearerRelDC != null &&
                                at.BookingDate.HasValue && at.SupportConceptCostBearerRelDC.CostBearer2SupportConceptOid == sc.CostBearerRelOids[0])
                            {
                                if (at.BookingDate.Value >= januar && at.BookingDate.Value < januar.AddMonths(1))
                                {
                                    bj.januarAtList.Add(at);
                                }
                                else if (at.BookingDate.Value >= januar.AddMonths(1) && at.BookingDate.Value < januar.AddMonths(2))
                                {
                                    bj.februarAtList.Add(at);
                                }
                                else if (at.BookingDate.Value >= januar.AddMonths(2) && at.BookingDate.Value < januar.AddMonths(3))
                                {
                                    bj.märzAtList.Add(at);
                                }
                                else if (at.BookingDate.Value >= januar.AddMonths(3) && at.BookingDate.Value < januar.AddMonths(4))
                                {
                                    bj.aprilAtList.Add(at);
                                }
                                else if (at.BookingDate.Value >= januar.AddMonths(4) && at.BookingDate.Value < januar.AddMonths(5))
                                {
                                    bj.maiAtList.Add(at);
                                }
                                else if (at.BookingDate.Value >= januar.AddMonths(5) && at.BookingDate.Value < januar.AddMonths(6))
                                {
                                    bj.juniAtList.Add(at);
                                }
                                else if (at.BookingDate.Value >= januar.AddMonths(6) && at.BookingDate.Value < januar.AddMonths(7))
                                {
                                    bj.juliAtList.Add(at);
                                }
                                else if (at.BookingDate.Value >= januar.AddMonths(7) && at.BookingDate.Value < januar.AddMonths(8))
                                {
                                    bj.augustAtList.Add(at);
                                }
                                else if (at.BookingDate.Value >= januar.AddMonths(8) && at.BookingDate.Value < januar.AddMonths(9))
                                {
                                    bj.septemberAtList.Add(at);
                                }
                                else if (at.BookingDate.Value >= januar.AddMonths(9) && at.BookingDate.Value < januar.AddMonths(10))
                                {
                                    bj.oktoberAtList.Add(at);
                                }
                                else if (at.BookingDate.Value >= januar.AddMonths(10) && at.BookingDate.Value < januar.AddMonths(11))
                                {
                                    bj.novemberAtList.Add(at);
                                }
                                else if (at.BookingDate.Value >= januar.AddMonths(11) && at.BookingDate.Value < januar.AddMonths(12))
                                {
                                    bj.dezemberAtList.Add(at);
                                }
                            }
                            bj.oidTransactionList.Add(at.AccountingTransactionOid);                        
                        }
                      
                    }
                }
                
                srGridControl.ItemsSource = buchungsjahresliste;

            });

        }

        private void SelectYearToExecuteProgramm (object sender,SelectionChangedEventArgs e){

            var yearString = combobox_year.SelectedItem.ToString();
            LoadServiceKlient(Int32.Parse(yearString));
        }

        private int GetYear()
        {
            var yearString = combobox_year.SelectedItem.ToString();
            return Int32.Parse(yearString);
        }

    }
}

    public class BuchungsJahr 
    {
       
        public String Klient { get; set; }

        public int year;

        bool freigabe = true;

        decimal speicherJan = 0; decimal speicherFeb = 0; decimal speicherMrz = 0; decimal speicherApr = 0; decimal speicherMai = 0;
        decimal speicherJuni = 0; decimal speicherJuli = 0; decimal speicherAug = 0; decimal speicherSep = 0; decimal speicherOkt = 0;
        decimal speicherNov = 0; decimal speicherDez = 0;

        public List<AccountingTransactionDC> januarAtList = new List<AccountingTransactionDC>(); 
        public List<AccountingTransactionDC> februarAtList = new List<AccountingTransactionDC>();
        public List<AccountingTransactionDC> märzAtList = new List<AccountingTransactionDC>();
        public List<AccountingTransactionDC> aprilAtList = new List<AccountingTransactionDC>();
        public List<AccountingTransactionDC> maiAtList = new List<AccountingTransactionDC>();
        public List<AccountingTransactionDC> juniAtList = new List<AccountingTransactionDC>();
        public List<AccountingTransactionDC> juliAtList = new List<AccountingTransactionDC>();
        public List<AccountingTransactionDC> augustAtList = new List<AccountingTransactionDC>();
        public List<AccountingTransactionDC> septemberAtList = new List<AccountingTransactionDC>();
        public List<AccountingTransactionDC> oktoberAtList = new List<AccountingTransactionDC>();
        public List<AccountingTransactionDC> novemberAtList = new List<AccountingTransactionDC>();
        public List<AccountingTransactionDC> dezemberAtList = new List<AccountingTransactionDC>();

        public List<long?> oidTransactionList = new List<long?>();

        public List<CompactSupportConceptDC> vergleicheHilfeplan = new List<CompactSupportConceptDC>();

        public List<AccountingTransactionDC> supportCostRelDC = new List<AccountingTransactionDC>();

        public List<AccountingTransactionDC> alleVorhandenenDC = new List<AccountingTransactionDC>();

        public List<AccountingTransactionDC> accountingTransDC = new List<AccountingTransactionDC> { new AccountingTransactionDC() };
        
        public decimal? Januar
        {
            get 
            {
                if (speicherJan == 0)
                    return Math.Round(januarAtList.Sum(s => s.Amount),2);
                else
                    return Math.Round(speicherJan,2);
            }
            set
            {
                for (int i = 0; i < januarAtList.Count; i++)
                {
                    if (i == 0)
                    {
                        januarAtList[i].Amount = value ?? 0;
                    }
                    else
                    {
                        januarAtList[i].Amount = 0;
                    }
                }
                if (januarAtList.Count == 0)
                {
                    createTsDC(value ?? 0, 1, "Januar");
                    if (freigabe == true)
                        speicherJan = value.Value;
                    else
                        freigabe = false;
                }else
                    OnPropertyChanged("Januar", januarAtList);
            }
        }

        public decimal? Februar
        {
            get 
            {
                if (speicherFeb == 0)
                    return Math.Round(februarAtList.Sum(s => s.Amount),2);
                else
                    return Math.Round(speicherFeb, 2);
            }
            set
            {
                for (int i = 0; i < februarAtList.Count; i++)
                {
                    if (i == 0)
                    {
                        februarAtList[i].Amount = value ?? 0;
                    }
                    else
                    {
                        februarAtList[i].Amount = 0;
                    }
                }
                if (februarAtList.Count == 0)
                {
                    createTsDC(value ?? 0, 2, "Februar");
                    if (freigabe == true)
                        speicherFeb = value.Value;
                    else
                        freigabe = true;
                }else
                    OnPropertyChanged("Februar", februarAtList);
            }
        }

        public decimal? März
        {
            get 
            {
                if (speicherMrz == 0)
                    return Math.Round(märzAtList.Sum(s => s.Amount),2);
                else
                    return Math.Round(speicherMrz, 2);
            }
            set
            {
                for (int i = 0; i < märzAtList.Count; i++)
                {
                    if (i == 0)
                    {
                        märzAtList[i].Amount = value ?? 0; 
                        
                    }
                    else
                    {
                        märzAtList[i].Amount = 0;
                    }
                }

                if (märzAtList.Count == 0)
                {
                    createTsDC(value ?? 0, 3, "März");
                    if (freigabe == true)
                        speicherMrz = value.Value;
                    else
                        freigabe = true;
                }else
                    OnPropertyChanged("März", märzAtList);

            }
        }

        public decimal? April
        {
            get 
            {
                if (speicherApr == 0)
                    return Math.Round(aprilAtList.Sum(s => s.Amount),2);
                else
                    return Math.Round(speicherApr, 2);
            }
            set
            {
                for (int i = 0; i < aprilAtList.Count; i++)
                {
                    if (i == 0)
                    {
                        aprilAtList[i].Amount = value ?? 0;
                       
                    }
                    else
                    {
                        aprilAtList[i].Amount = 0;
                    }
                }
                if (aprilAtList.Count == 0)
                {
                    createTsDC(value ?? 0, 4, "April");
                    if (freigabe == true)
                        speicherApr = value.Value;
                    else
                        freigabe = true;
                }else
                    OnPropertyChanged("April", aprilAtList);
            }
        }

        public decimal? Mai
        {
            get 
            {
                if (speicherMai == 0)
                    return Math.Round(maiAtList.Sum(s => s.Amount),2);
                else
                    return Math.Round(speicherMai, 2);
            }
            set
            {
                for (int i = 0; i < maiAtList.Count; i++)
                {
                    if (i == 0)
                    {
                        maiAtList[i].Amount = value ?? 0;
                        
                    }
                    else
                    {
                        maiAtList[i].Amount = 0;
                    }
                }
                if (maiAtList.Count == 0)
                {
                    createTsDC(value ?? 0, 5, "Mai");
                    if (freigabe == true)
                        speicherMai = value.Value;
                    else
                        freigabe = true;
                }else
                    OnPropertyChanged("Mai", maiAtList);
            }
        }

        public decimal? Juni
        {
           get 
            {
                if (speicherJuni == 0)
                    return Math.Round(juniAtList.Sum(s => s.Amount),2);
                else
                    return Math.Round(speicherJuni, 2);
            }
            set
            {
                for (int i = 0; i < juniAtList.Count; i++)
                {
                    if (i == 0)
                    {
                        juniAtList[i].Amount = value ?? 0; 
                        
                    }
                    else
                    {
                        juniAtList[i].Amount = 0;
                    }
                }
                if (juniAtList.Count == 0)
                {
                    createTsDC(value ?? 0, 6, "Juni");
                    if (freigabe == true)
                        speicherJuni = value.Value;
                    else
                        freigabe = true;
                }else
                    OnPropertyChanged("Juni", juniAtList);
            }
        }

        public decimal? Juli
        {
           get 
            {
                if (speicherJuli == 0)
                    return Math.Round(juliAtList.Sum(s => s.Amount),2);
                else
                    return Math.Round(speicherJuli, 2);
            }
            set
            {
                for (int i = 0; i < juliAtList.Count; i++)
                {
                    if (i == 0)
                    {
                        juliAtList[i].Amount = value ?? 0;   
                    }
                    else
                    {
                        juliAtList[i].Amount = 0;
                    }
                }
                if (juliAtList.Count == 0)
                {
                    createTsDC(value ?? 0, 7, "Juli");
                    if (freigabe == true)
                        speicherJuli = value.Value;
                    else
                        freigabe = true; 
                }else
                    OnPropertyChanged("Juli", juliAtList);
            }
        }

        public decimal? August
        {
            get 
            {
                if (speicherAug == 0)
                    return Math.Round(augustAtList.Sum(s => s.Amount),2);
                else
                    return Math.Round(speicherAug, 2);
            }
            set
            {
                for (int i = 0; i < augustAtList.Count; i++)
                {
                    if (i == 0)
                    {
                        augustAtList[i].Amount = value ?? 0;
                       
                    }
                    else
                    {
                        augustAtList[i].Amount = 0;
                    }
                }
                if (augustAtList.Count == 0)
                {
                    createTsDC(value ?? 0, 8, "August");
                    if (freigabe == true)
                        speicherAug = value.Value;
                    else
                        freigabe = true;
                }else
                    OnPropertyChanged("August", augustAtList);   
            }
        }

        public decimal? September
        {
           get 
            { 
                if (speicherSep == 0)
                    return Math.Round(septemberAtList.Sum(s => s.Amount), 2);
                else
                    return Math.Round(speicherSep,2);
            }
            set
            {
                for (int i = 0; i < septemberAtList.Count; i++)
                {
                    if (i == 0)
                    {
                        septemberAtList[i].Amount = value ?? 0;
                    }
                    else
                    {
                        septemberAtList[i].Amount = 0;
                    }
                }
                if (septemberAtList.Count == 0)
                {
                    createTsDC(value ?? 0, 9, "September");
                    if (freigabe == true)
                        speicherSep = value.Value;
                    else
                        freigabe = true;
                }else
                    OnPropertyChanged("September", septemberAtList);
            }
        }

        public decimal? Oktober
        {
           get 
            {
                if (speicherOkt == 0)
                    return Math.Round(oktoberAtList.Sum(s => s.Amount),2);
                else
                    return Math.Round(speicherOkt, 2);
            }
            set
            {
                for (int i = 0; i < oktoberAtList.Count; i++)
                {
                    if (i == 0)
                    {
                        oktoberAtList[i].Amount = value ?? 0;
                       
                    }
                    else
                    {
                        oktoberAtList[i].Amount = 0;
                    }
                }
                if (oktoberAtList.Count == 0)
                {
                    createTsDC(value ?? 0, 10, "Oktober");
                    if (freigabe == true)
                        speicherOkt = value.Value;
                    else
                        freigabe = true;
                }else
                    OnPropertyChanged("Oktober", oktoberAtList);
            }
        }

        public decimal? November
        {
           get 
            {
                if (speicherNov == 0)
                    return Math.Round(novemberAtList.Sum(s => s.Amount),2);
                else
                    return Math.Round(speicherNov, 2);
            }
            set
            {
                for (int i = 0; i < novemberAtList.Count; i++)
                {
                    if (i == 0)
                    {
                        novemberAtList[i].Amount = value ?? 0;
                       
                    }
                    else
                    {
                        novemberAtList[i].Amount = 0;
                    }
                }
                if (novemberAtList.Count == 0)
                {
                    createTsDC(value ?? 0, 11, "November");
                    if (freigabe == true)
                        speicherNov = value.Value;
                    else
                        freigabe = false;
                }else
                    OnPropertyChanged("November", novemberAtList);
            }
        }

        public decimal? Dezember
        {
           get 
            {
                if (speicherDez == 0)
                    return Math.Round(dezemberAtList.Sum(s => s.Amount),2);
                else
                    return Math.Round(speicherDez, 2);
            }
            set
            {
                for (int i = 0; i < dezemberAtList.Count; i++)
                {
                    if (i == 0)
                    {
                        dezemberAtList[i].Amount = value ?? 0;
                       
                    }
                    else
                    {
                        dezemberAtList[i].Amount = 0;
                    }
                }
                if (dezemberAtList.Count == 0)
                {
                    createTsDC(value ?? 0, 12, "Dezember");
                    if (freigabe == true)
                        speicherDez = value.Value;
                    else
                        freigabe = true;
                }else
                    OnPropertyChanged("Dezember", dezemberAtList);
            }
        }


        public event PropertyChangedEventHandler PropertyChanged;

        protected virtual void OnPropertyChanged(string propertyName,List<AccountingTransactionDC> Model)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyName));
           
 
          saveKlientBuchung(Model);      
        }

        
        protected virtual void OnPropertyChangedNew(string propertyName,List<AccountingTransactionDC> Model)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            
            if (handler != null)
                handler(this, new PropertyChangedEventArgs(propertyName));
                
            
            SaveNewAmount(Model);
        }
         
        private void saveKlientBuchung(List<AccountingTransactionDC> Model)
        {           
            for (int i = 0; i < oidTransactionList.Count; i++)
            {
                if (Model.Count == 0)
                {
                    MessageBox.Show("Die Eingetragenen Buchungen liegen nicht vor");
                }
                else
                {
                    if (Model[0].AccountingTransactionOid == null) {  
                    }
                    else if (oidTransactionList[i].Value == Model[0].AccountingTransactionOid.Value)
                    {
                        for (int x = 0; x < supportCostRelDC.Count; x++)
                        {
                            if (supportCostRelDC[x].AccountingTransactionOid == Model[0].AccountingTransactionOid.Value)
                            {
                                try
                                {
                                    if (supportCostRelDC[x].SupportConceptCostBearerRelDC.StartDate <= Model[0].BookingDate &&
                                        supportCostRelDC[x].SupportConceptCostBearerRelDC.EndDate >= Model[0].ValidityDate ||
                                        supportCostRelDC[x].SupportConceptCostBearerRelDC.StartDate <= Model[0].BookingDate &&
                                        Model[0].ValidityDate == null                       || 
                                        supportCostRelDC[x].SupportConceptCostBearerRelDC.StartDate.Value.Month == Model[0].BookingDate.Value.Month &&
                                        supportCostRelDC[x].SupportConceptCostBearerRelDC.StartDate.Value.Year == Model[0].BookingDate.Value.Year ||
                                        supportCostRelDC[x].SupportConceptCostBearerRelDC.StartDate.Value.Month == Model[0].BookingDate.Value.Month &&
                                        supportCostRelDC[x].SupportConceptCostBearerRelDC.StartDate.Value.Year == Model[0].BookingDate.Value.Year)
                                    {
                                        if (supportCostRelDC[x].SupportConceptCostBearerRelDC.StartDate.Value.Day <= Model[0].BookingDate.Value.Day &&
                                            supportCostRelDC[x].SupportConceptCostBearerRelDC.StartDate.Value.Month == Model[0].BookingDate.Value.Month
                                                                                            ||
                                            supportCostRelDC[x].SupportConceptCostBearerRelDC.StartDate.Value.Day >= Model[0].BookingDate.Value.Day &&
                                            supportCostRelDC[x].SupportConceptCostBearerRelDC.StartDate.Value.Month == Model[0].BookingDate.Value.Month
                                                                                            ||
                                            supportCostRelDC[x].SupportConceptCostBearerRelDC.StartDate.Value.Month <= Model[0].BookingDate.Value.Month &&
                                            supportCostRelDC[x].SupportConceptCostBearerRelDC.StartDate.Value.Year <= Model[0].BookingDate.Value.Year &&
                                            supportCostRelDC[x].SupportConceptCostBearerRelDC.EndDate.Value.Year >= Model[0].BookingDate.Value.Year
                                                                                            ||
                                            supportCostRelDC[x].SupportConceptCostBearerRelDC.StartDate.Value.Year < Model[0].BookingDate.Value.Year &&
                                            supportCostRelDC[x].SupportConceptCostBearerRelDC.EndDate.Value.Year > Model[0].BookingDate.Value.Year
                                                                                            ||
                                            supportCostRelDC[x].SupportConceptCostBearerRelDC.EndDate.Value.Month >= Model[0].BookingDate.Value.Month &&
                                            supportCostRelDC[x].SupportConceptCostBearerRelDC.EndDate.Value.Year == Model[0].BookingDate.Value.Year)  
                                        {
                                            ServiceFacade.DoOperationsServiceAsync(s => s.UpdateAccountingTransactions(Model));
                                            freigabe = false;
                                        }
                                        else
                                            MessageBox.Show("Die Eingetragene Buchung liegt nicht im Gültigen Hilfeplan bereich");
                                    }
                                    else
                                        MessageBox.Show("Die Eingetragene Buchung liegt nicht im Gültigen Hilfeplan bereich");
                                }
                                catch (Exception s) { MessageBox.Show("Es ist ein Fehler beim Update aufgetreten"); }
                            }
                        }
                    }               
                }
            }
        }

        private void SaveNewAmount(List<AccountingTransactionDC> Model)
        {
            try
            {
                if (vergleicheHilfeplan[0].StartDate == null || vergleicheHilfeplan[0].StartDate.Value.Month == 0)
                {
                    ServiceFacade.DoOperationsServiceAsync(s => s.InsertNewAccountingTransactions(Model)); 
                }
                else if (vergleicheHilfeplan[0].StartDate <= Model[0].BookingDate &&
                    vergleicheHilfeplan[0].EndDate >= Model[0].ValidityDate ||
                    vergleicheHilfeplan[0].StartDate <= Model[0].BookingDate &&
                    Model[0].ValidityDate == null ||
                    vergleicheHilfeplan[0].StartDate.Value.Month == Model[0].BookingDate.Value.Month &&
                    vergleicheHilfeplan[0].StartDate.Value.Year == Model[0].BookingDate.Value.Year ||
                    vergleicheHilfeplan[0].StartDate.Value.Month == Model[0].BookingDate.Value.Month)
                {
                    //Setzt den erstell Tag auf den Gültigen Hilfeplan Tag
                    if (vergleicheHilfeplan[0].StartDate.Value.Month == Model[0].BookingDate.Value.Month &&
                        vergleicheHilfeplan[0].StartDate.Value.Year == Model[0].BookingDate.Value.Year || 
                        vergleicheHilfeplan[0].EndDate.Value.Month == Model[0].BookingDate.Value.Month &&
                        vergleicheHilfeplan[0].EndDate.Value.Year == Model[0].BookingDate.Value.Year)
                    {
                        Model[0].BookingDate.Value.AddDays(vergleicheHilfeplan[0].StartDate.Value.Day - 1);
                    }

                    if (vergleicheHilfeplan[0].StartDate.Value.Day <= Model[0].BookingDate.Value.Day &&
                        vergleicheHilfeplan[0].StartDate.Value.Month == Model[0].BookingDate.Value.Month
                                                              ||
                        vergleicheHilfeplan[0].StartDate.Value.Day >= Model[0].BookingDate.Value.Day &&
                        vergleicheHilfeplan[0].StartDate.Value.Month == Model[0].BookingDate.Value.Month
                                                              ||
                        vergleicheHilfeplan[0].StartDate.Value.Month <= Model[0].BookingDate.Value.Month &&
                        vergleicheHilfeplan[0].StartDate.Value.Year <= Model[0].BookingDate.Value.Year &&
                        vergleicheHilfeplan[0].EndDate.Value.Year >= Model[0].BookingDate.Value.Year
                                                               ||
                        vergleicheHilfeplan[0].StartDate.Value.Year < Model[0].BookingDate.Value.Year &&
                        vergleicheHilfeplan[0].EndDate.Value.Year > Model[0].BookingDate.Value.Year
                                                               ||
                        vergleicheHilfeplan[0].EndDate.Value.Month >= Model[0].BookingDate.Value.Month &&
                        vergleicheHilfeplan[0].EndDate.Value.Year == Model[0].BookingDate.Value.Year)
                    {
                        ServiceFacade.DoOperationsServiceAsync(s => s.InsertNewAccountingTransactions(Model));
                    }
                    else
                    {
                        MessageBox.Show("Die Eingetragene Buchung liegt nicht im Gültigen Hilfeplan bereich");
                        freigabe = false;
                    }
                }
                else
                {
                    MessageBox.Show("Die Eingetragene Buchung liegt nicht im Gültigen Hilfeplan bereich");
                    freigabe = false;
                }
            }
            catch (Exception s) { MessageBox.Show("Es ist ein Fehler aufgetreten. "); freigabe = false; }
        }


        private void createTsDC(decimal value, int monat, String Monatname) {
            for (int y = 0; y < alleVorhandenenDC.Count; y++) {
                if (alleVorhandenenDC[y].SupportConceptCostBearerRelDC != null && vergleicheHilfeplan.Count > 0)
                {
                    if (alleVorhandenenDC[y].SupportConceptCostBearerRelDC.CostBearer2SupportConceptOid == vergleicheHilfeplan[0].CostBearerRelOids[0])
                    {
                        accountingTransDC[0].SupportConceptCostBearerRelDC = alleVorhandenenDC[y].SupportConceptCostBearerRelDC;
                    }
                    else {       
                        accountingTransDC[0].SupportConceptCostBearerRelDC = new SupportConceptCostBearerRelDC();
                        accountingTransDC[0].SupportConceptCostBearerRelDC.CostBearer2SupportConceptOid = vergleicheHilfeplan[0].CostBearerRelOids[0];
                    }
                }
            }

            

            if (vergleicheHilfeplan.Count == 0 || vergleicheHilfeplan[0].CostBearer.Equals("Selbstzahler nicht bewilligt!"))
            {
                MessageBox.Show("Selbstzahler Buchungen nicht bewilligt");
                freigabe = false;
            }
            else
            {
                if (accountingTransDC[0].SupportConceptCostBearerRelDC == null) {
                    accountingTransDC[0].SupportConceptCostBearerRelDC = new SupportConceptCostBearerRelDC();
                    accountingTransDC[0].SupportConceptCostBearerRelDC.CostBearer2SupportConceptOid = vergleicheHilfeplan[0].CostBearerRelOids[0];
                }


                accountingTransDC[0].BookingDate = new DateTime(year, monat, 1);
                accountingTransDC[0].ValidityDate = new DateTime(year, monat, DateTime.DaysInMonth(year, monat));
                accountingTransDC[0].Amount = value;

                OnPropertyChangedNew(Monatname, accountingTransDC);
            }
        }

        private AccountingBookingVM _ViewModel;


        internal AccountingBookingVM ViewModel
        {
            get
            {
                return _ViewModel;
            }

            set
            {
                _ViewModel = value;
            }
        }
}