Files
aso/Modules/Internal/Http/Controllers/Api/Linksehat/PaymentController.php
Server D3 Linksehat 1bf608b1ed Server 103 Commit
2024-07-18 16:05:33 +07:00

318 lines
11 KiB
PHP
Executable File

<?php
namespace Modules\Internal\Http\Controllers\Api\Linksehat;
use App\Helpers\Helper;
use App\Models\OLDLMS\Appointment;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Contracts\Support\Renderable;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller;
use Modules\Internal\Transformers\LinksehatPaymentResource;
use Carbon\Carbon;
use Maatwebsite\Excel\Facades\Excel;
use Box\Spout\Reader\Common\Creator\ReaderEntityFactory;
use Box\Spout\Writer\Common\Creator\WriterEntityFactory;
class PaymentController extends Controller
{
/**
* Display a listing of the resource.
* @return Renderable
*/
public function index(Request $request)
{
// return $request->toArray();
$appointments = Appointment::query()
->where('sPaymentStatus', 'settlement')
->with(['healthCare', 'healthCare.commission', 'detail', 'user', 'doctor', 'doctor.user']);
if ($request->has('search')) {
$appointments->where(function ($query) use ($request) {
$query->where('nID', $request->search)
->orWhere('sBookingCode', $request->search)
->orWhereHas('detail', function (Builder $detail) use ($request) {
$detail->where('sPaymentDetails', 'LIKE', '%' . $request->search . "%");
});
});
}
if (($request->has('appointment_start') || $request->has('appointment_end'))
&& !empty($request->appointment_start)
&& !empty($request->appointment_end)
) {
// $appointments = $appointments->whereHas('detail', function (Builder $detail) use ($request) {
// // Appointment Start
// // if ($request->has('appointment_start')) {
// $detail->where('dTanggalAppointment', '>=', $request->appointment_start);
// // } else {
// // $detail->where('dTanggalAppointment', '>', now()->format('Y-m-d'));
// // }
// // if ($request->has('appointment_end')) {
// $detail->where('dTanggalAppointment', '<=', $request->appointment_end);
// // } else {
// // $detail->where('dTanggalAppointment', '<', now()->addDay(1)->format('Y-m-d'));
// // }
// });
$appointments = $appointments->where(function($q) use ($request) {
$q->where('dCreateOn', '>=', $request->appointment_start)
->where('dCreateOn', '<=', $request->appointment_end);
});
}
if ($request->has('payment_status') && $request->payment_status != 'semua') {
$appointments->where('sPaymentStatus', $request->payment_status);
}
if ($request->has('healthcare_id') && !empty($request->healthcare_id)) {
$appointments->where('nIDHealthCare', $request->healthcare_id);
}
$appointments = $appointments->orderBy('dUpdateOn', 'DESC')
->paginate();
return Helper::responseJson(Helper::paginateResources(LinksehatPaymentResource::collection($appointments)));
}
/**
* Show the form for creating a new resource.
* @return Renderable
*/
public function create()
{
return view('internal::create');
}
/**
* Store a newly created resource in storage.
* @param Request $request
* @return Renderable
*/
public function store(Request $request)
{
//
}
/**
* Show the specified resource.
* @param int $id
* @return Renderable
*/
public function show($id)
{
return view('internal::show');
}
/**
* Show the form for editing the specified resource.
* @param int $id
* @return Renderable
*/
public function edit($id)
{
return view('internal::edit');
}
/**
* Update the specified resource in storage.
* @param Request $request
* @param int $id
* @return Renderable
*/
public function update(Request $request, $id)
{
//
}
/**
* Remove the specified resource from storage.
* @param int $id
* @return Renderable
*/
public function destroy($id)
{
//
}
public function generateExcel(Request $request){
Helper::setCustomPHPIniSettings();
$file_name = 'Data Report Appointment';
// Membuat penulis entitas Spout
$writer = WriterEntityFactory::createXLSXWriter();
// Membuka penulis untuk menulis ke file
$writer->openToFile(public_path('files/Report-Appointment.xlsx'));
$headerArray = [
'Faskes Name',
'Tanggal Bayar',
'Tanggal Konsultasi',
'Id Pemesanan',
'Kode Pemesanan',
'Dokter',
'Pasien',
'Email',
'Tipe',
'Metode Pembayaran',
'Jenis Benefit',
'Total Transfer',
'Konsultasi',
'Komisi',
'Biaya Admin',
'Status',
];
// Sheet 1
$writer->getCurrentSheet()->setName('Data');
$headers_map_to_table_fields = $headerArray;
$headerRow = WriterEntityFactory::createRowFromArray($headers_map_to_table_fields);
$writer->addRow($headerRow);
$dataReportPayment = Appointment::query()
->where('sPaymentStatus', 'settlement')
->with(['healthCare', 'healthCare.commission', 'detail', 'user', 'doctor', 'doctor.user']);
if (($request->has('appointment_start') || $request->has('appointment_end'))
&& !empty($request->appointment_start)
&& !empty($request->appointment_end)
) {
$dataReportPayment = $dataReportPayment->where(function($q) use ($request) {
$q->where('dCreateOn', '>=', $request->appointment_start)
->where('dCreateOn', '<=', $request->appointment_end);
});
}
$dataReportPayment = $dataReportPayment->get(); // Pastikan get() dipanggil setelah semua filter
foreach ($dataReportPayment as $index => $row){
$jenisTelekonsul = '-';
switch ($row->nIDJenisBooking) {
case 1:
$jenisTelekonsul = 'Rawat Jalan';
break;
case 2:
$jenisTelekonsul = 'Telekonsul';
break;
case 3:
$jenisTelekonsul = 'Chat Sekarang';
break;
}
$jenisPayment = '-';
switch ($row->sPaymentMethod) {
case 1:
$jenisPayment = 'Pribadi';
break;
case 2:
$jenisPayment = 'On-site payment';
break;
case 3:
$jenisPayment = 'OVO';
break;
case 4:
$jenisPayment = 'Benefit Insurance';
break;
case 5:
$jenisPayment = 'Voucher';
break;
}
$sStatus = '-';
switch ($row->sStatus) {
case 0:
$sStatus = 'Menunggu Pembayaran';
break;
case 1:
$sStatus = 'Pembayaran Terkonfirmasi';
break;
case 2:
$sStatus = 'Ditolak';
break;
case 3:
$sStatus = 'Dibatalkan';
break;
case 4:
$sStatus = 'Expired';
break;
}
$paymentDetail = $row->detail->sPaymentDetails;
$grossAmount = 0;
if ($paymentDetail) {
$grossAmount = $paymentDetail['gross_amount'] ?? 0;
}
$consulPrice = (float) $grossAmount - $row->nAdminFee;
if ($row->nIDJenisBooking == 3) { // Telekonsultasi Sekarang
// $consulPrice = $consulPrice * (100 - $row->healthCare->commission->nCommissionATC) / 100;
$consulPrice = $consulPrice * (100 - 2.5) / 100;
} else if ($row->nIDJenisBooking == 2) { // Telekonsultasi
$consulPrice = $consulPrice * (100 - 2.5) / 100;
// $consulPrice = $consulPrice * (100 - $row->healthCare->commission->nCommissionTC) / 100;
} else { // Walk In
// $consulPrice = $consulPrice * (100 - $row->healthCare->commission->nCommission) / 100;
$consulPrice = $consulPrice * (100 - 5) / 100;
}
$komisiPrice = (float) $grossAmount - $row->nAdminFee;
if ($row->nIDJenisBooking == 3) { // Telekonsultasi Sekarang
// $komisiPrice = $komisiPrice * ($row->healthCare->commission->nCommissionATC) / 100;
$komisiPrice = $komisiPrice * (2.5) / 100;
} else if ($row->nIDJenisBooking == 2) { // Telekonsultasi
// $komisiPrice = $komisiPrice * ($row->healthCare->commission->nCommissionTC) / 100;
$komisiPrice = $komisiPrice * (2.5) / 100;
} else { // Walk In
// $komisiPrice = $komisiPrice * ($row->healthCare->commission->nCommission) / 100;
$komisiPrice = $komisiPrice * (5) / 100;
}
// dd($row->healthCare->commission->nCommissionTC);
if ($row->doctor->user){
$firstNameDokter = $row->doctor->user->sFirstName . ' ' . $row->doctor->user->sLastName;
} else {
$firstNameDokter = $row->doctor->sIDDOkter ? $row->doctor->sIDDOkter : '-';
}
if ($row->user){
$firstNamePasien = $row->user->sFirstName. ' ' . $row->user->sLastName;
$sEmail = $row->user->sEmail;
} else {
$firstNamePasien = '-';
$sEmail = '-';
}
$rowData = [
$row->healthCare->sHealthCare, // nama faskes
Carbon::parse($row->dCreateOn)->format('Y-m-d H:i:s'), // tanggal bayar
$row->detail->dTanggalAppointment,
$row->nID,
$row->sBookingCode,
$firstNameDokter,
$firstNamePasien,
$sEmail,
$jenisTelekonsul,
$jenisPayment,
'-', // Jenis Benefit, as per original code
$grossAmount,
$consulPrice,
$komisiPrice,
$row->nAdminFee, // Biaya admin
$row->sPaymentStatus, // Status
];
$row = WriterEntityFactory::createRowFromArray($rowData);
$writer->addRow($row);
}
$writer->close();
return Helper::responseJson([
'file_name' => "Data Request Log " . date('Y-m-d h:i:s'),
"file_url" => url('files/Report-Appointment.xlsx')
]);
}
}