Files
aso/Modules/Linksehat/Transformers/Home/HomeResource.php
2024-05-06 08:15:58 +07:00

281 lines
11 KiB
PHP

<?php
namespace Modules\Linksehat\Transformers\Home;
use Illuminate\Http\Resources\Json\JsonResource;
use App\Models\OLDLMS\User;
use App\Models\OLDLMS\UserDetail;
use App\Models\OLDLMS\UserInsurance;
use App\Models\Organization;
use App\Models\Practitioner;
use App\Models\PractitionerRole;
use App\Models\Member;
use App\Models\Person;
use App\Models\CorporateEmployee;
use App\Models\Corporate;
use Illuminate\Support\Facades\Auth;
use Carbon\Carbon;
use App\Helpers\Helper;
use DB;
class HomeResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request
* @return array
*/
protected $request;
protected $connection = 'oldlms';
public function __construct($resource, $request)
{
$this->request = $request;
parent::__construct($resource);
}
public function toArray($request)
{
$memberProfile = User::with('detail')->where('nIDUser', $this->nID)->get()->toArray();
$dataMemberProfile = [];
$userInsurance = UserInsurance::where('nIDUser', $this->nID)->get()->first();
$memberId = null;
$linking = false;
$jam = date('G'); // Ambil jam dalam format 24 jam
if ($jam < 12) {
$wellcome = "Good Morning!";
} elseif ($jam < 18) {
$wellcome = "Good Afternoon!";
} else {
$wellcome = "Good Evening!";
}
if($userInsurance){
$memberId = $userInsurance->sNoPolis;
$linking = true;
} else {
$member = Member::where('email', $this->sEmail)->get()->first();
$person = Person::where('phone', $this->sPhone)->get()->first();
if ($member || $person){ // Autolinking
$corporateEmployee = CorporateEmployee::where('member_id', $member->id)->get()->first(); // cek corporate id empolyee/member
if ($corporateEmployee){
$corporate = Corporate::findOrFail($corporateEmployee->corporate_id)->automatic_linking; // cek autocomplete
if ($corporate){
if($member) {
// Insert into database linksehat
$insurance = UserInsurance::updateOrCreate(
[
'nIDUser' => $this->nID,
],
[
'nIDUser' => $this->nID,
'nIDInsurance' => $_ENV['LINKSEHAT_ASO_INSURANCE_ID'],
'sNoPolis' => $member->member_id,
'sNamaPeserta' => $member->fullName,
'sKartuPeserta' => '',
'sLayanan' => 'RJ,TC',
'dStartDate' => $member->members_effective_date,
'dExpireDate' => $member->members_expire_date,
'dTanggalLahir' => $member->birth_date,
'nNoKTP' => $member->nric != '' ? $member->nric : 0 ,
'sIsConfrimed' => 1,
'sStatus' => 1,
]);
$message = $member->currentPolicy->corporate->welcome_message;
$linking = true;
$memberId = $member->member_id;
}
}
}
}
};
if (count($memberProfile) > 0){
$urlAvatarDefault = $this->detail->nIDJenisKelamin == 1 ? 'https://linksehat.dev/assets/img/users/male-avatar.png' : 'https://linksehat.dev/assets/img/users/female-avatar.png';
$avatarMember = $this->detail->sImage ?? $urlAvatarDefault;
$relationship = DB::connection('oldlms')->table('tm_hubungan_keluarga')->where('nID', $this->nIDHubunganKeluarga)->first('sHubunganKeluarga');
$dataUser = [
'id' => $this->nID,
'name' => $this->sFirstName . ' ' . $this->sLastName,
'relationship' => $relationship ? $relationship->sHubunganKeluarga : '-',
'avatar' => $avatarMember
];
array_push($dataMemberProfile, $dataUser);
foreach($memberProfile as $m){
$urlAvatarDefault = $m['detail']['nIDJenisKelamin'] == 1 ? 'https://linksehat.dev/assets/img/users/male-avatar.png' : 'https://linksehat.dev/assets/img/users/female-avatar.png';
$avatarMember = $m['detail']['sImage'] ?? $urlAvatarDefault;
$relationship = DB::connection('oldlms')->table('tm_hubungan_keluarga')->where('nID', $m['nIDHubunganKeluarga'])->first('sHubunganKeluarga');
$data = [
'id' => $m['nID'],
'name' => $m['full_name'],
'relationship' => $relationship ? $relationship->sHubunganKeluarga : '-',
'avatar' => $avatarMember,
];
array_push( $dataMemberProfile, $data);
}
} else {
$nID = $this->nIDUser ? $this->nIDUser : $this->nID;
if ($nID){
$memberProfile = User::with('detail')->where('nIDUser', $nID)->get()->toArray();
$dataMember = User::with('detail')->where('nID', $nID)->get()->first();
if ($this->detail){
$urlAvatarDefault = $this->detail->nIDJenisKelamin == 1 ? 'https://linksehat.dev/assets/img/users/male-avatar.png' : 'https://linksehat.dev/assets/img/users/female-avatar.png';
} else {
$urlAvatarDefault = 'https://linksehat.dev/assets/img/users/male-avatar.png';
}
$avatar = $this->detail->sImage ?? $urlAvatarDefault;
$avatarMember = $dataMember->detail->sImage ?? $urlAvatarDefault;
$relationship = DB::connection('oldlms')->table('tm_hubungan_keluarga')->where('nID', $this->nIDHubunganKeluarga)->first('sHubunganKeluarga');
$dataUser = [
'id' => $dataMember->nID,
'name' => $dataMember->sFirstName . ' ' . $dataMember->sLastName,
'relationship' => 'Me',
'avatar' => $avatarMember
];
array_push($dataMemberProfile, $dataUser);
if (count($memberProfile) > 0){
foreach($memberProfile as $m){
$urlAvatarDefault = $m['detail']['nIDJenisKelamin'] == 1 ? 'https://linksehat.dev/assets/img/users/male-avatar.png' : 'https://linksehat.dev/assets/img/users/female-avatar.png';
$avatarMember = $m['detail']['sImage'] ?? $urlAvatarDefault;
$relationship = DB::connection('oldlms')->table('tm_hubungan_keluarga')->where('nID', $m['nIDHubunganKeluarga'])->first('sHubunganKeluarga');
$data = [
'id' => $m['nID'],
'name' => $m['full_name'],
'relationship' => $relationship->sHubunganKeluarga,
'avatar' => $avatarMember,
];
array_push( $dataMemberProfile, $data);
}
}
} else {
}
}
// Principal
if ($this->detail){
$urlAvatarDefault = $this->detail->nIDJenisKelamin == 1 ? 'https://linksehat.dev/assets/img/users/male-avatar.png' : 'https://linksehat.dev/assets/img/users/female-avatar.png';
} else {
$urlAvatarDefault = 'https://linksehat.dev/assets/img/users/male-avatar.png';
}
$avatar = $this->detail->sImage ?? $urlAvatarDefault;
// Doctor livechat
$doctors = Practitioner::with('person', 'practitionerRoles.organization', 'practitionerRoles.speciality')
->whereHas('person', function ($query) {
$query->where('isOnline', 1); // hanya online
})
->get()
->toArray();
$doctorsLivechat = [];
foreach($doctors as $doctor){
$specialist = 'Umum';
$year = 0;
$price = 0;
if (!empty($doctor['person']['start_date_work'])) {
$starExperience = Carbon::parse($doctor['person']['start_date_work'])->format('Y-m-d');
$experience = Carbon::createFromFormat('Y-m-d', $starExperience);
$year = $experience->diffInYears(Carbon::now());
}
if ($doctor['practitioner_roles']) {
if ($doctor['practitioner_roles'][0]['speciality']){
$specialist = $doctor['practitioner_roles'][0]['speciality']['name'];
}
if ($doctor['practitioner_roles'][0]['price']){
$price = $doctor['practitioner_roles'][0]['price'];
}
}
$data = [
'id' => $doctor['id'],
'full_name' => $doctor['person']['name'],
'specialist' => $specialist,
'experience' => $year,
'review' => $doctor['person']['review'],
'price' => $price,
'price_real' => $price
];
array_push($doctorsLivechat, $data);
}
// Hospital List
$hospitalList = [];
$hospitals = Organization::where([
'type' => 'hospital',
'status' => 'active',
])
->with('currentAddress')
->get()->toArray();
foreach($hospitals as $hospital){
$lat = 0;
$lang = 0;
if ($hospital['current_address']['lat']){
$lat = $hospital['current_address']['lat'];
}
if ($hospital['current_address']['lng']){
$lang = $hospital['current_address']['lng'];
}
$address = '';
if ($hospital['current_address']['text']){
$address = $hospital['current_address']['text'];
}
$radius = 0;
if ($lat && $lang && $request->longitude && $request->latitude){
$radius = round(Helper::calculateDistance($lat, $lang, $request->latitude, $request->longitude), 2);
}
$data = [
'name' => $hospital['name'],
'radius' => $radius,
'image' => '',
'address' => $address
];
array_push($hospitalList, $data);
}
usort($hospitalList, function($a, $b) {
return $a['radius'] <=> $b['radius'];
});
$hospitalList = array_slice($hospitalList, 0, 5);
return [
'id' => $this->nID,
'message' => $wellcome,
'full_name' => $this->sFirstName . ' '. $this->sLastName,
'avatar' => $avatar,
'member_id' => $memberId,
'linking' => $linking,
'doctors_livechat' => $doctorsLivechat,
'hospital' => $hospitalList
];
}
}