year ?? now()->year; $status = $request->status; $bn = $request->bn; $payorId = $request->payorId; $service = $request->service; $billing = $request->billing; $search = $request->search; $rows = DB::table('invoice_payments') ->join('invoice_payment_details', 'invoice_payment_details.invoice_payment_id', '=', 'invoice_payments.id') ->join('claim_requests', 'claim_requests.id', '=', 'invoice_payment_details.claim_request_id') ->join('request_logs', 'request_logs.id', '=', 'claim_requests.request_log_id') ->join('corporate_employees', 'corporate_employees.member_id', '=', 'request_logs.member_id') ->join('organizations', 'organizations.id', '=', 'request_logs.organization_id') ->leftJoin('members', 'members.id', '=', 'request_logs.member_id') ->whereYear('invoice_payments.created_at', $year) ->where('corporate_employees.corporate_id', '=', $corporate_id) // FILTERS ->when($status, fn ($q) => $q->where('invoice_payments.status', $status) ) ->when($bn, fn ($q) => $q->where('members.member_id', $bn) ) ->when($payorId, fn ($q) => $q->where('members.payor_id', $payorId) ) ->when($service, fn ($q) => $q->where('claim_requests.service_code', $service) ) ->when($billing, fn ($q) => $q->where('invoice_payments.invoice_number', $billing) ) // 🔍 SEARCH PROVIDER (LIKE) ->when($search, fn ($q) => $q->where('organizations.name', 'like', '%' . $search . '%') ) ->selectRaw(" MONTH(invoice_payments.created_at) as month_number, organizations.name as provider, SUM(request_logs.nominal) as total ") ->groupBy('month_number', 'organizations.name') ->orderBy('month_number') ->get(); return response()->json( $this->formatMonthly($rows) ); } protected function formatMonthly($rows): array { $months = [ 1 => 'Januari', 2 => 'Februari', 3 => 'Maret', 4 => 'April', 5 => 'Mei', 6 => 'Juni', 7 => 'Juli', 8 => 'Agustus', 9 => 'September', 10 => 'Oktober', 11 => 'November', 12 => 'Desember', ]; return collect($rows) ->groupBy('month_number') ->map(function ($items, $monthNumber) use ($months) { return [ 'month' => $months[$monthNumber], 'total' => $items->sum('total'), 'items' => $items->map(fn ($item) => [ 'provider' => $item->provider, 'total' => (float) $item->total, ])->values(), ]; }) ->values() ->toArray(); } public function providerSummary(Request $request, $corporate_id) { $year = $request->year ?? now()->year; $status = $request->status; $bn = $request->bn; $payorId = $request->payorId; $service = $request->service; $billing = $request->billing; $search = $request->search; $query = DB::table('invoice_payments') ->join('invoice_payment_details', 'invoice_payment_details.invoice_payment_id', '=', 'invoice_payments.id') ->join('claim_requests', 'claim_requests.id', '=', 'invoice_payment_details.claim_request_id') ->join('request_logs', 'request_logs.id', '=', 'claim_requests.request_log_id') ->join('corporate_employees', 'corporate_employees.member_id', '=', 'request_logs.member_id') ->join('organizations', 'organizations.id', '=', 'request_logs.organization_id') ->leftJoin('members', 'members.id', '=', 'request_logs.member_id') ->whereYear('invoice_payments.created_at', $year) ->where('corporate_employees.corporate_id', '=', $corporate_id) // FILTER ->when($status, fn ($q) => $q->where('invoice_payments.status', $status) ) ->when($bn, fn ($q) => $q->where('members.member_id', $bn) ) ->when($payorId, fn ($q) => $q->where('members.payor_id', $payorId) ) ->when($service, fn ($q) => $q->where('claim_requests.service_code', $service) ) ->when($billing, fn ($q) => $q->where('invoice_payments.invoice_number', $billing) ) ->when($search, fn ($q) => $q->where('organizations.name', 'like', '%' . $search . '%') ); $items = $query ->selectRaw(" organizations.name as provider, SUM(request_logs.nominal) as total ") ->groupBy('organizations.name') ->orderByDesc('total') ->get() ->map(fn ($row) => [ 'provider' => $row->provider, 'total' => (float) $row->total, ]); return response()->json([ 'total' => $items->sum('total'), 'items' => $items, ]); } public function topDiagnosis(Request $request, $corporate_id) { $year = $request->year ?? now()->year; $status = $request->status; $bn = $request->bn; $payorId = $request->payorId; $service = $request->service; $billing = $request->billing; $search = $request->search; $rows = DB::table('invoice_payments') ->join('invoice_payment_details', 'invoice_payment_details.invoice_payment_id', '=', 'invoice_payments.id') ->join('claim_requests', 'claim_requests.id', '=', 'invoice_payment_details.claim_request_id') ->join('request_logs', 'request_logs.id', '=', 'claim_requests.request_log_id') ->join('corporate_employees', 'corporate_employees.member_id', '=', 'request_logs.member_id') // 🔥 ICD JOIN ->leftJoin( 'icd', DB::raw("icd.code"), '=', DB::raw("SUBSTRING_INDEX(request_logs.diagnosis, ',', 1)") ) ->leftJoin('members', 'members.id', '=', 'request_logs.member_id') ->where('corporate_employees.corporate_id', '=', $corporate_id) ->whereYear('invoice_payments.created_at', $year) // FILTER ->when($status, fn ($q) => $q->where('invoice_payments.status', $status) ) ->when($bn, fn ($q) => $q->where('members.member_id', $bn) ) ->when($payorId, fn ($q) => $q->where('members.payor_id', $payorId) ) ->when($service, fn ($q) => $q->where('claim_requests.service_code', $service) ) ->when($billing, fn ($q) => $q->where('invoice_payments.invoice_number', $billing) ) ->selectRaw(" SUBSTRING_INDEX(request_logs.diagnosis, ',', 1) as code_diagnosis, icd.name as diagnosis, COUNT(request_logs.id) as total_case, SUM(request_logs.nominal) as total_billing ") ->groupBy('code_diagnosis', 'icd.name') ->orderByDesc('total_case') ->limit(10) ->get(); return response()->json( $rows->map(fn ($row) => [ 'code' => $row->code_diagnosis, 'diagnosis' => $row->diagnosis ?? '-', 'total_case' => (int) $row->total_case, 'total_billing' => (float) $row->total_billing, ]) ); } /** * Display a listing of the resource. * @return Renderable */ // public function index() // { // return view('client::index'); // } /** * Show the form for creating a new resource. * @return Renderable */ public function create() { return view('client::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('client::show'); } /** * Show the form for editing the specified resource. * @param int $id * @return Renderable */ public function edit($id) { return view('client::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) { // } }