From 450043b7cdcf29474d733929381fa1b2aa7a0ee9 Mon Sep 17 00:00:00 2001 From: Martin Zeitler Date: Sat, 24 Oct 2026 00:09:59 +0200 Subject: [PATCH 1/3] fix: store cost on timeline entries so exporting a timeline works Exporting a timeline failed with HTTP 500 ("cost is not a valid attribute") whenever the selected range contained time entries: postTimeline() calls TimelineEntry::setCost(), but timeline entries had no cost field or column. The timeline itself was already inserted, so every failed export also left an empty timeline behind. - Add a nullable cost column (cents, like work intervals) to timetracker_timeline_entry, and the property on TimelineEntry - Create the timeline and its entries in one transaction, so a failing entry doesn't leave an empty timeline - Include the cost and the total cost in the CSV download and the e-mailed CSV - Bump the app version to 0.0.93 so existing installs run the migration Fixes #288 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_015KJpM1wJdw3Uxw5p86uRBD --- appinfo/info.xml | 2 +- lib/Controller/AjaxController.php | 67 +++++++++++++------ lib/Db/TimelineEntry.php | 2 + .../Version000093Date20261024000000.php | 56 ++++++++++++++++ phpstan-baseline.neon | 10 +++ 5 files changed, 115 insertions(+), 22 deletions(-) create mode 100644 lib/Migration/Version000093Date20261024000000.php diff --git a/appinfo/info.xml b/appinfo/info.xml index 15040ae..15492d0 100644 --- a/appinfo/info.xml +++ b/appinfo/info.xml @@ -35,7 +35,7 @@ Mit dem integrierten Dashboard lässt sich anhand eines Kuchen-/Donut-Diagramms Diese App wird aktuell noch weiterentwickelt, also: Augen offen halten für neue Features! Und falls Dir irgendwelche Verbesserungsvorschläge, Probleme oder neue Features einfallen, schau mal auf unserem [GitHub Projekt](https://github.com/mtierltd/timetracker) vorbei, vielleicht wird Dein Thema bereits diskutiert! Und falls nicht, starte gerne eine neue Diskussion, wir freuen uns auf Dein Feedback! - 0.0.92 + 0.0.93 agpl MTier Ltd. TimeTracker diff --git a/lib/Controller/AjaxController.php b/lib/Controller/AjaxController.php index 242fb32..29b9d76 100644 --- a/lib/Controller/AjaxController.php +++ b/lib/Controller/AjaxController.php @@ -5,6 +5,7 @@ use OCP\AppFramework\Http\TemplateResponse; use OCP\AppFramework\Http\DataResponse; use OCP\AppFramework\Controller; +use OCP\IDBConnection; use OCP\IGroupManager; use OCP\IL10N; use OCP\IUserSession; @@ -53,12 +54,13 @@ class AjaxController extends Controller { protected $l10n; protected $groupManager; protected $mailer; + protected $db; public function __construct($AppName, IRequest $request, IUserSession $userSession, IL10N $l10n, WorkIntervalMapper $workIntervalMapper, ClientMapper $clientMapper, UserToClientMapper $userToClientMapper, ProjectMapper $projectMapper, UserToProjectMapper $userToProjectMapper, TagMapper $tagMapper, WorkIntervalToTagMapper $workIntervalToTagMapper, ReportItemMapper $reportItemMapper, TimelineMapper $timelineMapper, TimelineEntryMapper $timelineEntryMapper, GoalMapper $goalMapper, - IGroupManager $groupManager, IMailer $mailer, $userId){ + IGroupManager $groupManager, IMailer $mailer, IDBConnection $db, $userId){ parent::__construct($AppName, $request); $this->userId = $userId; $this->userSession = $userSession; @@ -82,6 +84,7 @@ public function __construct($AppName, IRequest $request, IUserSession $userSessi $this->l10n = $l10n; $this->groupManager = $groupManager; $this->mailer = $mailer; + $this->db = $db; } /** @@ -1012,20 +1015,28 @@ public function postTimeline(){ $timeline->setTotalDuration($totalDuration); $timeline->setCreatedAt(time()); $timeline->setStatus('pending'); - $this->timelineMapper->insert($timeline); - foreach($items as $i){ - $te = new TimelineEntry(); - $te->setTimelineId($timeline->id); - $te->setUserUid($timeline->userUid); - $te->setName($i->name); - $te->setProjectName($i->project ? $i->project : ""); - $te->setClientName($i->client ? $i->client : ""); - $te->setTimeInterval($i->time); - $te->setTotalDuration($i->totalDuration); - $te->setCreatedAt(time()); - $te->setCost($i->cost); - $this->timelineEntryMapper->insert($te); - + // Create the timeline and all its entries together, so a failing entry + // doesn't leave an empty timeline behind. + $this->db->beginTransaction(); + try { + $this->timelineMapper->insert($timeline); + foreach($items as $i){ + $te = new TimelineEntry(); + $te->setTimelineId($timeline->id); + $te->setUserUid($timeline->userUid); + $te->setName($i->name); + $te->setProjectName($i->project ? $i->project : ""); + $te->setClientName($i->client ? $i->client : ""); + $te->setTimeInterval($i->time); + $te->setTotalDuration($i->totalDuration); + $te->setCreatedAt(time()); + $te->setCost($i->cost); + $this->timelineEntryMapper->insert($te); + } + $this->db->commit(); + } catch (\Throwable $e) { + $this->db->rollBack(); + throw $e; } return new JSONResponse(["Timeline" => json_decode(json_encode($timeline), true)]); } @@ -1064,6 +1075,16 @@ public function secondsToTime($seconds){ return $timeFormat; } + /** + * Costs are stored in cents; empty when an entry has no cost. + */ + private function formatCost($cents){ + if ($cents === null || $cents === '') { + return ''; + } + return number_format(((int)$cents) / 100, 2, '.', ''); + } + /** * * @NoAdminRequired @@ -1085,14 +1106,16 @@ public function downloadTimeline($id){ $output = fopen('php://output', 'w'); // output the column headings - fputcsv($output, array('id', 'User Uid', 'Name', 'Project Name', 'Client Name', 'Time Interval', 'Total Duration')); + fputcsv($output, array('id', 'User Uid', 'Name', 'Project Name', 'Client Name', 'Time Interval', 'Total Duration', 'Cost')); $totalDuration = 0; + $totalCost = 0; foreach($te as $t){ - fputcsv($output, [$t->id, $t->userUid, $t->name, $t->projectName, $t->clientName, $t->timeInterval, $this->secondsToTime($t->totalDuration)]); + fputcsv($output, [$t->id, $t->userUid, $t->name, $t->projectName, $t->clientName, $t->timeInterval, $this->secondsToTime($t->totalDuration), $this->formatCost($t->cost)]); $totalDuration += $t->totalDuration; + $totalCost += (int)$t->cost; } - fputcsv($output, ['TOTAL', '', '', '', '', '', $this->secondsToTime($totalDuration)]); + fputcsv($output, ['TOTAL', '', '', '', '', '', $this->secondsToTime($totalDuration), $this->formatCost($totalCost)]); fclose($output); exit(0); } @@ -1151,14 +1174,16 @@ public function emailTimeline($id) { $output = fopen($path, "w"); //$path = stream_get_meta_data($output)['uri']; // output the column headings - fputcsv($output, array('id', 'User Uid', 'Name', 'Project Name', 'Client Name', 'Time Interval', 'Total Duration')); + fputcsv($output, array('id', 'User Uid', 'Name', 'Project Name', 'Client Name', 'Time Interval', 'Total Duration', 'Cost')); $totalDuration = 0; + $totalCost = 0; foreach($te as $t){ - fputcsv($output, [$t->id, $t->userUid, $t->name, $t->projectName, $t->clientName, $t->timeInterval, $this->secondsToTime($t->totalDuration)]); + fputcsv($output, [$t->id, $t->userUid, $t->name, $t->projectName, $t->clientName, $t->timeInterval, $this->secondsToTime($t->totalDuration), $this->formatCost($t->cost)]); $totalDuration += $t->totalDuration; + $totalCost += (int)$t->cost; } - fputcsv($output, ['TOTAL', '', '', '', '', '', $this->secondsToTime($totalDuration)]); + fputcsv($output, ['TOTAL', '', '', '', '', '', $this->secondsToTime($totalDuration), $this->formatCost($totalCost)]); diff --git a/lib/Db/TimelineEntry.php b/lib/Db/TimelineEntry.php index 43022cb..7ae0cb8 100644 --- a/lib/Db/TimelineEntry.php +++ b/lib/Db/TimelineEntry.php @@ -15,6 +15,7 @@ class TimelineEntry extends Entity { public $timeInterval; public $totalDuration; public $createdAt; + public $cost; public function __construct() { @@ -29,5 +30,6 @@ public function __construct() { $this->addType('timeInterval', 'string'); $this->addType('totalDuration', 'string'); $this->addType('createdAt', 'integer'); + $this->addType('cost', 'integer'); } } \ No newline at end of file diff --git a/lib/Migration/Version000093Date20261024000000.php b/lib/Migration/Version000093Date20261024000000.php new file mode 100644 index 0000000..83f343a --- /dev/null +++ b/lib/Migration/Version000093Date20261024000000.php @@ -0,0 +1,56 @@ +. + */ + +namespace OCA\TimeTracker\Migration; + +use Closure; +use OCP\DB\ISchemaWrapper; +use OCP\DB\Types; +use OCP\Migration\IOutput; +use OCP\Migration\SimpleMigrationStep; + +/** + * Stores the cost of each timeline entry, like work intervals do since + * Version000020Date20220528101009 (in cents, nullable). + */ +class Version000093Date20261024000000 extends SimpleMigrationStep { + + public function changeSchema(IOutput $output, Closure $schemaClosure, array $options): ?ISchemaWrapper { + /** @var ISchemaWrapper $schema */ + $schema = $schemaClosure(); + + if (!$schema->hasTable('timetracker_timeline_entry')) { + return null; + } + + $table = $schema->getTable('timetracker_timeline_entry'); + if ($table->hasColumn('cost')) { + return null; + } + + $table->addColumn('cost', Types::INTEGER, [ + 'notnull' => false, + 'length' => 10, + ]); + + return $schema; + } +} diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index ab4a21c..bcfba9d 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -409,3 +409,13 @@ parameters: message: "#^Call to method setPrimaryKey\\(\\) on an unknown class Doctrine\\\\DBAL\\\\Schema\\\\Table\\.$#" count: 2 path: lib/Migration/Version000001Date20210719192031.php + + - + message: "#^Call to method addColumn\\(\\) on an unknown class Doctrine\\\\DBAL\\\\Schema\\\\Table\\.$#" + count: 1 + path: lib/Migration/Version000093Date20261024000000.php + + - + message: "#^Call to method hasColumn\\(\\) on an unknown class Doctrine\\\\DBAL\\\\Schema\\\\Table\\.$#" + count: 1 + path: lib/Migration/Version000093Date20261024000000.php From 78408bc788a2010af9b521b00f07399e139e18c5 Mon Sep 17 00:00:00 2001 From: Martin Zeitler Date: Sat, 24 Oct 2026 00:18:43 +0200 Subject: [PATCH 2/3] feat(timer): select project and tags before starting a timer The Timer view only had a description field and a Start button, so a new entry could only get its project afterwards in the list. - Add a project and a tag selector next to the description field. - Pre-fill them with what the server would pick: for a description that was used before, the project and tags of its latest entry; otherwise the project of the latest entry that has one. Once the user changes the selection, it is kept until the timer starts. - Send the selection with POST /ajax/start-timer/{name}, which already accepts projectId and tags. Clearing the selection keeps the previous behaviour: the server copies project and tags from the latest entry with the same name. - The selectors are disabled while a timer is running. Fixes #293 Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_015KJpM1wJdw3Uxw5p86uRBD --- js/src/app/views/TimerView.vue | 68 +++++++++++++++++++++++++++++++++- 1 file changed, 67 insertions(+), 1 deletion(-) diff --git a/js/src/app/views/TimerView.vue b/js/src/app/views/TimerView.vue index 315f767..bb75cbc 100644 --- a/js/src/app/views/TimerView.vue +++ b/js/src/app/views/TimerView.vue @@ -5,6 +5,23 @@
+ + {{ isRunning ? 'Stop' : 'Start' }} {{ liveTimerText }} Manual entry @@ -164,6 +181,11 @@ export default { from.setDate(from.getDate() - 30) return { workInput: '', + // Project and tags for the next timer. Pre-filled by + // suggestStartSelection() until the user changes them. + startProject: null, + startTags: [], + startSelectionTouched: false, days: {}, running: [], projects: [], @@ -202,9 +224,21 @@ export default { } return result }, + // All loaded entries, newest first + recentEntries() { + return Object.values(this.flattenedDays) + .flatMap((day) => day.children) + .sort((a, b) => b.start - a.start) + }, + }, + watch: { + workInput() { + this.suggestStartSelection() + }, }, async mounted() { await Promise.all([this.loadWorkIntervals(), this.loadProjects(), this.loadTags()]) + this.suggestStartSelection() }, beforeUnmount() { clearInterval(this.liveTimerInterval) @@ -238,6 +272,28 @@ export default { this.days = data.days || {} this.running = data.running || [] this.setupLiveTimer(data.now) + this.suggestStartSelection() + }, + /** + * Pre-fill project and tags for the next timer, unless the user + * already changed them: + * - an earlier entry with the same name: its project and tags, which + * is what the server would copy anyway + * - otherwise: the project of the latest entry that has one + */ + suggestStartSelection() { + if (this.startSelectionTouched) return + const name = this.workInput.trim() + const sameName = name ? this.recentEntries.find((e) => e.name === name) : null + if (sameName) { + this.startProject = this.projectFor(sameName) + const tagIds = (sameName.tags || []).map((t) => t.id) + this.startTags = this.tags.filter((t) => tagIds.includes(t.id)) + return + } + const lastWithProject = this.recentEntries.find((e) => e.projectId) + this.startProject = lastWithProject ? this.projectFor(lastWithProject) : null + this.startTags = [] }, setupLiveTimer(serverNow) { clearInterval(this.liveTimerInterval) @@ -255,7 +311,15 @@ export default { if (this.isRunning) { await this.stopTimer() } else { - await this.startTimer(this.workInput.trim() || 'no description') + const started = await this.startTimer( + this.workInput.trim() || 'no description', + this.startProject?.id, + this.startTags.map((t) => t.id).join(','), + ) + if (started) { + this.startSelectionTouched = false + this.suggestStartSelection() + } } }, async startTimer(name, projectId, tagIds) { @@ -266,8 +330,10 @@ export default { }) this.workInput = '' await this.loadWorkIntervals() + return true } catch (e) { showError(e.message) + return false } }, async stopTimer() { From 3c95b60cd782bec6a4462931e4d9c2ef606798fb Mon Sep 17 00:00:00 2001 From: Martin Zeitler Date: Sat, 24 Oct 2026 00:19:33 +0200 Subject: [PATCH 3/3] fix(timer): include the whole end day when loading entries The Timer view sent the end date picker's value as the upper bound for the entry list. That is the page load time initially, and midnight at the start of the day once a date is picked, so entries started later that day were left out: a timer started after opening the page didn't show up in the list until reload, and picking an end date hid that day's entries. This also kept "last used project" from updating after starting a timer. Send the end of the selected day instead, like the quick range presets already do. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_015KJpM1wJdw3Uxw5p86uRBD --- js/src/app/views/TimerView.vue | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/js/src/app/views/TimerView.vue b/js/src/app/views/TimerView.vue index bb75cbc..1ac738c 100644 --- a/js/src/app/views/TimerView.vue +++ b/js/src/app/views/TimerView.vue @@ -264,9 +264,14 @@ export default { this.loadWorkIntervals() }, async loadWorkIntervals() { + // The end date picker gives a moment on that day (page load time, or + // midnight when picked), so include the whole day: otherwise entries + // started later that day, like a timer started just now, are missing. + const to = new Date(this.rangeTo) + to.setHours(23, 59, 59, 999) const data = await apiGet('/work-intervals', { from: Math.floor(this.rangeFrom.getTime() / 1000), - to: Math.floor(this.rangeTo.getTime() / 1000), + to: Math.floor(to.getTime() / 1000), tzoffset: new Date().getTimezoneOffset(), }) this.days = data.days || {}