File: MonthlyTimesheet.cpp

package info (click to toggle)
charmtimetracker 1.12.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 3,340 kB
  • sloc: cpp: 19,176; xml: 284; python: 120; makefile: 14
file content (316 lines) | stat: -rw-r--r-- 13,120 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
/*
  MonthlyTimesheet.cpp

  This file is part of Charm, a task-based time tracking application.

  Copyright (C) 2014-2018 Klarälvdalens Datakonsult AB, a KDAB Group company, info@kdab.com

  Author: Frank Osterfeld <frank.osterfeld@kdab.com>

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 2 of the License, or
  (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "MonthlyTimesheet.h"
#include "Reports/MonthlyTimesheetXmlWriter.h"

#include <QFile>
#include <QMessageBox>
#include <QPushButton>
#include <QSettings>
#include <QUrl>

#include <Core/Dates.h>

#include "ViewHelpers.h"

#include "CharmCMake.h"

namespace {
typedef QHash<int, QVector<int> > WeeksByYear;
static float SecondsInDay = 60. * 60. * 8. /* eight hour work day */;
}

MonthlyTimeSheetReport::MonthlyTimeSheetReport(QWidget *parent)
    : TimeSheetReport(parent)
{
    QSettings settings;
    settings.beginGroup(QStringLiteral("users"));
    m_weeklyhours = settings.value(QStringLiteral("weeklyhours")).toString().trimmed();
    settings.endGroup();
    m_dailyhours = m_weeklyhours.toInt() / 5;
    if (m_dailyhours > 0 && m_dailyhours <= 8) {
        SecondsInDay = 60. * 60. * m_dailyhours;
    } else {
        m_dailyhours = 8;
    }

    connect(this, &MonthlyTimeSheetReport::anchorClicked,
            this, &MonthlyTimeSheetReport::slotLinkClicked);
}

MonthlyTimeSheetReport::~MonthlyTimeSheetReport()
{
}

void MonthlyTimeSheetReport::setReportProperties(
    const QDate &start, const QDate &end, TaskId rootTask, bool activeTasksOnly)
{
    m_numberOfWeeks = Charm::weekDifference(start, end.addDays(-1)) + 1;
    m_monthNumber = start.month();
    m_yearOfMonth = start.year();
    TimeSheetReport::setReportProperties(start, end, rootTask, activeTasksOnly);
}

QString MonthlyTimeSheetReport::suggestedFileName() const
{
    return tr("MonthlyTimeSheet-%1-%2").arg(m_yearOfMonth).arg(m_monthNumber, 2, 10, QLatin1Char(
                                                                   '0'));
}

QByteArray MonthlyTimeSheetReport::saveToText()
{
    QByteArray output;
    QTextStream stream(&output);
    QString content = tr("Report for %1, %2 %3 (%4 to %5)")
                      .arg(CONFIGURATION.user.name(),
                           QDate::longMonthName(m_monthNumber),
                           QString::number(startDate().year()),
                           startDate().toString(Qt::TextDate),
                           endDate().addDays(-1).toString(Qt::TextDate));
    stream << content << '\n';
    stream << '\n';
    TimeSheetInfoList timeSheetInfo = TimeSheetInfo::filteredTaskWithSubTasks(
        TimeSheetInfo::taskWithSubTasks(DATAMODEL, m_numberOfWeeks, rootTask(), secondsMap()),
        activeTasksOnly());

    TimeSheetInfo totalsLine(m_numberOfWeeks);
    if (!timeSheetInfo.isEmpty()) {
        totalsLine = timeSheetInfo.first();
        if (rootTask() == 0)
            timeSheetInfo.removeAt(0);   // there is always one, because there is always the root item
    }

    for (int i = 0; i < timeSheetInfo.size(); ++i)
        stream << timeSheetInfo[i].formattedTaskIdAndName(CONFIGURATION.taskPaddingLength)
               << "\t" << hoursAndMinutes(timeSheetInfo[i].total()) << '\n';
    stream << '\n';
    stream << "Month total: " << hoursAndMinutes(totalsLine.total()) << '\n';
    stream.flush();

    return output;
}

QByteArray MonthlyTimeSheetReport::saveToXml(SaveToXmlMode mode)
{
    try {
        MonthlyTimesheetXmlWriter timesheet;
        timesheet.setDataModel(DATAMODEL);
        timesheet.setMonthNumber(m_monthNumber);
        timesheet.setYearOfMonth(m_yearOfMonth);
        timesheet.setNumberOfWeeks(m_numberOfWeeks);
        timesheet.setRootTask(rootTask());
        timesheet.setIncludeTaskList(mode == IncludeTaskList);
        const EventIdList matchingEventIds = DATAMODEL->eventsThatStartInTimeFrame(
            startDate(), endDate());
        EventList events;
        events.reserve(matchingEventIds.size());
        Q_FOREACH (const EventId &eventId, matchingEventIds)
            events.append(DATAMODEL->eventForId(eventId));
        timesheet.setEvents(events);
        return timesheet.saveToXml();
    } catch (const XmlSerializationException &e) {
        QMessageBox::critical(this, tr("Error exporting the report"), e.what());
    }

    return QByteArray();
}

static QDomElement addTblHdr(QDomElement &toRow, const QString &text)
{
    QDomElement header = toRow.ownerDocument().createElement(QStringLiteral("th"));
    QDomText textNode = toRow.ownerDocument().createTextNode(text);
    header.appendChild(textNode);
    toRow.appendChild(header);
    return header;
}

static QDomElement addTblCell(QDomElement &toRow, const QString &text)
{
    QDomElement cell = toRow.ownerDocument().createElement(QStringLiteral("td"));
    cell.setAttribute(QStringLiteral("align"), QStringLiteral("center"));
    QDomText textNode = toRow.ownerDocument().createTextNode(text);
    cell.appendChild(textNode);
    toRow.appendChild(cell);
    return cell;
}

void MonthlyTimeSheetReport::update()
{
    // this creates the time sheet
    // retrieve matching events:
    const EventIdList matchingEvents
        = DATAMODEL->eventsThatStartInTimeFrame(startDate(), endDate());

    m_secondsMap.clear();

    // for every task, make a vector that includes a number of seconds
    // for every week of a month ( int seconds[m_numberOfWeeks]), and store those in
    // a map by their task id
    Q_FOREACH (EventId id, matchingEvents) {
        const Event &event = DATAMODEL->eventForId(id);
        QVector<int> seconds(m_numberOfWeeks);
        if (m_secondsMap.contains(event.taskId()))
            seconds = m_secondsMap.value(event.taskId());
        // what week of the month is the event (normalized to vector indexes):
        const int weekOfMonth = Charm::weekDifference(startDate(), event.startDateTime().date());
        seconds[weekOfMonth] += event.duration();
        // store in minute map:
        m_secondsMap[event.taskId()] = seconds;
    }
    // now the reporting:
    // headline first:
    QTextDocument report;
    QDomDocument doc = createReportTemplate();
    QDomElement root = doc.documentElement();
    QDomElement body = root.firstChildElement(QStringLiteral("body"));

//     QTextCursor cursor( m_report );
    // create the caption:
    {
        QDomElement headline = doc.createElement(QStringLiteral("h1"));
        QDomText text = doc.createTextNode(tr("Monthly Time Sheet"));
        headline.appendChild(text);
        body.appendChild(headline);
    }
    {
        QDomElement headline = doc.createElement(QStringLiteral("h3"));
        QString content = tr("Report for %1, %2 %3 (%4 to %5)")
                          .arg(CONFIGURATION.user.name(),
                               QDate::longMonthName(m_monthNumber),
                               QString::number(startDate().year()),
                               startDate().toString(Qt::TextDate),
                               endDate().addDays(-1).toString(Qt::TextDate));
        QDomText text = doc.createTextNode(content);
        headline.appendChild(text);
        body.appendChild(headline);
        QDomElement previousLink = doc.createElement(QStringLiteral("a"));
        previousLink.setAttribute(QStringLiteral("href"), QStringLiteral("Previous"));
        QDomText previousLinkText = doc.createTextNode(tr("<Previous Month>"));
        previousLink.appendChild(previousLinkText);
        body.appendChild(previousLink);
        QDomElement nextLink = doc.createElement(QStringLiteral("a"));
        nextLink.setAttribute(QStringLiteral("href"), QStringLiteral("Next"));
        QDomText nextLinkText = doc.createTextNode(tr("<Next Month>"));
        nextLink.appendChild(nextLinkText);
        body.appendChild(nextLink);
        QDomElement paragraph = doc.createElement(QStringLiteral("br"));
        body.appendChild(paragraph);
    }
    {
        // now for a table
        // retrieve the information for the report:
        // TimeSheetInfoList timeSheetInfo = taskWithSubTasks( m_rootTask, m_secondsMap );
        TimeSheetInfoList timeSheetInfo = TimeSheetInfo::filteredTaskWithSubTasks(
            TimeSheetInfo::taskWithSubTasks(DATAMODEL, m_numberOfWeeks, rootTask(), secondsMap()),
            activeTasksOnly());

        QDomElement table = doc.createElement(QStringLiteral("table"));
        table.setAttribute(QStringLiteral("width"), QStringLiteral("100%"));
        table.setAttribute(QStringLiteral("align"), QStringLiteral("left"));
        table.setAttribute(QStringLiteral("cellpadding"), QStringLiteral("3"));
        table.setAttribute(QStringLiteral("cellspacing"), QStringLiteral("0"));
        body.appendChild(table);

        TimeSheetInfo totalsLine(m_numberOfWeeks);
        if (!timeSheetInfo.isEmpty()) {
            totalsLine = timeSheetInfo.first();
            if (rootTask() == 0)
                timeSheetInfo.removeAt(0);   // there is always one, because there is always the root item
        }

        {   //Header Row
            QDomElement headerRow = doc.createElement(QStringLiteral("tr"));
            headerRow.setAttribute(QStringLiteral("class"), QStringLiteral("header_row"));
            table.appendChild(headerRow);
            addTblHdr(headerRow, tr("Task"));
            for (int i = 0; i < m_numberOfWeeks; ++i)
                addTblHdr(headerRow, tr("Week"));
            addTblHdr(headerRow, tr("Total"));
            addTblHdr(headerRow, tr("Days"));
        }

        {   //Header day row
            QDomElement headerDayRow = doc.createElement(QStringLiteral("tr"));
            headerDayRow.setAttribute(QStringLiteral("class"), QStringLiteral("header_row"));
            table.appendChild(headerDayRow);
            addTblHdr(headerDayRow, QString());
            for (int i = 0; i < m_numberOfWeeks; ++i) {
                QString label = tr("%1").arg(startDate().addDays(
                                                 i * 7).weekNumber(), 2, 10, QLatin1Char('0'));
                addTblHdr(headerDayRow, label);
            }
            addTblHdr(headerDayRow, QString());
            addTblHdr(headerDayRow, QString::number(m_dailyhours) + tr(" hours"));
        }

        for (int i = 0; i < timeSheetInfo.size(); ++i) {
            QDomElement row = doc.createElement(QStringLiteral("tr"));
            if (i % 2)
                row.setAttribute(QStringLiteral("class"), QStringLiteral("alternate_row"));
            table.appendChild(row);

            QDomElement taskCell
                = addTblCell(row,
                             timeSheetInfo[i].formattedTaskIdAndName(
                                 CONFIGURATION.taskPaddingLength));
            taskCell.setAttribute(QStringLiteral("align"), QStringLiteral("left"));
            taskCell.setAttribute(QStringLiteral("style"), QStringLiteral("text-indent: %1px;")
                                  .arg(9 * timeSheetInfo[i].indentation));
            for (int week = 0; week < m_numberOfWeeks; ++week)
                addTblCell(row, hoursAndMinutes(timeSheetInfo[i].seconds[week]));
            addTblCell(row, hoursAndMinutes(timeSheetInfo[i].total()));
            addTblCell(row, QString::number(timeSheetInfo[i].total() / SecondsInDay, 'f', 1));
        }

        {   // Totals row
            QDomElement totals = doc.createElement(QStringLiteral("tr"));
            totals.setAttribute(QStringLiteral("class"), QStringLiteral("header_row"));
            table.appendChild(totals);

            addTblHdr(totals, tr("Total:"));
            for (int i = 0; i < m_numberOfWeeks; ++i)
                addTblHdr(totals, hoursAndMinutes(totalsLine.seconds[i]));
            addTblHdr(totals, hoursAndMinutes(totalsLine.total()));
            addTblHdr(totals, QString::number(totalsLine.total() / SecondsInDay, 'f', 1));
        }
    }

    // NOTE: seems like the style sheet has to be set before the html
    // code is pushed into the QTextDocument
    report.setDefaultStyleSheet(Charm::reportStylesheet(palette()));

    report.setHtml(doc.toString());
    setDocument(&report);
    uploadButton()->setVisible(false);
    uploadButton()->setEnabled(false);
}

void MonthlyTimeSheetReport::slotLinkClicked(const QUrl &which)
{
    QDate start = which.toString()
                  == QLatin1String("Previous") ? startDate().addMonths(-1) : startDate().addMonths(1);
    QDate end = which.toString()
                == QLatin1String("Previous") ? endDate().addMonths(-1) : endDate().addMonths(1);
    setReportProperties(start, end, rootTask(), activeTasksOnly());
}