88use App \Http \Helpers \RouteHelper ;
99use App \Models \CoeRequest ;
1010use App \Models \EmployeeDoc ;
11- use Barryvdh \ DomPDF \ Facade \ Pdf ;
12- use Illuminate \ Support \ Facades \ File ;
11+ use App \ Traits \ NeedsWordDocToPdf ;
12+ use Carbon \ Carbon ;
1313use Illuminate \Http \Request ;
1414use Illuminate \Support \Facades \DB ;
1515use Illuminate \Support \Facades \Storage ;
16+ use PhpOffice \PhpWord \IOFactory ;
17+ use PhpOffice \PhpWord \TemplateProcessor as WordTemplateProcessor ;
1618
1719class CoeController extends Controller
1820{
21+ use NeedsWordDocToPdf;
22+
23+ private $ coeTemplatePath = FilePath::DOC_TEMPLATE ->value . 'Certificate of Appreciation.docx ' ;
24+
1925 /**
2026 * Display a listing of the resource.
2127 */
@@ -48,6 +54,7 @@ public function show(string $coe)
4854
4955 $ coe = RouteHelper::validateModel (CoeRequest::class, $ coe );
5056
57+ $ coe ->loadMissing (['requestor.lifecycle ' , 'requestor.jobTitle.department ' ]);
5158
5259 return view ('employee.separation.coe.request ' , compact ('coe ' ));
5360 }
@@ -59,29 +66,92 @@ public function show(string $coe)
5966 public function edit ($ coeRequest )
6067 {
6168
62- // dump($coeRequest);
69+ $ coeRequest ->loadMissing (['requestor.lifecycle ' , 'requestor.jobTitle.department ' ]);
70+
6371 $ coeData = [
6472 'name ' => $ coeRequest ->requestor ->fullname ,
6573 'empStart ' => $ coeRequest ->requestor ->lifecycle ->start_date ,
6674 'empEnd ' => $ coeRequest ->requestor ->lifecycle ->separated_at ,
67- 'jobTitle ' => $ coeRequest ->requestor ->jobTitle ->department -> department_name ,
75+ 'jobTitle ' => $ coeRequest ->requestor ->jobTitle ->job_title ,
6876 'jobDepartment ' => $ coeRequest ->requestor ->jobTitle ->department ->department_name ,
6977 'issuedDate ' => now (),
7078 'hrManager ' => auth ()->user ()->account ->fullname ,
7179 'companyAddr ' => 'Rowsuz Business Center, Diversin Rd ' ,
7280 ];
7381
74- Pdf::setOption (['dpi ' => 300 ]);
75- $ coe = Pdf::loadView ('coe ' , $ coeData );
76- $ coe ->setPaper ('a4 ' , 'landscape ' );
77- $ coePdf = $ coe ->output ();
82+ $ relativePath = $ this ->generateContent ($ coeRequest );
83+
84+ return $ relativePath ;
85+ }
86+
87+ private function generateContent ($ coeRequest )
88+ {
89+ $ reader = IOFactory::createReader ('Word2007 ' );
7890
79- $ relativePath = FilePath::COE ->value . hash ('sha256 ' , time ()) . '.pdf ' ;
80- $ coePath = 'public/ ' . $ relativePath ;
8191
82- Storage::put ($ coePath , $ coePdf );
92+ if (Storage::disk ('public ' )->missing ($ this ->coeTemplatePath )) {
93+ abort (404 );
94+ }
8395
84- return $ relativePath ;
96+ $ templateProcessor = new WordTemplateProcessor (Storage::disk ('public ' )->path ($ this ->coeTemplatePath ));
97+
98+ $ issuedDate = now ();
99+
100+ $ issueDay = Carbon::parse ($ issuedDate )->isoFormat ('Do ' );
101+ $ issueMonth = Carbon::parse ($ issuedDate )->format ('F ' );
102+ $ issueYear = Carbon::parse ($ issuedDate )->isoFormat ('YYYY ' );
103+
104+ $ values = [
105+ // Long Names isnt shrinked
106+ 'EMPLOYEE_NAME ' => $ coeRequest ->requestor ->fullname ,
107+ 'START_DATE ' => $ coeRequest ->requestor ->lifecycle ->start_date ,
108+ 'END_DATE ' => $ coeRequest ->requestor ->lifecycle ->separated_at ,
109+ 'JOB_TITLE ' => $ coeRequest ->requestor ->jobTitle ->job_title ,
110+ 'DEPT_NAME ' => $ coeRequest ->requestor ->jobTitle ->department ->department_name ,
111+ 'ORDINAL ' => $ issueDay ,
112+ 'MONTH ' => $ issueMonth ,
113+ 'YEAR ' => $ issueYear ,
114+ 'COMPANY_ADDRESS ' => 'Rowsuz Business Center, Diversin Rd ' ,
115+ 'HRManager_NAME ' => auth ()->user ()->account ->fullname ,
116+ ];
117+
118+ $ signature = null ;
119+
120+
121+ if (auth ()->user ()->account ->signature ) {
122+ $ signatureData = auth ()->user ()->account ->signature ;
123+
124+ if (is_string ($ signatureData )) {
125+ $ signature = Storage::disk ('public ' )->path ($ signatureData );
126+ } else {
127+ $ signature = $ signatureData ;
128+ }
129+ }
130+ try {
131+ $ templateProcessor ->setImageValue ('USER_SIGNATURE ' , $ signature );
132+ } catch (\Throwable $ th ) {
133+ $ signature = Storage::disk ('public ' )->path (FilePath::DEFAULT_SIGNATURE ->value );
134+ $ templateProcessor ->setImageValue ('USER_SIGNATURE ' , $ signature );
135+ report ($ th );
136+ }
137+
138+ $ templateProcessor ->setValues ($ values );
139+ $ templateProcessor ->setImageValue ('USER_SIGNATURE ' , $ signature );
140+
141+ $ docxFilePath = FilePath::COE ->value . hash ('sha256 ' , time ()) . '.docx ' ;
142+ $ templateProcessor ->saveAs (Storage::disk ('public ' )->path ($ docxFilePath ));
143+
144+ $ disk = 'public ' ;
145+
146+ try {
147+ $ pdfFilePath = $ this ->convert ($ docxFilePath , FilePath::COE ->value , $ disk );
148+ Storage::disk ('public ' )->delete ($ docxFilePath );
149+
150+ return $ pdfFilePath ;
151+ } catch (\Throwable $ th ) {
152+ report ($ th );
153+ return response ()->json (['error ' => $ th ->getMessage ()], 500 );
154+ }
85155 }
86156
87157 /**
@@ -110,7 +180,6 @@ public function update($coe, $coePath)
110180 'separated_at ' => now (),
111181 ]);
112182 });
113-
114183 }
115184
116185 /**
0 commit comments