Ejemplo n.º 1
0
        public static FinancialAccountRows ForOrganization(Organization organization, DateTime fromDate,
                                                           DateTime toDate)
        {
            FinancialAccounts orgAccounts = FinancialAccounts.ForOrganization(organization);

            orgAccounts.GetRows(fromDate, toDate);
            return
                (FromArray(SwarmDb.GetDatabaseForReading()
                           .GetFinancialAccountRows(orgAccounts.Identities, fromDate, toDate, false)));
        }
Ejemplo n.º 2
0
        private void AddVatReportItemsFromAccountRows(FinancialAccountRows rows,
                                                      Dictionary <int, bool> transactionsIncludedLookup)
        {
            if (rows.Count == 0)
            {
                return;
            }

            Organization organization         = rows[0].Transaction.Organization; // there is always a rows[0] because check above
            int          vatInboundAccountId  = organization.FinancialAccounts.AssetsVatInboundUnreported.Identity;
            int          vatOutboundAccountId = organization.FinancialAccounts.DebtsVatOutboundUnreported.Identity;

            Dictionary <int, bool> turnoverAccountLookup = new Dictionary <int, bool>();

            FinancialAccounts turnoverAccounts = FinancialAccounts.ForOrganization(organization,
                                                                                   FinancialAccountType.Income);

            foreach (FinancialAccount turnoverAccount in turnoverAccounts)
            {
                turnoverAccountLookup[turnoverAccount.Identity] = true;
            }

            foreach (FinancialAccountRow accountRow in rows)
            {
                FinancialTransaction tx = accountRow.Transaction;

                if (tx.Dependency is VatReport)
                {
                    continue; // Never include previous VAT reports in new VAT reports
                }

                if (!transactionsIncludedLookup.ContainsKey(tx.Identity))
                {
                    Int64 vatInbound  = 0;
                    Int64 vatOutbound = 0;
                    Int64 turnOver    = 0;

                    transactionsIncludedLookup[accountRow.FinancialTransactionId] = true;

                    FinancialTransactionRows txRows = accountRow.Transaction.Rows;

                    foreach (FinancialTransactionRow txRow in txRows)
                    {
                        if (txRow.FinancialAccountId == vatInboundAccountId)
                        {
                            vatInbound += txRow.AmountCents;
                        }
                        else if (txRow.FinancialAccountId == vatOutboundAccountId)
                        {
                            vatOutbound += -txRow.AmountCents;  // this is a negative, so converting to positive
                        }
                        else if (turnoverAccountLookup.ContainsKey(txRow.FinancialAccountId))
                        {
                            turnOver -= txRow.AmountCents;  // turnover accounts are sign reversed, so convert to positive
                        }
                    }

                    // Add new row to the VAT report

                    AddItem(tx, turnOver, vatInbound, vatOutbound);
                }
            }
        }
Ejemplo n.º 3
0
        public static AnnualReport Create(Organization organization, int year, FinancialAccountType accountType)
        {
            AnnualReport report = new AnnualReport();

            report.Organization = organization;
            report.Year         = year;
            report._accountType = accountType;

            // Get accounts

            FinancialAccounts accounts = FinancialAccounts.ForOrganization(organization, accountType);

            // Remove unwanted accounts

            FinancialAccount resultsAccount = organization.FinancialAccounts.CostsYearlyResult;

            foreach (FinancialAccount account in accounts)
            {
                // For now, just remove the "results" account. TODO: Remove inactive accounts, too.

                if (account.Identity == resultsAccount.Identity)
                {
                    accounts.Remove(account);
                    break;
                }
            }

            // Build tree (there should be a template for this)

            report._treeMap = new Dictionary <int, List <FinancialAccount> >();

            foreach (FinancialAccount account in accounts)
            {
                if (!report._treeMap.ContainsKey(account.ParentIdentity))
                {
                    report._treeMap[account.ParentIdentity] = new List <FinancialAccount>();
                }

                report._treeMap[account.ParentIdentity].Add(account);
            }


            FinancialAccounts orderedList = new FinancialAccounts();

            // This list is guaranteed to have parents before children

            report.PopulateOrderedList(orderedList, 0); // recursively add nodes parents-first
            report.PopulateLookups(orderedList);        // populate the lookup tables for results per account
            report.PopulateTotals();

            report.ReportLines = new List <AnnualReportLine>();
            report.RecurseAddLines(report.ReportLines, 0);

            // Aggregate accounts, if appropriate

            if (report._treeMap[0].Count > 3)
            {
                // regroup list

                report.AggregateAccounts();
            }

            return(report);
        }