コード例 #1
0
        private void DeleteSelectedTransaction()
        {
            DialogResult result = MessageBox.Show("Du kommer att ta bort den markerade kontotransaktionen. Vill du även ta bort den aktuella affärshändelsen med alla associerade konto- och kassabokstransaktioner?\n\nJa: Alla assicierade transaktioner kommer att tas bort.\nNej: Endast den markerade transaktionen kommer att tas bort.", CurrentApplication.Name, MessageBoxButtons.YesNoCancel, MessageBoxIcon.Warning, MessageBoxDefaultButton.Button3);

            int transactionNo  = (int)transactionsDataGridView.SelectedRows[0].Cells[AccountTransaction.fNo].Value;
            int verificationNo = (int)transactionsDataGridView.SelectedRows[0].Cells[AccountTransaction.fVerificationNo].Value;

            try
            {
                if (result == DialogResult.Yes)
                {
                    using (var core = new StandardBusinessLayer(DataCache))
                    {
                        core.Connect();
                        core.DeleteVerificationAndAssociatedTransactions(verificationNo);
                    }

                    ApplicationEvents.OnVerificationDeleted(verificationNo);
                }
                else if (result == DialogResult.No)
                {
                    using (var core = new StandardBusinessLayer(DataCache))
                    {
                        core.Connect();

                        int noOfTransactions = core.CountAccountTransactionsByVerificationNo(verificationNo) +
                                               core.CountCashBookTransactionsByVerificationNo(verificationNo);

                        if (noOfTransactions > 1)
                        {
                            //var accountTransaction = core.GetAccountTransaction(transactionNo);
                            var  accountTransaction = DataCache.GetAccountTransaction(transactionNo);
                            bool accountHasTags     = core.CountAccountTagsByAccountNo(accountTransaction.AccountNo) > 0;

                            if (accountTransaction.Amount != 0 && accountHasTags)
                            {
                                throw new VerificationException("Du försöker ta bort en kontotransaktion från ett konto som har kontomärkningar och vars belopp inte är 0. Detta är inte tillåtet eftersom befintliga kontomärkningar kan bli felaktiga. Sätt transaktionsbeloppet för transaktionen till 0 och försök igen.");
                            }

                            DataCache.DeleteAccountTransaction(transactionNo);
                            ApplicationEvents.OnAccountTransactionDeleted(transactionNo, verificationNo);
                        }
                        else
                        {
                            core.DeleteVerificationAndAssociatedTransactions(verificationNo);
                            ApplicationEvents.OnVerificationDeleted(verificationNo);
                        }
                    }
                }
            }
            catch (VerificationException ex)
            {
                MessageBox.Show(ex.Message, CurrentApplication.Name, MessageBoxButtons.OK, MessageBoxIcon.Stop);
            }
        }