Skip to content

Commit

Permalink
Updated PDF reports
Browse files Browse the repository at this point in the history
  • Loading branch information
korridor committed Nov 28, 2024
1 parent 800f9a1 commit ccad1ad
Show file tree
Hide file tree
Showing 14 changed files with 432 additions and 233 deletions.
30 changes: 24 additions & 6 deletions app/Http/Controllers/Api/V1/TimeEntryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ private function getTimeEntriesQuery(Organization $organization, TimeEntryIndexR
*
* @operationId exportTimeEntries
*/
public function indexExport(Organization $organization, TimeEntryIndexExportRequest $request): JsonResponse
public function indexExport(Organization $organization, TimeEntryIndexExportRequest $request, TimeEntryAggregationService $timeEntryAggregationService): JsonResponse
{
/** @var Member|null $member */
$member = $request->has('member_id') ? Member::query()->findOrFail($request->input('member_id')) : null;
Expand Down Expand Up @@ -198,12 +198,29 @@ public function indexExport(Organization $organization, TimeEntryIndexExportRequ
if (config('services.gotenberg.url') === null) {
throw new PdfRendererIsNotConfiguredException;
}
$viewFile = file_get_contents(resource_path('views/reports/time-entry-index.blade.php'));
$viewFile = file_get_contents(resource_path('views/reports/time-entry-index/pdf.blade.php'));
if ($viewFile === false) {
throw new \LogicException('View file not found');
}
$html = Blade::render($viewFile, ['timeEntries' => $timeEntriesQuery->get()]);
$footerViewFile = file_get_contents(resource_path('views/reports/time-entry-index-footer.blade.php'));
$aggregatedData = $timeEntryAggregationService->getAggregatedTimeEntries(
$timeEntriesQuery->clone()->reorder()->withOnly([]),
null,
null,
$user->timezone,
$user->week_start,
false,
null,
null
);
$html = Blade::render($viewFile, [
'timeEntries' => $timeEntriesQuery->get(),
'aggregatedData' => $aggregatedData,
'timezone' => $timezone,
'currency' => $organization->currency,
'start' => $request->getStart()->timezone($timezone),
'end' => $request->getEnd()->timezone($timezone),
]);
$footerViewFile = file_get_contents(resource_path('views/reports/time-entry-index/pdf-footer.blade.php'));
if ($footerViewFile === false) {
throw new \LogicException('View file not found');
}
Expand Down Expand Up @@ -372,7 +389,7 @@ public function aggregateExport(Organization $organization, TimeEntryAggregateEx
config('services.gotenberg.basic_auth_password'),
] : null,
]);
$viewFile = file_get_contents(resource_path('views/reports/time-entry-aggregate-index.blade.php'));
$viewFile = file_get_contents(resource_path('views/reports/time-entry-aggregate/pdf.blade.php'));
if ($viewFile === false) {
throw new \LogicException('View file not found');
}
Expand All @@ -385,7 +402,7 @@ public function aggregateExport(Organization $organization, TimeEntryAggregateEx
'start' => $request->getStart()->timezone($timezone),
'end' => $request->getEnd()->timezone($timezone),
]);
$footerViewFile = file_get_contents(resource_path('views/reports/time-entry-index-footer.blade.php'));
$footerViewFile = file_get_contents(resource_path('views/reports/time-entry-aggregate/pdf-footer.blade.php'));
if ($footerViewFile === false) {
throw new \LogicException('View file not found');
}
Expand All @@ -395,6 +412,7 @@ public function aggregateExport(Organization $organization, TimeEntryAggregateEx
->pdfa('PDF/A-3b')
->paperSize('8.27', '11.7') // A4
->footer(Stream::string('footer', $footerHtml))
->assets(Stream::path(resource_path('pdf-js/echarts.min.js'), 'echarts.min.js'))
->html(Stream::string('body', $html));
$tempFolder = TemporaryDirectory::make();
$filenameTemp = Gotenberg::save($request, $tempFolder->path(), $client);
Expand Down
25 changes: 23 additions & 2 deletions app/Http/Requests/V1/TimeEntry/TimeEntryIndexExportRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use App\Models\Task;
use Illuminate\Contracts\Validation\ValidationRule;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\Carbon;
use Illuminate\Validation\Rule;
use Korridor\LaravelModelValidationRules\Rules\ExistsEloquent;

Expand Down Expand Up @@ -96,14 +97,14 @@ public function rules(): array
],
// Filter only time entries that have a start date after the given timestamp in UTC (example: 2021-01-01T00:00:00Z)
'start' => [
'nullable',
'required',
'string',
'date_format:Y-m-d\TH:i:s\Z',
'before:end',
],
// Filter only time entries that have a start date before the given timestamp in UTC (example: 2021-01-01T00:00:00Z)
'end' => [
'nullable',
'required',
'string',
'date_format:Y-m-d\TH:i:s\Z',
],
Expand Down Expand Up @@ -131,6 +132,26 @@ public function rules(): array
];
}

public function getStart(): Carbon
{
$start = Carbon::createFromFormat('Y-m-d\TH:i:s\Z', $this->input('start'), 'UTC');
if ($start === null) {
throw new \LogicException('Start date validation is not working');

Check warning on line 139 in app/Http/Requests/V1/TimeEntry/TimeEntryIndexExportRequest.php

View check run for this annotation

Codecov / codecov/patch

app/Http/Requests/V1/TimeEntry/TimeEntryIndexExportRequest.php#L139

Added line #L139 was not covered by tests
}

return $start;
}

public function getEnd(): Carbon
{
$end = Carbon::createFromFormat('Y-m-d\TH:i:s\Z', $this->input('end'), 'UTC');
if ($end === null) {
throw new \LogicException('End date validation is not working');

Check warning on line 149 in app/Http/Requests/V1/TimeEntry/TimeEntryIndexExportRequest.php

View check run for this annotation

Codecov / codecov/patch

app/Http/Requests/V1/TimeEntry/TimeEntryIndexExportRequest.php#L149

Added line #L149 was not covered by tests
}

return $end;
}

public function getOnlyFullDates(): bool
{
return $this->input('only_full_dates', 'false') === 'true';
Expand Down
2 changes: 1 addition & 1 deletion app/Service/ReportExport/TimeEntriesReportExport.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function __construct(array $data, ExportFormat $exportFormat, string $cur

public function view(): View
{
return view('reports.time-entry-aggregate-index-excel', [
return view('reports.time-entry-aggregate.spreadsheet', [
'data' => $this->data,
'currency' => $this->currency,
'group' => $this->group,
Expand Down
45 changes: 45 additions & 0 deletions resources/pdf-js/echarts.min.js

Large diffs are not rendered by default.

127 changes: 0 additions & 127 deletions resources/views/reports/time-entry-aggregate-index.blade.php

This file was deleted.

17 changes: 17 additions & 0 deletions resources/views/reports/time-entry-aggregate/pdf-footer.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<html lang="en">
<head>
<style>
body {
font-size: 12px;
margin-top: 40px;
margin-left: 47px;
}
</style>
</head>
<body>
<p>
Page <span class="pageNumber"></span> of <span class="totalPages"></span>
</p>
</body>
</html>
Loading

0 comments on commit ccad1ad

Please sign in to comment.