getMonthlyReport method

Future<MonthlyReport> getMonthlyReport(
  1. DateTime dateTime, {
  2. bool fiscalMonth = false,
})

Ahgora has a report page where user see the "fiscal month" instead of the month of a timestamp day - it means that when you open a report from february/2020 you will get a few timestamps from january (the last days, where the fiscal month of january ends and start february), and the timestamps will end before the end of the month.

If you clocked-in on 31/jan/2020, you will probably find this timestamp on february report. If you clocked-in on 26/feb/2020, you will probably find this timestamp on march report.

To avoid misleading the users, this API will try allow two ways to fetch data:

  1. The default way fiscalMonth = false will try it by fetching the two subsequents months (the current and the next one) and show reports in a way that users can easily find the desired timestamp.
  2. The optional way fiscalMonth = true will allow to calculate the hour balance correctly using the values from fiscal month.

Implementation

Future<MonthlyReport> getMonthlyReport(DateTime dateTime,
    {bool fiscalMonth = false}) async {
  if (!isLoggedIn) {
    throw InexistentSession();
  }

  final DateFormat dateFormat = DateFormat('yyyy-MM');

  MonthlyReport monthlyReport =
      await _fetchMonthlyReport(dateFormat.format(dateTime));

  if (!fiscalMonth) {
    // When not fetchin for fiscal reasons, make a second request to the next
    // month. Remove all results that are actually on the next month and add
    // all to our existing list.
    DateTime nextMonth = DateTime(dateTime.year, dateTime.month + 1, 1);

    MonthlyReport nextMonthlyReport =
        await _fetchMonthlyReport(dateFormat.format(nextMonth));
    monthlyReport.days.addAll(nextMonthlyReport.days);

    // Remove days from other months (previous and next).
    monthlyReport.days.removeWhere((Day day) =>
        day.clockTimes.isEmpty ||
        day.clockTimes.first.time.month != dateTime.month);
  }

  // Remove days without timestamps (most likely non business days).
  monthlyReport.days.removeWhere((Day day) => day.clockTimes.isEmpty);
  return monthlyReport;
}