public static GiftBatchTDSAGiftDetailTable LoadDonorLastGift(Int64 ADonorPartnerKey, Int32 ALedgerNumber) { #region Validate Arguments if (ADonorPartnerKey < 0) { throw new ArgumentException(String.Format(Catalog.GetString( "Function:{0} - The Donor Partnerkey cannot be a negative number!"), Utilities.GetMethodName(true))); } #endregion Validate Arguments GiftBatchTDSAGiftDetailTable LastGiftData = new GiftBatchTDSAGiftDetailTable(); TDBTransaction Transaction = null; DBAccess.GDBAccessObj.GetNewOrExistingAutoReadTransaction(IsolationLevel.ReadCommitted, ref Transaction, delegate { // load latest gift from donor string Query = "SELECT Gift.*" + " FROM a_gift AS Gift" + " WHERE Gift.a_ledger_number_i = " + ALedgerNumber + " AND Gift.p_donor_key_n = " + ADonorPartnerKey + " AND Gift.a_date_entered_d =" + "(SELECT MAX(a_gift.a_date_entered_d)" + " FROM a_gift" + " WHERE a_gift.a_ledger_number_i = " + ALedgerNumber + " AND a_gift.p_donor_key_n = " + ADonorPartnerKey + ")"; DataTable GiftTable = DBAccess.GDBAccessObj.SelectDT(Query, AGiftTable.GetTableDBName(), Transaction); if ((GiftTable == null) || (GiftTable.Rows.Count == 0)) { return; } DataRow GiftRow = GiftTable.Rows[0]; // load gift details for the latest gift Query = "SELECT a_gift_detail.*, p_partner.p_partner_short_name_c AS RecipientDescription" + " FROM a_gift_detail, p_partner" + " WHERE a_gift_detail.a_ledger_number_i = " + ALedgerNumber + " AND a_gift_detail.a_batch_number_i = " + GiftRow[AGiftTable.GetBatchNumberDBName()] + " AND a_gift_detail.a_gift_transaction_number_i = " + GiftRow[AGiftTable.GetGiftTransactionNumberDBName()] + " AND a_gift_detail.p_recipient_key_n = p_partner.p_partner_key_n"; DBAccess.GDBAccessObj.SelectDT(LastGiftData, Query, Transaction); }); LastGiftData.AcceptChanges(); return LastGiftData; }
/// <summary> /// auto populate new gift recipient info using the donor's last gift /// </summary> /// <param name="ADonorKey"></param> /// <param name="APartnerShortName"></param> /// <param name="AGiftTransactionNumber"></param> private void AutoPopulateGiftDetail(Int64 ADonorKey, String APartnerShortName, Int32 AGiftTransactionNumber) { FAutoPopulatingGiftInProcess = true; bool IsSplitGift = false; DateTime LatestUnpostedGiftDateEntered = new DateTime(1900, 1, 1); try { //Check for Donor in loaded gift batches // and record most recent date entered AGiftTable DonorRecentGiftsTable = new AGiftTable(); GiftBatchTDSAGiftDetailTable GiftDetailTable = new GiftBatchTDSAGiftDetailTable(); AGiftRow MostRecentLoadedGiftForDonorRow = null; DataView giftDV = new DataView(FMainDS.AGift); giftDV.RowStateFilter = DataViewRowState.CurrentRows; giftDV.RowFilter = string.Format("{0}={1} And Not ({2}={3} And {4}={5})", AGiftTable.GetDonorKeyDBName(), ADonorKey, AGiftTable.GetBatchNumberDBName(), FBatchNumber, AGiftTable.GetGiftTransactionNumberDBName(), AGiftTransactionNumber); giftDV.Sort = String.Format("{0} DESC, {1} DESC", AGiftTable.GetDateEnteredDBName(), AGiftTable.GetGiftTransactionNumberDBName()); if (giftDV.Count > 0) { //Take first row = most recent date entered value MostRecentLoadedGiftForDonorRow = (AGiftRow)giftDV[0].Row; LatestUnpostedGiftDateEntered = MostRecentLoadedGiftForDonorRow.DateEntered; DonorRecentGiftsTable.Rows.Add((object[])MostRecentLoadedGiftForDonorRow.ItemArray.Clone()); } //Check for even more recent saved gifts on server (i.e. not necessarily loaded) GiftDetailTable = TRemote.MFinance.Gift.WebConnectors.LoadDonorLastPostedGift(ADonorKey, FLedgerNumber, LatestUnpostedGiftDateEntered); if (((GiftDetailTable != null) && (GiftDetailTable.Count > 0))) { //UnLoaded/Saved gift from donor is more recent IsSplitGift = (GiftDetailTable.Count > 1); } else if (MostRecentLoadedGiftForDonorRow != null) { //Loaded/unsaved gift from donor is more recent DataView giftDetailDV = new DataView(FMainDS.AGiftDetail); giftDetailDV.RowStateFilter = DataViewRowState.CurrentRows; giftDetailDV.RowFilter = string.Format("{0}={1} And {2}={3}", AGiftDetailTable.GetBatchNumberDBName(), MostRecentLoadedGiftForDonorRow.BatchNumber, AGiftDetailTable.GetGiftTransactionNumberDBName(), MostRecentLoadedGiftForDonorRow.GiftTransactionNumber); foreach (DataRowView drv in giftDetailDV) { GiftBatchTDSAGiftDetailRow gDR = (GiftBatchTDSAGiftDetailRow)drv.Row; GiftDetailTable.Rows.Add((object[])gDR.ItemArray.Clone()); } IsSplitGift = (GiftDetailTable.Count > 1); } else { //nothing to autocopy return; } // if the last gift was a split gift (multiple details) then ask the user if they would like this new gift to also be split if (IsSplitGift) { GiftDetailTable.DefaultView.Sort = GiftBatchTDSAGiftDetailTable.GetDetailNumberDBName() + " ASC"; string Message = string.Format(Catalog.GetString( "The last gift from this donor was a split gift.{0}{0}Here are the details:{0}"), "\n"); int DetailNumber = 1; foreach (DataRowView dvr in GiftDetailTable.DefaultView) { GiftBatchTDSAGiftDetailRow Row = (GiftBatchTDSAGiftDetailRow)dvr.Row; Message += DetailNumber + ") "; if (Row.RecipientKey > 0) { Message += string.Format(Catalog.GetString("Recipient: {0} ({1}); Motivation Group: {2}; Motivation Detail: {3}; Amount: {4}"), Row.RecipientDescription, Row.RecipientKey, Row.MotivationGroupCode, Row.MotivationDetailCode, StringHelper.FormatUsingCurrencyCode(Row.GiftTransactionAmount, GetBatchRow().CurrencyCode) + " " + FBatchRow.CurrencyCode) + "\n"; } DetailNumber++; } Message += "\n" + Catalog.GetString("Do you want to create the same split gift again?"); if (!(MessageBox.Show(Message, Catalog.GetString( "Create Split Gift"), MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)) { if (cmbDetailMethodOfGivingCode.CanFocus) { cmbDetailMethodOfGivingCode.Focus(); } else if (txtDetailReference.CanFocus) { txtDetailReference.Focus(); } return; } } this.Cursor = Cursors.WaitCursor; GiftBatchTDSAGiftDetailRow giftDetailRow = (GiftBatchTDSAGiftDetailRow)GiftDetailTable.DefaultView[0].Row; // Handle first row, which is FPreviouslySelectedDetailRow txtDetailDonorKey.Text = String.Format("{0:0000000000}", ADonorKey); txtDetailRecipientKey.Text = String.Format("{0:0000000000}", giftDetailRow.RecipientKey); cmbDetailMotivationGroupCode.SetSelectedString(giftDetailRow.MotivationGroupCode); txtDetailMotivationDetailCode.Text = giftDetailRow.MotivationDetailCode; cmbMotivationDetailCode.SetSelectedString(giftDetailRow.MotivationDetailCode); chkDetailConfidentialGiftFlag.Checked = giftDetailRow.ConfidentialGiftFlag; // Change #5481 chkDetailChargeFlag.Checked = true; cmbDetailMethodOfPaymentCode.SetSelectedString(FBatchMethodOfPayment, -1); cmbDetailMethodOfGivingCode.SetSelectedString(giftDetailRow.MethodOfGivingCode, -1); //Handle mailing code if (FSETAutoCopyIncludeMailingCodeFlag) { cmbDetailMailingCode.SetSelectedString(giftDetailRow.MailingCode, -1); } else { cmbDetailMailingCode.SelectedIndex = -1; } //Copy the comments and comment types if required if (FSETAutoCopyIncludeCommentsFlag) { txtDetailGiftCommentOne.Text = giftDetailRow.GiftCommentOne; cmbDetailCommentOneType.SetSelectedString(giftDetailRow.CommentOneType); txtDetailGiftCommentTwo.Text = giftDetailRow.GiftCommentTwo; cmbDetailCommentTwoType.SetSelectedString(giftDetailRow.CommentTwoType); txtDetailGiftCommentThree.Text = giftDetailRow.GiftCommentThree; cmbDetailCommentThreeType.SetSelectedString(giftDetailRow.CommentThreeType); } //Handle tax fields on current row if (FSETUseTaxDeductiblePercentageFlag) { bool taxDeductible = (giftDetailRow.IsTaxDeductibleNull() ? true : giftDetailRow.TaxDeductible); giftDetailRow.TaxDeductible = taxDeductible; try { FPetraUtilsObject.SuppressChangeDetection = true; chkDetailTaxDeductible.Checked = taxDeductible; EnableTaxDeductibilityPct(taxDeductible); } finally { FPetraUtilsObject.SuppressChangeDetection = false; } if (!IsSplitGift) { //Most commonly not a split gift (?) if (!taxDeductible) { txtDeductiblePercentage.NumberValueDecimal = 0.0m; } txtTaxDeductAmount.NumberValueDecimal = 0.0m; txtNonDeductAmount.NumberValueDecimal = 0.0m; } else { if (taxDeductible) { //In case the tax percentage has changed or null values in amount fields ReconcileTaxDeductibleAmounts(giftDetailRow); } else { //Changing this will update the unbound amount textboxes txtDeductiblePercentage.NumberValueDecimal = 0.0m; } } } //Process values that are not bound to a control giftDetailRow.ReceiptPrinted = false; giftDetailRow.ReceiptNumber = 0; //Now process other gift details if they exist if (IsSplitGift) { //Only copy amount to first row if copying split gifts txtDetailGiftTransactionAmount.NumberValueDecimal = giftDetailRow.GiftTransactionAmount; // clear previous validation errors. // otherwise we get an error if the user has changed the control immediately after changing the donor key. FPetraUtilsObject.VerificationResultCollection.Clear(); bool SelectEndRow = (FBatchRow.LastGiftNumber == FPreviouslySelectedDetailRow.GiftTransactionNumber); //Just retain other details to add giftDetailRow.Delete(); GiftDetailTable.AcceptChanges(); foreach (DataRowView drv in GiftDetailTable.DefaultView) { GiftBatchTDSAGiftDetailRow detailRow = (GiftBatchTDSAGiftDetailRow)drv.Row; //______________________ //Update basic field values detailRow.LedgerNumber = FLedgerNumber; detailRow.BatchNumber = FBatchNumber; detailRow.GiftTransactionNumber = AGiftTransactionNumber; detailRow.DetailNumber = ++FGift.LastDetailNumber; detailRow.DonorName = APartnerShortName; detailRow.DonorClass = FPreviouslySelectedDetailRow.DonorClass; detailRow.DateEntered = FGift.DateEntered; detailRow.MethodOfPaymentCode = FPreviouslySelectedDetailRow.MethodOfPaymentCode; detailRow.ReceiptPrinted = false; detailRow.ReceiptNumber = 0; // Change #5481 detailRow.ChargeFlag = true; if (!FSETAutoCopyIncludeMailingCodeFlag) { detailRow.MailingCode = string.Empty; } //______________________ //process recipient details to get most recent data Int64 partnerKey = detailRow.RecipientKey; string partnerShortName = string.Empty; TPartnerClass partnerClass; bool recipientIsValid = true; if (TServerLookup.TMPartner.GetPartnerShortName(partnerKey, out partnerShortName, out partnerClass)) { detailRow.RecipientDescription = partnerShortName; detailRow.RecipientClass = partnerClass.ToString(); if (partnerClass == TPartnerClass.FAMILY) { detailRow.RecipientLedgerNumber = TRemote.MFinance.Gift.WebConnectors.GetRecipientFundNumber(partnerKey, FGift.DateEntered); detailRow.RecipientField = detailRow.RecipientLedgerNumber; detailRow.RecipientKeyMinistry = string.Empty; } else { //Class - UNIT Int64 field; string keyMinName; recipientIsValid = TFinanceControls.GetRecipientKeyMinData(partnerKey, out field, out keyMinName); detailRow.RecipientLedgerNumber = field; detailRow.RecipientField = field; detailRow.RecipientKeyMinistry = keyMinName; if (!recipientIsValid) { string msg = String.Format(Catalog.GetString( "Gift: {0}, Detail: {1} has a recipient: '{2}-{3}' that is an inactive Key Ministry!"), AGiftTransactionNumber, detailRow.DetailNumber, partnerKey, keyMinName); MessageBox.Show(msg, Catalog.GetString( "Copying Previous Split Gift"), MessageBoxButtons.OK, MessageBoxIcon.Warning); } } } else { recipientIsValid = false; string msg = String.Format(Catalog.GetString("Gift: {0}, Detail: {1} has an invalid Recipient key: '{2}'!"), AGiftTransactionNumber, detailRow.DetailNumber, partnerKey); MessageBox.Show(msg, Catalog.GetString("Copying Previous Split Gift"), MessageBoxButtons.OK, MessageBoxIcon.Warning); } //______________________ //Process motivation if (string.IsNullOrEmpty(detailRow.MotivationGroupCode)) { detailRow.MotivationGroupCode = string.Empty; string msg = String.Format(Catalog.GetString("Gift: {0}, Detail: {1} has no Motivation Group!"), AGiftTransactionNumber, detailRow.DetailNumber); MessageBox.Show(msg, Catalog.GetString("Copying Previous Split Gift"), MessageBoxButtons.OK, MessageBoxIcon.Warning); } else if (string.IsNullOrEmpty(detailRow.MotivationDetailCode)) { detailRow.MotivationDetailCode = string.Empty; string msg = String.Format(Catalog.GetString("Gift: {0}, Detail: {1} has no Motivation Detail!"), AGiftTransactionNumber, detailRow.DetailNumber); MessageBox.Show(msg, Catalog.GetString("Copying Previous Split Gift"), MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { AMotivationDetailRow motivationDetailRow = null; string motivationGroup = detailRow.MotivationGroupCode; string motivationDetail = detailRow.MotivationDetailCode; motivationDetailRow = (AMotivationDetailRow)FMainDS.AMotivationDetail.Rows.Find( new object[] { FLedgerNumber, motivationGroup, motivationDetail }); if (motivationDetailRow != null) { if (partnerKey > 0) { bool partnerIsMissingLink = false; detailRow.CostCentreCode = TRemote.MFinance.Gift.WebConnectors.RetrieveCostCentreCodeForRecipient(FLedgerNumber, partnerKey, detailRow.RecipientLedgerNumber, detailRow.DateEntered, motivationGroup, motivationDetail, out partnerIsMissingLink); } else { if (motivationGroup != MFinanceConstants.MOTIVATION_GROUP_GIFT) { detailRow.RecipientDescription = motivationGroup; } else { detailRow.RecipientDescription = string.Empty; } detailRow.CostCentreCode = motivationDetailRow.CostCentreCode; } detailRow.AccountCode = motivationDetailRow.AccountCode; if (FSETUseTaxDeductiblePercentageFlag && string.IsNullOrEmpty(motivationDetailRow.TaxDeductibleAccountCode)) { detailRow.TaxDeductibleAccountCode = string.Empty; string msg = String.Format(Catalog.GetString( "Gift: {0}, Detail: {1} has Motivation Detail: {2} which has no Tax Deductible Account!" + "This can be added in Finance / Setup / Motivation Details.{3}{3}" + "Unless this is changed it will be impossible to assign a Tax Deductible Percentage to this gift."), AGiftTransactionNumber, detailRow.DetailNumber, motivationDetail, Environment.NewLine); MessageBox.Show(msg, Catalog.GetString( "Copying Previous Split Gift"), MessageBoxButtons.OK, MessageBoxIcon.Warning); } else { detailRow.TaxDeductibleAccountCode = motivationDetailRow.TaxDeductibleAccountCode; } } else { string msg = String.Format(Catalog.GetString( "Gift: {0}, Detail: {1} has Motivation Group and Detail codes ('{2} : {3}') not found in the database!"), AGiftTransactionNumber, detailRow.DetailNumber, motivationGroup, motivationDetail); MessageBox.Show(msg, Catalog.GetString("Copying Previous Split Gift"), MessageBoxButtons.OK, MessageBoxIcon.Warning); detailRow.TaxDeductible = false; } } //______________________ //Handle tax fields detailRow.TaxDeductiblePct = RetrieveTaxDeductiblePct((recipientIsValid ? detailRow.RecipientKey : 0), detailRow.TaxDeductible); AGiftDetailRow giftDetails = (AGiftDetailRow)detailRow; TaxDeductibility.UpdateTaxDeductibiltyAmounts(ref giftDetails); //______________________ //Process comments if (!FSETAutoCopyIncludeCommentsFlag) { detailRow.SetCommentOneTypeNull(); detailRow.SetCommentTwoTypeNull(); detailRow.SetCommentThreeTypeNull(); detailRow.SetGiftCommentOneNull(); detailRow.SetGiftCommentTwoNull(); detailRow.SetGiftCommentThreeNull(); } detailRow.AcceptChanges(); detailRow.SetAdded(); } //Add in the new records (latter two arguments put in to parallel recurring form) FMainDS.AGiftDetail.Merge(GiftDetailTable, false, MissingSchemaAction.Ignore); int indexOfLatestRow = FMainDS.AGiftDetail.Rows.Count - 1; //Select last row added if (SelectEndRow) { grdDetails.SelectRowInGrid(grdDetails.Rows.Count - 1); } else if (!SelectDetailRowByDataTableIndex(indexOfLatestRow)) { if (!FFilterAndFindObject.IsActiveFilterEqualToBase) { MessageBox.Show( MCommonResourcestrings.StrNewRecordIsFiltered, MCommonResourcestrings.StrAddNewRecordTitle, MessageBoxButtons.OK, MessageBoxIcon.Information); FFilterAndFindObject.FilterPanelControls.ClearAllDiscretionaryFilters(); if (FFilterAndFindObject.FilterFindPanel.ShowApplyFilterButton != TUcoFilterAndFind.FilterContext.None) { FFilterAndFindObject.ApplyFilter(); } SelectDetailRowByDataTableIndex(indexOfLatestRow); } } ClearKeyMinistries(); cmbMotivationDetailCode.Clear(); mniRecipientHistory.Enabled = false; btnDeleteAll.Enabled = btnDelete.Enabled; UpdateRecordNumberDisplay(); FLastDonor = -1; } else { txtDetailDonorKey.FocusTextBoxPartAfterFindScreenCloses = false; txtDetailGiftTransactionAmount.Focus(); } FPetraUtilsObject.SetChangedFlag(); } finally { this.Cursor = Cursors.Default; FAutoPopulatingGiftInProcess = false; } }