-
-
Notifications
You must be signed in to change notification settings - Fork 18
Expand file tree
/
Copy pathPersonExportController.php
More file actions
94 lines (74 loc) · 3.67 KB
/
PersonExportController.php
File metadata and controls
94 lines (74 loc) · 3.67 KB
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
<?php
namespace App\Http\Controllers;
use App\Services\ExportServices\PersonExportService;
use App\Services\PersonService;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;
use Symfony\Component\HttpFoundation\StreamedResponse;
class PersonExportController extends Controller {
public function __construct(
private PersonService $personService,
private PersonExportService $peopleExportService,
) {}
public function download(Request $request): StreamedResponse|JsonResponse {
$format = $request->get('format', 'excel');
if ($format === 'csv') {
return $this->downloadCsv($request);
}
if ($format === 'json') {
return $this->downloadJson($request);
}
return $this->downloadExcel($request);
}
public function downloadExcel(Request $request): StreamedResponse {
$miniGridName = $request->get('miniGrid');
$villageName = $request->get('village');
$deviceType = $request->get('deviceType');
$isActive = $request->get('isActive');
$isActive = $isActive === 'true' ? true : ($isActive === 'false' ? false : null);
$people = $this->personService->getAllForExport($miniGridName, $villageName, $deviceType, $isActive);
$this->peopleExportService->createSpreadSheetFromTemplate($this->peopleExportService->getTemplatePath());
$this->peopleExportService->setPeopleData($people);
$this->peopleExportService->setExportingData();
$this->peopleExportService->writePeopleData();
$pathToSpreadSheet = $this->peopleExportService->saveSpreadSheet();
return Storage::download($pathToSpreadSheet, 'customer_export_'.now()->format('Ymd_His').'.xlsx');
}
public function downloadCsv(Request $request): StreamedResponse {
$miniGridName = $request->get('miniGrid');
$villageName = $request->get('village');
$deviceType = $request->get('deviceType');
$isActive = $request->get('isActive');
$isActive = $isActive === 'true' ? true : ($isActive === 'false' ? false : null);
$people = $this->personService->getAllForExport($miniGridName, $villageName, $deviceType, $isActive);
$this->peopleExportService->setPeopleData($people);
$this->peopleExportService->setExportingData();
$headers = ['Title', 'Name', 'Surname', 'Birth Date', 'Gender', 'Email', 'Phone', 'Village', 'Device Serial', 'Agent Name'];
$csvPath = $this->peopleExportService->saveCsv($headers);
return Storage::download($csvPath, 'customer_export_'.now()->format('Ymd_His').'.csv');
}
public function downloadJson(Request $request): JsonResponse {
$miniGridName = $request->get('miniGrid');
$villageName = $request->get('village');
$deviceType = $request->get('deviceType');
$isActive = $request->get('isActive');
$isActive = $isActive === 'true' ? true : ($isActive === 'false' ? false : null);
$people = $this->personService->getAllForExport($miniGridName, $villageName, $deviceType, $isActive);
$this->peopleExportService->setPeopleData($people);
$jsonData = $this->peopleExportService->exportDataToArray();
return response()->json([
'data' => $jsonData,
'meta' => [
'total' => count($jsonData),
'filters' => [
'mini_grid' => $miniGridName,
'village' => $villageName,
'device_type' => $deviceType,
'is_active' => $isActive,
],
'exported_at' => now()->toISOString(),
],
]);
}
}