// Todo: add user ID account ID to the GetTransacionById() method call to // join them with transaction ID to prevent unauthorized delete // Do this for all user-aware calls (i.e. Categories, Accounts etc.) /// <summary> /// Delete specific transaction. /// </summary> /// <param name="mc">Entity Framework model container.</param> /// <param name="transactionId">The transaction ID.</param> /// <param name="operationDate">Operation date.</param> internal static void DeleteTransaction(ModelContainer mc, int transactionId, DateTime operationDate) { Transaction transaction = GetTransacionById(mc, transactionId); transaction.IsDeleted = true; var deletedJournal = new DeletedJournal { JournalType = (byte)JournalType.Canceled, Comment = "Correction pair for deleted journal # " + transaction.Id, OriginalJournal = transaction }; foreach (var posting in transaction.Postings) { if (!posting.AccountReference.IsLoaded) { posting.AccountReference.Load(); } if (!posting.AssetTypeReference.IsLoaded) { posting.AssetTypeReference.Load(); } mc.Postings.AddObject(new Posting { Account = posting.Account, AssetType = posting.AssetType, Date = operationDate, Amount = -posting.Amount, Journal = deletedJournal }); } mc.Journals.AddObject(deletedJournal); }
// Todo: add user ID account ID to the GetJournalById() method call to // join them with transaction ID to prevent unauthorized delete // Do this for all user-aware calls (i.e. Categories, Accounts etc.) /// <summary> /// Delete specific transaction. /// </summary> /// <param name="mc">Entity Framework model container.</param> /// <param name="journalId">The journal ID.</param> internal static void DeleteJournal(ModelContainer mc, int journalId) { Journal journal = GetJournalById(mc, journalId); // decrement popularity of the journal if any if (journal.Category != null) { journal.Category.Popularity--; } // Create "deleted" journal var deletedJournal = new DeletedJournal { Id = journal.Id, JournalType = journal.JournalType, Comment = journal.Comment, Rate = journal.Rate, Quantity = journal.Quantity, Category = journal.Category, }; // Delete operation date should be the same for all "deleted" postings var deletedDate = DateTime.UtcNow; // Load and copy original journal postings to the deleted journal foreach (var posting in journal.Postings.ToList()) { if (!posting.AccountReference.IsLoaded) { posting.AccountReference.Load(); } if (!posting.AssetTypeReference.IsLoaded) { posting.AssetTypeReference.Load(); } // Copy postings from the original "postings" to the "deleted postings" table // and associate each of them with "deleted journal" mc.DeletedPostings.AddObject(new DeletedPosting { Id = posting.Id, Date = posting.Date, Amount = posting.Amount, Deleted = deletedDate, Account = posting.Account, AssetType = posting.AssetType, DeletedJournal = deletedJournal }); // Update cached account balance posting.Account.Balance -= posting.Amount; // Update cached postings count posting.Account.PostingsCount--; UpdateAccountPeriodAfterDeletePosting(mc, posting.Account, posting.Date); // Delete original postings mc.Postings.DeleteObject(posting); } // Add "deleted" journal mc.DeletedJournals.AddObject(deletedJournal); // Delete original journal mc.Journals.DeleteObject(journal); }