Esempio n. 1
0
        /// <summary>
        /// Deletes the current row and optionally populates a completion message
        /// </summary>
        /// <param name="ARowToDelete">the currently selected row to delete</param>
        /// <param name="AFPrevRow">Is FPreviouslySelectedDetailRow</param>
        /// <param name="ACompletionMessage">if specified, is the deletion completion message</param>
        /// <returns>true if row deletion is successful</returns>
        public bool DeleteRowManual(ARecurringGiftBatchRow ARowToDelete, ref ARecurringGiftBatchRow AFPrevRow, ref string ACompletionMessage)
        {
            bool DeletionSuccessful = false;

            if (ARowToDelete == null)
            {
                return(DeletionSuccessful);
            }

            int BatchNumber = ARowToDelete.BatchNumber;

            ACompletionMessage = string.Empty;

            bool RowToDeleteIsNew = (ARowToDelete.RowState == DataRowState.Added);

            //Backup the Dataset for reversion purposes
            GiftBatchTDS BackupMainDS = (GiftBatchTDS)FMainDS.Copy();

            BackupMainDS.Merge(FMainDS);

            if (!RowToDeleteIsNew)
            {
                //Return modified row to last saved state to avoid validation failures
                ARowToDelete.RejectChanges();

                if (!FMyForm.SaveChangesManual(TExtraGiftBatchChecks.GiftBatchAction.DELETING))
                {
                    MessageBox.Show(Catalog.GetString("Error in trying to save prior to deleting current recurring gift batch!"),
                                    Catalog.GetString("Deletion Error"),
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Error);

                    return(DeletionSuccessful);
                }
            }

            try
            {
                FMyForm.Cursor = Cursors.WaitCursor;

                ACompletionMessage = String.Format(Catalog.GetString("Batch no.: {0} deleted successfully."),
                                                   BatchNumber);

                //clear any transactions currently being editied in the Transaction Tab
                FMyForm.GetTransactionsControl().ClearCurrentSelection();

                if (!RowToDeleteIsNew)
                {
                    //Load tables afresh
                    FMainDS.Merge(TRemote.MFinance.Gift.WebConnectors.LoadRecurringGiftTransactionsForBatch(FLedgerNumber, BatchNumber));
                }

                FMyForm.GetTransactionsControl().DeleteCurrentRecurringBatchGiftData(BatchNumber);

                // Delete the recurring batch row.
                ARowToDelete.Delete();

                AFPrevRow = null;

                DeletionSuccessful = true;
            }
            catch (Exception ex)
            {
                //Revert to previous state
                FMainDS.Merge(BackupMainDS);

                ACompletionMessage = ex.Message;
                MessageBox.Show(ACompletionMessage,
                                "Deletion Error",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Error);

                TLogging.LogException(ex, Utilities.GetMethodSignature());
                throw;
            }
            finally
            {
                FMyForm.Cursor = Cursors.Default;
            }

            return(DeletionSuccessful);
        }
        /// <summary>
        /// Deletes the current row and optionally populates a completion message
        /// </summary>
        /// <param name="ARowToDelete">the currently selected row to delete</param>
        /// <param name="AFPrevRow">Is FPreviouslySelectedDetailRow</param>
        /// <param name="ACompletionMessage">if specified, is the deletion completion message</param>
        /// <returns>true if row deletion is successful</returns>
        public bool DeleteRowManual(ARecurringGiftBatchRow ARowToDelete, ref ARecurringGiftBatchRow AFPrevRow, ref string ACompletionMessage)
        {
            bool DeletionSuccessful = false;

            ACompletionMessage = string.Empty;

            if (ARowToDelete == null)
            {
                return(false);
            }

            int  CurrentBatchNo = ARowToDelete.BatchNumber;
            bool CurrentBatchTransactionsLoadedAndCurrent = false;

            //Backup the Dataset for reversion purposes
            GiftBatchTDS BackupMainDS = null;

            try
            {
                FMyForm.Cursor = Cursors.WaitCursor;

                //Backup the changes to allow rollback
                BackupMainDS = (GiftBatchTDS)FMainDS.GetChangesTyped(false);

                //Don't run an inactive fields check on this batch
                FMyForm.GetBatchControl().UpdateRecurringBatchDictionary(CurrentBatchNo);

                //Check if current batch gift details are currently loaded
                CurrentBatchTransactionsLoadedAndCurrent = (FMyForm.GetTransactionsControl().FBatchNumber == CurrentBatchNo);

                //Save and check for inactive values and ex-workers and anonymous gifts
                //  in other unsaved Batches
                FPetraUtilsObject.SetChangedFlag();

                if (!FMyForm.SaveChangesManual(TExtraGiftBatchChecks.GiftBatchAction.DELETING, !CurrentBatchTransactionsLoadedAndCurrent))
                {
                    FMyForm.GetBatchControl().UpdateRecurringBatchDictionary();

                    string msg = String.Format(Catalog.GetString("Recurring Batch {0} has not been deleted."),
                                               CurrentBatchNo);

                    MessageBox.Show(msg,
                                    "Recurring Gift Batch Deleted",
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Information);

                    return(false);
                }

                //Remove any changes to current batch that may cause validation issues
                FMyForm.GetBatchControl().PrepareBatchDataForDeleting(CurrentBatchNo, true);

                if (CurrentBatchTransactionsLoadedAndCurrent)
                {
                    //Clear any transactions currently being edited in the Transaction Tab
                    FMyForm.GetTransactionsControl().ClearCurrentSelection(CurrentBatchNo);
                }

                //Delete transactions
                FMyForm.GetTransactionsControl().DeleteRecurringBatchGiftData(CurrentBatchNo);

                // Delete the recurring batch row and save again after deleting the batch row.
                ARowToDelete.Delete();

                ACompletionMessage = String.Format(Catalog.GetString("Recurring Gift Batch no.: {0} deleted successfully."),
                                                   CurrentBatchNo);

                AFPrevRow = null;

                DeletionSuccessful = true;
            }
            catch (Exception ex)
            {
                //Revert to previous state
                if (BackupMainDS != null)
                {
                    FMainDS.RejectChanges();
                    FMainDS.Merge(BackupMainDS);

                    FMyForm.GetBatchControl().ShowDetailsRefresh();
                }

                TLogging.LogException(ex, Utilities.GetMethodSignature());
                throw;
            }
            finally
            {
                FMyForm.Cursor = Cursors.Default;
            }

            return(DeletionSuccessful);
        }
        private bool OnDeleteRowManual(GiftBatchTDSARecurringGiftDetailRow ARowToDelete, ref string ACompletionMessage)
        {
            //TODO: Make this like deleton on GL Transactions form
            // e.g. pass copy to delete method on server...
            //GiftBatchTDS TempDS = (GiftBatchTDS)FMainDS.Copy();
            //TempDS.Merge(FMainDS);

            bool DeletionSuccessful = false;

            ACompletionMessage = string.Empty;

            if (FBatchRow == null)
            {
                FBatchRow = GetRecurringBatchRow();
            }

            if (ARowToDelete == null)
            {
                return(false);
            }

            int  CurrentBatchNo   = ARowToDelete.BatchNumber;
            bool RowToDeleteIsNew = (ARowToDelete.RowState == DataRowState.Added);
            int  CurrentRowIndex  = GetSelectedRowIndex();

            TFrmRecurringGiftBatch FMyForm = (TFrmRecurringGiftBatch)this.ParentForm;

            GiftBatchTDS BackupMainDS = null;

            int    SelectedDetailNumber                 = ARowToDelete.DetailNumber;
            int    RecurringGiftToDeleteTransNo         = 0;
            string FilterAllRecurringGiftsOfBatch       = String.Empty;
            string FilterAllRecurringGiftDetailsOfBatch = String.Empty;

            int DetailRowCount = FGiftDetailView.Count;

            try
            {
                this.Cursor = Cursors.WaitCursor;
                //Specify current action
                FMyForm.FCurrentGiftBatchAction = Logic.TExtraGiftBatchChecks.GiftBatchAction.DELETINGTRANS;
                //Speeds up deletion of larger gift sets
                FMainDS.EnforceConstraints = false;
                // temporarily disable  New Donor Warning
                FMyForm.NewDonorWarning = false;

                //Backup the Dataset for reversion purposes
                BackupMainDS = (GiftBatchTDS)FMainDS.GetChangesTyped(false);

                //Don't run an inactive fields check on this batch
                FMyForm.GetBatchControl().UpdateRecurringBatchDictionary(CurrentBatchNo);

                //Delete current row
                ARowToDelete.RejectChanges();

                if (!RowToDeleteIsNew)
                {
                    ShowDetails(ARowToDelete);
                }

                ARowToDelete.Delete();

                //If there existed (before the delete row above) more than one detail row, then no need to delete Recurring Gift header row
                if (DetailRowCount > 1)
                {
                    ACompletionMessage = Catalog.GetString("Recurring Gift Detail row deleted successfully!");

                    FGiftSelectedForDeletionFlag = false;

                    foreach (DataRowView rv in FGiftDetailView)
                    {
                        GiftBatchTDSARecurringGiftDetailRow row = (GiftBatchTDSARecurringGiftDetailRow)rv.Row;

                        if (row.DetailNumber > SelectedDetailNumber)
                        {
                            row.DetailNumber--;
                        }
                    }

                    FGift.LastDetailNumber--;
                }
                else
                {
                    ACompletionMessage = Catalog.GetString("Recurring Gift deleted successfully!");

                    RecurringGiftToDeleteTransNo = FGift.GiftTransactionNumber;

                    // Reduce all Recurring Gift Detail row Transaction numbers by 1 if they are greater then Recurring Gift to be deleted
                    FilterAllRecurringGiftDetailsOfBatch = String.Format("{0}={1} And {2}>{3}",
                                                                         ARecurringGiftDetailTable.GetBatchNumberDBName(),
                                                                         FBatchNumber,
                                                                         ARecurringGiftDetailTable.GetGiftTransactionNumberDBName(),
                                                                         RecurringGiftToDeleteTransNo);

                    DataView RecurringGiftDetailView = new DataView(FMainDS.ARecurringGiftDetail);
                    RecurringGiftDetailView.RowFilter = FilterAllRecurringGiftDetailsOfBatch;
                    RecurringGiftDetailView.Sort      = String.Format("{0} ASC", ARecurringGiftDetailTable.GetGiftTransactionNumberDBName());

                    foreach (DataRowView rv in RecurringGiftDetailView)
                    {
                        GiftBatchTDSARecurringGiftDetailRow row = (GiftBatchTDSARecurringGiftDetailRow)rv.Row;

                        row.GiftTransactionNumber--;
                    }

                    //Cannot delete the Recurring Gift row, just copy the data of rows above down by 1 row
                    // and then mark the top row for deletion
                    //In other words, bubble the Recurring Gift row to be deleted to the top
                    FilterAllRecurringGiftsOfBatch = String.Format("{0}={1} And {2}>={3}",
                                                                   ARecurringGiftTable.GetBatchNumberDBName(),
                                                                   FBatchNumber,
                                                                   ARecurringGiftTable.GetGiftTransactionNumberDBName(),
                                                                   RecurringGiftToDeleteTransNo);

                    DataView RecurringGiftView = new DataView(FMainDS.ARecurringGift);
                    RecurringGiftView.RowFilter = FilterAllRecurringGiftsOfBatch;
                    RecurringGiftView.Sort      = String.Format("{0} ASC", ARecurringGiftTable.GetGiftTransactionNumberDBName());

                    ARecurringGiftRow RecurringGiftRowToReceive  = null;
                    ARecurringGiftRow RecurringGiftRowToCopyDown = null;
                    ARecurringGiftRow RecurringGiftRowCurrent    = null;

                    int currentRecurringGiftTransNo = 0;

                    foreach (DataRowView gv in RecurringGiftView)
                    {
                        RecurringGiftRowCurrent = (ARecurringGiftRow)gv.Row;

                        currentRecurringGiftTransNo = RecurringGiftRowCurrent.GiftTransactionNumber;

                        if (currentRecurringGiftTransNo > RecurringGiftToDeleteTransNo)
                        {
                            RecurringGiftRowToCopyDown = RecurringGiftRowCurrent;

                            //Copy column values down
                            for (int j = 3; j < RecurringGiftRowToCopyDown.Table.Columns.Count; j++)
                            {
                                //Update all columns except the pk fields that remain the same
                                if (!RecurringGiftRowToCopyDown.Table.Columns[j].ColumnName.EndsWith("_text"))
                                {
                                    RecurringGiftRowToReceive[j] = RecurringGiftRowToCopyDown[j];
                                }
                            }
                        }

                        if (currentRecurringGiftTransNo == FBatchRow.LastGiftNumber)
                        {
                            //Mark last record for deletion
                            RecurringGiftRowCurrent.ChargeStatus = MFinanceConstants.MARKED_FOR_DELETION;
                        }

                        //Will always be previous row
                        RecurringGiftRowToReceive = RecurringGiftRowCurrent;
                    }

                    FPreviouslySelectedDetailRow = null;
                    FGiftSelectedForDeletionFlag = true;
                    FBatchRow.LastGiftNumber--;
                }

                //Save and check for inactive values and ex-workers and anonymous gifts
                //  in other unsaved Batches
                FPetraUtilsObject.SetChangedFlag();

                if (!FMyForm.SaveChangesManual(Logic.TExtraGiftBatchChecks.GiftBatchAction.DELETINGTRANS, false, false))
                {
                    FMyForm.GetBatchControl().UpdateRecurringBatchDictionary();

                    MessageBox.Show(Catalog.GetString("The gift detail has been deleted but the changes are not saved!"),
                                    Catalog.GetString("Deletion Warning"),
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Warning);

                    ACompletionMessage = string.Empty;

                    if (FGiftSelectedForDeletionFlag)
                    {
                        FGiftSelectedForDeletionFlag = false;
                        SetBatchLastGiftNumber();
                        UpdateControlsProtection();
                    }

                    UpdateTotals();

                    return(false);
                }

                //Clear current batch's gift data and reload from server
                RefreshRecurringBatchGiftData(FBatchNumber, true);

                DeletionSuccessful = true;
            }
            catch (Exception ex)
            {
                //Revert to previous state
                RevertDataSet(FMainDS, BackupMainDS, CurrentRowIndex);

                TLogging.LogException(ex, Utilities.GetMethodSignature());
                throw;
            }
            finally
            {
                FMyForm.NewDonorWarning         = true;
                FMainDS.EnforceConstraints      = true;
                FMyForm.FCurrentGiftBatchAction = Logic.TExtraGiftBatchChecks.GiftBatchAction.NONE;
                this.Cursor = Cursors.Default;
            }

            SetGiftDetailDefaultView();
            FFilterAndFindObject.ApplyFilter();
            UpdateRecordNumberDisplay();

            return(DeletionSuccessful);
        }