/// create a new gift batch using some of the details of an existing gift batch private static AGiftBatchRow CreateNewGiftBatch( Int32 ALedgerNumber, Int32 ABatchNumber, DateTime ADateEffective, GiftAdjustmentFunctionEnum AFunction, ref GiftBatchTDS AMainDS, ref ALedgerTable ALedgerTable, TDBTransaction ATransaction) { AGiftBatchRow ReturnValue; AGiftBatchAccess.LoadByPrimaryKey(AMainDS, ALedgerNumber, ABatchNumber, ATransaction); AGiftBatchRow oldGiftBatch = AMainDS.AGiftBatch[0]; TGiftBatchFunctions.CreateANewGiftBatchRow(ref AMainDS, ref ATransaction, ref ALedgerTable, ALedgerNumber, ADateEffective); ReturnValue = AMainDS.AGiftBatch[1]; ReturnValue.BankAccountCode = oldGiftBatch.BankAccountCode; ReturnValue.BankCostCentre = oldGiftBatch.BankCostCentre; ReturnValue.CurrencyCode = oldGiftBatch.CurrencyCode; ReturnValue.ExchangeRateToBase = oldGiftBatch.ExchangeRateToBase; ReturnValue.MethodOfPaymentCode = oldGiftBatch.MethodOfPaymentCode; ReturnValue.HashTotal = 0; if (ReturnValue.MethodOfPaymentCode.Length == 0) { ReturnValue.SetMethodOfPaymentCodeNull(); } ReturnValue.BankCostCentre = oldGiftBatch.BankCostCentre; ReturnValue.GiftType = oldGiftBatch.GiftType; if (AFunction.Equals(GiftAdjustmentFunctionEnum.AdjustGift)) { ReturnValue.BatchDescription = Catalog.GetString("Gift Adjustment"); } else if (AFunction.Equals(GiftAdjustmentFunctionEnum.AdjustGiftBatch)) { ReturnValue.BatchDescription = Catalog.GetString("Gift Batch Adjustment"); } else if (AFunction.Equals(GiftAdjustmentFunctionEnum.FieldAdjust)) { ReturnValue.BatchDescription = Catalog.GetString("Gift Adjustment (Field Change)"); } else if (AFunction.Equals(GiftAdjustmentFunctionEnum.TaxDeductiblePctAdjust)) { ReturnValue.BatchDescription = Catalog.GetString("Gift Adjustment (Tax Deductible Pct Change)"); } else { ReturnValue.BatchDescription = Catalog.GetString("Reverse Gift"); } return(ReturnValue); }
public static bool GiftRevertAdjust(Hashtable requestParams, out TVerificationResultCollection AMessages) { AMessages = new TVerificationResultCollection(); Int32 ALedgerNumber = (Int32)requestParams["ALedgerNumber"]; Boolean batchSelected = (Boolean)requestParams["NewBatchSelected"]; Int32 ANewBatchNumber = 0; bool TaxDeductiblePercentageEnabled = Convert.ToBoolean( TSystemDefaults.GetSystemDefault(SharedConstants.SYSDEFAULT_TAXDEDUCTIBLEPERCENTAGE, "FALSE")); if (batchSelected) { ANewBatchNumber = (Int32)requestParams["NewBatchNumber"]; } String Function = (String)requestParams["Function"]; Int32 AGiftDetailNumber = (Int32)requestParams["GiftDetailNumber"]; Int32 AGiftNumber = (Int32)requestParams["GiftNumber"]; Int32 ABatchNumber = (Int32)requestParams["BatchNumber"]; //decimal batchHashTotal = 0; decimal batchGiftTotal = 0; GiftBatchTDS MainDS = new GiftBatchTDS(); TDBTransaction Transaction = null; DateTime ADateEffective; Transaction = DBAccess.GDBAccessObj.BeginTransaction(IsolationLevel.Serializable); try { ALedgerTable LedgerTable = ALedgerAccess.LoadByPrimaryKey(ALedgerNumber, Transaction); AGiftBatchRow giftBatch; if (!batchSelected) { ADateEffective = (DateTime)requestParams["GlEffectiveDate"]; AGiftBatchAccess.LoadByPrimaryKey(MainDS, ALedgerNumber, ABatchNumber, Transaction); AGiftBatchRow oldGiftBatch = MainDS.AGiftBatch[0]; TGiftBatchFunctions.CreateANewGiftBatchRow(ref MainDS, ref Transaction, ref LedgerTable, ALedgerNumber, ADateEffective); giftBatch = MainDS.AGiftBatch[1]; giftBatch.BankAccountCode = oldGiftBatch.BankAccountCode; giftBatch.BankCostCentre = oldGiftBatch.BankCostCentre; giftBatch.CurrencyCode = oldGiftBatch.CurrencyCode; giftBatch.ExchangeRateToBase = oldGiftBatch.ExchangeRateToBase; giftBatch.MethodOfPaymentCode = oldGiftBatch.MethodOfPaymentCode; giftBatch.HashTotal = 0; if (giftBatch.MethodOfPaymentCode.Length == 0) { giftBatch.SetMethodOfPaymentCodeNull(); } giftBatch.BankCostCentre = oldGiftBatch.BankCostCentre; giftBatch.GiftType = oldGiftBatch.GiftType; if (Function.Equals("AdjustGift")) { giftBatch.BatchDescription = Catalog.GetString("Gift Adjustment"); } else { giftBatch.BatchDescription = Catalog.GetString("Reverse Gift"); } } else { AGiftBatchAccess.LoadByPrimaryKey(MainDS, ALedgerNumber, ANewBatchNumber, Transaction); giftBatch = MainDS.AGiftBatch[0]; ADateEffective = giftBatch.GlEffectiveDate; //If into an existing batch, then retrive the existing batch total batchGiftTotal = giftBatch.BatchTotal; } if (Function.Equals("ReverseGiftBatch")) { AGiftAccess.LoadViaAGiftBatch(MainDS, ALedgerNumber, ABatchNumber, Transaction); foreach (AGiftRow gift in MainDS.AGift.Rows) { AGiftDetailAccess.LoadViaAGift(MainDS, ALedgerNumber, ABatchNumber, gift.GiftTransactionNumber, Transaction); } } else { AGiftAccess.LoadByPrimaryKey(MainDS, ALedgerNumber, ABatchNumber, AGiftNumber, Transaction); if (Function.Equals("ReverseGiftDetail")) { AGiftDetailAccess.LoadByPrimaryKey(MainDS, ALedgerNumber, ABatchNumber, AGiftNumber, AGiftDetailNumber, Transaction); } else { AGiftDetailAccess.LoadViaAGift(MainDS, ALedgerNumber, ABatchNumber, AGiftNumber, Transaction); } } //assuming new elements are added after these static borders int cycle = 0; MainDS.AGift.DefaultView.Sort = string.Format("{0}, {1}", AGiftTable.GetBatchNumberDBName(), AGiftTable.GetGiftTransactionNumberDBName()); MainDS.AGiftDetail.DefaultView.Sort = string.Format("{0}, {1}, {2}", AGiftDetailTable.GetBatchNumberDBName(), AGiftDetailTable.GetGiftTransactionNumberDBName(), AGiftDetailTable.GetDetailNumberDBName()); do { foreach (DataRowView giftRow in MainDS.AGift.DefaultView) { AGiftRow oldGift = (AGiftRow)giftRow.Row; if ((oldGift.BatchNumber == ABatchNumber) && (oldGift.LedgerNumber == ALedgerNumber) && (Function.Equals("ReverseGiftBatch") || (oldGift.GiftTransactionNumber == AGiftNumber))) { AGiftRow gift = MainDS.AGift.NewRowTyped(true); DataUtilities.CopyAllColumnValuesWithoutPK(oldGift, gift); gift.LedgerNumber = giftBatch.LedgerNumber; gift.BatchNumber = giftBatch.BatchNumber; gift.DateEntered = ADateEffective; gift.GiftTransactionNumber = giftBatch.LastGiftNumber + 1; giftBatch.LastGiftNumber++; gift.LastDetailNumber = 0; MainDS.AGift.Rows.Add(gift); foreach (DataRowView giftDetailRow in MainDS.AGiftDetail.DefaultView) { AGiftDetailRow oldGiftDetail = (AGiftDetailRow)giftDetailRow.Row; if ((oldGiftDetail.GiftTransactionNumber == oldGift.GiftTransactionNumber) && (oldGiftDetail.BatchNumber == ABatchNumber) && (oldGiftDetail.LedgerNumber == ALedgerNumber) && (!Function.Equals("ReverseGiftDetail") || (oldGiftDetail.DetailNumber == AGiftDetailNumber))) { if ((cycle == 0) && oldGiftDetail.ModifiedDetail) { AMessages.Add(new TVerificationResult( String.Format(Catalog.GetString("Cannot reverse or adjust Gift {0} with Detail {1} in Batch {2}"), oldGiftDetail.GiftTransactionNumber, oldGiftDetail.DetailNumber, oldGiftDetail.BatchNumber), String.Format(Catalog.GetString("It was already adjusted or reversed.")), TResultSeverity.Resv_Critical)); DBAccess.GDBAccessObj.RollbackTransaction(); return(false); } AGiftDetailRow giftDetail = MainDS.AGiftDetail.NewRowTyped(true); DataUtilities.CopyAllColumnValuesWithoutPK(oldGiftDetail, giftDetail); giftDetail.DetailNumber = ++gift.LastDetailNumber; giftDetail.LedgerNumber = gift.LedgerNumber; giftDetail.BatchNumber = giftBatch.BatchNumber; giftDetail.GiftTransactionNumber = gift.GiftTransactionNumber; //Identify the reversal source giftDetail.ModifiedDetailKey = "|" + oldGiftDetail.BatchNumber.ToString() + "|" + oldGiftDetail.GiftTransactionNumber.ToString() + "|" + oldGiftDetail.DetailNumber.ToString(); decimal signum = (cycle == 0) ? -1 : 1; giftDetail.GiftTransactionAmount = signum * oldGiftDetail.GiftTransactionAmount; batchGiftTotal += giftDetail.GiftTransactionAmount; giftDetail.GiftAmount = signum * oldGiftDetail.GiftAmount; giftDetail.GiftAmountIntl = signum * oldGiftDetail.GiftAmountIntl; if (TaxDeductiblePercentageEnabled) { giftDetail.TaxDeductibleAmount = signum * oldGiftDetail.TaxDeductibleAmount; giftDetail.TaxDeductibleAmountBase = signum * oldGiftDetail.TaxDeductibleAmountBase; giftDetail.TaxDeductibleAmountIntl = signum * oldGiftDetail.TaxDeductibleAmountIntl; giftDetail.NonDeductibleAmount = signum * oldGiftDetail.NonDeductibleAmount; giftDetail.NonDeductibleAmountBase = signum * oldGiftDetail.NonDeductibleAmountBase; giftDetail.NonDeductibleAmountIntl = signum * oldGiftDetail.NonDeductibleAmountIntl; } giftDetail.GiftCommentOne = (String)requestParams["ReversalCommentOne"]; giftDetail.GiftCommentTwo = (String)requestParams["ReversalCommentTwo"]; giftDetail.GiftCommentThree = (String)requestParams["ReversalCommentThree"]; giftDetail.CommentOneType = (String)requestParams["ReversalCommentOneType"]; giftDetail.CommentTwoType = (String)requestParams["ReversalCommentTwoType"]; giftDetail.CommentThreeType = (String)requestParams["ReversalCommentThreeType"]; // This is used to mark both as a Reverted giftDetails, except the adjusted (new) gift giftDetail.ModifiedDetail = (cycle == 0); oldGiftDetail.ModifiedDetail = (cycle == 0); MainDS.AGiftDetail.Rows.Add(giftDetail); } } } } cycle++; } while ((cycle < 2) && Function.Equals("AdjustGift")); //When reversing into a new or existing batch, set batch total if (!Function.Equals("AdjustGift")) { giftBatch.BatchTotal = batchGiftTotal; } // save everything at the end AGiftBatchAccess.SubmitChanges(MainDS.AGiftBatch, Transaction); ALedgerAccess.SubmitChanges(LedgerTable, Transaction); AGiftAccess.SubmitChanges(MainDS.AGift, Transaction); AGiftDetailAccess.SubmitChanges(MainDS.AGiftDetail, Transaction); MainDS.AGiftBatch.AcceptChanges(); DBAccess.GDBAccessObj.CommitTransaction(); return(true); } catch (Exception Exc) { TLogging.Log("An Exception occured while performing Gift Reverse/Adjust:" + Environment.NewLine + Exc.ToString()); DBAccess.GDBAccessObj.RollbackTransaction(); throw new EOPAppException(Catalog.GetString("Gift Reverse/Adjust failed."), Exc); } }
private static GiftBatchTDS CreateGiftBatches(SortedList <DateTime, List <XmlNode> > AGiftsPerDate, int APeriodNumber) { GiftBatchTDS MainDS = new GiftBatchTDS(); ALedgerTable LedgerTable = null; TDBTransaction ReadTransaction = new TDBTransaction(); DBAccess.ReadTransaction(ref ReadTransaction, delegate { TDataBase db = ReadTransaction.DataBaseObj; // get a list of potential donors (all class FAMILY) string sqlGetFamilyPartnerKeys = "SELECT p_partner_key_n FROM PUB_p_family"; DataTable FamilyKeys = db.SelectDT(sqlGetFamilyPartnerKeys, "keys", ReadTransaction); // get a list of workers (all class FAMILY, with special type WORKER) string sqlGetWorkerPartnerKeys = "SELECT PUB_p_family.p_partner_key_n FROM PUB_p_family, PUB_p_partner_type WHERE PUB_p_partner_type.p_partner_key_n = PUB_p_family.p_partner_key_n AND p_type_code_c = 'WORKER'"; DataTable WorkerKeys = db.SelectDT(sqlGetWorkerPartnerKeys, "keys", ReadTransaction); // get a list of fields (all class UNIT, with unit type F) string sqlGetFieldPartnerKeys = String.Format( "SELECT U.p_partner_key_n FROM PUB_p_unit U WHERE u_unit_type_code_c = 'F' AND EXISTS (SELECT * FROM PUB_a_valid_ledger_number V WHERE V.a_ledger_number_i = {0} AND V.p_partner_key_n = U.p_partner_key_n)", FLedgerNumber); DataTable FieldKeys = db.SelectDT(sqlGetFieldPartnerKeys, "keys", ReadTransaction); // get a list of key ministries (all class UNIT, with unit type KEY-MIN), and their field ledger number and cost centre code string sqlGetKeyMinPartnerKeys = "SELECT u.p_partner_key_n, us.um_parent_unit_key_n, vl.a_cost_centre_code_c " + "FROM PUB_p_unit u, PUB_um_unit_structure us, PUB_a_valid_ledger_number vl " + "WHERE u.u_unit_type_code_c = 'KEY-MIN' " + "AND us.um_child_unit_key_n = u.p_partner_key_n " + "AND vl.p_partner_key_n = us.um_parent_unit_key_n " + "AND vl.a_ledger_number_i = " + FLedgerNumber.ToString(); DataTable KeyMinistries = db.SelectDT(sqlGetKeyMinPartnerKeys, "keys", ReadTransaction); LedgerTable = ALedgerAccess.LoadByPrimaryKey(FLedgerNumber, ReadTransaction); AAccountingPeriodRow AccountingPeriodRow = AAccountingPeriodAccess.LoadByPrimaryKey(FLedgerNumber, APeriodNumber, ReadTransaction)[0]; // create a gift batch for each day. // TODO: could create one batch per month, if there are not so many gifts (less than 100 per month) foreach (DateTime GlEffectiveDate in AGiftsPerDate.Keys) { if ((GlEffectiveDate.CompareTo(AccountingPeriodRow.PeriodStartDate) < 0) || (GlEffectiveDate.CompareTo(AccountingPeriodRow.PeriodEndDate) > 0)) { // only create gifts in that period continue; } AGiftBatchRow giftBatch = TGiftBatchFunctions.CreateANewGiftBatchRow(ref MainDS, ref ReadTransaction, ref LedgerTable, FLedgerNumber, GlEffectiveDate); TLogging.LogAtLevel(1, "create gift batch for " + GlEffectiveDate.ToShortDateString()); giftBatch.BatchDescription = "Benerator Batch for " + GlEffectiveDate.ToShortDateString(); giftBatch.BatchTotal = 0.0m; foreach (XmlNode RecordNode in AGiftsPerDate[GlEffectiveDate]) { AGiftRow gift = MainDS.AGift.NewRowTyped(); gift.LedgerNumber = giftBatch.LedgerNumber; gift.BatchNumber = giftBatch.BatchNumber; gift.GiftTransactionNumber = giftBatch.LastGiftNumber + 1; gift.DateEntered = GlEffectiveDate; // set donorKey int donorID = Convert.ToInt32(TXMLParser.GetAttribute(RecordNode, "donor")) % FamilyKeys.Rows.Count; gift.DonorKey = Convert.ToInt64(FamilyKeys.Rows[donorID].ItemArray[0]); // calculate gift detail information int countDetails = Convert.ToInt32(TXMLParser.GetAttribute(RecordNode, "splitgift")); for (int counter = 1; counter <= countDetails; counter++) { AGiftDetailRow giftDetail = MainDS.AGiftDetail.NewRowTyped(); giftDetail.LedgerNumber = gift.LedgerNumber; giftDetail.BatchNumber = gift.BatchNumber; giftDetail.GiftTransactionNumber = gift.GiftTransactionNumber; giftDetail.MotivationGroupCode = "GIFT"; giftDetail.GiftTransactionAmount = Convert.ToDecimal(TXMLParser.GetAttribute(RecordNode, "amount_" + counter.ToString())); giftDetail.GiftAmount = giftDetail.GiftTransactionAmount; giftBatch.BatchTotal += giftDetail.GiftAmount; string motivation = TXMLParser.GetAttribute(RecordNode, "motivation_" + counter.ToString()); if (motivation == "SUPPORT") { if (WorkerKeys.Rows.Count == 0) { continue; } giftDetail.MotivationDetailCode = "SUPPORT"; int recipientID = Convert.ToInt32(TXMLParser.GetAttribute(RecordNode, "recipient_support_" + counter.ToString())) % WorkerKeys.Rows.Count; giftDetail.RecipientKey = Convert.ToInt64(WorkerKeys.Rows[recipientID].ItemArray[0]); giftDetail.RecipientLedgerNumber = TGiftTransactionWebConnector.GetRecipientFundNumber(giftDetail.RecipientKey, giftBatch.GlEffectiveDate); // ignore this gift detail, if there is no valid commitment period for the worker if (giftDetail.RecipientLedgerNumber == 0) { continue; } } else if (motivation == "FIELD") { if (FieldKeys.Rows.Count == 0) { continue; } giftDetail.MotivationDetailCode = "FIELD"; int recipientID = Convert.ToInt32(TXMLParser.GetAttribute(RecordNode, "recipient_field_" + counter.ToString())) % FieldKeys.Rows.Count; giftDetail.RecipientKey = Convert.ToInt64(FieldKeys.Rows[recipientID].ItemArray[0]); giftDetail.RecipientLedgerNumber = giftDetail.RecipientKey; giftDetail.CostCentreCode = (giftDetail.RecipientKey / 10000).ToString("0000"); } else if (motivation == "KEYMIN") { if (KeyMinistries.Rows.Count == 0) { continue; } giftDetail.MotivationDetailCode = "KEYMIN"; int recipientID = Convert.ToInt32(TXMLParser.GetAttribute(RecordNode, "recipient_keymin_" + counter.ToString())) % KeyMinistries.Rows.Count; giftDetail.RecipientKey = Convert.ToInt64(KeyMinistries.Rows[recipientID].ItemArray[0]); giftDetail.RecipientLedgerNumber = Convert.ToInt64(KeyMinistries.Rows[recipientID].ItemArray[1]); // TTransactionWebConnector.GetRecipientFundNumber(giftDetail.RecipientKey); giftDetail.CostCentreCode = KeyMinistries.Rows[recipientID].ItemArray[2].ToString(); // TTransactionWebConnector.IdentifyPartnerCostCentre(FLedgerNumber, giftDetail.RecipientLedgerNumber); } giftDetail.DetailNumber = gift.LastDetailNumber + 1; MainDS.AGiftDetail.Rows.Add(giftDetail); gift.LastDetailNumber = giftDetail.DetailNumber; } if (gift.LastDetailNumber > 0) { MainDS.AGift.Rows.Add(gift); giftBatch.LastGiftNumber = gift.GiftTransactionNumber; } if (giftBatch.LastGiftNumber >= MaxGiftsPerBatch) { break; } } if (TLogging.DebugLevel > 0) { TLogging.Log( GlEffectiveDate.ToShortDateString() + " " + giftBatch.LastGiftNumber.ToString()); } } }); // need to save the last gift batch number in a_ledger if (LedgerTable != null) { TDBTransaction WriteTransaction = new TDBTransaction(); bool SubmissionOk = false; DBAccess.WriteTransaction(ref WriteTransaction, ref SubmissionOk, delegate { ALedgerAccess.SubmitChanges(LedgerTable, WriteTransaction); SubmissionOk = true; }); if (!SubmissionOk) { TLogging.Log("An Exception occured during the creation of Gift Batches" + Environment.NewLine); } } return(MainDS); }
public static void InitRecurringGiftBatchForSponsorship(Int32 ALedgerNumber, string ABankAccountCode) { TDataBase db = DBAccess.Connect("InitRecurringGiftBatchForSponsorship"); TDBTransaction Transaction = new TDBTransaction(); Int32 BatchNumber = -1; string CurrencyCode = String.Empty; GiftBatchTDS GiftDS = new GiftBatchTDS(); DBAccess.ReadTransaction(ref Transaction, delegate { BatchNumber = GetRecurringGiftBatchForSponsorship(ALedgerNumber, Transaction); if ((BatchNumber == -1) && (ABankAccountCode == String.Empty)) { // bank account is not defined: use the first active bank account by default. // we don't support foreign currency accounts at the moment string sql = "SELECT MAX(a.a_account_code_c) FROM a_account AS a, a_account_property AS p " + "WHERE a.a_ledger_number_i = p.a_ledger_number_i " + "AND a.a_account_code_c = p.a_account_code_c " + "AND a.a_ledger_number_i = " + ALedgerNumber.ToString() + " " + "AND p.a_property_code_c = '" + MFinanceConstants.ACCOUNT_PROPERTY_BANK_ACCOUNT + "' " + "AND p.a_property_value_c = 'true' " + "AND a.a_foreign_currency_flag_l = false"; object result = Transaction.DataBaseObj.ExecuteScalar(sql, Transaction); // if no bank account exists, the result will be System.DBNull if (!(result is System.DBNull)) { ABankAccountCode = result.ToString(); } } if (CurrencyCode == String.Empty) { string sql = "SELECT a_base_currency_c FROM a_ledger WHERE a_ledger_number_i = " + ALedgerNumber.ToString(); object result = Transaction.DataBaseObj.ExecuteScalar(sql, Transaction); // if no ledger exists, the result will be System.DBNull if (!(result is System.DBNull)) { CurrencyCode = result.ToString(); } } TGiftBatchFunctions.CreateANewRecurringGiftBatchRow(ref GiftDS, ref Transaction, ALedgerNumber); }); if (BatchNumber != -1) { return; } ARecurringGiftBatchRow b = GiftDS.ARecurringGiftBatch[0]; b.BankAccountCode = ABankAccountCode; b.BatchDescription = BATCHNAME_SPONSORSHIP; b.CurrencyCode = CurrencyCode; GiftBatchTDSAccess.SubmitChanges(GiftDS, db); db.CloseDBConnection(); }