Пример #1
0
        public static DateTime NextReportDue(Organization organization)
        {
            int reportMonthInterval = organization.VatReportFrequencyMonths;

            // Get the list of previous VAT reports

            VatReports reports = VatReports.ForOrganization(organization, true);

            if (reports.Count == 0)
            {
                DateTime firstReportGenerationTime =
                    new DateTime(organization.FirstFiscalYear, 1, 1).AddMonths(reportMonthInterval);
                // construct VAT report on the first day of the new month

                return(firstReportGenerationTime);
            }
            else // if reports.Count > 0
            {
                reports.Sort(VatReports.VatReportSorterByDate);

                VatReport lastReport     = reports.Last();
                DateTime  lastReportDate = new DateTime(lastReport.YearMonthStart / 100, reports.Last().YearMonthStart % 100,
                                                        1);

                DateTime nextReportDate           = lastReportDate.AddMonths(lastReport.MonthCount);
                DateTime nextReportGenerationTime = nextReportDate.AddMonths(reportMonthInterval);

                return(nextReportGenerationTime);
            }
        }
Пример #2
0
        public static void CreateNext(Organization organization)
        {
            // Creates all VAT reports that are required up until today.
            // Normally this is just one, but in the case of a missed report slot, this function will
            // catch up with all the missed ones, too.

            DateTime nowUtc              = DateTime.UtcNow;
            DateTime nextReportDue       = NextReportDue(organization);
            int      reportMonthInterval = organization.VatReportFrequencyMonths;

            if (nextReportDue > nowUtc)
            {
                throw new InvalidOperationException("VAT report is not yet due or has already been generated");
            }

            DateTime   thisReportDate = new DateTime(organization.FirstFiscalYear, 1, 1); // default to first report
            VatReports reports        = VatReports.ForOrganization(organization, true);

            if (reports.Count > 0)
            {
                reports.Sort(VatReports.VatReportSorterByDate);

                VatReport lastReport     = reports.Last();
                DateTime  lastReportDate = new DateTime(lastReport.YearMonthStart / 100, reports.Last().YearMonthStart % 100,
                                                        1);

                thisReportDate = lastReportDate.AddMonths(lastReport.MonthCount);
            }

            while (thisReportDate.AddMonths(reportMonthInterval) < nowUtc)
            {
                VatReport.Create(organization, thisReportDate.Year, thisReportDate.Month, reportMonthInterval);
                thisReportDate = thisReportDate.AddMonths(reportMonthInterval);
            }
        }
Пример #3
0
        public static VatReportItem Create(VatReport report, FinancialTransaction transaction, Int64 turnoverCents,
                                           Int64 vatInboundCents, Int64 vatOutboundCents)
        {
            // Assumes there's a dependency of some sort

            IHasIdentity            foreignObject  = transaction.Dependency;
            FinancialDependencyType dependencyType =
                (foreignObject != null
                ? FinancialTransaction.GetFinancialDependencyType(transaction.Dependency)
                : FinancialDependencyType.Unknown);

            // The transaction dependency is stored for quick lookup; it duplicates information in the database
            // to save an expensive query as a mere optimization.

            int newVatReportItemId = SwarmDb.GetDatabaseForWriting()
                                     .CreateVatReportItem(report.Identity, transaction.Identity, foreignObject?.Identity ?? 0,
                                                          dependencyType, turnoverCents, vatInboundCents, vatOutboundCents);

            return(FromIdentityAggressive(newVatReportItemId));
        }
Пример #4
0
        public static VatReport Create(Organization organization, int year, int startMonth, int monthCount)
        {
            VatReport newReport = CreateDbRecord(organization, year, startMonth, monthCount);
            DateTime  endDate   = new DateTime(year, startMonth, 1).AddMonths(monthCount);

            FinancialAccount vatInbound  = organization.FinancialAccounts.AssetsVatInboundUnreported;
            FinancialAccount vatOutbound = organization.FinancialAccounts.DebtsVatOutboundUnreported;
            FinancialAccount sales       = organization.FinancialAccounts.IncomeSales;

            FinancialAccountRows inboundRows  = RowsNotInVatReport(vatInbound, endDate);
            FinancialAccountRows outboundRows = RowsNotInVatReport(vatOutbound, endDate);
            FinancialAccountRows turnoverRows = RowsNotInVatReport(sales, endDate);

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

            newReport.AddVatReportItemsFromAccountRows(inboundRows, transactionsIncludedLookup);
            newReport.AddVatReportItemsFromAccountRows(outboundRows, transactionsIncludedLookup);
            newReport.AddVatReportItemsFromAccountRows(turnoverRows, transactionsIncludedLookup);

            newReport.Release();

            // Create financial TX that moves this VAT from unreported to reported

            Int64 differenceCents = newReport.VatInboundCents - newReport.VatOutboundCents;

            if (differenceCents != 0 && newReport.VatInboundCents > 0)
            {
                // if there's anything to report

                FinancialTransaction vatReportTransaction = FinancialTransaction.Create(organization, endDate.AddDays(4).AddHours(9),
                                                                                        newReport.Description);

                if (newReport.VatInboundCents > 0)
                {
                    vatReportTransaction.AddRow(organization.FinancialAccounts.AssetsVatInboundUnreported,
                                                -newReport.VatInboundCents, null);
                }
                if (newReport.VatOutboundCents > 0)
                {
                    vatReportTransaction.AddRow(organization.FinancialAccounts.DebtsVatOutboundUnreported,
                                                newReport.VatOutboundCents, null);
                    // not negative, because our number is sign-different from the bookkeeping's
                }

                if (differenceCents < 0) // outbound > inbound
                {
                    vatReportTransaction.AddRow(organization.FinancialAccounts.DebtsVatOutboundReported,
                                                differenceCents, null); // debt, so negative as in our variable
                }
                else // inbound > outbound
                {
                    vatReportTransaction.AddRow(organization.FinancialAccounts.AssetsVatInboundReported,
                                                differenceCents, null); // asset, so positive as in our variable
                }

                vatReportTransaction.Dependency = newReport;
                newReport.OpenTransaction       = vatReportTransaction;
            }
            else
            {
                newReport.Open = false; // nothing to close, no tx created
            }

            return(newReport);
        }
Пример #5
0
 public static VatReportItems ForReport(VatReport report)
 {
     return(FromArray(SwarmDb.GetDatabaseForReading().GetVatReportItems(report)));
 }