Exemplo n.º 1
0
        public static TSubmitChangesResult SaveGiftBatchTDS(ref GiftBatchTDS AInspectDS,
            out TVerificationResultCollection AVerificationResult)
        {
            AVerificationResult = new TVerificationResultCollection();
            TSubmitChangesResult SubmissionResult = TSubmitChangesResult.scrError;

            // make sure that empty tables are removed !! This can return NULL!
            AInspectDS = AInspectDS.GetChangesTyped(true);

            if (AInspectDS == null)
            {
                AVerificationResult.Add(new TVerificationResult(
                        Catalog.GetString("Save Gift Batch"),
                        Catalog.GetString("No changes - nothing to do"),
                        TResultSeverity.Resv_Info));
                return TSubmitChangesResult.scrNothingToBeSaved;
            }

            bool AllValidationsOK = true;

            bool GiftBatchTableInDataSet = (AInspectDS.AGiftBatch != null && AInspectDS.AGiftBatch.Count > 0);
            bool GiftTableInDataSet = (AInspectDS.AGift != null && AInspectDS.AGift.Count > 0);
            bool GiftDetailTableInDataSet = (AInspectDS.AGiftDetail != null && AInspectDS.AGiftDetail.Count > 0);

            //Not needed at present
            //int GiftBatchCount = GiftBatchTableInDataSet ? AInspectDS.AGiftBatch.Count : 0;
            int GiftCount = GiftTableInDataSet ? AInspectDS.AGift.Count : 0;
            int GiftDetailCount = GiftDetailTableInDataSet ? AInspectDS.AGiftDetail.Count : 0;

            bool RecurrGiftBatchTableInDataSet = (AInspectDS.ARecurringGiftBatch != null && AInspectDS.ARecurringGiftBatch.Count > 0);
            bool RecurrGiftTableInDataSet = (AInspectDS.ARecurringGift != null && AInspectDS.ARecurringGift.Count > 0);
            bool RecurrGiftDetailTableInDataSet = (AInspectDS.ARecurringGiftDetail != null && AInspectDS.ARecurringGiftDetail.Count > 0);

            if (RecurrGiftBatchTableInDataSet || RecurrGiftTableInDataSet || RecurrGiftDetailTableInDataSet)
            {
                if (GiftBatchTableInDataSet || GiftTableInDataSet || GiftDetailTableInDataSet)
                {
                    throw new Exception(String.Format("Function:{0} - Recurring and normal gift data found in same changes batch!",
                            Utilities.GetMethodName(true)));
                }

                return SaveRecurringGiftBatchTDS(ref AInspectDS,
                    ref AVerificationResult,
                    RecurrGiftBatchTableInDataSet,
                    RecurrGiftTableInDataSet,
                    RecurrGiftDetailTableInDataSet);
            }
            else
            {
                if (!(GiftBatchTableInDataSet || GiftTableInDataSet || GiftDetailTableInDataSet))
                {
                    throw new Exception(String.Format("Function:{0} - No gift data changes to save!", Utilities.GetMethodName(true)));
                }
            }

            //Get a list of all batches involved
            List <Int32>ListAllGiftBatchesToProcess = new List <int>();

            //Get batch numbers involved
            if (GiftDetailTableInDataSet)
            {
                DataView AllBatchesToProcess = new DataView(AInspectDS.AGiftDetail);
                AllBatchesToProcess.RowStateFilter = DataViewRowState.OriginalRows | DataViewRowState.Added;

                foreach (DataRowView drv in AllBatchesToProcess)
                {
                    AGiftDetailRow gdr = (AGiftDetailRow)drv.Row;
                    int batchNumber;

                    if (gdr.RowState != DataRowState.Deleted)
                    {
                        batchNumber = gdr.BatchNumber;
                    }
                    else
                    {
                        batchNumber = (Int32)gdr[AGiftDetailTable.ColumnBatchNumberId, DataRowVersion.Original];
                    }

                    if (!ListAllGiftBatchesToProcess.Contains(batchNumber))
                    {
                        ListAllGiftBatchesToProcess.Add(batchNumber);
                    }
                }

                ValidateGiftDetail(ref AVerificationResult, AInspectDS.AGiftDetail);
                ValidateGiftDetailManual(ref AVerificationResult, AInspectDS.AGiftDetail);

                if (!TVerificationHelper.IsNullOrOnlyNonCritical(AVerificationResult))
                {
                    AllValidationsOK = false;
                }
            }

            //Get batch numbers involved
            if (GiftTableInDataSet)
            {
                DataView AllBatchesToProcess = new DataView(AInspectDS.AGift);
                AllBatchesToProcess.RowStateFilter = DataViewRowState.OriginalRows | DataViewRowState.Added;

                foreach (DataRowView drv in AllBatchesToProcess)
                {
                    AGiftRow gdr = (AGiftRow)drv.Row;
                    int batchNumber;

                    if (gdr.RowState != DataRowState.Deleted)
                    {
                        batchNumber = gdr.BatchNumber;
                    }
                    else
                    {
                        // for deleted batches
                        batchNumber = (Int32)gdr[AGiftTable.ColumnBatchNumberId, DataRowVersion.Original];
                    }

                    if (!ListAllGiftBatchesToProcess.Contains(batchNumber))
                    {
                        ListAllGiftBatchesToProcess.Add(batchNumber);
                    }
                }
            }

            //Get batch numbers involved
            if (GiftBatchTableInDataSet)
            {
                DataView AllBatchesToProcess = new DataView(AInspectDS.AGiftBatch);
                AllBatchesToProcess.RowStateFilter = DataViewRowState.OriginalRows | DataViewRowState.Added;

                foreach (DataRowView drv in AllBatchesToProcess)
                {
                    AGiftBatchRow gdr = (AGiftBatchRow)drv.Row;
                    int batchNumber;

                    if (gdr.RowState != DataRowState.Deleted)
                    {
                        batchNumber = gdr.BatchNumber;
                    }
                    else
                    {
                        // for deleted batches
                        batchNumber = (Int32)gdr[AGiftBatchTable.ColumnBatchNumberId, DataRowVersion.Original];
                    }

                    if (!ListAllGiftBatchesToProcess.Contains(batchNumber))
                    {
                        ListAllGiftBatchesToProcess.Add(batchNumber);
                    }
                }

                ValidateGiftBatch(ref AVerificationResult, AInspectDS.AGiftBatch);
                ValidateGiftBatchManual(ref AVerificationResult, AInspectDS.AGiftBatch);

                if (!TVerificationHelper.IsNullOrOnlyNonCritical(AVerificationResult))
                {
                    AllValidationsOK = false;
                }
            }

            if (AVerificationResult.Count > 0)
            {
                // Downgrade TScreenVerificationResults to TVerificationResults in order to allow
                // Serialisation (needed for .NET Remoting).
                TVerificationResultCollection.DowngradeScreenVerificationResults(AVerificationResult);
            }

            if (AllValidationsOK)
            {
                if ((GiftCount > 0) && (GiftDetailCount > 1))
                {
                    //The Gift Detail table must be in ascending order
                    AGiftDetailTable cloneDetail = (AGiftDetailTable)AInspectDS.AGiftDetail.Clone();

                    foreach (int batchNumber in ListAllGiftBatchesToProcess)
                    {
                        //Copy across any rows marked as deleted first.
                        DataView giftDetails1 = new DataView(AInspectDS.AGiftDetail);
                        giftDetails1.RowFilter = string.Format("{0}={1}",
                            AGiftDetailTable.GetBatchNumberDBName(),
                            batchNumber);
                        giftDetails1.RowStateFilter = DataViewRowState.Deleted;

                        foreach (DataRowView drv in giftDetails1)
                        {
                            AGiftDetailRow gDetailRow = (AGiftDetailRow)drv.Row;
                            cloneDetail.ImportRow(gDetailRow);
                        }

                        //Import the other rows in ascending order
                        DataView giftDetails2 = new DataView(AInspectDS.AGiftDetail);
                        giftDetails2.RowFilter = string.Format("{0}={1}",
                            AGiftDetailTable.GetBatchNumberDBName(),
                            batchNumber);

                        giftDetails2.Sort = String.Format("{0} ASC, {1} ASC, {2} ASC",
                            AGiftDetailTable.GetBatchNumberDBName(),
                            AGiftDetailTable.GetGiftTransactionNumberDBName(),
                            AGiftDetailTable.GetDetailNumberDBName());

                        foreach (DataRowView giftDetailRows in giftDetails2)
                        {
                            AGiftDetailRow gDR = (AGiftDetailRow)giftDetailRows.Row;
                            cloneDetail.ImportRow(gDR);
                        }
                    }

                    //Clear the table and import the rows from the clone
                    AInspectDS.AGiftDetail.Clear();

                    for (int i = 0; i < GiftDetailCount; i++)
                    {
                        AGiftDetailRow gDR2 = (AGiftDetailRow)cloneDetail[i];
                        AInspectDS.AGiftDetail.ImportRow(gDR2);
                    }
                }

                GiftBatchTDSAccess.SubmitChanges(AInspectDS);

                SubmissionResult = TSubmitChangesResult.scrOK;

                if (GiftTableInDataSet)
                {
                    if (GiftDetailTableInDataSet)
                    {
                        AInspectDS.AGiftDetail.AcceptChanges();
                    }

                    AInspectDS.AGift.AcceptChanges();

                    if (AInspectDS.AGift.Count > 0)
                    {
                        AGiftRow tranR = (AGiftRow)AInspectDS.AGift.Rows[0];

                        Int32 currentLedger = tranR.LedgerNumber;
                        Int32 currentBatch = tranR.BatchNumber;
                        Int32 giftToDelete = 0;

                        try
                        {
                            DataRow[] foundGiftsForDeletion = AInspectDS.AGift.Select(String.Format("{0} = '{1}'",
                                    AGiftTable.GetGiftStatusDBName(),
                                    MFinanceConstants.MARKED_FOR_DELETION));

                            if (foundGiftsForDeletion.Length > 0)
                            {
                                AGiftRow giftRowClient = null;

                                for (int i = 0; i < foundGiftsForDeletion.Length; i++)
                                {
                                    //A gift has been deleted
                                    giftRowClient = (AGiftRow)foundGiftsForDeletion[i];

                                    giftToDelete = giftRowClient.GiftTransactionNumber;
                                    TLogging.Log(String.Format("Gift to Delete: {0} from Batch: {1}",
                                            giftToDelete,
                                            currentBatch));

                                    giftRowClient.Delete();
                                }
                            }

                            GiftBatchTDSAccess.SubmitChanges(AInspectDS);

                            SubmissionResult = TSubmitChangesResult.scrOK;
                        }
                        catch (Exception ex)
                        {
                            TLogging.Log(String.Format("Method:{0} - Unexpected error trying to save gift batch!{1}{1}{2}",
                                    Utilities.GetMethodSignature(),
                                    Environment.NewLine,
                                    ex.Message));
                            throw ex;
                        }
                    }
                }
            }

            return SubmissionResult;
        }
        /// <summary>
        /// Method to cancel a specified batch
        /// </summary>
        /// <param name="ABatchRowToCancel"></param>
        /// <returns></returns>
        public bool CancelBatch(AGiftBatchRow ABatchRowToCancel)
        {
            bool CancellationSuccessful = false;

            if ((ABatchRowToCancel == null) || (ABatchRowToCancel.BatchStatus != MFinanceConstants.BATCH_UNPOSTED))
            {
                return(false);
            }

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

            string CancelMessage     = string.Empty;
            string CompletionMessage = string.Empty;

            List <string> ModifiedDetailKeys = new List <string>();

            CancelMessage = String.Format(Catalog.GetString("Are you sure you want to cancel Gift Batch {0}?"),
                                          CurrentBatchNo);

            if ((MessageBox.Show(CancelMessage,
                                 "Cancel Gift Batch",
                                 MessageBoxButtons.YesNo,
                                 MessageBoxIcon.Question,
                                 MessageBoxDefaultButton.Button2) != System.Windows.Forms.DialogResult.Yes))
            {
                return(false);
            }

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

            try
            {
                FMyForm.Cursor = Cursors.WaitCursor;

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

                //Don't run an inactive fields check on this batch
                FMyForm.GetBatchControl().UpdateUnpostedBatchDictionary(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.CANCELLING, !CurrentBatchTransactionsLoadedAndCurrent))
                {
                    FMyForm.GetBatchControl().UpdateUnpostedBatchDictionary();

                    CompletionMessage = String.Format(Catalog.GetString("Gift Batch {0} has not been cancelled."),
                                                      CurrentBatchNo);

                    MessageBox.Show(CompletionMessage,
                                    "Gift Batch Cancelled",
                                    MessageBoxButtons.OK,
                                    MessageBoxIcon.Information);

                    return(false);
                }

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

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

                //Delete transactions
                FMyForm.GetTransactionsControl().DeleteBatchGiftData(CurrentBatchNo, ref ModifiedDetailKeys);

                //Batch is only cancelled and never deleted
                ABatchRowToCancel.BeginEdit();
                ABatchRowToCancel.BatchTotal     = 0;
                ABatchRowToCancel.LastGiftNumber = 0;
                ABatchRowToCancel.BatchStatus    = MFinanceConstants.BATCH_CANCELLED;
                ABatchRowToCancel.EndEdit();

                if (ModifiedDetailKeys.Count > 0)
                {
                    TRemote.MFinance.Gift.WebConnectors.ReversedGiftReset(FLedgerNumber, ModifiedDetailKeys);
                }

                FPetraUtilsObject.SetChangedFlag();
                FMyForm.SaveChangesManual();

                CompletionMessage = String.Format(Catalog.GetString("Batch no.: {0} cancelled successfully."),
                                                  CurrentBatchNo);

                MessageBox.Show(CompletionMessage,
                                "Gift Batch Cancelled",
                                MessageBoxButtons.OK,
                                MessageBoxIcon.Information);

                FMyForm.DisableTransactions();

                CancellationSuccessful = 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(CancellationSuccessful);
        }
        /// <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);
        }