Files
streamline-emr/docker/statistics/Modules/Insurance/Http/Controllers/HeadsOfFamilyController.php
T
2025-03-31 03:46:05 +03:00

295 lines
13 KiB
PHP
Executable File

<?php
namespace Modules\Insurance\Http\Controllers;
use Illuminate\Http\Request;
use Streamline\Models\HeadOfFamily;
use Streamline\Models\InsuranceMember;
use Streamline\Models\InsuranceGroup;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Validator;
use Streamline\Models\Patient;
use Illuminate\Support\Facades\Auth;
class HeadsOfFamilyController extends Controller {
public function __construct() {
$this->middleware('auth');
$this->middleware('permission:family-head-list');
$this->middleware('permission:family-head-create', ['only' => ['create', 'store']]);
$this->middleware('permission:family-head-edit', ['only' => ['edit', 'update']]);
$this->middleware('permission:family-head-delete', ['only' => ['destroy']]);
}
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index() {
$family_heads = HeadOfFamily::orderBy('id', 'asc')
->join('patients', 'insurance_heads_of_family.patient_id', '=', 'patients.id')
->select('insurance_heads_of_family.*', 'patients.phone', 'patients.number')->paginate(200);
$insurance_groups = InsuranceGroup::pluck('name', 'id');
$full_names = HeadOfFamily::leftJoin('patients', 'insurance_heads_of_family.patient_id', '=', 'patients.id')
->select(DB::raw('CONCAT(first_name, " ", last_name, " ", number) AS full_name'))
->pluck("full_name");
$patient_numbers = HeadOfFamily::leftJoin('patients', 'insurance_heads_of_family.patient_id', '=', 'patients.id')->pluck('number');
return view('insurance::heads_of_family.index', compact('insurance_groups', 'family_heads', 'full_names', 'patient_numbers'));
}
/**
* Show search results when looking for a family head.
*
*/
public function search(Request $request) {
$insurance_groups = InsuranceGroup::pluck('name', 'id');
$criteria = '';
$filters = [];
if (!empty($request->full_name)){
$split_full_name_array = explode(' ', $request->full_name);
if (is_array($split_full_name_array)) {
$first_name_from_split = $split_full_name_array[0] ?? "";
$last_name_from_split = $split_full_name_array[1] ?? "";
// for people with 3, 4 names make sure to get last element
$patient_number_from_split = count($split_full_name_array) > 2 ? array_slice($split_full_name_array, -1)[0] : '';
//just only use the patient number from the full name submitted
if ($patient_number_from_split != "") {
$filters[] = ['number', 'LIKE', '%' . $patient_number_from_split . '%'];
$criteria .= 'Number (' . $patient_number_from_split . ') ';
} else {
$filters[] = ['first_name', 'LIKE', '%' . $first_name_from_split . '%'];
$filters[] = ['last_name', 'LIKE', '%' . $last_name_from_split . '%'];
$criteria .= 'Name (' . $request->full_name . ') ';
}
} else {
//just only use the patient number from the full name submitted
$filters[] = ['first_name', 'LIKE', '%' . $request->full_name . '%'];
$criteria .= 'Name (' . $request->full_name . ') ';
}
}
$family_heads_query = HeadOfFamily::query()
->leftJoin('patients', 'insurance_heads_of_family.patient_id', '=', 'patients.id')
->leftJoin('insurance_members', 'insurance_heads_of_family.patient_id', '=', 'insurance_members.patient_id')
->select('insurance_heads_of_family.*', 'patients.phone', 'patients.number')
->where($filters)->orderBy('id', 'asc');
if ($request->member_type == 1){
if(!empty($criteria)) $criteria .= 'and ';
$family_heads_query->where('insurance_members.current_insurance_end_date', '>', date('Y-m-d'));
$criteria .= __('heads_of_family.family_heads_with_active_insurance') . ' ';
} elseif ($request->member_type == 2) {
if(!empty($criteria)) $criteria .= 'and ';
$family_heads_query->whereRaw("(`insurance_members`.`current_insurance_end_date` is null or `insurance_members`.`current_insurance_end_date` <= '" . date('Y-m-d') . "')");
$criteria .= __('heads_of_family.family_heads_with_inactive_insurance') . ' ';
}
$family_heads = $family_heads_query->paginate(200);
$full_names = HeadOfFamily::leftJoin('patients', 'insurance_heads_of_family.patient_id', '=', 'patients.id')
->select(DB::raw('CONCAT(first_name, " ", last_name, " ", number) AS full_name'))
->pluck("full_name");
return view('insurance::heads_of_family.index', compact('insurance_groups', 'family_heads','criteria', 'full_names'));
}
/**
* Store a newly created resource in storage.
*
*/
public function store(Request $request) {}
/**
* Display the specified resource.
*
*/
public function show($id) {}
/**
* Show the form for editing the specified resource.
*
*/
public function edit($id) {}
/**
* Update the specified resource in storage.
*
*/
public function update(Request $request, $id) {}
/**
* Remove the specified resource from storage.
*
*/
public function destroy($id) {
$head_of_family = HeadOfFamily::findOrFail($id);
if(!empty($head_of_family->family_members_ids)){
flash()->error("Families with members can not be DELETED!");
return redirect('/heads_of_family/');
}
elseif($head_of_family->delete()) {
flash("Family was successfully deleted.")->success();
return redirect('/heads_of_family/');
}else {
flash()->error("Failed to delete Family!");
return redirect('/heads_of_family/');
}
}
/**
* Display a listing of the inactive resource(s).
*
* @return \Illuminate\Http\Response
*/
public function inactive() {
$family_heads = HeadOfFamily::onlyTrashed()
->orderBy('id', 'asc')
->join('patients', 'insurance_heads_of_family.patient_id', '=', 'patients.id')
->select('insurance_heads_of_family.*', 'patients.phone', 'patients.number')
->paginate(50);
$insurance_groups = DB::table('insurance_groups')->pluck('name', 'id');
$insurance_subscriptions = DB::table('insurance_subscriptions')->pluck('end_date', 'group_id');
if (empty($family_heads)) {
flash()->error("There is no inactive family heads");
return redirect('/heads_of_family/');
} else {
return view('insurance::heads_of_family.inactive', compact('insurance_groups', 'family_heads', 'insurance_subscriptions'));
}
}
/**
* Activate the specified resource in storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function activate($id) {
$head_of_family = HeadOfFamily::withTrashed()->find($id);
//edit is_family record in insurance members table
$insurance_member = InsuranceMember::where(['patient_id' => $head_of_family->patient_id])->first();
$insurance_member->is_family_head = 1;
$patient_id = $insurance_member->patient_id;
$patient = Patient::find($patient_id);
$patient->insurance_status = 1;
$patient->save();
if ($head_of_family->restore() && $insurance_member->save()):
flash("Head of Family has been activated.")->success();
return redirect('/heads_of_family/');
endif;
}
public function view_family_members_of_head($family_head_id)
{
$code = '';
$i = 1;
$members = InsuranceMember::where(['family_id' => $family_head_id])->get();
$code .= '<table class="table success-table table-sm">';
$family_total_premiums_to_pay = 0;
$family_members_to_exclude_array = session()->has($family_head_id) ? session()->get($family_head_id) : [];
//check if families exist in this group
if ($members->count() > 0) {
$code .= '<tbody class="dynamicFamilyMembers">';
$code .= '<tr><th>#</th><th><b>Name</b></th><th><b>Premium</b></th><th><b>Relation to head</b></th><th></th></tr>';
//check if a subscription has already been made to avoid duplication
foreach ($members as $member) {
if (!in_array($member->id, $family_members_to_exclude_array)) {
$code .= '<tr>';
$code .= '<td>';
$code .= $i;
$code .= '</td>';
$code .= '<td>';
$code .= insurance_flag($member->patient_id).' ('.get_name($member->patient_id, "id", "number", "patients").')';
$code .= ($member->is_family_head == 1) ? ' <span style="color: #7b6dc7; font-weight: bolder"><strong>Family Head</strong></span>' : '';
$code .= '</td>';
$code .= '<td>';
$code .= ugandan_shillings($member->premium);
$code .= '</td>';
$code .= '<td>';
$code .= get_name($member->relationship_to_head, "id", "name", "family_relations");
$code .= '</td>';
$code .= '<td><a href="/change_family_head/'.$family_head_id.'/'.$member->id.'" onclick="return confirm(\'Are you sure you want to change the head of this family?\')">Make family head</a></td>';
$code .= '</tr>';
$i++;
$family_total_premiums_to_pay += $member->premium;
}
}
$code .= '<tr><td colspan="2"><b>Total</b></td><td colspan="4"><b>'.ugandan_shillings($family_total_premiums_to_pay).'</b></td><tr>';
$code .= '<tbody>';
}
$code .= '</table>';
//$code .= '<a href="#" class="btn btn-info btn-sm btn-rounded addMemberBtn" id="'.$family_head_id.'">Add new member to this family</a>';
return $code;
}
public function change_family_head($current_head, $member_id)
{
$insurance_member = InsuranceMember::where(['family_id' => $current_head, 'id' => $member_id])->orderBy('id', 'desc')->first();
$family_head_patient_id = get_name($current_head, "id", "patient_id", "insurance_heads_of_family");
if ($insurance_member) {
$head_of_family = HeadOfFamily::find($current_head);
$patient = Patient::withTrashed()->find($insurance_member->patient_id);
$head_of_family->name = $patient->first_name . " " . $patient->last_name;
$head_of_family->group_id = $insurance_member->group_id;
$head_of_family->patient_id = $patient->id;
$head_of_family->created_by = auth()->user()->id;
$head_of_family->updated_by = auth()->user()->id;
if ($head_of_family->save()) {
$insurance_member->family_id = $head_of_family->id;
$insurance_member->is_family_head = 1;
$insurance_member->update();
//update all members related to the previous family head
$attached_insurance_members = InsuranceMember::where(['family_id' => $current_head])->get();
if (count($attached_insurance_members)) {
foreach ($attached_insurance_members as $attached_member) {
$attached_member->family_id = $head_of_family->id;
$attached_member->is_family_head = ($attached_member->id == $member_id) ? 1 : 0;
$attached_member->update();
}
}
}
flash(get_full_name($insurance_member->patient_id, "id", "first_name", "last_name", "patients").' has been made the head from the family of '.get_full_name($family_head_patient_id, "id", "first_name", "last_name", "patients"))->success();
} else{
flash('Family member could not be found')->error();
}
return redirect('heads_of_family');
}
public function search_family_head_by_name_number(Request $request)
{
$data = [];
if ($request->has('q')) {
$search = $request->q;
$data = HeadOfFamily::leftJoin('patients', 'insurance_heads_of_family.patient_id', '=', 'patients.id')
->select("patients.id", "first_name", "last_name", "number", "phone")
->where('name', 'LIKE', "%$search%")
->orWhere('number', 'LIKE', "%$search%")
->get();
}
return response()->json($data);
}
}