getRateDetail

mail@pastecode.io avatar
unknown
php
24 days ago
4.8 kB
4
Indexable
Never
 public function getRateDetail(Request $request)
    {

        $rate_header_id = null;
        $case = null;

        if ($request->rate_header_id) {
            list($case, $rate_header_id) = explode("-", $request->rate_header_id);
        }

        if (is_null($request->management_clinic_id)) {
            $doctor = Doctor::where('id', $request->doctor_id)->first();

            if ($doctor) {
                $rates_detail = DB::table('doctor_rates')
                                ->select('id', 'name')
                                ->where('doctor_id', $doctor->id)
                                ->where('doctor_rate_group_id', $rate_header_id)
                                ->get();
            }
            

            $data = [
                'select' => $request->rate_detail_id_selected,
                'data' => $rates_detail,
            ];

            return response()->json($data);
        }

        $contrated = $this->is_contracted($request->management_clinic_id, $request->doctor_id);

        if ($contrated) {
            Log::debug("valores", [$case, $rate_header_id]);
            if ($case == "contractdoc" || $case == "doctor") {
                $doctor = Doctor::where('id', $request->doctor_id)->first();
                if ($doctor) {
                    $doctor_details = DB::table('doctor_rates')
                            ->select('id', 'name')
                            ->where('doctor_rate_group_id', $rate_header_id)
                            ->where('doctor_id', $doctor->id)
                            ->get();
                }

                $data = [
                    'type' => 'doctor',
                    'select' => $request->rate_detail_id_selected,
                    'data' => $doctor_details,
                ];

                return response()->json($data);
            } else {
                $clinic_rates = DB::table('clinic_rates')
                            ->select('id', 'name')
                            ->where('clinic_id', $request->management_clinic_id)
                            ->where('clinic_rate_group_id', $rate_header_id)
                            ->get();

                $rate_details = $clinic_rates;

                $data = [
                    'type' => 'clinic',
                    'select' => $request->rate_detail_id_selected,
                    'data' => $rate_details,
                ];

                return response()->json($data);
            }
        } else {
            Log::debug("valores", [$case, $rate_header_id]);
            switch ($case) {
                case 'doctor':
                        $doctor = Doctor::where('id', $request->doctor_id)->first();
                    if ($doctor) {
                        $doctor_details = DB::table('doctor_rates')
                                ->select('id', 'name')
                                ->where('doctor_rate_group_id', $rate_header_id)
                                ->where('doctor_id', $doctor->id)
                                ->get();
                    }

                        $data = [
                            'type' => 'doctor',
                            'select' => $request->rate_detail_id_selected,
                            'data' => $doctor_details,
                        ];
                    break;
                
                case 'clinic':

                        $rate_details = DB::table('clinic_rates')
                            ->select('id', 'name')
                            ->where('clinic_id', $request->management_clinic_id)
                            ->where('clinic_rate_group_id', $rate_header_id)
                            ->get();

                        $data = [
                            'type' => 'clinic',
                            'select' => $request->rate_detail_id_selected,
                            'data' => $rate_details,
                        ];
                    break;
                
                default:
                    $doctor = Doctor::where('id', $request->doctor_id)->first();
                    if ($doctor) {
                        $doctor_details = DB::table('doctor_rates')
                                ->select('id', 'name')
                                ->where('doctor_rate_group_id', $rate_header_id)
                                ->where('doctor_id', $doctor->id)
                                ->get();
                    }
                    $data = [
                        'type' => 'doctor',
                        'select' => $request->rate_detail_id_selected,
                        'data' => $doctor_details,
                    ];
                    break;
            }
        }

        return response()->json($data);
    }
Leave a Comment