middleware('auth'); $this->middleware('permission:discount-list', ['only' => ['index']]); $this->middleware('permission:discount-create', ['only' => ['create', 'store']]); $this->middleware('permission:discount-edit', ['only' => ['edit', 'update']]); } /** * Display a listing of the resource. * */ public function index(){ $discounts = PatientDiscount::orderBy('patient_category', 'asc') ->paginate(50); $categories = DB::table('patient_categories')->where('available', 1)->pluck("name", "id")->toArray(); return view('patient_discounts::discounts.index',compact('discounts','categories')); } /** * Show the form for creating a new resource. * */ public function create(){ // make sure that patient discounts are not repeated $exiting_discount = PatientDiscount::all()->pluck('patient_category')->toArray(); $categories = PatientCategory::whereNotIn('id',$exiting_discount)->pluck("name", "id")->toArray(); $categories = ['' => '- select -'] + $categories; $expense_accounts = ChartOfAccount::where('type', 2)->pluck('name', 'id')->prepend('-select-', '')->toArray(); return view('patient_discounts::discounts.create',compact('categories', 'expense_accounts')); } /** * Store a newly created resource in storage. * */ public function store(Request $request){ request()->validate([ 'patient_category' => 'required', 'pay_later' => 'required' ]); $patient_category_discount = new PatientDiscount; $patient_category_discount->patient_category = $request->patient_category; $patient_category_discount->discount = $request->discount; $patient_category_discount->pay_later = $request->pay_later; if ($request->is_pay_later_threshold_discount == "1") { $patient_category_discount->threshold_type = $request->threshold_type; $patient_category_discount->threshold_amount = $request->threshold_amount; } $patient_category_discount->co_payment = $request->co_payment; $patient_category_discount->co_payment_share = $request->co_payment_share; $patient_category_discount->tracking_expense_account = $request->tracking_expense_account; $patient_category_discount->created_by = Auth::id(); if ($patient_category_discount->save()) { flash('new patient discount saved')->success(); return redirect('/discounts/'); } } /** * 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\Contracts\View\Factory|\Illuminate\View\View */ public function edit($id){ $discount = PatientDiscount::find($id); $categories = DB::table('patient_categories')->where('available', 1)->pluck("name", "id")->toArray(); $expense_accounts = ChartOfAccount::where('type', 2)->pluck('name', 'id')->prepend('-select-', '')->toArray(); return view('patient_discounts::discounts.edit',compact('discount','categories', 'id', 'expense_accounts')); } /** * Update the specified resource in storage. * * @param \Illuminate\Http\Request $request * @param int $id */ public function update(Request $request) { $validator = Validator::make($request->all(), [ 'discount' => 'required', 'pay_later' => 'required' ]); if ($validator->fails()) { $string = ""; foreach ($validator->errors()->getMessages() as $item) { $string .= "{$item[0]}
"; } flash($string)->error(); return back()->withErrors($validator)->withInput(); } else { $patient_category_discount = PatientDiscount::find($request->id); $patient_category_discount->discount = $request->discount; $patient_category_discount->pay_later = $request->pay_later; $patient_category_discount->updated_by = $request->discount; if ($patient_category_discount->pay_later == 0) { $request->is_pay_later_threshold_discount = 0; $request->co_payment = 0; } if ($request->is_pay_later_threshold_discount == 1) { $patient_category_discount->threshold_type = $request->threshold_type; $patient_category_discount->threshold_amount = $request->threshold_amount; } else { $patient_category_discount->threshold_type = NULL; $patient_category_discount->threshold_amount = NULL; } if ($request->track_discounts == 1 || $request->is_pay_later_threshold_discount == 1) { $patient_category_discount->tracking_expense_account = $request->tracking_expense_account; } else { $patient_category_discount->tracking_expense_account = NULL; } $patient_category_discount->co_payment = $request->co_payment; if ($request->co_payment == 1) { $patient_category_discount->co_payment_share = $request->co_payment_share; } else { $patient_category_discount->co_payment_share = 0; } if ($patient_category_discount->update()) { flash('Patient discount has been updated')->success(); } else { flash("An error occurred!")->error(); } return redirect('/discounts/'); } } /** * Remove the specified resource from storage. * * @param int $id */ public function destroy($id) { if (PatientDiscount::destroy($id)) { flash("Patient discount has been deleted.")->success(); return redirect('/discounts/'); } else { flash("Patient discount failed to deleted.")->error(); return redirect('/discounts/'); } } /** * Display a listing of the inactive resource(s). * */ public function inactive() { $discounts = PatientDiscount::onlyTrashed() ->orderBy('patient_category', 'asc') ->paginate(50); $categories = DB::table('patient_categories')->where('available', 1)->pluck("name", "id")->toArray(); if (count($discounts) < 1) { flash()->error("There is no inactive discount"); return redirect('/discounts/'); } else { return view('patient_discounts::discounts.inactive', compact('discounts', 'categories')); } } /** * Activate the specified resource in storage. * * @param int $id */ public function activate($id) { $discount = PatientDiscount::withTrashed()->find($id); if($discount->restore()){ flash("Patient discount has been activated.")->success(); return redirect('/discounts/inactive'); } else { flash("Patient discount failed to activate.")->error(); return redirect('/discounts/inactive'); } } public function edit_insured_drugs($patient_category_id) { $drugs = DB::table('drugs')->whereNull('deleted_at')->get(['id', 'name', 'patient_category_coverage']); return view('patient_discounts::discounts.edit_insured_drugs', compact('drugs', 'patient_category_id')); } public function store_edited_insured_drugs(Request $request) { $is_covered = $request->is_covered; $current_covered = is_null($request->current_covered) ? [] : $request->current_covered; $removed_coverage = array_diff($current_covered, $is_covered); foreach ($is_covered as $value) { $current_coverages = get_name($value, 'id', 'patient_category_coverage', 'drugs'); if ($current_coverages != 'N/A') { if (is_null($current_coverages) || $current_coverages == "") { $current_coverages_array = []; } else { $current_coverages_array = explode(",", $current_coverages); // check if the id is already in the array and just chill if (in_array($request->patient_category_id, $current_coverages_array)) { continue; } } $current_coverages_array[] = $request->patient_category_id; DB::table('drugs')->where('id', $value)->update(['patient_category_coverage' => implode(",", $current_coverages_array)]); } } foreach ($removed_coverage as $value) { $current_coverages = get_name($value, 'id', 'patient_category_coverage', 'drugs'); if ($current_coverages != 'N/A') { $current_coverages_array = explode(",", $current_coverages); if (in_array($request->patient_category_id, $current_coverages_array)) { unset($current_coverages_array[array_search($request->patient_category_id, $current_coverages_array)]); } DB::table('drugs')->where('id', $value)->update(['patient_category_coverage' => implode(",", $current_coverages_array)]); } } flash("Drug coverages have been updated")->success(); return redirect('/discounts/edit_insured_drugs/' . $request->patient_category_id); } public function add_category_patient_dependants(Request $request) { $discount_id = $request->discount_id; $patient_discount = PatientDiscount::find($discount_id); $patient_category_patients = Patient::where('category_id', $patient_discount->patient_category)->orderBy('first_name', 'asc')->get(); /* $patient_category_patients = DB::table("patients")->select('*')->where('category_id', $patient_discount->patient_category)->whereRaw('!FIND_IN_SET(id,'.function($query) { $query->select('dependant_patient_ids')->from('category_patient_dependants'); }.')')->get(); */ return view('patient_discounts::discounts.patient_category_patients', compact('patient_discount','patient_category_patients')); } /* * Display blade to enable adding dependants to a main category patient */ public function add_dependants_to_category_patient(Request $request) { $patient_id = $request->patient_id; $patient_category_id = $request->patient_category; $patient_discount_id = $request->patient_discount_id; $patient = Patient::find($patient_id); $patients_array = [];//Patient::all(['id', 'first_name', 'last_name'])->where('category_id', 1)->pluck("full_name", "id")->toArray(); return view('patient_discounts::discounts.add_dependants_to_category_patient', compact('patient_id','patient_discount_id','patient_category_id','patient','patients_array')); } /* * Store the added dependants */ public function store_dependants_to_category_patient(Request $request) { //check to make sure that the dependants are not attached to another dependant or even are the main patients $all_main_patients_array = CategoryPatientDependant::pluck('main_patient_id')->toArray(); $patient_dependants_array = is_null($request->dependants) ? [] : $request->dependants; for ($i=0; $i < count($patient_dependants_array) ; $i++) { if (in_array($patient_dependants_array[$i],$all_main_patients_array)) { flash('Patient '.get_full_name($patient_dependants_array[$i], "id", "first_name", "last_name", "patients").' is already a main patient and so can not be another person\'s dependant ')->error(); return redirect('discounts'); } $all_dependants = CategoryPatientDependant::whereRaw('FIND_IN_SET(' . $patient_dependants_array[$i] . ',dependant_patient_ids)')->get(); if (count($all_dependants)) { $patient_is_a_dependant_of = patient_is_a_dependant_of($patient_dependants_array[$i]); if (!is_null($patient_is_a_dependant_of) && ($patient_is_a_dependant_of != $request->patient_id)) { flash('Patient '.get_full_name($patient_dependants_array[$i], "id", "first_name", "last_name", "patients").' is already a dependant of someone else')->error(); return redirect('discounts'); } } //make sure that they are not configured to the same category $main_patient_category = get_name($request->patient_id, "id", "category_id", "patients"); $dependant_patient_category = get_name($patient_dependants_array[$i], "id", "category_id", "patients"); if ($main_patient_category == $dependant_patient_category) { flash('Patient '.get_full_name($patient_dependants_array[$i], "id", "first_name", "last_name", "patients").' belongs to the same patient category. Both the dependant and dependee can not belong to the same category')->error(); return redirect('discounts'); } } $patient = Patient::find($request->patient_id); $patient_category_id = $request->patient_category_id; $start_date = is_null($request->start_date) ? null : Carbon::createFromFormat('d/m/Y', $request->start_date)->toDateString(); $patient_category_depedants_record = CategoryPatientDependant::where('main_patient_id', $request->patient_id)->get(); if (count($patient_category_depedants_record) > 0) { $patient_category_depedants = $patient_category_depedants_record->first(); $patient_category_depedants->main_patient_id = $request->patient_id; $patient_category_depedants->dependant_patient_ids = implode(",", $patient_dependants_array); $patient_category_depedants->patient_category_id = $patient_category_id; $patient_category_depedants->start_date = $start_date; $duration = 0; $duration = get_name($patient_category_id, "patient_category", "threshold_type", "patient_discounts"); if ($duration == 2) { $duration = 12; //12 months } if ($duration == 1) { $duration = 1; //1 month } $patient_category_depedants->duration = $duration; $patient_category_depedants->update(); } else { $patient_category_depedants = new CategoryPatientDependant; $patient_category_depedants->main_patient_id = $request->patient_id; $patient_category_depedants->dependant_patient_ids = implode(",", $patient_dependants_array); $patient_category_depedants->patient_category_id = $patient_category_id; $patient_category_depedants->start_date = $start_date; $duration = 0; $duration = get_name($patient_category_id, "patient_category", "threshold_type", "patient_discounts"); if ($duration == 2) { $duration = 12; //12 months } if ($duration == 1) { $duration = 1; //1 month } $patient_category_depedants->duration = $duration; $patient_category_depedants->save(); } flash('new dependants have been saved')->success(); return redirect('discounts'); } public function view_patient_dependant_details(Request $request) { $patient_category_depedant = CategoryPatientDependant::first(); return view('patient_discounts::discounts.view_patient_dependant_details', compact('patient_category_depedant')); } /* View all dependants to a category patient */ public function view_dependants_to_category_patient($id) { $patient_category_depedants_record = CategoryPatientDependant::where('main_patient_id', $id)->get(); $patient = Patient::find($id); $dependants_record = null; if (count($patient_category_depedants_record) > 0) { $dependants_record = $patient_category_depedants_record->first(); return view('patient_discounts::discounts.view_dependants_to_category_patient', compact('patient','dependants_record')); } return view('patient_discounts::discounts.view_dependants_to_category_patient', compact('patient','dependants_record')); } public function search_dependant_name(Request $request) { $data = []; if ($request->has('q')) { $search = $request->q; $data = DB::table('patients') ->where('category_id', 1) ->where('first_name', 'LIKE', "%$search%") ->orWhere('last_name', 'LIKE', "%$search%") ->orWhere('number', 'LIKE', "%$search%") ->get(["id", "first_name", "last_name", "number", "phone"]); } return response()->json($data); } public function generate_dependants_payment_invoice(Request $request) { $main_patient_id = $request->main_patient_id; $dependants_array = []; $dependants = CategoryPatientDependant::where('main_patient_id', $main_patient_id)->get(); $consumptions = DependantsConsumption::where('main_patient_id', $main_patient_id)->get(); $patient_category_id = 0; foreach ($dependants as $record) { $dependants_array = explode(",", $record->dependant_patient_ids); $patient_category_id = $record->patient_category_id; } $dependants_array[] = $main_patient_id; //add the main patient in array $start_of_year = Carbon::now()->startOfYear(); $start = $start_of_year->toDateTimeString(); $end = Carbon::now()->toDateString(); $patient_category_name = get_name($patient_category_id, "id", "name", "patient_categories"); $invoice_number = generateInvoiceNumberFromDB($patient_category_name); $invoice_start = Carbon::parse($start)->format('d-m-Y'); $invoice_date = $invoice_start."/".$end; //according to how Benjamin organized it $invoice_with_payment_amount_paid = $invoice_with_payment_balance = 0; $update = PatientCategoryInvoice::where('invoice_generated', 0) ->where('patient_category', $patient_category_id) ->whereIn('patient_id', $dependants_array) ->update( [ 'invoice_generated' => 1, 'invoice_date' => $invoice_date, 'invoice_number' => $invoice_number, 'is_invoice_for_individual' => $main_patient_id ] ); $track_invoice = new TrackInvoice; $track_invoice->reason = $patient_category_id; $track_invoice->created_by = Auth::id(); if (!is_null($update) && $track_invoice->save()) { $hospital_information = HospitalInformation::first(); $users_name = get_full_name(Auth::id(), 'id', 'first_name', 'last_name', 'users'); $start_date = Carbon::parse($start)->startOfDay()->toDateTimeString(); $end_date = Carbon::parse($end)->endOfDay()->toDateTimeString(); //update the dependant consumption table with the generated invoices number $consumption = DependantsConsumption::where('main_patient_id', $main_patient_id)->whereNull('invoice_number') ->where('unpaid_balance', '>', 0) ->whereBetween('created_at', [$start_date, $end_date]) ->update(['invoice_number' => $invoice_number]); $last_invoice = DB::table('patient_category_invoices')->select('invoice_number')->distinct()->orderBy('invoice_number', 'desc')->first(); $last_invoice_number = $invoice_number; $invoices = DB::table('patient_category_invoices') ->whereNotIn('patient_id', findTestOrDemoPatients()) ->whereBetween('created_at', [$start_date, $end_date]) ->where('patient_category', $patient_category_id) ->where('invoice_generated', 1) ->where('status', 0) ->where('invoice_number', $last_invoice_number) ->groupBy('episode_id')->selectRaw('*, sum(patient_amount) as sum') ->get() ->toArray(); $request->merge(['invoice_number' => $invoice_number]); $request->merge(['patient_category' => $patient_category_id]); $request->merge(['start_date' => $start_date]); $request->merge(['end_date' => $end_date]); return view('invoices::invoices.generate_invoice.patient_invoice', compact( 'invoices', 'request', 'users_name', 'hospital_information', 'invoice_with_payment_amount_paid', 'invoice_with_payment_balance' )); } else { return back()->withInput(); } } /* receive payment for a patient and his dependants */ public function receive_dependants_payment(Request $request) { //load the page that loads invoice payment with the requests as below /* "patient_category" => "15" "created_by" => "17" "created_at" => "2021-02-01 10:35:49" "status" => "new" "total" => "3150600" "invoice_date" => "01-02-2021/2021-02-27" "invoice_number" => "0865" */ } public function clean_dependants_that_are_main_patients() { //1. loop through category_patient_dependants table //2. explode dependants and check if any is main patient //3. if main patient record exists, delete main patient record, soft delete consumption and make records unpaid //4. soft delete episode record from patient_category_invoices and make ordered record not paid $patient_category_dependants = CategoryPatientDependant::all(); if (count($patient_category_dependants) > 0) { foreach ($patient_category_dependants as $category_record) { $dependants_array = explode(",", $category_record->dependant_patient_ids); for ($i=0; $i < count($dependants_array) ; $i++) { $checking_for_main_patients = CategoryPatientDependant::where('main_patient_id',$dependants_array[$i])->get(); if (count($checking_for_main_patients) > 0) { foreach ($checking_for_main_patients as $main_record) { $main_record->delete(); $consumptions_records = DependantsConsumption::where('main_patient_id', $main_record->main_patient_id)->get(); if (count($consumptions_records) > 0) { foreach ($consumptions_records as $consumption) { $consumption->delete(); //treatments $treatments = Treatment::where('episode_id', $consumption->episode_id)->get(); if (count($treatments) > 0) { foreach ($treatments as $treatment) { $treatment->update(['payment_status' => 0]); } } //investigations $investigations = OrderedInvestigation::where('episode_id', $consumption->episode_id)->get(); if (count($investigations) > 0) { foreach ($investigations as $ordered_investigation) { $ordered_investigation->update(['payment_status' => 0]); } } //procedures $procedures = OrderedProcedure::where('episode_id', $consumption->episode_id)->get(); if (count($procedures) > 0) { foreach ($procedures as $ordered_procedure) { $ordered_procedure->update(['payment_status' => 0]); } } //services $services = OrderedService::where('episode_id', $consumption->episode_id)->get(); if (count($services) > 0) { foreach ($services as $ordered_service) { $ordered_service->update(['payment_status' => 0]); } } //sundrys $sundrys = OrderedSundry::where('episode_id', $consumption->episode_id)->get(); if (count($sundrys) > 0) { foreach ($sundrys as $ordered_sundry) { $ordered_sundry->update(['payment_status' => 0]); } } //patient category invoice $patient_category_invoice = PatientCategoryInvoice::where('episode_id',$consumption->episode_id)->delete(); } } } } } } } } }