/// <summary> /// Shows the edit details. /// </summary> /// <param name="txn">The TXN.</param> private void ShowEditDetails(FinancialTransaction txn, RockContext rockContext) { this.Page.ClientScript.RegisterStartupScript( this.GetType(), "StartupScript", @"Sys.Application.add_load(function () { // if the person picker is empty then open it for quick entry var personPicker = $('.js-authorizedperson'); var currentPerson = personPicker.find('.picker-selectedperson').html(); if (currentPerson != null && currentPerson.length == 0) { $(personPicker).find('a.picker-label').trigger('click'); } });", true); if (txn != null) { BindDropdowns(rockContext); hfTransactionId.Value = txn.Id.ToString(); SetHeadingInfo(txn); SetEditMode(true); if (txn.AuthorizedPersonAlias != null) { ppAuthorizedPerson.SetValue(txn.AuthorizedPersonAlias.Person); } else { ppAuthorizedPerson.SetValue(null); } dtTransactionDateTime.SelectedDateTime = txn.TransactionDateTime; ddlTransactionType.SetValue(txn.TransactionTypeValueId); ddlSourceType.SetValue(txn.SourceTypeValueId); cpPaymentGateway.SetValue(txn.GatewayEntityType != null ? txn.GatewayEntityType.Guid.ToString().ToUpper() : string.Empty); tbTransactionCode.Text = txn.TransactionCode; ddlCurrencyType.SetValue(txn.CurrencyTypeValueId); ddlCreditCardType.SetValue(txn.CreditCardTypeValueId); SetCreditCardVisibility(); TransactionDetailsState = txn.TransactionDetails.ToList(); TransactionImagesState = txn.Images.OrderBy(i => i.Order).Select(i => i.BinaryFileId).ToList(); if (TransactionDetailsState.Count() == 1) { UseSimpleAccountMode = true; } else { UseSimpleAccountMode = false; } BindAccounts(); tbSummary.Text = txn.Summary; BindImages(); } }
/// <summary> /// Handles the Click event of the lbShowMore control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> protected void lbShowMore_Click(object sender, EventArgs e) { if (TransactionDetailsState.Count() == 1) { TransactionDetailsState.First().Amount = tbSingleAccountAmount.Text.AsDecimal(); UseSimpleAccountMode = false; BindAccounts(); } }
/// <summary> /// Binds the transaction details. /// </summary> private void BindAccounts() { if (UseSimpleAccountMode && TransactionDetailsState.Count() == 1) { var txnDetail = TransactionDetailsState.First(); tbSingleAccountAmount.Label = AccountName(txnDetail.AccountId); tbSingleAccountAmount.Text = txnDetail.Amount.ToString("N2"); } else { var accounts = TransactionDetailsState.ToList(); accounts.Add(new FinancialTransactionDetail { AccountId = int.MinValue, Amount = TransactionDetailsState.Sum(d => (decimal?)d.Amount) ?? 0.0M }); gAccountsEdit.DataSource = accounts; gAccountsEdit.DataBind(); } }
/// <summary> /// Handles the Click event of the lbSave control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> protected void lbSave_Click(object sender, EventArgs e) { var rockContext = new RockContext(); var txnService = new FinancialTransactionService(rockContext); var txnDetailService = new FinancialTransactionDetailService(rockContext); var txnImageService = new FinancialTransactionImageService(rockContext); var binaryFileService = new BinaryFileService(rockContext); FinancialTransaction txn = null; int?txnId = hfTransactionId.Value.AsIntegerOrNull(); int?batchId = hfBatchId.Value.AsIntegerOrNull(); if (txnId.HasValue) { txn = txnService.Get(txnId.Value); } if (txn == null) { txn = new FinancialTransaction(); txnService.Add(txn); txn.BatchId = batchId; } if (txn != null) { if (ppAuthorizedPerson.PersonId.HasValue) { txn.AuthorizedPersonAliasId = ppAuthorizedPerson.PersonAliasId; } txn.TransactionDateTime = dtTransactionDateTime.SelectedDateTime; txn.TransactionTypeValueId = ddlTransactionType.SelectedValue.AsInteger(); txn.SourceTypeValueId = ddlSourceType.SelectedValueAsInt(); Guid?gatewayGuid = cpPaymentGateway.SelectedValueAsGuid(); if (gatewayGuid.HasValue) { var gatewayEntity = EntityTypeCache.Read(gatewayGuid.Value); if (gatewayEntity != null) { txn.GatewayEntityTypeId = gatewayEntity.Id; } else { txn.GatewayEntityTypeId = null; } } else { txn.GatewayEntityTypeId = null; } txn.TransactionCode = tbTransactionCode.Text; txn.CurrencyTypeValueId = ddlCurrencyType.SelectedValueAsInt(); txn.CreditCardTypeValueId = ddlCreditCardType.SelectedValueAsInt(); txn.Summary = tbSummary.Text; if (!Page.IsValid || !txn.IsValid) { return; } foreach (var txnDetail in TransactionDetailsState) { if (!txnDetail.IsValid) { return; } } rockContext.WrapTransaction(() => { // Save the transaction rockContext.SaveChanges(); // Delete any transaction details that were removed var txnDetailsInDB = txnDetailService.Queryable().Where(a => a.TransactionId.Equals(txn.Id)).ToList(); var deletedDetails = from txnDetail in txnDetailsInDB where !TransactionDetailsState.Select(d => d.Guid).Contains(txnDetail.Guid) select txnDetail; deletedDetails.ToList().ForEach(txnDetail => { txnDetailService.Delete(txnDetail); }); rockContext.SaveChanges(); // Save Transaction Details foreach (var editorTxnDetail in TransactionDetailsState) { // Add or Update the activity type var txnDetail = txn.TransactionDetails.FirstOrDefault(d => d.Guid.Equals(editorTxnDetail.Guid)); if (txnDetail == null) { txnDetail = new FinancialTransactionDetail(); txnDetail.Guid = editorTxnDetail.Guid; txn.TransactionDetails.Add(txnDetail); } txnDetail.AccountId = editorTxnDetail.AccountId; txnDetail.Amount = UseSimpleAccountMode ? tbSingleAccountAmount.Text.AsDecimal() : editorTxnDetail.Amount; txnDetail.Summary = editorTxnDetail.Summary; } rockContext.SaveChanges(); // Delete any transaction images that were removed var orphanedBinaryFileIds = new List <int>(); var txnImagesInDB = txnImageService.Queryable().Where(a => a.TransactionId.Equals(txn.Id)).ToList(); foreach (var txnImage in txnImagesInDB.Where(i => !TransactionImagesState.Contains(i.BinaryFileId))) { orphanedBinaryFileIds.Add(txnImage.BinaryFileId); txnImageService.Delete(txnImage); } // Save Transaction Images int imageOrder = 0; foreach (var binaryFileId in TransactionImagesState) { // Add or Update the activity type var txnImage = txnImagesInDB.FirstOrDefault(i => i.BinaryFileId == binaryFileId); if (txnImage == null) { txnImage = new FinancialTransactionImage(); txnImage.TransactionId = txn.Id; txn.Images.Add(txnImage); } txnImage.BinaryFileId = binaryFileId; txnImage.Order = imageOrder; imageOrder++; } rockContext.SaveChanges(); // Make sure updated binary files are not temporary foreach (var binaryFile in binaryFileService.Queryable().Where(f => TransactionImagesState.Contains(f.Id))) { binaryFile.IsTemporary = false; } // Delete any orphaned images foreach (var binaryFile in binaryFileService.Queryable().Where(f => orphanedBinaryFileIds.Contains(f.Id))) { binaryFileService.Delete(binaryFile); } rockContext.SaveChanges(); }); // Save selected options to session state in order to prefill values for next added txn Session["NewTxnDefault_BatchId"] = txn.BatchId; Session["NewTxnDefault_TransactionDateTime"] = txn.TransactionDateTime; Session["NewTxnDefault_TransactionType"] = txn.TransactionTypeValueId; Session["NewTxnDefault_SourceType"] = txn.SourceTypeValueId; Session["NewTxnDefault_CurrencyType"] = txn.CurrencyTypeValueId; Session["NewTxnDefault_CreditCardType"] = txn.CreditCardTypeValueId; if (TransactionDetailsState.Count() == 1) { Session["NewTxnDefault_Account"] = TransactionDetailsState.First().AccountId; } else { Session.Remove("NewTxnDefault_Account"); } // Requery the batch to support EF navigation properties var savedTxn = GetTransaction(txn.Id); ShowReadOnlyDetails(savedTxn); } }