Files
aso/Modules/Internal/Http/Controllers/Api/DoctorRatingController.php
Linksehat Staging Server 70fc1579e7 update
2024-07-12 08:41:18 +07:00

278 lines
9.7 KiB
PHP
Executable File

<?php
namespace Modules\Internal\Http\Controllers\Api;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use Illuminate\Support\Facades\DB;
use App\Helpers\Helper;
use App\Models\OLDLMS\DoctorRating;
use Box\Spout\Writer\Common\Creator\WriterEntityFactory;
use Box\Spout\Writer\Common\Creator\Style\StyleBuilder;
use Box\Spout\Common\Entity\Style\CellAlignment;
class DoctorRatingController extends Controller
{
/**
* Display a listing of the resource.
* @param int|null $id
* @return \Illuminate\Http\JsonResponse
*/
public function index($id = null)
{
$query = DoctorRating::query();
if ($id !== null) {
$query->where('nID', $id);
}
$doctorRatings = $query->with([
'user' => function ($query) {
$query->select('nID', 'sFirstName'); // Select only necessary columns
}
])
->select('nIDUser', 'nIDDokter', 'nRating', 'sNotes', 'dCreateOn')
->get();
// $prescriptions->toArray();
// dd($prescriptions);
return response()->json($doctorRatings);
// return response()->json(Helper::paginateResources(LivechatResource::collection($livechat)));
}
public function getData(Request $request)
{
$limit = $request->has('per_page') ? $request->input('per_page') : 50;
$results = DB::connection('oldlms')->table('tx_dokter_rating')
->leftJoin('tm_users', 'tx_dokter_rating.nIDUser', '=', 'tm_users.nID')
->leftJoin('tm_dokter', 'tx_dokter_rating.nIDDokter', '=', 'tm_dokter.nID')
->when($request->input('search'), function ($query, $search) {
$query->where(function ($query) use ($search) {
$query->orWhere('tm_users.sFirstname', 'like', "%" . $search . "%");
$query->orWhere('tx_dokter_rating.sNotes', 'like', "%" . $search . "%");
});
})
->when($request->has('orderBy'), function ($query) use ($request) {
$orderBy = $request->orderBy;
$direction = $request->order ?? 'asc';
$query->orderBy($orderBy, $direction);
})
->when($request->input('start_date') , function ($query, $start_date) {
$query->where(function ($query) use ($start_date) {
$query->where('tx_dokter_rating.dCreateOn', '>=', $start_date. ' 00:00:00');
});
})
->when($request->input('end_date') , function ($query, $end_date) {
$query->where(function ($query) use ($end_date) {
$query->where('tx_dokter_rating.dCreateOn', '<=', $end_date. ' 23:59:59');
});
})
// ->when($request->input('provider') , function ($query, $provider) {
// $query->where(function ($query) use ($provider) {
// $query->where('request_logs.organization_id', '=', $provider);
// });
// })
// ->where('files.fileable_type', '=', 'App\Models\RequestLog')
// ->where('request_logs.final_log', '=', '1')
// ->where('request_logs.status_final_log', '=', 'approved')
->select(
DB::connection('oldlms')->raw("CONCAT(tm_users.sFirstName, ' ', IFNULL(tm_users.sMiddleName, ''), ' ', IFNULL(tm_users.sLastName, '')) as nama_peserta"),
'tx_dokter_rating.nRating as rating',
'tx_dokter_rating.sNotes',
'tx_dokter_rating.dCreateOn',
DB::connection('oldlms')->raw("
(SELECT CONCAT(tm_users.sFirstName, ' ', IFNULL(tm_users.sMiddleName, ''), ' ', IFNULL(tm_users.sLastName, '')) FROM tm_users WHERE tm_users.nID = tm_dokter.nIDUser LIMIT 1) AS nama_dokter
")
)
->paginate($limit);
return response()->json(Helper::paginateResources($results));
}
public function export(Request $request)
{
$start_date = $request->input('start_date') ? $request->input('start_date') : 'all';
$end_date = $request->input('end_date') ? $request->input('end_date') : 'all';
$writer = WriterEntityFactory::createXLSXWriter();
$writer->openToFile(public_path('files/Report-Data-Rating-Dokter-'.$start_date.'-'.$end_date.'.xlsx'));
$header = [
'No',
'Nama Peserta',
'Nama Dokter',
'Rating',
'Review',
'Tanggal Konsultasi',
];
$style = (new StyleBuilder())
->setFontBold()
// ->setFontSize(15)
// ->setFontColor(Color::BLUE)
// ->setShouldWrapText()
->setCellAlignment(CellAlignment::LEFT)
// ->setBackgroundColor(Color::YELLOW)
->build();
$headerRow = WriterEntityFactory::createRowFromArray($header, $style);
$writer->addRow($headerRow);
// ============================
$results = DB::connection('oldlms')->table('tx_dokter_rating')
->leftJoin('tm_users', 'tx_dokter_rating.nIDUser', '=', 'tm_users.nID')
->leftJoin('tm_dokter', 'tx_dokter_rating.nIDDokter', '=', 'tm_dokter.nID')
->when($request->input('search'), function ($query, $search) {
$query->where(function ($query) use ($search) {
$query->orWhere('tm_users.sFirstname', 'like', "%" . $search . "%");
$query->orWhere('tx_dokter_rating.sNotes', 'like', "%" . $search . "%");
});
})
->when($request->has('orderBy'), function ($query) use ($request) {
$orderBy = $request->orderBy;
$direction = $request->order ?? 'asc';
$query->orderBy($orderBy, $direction);
})
->when($request->input('start_date') , function ($query, $start_date) {
$query->where(function ($query) use ($start_date) {
$query->where('tx_dokter_rating.dCreateOn', '>=', $start_date. ' 00:00:00');
});
})
->when($request->input('end_date') , function ($query, $end_date) {
$query->where(function ($query) use ($end_date) {
$query->where('tx_dokter_rating.dCreateOn', '<=', $end_date. ' 23:59:59');
});
})
// ->when($request->input('provider') , function ($query, $provider) {
// $query->where(function ($query) use ($provider) {
// $query->where('request_logs.organization_id', '=', $provider);
// });
// })
// ->where('files.fileable_type', '=', 'App\Models\RequestLog')
// ->where('request_logs.final_log', '=', '1')
// ->where('request_logs.status_final_log', '=', 'approved')
->select(
DB::connection('oldlms')->raw("CONCAT(tm_users.sFirstName, ' ', IFNULL(tm_users.sMiddleName, ''), ' ', IFNULL(tm_users.sLastName, '')) as nama_peserta"),
'tx_dokter_rating.nRating',
'tx_dokter_rating.sNotes',
'tx_dokter_rating.dCreateOn',
DB::connection('oldlms')->raw("
(SELECT CONCAT(tm_users.sFirstName, ' ', IFNULL(tm_users.sMiddleName, ''), ' ', IFNULL(tm_users.sLastName, '')) FROM tm_users WHERE tm_users.nID = tm_dokter.nIDUser LIMIT 1) AS nama_dokter
")
)
->get();
$no=0;
foreach($results as $item)
{
$no++;
$rowData = [
$no,
$item->nama_peserta,
$item->nama_dokter,
$item->nRating,
$item->sNotes,
$item->dCreateOn,
];
$style = (new StyleBuilder())
//->setFontBold()
// ->setFontSize(15)
// ->setFontColor(Color::BLUE)
// ->setShouldWrapText()
->setCellAlignment(CellAlignment::LEFT)
// ->setBackgroundColor(Color::YELLOW)
->build();
$row = WriterEntityFactory::createRowFromArray($rowData, $style);
$writer->addRow($row);
}
$footer = [
'',
'',
'',
'',
'',
''
];
$style = (new StyleBuilder())
->setFontBold()
// ->setFontSize(15)
// ->setFontColor(Color::BLUE)
// ->setShouldWrapText()
->setCellAlignment(CellAlignment::LEFT)
// ->setBackgroundColor(Color::YELLOW)
->build();
$footerRow = WriterEntityFactory::createRowFromArray($footer, $style);
$writer->addRow($footerRow);
$writer->close();
return Helper::responseJson([
'file_name' => 'Report-Data-Rating-Dokter-'. $start_date.'-'.$end_date,
"file_url" => url('files/Report-Data-Rating-Dokter-'. $start_date.'-'.$end_date.'.xlsx')
]);
}
/**
* 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)
{
}
/**
* 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)
{
//
}
}