private void DeleteEntry(object obj)
 {
     using (var context = new LedgerContext())
     {
         if (!SelectedItem.Comment.IsInDictionary)
         {
             context.LedgerDictionaries.Attach(SelectedItem.Comment);
             context.LedgerDictionaries.Remove(SelectedItem.Comment);
         }
         if (!SelectedItem.Contractor.IsInDictionary)
         {
             context.LedgerDictionaries.Attach(SelectedItem.Contractor);
             context.LedgerDictionaries.Remove(SelectedItem.Contractor);
         }
         if (!SelectedItem.EconomicEvent.IsInDictionary)
         {
             context.LedgerDictionaries.Attach(SelectedItem.EconomicEvent);
             context.LedgerDictionaries.Remove(SelectedItem.EconomicEvent);
         }
         context.Entries.Attach(SelectedItem);
         context.Entries.Remove(SelectedItem);
         context.SaveChanges();
     }
     GetDataFromDatabase();
 }
 public void AddContractor(object obj)
 {
     using (var database = new LedgerContext())
     {
         database.LedgerDictionaries.Add(Contractor);
         database.SaveChanges();
     }
     CloseWindow();
 }
 public void EditContractor(object obj)
 {
     using (var context = new LedgerContext())
     {
         var contractorInDb = context.LedgerDictionaries.OfType<Contractor>().AsNoTracking().Single(x => x.Id == Contractor.Id);
         if (!Contractor.Equals(contractorInDb))
             context.Entry(Contractor).State = EntityState.Modified;
         context.SaveChanges();
     }
     CloseWindow();
 }
 public MainViewModel()
 {
     SelectedDate = DateTime.Today;
     GetDataFromDatabase();
     Columns = XmlColumn.GetColumns(ListViewType.MainWindow);
     RefreshColumns += (s, e) => GetColumns();
     UpdateData += (s, e) => GetDataFromDatabase();
     using (var context = new LedgerContext())
     {
         if (context.LedgerDictionaries.Any()) return;
         using (var writer = new XmlTextWriter(Path.GetDirectoryName(Path.GetDirectoryName(Directory.GetCurrentDirectory())) + @"\Model\Model.edmx", Encoding.Default))
         {
             EdmxWriter.WriteEdmx(context, writer);
         }
     }       
 }
 public ExpenseViewModel()
 {
     Header = "Wydatki (koszty)";
     var startOfTheMonth = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1);
     int count;
     using (var context = new LedgerContext())
     {
         EconomicEvents = context.LedgerDictionaries.OfType<EconomicEvent>()
             .Where(x => x.IsInDictionary && x.ForExpense).ToList();
         Comments = context.LedgerDictionaries.OfType<Comment>()
             .Where(x => x.IsInDictionary && x.ForExpense).ToList();
         count = context.Entries
             .Count(x => x.Date >= startOfTheMonth);
     }
     Entry = new Expense
     {
         Date = DateTime.Today,
         EconomicEventId = EconomicEvents.First().Id,
         CommentId = Comments.First().Id,
         EvidenceNumber = $"{count + 1}/{ DateTime.Today.Month}/{DateTime.Today.Year}"
     };
 }
 public RevenueViewModel()
 {
     Header = "Przychód";
     var startOfTheMonth = new DateTime(DateTime.Today.Year, DateTime.Today.Month, 1);
     int count;
     using (var database = new LedgerContext())
     {
         EconomicEvents = database.LedgerDictionaries.OfType<EconomicEvent>()
             .Where(x => x.IsInDictionary && x.ForRevenue).ToList();
         Comments = database.LedgerDictionaries.OfType<Comment>()
            .Where(x => x.IsInDictionary && x.ForRevenue).ToList();
         count = database.Entries
             .Count(x => x.Date >= startOfTheMonth);
     }
     Entry = new Revenue
     {
         Date = DateTime.Today,
         EconomicEventId = EconomicEvents.First().Id,
         CommentId = Comments.First().Id,
         EvidenceNumber = $"{count + 1}/{ DateTime.Today.Month}/{DateTime.Today.Year}"
     };
 }
 private void GetDataFromDatabase()
 {
     using (var context = new LedgerContext())
     {
         Entries = new ObservableCollection<Entry>(context.Entries
             .Include(x => x.EconomicEvent).Include(x => x.Comment).Include(x => x.Contractor)
             .Where(entry => entry.Date.Month == SelectedDate.Month && entry.Date.Year == SelectedDate.Year));
         PreviousMonthEntry = context.Entries.FirstOrDefault(x => x.Date < SelectedDate && (x.Date.Month < SelectedDate.Month || x.Date.Year < SelectedDate.Year));
         NextMonthEntry = context.Entries.FirstOrDefault(x => x.Date > SelectedDate && (x.Date.Month > SelectedDate.Month || x.Date.Year > SelectedDate.Year));
     }
     UpdateFooterValues();
 }
 public void EditEntry(object obj)
 {
     CheckIfAddedNewDictionaryEntry();
     using (var context = new LedgerContext())
     {
         var contractorInDb = context.LedgerDictionaries.OfType<Contractor>().AsNoTracking().Single(x => x.Id == Entry.Contractor.Id);
         if (!Entry.Contractor.Equals(contractorInDb))
             context.Entry(Contractor).State=EntityState.Modified;
         context.Entry(Entry).State=EntityState.Modified;
         context.SaveChanges();
     }
     Close();
 }
 public void AddEntry(object obj)
 {
     CheckIfAddedNewDictionaryEntry();
     using (var database = new LedgerContext())
     {
         database.Entries.Add(Entry);
         database.SaveChanges();
     }
     Close();
 }