336 lines
13 KiB
PHP
336 lines
13 KiB
PHP
<?php
|
|
|
|
namespace Modules\Linksehat\Http\Controllers\Api;
|
|
|
|
use App\Notifications\SendNotification;
|
|
use App\Helpers\Helper;
|
|
use App\Helpers\DuitkuHelper;
|
|
use App\Services\Duitku;
|
|
use App\Models\Organization;
|
|
use App\Models\PractitionerRole;
|
|
use App\Models\Invoice;
|
|
use App\Models\PaymentsMethods;
|
|
use App\Models\Channel;
|
|
use App\Models\Livechat;
|
|
use App\Models\OLDLMS\User;
|
|
use App\Models\User as UserAso;
|
|
use Illuminate\Routing\Controller;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\Http;
|
|
use Modules\Linksehat\Transformers\Livechat\LivechatResource;
|
|
use Illuminate\Support\Facades\Validator;
|
|
use App\Http\Controllers\DuitkuController;
|
|
|
|
use DB;
|
|
use Str;
|
|
|
|
class LivechatController extends Controller
|
|
{
|
|
public function index(Request $request)
|
|
{
|
|
$user = User::with('detail')
|
|
->where('nId', $request->id)
|
|
->first();
|
|
return Helper::responseJson([
|
|
'livechat' => LivechatResource::make($user, $request),
|
|
]);
|
|
}
|
|
|
|
public function consultation(Request $request)
|
|
{
|
|
$dataMemberProfile = [];
|
|
$user = User::with('detail')
|
|
->where('nId', $request->member_id)
|
|
->first();
|
|
$memberProfile = User::with('detail')->where('nIDUser', $request->member_id)->get()->toArray();
|
|
if (count($memberProfile) > 0){
|
|
$urlAvatarDefault = $user->detail->nIDJenisKelamin == 1 ? 'https://linksehat.dev/assets/img/users/male-avatar.png' : 'https://linksehat.dev/assets/img/users/female-avatar.png';
|
|
$avatarMember = $user->detail->sImage ?? $urlAvatarDefault;
|
|
$relationship = DB::connection('oldlms')->table('tm_hubungan_keluarga')->where('nID', $user->nIDHubunganKeluarga)->first('sHubunganKeluarga');
|
|
|
|
$dataUser = [
|
|
'id' => $user->nID,
|
|
'name' => $user->sFirstName . ' ' . $user->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 = $user->nIDUser ? $user->nIDUser : $user->nID;
|
|
if ($nID){
|
|
$memberProfile = User::with('detail')->where('nIDUser', $nID)->get()->toArray();
|
|
|
|
$dataMember = User::with('detail')->where('nID', $nID)->get()->first();
|
|
|
|
if ($user->detail){
|
|
$urlAvatarDefault = $user->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 = $user->detail->sImage ?? $urlAvatarDefault;
|
|
|
|
$avatarMember = $dataMember->detail->sImage ?? $urlAvatarDefault;
|
|
$relationship = DB::connection('oldlms')->table('tm_hubungan_keluarga')->where('nID', $user->detail->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);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
return Helper::responseJson([
|
|
'member' => $dataMemberProfile
|
|
]);
|
|
}
|
|
|
|
public function consultation_request(Request $request)
|
|
{
|
|
$data = [
|
|
'doctor_id' => $request->doctor_id,
|
|
'patient_id' => $request->patient_id,
|
|
'organization_id' => $request->organization_id,
|
|
'descriptions' => $request->descriptions
|
|
];
|
|
|
|
$validator = Validator::make($request->all(), [
|
|
'doctor_id' => 'required',
|
|
'patient_id' => 'required',
|
|
'descriptions' => 'required',
|
|
], [
|
|
'doctor_id.required' => 'ID Dokter harus diisi',
|
|
'patient_id.required' => 'ID Pasien harus diisi',
|
|
'descriptions.required' => 'Deskripsi harus diisi',
|
|
]);
|
|
|
|
if ($validator->fails()) {
|
|
return Helper::responseJson(
|
|
status: 'Bad Request',
|
|
statusCode: 400,
|
|
message: $validator->errors()
|
|
);
|
|
} else {
|
|
// insert table livechat
|
|
|
|
/**
|
|
* Status Livechat
|
|
* 1=Request, 2=Accept, 3=Decline, 4=Waiting Payment, 5=Success Payment, 6 = End Chat, 7=Payment Failed
|
|
*/
|
|
|
|
$timezone = date_default_timezone_get();
|
|
$data['request_date'] = date('Y-m-d H:i:s');
|
|
$data['timezone'] = $timezone;
|
|
$data['uuid'] = (string) Str::orderedUuid();
|
|
$data['status'] = 1; // Request
|
|
|
|
$livechat = Livechat::create($data);
|
|
$doctor = $livechat->doctor;
|
|
$responseData = [
|
|
'id' => $livechat->id,
|
|
'request_date' => $livechat->request_date,
|
|
'image_path' => 'https' // Ganti dengan path yang benar jika ada
|
|
];
|
|
|
|
// Send Notification
|
|
$doctorId = $livechat->doctor_id;
|
|
$user = UserAso::find($doctorId);
|
|
$title = 'New Request Livechat';
|
|
$patient = User::where('nID', $livechat->patient_id)->first();
|
|
$body = 'Request Livechat from ' . $patient->sFirstName . ' ' . $patient->sLastName;
|
|
$channel = Channel::where([
|
|
'member_id' => $livechat->patient_id,
|
|
'doctor_id' => $livechat->doctor_id
|
|
])->first();
|
|
$dataNotif = [
|
|
'channel_id' => $channel->id,
|
|
'livechat_id' => $livechat->id,
|
|
'type' => 'request-chat'
|
|
];
|
|
|
|
if ($user) {
|
|
$user->notify(new SendNotification($title, $body, $dataNotif));
|
|
return Helper::responseJson(data: $responseData);
|
|
} else {
|
|
return Helper::responseJson(
|
|
status: 'Not Found',
|
|
statusCode: 404,
|
|
message: 'Doctor not found.'
|
|
);
|
|
}
|
|
}
|
|
}
|
|
|
|
public function consultation_request_show($id){
|
|
$livechat = Livechat::where('id', $id)->with(['doctor', 'practitioner'])->first();
|
|
$practitionerRole = PractitionerRole::where('id',$livechat->practitioner->id)->first();
|
|
|
|
$price = $practitionerRole->price ? $practitionerRole->price : 30000;
|
|
$discount = 0;
|
|
$adminFee = 5000;
|
|
$totalPay = $price + $adminFee - $discount;
|
|
$data = [
|
|
'id' => $livechat->id,
|
|
'code_transaksi' => $livechat->uuid,
|
|
'doctor_id' => $livechat->doctor_id,
|
|
'doctor_name' => $livechat->doctor->name,
|
|
'doctor_specialist' => 'Umum',
|
|
'price' => $price,
|
|
'admin_price' => $adminFee,
|
|
'promo' => [
|
|
[
|
|
'id' => 1,
|
|
'code' => 'SEHATBERSAMA',
|
|
'discount_percent' => 20
|
|
],
|
|
[
|
|
'id' => 2,
|
|
'code' => 'MARET MERIAH',
|
|
'discount_percent' => 5
|
|
],
|
|
],
|
|
'total' => $totalPay
|
|
|
|
];
|
|
return Helper::responseJson(data: $data);
|
|
}
|
|
|
|
public function consultation_payment_choose($id){
|
|
$livechat = Livechat::where('id', $id)->with(['doctor', 'practitioner'])->first();
|
|
$practitionerRole = PractitionerRole::where('id',$livechat->practitioner->id)->first();
|
|
$eWallet = PaymentsMethods::where(
|
|
[
|
|
'active' => 1,
|
|
'config_pmc_id' => 3,
|
|
])->get()->toArray();
|
|
$va = PaymentsMethods::where(
|
|
[
|
|
'active' => 1,
|
|
'config_pmc_id' => 2,
|
|
])->get()->toArray();
|
|
|
|
$payment = DuitkuHelper::paymentMethod();
|
|
|
|
$price = $practitionerRole->price ? $practitionerRole->price : 30000;
|
|
$discount = 0;
|
|
$adminFee = 5000;
|
|
$totalPay = $price + $adminFee - $discount;
|
|
$data = [
|
|
'id' => $livechat->id,
|
|
'code_transaksi' => $livechat->uuid,
|
|
'price' => $price,
|
|
'admin_price' => $adminFee,
|
|
'total' => $totalPay,
|
|
'payment_method' => [
|
|
'ewallet' => $eWallet,
|
|
'va' => $va
|
|
]
|
|
// 'payment_method' => json_decode($payment)
|
|
|
|
];
|
|
return Helper::responseJson(data: $data);
|
|
}
|
|
|
|
public function consultation_payment(Request $request)
|
|
{
|
|
try {
|
|
// Mengambil data Livechat dengan relasi doctor dan practitioner
|
|
$livechat = Livechat::with(['doctor', 'practitioner'])->find($request->consultation_id);
|
|
|
|
if (!$livechat) {
|
|
return response()->json(['success' => false, 'message' => 'Consultation not found'], 404);
|
|
}
|
|
|
|
// Update status
|
|
$livechat->status = 4;
|
|
$livechat->save();
|
|
|
|
$practitionerRole = PractitionerRole::find($livechat->practitioner->id);
|
|
$price = $practitionerRole->price ?? 30000; // Gunakan null coalescing operator
|
|
$adminFee = 5000;
|
|
$discount = 0;
|
|
$totalPay = $price + $adminFee - $discount;
|
|
|
|
// Mengambil user dari database
|
|
$user = User::with('detail')->where('nId', $livechat->patient_id)->first();
|
|
$address = DB::connection('oldlms')->table('tm_users_address')->where('nIDUser', $user->nID)->first('sAlamat');
|
|
if($address){
|
|
$address = $address->sAlamat;
|
|
}
|
|
if (!$user) {
|
|
return response()->json(['success' => false, 'message' => 'User not found'], 404);
|
|
}
|
|
|
|
// Menyiapkan data untuk invoice
|
|
$data = [
|
|
'paymentMethod' => $request->payment_code,
|
|
'paymentAmount' => $totalPay,
|
|
'email' => $user->sEmail,
|
|
'phoneNumber' => $user->sPhone,
|
|
'productDetails' => 'INV-' . date('Ymd') . '-' . rand(100, 999),
|
|
'merchantOrderId' => $livechat->uuid,
|
|
'additionalParam' => '',
|
|
'merchantUserInfo' => '',
|
|
'customerVaName' => $user->sFirstName . ' ' . $user->sLastName,
|
|
'firstName' => $user->sFirstName,
|
|
'lastName' => $user->sLastName,
|
|
'alamat' => $address,
|
|
'city' => '',
|
|
'postalCode' => ''
|
|
];
|
|
|
|
// Membuat invoice menggunakan DuitkuHelper
|
|
$duitku = DuitkuHelper::createInvoice($data);
|
|
|
|
return response()->json(['success' => true, 'data' => $duitku], 200);
|
|
} catch (Exception $e) {
|
|
// Menangkap error dan mengembalikan respon error
|
|
return response()->json(['success' => false, 'message' => $e->getMessage()], 500);
|
|
}
|
|
}
|
|
|
|
public function consultation_check_payment($id){
|
|
$livechat = Livechat::where('id',$id)->with(['doctor', 'practitioner'])->first();
|
|
$duitku = DuitkuHelper::checkStatus($livechat->uuid);
|
|
|
|
return $duitku;
|
|
|
|
}
|
|
}
|