135 lines
4.3 KiB
PHP
135 lines
4.3 KiB
PHP
|
|
<?php
|
||
|
|
|
||
|
|
namespace App\Repositories\Backend\Certificate;
|
||
|
|
|
||
|
|
use App\Constant\Constant;
|
||
|
|
use App\Jobs\CertificateApprovedJob;
|
||
|
|
use App\Jobs\CertificateRejectionJob;
|
||
|
|
use App\Models\UserHealthInsuranceCertificate;
|
||
|
|
use App\Services\BaseService;
|
||
|
|
use Barryvdh\DomPDF\Facade\Pdf;
|
||
|
|
use Carbon\Carbon;
|
||
|
|
use Yajra\DataTables\Facades\DataTables;
|
||
|
|
|
||
|
|
class CertificateService extends BaseService
|
||
|
|
{
|
||
|
|
const MODEL = UserHealthInsuranceCertificate::class;
|
||
|
|
|
||
|
|
public function getListing($request): object
|
||
|
|
{
|
||
|
|
$dataTableQuery = $this->prepareQuery($request->all());
|
||
|
|
return DataTables::of($dataTableQuery)
|
||
|
|
->addColumn(
|
||
|
|
'checkbox_action',
|
||
|
|
function ($hospital) {
|
||
|
|
return $hospital?->hospital_checkbox_action;
|
||
|
|
}
|
||
|
|
)
|
||
|
|
->addColumn(
|
||
|
|
'action',
|
||
|
|
function ($hospital) {
|
||
|
|
return $hospital?->certificate_action_buttons;
|
||
|
|
}
|
||
|
|
)
|
||
|
|
->addColumn(
|
||
|
|
'hospital_name',
|
||
|
|
function ($hospital) {
|
||
|
|
return $hospital?->hospital?->name;
|
||
|
|
}
|
||
|
|
)
|
||
|
|
->addColumn(
|
||
|
|
'mobile_number',
|
||
|
|
function ($hospital) {
|
||
|
|
return $hospital?->user?->mobile;
|
||
|
|
}
|
||
|
|
)
|
||
|
|
->editColumn(
|
||
|
|
'generated_date',
|
||
|
|
function ($hospital) {
|
||
|
|
return ($hospital?->generated_date) ? Carbon::parse($hospital?->generated_date)->format('d-m-Y H:i:s') : '-';
|
||
|
|
}
|
||
|
|
)
|
||
|
|
->editColumn(
|
||
|
|
'status',
|
||
|
|
function ($hospital) {
|
||
|
|
return $hospital?->certificate_status_action;
|
||
|
|
}
|
||
|
|
)
|
||
|
|
->addIndexColumn()
|
||
|
|
->rawColumns(['action', 'mobile_number', 'status', 'checkbox_action'])
|
||
|
|
->make(true);
|
||
|
|
}
|
||
|
|
|
||
|
|
public function prepareQuery($request): object
|
||
|
|
{
|
||
|
|
$searchQuery = $this->query()->with(['hospital','user']);
|
||
|
|
|
||
|
|
$columns = ['created_at', 'hospital_id', 'full_name', 'email', 'aadhar_card', 'generated_date', 'status'];
|
||
|
|
|
||
|
|
$searchQuery = $searchQuery->when(array_key_exists('full_name', $request), function ($q) use ($request) {
|
||
|
|
return $q->where('full_name', 'like', '%' . $request['name'] . '%');
|
||
|
|
});
|
||
|
|
|
||
|
|
if (isset($request['order']) && $request['order'] != "") {
|
||
|
|
$searchQuery = $searchQuery->orderBy(
|
||
|
|
$columns[$request['order'][0]['column']],
|
||
|
|
$request['order'][0]['dir']
|
||
|
|
);
|
||
|
|
}
|
||
|
|
|
||
|
|
return $searchQuery;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @param $id
|
||
|
|
* @param $request
|
||
|
|
* @return void
|
||
|
|
*/
|
||
|
|
public function statusUpdate($id, $request): array
|
||
|
|
{
|
||
|
|
$response['status'] = Constant::STATUS_ZERO;
|
||
|
|
|
||
|
|
$certificate = $this->query()->where('id', $id)->first();
|
||
|
|
|
||
|
|
if ($certificate) {
|
||
|
|
|
||
|
|
$certificate->rejection_reason = ($request?->status == Constant::STATUS_TWO) ? $request?->rejection_reason : '';
|
||
|
|
$certificate->status = $request?->status;
|
||
|
|
$certificate->generated_date = ($request?->status == 1) ? now() : null;
|
||
|
|
$certificate->pdf_url = ($request?->status == 1) ? $this->generatePDF($certificate) : null;
|
||
|
|
$certificate->save();
|
||
|
|
|
||
|
|
if ($request?->status == 1) {
|
||
|
|
dispatch(new CertificateApprovedJob($certificate));
|
||
|
|
} elseif ($request?->status == 2) {
|
||
|
|
dispatch(new CertificateRejectionJob($certificate));
|
||
|
|
}
|
||
|
|
|
||
|
|
$response['status'] = Constant::STATUS_ONE;
|
||
|
|
}
|
||
|
|
|
||
|
|
return $response;
|
||
|
|
}
|
||
|
|
|
||
|
|
/**
|
||
|
|
* @param $certificate
|
||
|
|
* @return string
|
||
|
|
*/
|
||
|
|
public function generatePDF($certificate): string
|
||
|
|
{
|
||
|
|
$certificate->load('hospital');
|
||
|
|
$expireDate = Carbon::parse($certificate->generated_date)->addDays(2);
|
||
|
|
$expireDate->format('Y-m-d H:i:s');
|
||
|
|
$certificate->toArray();
|
||
|
|
$pdf = Pdf::loadView('backend.certificate.pdf', compact('certificate','expireDate'))
|
||
|
|
->setPaper('a4')
|
||
|
|
->setWarnings(false);
|
||
|
|
|
||
|
|
$name = 'JHC-'.str_replace(' ','-',$certificate->full_name).'-'.Carbon::parse(now())->format('d-m-Y').'.pdf';
|
||
|
|
$path = Constant::USER_CERTIFICATES_UPLOAD_PATH . $name;
|
||
|
|
$pdf->save($path,env('FILESYSTEM_DRIVER'));
|
||
|
|
|
||
|
|
return $name;
|
||
|
|
}
|
||
|
|
}
|