public static bool MaintainSponsorshipRecurringGifts( Int32 ALedgerNumber, Int32 ABatchNumber, Int32 AGiftTransactionNumber, Int32 ADetailNumber, Int64 ARecipientKey, Int64 ADonorKey, String AMotivationGroupCode, String AMotivationDetailCode, decimal AGiftAmount, DateTime AStartDonations, DateTime?AEndDonations, out TVerificationResultCollection AVerificationResult) { AVerificationResult = new TVerificationResultCollection(); if (ADonorKey <= 0) { AVerificationResult.Add(new TVerificationResult("error", "Please specify the donor", "", "MaintainChildren.ErrMissingDonor", TResultSeverity.Resv_Critical)); } if (AGiftAmount <= 0) { AVerificationResult.Add(new TVerificationResult("error", "Please specify a valid amount", "", "MaintainChildren.ErrMissingAmount", TResultSeverity.Resv_Critical)); } if ((AMotivationDetailCode == "") || (AMotivationDetailCode == null)) { AVerificationResult.Add(new TVerificationResult("error", "Please specify the motivation", "", "MaintainChildren.ErrMissingMotivation", TResultSeverity.Resv_Critical)); } if (AVerificationResult.HasCriticalErrors) { return(false); } TDBTransaction Transaction = new TDBTransaction(); SponsorshipTDS MainDS = new SponsorshipTDS(); TDataBase DB = DBAccess.Connect("MaintainRecurringGifts"); bool MotivationExists = false; // load batches and their transactions based on their id / batch number DB.ReadTransaction(ref Transaction, delegate { // we overwrite the user input, since the user can't really send the right batch number on create ABatchNumber = GetRecurringGiftBatchForSponsorship(ALedgerNumber, Transaction); ARecurringGiftBatchAccess.LoadByPrimaryKey(MainDS, ALedgerNumber, ABatchNumber, Transaction); ARecurringGiftAccess.LoadViaARecurringGiftBatch(MainDS, ALedgerNumber, ABatchNumber, Transaction); MotivationExists = AMotivationDetailAccess.Exists(ALedgerNumber, AMotivationGroupCode, AMotivationDetailCode, Transaction); }); if (!MotivationExists) { AVerificationResult.Add(new TVerificationResult("error", "Please specify a valid motivation", "", "MaintainChildren.ErrMissingMotivation", TResultSeverity.Resv_Critical)); return(false); } // try to get a row with requested id, aka edit else make a new ARecurringGiftRow EditGiftRow = null; foreach (ARecurringGiftRow CheckGiftRow in MainDS.ARecurringGift.Rows) { if (CheckGiftRow.GiftTransactionNumber == AGiftTransactionNumber) { EditGiftRow = CheckGiftRow; break; } } // we did not find a Transaction in this Batch, so we create one if (EditGiftRow == null) { // TODO: we could look for a gift transaction of the same donor??? EditGiftRow = MainDS.ARecurringGift.NewRowTyped(true); EditGiftRow.DonorKey = ADonorKey; EditGiftRow.BatchNumber = ABatchNumber; EditGiftRow.LedgerNumber = ALedgerNumber; EditGiftRow.GiftTransactionNumber = MainDS.ARecurringGiftBatch[0].LastGiftNumber + 1; MainDS.ARecurringGiftBatch[0].LastGiftNumber++; MainDS.ARecurringGift.Rows.Add(EditGiftRow); } // load stuff based on the current edit row DB.ReadTransaction(ref Transaction, delegate { ARecurringGiftDetailAccess.LoadViaARecurringGift(MainDS, ALedgerNumber, ABatchNumber, EditGiftRow.GiftTransactionNumber, Transaction); }); DB.CloseDBConnection(); // try to get a row with requested id, aka edit else make a new ARecurringGiftDetailRow EditGiftDetailRow = null; foreach (ARecurringGiftDetailRow CheckGiftDetailRow in MainDS.ARecurringGiftDetail.Rows) { if (CheckGiftDetailRow.DetailNumber == ADetailNumber) { EditGiftDetailRow = CheckGiftDetailRow; break; } } // none found, make one if (EditGiftDetailRow == null) { EditGiftDetailRow = MainDS.ARecurringGiftDetail.NewRowTyped(true); EditGiftDetailRow.LedgerNumber = ALedgerNumber; EditGiftDetailRow.BatchNumber = ABatchNumber; EditGiftDetailRow.RecipientKey = ARecipientKey; EditGiftDetailRow.GiftTransactionNumber = EditGiftRow.GiftTransactionNumber; EditGiftDetailRow.DetailNumber = EditGiftRow.LastDetailNumber + 1; EditGiftRow.LastDetailNumber++; MainDS.ARecurringGiftDetail.Rows.Add(EditGiftDetailRow); } EditGiftRow.DonorKey = ADonorKey; EditGiftDetailRow.GiftAmount = AGiftAmount; EditGiftDetailRow.MotivationGroupCode = AMotivationGroupCode; EditGiftDetailRow.MotivationDetailCode = AMotivationDetailCode; EditGiftDetailRow.EndDonations = AEndDonations; EditGiftDetailRow.StartDonations = AStartDonations; try { SponsorshipTDSAccess.SubmitChanges(MainDS); return(true); } catch (Exception e) { TLogging.Log(e.ToString()); AVerificationResult.Add(new TVerificationResult("error", e.Message, TResultSeverity.Resv_Critical)); return(false); } }
public static bool DeleteSponsorshipRecurringGift( Int32 ALedgerNumber, Int32 ABatchNumber, Int32 AGiftTransactionNumber, Int32 ADetailNumber, out TVerificationResultCollection AVerificationResult) { AVerificationResult = new TVerificationResultCollection(); TDBTransaction Transaction = new TDBTransaction(); SponsorshipTDS MainDS = new SponsorshipTDS(); TDataBase DB = DBAccess.Connect("DeleteRecurringGift"); // load batches and their transactions based on their id / batch number DB.ReadTransaction(ref Transaction, delegate { ARecurringGiftBatchAccess.LoadByPrimaryKey(MainDS, ALedgerNumber, ABatchNumber, Transaction); ARecurringGiftAccess.LoadViaARecurringGiftBatch(MainDS, ALedgerNumber, ABatchNumber, Transaction); ARecurringGiftDetailAccess.LoadViaARecurringGiftBatch(MainDS, ALedgerNumber, ABatchNumber, Transaction); }); try { bool LastDetail = true; ARecurringGiftDetailRow DetailToDelete = null; ARecurringGiftRow GiftToDelete = null; foreach (ARecurringGiftDetailRow CheckGiftDetailRow in MainDS.ARecurringGiftDetail.Rows) { if (CheckGiftDetailRow.GiftTransactionNumber == AGiftTransactionNumber) { if (CheckGiftDetailRow.DetailNumber == ADetailNumber) { DetailToDelete = CheckGiftDetailRow; DetailToDelete.DetailNumber = 20000; } else { LastDetail = false; if (CheckGiftDetailRow.DetailNumber > ADetailNumber) { CheckGiftDetailRow.DetailNumber--; } } } } foreach (ARecurringGiftRow CheckGiftRow in MainDS.ARecurringGift.Rows) { if (CheckGiftRow.GiftTransactionNumber == AGiftTransactionNumber) { if (LastDetail) { GiftToDelete = CheckGiftRow; } else { CheckGiftRow.LastDetailNumber--; } } } // delete the detail if (DetailToDelete != null) { DetailToDelete.Delete(); } // delete the gift if (GiftToDelete != null) { // we keep the gift transaction numbers, and live with gaps. GiftToDelete.Delete(); } SponsorshipTDSAccess.SubmitChanges(MainDS, DB); return(true); } catch (Exception e) { TLogging.Log(e.ToString()); AVerificationResult.Add(new TVerificationResult("error", e.Message, TResultSeverity.Resv_Critical)); return(false); } finally { DB.CloseDBConnection(); } }
public static SponsorshipTDS GetChildDetails(Int64 APartnerKey, Int32 ALedgerNumber, bool AWithPhoto, out string ASponsorshipStatus) { SponsorshipTDS MainDS = new SponsorshipTDS(); TDBTransaction Transaction = new TDBTransaction(); DBAccess.ReadTransaction(ref Transaction, delegate { PPartnerAccess.LoadByPrimaryKey(MainDS, APartnerKey, Transaction); PFamilyAccess.LoadByPrimaryKey(MainDS, APartnerKey, Transaction); PPartnerTypeAccess.LoadViaPPartner(MainDS, APartnerKey, Transaction); PPartnerCommentAccess.LoadViaPPartner(MainDS, APartnerKey, Transaction); PPartnerReminderAccess.LoadViaPPartner(MainDS, APartnerKey, Transaction); if (!AWithPhoto && (MainDS.PFamily.Rows.Count == 1)) { MainDS.PFamily[0].Photo = ""; } int SponsorshipBatchNumber = GetRecurringGiftBatchForSponsorship(ALedgerNumber, Transaction); if (SponsorshipBatchNumber > -1) { ARecurringGiftBatchAccess.LoadByPrimaryKey(MainDS, ALedgerNumber, SponsorshipBatchNumber, Transaction); ARecurringGiftAccess.LoadViaARecurringGiftBatch(MainDS, ALedgerNumber, SponsorshipBatchNumber, Transaction); ARecurringGiftDetailAccess.LoadViaARecurringGiftBatch(MainDS, ALedgerNumber, SponsorshipBatchNumber, Transaction); GiftBatchTDS GiftDS = new GiftBatchTDS(); TGiftTransactionWebConnector.LoadGiftDonorRelatedData(GiftDS, true, ALedgerNumber, SponsorshipBatchNumber, Transaction); for (int i = 0; i < MainDS.ARecurringGiftDetail.Count;) { SponsorshipTDSARecurringGiftDetailRow gdr = MainDS.ARecurringGiftDetail[i]; // drop all recurring gift details, that are not related to this child (RecipientKey) if (gdr.RecipientKey != APartnerKey) { MainDS.ARecurringGiftDetail.Rows.RemoveAt(i); } else { i++; // set the donor key from the appropriate recurring gift MainDS.ARecurringGift.DefaultView.RowFilter = String.Format("{0} = {1}", ARecurringGiftTable.GetGiftTransactionNumberDBName(), gdr.GiftTransactionNumber); // there should be only one row foreach (DataRowView drv in MainDS.ARecurringGift.DefaultView) { ARecurringGiftRow recurrGiftRow = (ARecurringGiftRow)drv.Row; gdr.DonorKey = recurrGiftRow.DonorKey; PPartnerRow donorRow = (PPartnerRow)GiftDS.DonorPartners.Rows.Find(recurrGiftRow.DonorKey); gdr.DonorName = donorRow.PartnerShortName; gdr.CurrencyCode = MainDS.ARecurringGiftBatch[0].CurrencyCode; } } } // drop all unrelated gift rows, that don't have a detail for this child for (int i = 0; i < MainDS.ARecurringGift.Count;) { ARecurringGiftRow gr = MainDS.ARecurringGift[0]; MainDS.ARecurringGiftDetail.DefaultView.RowFilter = String.Format("{0} = {1}", ARecurringGiftDetailTable.GetGiftTransactionNumberDBName(), gr.GiftTransactionNumber); if (MainDS.ARecurringGiftDetail.DefaultView.Count == 0) { MainDS.ARecurringGift.Rows.RemoveAt(i); } else { i++; } } } }); bool isSponsoredChild = false; ASponsorshipStatus = "[N/A]"; foreach (PPartnerTypeRow type in MainDS.PPartnerType.Rows) { if (type.TypeCode == "CHILDREN_HOME" || type.TypeCode == "HOME_BASED" || type.TypeCode == "BOARDING_SCHOOL" || type.TypeCode == "PREVIOUS_CHILD" || type.TypeCode == "CHILD_DIED") { isSponsoredChild = true; } ASponsorshipStatus = type.TypeCode; } if (!isSponsoredChild) { return(new SponsorshipTDS()); } return(MainDS); }