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); } }
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); } }
public static string LastReportDescription(Organization organization) { VatReports reports = VatReports.ForOrganization(organization, true); if (reports.Count == 0) { throw new InvalidOperationException("Cannot look up last description of nonexistent VAT report collection"); } reports.Sort(VatReports.VatReportSorterByDate); return(reports.Last().Description); }