select(DB::raw('DISTINCT(user_id)'))->pluck('user_id')->toArray(); $users_array = []; for ($i=0; $i < count($staff_ids_with_configurations) ; $i++) { $user = User::find($staff_ids_with_configurations[$i]); if (!is_null($user)) { $users_array[$staff_ids_with_configurations[$i]] = get_full_name($staff_ids_with_configurations[$i], 'id', 'first_name', 'last_name', 'users'); } } $users_array = ['' => '- Select Staff -'] + $users_array; $procedures = Procedure::where('available', 1)->orderBy("name","asc")->get(); $investigations = Investigation::where('available', 1)->orderby("name","asc")->get(); $services = Services::where('available', 1)->orderby("name","asc")->get(); $price_list_categories = PriceListCategories::select('patient_category_id', 'id')->get(); $price_list_category_id = null; return view('finance::staff_payments_configuration.index',compact('users_array', 'procedures', 'investigations', 'services', 'price_list_categories', 'price_list_category_id')); } public function staff_payment_configuration_search(Request $request) { $staff_ids_with_configurations = DB::table('staff_payment_configurations')->select(DB::raw('DISTINCT(user_id)'))->pluck('user_id')->toArray(); $users_array = []; for ($i=0; $i < count($staff_ids_with_configurations) ; $i++) { $user = User::find($staff_ids_with_configurations[$i]); if (!is_null($user)) { $users_array[$staff_ids_with_configurations[$i]] = get_full_name($staff_ids_with_configurations[$i], 'id', 'first_name', 'last_name', 'users'); } } $users_array = ['' => '- Select Staff -'] + $users_array; $procedures = Procedure::withTrashed()->orderBy("name","asc")->get(); $investigations = Investigation::withTrashed()->orderBy("name","asc")->get(); $services = Services::withTrashed()->orderBy("name","asc")->get(); $price_list_category_id = $request->price_list_category_id; if ($price_list_category_id) { $staff_payment_configurations = StaffPaymentConfiguration::where(['user_id' => $request->staff_id, 'price_list_category_id' => $price_list_category_id])->get(); } elseif ($price_list_category_id == 0) { $staff_payment_configurations = StaffPaymentConfiguration::where('price_list_category_id', 0)->where(['user_id' => $request->staff_id])->get(); } else { $staff_payment_configurations = StaffPaymentConfiguration::where('price_list_category_id', 0)->where(['user_id' => $request->staff_id])->get(); } $search_complete = true; $searched_staff_name = get_full_name($request->staff_id, 'id', 'first_name', 'last_name', 'users'); $price_list_categories = PriceListCategories::select('patient_category_id', 'id')->get(); return view('finance::staff_payments_configuration.index',compact('users_array', 'procedures', 'investigations', 'services', 'search_complete', 'staff_payment_configurations', 'searched_staff_name', 'price_list_categories', 'price_list_category_id')); } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create() { $users = DB::table('users')->orderBy("first_name","asc")->select("id")->get()->toArray(); $users_array = []; foreach ($users as $value){ $user = User::find($value->id); if (!is_null($user)) { $users_array[$value->id] = get_full_name($value->id, 'id', 'first_name', 'last_name', 'users'); } } $users_array = ['' => '- Select Staff -'] + $users_array; $procedures = Procedure::where('available', 1)->orderBy("name","asc")->get(); $investigations = Investigation::where('available', 1)->orderby("name","asc")->get(); $services = Services::where('available', 1)->orderby("name","asc")->get(); $price_list_categories = PriceListCategories::select('patient_category_id', 'id')->get(); $price_list_category_id = session()->has('price_list_category_id') ? session()->get('price_list_category_id') : null; //forget the patient cetgory session()->forget('price_list_category_id'); return view('finance::staff_payments_configuration.create',compact('users_array', 'procedures', 'investigations', 'services', 'price_list_categories', 'price_list_category_id')); } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { if ($request->get('submit-btn') == 'change_pricing') { //first forget the existing price list category if it exists session()->forget('price_list_category_id'); $price_list_category_id = $request->price_list_category_id; session()->put(['price_list_category_id' => $price_list_category_id]); return redirect('staff_payment_configuration/create'); } /* handle procedures */ $procedures_ids_array = $request->procedure_id; $procedures_fees_array = $request->procedure_fee; $procedures_percentages_array = $request->procedure_percentage; /* handle services */ $services_ids_array = $request->service_id; $services_fees_array = $request->service_fee; $services_percentages_array = $request->service_percentage; /* handle investigations */ $investigation_ids_array = $request->investigation_id; $investigation_fees_array = $request->investigation_fee; $investigation_percentages_array = $request->investigation_percentage; $selected_price_list_category_id = $request->selected_price_list_category_id; for ($i=0; $i < count($procedures_ids_array) ; $i++) { $staff_payment = new StaffPaymentConfiguration; $staff_payment->user_id = $request->user_id; $staff_payment->item_category = 1; // 1-procedures $staff_payment->payment_type = $request->payment_type; $staff_payment->item_id = $procedures_ids_array[$i]; $staff_payment->price_list_category_id = is_null($selected_price_list_category_id) ? 0 : $selected_price_list_category_id; if ($request->payment_type == 0) { $staff_payment->amount_fee = $procedures_fees_array[$i]; } elseif ($request->payment_type == 1) { $staff_payment->amount_percentage = $procedures_percentages_array[$i]; } $staff_payment->created_by = Auth::id(); /* only save a record in the db if both the fee and percentage are not null i.e when atleast one is filled */ if (!is_null($procedures_fees_array[$i]) || !is_null($procedures_percentages_array[$i])) { $staff_payment->save(); } } for ($i=0; $i < count($services_ids_array) ; $i++) { $staff_payment = new StaffPaymentConfiguration; $staff_payment->user_id = $request->user_id; $staff_payment->item_category = 3; // 3-Consultation and services $staff_payment->payment_type = $request->payment_type; $staff_payment->item_id = $services_ids_array[$i]; $staff_payment->price_list_category_id = is_null($selected_price_list_category_id) ? 0 : $selected_price_list_category_id; if ($request->payment_type == 0) { $staff_payment->amount_fee = $services_fees_array[$i]; } elseif ($request->payment_type == 1) { $staff_payment->amount_percentage = $services_percentages_array[$i]; } $staff_payment->created_by = Auth::id(); /* only save a record in the db if both the fee and percentage are not null i.e when atleast one is filled */ if (!is_null($services_fees_array[$i]) || !is_null($services_percentages_array[$i])) { $staff_payment->save(); } } for ($i=0; $i < count($investigation_ids_array) ; $i++) { $staff_payment = new StaffPaymentConfiguration; $staff_payment->user_id = $request->user_id; $staff_payment->item_category = 2; // 3-Investigations $staff_payment->payment_type = $request->payment_type; $staff_payment->item_id = $investigation_ids_array[$i]; $staff_payment->price_list_category_id = is_null($selected_price_list_category_id) ? 0 : $selected_price_list_category_id; if ($request->payment_type == 0) { $staff_payment->amount_fee = $investigation_fees_array[$i]; } elseif ($request->payment_type == 1) { $staff_payment->amount_percentage = $investigation_percentages_array[$i]; } $staff_payment->created_by = Auth::id(); /* only save a record in the db if both the fee and percentage are not null i.e when atleast one is filled */ if (!is_null($investigation_fees_array[$i]) || !is_null($investigation_percentages_array[$i])) { $staff_payment->save(); } } flash('staff payment fees for '.get_full_name($request->user_id, 'id', 'first_name', 'last_name', 'users').' have been set')->success(); return redirect('staff_payment_configuration/create'); } /** * Display the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function show($id) { // } /** * Show the form for editing the specified resource. * * @param int $id * @return \Illuminate\Http\Response */ public function edit($id) { // } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id * @return \Illuminate\Http\Response */ public function update(Request $request, $id) { // } /** * Remove the specified resource from storage. * * @param int $id * @return \Illuminate\Http\Response */ public function destroy($id) { // } public function payments_report(Request $request) { $search_complete = null; $filters = []; $start_date = null; $end_date = null; $results = null; $report_by = null; $search_text = ""; $whole_amount = 0; $users = DB::table('users')->orderBy("first_name","asc")->select("id")->get()->toArray(); $users_array = []; foreach ($users as $value){ $user = User::find($value->id); if (!is_null($user)) { $users_array[$value->id] = get_full_name($value->id, 'id', 'first_name', 'last_name', 'users'); } } $users_array = ['each_staff' => 'Each Staff'] + $users_array; $users_array = ['' => '- Select Staff -'] + $users_array; $services_rendered_array = ['' => '-- Select --',1 => 'Procedures', 2 => 'Investigations', 3 => 'Consultations']; $services_rendered_array = ['all_services' => 'All Services'] + $services_rendered_array; if($request->search_by == 0){ // last 24 hours $last_day = Carbon::now()->subDay(); array_push($filters, ['created_at', '>', $last_day]); $search_text = "Showing payment report for last 24 Hours"; } elseif($request->search_by == 1){ // custom date $start_date = Carbon::parse($request->reg_date)->startOfDay()->toDateTimeString(); $end_date = Carbon::parse($request->reg_date)->endOfDay()->toDateTimeString(); array_push($filters, ['created_at', '>', $start_date]); array_push($filters, ['created_at', '<', $end_date]); $search_text = "Showing payment report for " . streamline_date_plain($request->reg_date); } elseif($request->search_by == 2){ // custom date range $start_date = Carbon::parse($request->start_date)->startOfDay()->toDateTimeString(); $end_date = Carbon::parse($request->end_date)->endOfDay()->toDateTimeString(); array_push($filters, ['created_at', '>', $start_date]); array_push($filters, ['created_at', '<', $end_date]); $search_text = "Showing payment report for period between " . streamline_date_plain($start_date) . " and " . streamline_date_plain($end_date); } $staff_performed_items_array = []; if ($request->report_by == 1) { $search_complete = true; $report_by = $request->report_by; # report by staff $staff_id = $request->staff; if ($staff_id == "each_staff") { $results = StaffPerformedService::where($filters)->get(); $staff_ids_array = []; foreach ($results as $record) { if ( array_key_exists($record->performed_by, $staff_performed_items_array)) { if(is_null($record->performance_fee)){ $staff_fee = null; $staff_fee = get_fee_owed_to_staff_for_perfomed_service($record->item_id, $record->item_category, $record->performed_by); $staff_fee = is_numeric($staff_fee) ? $staff_fee : 0; $staff_performed_items_array[$record->performed_by] += $staff_fee; } else{ $staff_performed_items_array[$record->performed_by] += $record->performance_fee; } } else { if(is_null($record->performance_fee)){ $staff_fee = null; $staff_fee = get_fee_owed_to_staff_for_perfomed_service($record->item_id, $record->item_category, $record->performed_by); $staff_fee = is_numeric($staff_fee) ? $staff_fee : 0; $staff_performed_items_array[$record->performed_by] = $staff_fee; } else{ $staff_performed_items_array[$record->performed_by] = get_fee_owed_to_staff_for_perfomed_service($record->item_id, $record->item_category, $record->performed_by); } } } } else { $results = StaffPerformedService::where('performed_by', $staff_id)->where($filters)->get(); foreach ($results as $record) { if ( array_key_exists($record->performed_by, $staff_performed_items_array)) { $this_staff_fee = 0; if(is_null($record->performance_fee)){ $this_staff_fee = get_fee_owed_to_staff_for_perfomed_service($record->item_id, $record->item_category, $record->performed_by); $this_staff_fee = is_numeric($this_staff_fee) ? $this_staff_fee : 0; } else { $this_staff_fee = $record->performance_fee; } $staff_performed_items_array[$record->performed_by] += $this_staff_fee; } else { $this_staff_fee = 0; if(is_null($record->performance_fee)){ $this_staff_fee = get_fee_owed_to_staff_for_perfomed_service($record->item_id, $record->item_category, $record->performed_by); $this_staff_fee = is_numeric($this_staff_fee) ? $this_staff_fee : 0; } else { $this_staff_fee = $record->performance_fee; } $staff_performed_items_array[$record->performed_by] = $this_staff_fee; } } $staff_ward_procedures_total = 0; $ward_procedures_records = WardProcedure::where('performed_by', $staff_id)->where($filters)->orderBy('created_at', 'desc')->get(); if (count($ward_procedures_records) > 0) { foreach ($ward_procedures_records as $ward_procedure) { $staff_ward_procedures_total += $ward_procedure->staff_fee; } } $staff_performed_items_array[$staff_id] = $staff_performed_items_array[$staff_id] + $staff_ward_procedures_total; $staff_ward_services_total = 0; $searchTerm = "__".$staff_id; $ward_services_records = WardConsultationsAndService::where('service_id', 'LIKE', "%{$searchTerm}")->where($filters)->orderBy('created_at', 'desc')->get(); if (count($ward_services_records) > 0) { foreach ($ward_services_records as $ward_service) { $staff_ward_services_total += $ward_service->staff_fee; } } $staff_performed_items_array[$staff_id] = $staff_performed_items_array[$staff_id] + $staff_ward_services_total; } } elseif ($request->report_by == 2) { $search_complete = true; $report_by = $request->report_by; # report by services rendered i.e 1-procedures, 2-investigations, 3-services and consultation all_services-all services $services_rendered_id = $request->services_rendered_id; if ($services_rendered_id == "all_services") { $results = StaffPerformedService::where($filters)->get(); $staff_ids_array = []; foreach ($results as $record) { if ( array_key_exists($record->item_category, $staff_performed_items_array)) { if(is_null($record->performance_fee)){ $staff_performed_items_array[$record->item_category] += get_fee_owed_to_staff_for_perfomed_service_based_on_category($record->item_id, $record->performed_by, $record->item_category); } else{ $staff_performed_items_array[$record->item_category] += $record->performance_fee; } } else { if(is_null($record->performance_fee)){ $staff_performed_items_array[$record->item_category] = get_fee_owed_to_staff_for_perfomed_service_based_on_category($record->item_id, $record->performed_by, $record->item_category); } else{ $staff_performed_items_array[$record->item_category] = $record->performance_fee; } } } //include records from inpatient procedures $inpatient_procedures_total = 0; $ward_procedures_records = WardProcedure::where($filters)->orderBy('created_at', 'desc')->get(); if (count($ward_procedures_records) > 0) { foreach ($ward_procedures_records as $ward_procedure) { $inpatient_procedures_total += $ward_procedure->staff_fee; } } $staff_performed_items_array[1] = isset($staff_performed_items_array[1]) ? $staff_performed_items_array[1] + $inpatient_procedures_total : $inpatient_procedures_total; //include records from inpatient services $inpatient_services_total = 0; $ward_services_records = WardConsultationsAndService::where($filters)->orderBy('created_at', 'desc')->get(); if (count($ward_services_records) > 0) { foreach ($ward_services_records as $ward_services) { $inpatient_services_total += $ward_services->staff_fee; } } $staff_performed_items_array[3] = isset($staff_performed_items_array[3]) ? $staff_performed_items_array[3] + $inpatient_services_total : $inpatient_services_total; } else { $results = StaffPerformedService::where('item_category', $services_rendered_id)->where($filters)->get(); foreach ($results as $record) { if ( array_key_exists($record->item_category, $staff_performed_items_array)) { if(is_null($record->performance_fee)){ $whole_amount += get_fee_owed_to_staff_for_perfomed_service_based_on_category($record->item_id, $record->performed_by, $record->item_category); } else{ $whole_amount += $record->performance_fee; } $staff_performed_items_array[$record->item_category] = $whole_amount; } else { if(is_null($record->performance_fee)){ $whole_amount = $record->performance_fee; } else{ $whole_amount = get_fee_owed_to_staff_for_perfomed_service_based_on_category($record->item_id, $record->performed_by, $record->item_category); } $staff_performed_items_array[$record->item_category] = $whole_amount; } } //include records from inpatient procedures $inpatient_procedures_total = 0; if ($services_rendered_id == 1) { $ward_procedures_records = WardProcedure::where($filters)->orderBy('created_at', 'desc')->get(); if (count($ward_procedures_records) > 0) { foreach ($ward_procedures_records as $ward_procedure) { $inpatient_procedures_total += $ward_procedure->staff_fee; } } $staff_performed_items_array[1] = isset($staff_performed_items_array[1]) ? $staff_performed_items_array[1] + $inpatient_procedures_total : $inpatient_procedures_total; } //include records from inpatient services $inpatient_services_total = 0; if ($services_rendered_id == 3) { $ward_services_records = WardConsultationsAndService::where($filters)->orderBy('created_at', 'desc')->get(); if (count($ward_services_records) > 0) { foreach ($ward_services_records as $ward_services) { $inpatient_services_total += $ward_services->staff_fee; } } $staff_performed_items_array[3] = isset($staff_performed_items_array[3]) ? $staff_performed_items_array[3] + $inpatient_services_total : $inpatient_services_total; } } } $details_report_by = $request->report_by; $details_staff = $request->staff; $details_services_rendered_id = $request->services_rendered_id; $details_search_by = $request->search_by; $details_reg_date = $request->reg_date; $details_start_date = $request->start_date; $details_end_date = $request->end_date; return view('finance::staff_payments_configuration.payments_report', compact('users_array', 'services_rendered_array', 'search_complete', 'results', 'report_by', 'staff_performed_items_array', 'details_report_by', 'details_staff', 'details_services_rendered_id', 'details_search_by', 'details_reg_date', 'details_start_date', 'details_end_date', 'search_text')); } public function payments_details_report(Request $request) { /* *show money from patient, what the doctor made */ $filters = $ward_procedures_records = $ward_services_records = []; $staff_performed_services = null; $search_string = null; if($request->details_search_by == 0){ // last 24 hours $last_day = Carbon::now()->subDay(); array_push($filters, ['created_at', '>', $last_day]); $search_string = "

Showing results of ".streamline_date($last_day)."

"; } elseif($request->details_search_by == 1){ // custom date $start_date = Carbon::parse($request->details_reg_date)->startOfDay()->toDateTimeString(); $end_date = Carbon::parse($request->details_reg_date)->endOfDay()->toDateTimeString(); array_push($filters, ['created_at', '>', $start_date]); array_push($filters, ['created_at', '<', $end_date]); $search_string = "

Showing results of ".streamline_date($request->details_reg_date)."

"; } elseif($request->details_search_by == 2){ // custom date range $start_date = Carbon::parse($request->details_start_date)->startOfDay()->toDateTimeString(); $end_date = Carbon::parse($request->details_end_date)->endOfDay()->toDateTimeString(); array_push($filters, ['created_at', '>', $start_date]); array_push($filters, ['created_at', '<', $end_date]); $search_string = "

Showing between ".streamline_date($start_date). " and ".streamline_date($end_date)."

"; } if ($request->details_report_by == 1) { $report_by = $request->details_report_by; # report by staff $staff_id = $request->details_staff; if ($staff_id == "each_staff") { $staff_performed_services = StaffPerformedService::where($filters)->get(); } else { $staff_performed_services = StaffPerformedService::where('performed_by', $staff_id)->where($filters)->get(); $ward_procedures_records = WardProcedure::where('performed_by', $staff_id)->where($filters)->orderBy('created_at', 'desc')->get(); $searchTerm = "__".$staff_id; $ward_services_records = WardConsultationsAndService::where('service_id', 'LIKE', "%{$searchTerm}")->where($filters)->orderBy('created_at', 'desc')->get(); } } elseif ($request->details_report_by == 2) { $report_by = $request->details_report_by; # report by services rendered i.e 1-procedures, 2-investigations, 3-services, all_services-all services $services_rendered_id = $request->details_services_rendered_id; if ($services_rendered_id == "all_services") { $staff_performed_services = StaffPerformedService::where($filters)->get(); } else { $staff_performed_services = StaffPerformedService::where('item_category', $services_rendered_id)->where($filters)->get(); //include records from inpatient services if ($services_rendered_id == 1) { $ward_procedures_records = WardProcedure::where($filters)->orderBy('created_at', 'desc')->get(); } //include records from inpatient services if ($services_rendered_id == 3) { $ward_services_records = WardConsultationsAndService::where($filters)->orderBy('created_at', 'desc')->get(); } } } $report_based_on_staff_or_category = $request->details_report_by; return view('finance::staff_payments_configuration.payments_report_details', compact('staff_performed_services', 'report_by', 'report_based_on_staff_or_category', 'ward_procedures_records', 'ward_services_records', 'search_string')); } public function delete_staff_configuration(Request $request) { $spc = StaffPaymentConfiguration::find($request->config_id); if ($spc->delete()) { return 1; } else { return 0; } } public function edit_staff_configuration(Request $request) { $spc = StaffPaymentConfiguration::find($request->config_id); $spc->amount_fee = $request->amount_fee; if ($spc->save()) { return 1; } else { return 0; } } public function clean_staff_payments() { $filters = []; $start_date = Carbon::parse("2020-10-14")->startOfDay()->toDateTimeString(); $end_date = Carbon::parse("2020-10-15")->endOfDay()->toDateTimeString(); array_push($filters, ['created_at', '>', $start_date]); array_push($filters, ['created_at', '<', $end_date]); $staff_performed_services = StaffPerformedService::where($filters)->get(); if(count($staff_performed_services) > 0){ foreach($staff_performed_services as $record){ if ($record->item_category == 1){ $procedure = Procedure::find($record->item_id); $non_insured_price = $procedure->non_insured_price; $item_name = $procedure->name; }elseif ($record->item_category == 2){ $investigation = Investigation::find($record->item_id); $non_insured_price = $investigation->non_insured_price; $item_name = $investigation->name; }elseif ($record->item_category == 3){ $service = Services::find($record->item_id); $non_insured_price = $service->cost_price; $item_name = $service->name; } if (get_name($record->patient_id, "id", "category_id", "patients") == 1) { record_staff_that_has_performed_the_service($record->patient_id, $record->episode_id, $record->item_category, $record->item_id, $record->inpatient, $record->performed_by); } } } return response()->json("successfully cleaned staff configs"); } public function fetch_performed_by_info($id) { $record = DB::table('staff_performed_services')->where('id', $id)->first(); return $record->performed_by . "," . $record->performance_fee . "," . \Carbon\Carbon::createFromFormat('Y-m-d H:i:s', $record->created_at)->toDateString(); } public function edit_performed_by_info(Request $request) { $update = DB::table('staff_performed_services')->where('id', $request->id) ->update([ 'performed_by' => $request->edit_performed_by, 'performance_fee' => $request->edit_performed_by_amount, 'created_at' => $request->edit_performed_date . " " . \Carbon\Carbon::now()->toTimeString() ]); if ($update) { return get_full_name($request->edit_performed_by, 'id', 'first_name', 'last_name', 'users'); } else { return "0"; } } public function delete_performed_by($id, $position, $order_id, $type) { $staff_payment = DB::table('staff_performed_services')->where(['id' => $id])->delete(); if ($staff_payment) { if($type == 1) { $arr = explode(",", get_name($order_id, 'id', 'performed', 'ordered_procedures')); $arr_id = explode(",", get_name($order_id, 'id', 'performed_id', 'ordered_procedures')); if (isset($arr[$position])) { $arr[$position] = 0; $arr_id[$position] = 0; $update = DB::table('ordered_procedures')->where('id', $order_id) ->update(['performed' => implode(",", $arr), 'performed_id' => implode(",", $arr_id)]); } } else { $arr = explode(",", get_name($order_id, 'id', 'performed', 'ordered_services')); $arr_id = explode(",", get_name($order_id, 'id', 'performed_id', 'ordered_services')); if (isset($arr[$position])) { $arr[$position] = 0; $arr_id[$position] = 0; $update = DB::table('ordered_services')->where('id', $order_id) ->update(['performed' => implode(",", $arr), 'performed_id' => implode(",", $arr_id)]); } } return 1; } else { return 0; } } public function record_staff_service_performance() { #record that staff has performed a service so that they are paid their money $employees = DB::table('users')->whereNull('deleted_at')->orderBy('first_name', 'asc')->get(); $patient_id = session()->get('patient_id'); $procedures = DB::table('procedures')->whereNull('deleted_at')->orderBy('name', 'asc')->get(); $investigations = DB::table('investigations')->whereNull('deleted_at')->orderBy('name', 'asc')->get(); $services = DB::table('services')->whereNull('deleted_at')->orderBy('name', 'asc')->get(); $sundries = DB::table('sundries')->whereNull('deleted_at')->orderBy('name', 'asc')->get(); return view('finance::staff_payments_configuration.record_service_perfomance', compact('employees', 'procedures', 'investigations', 'services', 'sundries', 'patient_id')); } public function store_recorded_performed_service(Request $request) { $patient_id = session()->get('patient_id'); $episode_id = session()->get('episode_id'); if(session()->has('patient_id') == false){ flash('No patient selected')->error(); return back()->withInput(); } $item_categories_array = $request->item_category; $performed_investigations_array = $request->performed_investigation_item; $performed_procedures_array = $request->performed_procedure_item; $performed_services_array = $request->performed_service_item; $performed_sundries_array = $request->performed_sundry_item; $items_prices_array = $request->item_price; $items_quantities_array = $request->item_quantity; $performed_by_ids_array = $request->performed_by_id; $performance_fees_array = $request->performance_fee; $lab_investigation_array = []; $xray_investigation_array = []; $obstetric_ultrasound_investigation_array = []; $ultrasound_investigation_array = []; $cardiology_investigation_array = []; $lab_inv_prices_array = []; $xray_inv_prices_array = []; $ultrasound_inv_prices_array = []; $obstetric_inv_prices_array = []; $cardiology_inv_prices_array = []; $ordered_procedures_array = []; $ordered_procedures_amount_array = []; $ordered_services_array = []; $ordered_services_amounts_array = []; $ordered_services_quantity_array = []; $ordered_sundries_array = []; $ordered_sundries_amounts_array = []; $ordered_sundries_quantity_array = []; $is_procedure_performed_array = []; $is_service_performed_array = []; $is_procedure_performed_id_array = []; $is_service_performed_id_array = []; for ($i=0; $i < count($item_categories_array) ; $i++) { if ($item_categories_array[$i] == 1) { if (!is_null($performed_procedures_array[$i])) { $ordered_procedures_array[] = $performed_procedures_array[$i]; $ordered_procedures_amount_array[] = $items_prices_array[$i]; } if (!is_null($performance_fees_array[$i]) && !is_null($performed_by_ids_array[$i])) { $is_procedure_performed_array[] = 1; $is_procedure_performed_id_array[] = record_staff_that_has_performed_the_service_with_price($patient_id, $episode_id, 1, $performed_procedures_array[$i], 0, $performed_by_ids_array[$i], $performance_fees_array[$i], date('Y-m-d')); } else { $is_procedure_performed_array[] = 0; $is_procedure_performed_id_array[] = 0; } } elseif ($item_categories_array[$i] == 2) { $inv_order_type = get_name(get_name(get_name($performed_investigations_array[$i], "id", "category", "investigations"), "id", "super_category_id", "investigation_categories"),"id", "id", "investigation_super_categories"); if ($inv_order_type == 1) { $lab_investigation_array[] = $performed_investigations_array[$i]; $lab_inv_prices_array[] = $items_prices_array[$i]; } elseif ($inv_order_type == 2) { $xray_investigation_array[] = $performed_investigations_array[$i]; $xray_inv_prices_array[] = $items_prices_array[$i]; } elseif ($inv_order_type == 3) { $ultrasound_investigation_array[] = $performed_investigations_array[$i]; $ultrasound_inv_prices_array[] = $items_prices_array[$i]; } elseif ($inv_order_type == 4) { $obstetric_ultrasound_investigation_array[] = $performed_investigations_array[$i]; $obstetric_inv_prices_array[] = $items_prices_array[$i]; } elseif ($inv_order_type == 5) { $cardiology_investigation_array[] = $performed_investigations_array[$i]; $cardiology_inv_prices_array[] = $items_prices_array[$i]; } if (!is_null($performance_fees_array[$i]) && !is_null($performed_by_ids_array[$i])) { $ignored_return_value = record_staff_that_has_performed_the_service_with_price($patient_id, $episode_id, 2, $performed_investigations_array[$i], 0, $performed_by_ids_array[$i], $performance_fees_array[$i], date('Y-m-d')); } } elseif ($item_categories_array[$i] == 3) { if (!is_null($performed_services_array[$i])) { $ordered_services_array[] = $performed_services_array[$i]; $ordered_services_amounts_array[] = $items_prices_array[$i]; $ordered_services_quantity_array[] = $items_quantities_array[$i]; } if (!is_null($performance_fees_array[$i]) && !is_null($performed_by_ids_array[$i])) { $is_service_performed_array[] = 1; $is_service_performed_id_array[] = record_staff_that_has_performed_the_service_with_price($patient_id, $episode_id, 3, $performed_services_array[$i], 0, $performed_by_ids_array[$i], $performance_fees_array[$i], date('Y-m-d')); } else { $is_service_performed_array[] = 0; $is_service_performed_id_array[] = 0; } } elseif ($item_categories_array[$i] == 4) { if (!is_null($performed_sundries_array[$i])) { $ordered_sundries_array[] = $performed_sundries_array[$i]; $ordered_sundries_amounts_array[] = $items_prices_array[$i]; $ordered_sundries_quantity_array[] = $items_quantities_array[$i]; } } } // submit the orders to the respective tables if (count($ordered_procedures_array) > 0) { $new_ordered_procedure = new OrderedProcedure; $new_ordered_procedure->patient_id = $patient_id; $new_ordered_procedure->episode_id = $episode_id; $new_ordered_procedure->procedure_id = implode(",", $ordered_procedures_array); $new_ordered_procedure->procedure_amount = implode(",", $ordered_procedures_amount_array); $new_ordered_procedure->payment_status = 0; //0 by default to mean not paid $new_ordered_procedure->performed = implode(",", $is_procedure_performed_array); $new_ordered_procedure->performed_id = implode(",", $is_procedure_performed_id_array); $new_ordered_procedure->created_by = Auth::id(); $new_ordered_procedure->save(); // create an insurance claim for the ordered items if ($request->patient_insurance_status == 1) { generate_insurance_claim($new_ordered_procedure->id, 2); } } if (count($ordered_services_array) > 0) { $new_ordered_service = new OrderedService; $new_ordered_service->patient_id = $patient_id; $new_ordered_service->episode_id = $episode_id; $new_ordered_service->service_id = implode(",", $ordered_services_array); $new_ordered_service->service_amount = implode(",", $ordered_services_amounts_array); $new_ordered_service->quantity = implode(",", $ordered_services_quantity_array); $new_ordered_service->payment_status = 0; //0 by default to mean not paid $new_ordered_service->performed = implode(",", $is_service_performed_array); $new_ordered_service->performed_id = implode(",", $is_service_performed_id_array); $new_ordered_service->created_by = Auth::id(); $new_ordered_service->save(); // create an insurance claim for the ordered items if ($request->patient_insurance_status == 1) { generate_insurance_claim($new_ordered_service->id, 1); } } if (count($ordered_sundries_array) > 0) { $new_ordered_sundry = new OrderedSundry; $new_ordered_sundry->patient_id = $patient_id; $new_ordered_sundry->episode_id = $episode_id; $new_ordered_sundry->sundries_id = implode(",", $ordered_sundries_array); $new_ordered_sundry->sundries_amount = implode(",", $ordered_sundries_amounts_array); $new_ordered_sundry->quantity = implode(",", $ordered_sundries_quantity_array); $new_ordered_sundry->payment_status = 0; //0 by default to mean not paid $new_ordered_sundry->created_by = Auth::id(); $new_ordered_sundry->save(); // create an insurance claim for the ordered items if ($request->patient_insurance_status == 1) { generate_insurance_claim($new_ordered_sundry->id, 5); } } // start looping thru for any investigations if (count($lab_investigation_array) > 0) { $investigation_ids_string = ""; $for_inpatient_string = ""; $urgent_ids = ""; $commments_string = ""; for ($i = 0; $i < count($lab_investigation_array); $i++) { if ($i == (count($lab_investigation_array) - 1)) { $investigation_ids_string .= $lab_investigation_array[$i]; $for_inpatient_string .= "0"; $urgent_ids .= ""; $commments_string .= ""; } else { $investigation_ids_string .= $lab_investigation_array[$i] . ","; $for_inpatient_string .= "0" . ","; $urgent_ids .= ","; $commments_string .= ","; } } $order = new OrderedInvestigation; $order->investigation_id = $investigation_ids_string; $order->investigation_amount = implode(",", $lab_inv_prices_array); $order->patient_id = $patient_id; $order->episode_id = $episode_id; $order->urgent_ids = $urgent_ids; $order->inpatient = 0; $order->for_inpatient = $for_inpatient_string; $order->order_type = "Lab"; $order->order_comments = $commments_string; $order->created_by = Auth::id(); $order->save(); // create an insurance claim for the ordered items if ($request->patient_insurance_status == 1) { generate_insurance_claim($order->id, 3); } } if (count($xray_investigation_array) > 0) { $investigation_ids_string = ""; $for_inpatient_string = ""; $urgent_ids = ""; $commments_string = ""; for ($i = 0; $i < count($xray_investigation_array); $i++) { if ($i == (count($xray_investigation_array) - 1)) { $investigation_ids_string .= $xray_investigation_array[$i]; $for_inpatient_string .= "0"; $urgent_ids .= ""; $commments_string .= ""; } else { $investigation_ids_string .= $xray_investigation_array[$i] . ","; $for_inpatient_string .= "0" . ","; $urgent_ids .= ","; $commments_string .= ","; } } $order = new OrderedInvestigation; $order->investigation_id = $investigation_ids_string; $order->investigation_amount = implode(",", $xray_inv_prices_array); $order->patient_id = $patient_id; $order->episode_id = $episode_id; $order->urgent_ids = $urgent_ids; $order->inpatient = 0; $order->for_inpatient = $for_inpatient_string; $order->order_type = "Imaging"; $order->order_comments = $commments_string; $order->created_by = Auth::id(); $order->save(); // create an insurance claim for the ordered items if ($request->patient_insurance_status == 1) { generate_insurance_claim($order->id, 3); } } if (count($ultrasound_investigation_array) > 0) { $investigation_ids_string = ""; $for_inpatient_string = ""; $urgent_ids = ""; $commments_string = ""; for ($i = 0; $i < count($ultrasound_investigation_array); $i++) { if ($i == (count($ultrasound_investigation_array) - 1)) { $investigation_ids_string .= $ultrasound_investigation_array[$i]; $for_inpatient_string .= "0"; $urgent_ids .= ""; $commments_string .= ""; } else { $investigation_ids_string .= $ultrasound_investigation_array[$i] . ","; $for_inpatient_string .= "0" . ","; $urgent_ids .= ","; $commments_string .= ","; } } $order = new OrderedInvestigation; $order->investigation_id = $investigation_ids_string; $order->investigation_amount = implode(",", $ultrasound_inv_prices_array); $order->patient_id = $patient_id; $order->episode_id = $episode_id; $order->urgent_ids = $urgent_ids; $order->inpatient = 0; $order->for_inpatient = $for_inpatient_string; $order->order_type = "Ultrasound"; $order->order_comments = $commments_string; $order->created_by = Auth::id(); $order->save(); // create an insurance claim for the ordered items if ($request->patient_insurance_status == 1) { generate_insurance_claim($order->id, 3); } } if (count($obstetric_ultrasound_investigation_array) > 0) { $investigation_ids_string = ""; $for_inpatient_string = ""; $urgent_ids = ""; $commments_string = ""; for ($i = 0; $i < count($obstetric_ultrasound_investigation_array); $i++) { if ($i == (count($obstetric_ultrasound_investigation_array) - 1)) { $investigation_ids_string .= $obstetric_ultrasound_investigation_array[$i]; $for_inpatient_string .= "0"; $urgent_ids .= ""; $commments_string .= ""; } else { $investigation_ids_string .= $obstetric_ultrasound_investigation_array[$i] . ","; $for_inpatient_string .= "0" . ","; $urgent_ids .= ","; $commments_string .= ","; } } $order = new OrderedInvestigation; $order->investigation_id = $investigation_ids_string; $order->investigation_amount = implode(",", $obstetric_inv_prices_array); $order->patient_id = $patient_id; $order->episode_id = $episode_id; $order->urgent_ids = $urgent_ids; $order->inpatient = 0; $order->for_inpatient = $for_inpatient_string; $order->order_type = "Ultrasound_Obstetric"; $order->order_comments = $commments_string; $order->created_by = Auth::id(); $order->save(); // create an insurance claim for the ordered items if ($request->patient_insurance_status == 1) { generate_insurance_claim($order->id, 3); } } if (count($cardiology_investigation_array) > 0) { $investigation_ids_string = ""; $for_inpatient_string = ""; $urgent_ids = ""; $commments_string = ""; for ($i = 0; $i < count($cardiology_investigation_array); $i++) { if ($i == (count($cardiology_investigation_array) - 1)) { $investigation_ids_string .= $cardiology_investigation_array[$i]; $for_inpatient_string .= "0"; $urgent_ids .= ""; $commments_string .= ""; } else { $investigation_ids_string .= $cardiology_investigation_array[$i] . ","; $for_inpatient_string .= "0" . ","; $urgent_ids .= ","; $commments_string .= ","; } } $order = new OrderedInvestigation; $order->investigation_id = $investigation_ids_string; $order->investigation_amount = implode(",", $cardiology_inv_prices_array); $order->patient_id = $patient_id; $order->episode_id = $episode_id; $order->urgent_ids = $urgent_ids; $order->inpatient = 0; $order->for_inpatient = $for_inpatient_string; $order->order_type = "Cardio"; $order->order_comments = $commments_string; $order->created_by = Auth::id(); $order->save(); // create an insurance claim for the ordered items if ($request->patient_insurance_status == 1) { generate_insurance_claim($order->id, 3); } } // end looping thru for any investigations flash('Items have been added')->success(); return redirect('patient_episodes'); } public function get_item_price(Request $request) { //categories 1-procedures, 2-investigations, 3-procedures $category_id = $request->category; $item_id = $request->item; $patient_insurance_status = $request->patient_insurance_status ?? 0; $patient_id = $request->patient_id ?? 0; $item_price = 0; $price_list_id = $request->price_list_id; $item_category = $request->item_category?? 0; if (!empty($item_category) && !empty($price_list_id)) $item_price = get_price_list_category_price($price_list_id, $item_category, $item_id); else { if (is_numeric($patient_insurance_status) && $patient_insurance_status == 1 && $patient_id != 0 && is_numeric($patient_id)) { if ($category_id == 1) { $item_price = get_item_insurance_co_payment($patient_id, $item_id, 2, false, 0); } elseif ($category_id == 2) { $item_price = get_item_insurance_co_payment($patient_id, $item_id, 3, false, 0); } elseif ($category_id == 3) { $item_price = get_item_insurance_co_payment($patient_id, $item_id, 1, false, 0); } elseif ($category_id == 4) { $item_price = get_item_insurance_co_payment($patient_id, $item_id, 5, false, 0); } } else { if ($category_id == 1) { $item_price = get_name($item_id, "id", "non_insured_price", "procedures"); } elseif ($category_id == 2) { $item_price = get_name($item_id, "id", "non_insured_price", "investigations"); } elseif ($category_id == 3) { $item_price = get_name($item_id, "id", "non_insured_price", "services"); } elseif ($category_id == 4) { $item_price = get_name($item_id, "id", "non_insured_price", "sundries"); } elseif ($category_id == 5) { $item_price = get_name($item_id, "id", "non_insured_price", "eye_glasses"); } } } return $item_price; } }