1 | <?php |
||
2 | |||
3 | namespace App\Services; |
||
4 | |||
5 | use App\Interfaces\EnrollmentSheetInterface; |
||
6 | use App\Models\Enrollment; |
||
7 | use Illuminate\Support\Facades\App; |
||
8 | use Illuminate\Support\Str; |
||
9 | use PhpOffice\PhpWord\Element\Table; |
||
10 | use PhpOffice\PhpWord\SimpleType\JcTable; |
||
11 | use PhpOffice\PhpWord\TemplateProcessor; |
||
12 | |||
13 | class AFSantiagoEnrollmentSheetService implements EnrollmentSheetInterface |
||
14 | { |
||
15 | public function __construct() |
||
16 | { |
||
17 | if (config('enrollment-sheet.style' !== 'afsantiago')) { |
||
18 | abort(403); |
||
19 | } |
||
20 | } |
||
21 | |||
22 | private function utf8_for_xml($string) |
||
23 | { |
||
24 | return preg_replace('/^[\p{L}\p{N}_-]+$/u', ' ', $string); |
||
25 | } |
||
26 | |||
27 | public function exportToWord(Enrollment $enrollment) |
||
28 | { |
||
29 | App::setLocale(config('app.locale')); |
||
30 | $templateProcessor = new TemplateProcessor(storage_path('afsantiago/enrollment.docx')); |
||
31 | |||
32 | $templateProcessor->setValue('enrollment_date', $enrollment->date); |
||
33 | $templateProcessor->setValue('name', $enrollment->student_name); |
||
34 | |||
35 | $nif = $enrollment->student->idnumber ?: ''; |
||
36 | $phone = $enrollment->student->phone->count() > 0 && $enrollment->student->phone->first()->phone_number ? $enrollment->student->phone->first()->phone_number : ''; |
||
37 | $email = $enrollment->student->email ?: ''; |
||
38 | $address = $enrollment->student->address ?: ''; |
||
39 | $city = $enrollment->student->city ?: ''; |
||
40 | |||
41 | $templateProcessor->setValue('address', $address); |
||
42 | $templateProcessor->setValue('city', $city); |
||
43 | $templateProcessor->setValue('phone', $phone); |
||
44 | $templateProcessor->setValue('nif', $nif); |
||
45 | $templateProcessor->setValue('email', $email); |
||
46 | |||
47 | $templateProcessor->setValue('description', $this->utf8_for_xml($enrollment->course->name ?? '')); |
||
48 | $templateProcessor->setValue('start_date', $enrollment->course->formatted_start_date); |
||
49 | $templateProcessor->setValue('end_date', $enrollment->course->formatted_end_date); |
||
50 | $templateProcessor->setValue('volume', $enrollment->course->volume); |
||
51 | |||
52 | $table = new Table(['borderSize' => 8, |
||
53 | 'borderColor' => 'black', |
||
54 | 'cellMargin' => 80, |
||
55 | 'alignment' => JcTable::CENTER, |
||
56 | 'cellSpacing' => 50, |
||
57 | 'width' => 100 * 50, ]); |
||
58 | |||
59 | $firstRowStyle = ['bgColor' => 'd9d9d9']; |
||
60 | |||
61 | $table->addRow(500, $firstRowStyle); |
||
62 | $table->addCell(4000, $firstRowStyle)->addText(Str::upper(__('Due Date'))); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
63 | $table->addCell(5000, $firstRowStyle)->addText(Str::upper(__('Total'))); |
||
64 | |||
65 | if ($enrollment->scheduledPayments->count() > 0) { |
||
66 | foreach ($enrollment->scheduledPayments as $payment) { |
||
67 | $table->addRow(500); |
||
68 | $table->addCell(4000)->addText($payment->date_for_humans, [], ['spaceAfter' => 0]); |
||
69 | $table->addCell(5000)->addText($payment->value_with_currency, [], ['spaceAfter' => 0]); |
||
70 | } |
||
71 | $templateProcessor->setComplexBlock('payments', $table); |
||
72 | } else { |
||
73 | $templateProcessor->setValue('payments', ''); |
||
74 | } |
||
75 | |||
76 | $path = $templateProcessor->save(); |
||
77 | |||
78 | return response()->download($path)->deleteFileAfterSend(true); |
||
79 | } |
||
80 | } |
||
81 |