Esempio n. 1
0
        private List <JournalEntryLine> GetGlEntries(FinancialBatch financialBatch, ref string debugLava, string DescriptionLava = "")
        {
            if (string.IsNullOrWhiteSpace(DescriptionLava))
            {
                DescriptionLava = "{{ Batch.Id }}: {{ Batch.Name }}";
            }

            var rockContext = new RockContext();

            //
            // Group/Sum Transactions by Account and Project since Project can come from Account or Transaction Details
            //
            List <RegistrationInstance> registrationLinks;
            List <GroupMember>          groupMemberLinks;
            var batchTransactionsSummary = TransactionHelpers.GetTransactionSummary(financialBatch, rockContext, out registrationLinks, out groupMemberLinks);

            //
            // Get the Dimensions from the Account since the Transaction Details have been Grouped already
            //
            var customDimensions = TransactionHelpers.GetCustomDimensions();
            var batchSummary     = new List <GLBatchTotals>();

            foreach (var summary in batchTransactionsSummary)
            {
                var account = new FinancialAccountService(rockContext).Get(summary.FinancialAccountId);
                var customDimensionValues = new Dictionary <string, dynamic>();
                account.LoadAttributes();
                var mergeFieldObjects = new MergeFieldObjects
                {
                    Account          = account,
                    Batch            = financialBatch,
                    Registrations    = registrationLinks,
                    GroupMembers     = groupMemberLinks,
                    Summary          = summary,
                    CustomDimensions = customDimensions
                };
                Dictionary <string, object> mergeFields = TransactionHelpers.GetMergeFieldsAndDimensions(ref debugLava, customDimensionValues, mergeFieldObjects);

                var batchSummaryItem = new GLBatchTotals()
                {
                    Amount                = summary.Amount,
                    CreditAccount         = account.GetAttributeValue("rocks.kfs.Intacct.ACCOUNTNO"),
                    DebitAccount          = account.GetAttributeValue("rocks.kfs.Intacct.DEBITACCOUNTNO"),
                    TransactionFeeAmount  = summary.TransactionFeeAmount,
                    TransactionFeeAccount = summary.TransactionFeeAccount,
                    Class                  = account.GetAttributeValue("rocks.kfs.Intacct.CLASSID"),
                    Department             = account.GetAttributeValue("rocks.kfs.Intacct.DEPARTMENT"),
                    Location               = account.GetAttributeValue("rocks.kfs.Intacct.LOCATION"),
                    Project                = summary.Project,
                    Description            = DescriptionLava.ResolveMergeFields(mergeFields),
                    CustomDimensions       = customDimensionValues,
                    ProcessTransactionFees = summary.ProcessTransactionFees
                };

                batchSummary.Add(batchSummaryItem);
            }

            return(GenerateLineItems(batchSummary));
        }
Esempio n. 2
0
        private List <JournalEntryLine> GetGlEntries(RockContext rockContext, FinancialBatch financialBatch, string journalCode, int period, ref string debugLava, string DescriptionLava = "")
        {
            if (string.IsNullOrWhiteSpace(DescriptionLava))
            {
                DescriptionLava = "{{ Batch.Id }}: {{ Batch.Name }}";
            }
            //
            // Group/Sum Transactions by Account and Project since Project can come from Account or Transaction Details
            //
            var batchTransactions = new List <GLTransaction>();
            var registrationLinks = new List <RegistrationInstance>();
            var groupMemberLinks  = new List <GroupMember>();

            foreach (var transaction in financialBatch.Transactions)
            {
                transaction.LoadAttributes();
                foreach (var transactionDetail in transaction.TransactionDetails)
                {
                    transactionDetail.LoadAttributes();
                    transactionDetail.Account.LoadAttributes();

                    var detailProject      = transactionDetail.GetAttributeValue("rocks.kfs.ShelbyFinancials.Project").AsGuidOrNull();
                    var transactionProject = transaction.GetAttributeValue("rocks.kfs.ShelbyFinancials.Project").AsGuidOrNull();
                    var accountProject     = transactionDetail.Account.GetAttributeValue("rocks.kfs.ShelbyFinancials.Project").AsGuidOrNull();

                    var projectCode = string.Empty;
                    if (detailProject != null)
                    {
                        projectCode = DefinedValueCache.Get(( Guid )detailProject).Value;
                    }
                    else if (transactionProject != null)
                    {
                        projectCode = DefinedValueCache.Get(( Guid )transactionProject).Value;
                    }
                    else if (accountProject != null)
                    {
                        projectCode = DefinedValueCache.Get(( Guid )accountProject).Value;
                    }

                    if (transactionDetail.EntityTypeId.HasValue)
                    {
                        var registrationEntityType = EntityTypeCache.Get(typeof(Rock.Model.Registration));
                        var groupMemberEntityType  = EntityTypeCache.Get(typeof(Rock.Model.GroupMember));

                        if (transactionDetail.EntityId.HasValue && transactionDetail.EntityTypeId == registrationEntityType.Id)
                        {
                            foreach (var registration in new RegistrationService(rockContext)
                                     .Queryable().AsNoTracking()
                                     .Where(r =>
                                            r.RegistrationInstance != null &&
                                            r.RegistrationInstance.RegistrationTemplate != null &&
                                            r.Id == transactionDetail.EntityId))
                            {
                                registrationLinks.Add(registration.RegistrationInstance);
                            }
                        }
                        if (transactionDetail.EntityId.HasValue && transactionDetail.EntityTypeId == groupMemberEntityType.Id)
                        {
                            foreach (var groupMember in new GroupMemberService(rockContext)
                                     .Queryable().AsNoTracking()
                                     .Where(gm =>
                                            gm.Group != null &&
                                            gm.Id == transactionDetail.EntityId))
                            {
                                groupMemberLinks.Add(groupMember);
                            }
                        }
                    }

                    var transactionItem = new GLTransaction()
                    {
                        Amount             = transactionDetail.Amount,
                        FinancialAccountId = transactionDetail.AccountId,
                        Project            = projectCode
                    };

                    batchTransactions.Add(transactionItem);
                }
            }

            var batchTransactionsSummary = batchTransactions
                                           .GroupBy(d => new { d.FinancialAccountId, d.Project })
                                           .Select(s => new GLTransaction
            {
                FinancialAccountId = s.Key.FinancialAccountId,
                Project            = s.Key.Project,
                Amount             = s.Sum(f => ( decimal? )f.Amount) ?? 0.0M
            })
                                           .ToList();

            var batchSummary = new List <GLBatchTotals>();

            foreach (var summary in batchTransactionsSummary)
            {
                var account = new FinancialAccountService(rockContext).Get(summary.FinancialAccountId);
                account.LoadAttributes();

                var mergeFields = new Dictionary <string, object>();
                mergeFields.Add("Account", account);
                mergeFields.Add("Summary", summary);
                mergeFields.Add("Batch", financialBatch);
                mergeFields.Add("Registrations", registrationLinks);
                mergeFields.Add("GroupMembers", groupMemberLinks);
                mergeFields.Add("JournalCode", journalCode);
                mergeFields.Add("Period", period);

                var batchSummaryItem = new GLBatchTotals()
                {
                    CompanyNumber       = account.GetAttributeValue("rocks.kfs.ShelbyFinancials.Company"),
                    RegionNumber        = account.GetAttributeValue("rocks.kfs.ShelbyFinancials.Region"),
                    SuperFundNumber     = account.GetAttributeValue("rocks.kfs.ShelbyFinancials.SuperFund"),
                    FundNumber          = account.GetAttributeValue("rocks.kfs.ShelbyFinancials.Fund"),
                    LocationNumber      = account.GetAttributeValue("rocks.kfs.ShelbyFinancials.Location"),
                    CostCenterNumber    = account.GetAttributeValue("rocks.kfs.ShelbyFinancials.CostCenter"),
                    DepartmentNumber    = account.GetAttributeValue("rocks.kfs.ShelbyFinancials.Department"),
                    CreditAccountNumber = account.GetAttributeValue("rocks.kfs.ShelbyFinancials.CreditAccount"),
                    DebitAccountNumber  = account.GetAttributeValue("rocks.kfs.ShelbyFinancials.DebitAccount"),
                    AccountSub          = account.GetAttributeValue("rocks.kfs.ShelbyFinancials.AccountSub"),
                    Amount             = summary.Amount,
                    Project            = summary.Project,
                    JournalNumber      = financialBatch.Id,
                    JournalDescription = DescriptionLava.ResolveMergeFields(mergeFields),
                    Date = financialBatch.BatchStartDateTime ?? RockDateTime.Now,
                    Note = financialBatch.Note
                };

                if (debugLava.Length < 6 && debugLava.AsBoolean())
                {
                    debugLava = mergeFields.lavaDebugInfo();
                }

                batchSummary.Add(batchSummaryItem);
            }

            return(GenerateLineItems(batchSummary));
        }
        private OtherReceipt BuildOtherReceipt(FinancialBatch financialBatch, ref string debugLava, PaymentMethod paymentMethod, string bankAccountId = null, string unDepGLAccountId = null, string DescriptionLava = "")
        {
            if (string.IsNullOrWhiteSpace(DescriptionLava))
            {
                DescriptionLava = "{{ Batch.Id }}: {{ Batch.Name }}";
            }

            var rockContext = new RockContext();

            var batchDate    = financialBatch.BatchStartDateTime == null ? RockDateTime.Now : ((System.DateTime)financialBatch.BatchStartDateTime);
            var otherReceipt = new OtherReceipt
            {
                Payer            = "Rock Batch Import",
                PaymentDate      = batchDate,
                ReceivedDate     = batchDate,
                PaymentMethod    = paymentMethod,
                BankAccountId    = bankAccountId,
                UnDepGLAccountNo = unDepGLAccountId,
                DepositDate      = batchDate,
                Description      = string.Format("Imported From Rock batch {0}: {1}", financialBatch.Id, financialBatch.Name),
                RefId            = financialBatch.Id.ToString(),
                ReceiptItems     = new List <ReceiptLineItem>()
            };
            List <RegistrationInstance> registrationLinks;
            List <GroupMember>          groupMemberLinks;
            var receiptTransactions = TransactionHelpers.GetTransactionSummary(financialBatch, rockContext, out registrationLinks, out groupMemberLinks);

            //
            // Get the Dimensions from the Account since the Transaction Details have been Grouped already
            //
            var customDimensions = TransactionHelpers.GetCustomDimensions();

            // Create Receipt Item for each entry within a grouping
            foreach (var bTran in receiptTransactions)
            {
                var account = new FinancialAccountService(rockContext).Get(bTran.FinancialAccountId);
                var customDimensionValues = new Dictionary <string, dynamic>();
                account.LoadAttributes();
                var mergeFieldObjects = new MergeFieldObjects
                {
                    Account          = account,
                    Batch            = financialBatch,
                    Registrations    = registrationLinks,
                    GroupMembers     = groupMemberLinks,
                    Summary          = bTran,
                    CustomDimensions = customDimensions
                };
                Dictionary <string, object> mergeFields = TransactionHelpers.GetMergeFieldsAndDimensions(ref debugLava, customDimensionValues, mergeFieldObjects);

                var classId      = account.GetAttributeValue("rocks.kfs.Intacct.CLASSID");
                var departmentId = account.GetAttributeValue("rocks.kfs.Intacct.DEPARTMENT");
                var locationId   = account.GetAttributeValue("rocks.kfs.Intacct.LOCATION");

                var receiptItem = new ReceiptLineItem
                {
                    GlAccountNo  = account.GetAttributeValue("rocks.kfs.Intacct.ACCOUNTNO"),
                    Amount       = bTran.ProcessTransactionFees == 1 ? bTran.Amount - bTran.TransactionFeeAmount : bTran.Amount,
                    Memo         = DescriptionLava.ResolveMergeFields(mergeFields),
                    LocationId   = locationId,
                    DepartmentId = departmentId,
                    ProjectId    = bTran.Project,
                    ClassId      = classId,
                    CustomFields = customDimensionValues
                };
                otherReceipt.ReceiptItems.Add(receiptItem);

                if (bTran.ProcessTransactionFees == 2)
                {
                    var feeLineItem = new ReceiptLineItem
                    {
                        GlAccountNo  = bTran.TransactionFeeAccount,
                        Amount       = bTran.TransactionFeeAmount * -1,
                        Memo         = "Transaction Fees",
                        LocationId   = locationId,
                        DepartmentId = departmentId,
                        ProjectId    = bTran.Project,
                        ClassId      = classId
                    };
                    otherReceipt.ReceiptItems.Add(feeLineItem);
                }
            }

            return(otherReceipt);
        }