Beispiel #1
0
        /// <summary>
        /// Loads the drop downs.
        /// </summary>
        public void LoadDropDowns()
        {
            // get accounts that are both allowed by the BlockSettings and also in the personal AccountList setting
            var rockContext     = new RockContext();
            var accountGuidList = GetAttributeValue("Accounts").SplitDelimitedValues().Select(a => a.AsGuid());

            string keyPrefix = string.Format("transaction-matching-{0}-", this.BlockId);
            var    personalAccountGuidList = (this.GetUserPreference(keyPrefix + "account-list") ?? string.Empty).SplitDelimitedValues().Select(a => a.AsGuid()).ToList();

            var accountQry = new FinancialAccountService(rockContext)
                             .Queryable()
                             .Where(a => a.IsActive);

            // no accounts specified means "all Active"
            if (accountGuidList.Any())
            {
                accountQry = accountQry.Where(a => accountGuidList.Contains(a.Guid));
            }

            // no personal accounts specified means "all(that are allowed in block settings)"
            if (personalAccountGuidList.Any())
            {
                accountQry = accountQry.Where(a => personalAccountGuidList.Contains(a.Guid));
            }

            rptAccounts.DataSource = accountQry.OrderBy(a => a.Order).ThenBy(a => a.Name).ToList();
            rptAccounts.DataBind();
        }
        /// <summary>
        /// Loads the drop downs.
        /// </summary>
        public void LoadDropDowns()
        {
            // get accounts that are both allowed by the BlockSettings and also in the personal AccountList setting
            var rockContext = new RockContext();
            var accountGuidList = GetAttributeValue( "Accounts" ).SplitDelimitedValues().Select( a => a.AsGuid() );

            string keyPrefix = string.Format( "transaction-matching-{0}-", this.BlockId );
            var personalAccountGuidList = ( this.GetUserPreference( keyPrefix + "account-list" ) ?? string.Empty ).SplitDelimitedValues().Select( a => a.AsGuid() ).ToList();

            var accountQry = new FinancialAccountService( rockContext ).Queryable();

            // no accounts specified means "all"
            if ( accountGuidList.Any() )
            {
                accountQry = accountQry.Where( a => accountGuidList.Contains( a.Guid ) );
            }

            // no personal accounts specified means "all(that are allowed in block settings)"
            if ( personalAccountGuidList.Any() )
            {
                accountQry = accountQry.Where( a => personalAccountGuidList.Contains( a.Guid ) );
            }

            rptAccounts.DataSource = accountQry.OrderBy( a => a.Order ).ThenBy( a => a.Name ).ToList();
            rptAccounts.DataBind();
        }
Beispiel #3
0
        /// <summary>
        /// Processes a transaction.
        /// </summary>
        /// <param name="giftElement">The gift element.</param>
        /// <param name="batch">The batch.</param>
        /// <param name="rockContext">The rock context.</param>
        /// <param name="envelopeAttributeId">The envelope attribute identifier.</param>
        /// <param name="errorMessages">The error messages.</param>
        /// <returns></returns>
        private ProcessStatus ProcessTransaction(XElement giftElement, int?batchId, int?envelopeAttributeId, int?binaryFileTypeId, string accountMatchField, out List <string> errorMessages)
        {
            errorMessages = new List <string>();

            // Get batch/sequence number first as they are used in any error messages
            string batchNo = GetChildElementValue(giftElement, "BatchNo");
            string seqNo   = GetChildElementValue(giftElement, "SequenceNo");

            try
            {
                using (var rockContext = new RockContext())
                {
                    // Check to see if accout/routing/check information is specified
                    string encryptedAccountNum = GetChildElementValue(giftElement, "DFIAcct");
                    string encryptedRoutingNum = GetChildElementValue(giftElement, "DFID");
                    string checkNum            = GetChildElementValue(giftElement, "CheckNum");

                    // Start to create the transaction
                    var txn = new FinancialTransaction();
                    txn.BatchId = batchId;
                    txn.TransactionTypeValueId = _transactionTypeContributionId;
                    txn.TransactionDateTime    = GetChildElementValue(giftElement, "GiftDate").AsDateTime();
                    txn.Summary = string.Format("{0}:{1}", batchNo, seqNo);
                    txn.FinancialPaymentDetail = new FinancialPaymentDetail();

                    // Try to find an person to associate with this account
                    int?personAliasId = null;
                    if (!string.IsNullOrWhiteSpace(encryptedAccountNum) && !string.IsNullOrWhiteSpace(encryptedRoutingNum))
                    {
                        // If account/routing information was included, use it to find matching person from Bank Account table
                        // A person will be selected if there is only ONE match found with same information.
                        var accountNum = DecryptAccountInformation(encryptedAccountNum);
                        var routingNum = DecryptAccountInformation(encryptedRoutingNum);

                        string checkMicrHashed = FinancialPersonBankAccount.EncodeAccountNumber(routingNum, accountNum);

                        if (!string.IsNullOrWhiteSpace(checkMicrHashed))
                        {
                            var matchedPersonIds = new FinancialPersonBankAccountService(rockContext)
                                                   .Queryable()
                                                   .Where(a => a.AccountNumberSecured == checkMicrHashed)
                                                   .Select(a => a.PersonAlias.PersonId)
                                                   .Distinct()
                                                   .ToList();

                            if (matchedPersonIds.Count() == 1)
                            {
                                personAliasId = new PersonAliasService(rockContext)
                                                .GetPrimaryAliasId(matchedPersonIds.First());
                            }
                        }

                        txn.MICRStatus     = MICRStatus.Success;
                        txn.CheckMicrParts = Encryption.EncryptString(string.Format("{0}_{1}_{2}", routingNum, accountNum, checkNum));
                        txn.FinancialPaymentDetail.CurrencyTypeValueId = _currencyTypeCheck;
                        txn.TransactionCode = checkNum;
                    }
                    else
                    {
                        // If account/routing number was NOT included, check for an envelope number, and if provided find first
                        // person with same envelope number (unlike account/routing number, this will automatically select first
                        // person when there are multiple people with same envelope number.
                        string envelopeNum = GetChildElementValue(giftElement, "EnvNum");
                        if (!string.IsNullOrWhiteSpace(envelopeNum))
                        {
                            int?personId = new AttributeValueService(rockContext)
                                           .Queryable().AsNoTracking()
                                           .Where(v =>
                                                  v.AttributeId == envelopeAttributeId &&
                                                  v.Value == envelopeNum)
                                           .OrderBy(v => v.EntityId)
                                           .Select(v => v.EntityId)
                                           .FirstOrDefault();

                            if (personId.HasValue)
                            {
                                personAliasId = new PersonAliasService(rockContext)
                                                .GetPrimaryAliasId(personId.Value);
                            }

                            txn.TransactionCode = envelopeNum;
                        }

                        txn.FinancialPaymentDetail.CurrencyTypeValueId = _currencyTypeCash;
                    }
                    txn.AuthorizedPersonAliasId = personAliasId;

                    // Save any images
                    if (binaryFileTypeId.HasValue)
                    {
                        SaveImage(txn, GetChildElementValue(giftElement, "CheckImgFront"), binaryFileTypeId.Value, string.Format("CheckImageFront_{0}:{1}", batchNo, seqNo));
                        SaveImage(txn, GetChildElementValue(giftElement, "CheckImgBack"), binaryFileTypeId.Value, string.Format("CheckImageBack_{0}:{1}", batchNo, seqNo));
                        SaveImage(txn, GetChildElementValue(giftElement, "EnvImgFront"), binaryFileTypeId.Value, string.Format("EnvelopeImageFront_{0}:{1}", batchNo, seqNo));
                        SaveImage(txn, GetChildElementValue(giftElement, "EnvImgBack"), binaryFileTypeId.Value, string.Format("EnvelopeImageBack_{0}:{1}", batchNo, seqNo));
                    }

                    // Loop through the purposes and create the transaction detail (account) records
                    var purposes = giftElement.Element("Purposes");
                    if (purposes != null)
                    {
                        foreach (var purpose in purposes.Descendants().Where(p => p.Name == "Purpose"))
                        {
                            FinancialTransactionDetail txnDetail = null;

                            int?    accountId = GetChildElementValue(purpose, "PurposeID").AsIntegerOrNull();
                            decimal?amount    = GetChildElementValue(purpose, "Amount").AsDecimalOrNull();

                            if (accountId.HasValue && amount.HasValue)
                            {
                                var accountQry = new FinancialAccountService(rockContext).Queryable().AsNoTracking();
                                accountQry = accountMatchField == "FOREIGNID" ?
                                             accountQry.Where(a => a.ForeignId == accountId.Value) :
                                             accountQry.Where(a => a.Id == accountId.Value);
                                int?rockAccountId = accountQry.Select(a => a.Id).FirstOrDefault();
                                if (rockAccountId.HasValue)
                                {
                                    txnDetail           = new FinancialTransactionDetail();
                                    txnDetail.AccountId = rockAccountId.Value;
                                    txnDetail.Amount    = amount.Value;
                                    txn.TransactionDetails.Add(txnDetail);

                                    _totalAmount += amount.Value;
                                }
                            }

                            if (txnDetail == null)
                            {
                                errorMessages.Add(string.Format("Batch: {0}; Sequence: {1}; Error: Invalid Account (PurposeId:{2})", batchNo, seqNo, accountId));
                            }
                        }
                    }

                    if (errorMessages.Any())
                    {
                        return(ProcessStatus.Error);
                    }

                    // Save the transaction and update the batch control amount
                    new FinancialTransactionService(rockContext).Add(txn);
                    rockContext.SaveChanges();

                    return(personAliasId.HasValue ? ProcessStatus.Matched : ProcessStatus.Unmatched);
                }
            }

            catch (Exception ex)
            {
                errorMessages.Add(string.Format("Batch: {0}; Sequence: {1}; Error: {2}", batchNo, seqNo, ex.Message));
                return(ProcessStatus.Error);
            }
        }