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/js/src/app/views/TimerView.vue b/js/src/app/views/TimerView.vue index 315f767..1ac738c 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) @@ -230,14 +264,41 @@ 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 || {} 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 +316,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 +335,10 @@ export default { }) this.workInput = '' await this.loadWorkIntervals() + return true } catch (e) { showError(e.message) + return false } }, async stopTimer() { 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