middleware('auth'); $this->middleware('permission:finance-patient-search', ['only' => ['search_patients', 'search_results','home']]); $this->middleware('permission:make-sundries-deposits', ['only' => ['make_deposit_sundries', 'store_sundries_deposit','print_sundries_receipt']]); $this->middleware('permission:make-procedures-deposits', ['only' => ['make_deposit_procedures', 'store_deposit_procedures','print_procedures_receipt']]); $this->middleware('permission:make-investigations-deposits', ['only' => ['make_deposit_investigations', 'store_deposit_investigations','print_investigation_receipt']]); $this->middleware('permission:make-treatment-deposits', ['only' => ['make_deposit_treatment', 'store_deposit_treatment','print_treatment_receipt']]); $this->middleware('permission:make-co-payments-deposits', ['only' => ['make_deposit_co_payments', 'store_deposit_services','print_service_receipt']]); $this->middleware('permission:make-other-services-deposits', ['only' => ['make_deposit_other_services', 'store_deposit_services','print_service_receipt']]); $this->middleware('permission:make-consultation-deposits', ['only' => ['make_deposit_consultation', 'store_deposit_services','print_service_receipt']]); $this->middleware('permission:make-central-billing', ['only' => ['make_deposit_central_billing', 'store_deposit_central_billing']]); } /** * Display the patient search for the finance section * * @return \Illuminate\Http\Response */ public function search_patients(){ $PatientCount = Patient::count(); $patients = Patient::orderBy('id', 'desc')->paginate(20); $patient_categories = DB::table('patient_categories')->where('available', 1)->pluck("name", "id"); $patient_numbers = Patient::orderBy('number')->distinct()->pluck('number'); $first_names = Patient::orderBy('first_name')->distinct()->pluck('first_name'); $last_names = Patient::orderBy('last_name')->distinct()->pluck('last_name'); $national_ids = Patient::orderBy('national_id')->distinct()->pluck('national_id'); $subcounties = Subcounty::pluck("name", "id"); $parishes = Parish::pluck("name", "id"); $villages = Village::pluck("name", "id"); $insurance_groups = InsuranceGroup::pluck("name", "id"); return view('patient_finance::patient_finance.search_patients',compact('PatientCount','patients', 'patient_categories', 'patient_numbers', 'first_names', 'last_names','national_ids','insurance_groups','subcounties','parishes','villages')); } /** * Search for the patient information given in the search and display results * * @param Request $request * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View */ public function search_results(Request $request){ $criteria = ''; $filters = []; $subcounty_ids = []; $parish_ids = []; $village_ids = []; $insurance_group_ids = []; $patient_category_ids = []; if (!empty($request->number)) { array_push($filters, ['number', 'LIKE', '%' . $request->number . '%']); $criteria .= 'Number (' . $request->number . ') '; } if (!empty($request->first_name)) { array_push($filters, ['first_name', 'LIKE', '%' . $request->first_name . '%']); $criteria .= 'First name (' . $request->first_name . ') '; } if (!empty($request->last_name)) { array_push($filters, ['last_name', 'LIKE', '%' . $request->last_name . '%']); $criteria .= 'Last name (' . $request->last_name . ') '; } if (!empty($request->national_id)) { array_push($filters, ['national_id', 'LIKE', '%' . $request->national_id . '%']); $criteria .= 'National ID (' . $request->national_id . ') '; } if (!empty($request->subcounty)) { $subcounty_ids = DB::table('subcounties')->where('name', 'LIKE', '%' . $request->subcounty . '%')->pluck('id'); $criteria .= 'Subcounty (' . $request->subcounty . ') '; } if (!empty($request->parish)) { $parish_ids = DB::table('parishes')->where('name', 'LIKE', '%' . $request->parish . '%')->pluck('id'); $criteria .= 'Parish (' . $request->parish . ') '; } if (!empty($request->village)) { $village_ids = DB::table('villages')->where('name', 'LIKE', '%' . $request->village . '%')->pluck('id'); $criteria .= 'Village (' . $request->village . ') '; } if (!empty($request->insurance_group)) { $insurance_group_ids = DB::table('insurance_groups')->where('name', 'LIKE', '%' . $request->insurance_group . '%')->pluck('id'); $criteria .= 'Insurance group (' . $request->insurance_group . ') '; } if (!empty($request->patient_category)) { $patient_category_ids = DB::table('patient_categories')->where('name', 'LIKE', '%' . $request->patient_category . '%')->pluck('id'); $criteria .= 'Category (' . $request->patient_category . ') '; } $patients = Patient::orderBy('first_name', 'asc') ->where($filters) ->orwhereIn('subcounty_id', $subcounty_ids) ->orWhereIn('parish_id', $parish_ids) ->orWhereIn('village_id', $village_ids) //->orWhereIn('insurance_group', $insurance_group_ids) ->orWhereIn('category_id', $patient_category_ids) ->paginate(50); $marital_statuses = DB::table('marital_statuses')->pluck("name", "id")->toArray(); $categories = DB::table('patient_categories')->where('available', 1)->pluck("name", "id")->toArray(); $occupations = DB::table('occupations')->pluck('name', 'id')->toArray(); return view('patient_finance::patient_finance.search_results',compact('criteria','patients','marital_statuses','categories','occupations')); } /** * Set patient session for finance * * @param $id * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector */ public function set_patient_session($id){ session()->put(['patient_id' => $id]); return redirect('/patient_finance/home'); } /** * Show the patient finance home * * @return \Illuminate\Http\Response */ public function home(){ $patient_id = session()->get('patient_id'); $episodes = PatientEpisode::where(['patient_id'=>$patient_id])->orderBy('id','desc')->paginate(20); $patient = Patient::find($patient_id); $categories = DB::table('patient_categories')->where('available', 1)->pluck("name", "id"); $drug_categories = DB::table('drug_categories')->get(); $marital_statuses = DB::table('marital_statuses')->pluck("name", "id"); $districts = DB::table('districts')->pluck('name', 'id'); $counties = DB::table('counties')->pluck('name', 'id'); $subcounties = DB::table('subcounties')->pluck('name', 'id'); $parishes = DB::table('parishes')->pluck('name', 'id'); $villages = DB::table('villages')->pluck('name', 'id'); $relationships = DB::table('family_relations')->pluck('name', 'id'); $occupations = DB::table('occupations')->pluck('name', 'id'); $patient_categories = DB::table('patient_categories')->where('available', 1)->pluck('name', 'id'); $pay_later_categories = PatientDiscount::where('pay_later', 1)->pluck('patient_category')->toArray(); return view('patient_finance::patient_finance.home',compact('episodes','patient','categories','drug_categories','marital_statuses', 'patient_id', 'districts', 'subcounties', 'counties', 'parishes', 'villages', 'relationships', 'occupations', 'patient_categories', 'pay_later_categories')); } /** * Select appropriate payment avenue according to selected input * * @param Request $request * @return \Illuminate\Http\RedirectResponse|\Illuminate\Routing\Redirector */ public function select_payment_option(Request $request){ // get the selected episode id and set the session if (!is_null($request->episode_id)) { session()->put(['episode_id' => $request->episode_id]); } if (!is_null($request->patient_id)) { session()->put(['patient_id' => $request->patient_id]); } $patient_id = session()->get('patient_id'); $episode_id = $request->episode_id; // check which payment option if(isset($request->consultation)){ if (patient_insurance_status($patient_id) == 1 && are_item_type_claims_available($episode_id, 1)) { return redirect('/patient_finance/make_chi_deposits/1'); } else { return redirect('/patient_finance/make_deposit_consultation'); } } elseif (isset($request->investigations)){ if (patient_insurance_status($patient_id) == 1 && are_item_type_claims_available($episode_id, 3)) { return redirect('/patient_finance/make_chi_deposits/3'); } else { return redirect('/patient_finance/make_deposit_investigations'); } } elseif (isset($request->treatment)){ if (patient_insurance_status($patient_id) == 1 && are_item_type_claims_available($episode_id, 4)) { return redirect('/patient_finance/make_chi_deposits/4'); } else { return redirect('/patient_finance/make_deposit_treatment'); } } elseif (isset($request->other_services)){ if (patient_insurance_status($patient_id) == 1 && are_item_type_claims_available($episode_id, 1)) { return redirect('/patient_finance/make_chi_deposits/1'); } else { return redirect('/patient_finance/make_deposit_other_services'); } } elseif (isset($request->co_payments)){ if (patient_insurance_status($patient_id) == 1 && are_item_type_claims_available($episode_id, 1)) { return redirect('/patient_finance/make_chi_deposits/1'); } else { return redirect('/patient_finance/make_deposit_co_payments'); } } elseif (isset($request->procedures)){ if (patient_insurance_status($patient_id) == 1 && are_item_type_claims_available($episode_id, 2)) { return redirect('/patient_finance/make_chi_deposits/2'); } else { return redirect('/patient_finance/make_deposit_procedures'); } } elseif (isset($request->sundries)){ if (patient_insurance_status($patient_id) == 1 && are_item_type_claims_available($episode_id, 5)) { return redirect('/patient_finance/make_chi_deposits/5'); } else { return redirect('/patient_finance/make_deposit_sundries'); } } elseif (isset($request->central_billing)){ if (patient_insurance_status($patient_id) == 1 && are_item_type_claims_available($episode_id, 0)) { return redirect('/patient_finance/make_chi_deposits/0'); } else { return redirect('/patient_finance/make_deposit_central_billing'); } } elseif (isset($request->inpatient_deposit)){ // get the patient id $patient_id = session()->get("patient_id"); // check if patient has a record with the inpatient $inpatient_info = InpatientInfo::where(['patient_id' => $patient_id,'episode_id' => $request->episode_id])->orderBy('created_at','desc')->first(); if ($inpatient_info){ return redirect('/patient_finance/make_inpatient_deposit'); } else { flash("Patient has no inpatient records to make inpatient deposits")->error(); return redirect()->back(); } } elseif (isset($request->inpatient_billing)){ // get the patient id $patient_id = session()->get("patient_id"); // check if patient has a record with the inpatient $inpatient_info = InpatientInfo::where(['patient_id' => $patient_id,'episode_id' => $request->episode_id])->first(); if ($inpatient_info){ return redirect('inpatient_billing'); } else { flash("Patient has no inpatient records to enable inpatient billing")->error(); return redirect()->back(); } } elseif (isset($request->refund_patient)){ return redirect('/patient_refund/choose_refund_point'); } elseif (isset($request->edit_claim_number)){ session()->put(['edit_claim_number' => 'finance_home']); return redirect('edit_claim_number'); } elseif (isset($request->patient_debts)){ $debt_paid = $this->check_debt($request->episode_id); if ($debt_paid === null) { flash("Patient has no active debt records")->error(); return redirect()->back(); } else { return redirect('patient_debtors/debtors_patient_search/' . session()->get("patient_id")); } }elseif (isset($request->patient_debt_plan)){ $debt_plan_paid = $this->check_patient_debt_plan($request->episode_id); if ($debt_plan_paid === null) { flash("Patient has no debt plan records")->error(); return redirect()->back(); } else { return redirect('patient_debtors/debt_plan_patient_search/' . session()->get("patient_id")); } } elseif (isset($request->optical_items)){ return redirect('/patient_finance/make_deposit_optical_items'); } else { return redirect('home'); } } /** * Show the form for creating a new resource. * * @return \Illuminate\Http\Response */ public function create(){ // } public function make_deposit_sundries(Request $request){ if (isset($request->price_list_category_id) && $request->price_list_category_id != 0){ $price_list_set = $request->price_list_category_id; } else { $price_list_set = false; } $price_list_categories = PriceListCategories::select('patient_category_id', 'id')->get(); $patient_id = session()->get('patient_id'); $episode_id = session()->get('episode_id'); $patient = Patient::find($patient_id); $episode = PatientEpisode::find($episode_id); $categories = DB::table('patient_categories')->where('available', 1)->pluck('name', 'id'); $drug_categories = DB::table('drug_categories')->get(); $marital_statuses = DB::table('marital_statuses')->pluck('name', 'id'); $discount = PatientDiscount::where(['patient_category'=>$patient->category_id])->first(); $discount_options = DiscountCategories::where(['patient_category' => $patient->category_id])->pluck('name','id')->toArray(); $discount_options = ['' => '- Select Discount -'] + $discount_options; $payment_arrangement = DB::table('debt_plan_arrangements')->pluck('name', 'id')->toArray(); $payment_arrangement = ['' => '- select -'] + $payment_arrangement; $staff_guarantors = DB::table('users')->pluck('first_name', 'id')->toArray(); foreach ($staff_guarantors as $key => $value) { $full_name = $value . " " . get_name($key, "id", "last_name", "users"); $staff_guarantors[$key] = $full_name; } $ordered_sundries = OrderedSundry::where(['episode_id' => $episode_id,'payment_status' => '0' ,'inpatient_bill_generated' => 0])->get(); // check if there are any paid procedure to display receipt $paid_sundries = SundryDeposit::where(['episode_id' => $episode_id])->get(); $invoiced_sundries = PatientCategoryInvoice::where('episode_id', $episode_id)->where('tag_id', 5)->get(); $patient_payment_methods = PatientPaymentMethod::pluck('name', 'id'); $patient_payment_methods_options = ""; foreach ($patient_payment_methods as $key => $value) { $patient_payment_methods_options .= ''; } $patient_unpaid_items_array = explode(",", $this->does_patient_have_unpaid_items($patient_id, $episode_id)); $patient_unpaid_items = $patient_unpaid_items_array[0]; $patient_debts = $patient_unpaid_items_array[1]; $non_staff_guarantors = DB::table('non_staff_guarantors')->pluck('first_name', 'id')->toArray(); foreach ($non_staff_guarantors as $key => $value) { $full_guarantor_name = $value . " " . get_name($key, "id", "last_name", "non_staff_guarantors"); $non_staff_guarantors[$key] = $full_guarantor_name; } return view('patient_finance::patient_finance.make_sundries_deposit',compact('ordered_sundries','patient_id','episode_id', 'patient_unpaid_items', 'patient_debts', 'patient','episode','categories','drug_categories','marital_statuses','discount','discount_options','payment_arrangement', 'invoiced_sundries', 'staff_guarantors', 'paid_sundries', 'price_list_set', 'price_list_categories', 'patient_payment_methods', 'patient_payment_methods_options', 'non_staff_guarantors')); } public function store_sundries_deposit(Request $request){ $wallets_amount = 0; if ($request->has('patient_account_pay_with')) { if ($request->patient_account_pay_with > $request->patient_account_balance) { // amount is more than is on the patient account flash('Patient account balance is less than the current bill.')->error(); return redirect()->back()->withInput(); } $wallets_amount += $request->patient_account_pay_with; } if ($request->has('family_account_pay_with')) { $this_patient_to_pay = $request->family_account_pay_with; $this_family_account_balance = $request->family_account_balance; $family_account_credit_limit = $request->family_account_credit_limit; $wallets_amount += $request->family_account_pay_with; if (!is_family_consumption_allowed($this_patient_to_pay, $this_family_account_balance, $family_account_credit_limit)) { return redirect()->back()->withInput(); } } //check if patient is a dependant of another patient e.g main patient with a discount. If so, let Neo enter the matrix if ($request->has('dependants_balance')) { $original_dependants_balance = $request->dependants_balance; $original_patient_category_to_pay = $request->patient_category_to_pay; //if new dependants balance is -ve, make it zero $new_dependants_balance = $request->dependants_balance < 0 ? 0 : $request->dependants_balance; $request->merge(['dependants_balance' => $new_dependants_balance]); //recalculate the patient category to pay if ($request->patient_category_to_pay > $original_dependants_balance) { if ($original_dependants_balance < 0) { $patient_category_to_pay = $request->patient_category_to_pay; } else{ $patient_category_to_pay = $request->patient_category_to_pay - $original_dependants_balance; } } else { $patient_category_to_pay = 0; } $request->merge(['patient_category_to_pay' => $patient_category_to_pay]); //make the main patient's patient category override the category of the dependant $patient_is_a_dependant_of = patient_is_a_dependant_of($request->patient_id); $main_patient_patient_category = get_name($patient_is_a_dependant_of, "id", "category_id", "patients"); $request->merge(['patient_category_id' => $main_patient_patient_category]); } $request->merge(['wallets_amount' => $wallets_amount]); if ($request->patient_unpaid_debts > 0) { $this->debtorsService->pay_for_patient_debt($request->episode_id, $request->patient_unpaid_debts); } $patient_unpaid_debts = $request->patient_unpaid_debts; $sundries_deposit = new SundriesDeposit; // begin transaction DB::beginTransaction(); try { // send data to be saved in treatment deposits table and get the is_invoice flag and receipt number $return_data_array = explode(",", $sundries_deposit->store_payment($request)); // all good so we can commit DB::commit(); } catch (\Exception | \Error $exception) { DB::rollback(); DB::table('system_errors')->insert([ 'error_message' => $exception->getMessage(), 'error_trace' => $exception->getTraceAsString(), 'logged_in_user' => Auth::id() ]); flash("An error occurred. Please try again or contact Stre@mline Support")->error(); return back()->withInput(); } // is_invoice flag $is_invoice = $return_data_array[0]; // receipt number $receipt_number = $return_data_array[1]; if ($request->has('patient_account_pay_with')) { record_patient_accounts_consumption($request->patient_id, $request->episode_id, $request->patient_account_pay_with, $request->tag_id, $receipt_number); } if ($request->has('family_account_pay_with')) { record_family_consumption($request->patient_id, $request->episode_id, $request->family_account_id, $request->family_account_pay_with, 'Sundries', $receipt_number); } // for the staff guarantor, if debt plan id is not 0, add receipt number to it if ($request->debt_plan_id != 0) { DB::table('debt_plan')->where('id', $request->debt_plan_id) ->update(['receipt_number' => $receipt_number]); } // details to be used for receipt $hospital_information = HospitalInformation::first(); $total_amount_pay = $request->total_amount_pay; $staff_guarantor_to_pay = $request->staff_guarantor_to_pay; $donor_to_pay = $request->donor_to_pay; $patient_to_pay = $request->patient_to_pay; $hospital_to_pay_selected_discount = $request->hospital_to_pay_selected_discount; $hospital_to_pay_general_discount = $request->hospital_to_pay_general_discount; $balance = $request->balance; $patient_category_to_pay = $request->patient_category_to_pay; $patient_category_id_to_pay = $request->patient_category_id; $one_off_discount = $request->one_off_discount; $one_off_discount_memo = $request->one_off_discount_memo; $patient_account_pay_with = $request->patient_account_pay_with ?? 0; $family_account_pay_with = $request->family_account_pay_with ?? 0; $amount_paid_from_wallets = $patient_account_pay_with + $family_account_pay_with; // check if hospital discounts are positive to add them to total if($hospital_to_pay_selected_discount > 0){ $hospital_to_pay = $hospital_to_pay_general_discount + $hospital_to_pay_selected_discount; } else { $hospital_to_pay = $hospital_to_pay_general_discount; } $sundry_item = explode(",", $request->sundry_ids); $sundry_quantity = $request->quantity; $sundry_subtotal = $request->sundry_subtotal; $patient = Patient::find($request->patient_id); $receipt_date = date('Y-m-d H:i:s'); /*====== if the patient is a dependant of a certain patient category patient, do this =====*/ $patient_category_id = get_name($request->patient_id, "id", "category_id", "patients"); $does_patient_category_have_threshold = does_patient_category_have_threshold($patient_category_id); $patient_is_a_dependant_of = patient_is_a_dependant_of($request->patient_id); if ($does_patient_category_have_threshold || !is_null($patient_is_a_dependant_of)) { $tag_id = 5; $amount_consumed = $patient_to_pay + $original_patient_category_to_pay; $this->record_dependant_consumption($request, $patient_is_a_dependant_of, $sundry_item, $sundry_subtotal, $amount_consumed, $tag_id, "Dependant-Sundries", $receipt_number); } /*===============end of that dependant functionality =======================*/ $return_payment_methods = $this->register_payment_method($request->patient_id, $request->episode_id, $request->original_cash_to_pay, $request->payment_method, $request->payment_methods_amount, $request->ref_number, $request->ref_number_added_by, $receipt_number, 5, Auth::id(), $amount_paid_from_wallets); $cashier = Auth::id(); $compact_values = compact('hospital_information', 'receipt_date', 'receipt_number', 'one_off_discount_memo', 'one_off_discount', 'patient', 'is_invoice', 'sundry_item', 'sundry_quantity', 'sundry_subtotal', 'patient_category_id_to_pay', 'patient_account_pay_with', 'total_amount_pay', 'staff_guarantor_to_pay', 'patient_unpaid_debts','family_account_pay_with', 'cashier', 'donor_to_pay', 'balance', 'hospital_to_pay', 'patient_to_pay', 'patient_category_to_pay', 'return_payment_methods'); $this->save_receipt_json($receipt_number, json_encode($compact_values)); if (is_cashier_receipt_type_print_html()) { return redirect('/patient_finance/print_sundries_receipt/' . 1 . '/' . $receipt_number); } else { // so we first have to set a session and then go back to the payment page // on the payment page we can then set a JS variable that can help redirect us to our pdf print session()->put('print_cashier_receipt_pdf', 1); // since we want to reduce the amount of duplicate code, we shall send all items to one file for printing $data = [ "hospital_information" => $compact_values["hospital_information"], "receipt_date" => $compact_values["receipt_date"], "receipt_number" => $compact_values["receipt_number"], "one_off_discount_memo" => $compact_values["one_off_discount_memo"], "one_off_discount" => $compact_values["one_off_discount"], "patient" => $compact_values["patient"], "is_invoice" => $compact_values["is_invoice"], "items" => $compact_values["sundry_item"], "item_quantity" => $compact_values["sundry_quantity"], "item_subtotal" => $compact_values["sundry_subtotal"], "patient_category_id_to_pay" => $compact_values["patient_category_id_to_pay"], "total_amount_pay" => $compact_values["total_amount_pay"], "staff_guarantor_to_pay" => $compact_values["staff_guarantor_to_pay"], "patient_unpaid_debts" => $compact_values["patient_unpaid_debts"], "donor_to_pay" => $compact_values["donor_to_pay"], "balance" => $compact_values["balance"], "hospital_to_pay" => $compact_values["hospital_to_pay"], "patient_to_pay" => $compact_values["patient_to_pay"], "patient_category_to_pay" => $compact_values["patient_category_to_pay"], "return_payment_methods" => $compact_values["return_payment_methods"], "treatment_balance" => 0, "item_table" => "sundries", "ward_discount_amount" => 0, "discounts_applied" => 0, "patient_account_pay_with" => $compact_values["patient_account_pay_with"], "family_account_pay_with" => $compact_values["family_account_pay_with"] ]; session()->put('print_cashier_receipt_pdf_details', $data); return redirect('/patient_finance/make_deposit_sundries'); } } public function print_sundries_receipt($type, $receipt_number){ // check if track_receipts has details $receipt_json = get_name((int)$receipt_number, 'id', 'receipt_json', 'track_receipts'); if ($receipt_json) { $compact_values = json_decode($receipt_json, true); $compact_values["patient"] = Patient::find($compact_values["patient"]["id"]); $compact_values["hospital_information"] = HospitalInformation::first(); $compact_values["discounts_applied"] = 0; $compact_values["sundry_ids_array"] = $compact_values["sundry_item"]; $compact_values["sundry_quantity_array"] = $compact_values["sundry_quantity"]; $compact_values["sundry_prices_array"] = $compact_values["sundry_subtotal"]; $compact_values["cashier"] = $compact_values["cashier"] ?? Auth::id(); } else { if ($type == 1) { // get payment details for the sundry $sundry_payment = SundryDeposit::where(['receipt_number' => $receipt_number])->first(); $patient_to_pay = $sundry_payment->patient_amount_paid; $sundry_prices_array = explode(",", $sundry_payment->sundry_subtotals); $sundry_ids_array = explode(",", $sundry_payment->sundry_items); $sundry_quantity_array = explode(",", $sundry_payment->sundry_quantity); } else { // get payment details for the sundry $sundry_payment = PatientCategoryInvoice::where(['receipt_number' => $receipt_number])->first(); $patient_to_pay = 0; $sundry_prices_array = explode(",", $sundry_payment->items_amounts); $sundry_ids_array = explode(",", $sundry_payment->items_ids); $sundry_quantity_array = explode(",", $sundry_payment->items_quantity); } $total_amount_pay = 0; foreach ($sundry_prices_array as $price){ $total_amount_pay += $price; } $receipt_date = $sundry_payment->created_at; $cashier = $sundry_payment->created_by; // receipt number $receipt_number = $sundry_payment->receipt_number; // details to be used for receipt $hospital_information = HospitalInformation::first(); $discounts_applied = $total_amount_pay - $patient_to_pay; $patient = Patient::find($sundry_payment->patient_id); $patient_account_pay_with = 0; $family_account_pay_with = 0; $compact_values = compact('hospital_information', 'receipt_date', 'receipt_number', 'discounts_applied', 'type', 'patient', 'sundry_prices_array', 'sundry_ids_array', 'sundry_quantity_array','total_amount_pay','patient_to_pay', 'cashier', 'patient_account_pay_with', 'family_account_pay_with'); } if (is_cashier_receipt_type_print_html()) { if ($receipt_json) { return view('patient_finance::patient_finance.receipts.sundries', $compact_values); } else { return view('patient_finance::patient_finance.receipts.sundries_reprint', $compact_values); } } else { // so we first have to set a session and then go back to the payment page // on the payment page we can then set a JS variable that can help redirect us to our pdf print session()->put('print_cashier_receipt_pdf', 1); // since we want to reduce the amount of duplicate code, we shall send all items to one file for printing $data = [ "hospital_information" => $compact_values["hospital_information"], "receipt_date" => $compact_values["receipt_date"], "receipt_number" => $compact_values["receipt_number"], "one_off_discount_memo" => "", "one_off_discount" => "", "patient" => $compact_values["patient"], "is_invoice" => "", "items" => $compact_values["sundry_ids_array"], "item_quantity" => $compact_values["sundry_quantity_array"], "item_subtotal" => $compact_values["sundry_prices_array"], "patient_category_id_to_pay" => 0, "total_amount_pay" => $compact_values["total_amount_pay"], "staff_guarantor_to_pay" => "", "patient_unpaid_debts" => 0, "donor_to_pay" => 0, "balance" => 0, "hospital_to_pay" => "", "patient_to_pay" => $compact_values["patient_to_pay"], "patient_category_to_pay" => 0, "return_payment_methods" => [], "treatment_balance" => 0, "item_table" => "sundries", "ward_discount_amount" => 0, "discounts_applied" => $compact_values["discounts_applied"], "patient_account_pay_with" => $compact_values["patient_account_pay_with"], "family_account_pay_with" => $compact_values["family_account_pay_with"] ]; session()->put('print_cashier_receipt_pdf_details', $data); return redirect('/patient_finance/make_deposit_sundries'); } } public function make_deposit_optical_items(Request $request){ if (isset($request->price_list_category_id) && $request->price_list_category_id != 0){ $price_list_set = $request->price_list_category_id; } else { $price_list_set = false; } $price_list_categories = PriceListCategories::select('patient_category_id', 'id')->get(); $patient_id = session()->get('patient_id'); $episode_id = session()->get('episode_id'); $patient = Patient::find($patient_id); $episode = PatientEpisode::find($episode_id); $categories = DB::table('patient_categories')->pluck('name', 'id'); $drug_categories = DB::table('drug_categories')->get(); $marital_statuses = DB::table('marital_statuses')->pluck('name', 'id'); $discount = PatientDiscount::where(['patient_category'=>$patient->category_id])->first(); $discount_options = DiscountCategories::where(['patient_category' => $patient->category_id])->pluck('name','id')->toArray(); $discount_options = ['' => '- Select Discount -'] + $discount_options; $payment_arrangement = DB::table('debt_plan_arrangements')->pluck('name', 'id')->toArray(); $payment_arrangement = ['' => '- select -'] + $payment_arrangement; $staff_guarantors = DB::table('users')->pluck('first_name', 'id')->toArray(); foreach ($staff_guarantors as $key => $value) { $full_name = $value . " " . get_name($key, "id", "last_name", "users"); $staff_guarantors[$key] = $full_name; } $ordered_opticals = OrderedEyeGlasses::where(['episode_id' => $episode_id,'payment_status' => '0'])->get(); // check if there are any paid opticals to display receipt $paid_opticals = EyeGlassesDeposits::where(['episode_id' => $episode_id])->get(); $invoiced_opticals = PatientCategoryInvoice::where('episode_id', $episode_id)->where('tag_id', 12)->get(); $patient_payment_methods = PatientPaymentMethod::pluck('name', 'id'); $patient_payment_methods_options = ""; foreach ($patient_payment_methods as $key => $value) { $patient_payment_methods_options .= ''; } $patient_unpaid_items_array = explode(",", $this->does_patient_have_unpaid_items($patient_id, $episode_id)); $patient_unpaid_items = $patient_unpaid_items_array[0]; $patient_debts = $patient_unpaid_items_array[1]; $non_staff_guarantors = DB::table('non_staff_guarantors')->pluck('first_name', 'id')->toArray(); foreach ($non_staff_guarantors as $key => $value) { $full_guarantor_name = $value . " " . get_name($key, "id", "last_name", "non_staff_guarantors"); $non_staff_guarantors[$key] = $full_guarantor_name; } return view('patient_finance::patient_finance.make_deposit_optical_items',compact('ordered_opticals','patient_id','episode_id', 'patient_unpaid_items', 'patient_debts', 'patient','episode','categories','drug_categories','marital_statuses','discount','discount_options','payment_arrangement', 'invoiced_opticals', 'staff_guarantors', 'paid_opticals', 'price_list_set', 'price_list_categories', 'patient_payment_methods', 'patient_payment_methods_options', 'non_staff_guarantors')); } public function store_optical_items_deposit(Request $request){ $wallets_amount = $original_patient_category_to_pay = 0; if ($request->has('patient_account_pay_with')) { if ($request->patient_account_pay_with > $request->patient_account_balance) { // amount is more than is on the patient account flash('Patient account balance is less than the current bill.')->error(); return redirect()->back()->withInput(); } $wallets_amount += $request->patient_account_pay_with; } if ($request->has('family_account_pay_with')) { $this_patient_to_pay = $request->family_account_pay_with; $this_family_account_balance = $request->family_account_balance; $family_account_credit_limit = $request->family_account_credit_limit; $wallets_amount += $request->family_account_pay_with; if (!is_family_consumption_allowed($this_patient_to_pay, $this_family_account_balance, $family_account_credit_limit)) { return redirect()->back()->withInput(); } } $request->merge(['wallets_amount' => $wallets_amount]); if ($request->patient_unpaid_debts > 0) { $this->debtorsService->pay_for_patient_debt($request->episode_id, $request->patient_unpaid_debts); } $patient_unpaid_debts = $request->patient_unpaid_debts; $optical_deposit = new EyeGlassesDeposit(); // send data to be saved in treatment deposits table and get the is_invoice flag and receipt number $return_data_array = explode(",", $optical_deposit->store_payment($request)); // is_invoice flag $is_invoice = $return_data_array[0]; // receipt number $receipt_number = $return_data_array[1]; if ($request->has('patient_account_pay_with')) { record_patient_accounts_consumption($request->patient_id, $request->episode_id, $request->patient_account_pay_with, $request->tag_id, $receipt_number); } if ($request->has('family_account_pay_with')) { record_family_consumption($request->patient_id, $request->episode_id, $request->family_account_id, $request->family_account_pay_with, 'Optical Items', $receipt_number); } //check if patient is a dependant of another patient e.g staff with a discount. If so, let Neo enter the matrix if ($request->has('dependants_balance')) { $original_dependants_balance = $request->dependants_balance; $original_patient_category_to_pay = $request->patient_category_to_pay; //if new dependants balance is -ve, make it zero $new_dependants_balance = $request->dependants_balance < 0 ? 0 : $request->dependants_balance; $request->merge(['dependants_balance' => $new_dependants_balance]); if($request->patient_category_to_pay > $request->dependants_balance){ $patient_category_to_pay = $request->patient_category_to_pay - $request->dependants_balance; } else{ $patient_category_to_pay = 0; } $request->merge(['patient_category_to_pay' => $patient_category_to_pay]); } // for the staff guarantor, if debt plan id is not 0, add receipt number to it if ($request->debt_plan_id != 0) { DB::table('debt_plan')->where('id', $request->debt_plan_id) ->update(['receipt_number' => $receipt_number]); } // details to be used for receipt $hospital_information = HospitalInformation::first(); $total_amount_pay = $request->total_amount_pay; $staff_guarantor_to_pay = $request->staff_guarantor_to_pay; $donor_to_pay = $request->donor_to_pay; $patient_to_pay = $request->patient_to_pay; $hospital_to_pay_selected_discount = $request->hospital_to_pay_selected_discount; $hospital_to_pay_general_discount = $request->hospital_to_pay_general_discount; $balance = $request->balance; $patient_category_to_pay = $request->patient_category_to_pay; $patient_category_id_to_pay = $request->patient_category_id; $one_off_discount = $request->one_off_discount; $one_off_discount_memo = $request->one_off_discount_memo; $patient_account_pay_with = $request->patient_account_pay_with ?? 0; $family_account_pay_with = $request->family_account_pay_with ?? 0; $amount_paid_from_wallets = $patient_account_pay_with + $family_account_pay_with; // check if hospital discounts are positive to add them to total if($hospital_to_pay_selected_discount > 0){ $hospital_to_pay = $hospital_to_pay_general_discount + $hospital_to_pay_selected_discount; } else { $hospital_to_pay = $hospital_to_pay_general_discount; } $optical_item = explode(",", $request->optical_ids); $optical_quantity = $request->quantity; $optical_subtotal = $request->optical_subtotal; $patient = Patient::find($request->patient_id); $receipt_date = date('Y-m-d H:i:s'); /*====== if the patient is a dependant of a certain patient category patient, do this =====*/ $patient_category_id = get_name($request->patient_id, "id", "category_id", "patients"); $does_patient_category_have_threshold = does_patient_category_have_threshold($patient_category_id); $patient_is_a_dependant_of = patient_is_a_dependant_of($request->patient_id); if ($does_patient_category_have_threshold || !is_null($patient_is_a_dependant_of)) { $amount_consumed = $patient_to_pay + $original_patient_category_to_pay; $this->record_dependant_consumption($request, $patient_is_a_dependant_of, $optical_item, $optical_subtotal, $amount_consumed, 19, "Optical Items", $receipt_number); } /*===============end of that dependant functionality =======================*/ $return_payment_methods = $this->register_payment_method($request->patient_id, $request->episode_id, $request->original_cash_to_pay, $request->payment_method, $request->payment_methods_amount, $request->ref_number, $request->ref_number_added_by, $receipt_number, 19, Auth::id(), $amount_paid_from_wallets); $compact_values = compact('hospital_information', 'receipt_date', 'receipt_number', 'one_off_discount_memo', 'one_off_discount', 'patient', 'is_invoice', 'optical_item', 'optical_quantity', 'optical_subtotal', 'patient_category_id_to_pay', 'patient_account_pay_with', 'total_amount_pay', 'staff_guarantor_to_pay', 'patient_unpaid_debts','family_account_pay_with', 'donor_to_pay', 'balance', 'hospital_to_pay', 'patient_to_pay', 'patient_category_to_pay', 'return_payment_methods'); $this->save_receipt_json($receipt_number, json_encode($compact_values)); if (is_cashier_receipt_type_print_html()) { return redirect('/patient_finance/print_optical_items_receipt/' . 1 . '/' . $receipt_number); } else { // so we first have to set a session and then go back to the payment page // on the payment page we can then set a JS variable that can help redirect us to our pdf print session()->put('print_cashier_receipt_pdf', 1); // since we want to reduce the amount of duplicate code, we shall send all items to one file for printing $data = [ "hospital_information" => $compact_values["hospital_information"], "receipt_date" => $compact_values["receipt_date"], "receipt_number" => $compact_values["receipt_number"], "one_off_discount_memo" => $compact_values["one_off_discount_memo"], "one_off_discount" => $compact_values["one_off_discount"], "patient" => $compact_values["patient"], "is_invoice" => $compact_values["is_invoice"], "items" => $compact_values["optical_item"], "item_quantity" => $compact_values["optical_quantity"], "item_subtotal" => $compact_values["optical_subtotal"], "patient_category_id_to_pay" => $compact_values["patient_category_id_to_pay"], "total_amount_pay" => $compact_values["total_amount_pay"], "staff_guarantor_to_pay" => $compact_values["staff_guarantor_to_pay"], "patient_unpaid_debts" => $compact_values["patient_unpaid_debts"], "donor_to_pay" => $compact_values["donor_to_pay"], "balance" => $compact_values["balance"], "hospital_to_pay" => $compact_values["hospital_to_pay"], "patient_to_pay" => $compact_values["patient_to_pay"], "patient_category_to_pay" => $compact_values["patient_category_to_pay"], "return_payment_methods" => $compact_values["return_payment_methods"], "treatment_balance" => 0, "item_table" => "eye_glasses", "ward_discount_amount" => 0, "discounts_applied" => 0, "patient_account_pay_with" => $compact_values["patient_account_pay_with"], "family_account_pay_with" => $compact_values["family_account_pay_with"] ]; session()->put('print_cashier_receipt_pdf_details', $data); return redirect('/patient_finance/make_deposit_optical_items'); } } public function print_optical_items_receipt($type, $receipt_number){ // check if track_receipts has details $receipt_json = get_name((int)$receipt_number, 'id', 'receipt_json', 'track_receipts'); if ($receipt_json) { $compact_values = json_decode($receipt_json, true); $compact_values["patient"] = Patient::find($compact_values["patient"]["id"]); $compact_values["hospital_information"] = HospitalInformation::first(); $compact_values["discounts_applied"] = 0; $compact_values["optical_ids_array"] = $compact_values["optical_item"]; $compact_values["optical_quantity_array"] = $compact_values["optical_quantity"]; $compact_values["optical_prices_array"] = $compact_values["optical_subtotal"]; $compact_values["cashier"] = Auth::id(); } else { if ($type == 1) { // get payment details for the optical $optical_payment = EyeGlassesDeposits::where(['receipt_number' => $receipt_number])->first(); $patient_to_pay = $optical_payment->patient_amount_paid; $optical_prices_array = explode(",", $optical_payment->subtotals); $optical_ids_array = explode(",", $optical_payment->items); $optical_quantity_array = explode(",", $optical_payment->quantity); } else { // get payment details for the optical $optical_payment = PatientCategoryInvoice::where(['receipt_number' => $receipt_number])->first(); $patient_to_pay = 0; $optical_prices_array = explode(",", $optical_payment->items_amounts); $optical_ids_array = explode(",", $optical_payment->items_ids); $optical_quantity_array = explode(",", $optical_payment->items_quantity); } $total_amount_pay = 0; foreach ($optical_prices_array as $price){ $total_amount_pay += $price; } $receipt_date = $optical_payment->created_at; $cashier = $optical_payment->created_by; // receipt number $receipt_number = $optical_payment->receipt_number; // details to be used for receipt $hospital_information = HospitalInformation::first(); $discounts_applied = $total_amount_pay - $patient_to_pay; $patient = Patient::find($optical_payment->patient_id); $patient_account_pay_with = 0; $family_account_pay_with = 0; $compact_values = compact('hospital_information', 'receipt_date', 'receipt_number', 'discounts_applied', 'type', 'patient', 'optical_prices_array', 'optical_ids_array', 'optical_quantity_array','total_amount_pay','patient_to_pay', 'cashier', 'patient_account_pay_with', 'family_account_pay_with'); } if (is_cashier_receipt_type_print_html()) { if ($receipt_json) { return view('patient_finance::patient_finance.receipts.optical_items', $compact_values); } else { return view('patient_finance::patient_finance.receipts.optical_items_reprint', $compact_values); } } else { // so we first have to set a session and then go back to the payment page // on the payment page we can then set a JS variable that can help redirect us to our pdf print session()->put('print_cashier_receipt_pdf', 1); // since we want to reduce the amount of duplicate code, we shall send all items to one file for printing $data = [ "hospital_information" => $compact_values["hospital_information"], "receipt_date" => $compact_values["receipt_date"], "receipt_number" => $compact_values["receipt_number"], "one_off_discount_memo" => "", "one_off_discount" => "", "patient" => $compact_values["patient"], "is_invoice" => "", "items" => $compact_values["optical_ids_array"], "item_quantity" => $compact_values["optical_quantity_array"], "item_subtotal" => $compact_values["optical_prices_array"], "patient_category_id_to_pay" => 0, "total_amount_pay" => $compact_values["total_amount_pay"], "staff_guarantor_to_pay" => "", "patient_unpaid_debts" => 0, "donor_to_pay" => 0, "balance" => 0, "hospital_to_pay" => "", "patient_to_pay" => $compact_values["patient_to_pay"], "patient_category_to_pay" => 0, "return_payment_methods" => [], "treatment_balance" => 0, "item_table" => "eye_glasses", "ward_discount_amount" => 0, "discounts_applied" => $compact_values["discounts_applied"], "patient_account_pay_with" => $compact_values["patient_account_pay_with"], "family_account_pay_with" => $compact_values["family_account_pay_with"] ]; session()->put('print_cashier_receipt_pdf_details', $data); return redirect('/patient_finance/make_deposit_optical_items'); } } /** * Make patient procedure deposits * * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View */ public function make_deposit_procedures(Request $request){ if (isset($request->price_list_category_id) && $request->price_list_category_id != 0){ $price_list_set = $request->price_list_category_id; } else { $price_list_set = false; } $price_list_categories = PriceListCategories::select('patient_category_id', 'id')->get(); $patient_id = session()->get('patient_id'); $episode_id = session()->get('episode_id'); $patient = Patient::find($patient_id); $episode = PatientEpisode::find($episode_id); $categories = DB::table('patient_categories')->where('available', 1)->pluck('name', 'id'); $drug_categories = DB::table('drug_categories')->get(); $marital_statuses = DB::table('marital_statuses')->pluck('name', 'id'); $discount = PatientDiscount::where(['patient_category'=>$patient->category_id])->first(); $discount_options = DiscountCategories::where(['patient_category' => $patient->category_id])->pluck('name','id')->toArray(); $discount_options = ['' => '- Select Discount -'] + $discount_options; $payment_arrangement = DB::table('debt_plan_arrangements')->pluck('name', 'id')->toArray(); $payment_arrangement = ['' => '- select -'] + $payment_arrangement; $staff_guarantors = DB::table('users')->pluck('first_name', 'id')->toArray(); foreach ($staff_guarantors as $key => $value) { $full_name = $value . " " . get_name($key, "id", "last_name", "users"); $staff_guarantors[$key] = $full_name; } $ordered_procedures = OrderedProcedure::where(['episode_id' => $episode_id,'payment_status' => '0' ,'inpatient_bill_generated' => 0])->get(); // check if there are any paid procedure to display receipt $paid_procedures = ProcedureDeposit::where(['episode_id' => $episode_id])->orderBy('created_at', 'desc')->get(); $invoiced_procedures = PatientCategoryInvoice::where('episode_id', $episode_id)->where('tag_id', 4)->get(); $patient_payment_methods = PatientPaymentMethod::pluck('name', 'id'); $patient_payment_methods_options = ""; foreach ($patient_payment_methods as $key => $value) { $patient_payment_methods_options .= ''; } $patient_unpaid_items_array = explode(",", $this->does_patient_have_unpaid_items($patient_id, $episode_id)); $patient_unpaid_items = $patient_unpaid_items_array[0]; $patient_debts = $patient_unpaid_items_array[1]; $non_staff_guarantors = DB::table('non_staff_guarantors')->pluck('first_name', 'id')->toArray(); foreach ($non_staff_guarantors as $key => $value) { $full_guarantor_name = $value . " " . get_name($key, "id", "last_name", "non_staff_guarantors"); $non_staff_guarantors[$key] = $full_guarantor_name; } return view('patient_finance::patient_finance.make_deposit_procedures',compact('patient_id','episode_id','patient','episode','categories','drug_categories','marital_statuses','discount','discount_options','payment_arrangement', 'staff_guarantors', 'ordered_procedures', 'paid_procedures', 'patient_unpaid_items', 'patient_debts', 'price_list_set', 'price_list_categories', 'patient_payment_methods', 'patient_payment_methods_options', 'invoiced_procedures', 'non_staff_guarantors')); } public function store_deposit_procedures(Request $request) { $wallets_amount = 0; if ($request->has('patient_account_pay_with')) { if ($request->patient_account_pay_with > $request->patient_account_balance) { // amount is more than is on the patient account flash('Patient account balance is less than the current bill.')->error(); return redirect()->back()->withInput(); } $wallets_amount += $request->patient_account_pay_with; } if ($request->has('family_account_pay_with')) { $this_patient_to_pay = $request->family_account_pay_with; $this_family_account_balance = $request->family_account_balance; $family_account_credit_limit = $request->family_account_credit_limit; $wallets_amount += $request->family_account_pay_with; if (!is_family_consumption_allowed($this_patient_to_pay, $this_family_account_balance, $family_account_credit_limit)) { return redirect()->back()->withInput(); } } $request->merge(['wallets_amount' => $wallets_amount]); //check if patient is a dependant of another patient e.g main patient with a discount. If so, let Neo enter the matrix if ($request->has('dependants_balance')) { $original_dependants_balance = $request->dependants_balance; $original_patient_category_to_pay = $request->patient_category_to_pay; //if new dependants balance is -ve, make it zero $new_dependants_balance = $request->dependants_balance < 0 ? 0 : $request->dependants_balance; $request->merge(['dependants_balance' => $new_dependants_balance]); //recalculate the patient category to pay if ($request->patient_category_to_pay > $original_dependants_balance) { if ($original_dependants_balance < 0) { $patient_category_to_pay = $request->patient_category_to_pay; } else{ $patient_category_to_pay = $request->patient_category_to_pay - $original_dependants_balance; } } else { $patient_category_to_pay = 0; } $request->merge(['patient_category_to_pay' => $patient_category_to_pay]); //make the main patient's patient category override the category of the dependant $patient_is_a_dependant_of = patient_is_a_dependant_of($request->patient_id); $main_patient_patient_category = get_name($patient_is_a_dependant_of, "id", "category_id", "patients"); $request->merge(['patient_category_id' => $main_patient_patient_category]); } if ($request->patient_unpaid_debts > 0) { $this->debtorsService->pay_for_patient_debt($request->episode_id, $request->patient_unpaid_debts); } $patient_unpaid_debts = $request->patient_unpaid_debts; $procedures_deposit = new ProceduresDeposit; // begin transaction DB::beginTransaction(); try { // send data to be saved in services table and get the is_invoice flag and receipt number $return_data_array = explode(",", $procedures_deposit->store_payment($request)); // all good so we can commit DB::commit(); } catch (\Exception | \Error $exception) { DB::rollback(); DB::table('system_errors')->insert([ 'error_message' => $exception->getMessage(), 'error_trace' => $exception->getTraceAsString(), 'logged_in_user' => Auth::id() ]); flash("An error occurred. Please try again or contact Stre@mline Support")->error(); return back()->withInput(); } // is_invoice flag $is_invoice = $return_data_array[0]; // receipt number $receipt_number = $return_data_array[1]; if ($request->has('patient_account_pay_with')) { record_patient_accounts_consumption($request->patient_id, $request->episode_id, $request->patient_account_pay_with, $request->tag_id, $receipt_number); } if ($request->has('family_account_pay_with')) { record_family_consumption($request->patient_id, $request->episode_id, $request->family_account_id, $request->family_account_pay_with, 'Procedures', $receipt_number); } // for the staff guarantor, if debt plan id is not 0, add receipt number to it if ($request->debt_plan_id != 0) { DB::table('debt_plan')->where('id', $request->debt_plan_id) ->update(['receipt_number' => $receipt_number]); } // details to be used for receipt $hospital_information = HospitalInformation::first(); $total_amount_pay = $request->total_amount_pay; $staff_guarantor_to_pay = $request->staff_guarantor_to_pay; $donor_to_pay = $request->donor_to_pay; $patient_to_pay = $request->patient_to_pay; $hospital_to_pay_selected_discount = $request->hospital_to_pay_selected_discount; $hospital_to_pay_general_discount = $request->hospital_to_pay_general_discount; $balance = $request->balance; $patient_category_to_pay = $request->patient_category_to_pay; $patient_category_id_to_pay = $request->patient_category_id; $one_off_discount = $request->one_off_discount; $one_off_discount_memo = $request->one_off_discount_memo; $patient_account_pay_with = $request->patient_account_pay_with ?? 0; $family_account_pay_with = $request->family_account_pay_with ?? 0; $amount_paid_from_wallets = $patient_account_pay_with + $family_account_pay_with; // check if hospital discounts are positive to add them to total if($hospital_to_pay_selected_discount > 0){ $hospital_to_pay = $hospital_to_pay_general_discount + $hospital_to_pay_selected_discount; } else { $hospital_to_pay = $hospital_to_pay_general_discount; } $procedure_ids_array = explode(",", $request->procedure_ids); $procedure_amounts_array = $request->procedure_amount; $patient = Patient::find($request->patient_id); $receipt_date = date('Y-m-d H:i:s'); /*====== if the patient is a dependant of a certain patient category patient, do this =====*/ $patient_category_id = get_name($request->patient_id, "id", "category_id", "patients"); $does_patient_category_have_threshold = does_patient_category_have_threshold($patient_category_id); $patient_is_a_dependant_of = patient_is_a_dependant_of($request->patient_id); if ($does_patient_category_have_threshold || !is_null($patient_is_a_dependant_of)) { $tag_id = 4; $amount_consumed = $patient_to_pay + $original_patient_category_to_pay; $this->record_dependant_consumption($request, $patient_is_a_dependant_of, $procedure_ids_array, $procedure_amounts_array, $amount_consumed, $tag_id, "Dependant-Procedures", $receipt_number); } /*===============end of that dependant functionality =======================*/ $return_payment_methods = $this->register_payment_method($request->patient_id, $request->episode_id, $request->original_cash_to_pay, $request->payment_method, $request->payment_methods_amount, $request->ref_number, $request->ref_number_added_by, $receipt_number, 4, Auth::id(), $amount_paid_from_wallets); $cashier = Auth::id(); $compact_values = compact('hospital_information', 'receipt_date', 'receipt_number', 'one_off_discount_memo', 'one_off_discount', 'patient', 'is_invoice', 'procedure_ids_array', 'procedure_amounts_array', 'patient_category_id_to_pay', 'patient_account_pay_with', 'total_amount_pay', 'staff_guarantor_to_pay', 'patient_unpaid_debts', 'family_account_pay_with', 'cashier', 'donor_to_pay', 'balance', 'hospital_to_pay', 'patient_to_pay', 'patient_category_to_pay', 'return_payment_methods'); $this->save_receipt_json($receipt_number, json_encode($compact_values)); flash("Procedure deposit saved successfully")->success(); if (is_cashier_receipt_type_print_html()) { return redirect('/patient_finance/print_procedures_receipt/' . 1 . '/' . $receipt_number); } else { // so we first have to set a session and then go back to the payment page // on the payment page we can then set a JS variable that can help redirect us to our pdf print session()->put('print_cashier_receipt_pdf', 1); // since we want to reduce the amount of duplicate code, we shall send all items to one file for printing $data = [ "hospital_information" => $compact_values["hospital_information"], "receipt_date" => $compact_values["receipt_date"], "receipt_number" => $compact_values["receipt_number"], "one_off_discount_memo" => $compact_values["one_off_discount_memo"], "one_off_discount" => $compact_values["one_off_discount"], "patient" => $compact_values["patient"], "is_invoice" => $compact_values["is_invoice"], "items" => $compact_values["procedure_ids_array"], "item_quantity" => array_fill(0, count($compact_values["procedure_ids_array"]), 1), "item_subtotal" => $compact_values["procedure_amounts_array"], "patient_category_id_to_pay" => $compact_values["patient_category_id_to_pay"], "total_amount_pay" => $compact_values["total_amount_pay"], "staff_guarantor_to_pay" => $compact_values["staff_guarantor_to_pay"], "patient_unpaid_debts" => $compact_values["patient_unpaid_debts"], "donor_to_pay" => $compact_values["donor_to_pay"], "balance" => $compact_values["balance"], "hospital_to_pay" => $compact_values["hospital_to_pay"], "patient_to_pay" => $compact_values["patient_to_pay"], "patient_category_to_pay" => $compact_values["patient_category_to_pay"], "return_payment_methods" => $compact_values["return_payment_methods"], "treatment_balance" => 0, "item_table" => "procedures", "ward_discount_amount" => 0, "discounts_applied" => 0, "patient_account_pay_with" => $compact_values["patient_account_pay_with"], "family_account_pay_with" => $compact_values["family_account_pay_with"] ]; session()->put('print_cashier_receipt_pdf_details', $data); return redirect('/patient_finance/make_deposit_procedures'); } } /** * Reprint Procedures Receipt * * @param $id * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View */ public function print_procedures_receipt($type, $receipt_number){ // check if track_receipts has details $receipt_json = get_name((int)$receipt_number, 'id', 'receipt_json', 'track_receipts'); if ($receipt_json) { $compact_values = json_decode($receipt_json, true); $compact_values["patient"] = Patient::find($compact_values["patient"]["id"]); $compact_values["hospital_information"] = HospitalInformation::first(); $compact_values["discounts_applied"] = 0; $compact_values["procedure_prices_array"] = $compact_values["procedure_amounts_array"]; $compact_values["cashier"] = $compact_values["cashier"] ?? Auth::id(); } else { if ($type == 1) { // get payment details for the services $procedure_payment = ProcedureDeposit::where(['receipt_number' => $receipt_number])->first(); $patient_to_pay = $procedure_payment->patient_amount_paid; $procedure_prices_array = explode(",", $procedure_payment->procedure_amounts); $procedure_ids_array = explode(",", $procedure_payment->procedure_items); } else { // get payment details for the services $procedure_payment = PatientCategoryInvoice::where(['receipt_number' => $receipt_number])->first(); $patient_to_pay = 0; $procedure_prices_array = explode(",", $procedure_payment->items_amounts); $procedure_ids_array = explode(",", $procedure_payment->items_ids); } $total_amount_pay = 0; foreach ($procedure_prices_array as $price){ $total_amount_pay += $price; } $receipt_date = $procedure_payment->created_at; $cashier = $procedure_payment->created_by; // receipt number $receipt_number = $procedure_payment->receipt_number; // details to be used for receipt $hospital_information = HospitalInformation::first(); $discounts_applied = $total_amount_pay - $patient_to_pay; $patient = Patient::find($procedure_payment->patient_id); $patient_account_pay_with = 0; $family_account_pay_with = 0; $compact_values = compact('hospital_information', 'receipt_date', 'receipt_number', 'discounts_applied', 'type', 'patient', 'procedure_prices_array', 'procedure_ids_array','total_amount_pay','patient_to_pay', 'cashier', 'patient_account_pay_with', 'family_account_pay_with'); } if (is_cashier_receipt_type_print_html()) { if ($receipt_json) { return view('patient_finance::patient_finance.receipts.procedures', $compact_values); } else { return view('patient_finance::patient_finance.receipts.procedures_reprint', $compact_values); } } else { // so we first have to set a session and then go back to the payment page // on the payment page we can then set a JS variable that can help redirect us to our pdf print session()->put('print_cashier_receipt_pdf', 1); // since we want to reduce the amount of duplicate code, we shall send all items to one file for printing $data = [ "hospital_information" => $compact_values["hospital_information"], "receipt_date" => $compact_values["receipt_date"], "receipt_number" => $compact_values["receipt_number"], "one_off_discount_memo" => "", "one_off_discount" => "", "patient" => $compact_values["patient"], "is_invoice" => "", "items" => $compact_values["procedure_ids_array"], "item_quantity" => array_fill(0, count($compact_values["procedure_ids_array"]), 1), "item_subtotal" => $compact_values["procedure_prices_array"], "patient_category_id_to_pay" => 0, "total_amount_pay" => $compact_values["total_amount_pay"], "staff_guarantor_to_pay" => "", "patient_unpaid_debts" => 0, "donor_to_pay" => 0, "balance" => 0, "hospital_to_pay" => "", "patient_to_pay" => $compact_values["patient_to_pay"], "patient_category_to_pay" => 0, "return_payment_methods" => [], "treatment_balance" => 0, "item_table" => "procedures", "ward_discount_amount" => 0, "discounts_applied" => $compact_values["discounts_applied"], "patient_account_pay_with" => $compact_values["patient_account_pay_with"], "family_account_pay_with" => $compact_values["family_account_pay_with"] ]; session()->put('print_cashier_receipt_pdf_details', $data); return redirect('/patient_finance/make_deposit_procedures'); } } /** * Make patient investigation deposits * * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View */ public function make_deposit_investigations(Request $request){ if (isset($request->price_list_category_id) && $request->price_list_category_id != 0){ $price_list_set = $request->price_list_category_id; } else { $price_list_set = false; } $price_list_categories = PriceListCategories::select('patient_category_id', 'id')->get(); $patient_id = session()->get('patient_id'); $episode_id = session()->get('episode_id'); $patient = Patient::find($patient_id); $episode = PatientEpisode::find($episode_id); $categories = DB::table('patient_categories')->where('available', 1)->pluck('name', 'id'); $drug_categories = DB::table('drug_categories')->get(); $marital_statuses = DB::table('marital_statuses')->pluck('name', 'id'); $discount = PatientDiscount::where(['patient_category'=>$patient->category_id])->first(); $discount_options = DiscountCategories::where(['patient_category' => $patient->category_id])->pluck('name','id')->toArray(); $discount_options = ['' => '- Select Discount -'] + $discount_options; $payment_arrangement = DB::table('debt_plan_arrangements')->pluck('name', 'id')->toArray(); $payment_arrangement = ['' => '- select -'] + $payment_arrangement; $staff_guarantors = DB::table('users')->pluck('first_name', 'id')->toArray(); foreach ($staff_guarantors as $key => $value) { $full_name = $value . " " . get_name($key, "id", "last_name", "users"); $staff_guarantors[$key] = $full_name; } $ordered_investigations = OrderedInvestigation::where(['episode_id' => $episode_id,'payment_status' => '0','inpatient_bill_generated' => 0])->get(); // check if there are any paid investigations to display receipt $paid_investigations = InvestigationDeposit::where(['episode_id' => $episode_id])->orderBy('created_at', 'desc')->get(); $invoiced_investigations = PatientCategoryInvoice::where('episode_id', $episode_id)->where('tag_id', 2)->get(); $patient_unpaid_items_array = explode(",", $this->does_patient_have_unpaid_items($patient_id, $episode_id)); $patient_unpaid_items = $patient_unpaid_items_array[0]; $patient_debts = $patient_unpaid_items_array[1]; $non_staff_guarantors = DB::table('non_staff_guarantors')->pluck('first_name', 'id')->toArray(); foreach ($non_staff_guarantors as $key => $value) { $full_guarantor_name = $value . " " . get_name($key, "id", "last_name", "non_staff_guarantors"); $non_staff_guarantors[$key] = $full_guarantor_name; } return view('patient_finance::patient_finance.make_deposit_investigations',compact('patient_id','episode_id','patient','episode','categories','drug_categories','marital_statuses','discount','discount_options','ordered_investigations','payment_arrangement','staff_guarantors', 'paid_investigations', 'price_list_set', 'price_list_categories', 'invoiced_investigations', 'patient_unpaid_items', 'patient_debts', 'non_staff_guarantors')); } public function store_deposit_investigations(Request $request){ $wallets_amount = 0; if ($request->has('patient_account_pay_with')) { if ($request->patient_account_pay_with > $request->patient_account_balance) { // amount is more than is on the patient account flash('Patient account balance is less than the current bill.')->error(); return redirect()->back()->withInput(); } $wallets_amount += $request->patient_account_pay_with; } if ($request->has('family_account_pay_with')) { $this_patient_to_pay = $request->family_account_pay_with; $this_family_account_balance = $request->family_account_balance; $family_account_credit_limit = $request->family_account_credit_limit; $wallets_amount += $request->family_account_pay_with; if (!is_family_consumption_allowed($this_patient_to_pay, $this_family_account_balance, $family_account_credit_limit)) { return redirect()->back()->withInput(); } } $request->merge(['wallets_amount' => $wallets_amount]); //check if patient is a dependant of another patient e.g main patient with a discount. If so, let Neo enter the matrix if ($request->has('dependants_balance')) { $original_dependants_balance = $request->dependants_balance; $original_patient_category_to_pay = $request->patient_category_to_pay; //if new dependants balance is -ve, make it zero $new_dependants_balance = $request->dependants_balance < 0 ? 0 : $request->dependants_balance; $request->merge(['dependants_balance' => $new_dependants_balance]); //recalculate the patient category to pay if ($request->patient_category_to_pay > $original_dependants_balance) { if ($original_dependants_balance < 0) { $patient_category_to_pay = $request->patient_category_to_pay; } else{ $patient_category_to_pay = $request->patient_category_to_pay - $original_dependants_balance; } } else { $patient_category_to_pay = 0; } $request->merge(['patient_category_to_pay' => $patient_category_to_pay]); //make the main patient's patient category override the category of the dependant $patient_is_a_dependant_of = patient_is_a_dependant_of($request->patient_id); $main_patient_patient_category = get_name($patient_is_a_dependant_of, "id", "category_id", "patients"); $request->merge(['patient_category_id' => $main_patient_patient_category]); } if ($request->patient_unpaid_debts > 0) { $this->debtorsService->pay_for_patient_debt($request->episode_id, $request->patient_unpaid_debts); } $patient_unpaid_debts = $request->patient_unpaid_debts; $investigation_deposit = new InvestigationsDeposit; // begin transaction DB::beginTransaction(); try { // send data to be saved in services table and get the is_invoice flag and receipt number $return_data_array = explode(",", $investigation_deposit->store_payment($request)); // all good so we can commit DB::commit(); } catch (\Exception | \Error $exception) { DB::rollback(); DB::table('system_errors')->insert([ 'error_message' => $exception->getMessage(), 'error_trace' => $exception->getTraceAsString(), 'logged_in_user' => Auth::id() ]); flash("An error occurred. Please try again or contact Stre@mline Support")->error(); return back()->withInput(); } // is_invoice flag $is_invoice = $return_data_array[0]; // receipt number $receipt_number = $return_data_array[1]; if ($request->has('patient_account_pay_with')) { record_patient_accounts_consumption($request->patient_id, $request->episode_id, $request->patient_account_pay_with, $request->tag_id, $receipt_number); } if ($request->has('family_account_pay_with')) { record_family_consumption($request->patient_id, $request->episode_id, $request->family_account_id, $request->family_account_pay_with, 'Investigations', $receipt_number); } // for the staff guarantor, if debt plan id is not 0, add receipt number to it if ($request->debt_plan_id != 0) { DB::table('debt_plan')->where('id', $request->debt_plan_id) ->update(['receipt_number' => $receipt_number]); } // details to be used for receipt $hospital_information = HospitalInformation::first(); $total_amount_pay = $request->total_amount_pay; $staff_guarantor_to_pay = $request->staff_guarantor_to_pay; $donor_to_pay = $request->donor_to_pay; $patient_to_pay = $request->patient_to_pay; $hospital_to_pay_selected_discount = $request->hospital_to_pay_selected_discount; $hospital_to_pay_general_discount = $request->hospital_to_pay_general_discount; $balance = $request->balance; $patient_category_to_pay = $request->patient_category_to_pay; $patient_category_id_to_pay = $request->patient_category_id; $one_off_discount = $request->one_off_discount; $one_off_discount_memo = $request->one_off_discount_memo; $patient_account_pay_with = $request->patient_account_pay_with ?? 0; $family_account_pay_with = $request->family_account_pay_with ?? 0; $amount_paid_from_wallets = $patient_account_pay_with + $family_account_pay_with; // check if hospital discounts are positive to add them to total if($hospital_to_pay_selected_discount > 0){ $hospital_to_pay = $hospital_to_pay_general_discount + $hospital_to_pay_selected_discount; } else { $hospital_to_pay = $hospital_to_pay_general_discount; } $return_payment_methods = $this->register_payment_method($request->patient_id, $request->episode_id, $request->original_cash_to_pay, $request->payment_method, $request->payment_methods_amount, $request->ref_number, $request->ref_number_added_by, $receipt_number, 2, Auth::id(), $amount_paid_from_wallets); $investigation_ids_array = explode(",", $request->investigation_ids); $investigation_amounts_array = $request->investigation_amount; $patient = Patient::find($request->patient_id); $receipt_date = date('Y-m-d H:i:s'); /*====== if the patient is a dependant of a certain patient category patient, do this =====*/ $patient_category_id = get_name($request->patient_id, "id", "category_id", "patients"); $does_patient_category_have_threshold = does_patient_category_have_threshold($patient_category_id); $patient_is_a_dependant_of = patient_is_a_dependant_of($request->patient_id); if ($does_patient_category_have_threshold || !is_null($patient_is_a_dependant_of)) { $tag_id = 2; $amount_consumed = $patient_to_pay + $original_patient_category_to_pay; $this->record_dependant_consumption($request, $patient_is_a_dependant_of, $investigation_ids_array, $investigation_amounts_array, $amount_consumed, $tag_id, "Dependant-Investigations", $receipt_number); } /*===============end of that dependant functionality =======================*/ $cashier = Auth::id(); $compact_values = compact('hospital_information', 'receipt_date', 'receipt_number', 'patient_unpaid_debts', 'one_off_discount_memo', 'one_off_discount', 'patient', 'is_invoice', 'investigation_ids_array', 'investigation_amounts_array', 'patient_category_id_to_pay', 'family_account_pay_with', 'total_amount_pay', 'staff_guarantor_to_pay', 'return_payment_methods', 'patient_account_pay_with', 'cashier', 'donor_to_pay', 'balance', 'hospital_to_pay', 'patient_to_pay', 'patient_category_to_pay', 'one_off_discount_memo', 'one_off_discount'); $this->save_receipt_json($receipt_number, json_encode($compact_values)); flash("Investigation deposit saved successfully")->success(); if (is_cashier_receipt_type_print_html()) { return redirect('/patient_finance/print_investigation_receipt/' . 1 . '/' . $receipt_number); } else { // so we first have to set a session and then go back to the payment page // on the payment page we can then set a JS variable that can help redirect us to our pdf print session()->put('print_cashier_receipt_pdf', 1); // since we want to reduce the amount of duplicate code, we shall send all items to one file for printing $data = [ "hospital_information" => $compact_values["hospital_information"], "receipt_date" => $compact_values["receipt_date"], "receipt_number" => $compact_values["receipt_number"], "one_off_discount_memo" => $compact_values["one_off_discount_memo"], "one_off_discount" => $compact_values["one_off_discount"], "patient" => $compact_values["patient"], "is_invoice" => $compact_values["is_invoice"], "items" => $compact_values["investigation_ids_array"], "item_quantity" => array_fill(0, count($compact_values["investigation_ids_array"]), 1), "item_subtotal" => $compact_values["investigation_amounts_array"], "patient_category_id_to_pay" => $compact_values["patient_category_id_to_pay"], "total_amount_pay" => $compact_values["total_amount_pay"], "staff_guarantor_to_pay" => $compact_values["staff_guarantor_to_pay"], "patient_unpaid_debts" => $compact_values["patient_unpaid_debts"], "donor_to_pay" => $compact_values["donor_to_pay"], "balance" => $compact_values["balance"], "hospital_to_pay" => $compact_values["hospital_to_pay"], "patient_to_pay" => $compact_values["patient_to_pay"], "patient_category_to_pay" => $compact_values["patient_category_to_pay"], "return_payment_methods" => $compact_values["return_payment_methods"], "treatment_balance" => 0, "item_table" => "investigations", "ward_discount_amount" => 0, "discounts_applied" => 0, "patient_account_pay_with" => $compact_values["patient_account_pay_with"], "family_account_pay_with" => $compact_values["family_account_pay_with"] ]; session()->put('print_cashier_receipt_pdf_details', $data); return redirect('/patient_finance/make_deposit_investigations'); } } /** * Reprint Investigations Receipt * * @param $id * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View */ public function print_investigation_receipt($type, $receipt_number){ // check if track_receipts has details $receipt_json = get_name((int)$receipt_number, 'id', 'receipt_json', 'track_receipts'); if ($receipt_json) { $compact_values = json_decode($receipt_json, true); $compact_values["patient"] = Patient::find($compact_values["patient"]["id"]); $compact_values["hospital_information"] = HospitalInformation::first(); $compact_values["investigation_prices_array"] = $compact_values["investigation_amounts_array"]; $compact_values["discounts_applied"] = 0; $compact_values["cashier"] = $compact_values["cashier"] ?? Auth::id(); } else { if ($type == 1) { // get payment details for the services $investigation_payment = InvestigationDeposit::where(['receipt_number' => $receipt_number])->first(); $patient_to_pay = $investigation_payment->patient_amount_paid; $investigation_prices_array = explode(",", $investigation_payment->investigation_amounts); $investigation_ids_array = explode(",", $investigation_payment->investigation_items); } else { // get payment details for the services $investigation_payment = PatientCategoryInvoice::where(['receipt_number' => $receipt_number])->first(); $patient_to_pay = 0; $investigation_prices_array = explode(",", $investigation_payment->items_amounts); $investigation_ids_array = explode(",", $investigation_payment->items_ids); } $total_amount_pay = 0; foreach ($investigation_prices_array as $price){ $total_amount_pay += $price; } $receipt_date = $investigation_payment->created_at; $cashier = $investigation_payment->created_by; // receipt number $receipt_number = $investigation_payment->receipt_number; // details to be used for receipt $hospital_information = HospitalInformation::first(); $discounts_applied = $total_amount_pay - $patient_to_pay; $patient = Patient::find($investigation_payment->patient_id); $patient_account_pay_with = 0; $family_account_pay_with = 0; $compact_values = compact('hospital_information', 'receipt_date', 'receipt_number', 'discounts_applied', 'patient', 'investigation_ids_array', 'investigation_prices_array','total_amount_pay','patient_to_pay', 'cashier', 'type', 'patient_account_pay_with', 'family_account_pay_with'); } if (is_cashier_receipt_type_print_html()) { if ($receipt_json) { return view('patient_finance::patient_finance.receipts.investigations', $compact_values); } else { return view('patient_finance::patient_finance.receipts.investigations_reprint', $compact_values); } } else { // so we first have to set a session and then go back to the payment page // on the payment page we can then set a JS variable that can help redirect us to our pdf print session()->put('print_cashier_receipt_pdf', 1); // since we want to reduce the amount of duplicate code, we shall send all items to one file for printing $data = [ "hospital_information" => $compact_values["hospital_information"], "receipt_date" => $compact_values["receipt_date"], "receipt_number" => $compact_values["receipt_number"], "one_off_discount_memo" => "", "one_off_discount" => "", "patient" => $compact_values["patient"], "is_invoice" => "", "items" => $compact_values["investigation_ids_array"], "item_quantity" => array_fill(0, count($compact_values["investigation_ids_array"]), 1), "item_subtotal" => $compact_values["investigation_prices_array"], "patient_category_id_to_pay" => 0, "total_amount_pay" => $compact_values["total_amount_pay"], "staff_guarantor_to_pay" => "", "patient_unpaid_debts" => 0, "donor_to_pay" => 0, "balance" => 0, "hospital_to_pay" => "", "patient_to_pay" => $compact_values["patient_to_pay"], "patient_category_to_pay" => 0, "return_payment_methods" => [], "treatment_balance" => 0, "item_table" => "investigations", "ward_discount_amount" => 0, "discounts_applied" => $compact_values["discounts_applied"], "patient_account_pay_with" => $compact_values["patient_account_pay_with"], "family_account_pay_with" => $compact_values["family_account_pay_with"] ]; session()->put('print_cashier_receipt_pdf_details', $data); return redirect('/patient_finance/make_deposit_investigations'); } } /** * Start payment process for prescriptions * * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View */ public function make_deposit_treatment(Request $request){ if (isset($request->price_list_category_id) && $request->price_list_category_id != 0){ $price_list_set = $request->price_list_category_id; } else { $price_list_set = false; } $price_list_categories = PriceListCategories::select('patient_category_id', 'id')->get(); $patient_id = session()->get('patient_id'); $episode_id = session()->get('episode_id'); $episode = PatientEpisode::find($episode_id); $patient = Patient::find($patient_id); $staff_guarantors = DB::table('users')->pluck('first_name', 'id')->toArray(); foreach ($staff_guarantors as $key => $value) { $full_name = $value . " " . get_name($key, "id", "last_name", "users"); $staff_guarantors[$key] = $full_name; } $categories = DB::table('patient_categories')->where('available', 1)->pluck('name', 'id'); $drug_categories = DB::table('drug_categories')->get(); $marital_statuses = DB::table('marital_statuses')->pluck('name', 'id'); $payment_arrangement = DB::table('debt_plan_arrangements')->pluck('name', 'id')->toArray(); $payment_arrangement = ['' => '- select -'] + $payment_arrangement; $discount = PatientDiscount::where(['patient_category'=>$patient->category_id])->first(); $discount_options = DiscountCategories::where(['patient_category' => $patient->category_id])->pluck('name','id')->toArray(); $discount_options = ['' => '- Select Discount -'] + $discount_options; $treatments = Treatment::where(['payment_status' => 0, 'episode_id' => $episode_id ,'inpatient_bill_generated' => 0])->get(); // check if there are any paid treatments to display receipt $paid_treatments = TreatmentDeposits::where(['episode_id' => $episode_id])->get(); $invoiced_treatments = PatientCategoryInvoice::where('episode_id', $episode_id)->where('tag_id', 3)->get(); $patient_payment_methods = PatientPaymentMethod::pluck('name', 'id'); $patient_payment_methods_options = ""; foreach ($patient_payment_methods as $key => $value) { $patient_payment_methods_options .= ''; } $patient_unpaid_items_array = explode(",", $this->does_patient_have_unpaid_items($patient_id, $episode_id)); $patient_unpaid_items = $patient_unpaid_items_array[0]; $patient_debts = $patient_unpaid_items_array[1]; $non_staff_guarantors = DB::table('non_staff_guarantors')->pluck('first_name', 'id')->toArray(); foreach ($non_staff_guarantors as $key => $value) { $full_guarantor_name = $value . " " . get_name($key, "id", "last_name", "non_staff_guarantors"); $non_staff_guarantors[$key] = $full_guarantor_name; } return view('patient_finance::patient_finance.make_deposit_treatment',compact('patient_id','episode_id', 'patient_unpaid_items', 'patient','episode','categories','drug_categories','marital_statuses','discount','discount_options','treatments', 'invoiced_treatments', 'patient_debts', 'payment_arrangement','staff_guarantors', 'paid_treatments', 'price_list_set', 'price_list_categories', 'patient_payment_methods', 'patient_payment_methods_options', 'non_staff_guarantors')); } public function store_deposit_treatment(Request $request){ $wallets_amount = 0; if ($request->has('patient_account_pay_with')) { if ($request->patient_account_pay_with > $request->patient_account_balance) { // amount is more than is on the patient account flash('Patient account balance is less than the current bill.')->error(); return redirect()->back()->withInput(); } $wallets_amount += $request->patient_account_pay_with; } if ($request->has('family_account_pay_with')) { $this_patient_to_pay = $request->family_account_pay_with; $this_family_account_balance = $request->family_account_balance; $family_account_credit_limit = $request->family_account_credit_limit; $wallets_amount += $request->family_account_pay_with; if (!is_family_consumption_allowed($this_patient_to_pay, $this_family_account_balance, $family_account_credit_limit)) { return redirect()->back()->withInput(); } } $request->merge(['wallets_amount' => $wallets_amount]); //check if patient is a dependant of another patient e.g main patient with a discount. If so, let Neo enter the matrix if ($request->has('dependants_balance')) { $original_dependants_balance = $request->dependants_balance; $original_patient_category_to_pay = $request->patient_category_to_pay; //if new dependants balance is -ve, make it zero $new_dependants_balance = $request->dependants_balance < 0 ? 0 : $request->dependants_balance; $request->merge(['dependants_balance' => $new_dependants_balance]); //recalculate the patient category to pay if ($request->patient_category_to_pay > $original_dependants_balance) { if ($original_dependants_balance < 0) { $patient_category_to_pay = $request->patient_category_to_pay; } else{ $patient_category_to_pay = $request->patient_category_to_pay - $original_dependants_balance; } } else { $patient_category_to_pay = 0; } $request->merge(['patient_category_to_pay' => $patient_category_to_pay]); //make the main patient's patient category override the category of the dependant $patient_is_a_dependant_of = patient_is_a_dependant_of($request->patient_id); $main_patient_patient_category = get_name($patient_is_a_dependant_of, "id", "category_id", "patients"); $request->merge(['patient_category_id' => $main_patient_patient_category]); } if ($request->patient_unpaid_debts > 0) { $this->debtorsService->pay_for_patient_debt($request->episode_id, $request->patient_unpaid_debts); } $patient_unpaid_debts = $request->patient_unpaid_debts; $treatment_deposit = new TreatmentDeposit; // begin transaction DB::beginTransaction(); try { // send data to be saved in treatment deposits table and get the is_invoice flag and receipt number $return_data_array = explode(",", $treatment_deposit->store_payment($request)); // all good so we can commit DB::commit(); } catch (\Exception | \Error $exception) { DB::rollback(); DB::table('system_errors')->insert([ 'error_message' => $exception->getMessage(), 'error_trace' => $exception->getTraceAsString(), 'logged_in_user' => Auth::id() ]); flash("An error occurred. Please try again or contact Stre@mline Support")->error(); return back()->withInput(); } if ($return_data_array[1] == 0) { flash("Treatment record has been cleared successfully.")->success(); return redirect('/patient_finance/home'); } // is_invoice flag $is_invoice = $return_data_array[0]; // receipt number $receipt_number = $return_data_array[1]; if ($request->has('patient_account_pay_with')) { record_patient_accounts_consumption($request->patient_id, $request->episode_id, $request->patient_account_pay_with, $request->tag_id, $receipt_number); } if ($request->has('family_account_pay_with')) { record_family_consumption($request->patient_id, $request->episode_id, $request->family_account_id, $request->family_account_pay_with, 'Treatment', $receipt_number); } // for the staff guarantor, if debt plan id is not 0, add receipt number to it if ($request->debt_plan_id != 0) { DB::table('debt_plan')->where('id', $request->debt_plan_id) ->update(['receipt_number' => $receipt_number]); } // details to be used for receipt $hospital_information = HospitalInformation::first(); $total_amount_pay = $request->total_amount_pay; $staff_guarantor_to_pay = $request->staff_guarantor_to_pay; $donor_to_pay = $request->donor_to_pay; $patient_to_pay = $request->patient_to_pay; $hospital_to_pay_selected_discount = $request->hospital_to_pay_selected_discount; $hospital_to_pay_general_discount = $request->hospital_to_pay_general_discount; $balance = $request->balance; $patient_category_to_pay = $request->patient_category_to_pay; $patient_category_id_to_pay = $request->patient_category_id; $one_off_discount = $request->one_off_discount; $one_off_discount_memo = $request->one_off_discount_memo; $treatment_balance = $request->treatment_balance; $patient_account_pay_with = $request->patient_account_pay_with ?? 0; $family_account_pay_with = $request->family_account_pay_with ?? 0; $amount_paid_from_wallets = $patient_account_pay_with + $family_account_pay_with; // check if hospital discounts are positive to add them to total if($hospital_to_pay_selected_discount > 0){ $hospital_to_pay = $hospital_to_pay_general_discount + $hospital_to_pay_selected_discount; } else { $hospital_to_pay = $hospital_to_pay_general_discount; } $treatment_item = $request->treatment_item; $treatment_quantity = $request->treatment_quantity; $treatment_subtotal = $request->treatment_subtotal; $patient = Patient::find($request->patient_id); $receipt_date = date('Y-m-d H:i:s'); /*====== if the patient is a dependant of a certain patient category patient, do this =====*/ $patient_category_id = get_name($request->patient_id, "id", "category_id", "patients"); $does_patient_category_have_threshold = does_patient_category_have_threshold($patient_category_id); $patient_is_a_dependant_of = patient_is_a_dependant_of($request->patient_id); if ($does_patient_category_have_threshold || !is_null($patient_is_a_dependant_of)) { $tag_id = 3; $amount_consumed = $patient_to_pay + $original_patient_category_to_pay; $this->record_dependant_consumption($request, $patient_is_a_dependant_of, $request->treatment_item, $treatment_subtotal, $amount_consumed, $tag_id, "Dependant-Treatment", $receipt_number); } /*===============end of that dependant functionality =======================*/ $return_payment_methods = $this->register_payment_method($request->patient_id, $request->episode_id, $request->original_cash_to_pay, $request->payment_method, $request->payment_methods_amount, $request->ref_number, $request->ref_number_added_by, $receipt_number, 3, Auth::id(), $amount_paid_from_wallets); $cashier = Auth::id(); $compact_values = compact('hospital_information', 'receipt_date', 'receipt_number', 'one_off_discount_memo', 'one_off_discount', 'patient', 'is_invoice', 'treatment_item', 'treatment_quantity', 'treatment_subtotal', 'patient_category_id_to_pay', 'family_account_pay_with', 'total_amount_pay', 'staff_guarantor_to_pay', 'patient_unpaid_debts', 'patient_account_pay_with', 'cashier', 'donor_to_pay', 'balance', 'hospital_to_pay', 'patient_to_pay', 'patient_category_to_pay', 'return_payment_methods', 'treatment_balance'); $this->save_receipt_json($receipt_number, json_encode($compact_values)); flash("Treatment deposit saved successfully")->success(); if (is_cashier_receipt_type_print_html()) { return redirect('/patient_finance/print_treatment_receipt/' . 1 . '/' . $receipt_number); } else { // so we first have to set a session and then go back to the payment page // on the payment page we can then set a JS variable that can help redirect us to our pdf print session()->put('print_cashier_receipt_pdf', 1); // since we want to reduce the amount of duplicate code, we shall send all items to one file for printing $data = [ "hospital_information" => $compact_values["hospital_information"], "receipt_date" => $compact_values["receipt_date"], "receipt_number" => $compact_values["receipt_number"], "one_off_discount_memo" => $compact_values["one_off_discount_memo"], "one_off_discount" => $compact_values["one_off_discount"], "patient" => $compact_values["patient"], "is_invoice" => $compact_values["is_invoice"], "items" => $compact_values["treatment_item"], "item_quantity" => $compact_values["treatment_quantity"], "item_subtotal" => $compact_values["treatment_subtotal"], "patient_category_id_to_pay" => $compact_values["patient_category_id_to_pay"], "total_amount_pay" => $compact_values["total_amount_pay"], "staff_guarantor_to_pay" => $compact_values["staff_guarantor_to_pay"], "patient_unpaid_debts" => $compact_values["patient_unpaid_debts"], "donor_to_pay" => $compact_values["donor_to_pay"], "balance" => $compact_values["balance"], "hospital_to_pay" => $compact_values["hospital_to_pay"], "patient_to_pay" => $compact_values["patient_to_pay"], "patient_category_to_pay" => $compact_values["patient_category_to_pay"], "return_payment_methods" => $compact_values["return_payment_methods"], "treatment_balance" => $compact_values["treatment_balance"], "item_table" => "drugs", "ward_discount_amount" => 0, "discounts_applied" => 0, "patient_account_pay_with" => $compact_values["patient_account_pay_with"], "family_account_pay_with" => $compact_values["family_account_pay_with"] ]; session()->put('print_cashier_receipt_pdf_details', $data); return redirect('/patient_finance/make_deposit_treatment'); } } /** * Reprint Treatments Receipt * * @param $type * @param $id * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View */ public function print_treatment_receipt($type, $receipt_number){ // check if track_receipts has details $receipt_json = get_name((int)$receipt_number, 'id', 'receipt_json', 'track_receipts'); if ($receipt_json) { $compact_values = json_decode($receipt_json, true); $compact_values["patient"] = Patient::find($compact_values["patient"]["id"]); $compact_values["hospital_information"] = HospitalInformation::first(); $compact_values["treatment_prices_array"] = $compact_values["treatment_subtotal"]; $compact_values["treatment_ids_array"] = $compact_values["treatment_item"]; $compact_values["treatment_quantity_array"] = $compact_values["treatment_quantity"]; $compact_values["discounts_applied"] = 0; $compact_values["cashier"] = $compact_values["cashier"] ?? Auth::id(); } else { if ($type == 1) { // get payment details for the treatment $treatment_payment = TreatmentDeposits::where(['receipt_number' => $receipt_number])->first(); $patient_to_pay = $treatment_payment->patient_amount_paid; $treatment_prices_array = explode(",", $treatment_payment->treatment_subtotals); $treatment_ids_array = explode(",", $treatment_payment->treatment_items); $treatment_quantity_array = explode(",", $treatment_payment->treatment_quantities); } else { // get payment details for the treatment $treatment_payment = PatientCategoryInvoice::where(['receipt_number' => $receipt_number])->first(); $patient_to_pay = 0; $treatment_prices_array = explode(",", $treatment_payment->items_amounts); $treatment_ids_array = explode(",", $treatment_payment->items_ids); $treatment_quantity_array = explode(",", $treatment_payment->items_quantity); } $total_amount_pay = 0; foreach ($treatment_prices_array as $price){ $total_amount_pay += $price; } $receipt_date = $treatment_payment->created_at; $cashier = $treatment_payment->created_by; // receipt number $receipt_number = $treatment_payment->receipt_number; // details to be used for receipt $hospital_information = HospitalInformation::first(); $discounts_applied = $total_amount_pay - $patient_to_pay; $patient = Patient::find($treatment_payment->patient_id); $patient_account_pay_with = 0; $family_account_pay_with = 0; $compact_values = compact('hospital_information', 'receipt_date', 'receipt_number', 'discounts_applied', 'type', 'patient', 'treatment_quantity_array', 'treatment_ids_array', 'treatment_prices_array','total_amount_pay','patient_to_pay', 'cashier', 'patient_account_pay_with', 'family_account_pay_with'); } if (is_cashier_receipt_type_print_html()) { if ($receipt_json) { return view('patient_finance::patient_finance.receipts.treatment', $compact_values); } else { return view('patient_finance::patient_finance.receipts.treatment_reprint', $compact_values); } } else { // so we first have to set a session and then go back to the payment page // on the payment page we can then set a JS variable that can help redirect us to our pdf print session()->put('print_cashier_receipt_pdf', 1); // since we want to reduce the amount of duplicate code, we shall send all items to one file for printing $data = [ "hospital_information" => $compact_values["hospital_information"], "receipt_date" => $compact_values["receipt_date"], "receipt_number" => $compact_values["receipt_number"], "one_off_discount_memo" => "", "one_off_discount" => "", "patient" => $compact_values["patient"], "is_invoice" => "", "items" => $compact_values["treatment_ids_array"], "item_quantity" => $compact_values["treatment_quantity_array"], "item_subtotal" => $compact_values["treatment_prices_array"], "patient_category_id_to_pay" => 0, "total_amount_pay" => $compact_values["total_amount_pay"], "staff_guarantor_to_pay" => "", "patient_unpaid_debts" => 0, "donor_to_pay" => 0, "balance" => 0, "hospital_to_pay" => "", "patient_to_pay" => $compact_values["patient_to_pay"], "patient_category_to_pay" => 0, "return_payment_methods" => [], "treatment_balance" => 0, "item_table" => "drugs", "ward_discount_amount" => 0, "discounts_applied" => $compact_values["discounts_applied"], "patient_account_pay_with" => $compact_values["patient_account_pay_with"], "family_account_pay_with" => $compact_values["family_account_pay_with"] ]; session()->put('print_cashier_receipt_pdf_details', $data); return redirect('/patient_finance/make_deposit_treatment'); } } /** * Make deposit for co-payments * * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View */ public function make_deposit_co_payments(Request $request){ if (isset($request->price_list_category_id) && $request->price_list_category_id != 0){ $price_list_set = $request->price_list_category_id; } else { $price_list_set = false; } $price_list_categories = PriceListCategories::select('patient_category_id', 'id')->get(); $patient_id = session()->get('patient_id'); $episode_id = session()->get('episode_id'); $patient = Patient::find($patient_id); $episode = PatientEpisode::find($episode_id); $categories = DB::table('patient_categories')->where('available', 1)->pluck('name', 'id'); $drug_categories = DB::table('drug_categories')->get(); $marital_statuses = DB::table('marital_statuses')->pluck('name', 'id'); $discount = PatientDiscount::where(['patient_category'=>$patient->category_id])->first(); $discount_options = DiscountCategories::where(['patient_category' => $patient->category_id])->pluck('name','id')->toArray(); $discount_options = ['' => '- Select Discount -'] + $discount_options; $payment_arrangement = DB::table('debt_plan_arrangements')->pluck('name', 'id')->toArray(); $payment_arrangement = ['' => '- select -'] + $payment_arrangement; $staff_guarantors = DB::table('users')->pluck('first_name', 'id')->toArray(); foreach ($staff_guarantors as $key => $value) { $full_name = $value . " " . get_name($key, "id", "last_name", "users"); $staff_guarantors[$key] = $full_name; } $service_items = Services::where(['item_type'=>'Co_Payment'])->orderBy('name','asc')->get(); $deposit_type = 'Co_Payment'; $tag_id = 7; // check if there are any paid services to display receipt $paid_services = ServiceDeposit::where(['episode_id' => $episode_id, 'service_type' => $deposit_type])->orderBy('created_at', 'desc')->get(); $invoiced_services = PatientCategoryInvoice::where('episode_id', $episode_id)->where('tag_id', 7)->get(); $patient_payment_methods = PatientPaymentMethod::pluck('name', 'id'); $patient_payment_methods_options = ""; foreach ($patient_payment_methods as $key => $value) { $patient_payment_methods_options .= ''; } $patient_unpaid_items_array = explode(",", $this->does_patient_have_unpaid_items($patient_id, $episode_id)); $patient_unpaid_items = $patient_unpaid_items_array[0]; $patient_debts = $patient_unpaid_items_array[1]; $non_staff_guarantors = DB::table('non_staff_guarantors')->pluck('first_name', 'id')->toArray(); foreach ($non_staff_guarantors as $key => $value) { $full_guarantor_name = $value . " " . get_name($key, "id", "last_name", "non_staff_guarantors"); $non_staff_guarantors[$key] = $full_guarantor_name; } return view('patient_finance::patient_finance.make_deposit_services',compact('patient','episode','episode_id','patient_id','categories', 'patient_unpaid_items', 'drug_categories','marital_statuses','service_items','deposit_type','discount','discount_options','payment_arrangement','staff_guarantors', 'patient_debts', 'paid_services', 'tag_id', 'price_list_set', 'price_list_categories', 'patient_payment_methods', 'patient_payment_methods_options', 'invoiced_services', 'non_staff_guarantors')); } /** * Make deposit for services and other services */ public function make_deposit_other_services(Request $request){ if (isset($request->price_list_category_id) && $request->price_list_category_id != 0){ $price_list_set = $request->price_list_category_id; } else { $price_list_set = false; } $price_list_categories = PriceListCategories::select('patient_category_id', 'id')->get(); $patient_id = session()->get('patient_id'); $episode_id = session()->get('episode_id'); $patient = Patient::find($patient_id); $episode = PatientEpisode::find($episode_id); $categories = DB::table('patient_categories')->where('available', 1)->pluck('name', 'id'); $drug_categories = DB::table('drug_categories')->get(); $marital_statuses = DB::table('marital_statuses')->pluck('name', 'id'); $discount = PatientDiscount::where(['patient_category'=>$patient->category_id])->first(); $discount_options = DiscountCategories::where(['patient_category' => $patient->category_id])->pluck('name','id')->toArray(); $discount_options = ['' => '- Select Discount -'] + $discount_options; $payment_arrangement = DB::table('debt_plan_arrangements')->pluck('name', 'id')->toArray(); $payment_arrangement = ['' => '- select -'] + $payment_arrangement; $staff_guarantors = DB::table('users')->pluck('first_name', 'id')->toArray(); foreach ($staff_guarantors as $key => $value) { $full_name = $value . " " . get_name($key, "id", "last_name", "users"); $staff_guarantors[$key] = $full_name; } $service_items = Services::where(['item_type'=>'Other_service'])->orWhere(['item_type'=>'Service'])->orderBy('name','asc')->get(); $deposit_type = 'Services'; $tag_id = 8; // check if there are any paid services to display receipt $paid_services = ServiceDeposit::where(['episode_id' => $episode_id, 'service_type' => $deposit_type])->orderBy('created_at', 'desc')->get(); // check for any used services that were recorded from th clinical side $used_services = OrderedService::where(['patient_id' => $episode->patient_id, 'episode_id' => $episode->id, 'payment_status' => 0 ,'inpatient_bill_generated' => 0])->get(); $invoiced_services = PatientCategoryInvoice::where('episode_id', $episode_id)->where('tag_id', 8)->get(); $patient_payment_methods = PatientPaymentMethod::pluck('name', 'id'); $patient_payment_methods_options = ""; foreach ($patient_payment_methods as $key => $value) { $patient_payment_methods_options .= ''; } $patient_unpaid_items_array = explode(",", $this->does_patient_have_unpaid_items($patient_id, $episode_id)); $patient_unpaid_items = $patient_unpaid_items_array[0]; $patient_debts = $patient_unpaid_items_array[1]; $non_staff_guarantors = DB::table('non_staff_guarantors')->pluck('first_name', 'id')->toArray(); foreach ($non_staff_guarantors as $key => $value) { $full_guarantor_name = $value . " " . get_name($key, "id", "last_name", "non_staff_guarantors"); $non_staff_guarantors[$key] = $full_guarantor_name; } return view('patient_finance::patient_finance.make_deposit_services',compact('patient','episode','episode_id','patient_id','categories','drug_categories', 'patient_unpaid_items', 'marital_statuses','service_items','deposit_type','discount','discount_options','payment_arrangement','staff_guarantors', 'paid_services', 'tag_id', 'patient_debts', 'price_list_set', 'price_list_categories','used_services', 'patient_payment_methods', 'patient_payment_methods_options', 'invoiced_services', 'non_staff_guarantors')); } /** * Make deposit for consultation */ public function make_deposit_consultation(Request $request){ if (isset($request->price_list_category_id) && $request->price_list_category_id != 0){ $price_list_set = $request->price_list_category_id; } else { $price_list_set = false; } $price_list_categories = PriceListCategories::select('patient_category_id', 'id')->get(); $patient_id = session()->get('patient_id'); $episode_id = session()->get('episode_id'); $patient = Patient::find($patient_id); $episode = PatientEpisode::find($episode_id); $categories = DB::table('patient_categories')->where('available', 1)->pluck('name', 'id'); $drug_categories = DB::table('drug_categories')->get(); $marital_statuses = DB::table('marital_statuses')->pluck('name', 'id'); $discount = PatientDiscount::where(['patient_category'=>$patient->category_id])->first(); $discount_options = DiscountCategories::where(['patient_category' => $patient->category_id])->pluck('name','id')->toArray(); $discount_options = ['' => '- Select Discount -'] + $discount_options; $payment_arrangement = DB::table('debt_plan_arrangements')->pluck('name', 'id')->toArray(); $payment_arrangement = ['' => '- select -'] + $payment_arrangement; $staff_guarantors = DB::table('users')->pluck('first_name', 'id')->toArray(); foreach ($staff_guarantors as $key => $value) { $full_name = $value . " " . get_name($key, "id", "last_name", "users"); $staff_guarantors[$key] = $full_name; } $service_items = Services::where(['item_type'=>'Consultation'])->orderBy('name','asc')->get(); $deposit_type = 'Consultation'; $tag_id = 6; // check if there are any paid services to display receipt $paid_services = ServiceDeposit::where(['episode_id' => $episode_id, 'service_type' => $deposit_type])->orderBy('created_at', 'desc')->get(); $invoiced_services = PatientCategoryInvoice::where('episode_id', $episode_id)->where('tag_id', 6)->get(); $patient_payment_methods = PatientPaymentMethod::pluck('name', 'id'); $patient_payment_methods_options = ""; foreach ($patient_payment_methods as $key => $value) { $patient_payment_methods_options .= ''; } $patient_unpaid_items_array = explode(",", $this->does_patient_have_unpaid_items($patient_id, $episode_id)); $patient_unpaid_items = $patient_unpaid_items_array[0]; $patient_debts = $patient_unpaid_items_array[1]; $non_staff_guarantors = DB::table('non_staff_guarantors')->pluck('first_name', 'id')->toArray(); foreach ($non_staff_guarantors as $key => $value) { $full_guarantor_name = $value . " " . get_name($key, "id", "last_name", "non_staff_guarantors"); $non_staff_guarantors[$key] = $full_guarantor_name; } $used_services = OrderedService::where(['patient_id' => $episode->patient_id, 'episode_id' => $episode->id, 'payment_status' => 0 ,'inpatient_bill_generated' => 0])->get(); return view('patient_finance::patient_finance.make_deposit_services',compact('patient','episode','episode_id','patient_id','categories','drug_categories','marital_statuses','service_items','deposit_type', 'patient_unpaid_items', 'patient_debts', 'discount','discount_options','payment_arrangement','staff_guarantors', 'paid_services', 'tag_id', 'price_list_set', 'price_list_categories', 'patient_payment_methods', 'patient_payment_methods_options', 'invoiced_services', 'used_services', 'non_staff_guarantors')); } public function store_deposit_services(Request $request){ $wallets_amount = 0; if ($request->has('patient_account_pay_with')) { if ($request->patient_account_pay_with > $request->patient_account_balance) { // amount is more than is on the patient account flash('Patient account balance is less than the current bill.')->error(); return redirect()->back()->withInput(); } $wallets_amount += $request->patient_account_pay_with; } if ($request->has('family_account_pay_with')) { $this_patient_to_pay = $request->family_account_pay_with; $this_family_account_balance = $request->family_account_balance; $family_account_credit_limit = $request->family_account_credit_limit; $wallets_amount += $request->family_account_pay_with; if (!is_family_consumption_allowed($this_patient_to_pay, $this_family_account_balance, $family_account_credit_limit)) { return redirect()->back()->withInput(); } } $request->merge(['wallets_amount' => $wallets_amount]); //check if patient is a dependant of another patient e.g main patient with a discount. If so, let Neo enter the matrix if ($request->has('dependants_balance')) { $original_dependants_balance = $request->dependants_balance; $original_patient_category_to_pay = $request->patient_category_to_pay; //if new dependants balance is -ve, make it zero $new_dependants_balance = $request->dependants_balance < 0 ? 0 : $request->dependants_balance; $request->merge(['dependants_balance' => $new_dependants_balance]); //recalculate the patient category to pay if ($request->patient_category_to_pay > $original_dependants_balance) { if ($original_dependants_balance < 0) { $patient_category_to_pay = $request->patient_category_to_pay; } else{ $patient_category_to_pay = $request->patient_category_to_pay - $original_dependants_balance; } } else { $patient_category_to_pay = 0; } $request->merge(['patient_category_to_pay' => $patient_category_to_pay]); //make the main patient's patient category override the category of the dependant $patient_is_a_dependant_of = patient_is_a_dependant_of($request->patient_id); $main_patient_patient_category = get_name($patient_is_a_dependant_of, "id", "category_id", "patients"); $request->merge(['patient_category_id' => $main_patient_patient_category]); } // clean up the services $temp_service_ids_array = $request->service_id; if (in_array("--Select Service--", $temp_service_ids_array)) { $dirty_service_ids_array = $request->service_id; $temp_service_ids_array = []; for ($i=0; $i < count($dirty_service_ids_array) ; $i++) { if ($dirty_service_ids_array[$i] != "--Select Service--") { $temp_service_ids_array[] = $dirty_service_ids_array[$i]; } } } $request->service_id = array_values($temp_service_ids_array); if (is_null($request->service_amount) || !is_array($request->service_id) || (count($request->service_id) == 1 && $request->service_id[0] == "--Select Service--")) { // check if any items are available to pay for flash('Please make sure you select some services before submitting!')->error(); return redirect()->back(); } if ($request->patient_unpaid_debts > 0) { $this->debtorsService->pay_for_patient_debt($request->episode_id, $request->patient_unpaid_debts); } $patient_unpaid_debts = $request->patient_unpaid_debts; $service_deposit = new ServicesDeposit; // begin transaction DB::beginTransaction(); try { // send data to be saved in services table and get the is_invoice flag and receipt number $return_data_array = explode(",", $service_deposit->store_payment($request)); // all good so we can commit DB::commit(); } catch (\Exception | \Error $exception) { DB::rollback(); DB::table('system_errors')->insert([ 'error_message' => $exception->getMessage(), 'error_trace' => $exception->getTraceAsString(), 'logged_in_user' => Auth::id() ]); flash("An error occurred. Please try again or contact Stre@mline Support")->error(); return back()->withInput(); } // is_invoice flag $is_invoice = $return_data_array[0]; // receipt number $receipt_number = $return_data_array[1]; if ($request->has('patient_account_pay_with')) { record_patient_accounts_consumption($request->patient_id, $request->episode_id, $request->patient_account_pay_with, $request->tag_id, $receipt_number); } if ($request->has('family_account_pay_with')) { record_family_consumption($request->patient_id, $request->episode_id, $request->family_account_id, $request->family_account_pay_with, 'Services', $receipt_number); } // for the staff guarantor, if debt plan id is not 0, add receipt number to it if ($request->debt_plan_id != 0) { DB::table('debt_plan')->where('id', $request->debt_plan_id) ->update(['receipt_number' => $receipt_number]); } // details to be used for receipt $hospital_information = HospitalInformation::first(); $total_amount_pay = $request->total_amount_pay; $staff_guarantor_to_pay = $request->staff_guarantor_to_pay; $donor_to_pay = $request->donor_to_pay; $patient_to_pay = $request->patient_to_pay; $hospital_to_pay_selected_discount = $request->hospital_to_pay_selected_discount; $hospital_to_pay_general_discount = $request->hospital_to_pay_general_discount; $balance = $request->balance; $patient_category_to_pay = $request->patient_category_to_pay; $patient_category_id_to_pay = $request->patient_category_id; $one_off_discount = $request->one_off_discount; $one_off_discount_memo = $request->one_off_discount_memo; $patient_account_pay_with = $request->patient_account_pay_with ?? 0; $family_account_pay_with = $request->family_account_pay_with ?? 0; $amount_paid_from_wallets = $patient_account_pay_with + $family_account_pay_with; // check if hospital discounts are positive to add them to total if($hospital_to_pay_selected_discount > 0){ $hospital_to_pay = $hospital_to_pay_general_discount + $hospital_to_pay_selected_discount; } else { $hospital_to_pay = $hospital_to_pay_general_discount; } $service_prices_array = $request->service_amount; $service_ids_array = $request->service_id; $patient = Patient::find($request->patient_id); $receipt_date = date('Y-m-d H:i:s'); /*====== if the patient is a dependant of a certain patient category patient, do this =====*/ $patient_category_id = get_name($request->patient_id, "id", "category_id", "patients"); $does_patient_category_have_threshold = does_patient_category_have_threshold($patient_category_id); $patient_is_a_dependant_of = patient_is_a_dependant_of($request->patient_id); if ($does_patient_category_have_threshold || !is_null($patient_is_a_dependant_of)) { $tag_id = 6; $amount_consumed = $patient_to_pay + $original_patient_category_to_pay; $this->record_dependant_consumption($request, $patient_is_a_dependant_of, $request->service_id, $service_prices_array, $amount_consumed, $tag_id, "Dependant-Consultation", $receipt_number); } /*===============end of that dependant functionality =======================*/ $return_payment_methods = $this->register_payment_method($request->patient_id, $request->episode_id, $request->original_cash_to_pay, $request->payment_method, $request->payment_methods_amount, $request->ref_number, $request->ref_number_added_by, $receipt_number, $request->tag_id, Auth::id(), $amount_paid_from_wallets); $cashier = Auth::id(); $compact_values = compact('hospital_information', 'receipt_date', 'receipt_number', 'return_payment_methods', 'patient', 'is_invoice', 'service_prices_array', 'service_ids_array', 'patient_category_id_to_pay', 'patient_account_pay_with', 'total_amount_pay', 'staff_guarantor_to_pay', 'patient_unpaid_debts', 'family_account_pay_with', 'cashier', 'donor_to_pay', 'balance', 'hospital_to_pay', 'patient_to_pay', 'patient_category_to_pay', 'one_off_discount_memo', 'one_off_discount'); $this->save_receipt_json($receipt_number, json_encode($compact_values)); flash("Service deposit saved successfully")->success(); if (is_cashier_receipt_type_print_html()) { return redirect('/patient_finance/print_service_receipt/' . 1 . '/' . $receipt_number); } else { // so we first have to set a session and then go back to the payment page // on the payment page we can then set a JS variable that can help redirect us to our pdf print session()->put('print_cashier_receipt_pdf', 1); // since we want to reduce the amount of duplicate code, we shall send all items to one file for printing $data = [ "hospital_information" => $compact_values["hospital_information"], "receipt_date" => $compact_values["receipt_date"], "receipt_number" => $compact_values["receipt_number"], "one_off_discount_memo" => $compact_values["one_off_discount_memo"], "one_off_discount" => $compact_values["one_off_discount"], "patient" => $compact_values["patient"], "is_invoice" => $compact_values["is_invoice"], "items" => $compact_values["service_ids_array"], "item_quantity" => array_fill(0, count($compact_values["service_ids_array"]), 1), "item_subtotal" => $compact_values["service_prices_array"], "patient_category_id_to_pay" => $compact_values["patient_category_id_to_pay"], "total_amount_pay" => $compact_values["total_amount_pay"], "staff_guarantor_to_pay" => $compact_values["staff_guarantor_to_pay"], "patient_unpaid_debts" => $compact_values["patient_unpaid_debts"], "donor_to_pay" => $compact_values["donor_to_pay"], "balance" => $compact_values["balance"], "hospital_to_pay" => $compact_values["hospital_to_pay"], "patient_to_pay" => $compact_values["patient_to_pay"], "patient_category_to_pay" => $compact_values["patient_category_to_pay"], "return_payment_methods" => $compact_values["return_payment_methods"], "treatment_balance" => 0, "item_table" => "services", "ward_discount_amount" => 0, "discounts_applied" => 0, "patient_account_pay_with" => $compact_values["patient_account_pay_with"], "family_account_pay_with" => $compact_values["family_account_pay_with"] ]; session()->put('print_cashier_receipt_pdf_details', $data); return redirect('/patient_finance/make_deposit_other_services'); } } /** * Reprint service receipt * * @param $id * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View */ public function print_service_receipt($type, $receipt_number){ // check if track_receipts has details $receipt_json = get_name((int)$receipt_number, 'id', 'receipt_json', 'track_receipts'); if ($receipt_json) { $compact_values = json_decode($receipt_json, true); $compact_values["patient"] = Patient::find($compact_values["patient"]["id"]); $compact_values["hospital_information"] = HospitalInformation::first(); $compact_values["discounts_applied"] = 0; $compact_values["cashier"] = $compact_values["cashier"] ?? Auth::id(); } else { // get payment details for the services if ($type == 1) { $service_payment = ServiceDeposit::where(['receipt_number' => $receipt_number])->first(); $patient_to_pay = $service_payment->patient_amount_paid; $service_prices_array = explode(",", $service_payment->items_amounts); $service_ids_array = explode(",", $service_payment->items_ids); } else { $service_payment = PatientCategoryInvoice::where(['receipt_number' => $receipt_number])->first(); $patient_to_pay = 0; $service_prices_array = explode(",", $service_payment->items_amounts); $service_ids_array = explode(",", $service_payment->items_ids); } $total_amount_pay = 0; foreach ($service_prices_array as $price){ $total_amount_pay += $price; } $receipt_date = $service_payment->created_at; // receipt number $receipt_number = $service_payment->receipt_number; $cashier = $service_payment->created_by; // details to be used for receipt $hospital_information = HospitalInformation::first(); $discounts_applied = $total_amount_pay - $patient_to_pay; $patient = Patient::find($service_payment->patient_id); $patient_account_pay_with = 0; $family_account_pay_with = 0; $compact_values = compact('hospital_information', 'receipt_date', 'receipt_number', 'discounts_applied', 'patient', 'service_prices_array', 'service_ids_array','total_amount_pay','patient_to_pay', 'cashier', 'type', 'patient_account_pay_with', 'family_account_pay_with'); } if (is_cashier_receipt_type_print_html()) { if ($receipt_json) { return view('patient_finance::patient_finance.receipts.services', $compact_values); } else { return view('patient_finance::patient_finance.receipts.services_reprint', $compact_values); } } else { // so we first have to set a session and then go back to the payment page // on the payment page we can then set a JS variable that can help redirect us to our pdf print session()->put('print_cashier_receipt_pdf', 1); // since we want to reduce the amount of duplicate code, we shall send all items to one file for printing $data = [ "hospital_information" => $compact_values["hospital_information"], "receipt_date" => $compact_values["receipt_date"], "receipt_number" => $compact_values["receipt_number"], "one_off_discount_memo" => "", "one_off_discount" => "", "patient" => $compact_values["patient"], "is_invoice" => "", "items" => $compact_values["service_ids_array"], "item_quantity" => array_fill(0, count($compact_values["service_ids_array"]), 1), "item_subtotal" => $compact_values["service_prices_array"], "patient_category_id_to_pay" => 0, "total_amount_pay" => $compact_values["total_amount_pay"], "staff_guarantor_to_pay" => "", "patient_unpaid_debts" => 0, "donor_to_pay" => 0, "balance" => 0, "hospital_to_pay" => "", "patient_to_pay" => $compact_values["patient_to_pay"], "patient_category_to_pay" => 0, "return_payment_methods" => [], "treatment_balance" => 0, "item_table" => "services", "ward_discount_amount" => 0, "discounts_applied" => $compact_values["discounts_applied"], "patient_account_pay_with" => $compact_values["patient_account_pay_with"], "family_account_pay_with" => $compact_values["family_account_pay_with"], "cashier" => $compact_values["cashier"] ]; session()->put('print_cashier_receipt_pdf_details', $data); return redirect('/patient_finance/make_deposit_other_services'); } } /** * Get price for service in consideration of insurance * * @param Request $request * @return string */ public function get_service_cost(Request $request){ $patient_id = $request->patient_id; $service_id = $request->service_id; $price_list_category_id = $request->price_list_category; $response_text = ""; // get the service from table $service = Services::where(['id' => $service_id])->first(); if ($price_list_category_id && $price_list_category_id != 0){ $price_list_category = is_null($service->price_list_category) ? [] : explode(',', $service->price_list_category); $price_list_price = is_null($service->price_list_price) ? [] : explode(',', $service->price_list_price); $key = array_search($price_list_category_id, $price_list_category); $service_price = isset($price_list_price[$key]) ? $price_list_price[$key] : 0; } else { $service_price = $service->non_insured_price; } $response_text .= ""; return $response_text; } /** * Get the donor discount prices * * @param Request $request * @return string */ public function get_discount_cost(Request $request){ $discount_id = $request->discount_id; $discount_category = DiscountCategories::find($discount_id); $patient_to_pay = $discount_category->patient_amount; $donor_to_pay = $discount_category->donor_amount; $discount_type = $discount_category->discount_type; $donor_id = $discount_category->donor_id; return $patient_to_pay . ',' . $donor_to_pay . ',' . $discount_type . ',' . $donor_id; } /** * Save debt plan information * * @param Request $request * @return int */ public function save_debt_plan(Request $request){ $new_debt_plan = new DebtPlan; $tag_id = $request->tag_id; if ($tag_id == 6 || $tag_id == 8 || $tag_id == 7 || $tag_id == 10) { //Consultations / Services $income_account = 7; } elseif ($tag_id == 3 || $tag_id == 1) { //Drugs / Treatments $income_account = 4; } elseif ($tag_id == 5) { //Sundries $income_account = 12; } elseif ($tag_id == 2) { //Investigations $income_account = 11; } elseif ($tag_id == 4) { //Procedures $income_account = 8; } else { $income_account = NULL; } $new_debt_plan->patient_id = $request->patient_id; $new_debt_plan->episode_id = $request->episode_id; $new_debt_plan->amount_owed = $request->amount_owed; $new_debt_plan->date_of_bill = Carbon::createFromFormat('d/m/Y', $request->date_of_bill)->toDateString(); $new_debt_plan->guarantor_type = $request->guarantor_type; $new_debt_plan->staff_guarantor = implode(",", $request->staff_guarantor); $new_debt_plan->staff_guarantor_to_pay = $request->guarantor_to_pay; $new_debt_plan->debt_plan_arrangement = $request->payment_arrangement; $new_debt_plan->first_installment_date = is_null($request->first_installment_date) ? null : Carbon::createFromFormat('d/m/Y', $request->first_installment_date)->toDateString(); $new_debt_plan->second_installment_date = is_null($request->second_installment_date) ? null : Carbon::createFromFormat('d/m/Y', $request->second_installment_date)->toDateString(); $new_debt_plan->third_installment_date = is_null($request->third_installment_date) ? null : Carbon::createFromFormat('d/m/Y', $request->third_installment_date)->toDateString(); $new_debt_plan->fourth_installment_date = is_null($request->fourth_installment_date) ? null : Carbon::createFromFormat('d/m/Y', $request->fourth_installment_date)->toDateString(); $new_debt_plan->comment = $request->comment; $new_debt_plan->authorised_by = $request->authorised_by; $new_debt_plan->tag_id = $request->tag_id; $new_debt_plan->income_account = $income_account; $new_debt_plan->payment_id = 0; $new_debt_plan->created_by = Auth::user()->id; try { if($new_debt_plan->save()){ flash("Debt Plan has been saved successfully")->success(); return $new_debt_plan->id; } else { return 0; } } catch (\Exception | \Error $exception) { return 0; } } /** * Get service items for current episode if they exist * * @param $episode_id * @return mixed */ public static function get_service_items_string($episode_id){ $service_items = ServiceDeposit::where(['episode_id'=>$episode_id])->select('items_ids', 'service_type')->get()->toArray(); return $service_items; } /** * Get service items for current episode from pay later patients * * @param $episode_id * @return mixed */ public static function get_service_items_pay_later_string($episode_id){ $service_items = PatientCategoryInvoice::where(['episode_id'=>$episode_id]) ->whereIn('tag_id', [6, 8]) ->select('items_ids') ->get() ->toArray(); return $service_items; } /** * Get service items for current episode if they exist * * @param $episode_id * @return mixed */ public static function get_inpatient_deposit_string($episode_id){ $service_items = ServiceDeposit::where(['episode_id'=>$episode_id, 'items_ids' => 'Inpatient_Deposit'])->select('items_ids')->get()->toArray(); return $service_items; } public static function get_inpatient_deposit_status($episode_id){ $inpatient_sum = DB::table('service_deposits')->whereNull('deleted_at')->where(['episode_id' => $episode_id,'service_type' => 'Inpatient_Deposit'])->sum('patient_amount_paid'); $inpatient_info = InpatientBill::where(['episode_id' => $episode_id])->first(); if ($inpatient_info) { if ($inpatient_info->amount_to_pay > $inpatient_sum){ return 2; } else { return 1; } } else { return 1; } } /** * Check if patient has a debt and if it is paid or not * * @param $episode_id * @return string */ public static function check_debt($episode_id){ $paid = Debtor::where(['episode_id' => $episode_id, 'payment_status' => 1])->get(); $not_paid = Debtor::where(['episode_id' => $episode_id, 'payment_status' => 0])->get(); if($not_paid->count() > 0){ return 0; } elseif ($paid->count() > 0){ return 1; } else { return null; } } /** * Check if patient episode has a debt plan. * * @param $episode_id * @return string */ public static function check_patient_debt_plan($episode_id){ $paid = DebtPlan::where(['episode_id' => $episode_id, 'balance_remaining' => 0])->count(); $not_paid = DebtPlan::where('episode_id',$episode_id)->where(function ($query) { $query->whereNull('balance_remaining')->orWhere('balance_remaining','>', 0); })->count(); if(!empty($not_paid)) return 0; elseif (!empty($paid)) return 1; else return null; } /** * Check if patient has unpaid sundries * * @param $episode_id * @return mixed */ public static function check_sundry_payment($episode_id){ $paid = DB::table('ordered_sundries')->whereNull('deleted_at')->where(['episode_id' => $episode_id, 'payment_status' => 1])->get(); $not_paid = DB::table('ordered_sundries')->whereNull('deleted_at')->where(['episode_id' => $episode_id, 'payment_status' => 0, 'inpatient_bill_generated' => 0])->get(); if(count($not_paid) > 0){ return 0; } elseif (count($paid) > 0){ return 1; } else { return null; } } /** * Check if patient has unpaid sundries * * @param $episode_id * @return mixed */ public static function check_optical_payment($episode_id){ $paid = DB::table('ordered_eye_glasses')->whereNull('deleted_at')->where(['episode_id' => $episode_id, 'payment_status' => 1])->get(); $not_paid = DB::table('ordered_eye_glasses')->whereNull('deleted_at')->where(['episode_id' => $episode_id, 'payment_status' => 0])->get(); if(count($not_paid) > 0){ return 0; } elseif (count($paid) > 0){ return 1; } else { return null; } } /** * Check if patient has unpaid investigations * * @param $episode_id * @return mixed */ public static function check_investigations_payment($episode_id){ $paid = DB::table('ordered_investigations')->whereNull('deleted_at')->where(['episode_id' => $episode_id, 'payment_status' => 1, 'inpatient_bill_generated' => 0])->get(); $not_paid = DB::table('ordered_investigations')->whereNull('deleted_at')->where(['episode_id' => $episode_id, 'payment_status' => 0, 'inpatient_bill_generated' => 0])->get(); if(count($not_paid) > 0){ return 0; } elseif (count($paid) > 0){ return 1; } else { return null; } } /** * Check if patient has unpaid prescriptions * * @param $episode_id * @return mixed */ public static function check_prescription_payment($episode_id){ $treatments = DB::table('treatments')->whereNull('deleted_at')->where(['episode_id' => $episode_id, 'inpatient_bill_generated' => 0])->get(); $status = null; if(count($treatments) > 0){ foreach ($treatments as $treatment) { if ($treatment->tta == 0) { if ($treatment->payment_status == 1) { $status = 1; } else if ($treatment->payment_status == 0) { $status = 0; break; } } } } return $status; } /** * Check if patient has unpaid procedures * * @param $episode_id * @return mixed */ public static function check_procedures_payment($episode_id){ $paid = DB::table('ordered_procedures')->whereNull('deleted_at')->where(['episode_id' => $episode_id, 'payment_status' => 1])->get(); $not_paid = DB::table('ordered_procedures')->whereNull('deleted_at')->where(['episode_id' => $episode_id, 'payment_status' => 0, 'inpatient_bill_generated' => 0])->get(); if(count($not_paid) > 0){ return 0; } elseif (count($paid) > 0){ return 1; } else { return null; } } /** * Make deposits using central billing * * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View */ public function make_deposit_central_billing(Request $request){ if (isset($request->price_list_category_id) && $request->price_list_category_id != 0){ $price_list_set = $request->price_list_category_id; } else { $price_list_set = false; } $price_list_categories = PriceListCategories::select('patient_category_id', 'id')->get(); $patient_id = session()->get('patient_id'); $episode_id = session()->get('episode_id'); $episode = PatientEpisode::find($episode_id); $patient = Patient::find($patient_id); $staff_guarantors = DB::table('users')->pluck('first_name', 'id')->toArray(); foreach ($staff_guarantors as $key => $value) { $full_name = $value . " " . get_name($key, "id", "last_name", "users"); $staff_guarantors[$key] = $full_name; } $categories = DB::table('patient_categories')->where('available', 1)->pluck('name', 'id'); $drug_categories = DB::table('drug_categories')->get(); $marital_statuses = DB::table('marital_statuses')->pluck('name', 'id'); $payment_arrangement = DB::table('debt_plan_arrangements')->pluck('name', 'id')->toArray(); $payment_arrangement = ['' => '- select -'] + $payment_arrangement; $discount = PatientDiscount::where(['patient_category'=>$patient->category_id])->first(); $discount_options = DiscountCategories::where(['patient_category' => $patient->category_id])->pluck('name','id')->toArray(); $discount_options = ['' => '- Select Discount -'] + $discount_options; // get services $service_items = Services::where('available', 1)->orderby('name','asc')->get(); // check for any used services that were recorded from th clinical side $used_services = OrderedService::where(['patient_id' => $episode->patient_id, 'episode_id' => $episode->id, 'payment_status' => 0 ,'inpatient_bill_generated' => 0])->get(); // get procedures $ordered_procedures = OrderedProcedure::where(['episode_id' => $episode_id,'payment_status' => '0','inpatient_bill_generated' => 0])->get(); // get investigations $ordered_investigations = OrderedInvestigation::where(['episode_id' => $episode_id,'payment_status' => '0', 'inpatient_bill_generated' => 0])->get(); // get ordered treatment $treatments = Treatment::where(['payment_status' => 0, 'episode_id' => $episode_id, 'inpatient_bill_generated' => 0])->get(); // get sundries $ordered_sundries = OrderedSundry::where(['episode_id' => $episode_id,'payment_status' => '0' ,'inpatient_bill_generated' => 0])->get(); // get eyeglasses $ordered_optics = OrderedEyeGlasses::where(['episode_id' => $episode_id,'payment_status' => '0'])->get(); $paid_service_items = []; $paid_services = ServiceDeposit::where(['episode_id' => $episode_id])->orderBy('created_at', 'desc')->get(); $invoiced_services = PatientCategoryInvoice::where('episode_id', $episode_id)->whereIn('tag_id', [6, 8])->get(); foreach ($paid_services as $service) { $paid_service_items = array_merge($paid_service_items, explode(",", $service->items_ids)); } foreach ($invoiced_services as $service) { $paid_service_items = array_merge($paid_service_items, explode(",", $service->items_ids)); } $central_billing_receipts = CentralBillingDeposits::where('episode_id', $episode_id)->groupBy('receipt_number')->get(['receipt_number', 'patient_amount_paid', 'created_at', 'created_by']); $patient_payment_methods = PatientPaymentMethod::pluck('name', 'id'); $patient_payment_methods_options = ""; foreach ($patient_payment_methods as $key => $value) { $patient_payment_methods_options .= ''; } $patient_unpaid_items_array = explode(",", $this->does_patient_have_unpaid_items($patient_id, $episode_id)); $patient_unpaid_items = $patient_unpaid_items_array[0]; $patient_debts = $patient_unpaid_items_array[1]; $non_staff_guarantors = DB::table('non_staff_guarantors')->pluck('first_name', 'id')->toArray(); foreach ($non_staff_guarantors as $key => $value) { $full_guarantor_name = $value . " " . get_name($key, "id", "last_name", "non_staff_guarantors"); $non_staff_guarantors[$key] = $full_guarantor_name; } return view('patient_finance::patient_finance.make_deposit_central_billing',compact('patient_id','episode_id', 'paid_service_items', 'patient_payment_methods', 'patient_payment_methods_options', 'patient','episode','categories','drug_categories','marital_statuses','discount','discount_options', 'ordered_sundries', 'used_services', 'central_billing_receipts', 'patient_unpaid_items', 'payment_arrangement','staff_guarantors', 'service_items', 'ordered_procedures', 'ordered_investigations', 'treatments', 'price_list_set', 'price_list_categories', 'patient_debts', 'non_staff_guarantors', 'ordered_optics')); } public function central_billing_receipt_details($receipt_number) { $tags = [ 1 => 'Drugs', 2 => 'Investigations', 3 => 'Treatments', 4 => 'Procedures', 5 => 'Sundries', 6 => 'Consultation', 8 => 'Services', 16 => 'Optical Items' ]; $tags_parent_tables = [ 1 => 'drugs', 2 => 'investigations', 3 => 'drugs', 4 => 'procedures', 5 => 'sundries', 6 => 'services', 8 => 'services', 16 => 'eye_glasses' ]; $html_text = ""; $html_text .= ""; $html_text .= ""; $html_text .= ""; $html_text .= ""; $html_text .= ""; $central_billing_payments = DB::table('central_billing_deposits') ->where('receipt_number', $receipt_number)->get(); foreach($central_billing_payments as $item){ $html_text .= ""; $html_text .= ""; $html_text .= ""; $item_ids = explode(",", $item->items_ids); $item_amounts = explode(",", $item->items_amounts); $items_quantity = explode(",", $item->items_quantity); for($z = 0; $z < count($item_ids); $z++){ if($item_ids[$z] != 0) { $html_text .= ""; $html_text .= ""; $html_text .= ""; $html_text .= ""; $html_text .= ""; } } } $html_text .= "
Name Quantity Amount

" . $tags[$item->tag_id] ."

" . get_name($item_ids[$z], 'id', 'name', $tags_parent_tables[$item->tag_id]) . "" . ($items_quantity[$z] ?? "") . "" . ugandan_shillings($item_amounts[$z]) . "
"; return json_encode([ "html" => $html_text ]); } public function reprint_central_billing($receipt_number) { // check if track_receipts has details $receipt_json = get_name((int)$receipt_number, 'id', 'receipt_json', 'track_receipts'); if ($receipt_json) { $compact_values = json_decode($receipt_json, true); $compact_values["patient"] = Patient::find($compact_values["patient"]["id"]); $compact_values["cashier"] = $compact_values["cashier"] ?? Auth::id(); $compact_values["hospital_information"] = HospitalInformation::first(); } else { // details to be used for receipt $hospital_information = HospitalInformation::first(); $service_prices_array = []; $service_ids_array = []; $procedure_ids_array = []; $procedure_amounts_array = []; $investigation_ids_array = []; $investigation_amounts_array = []; $treatment_item = []; $treatment_quantity = []; $treatment_subtotal = []; $sundry_item = []; $sundry_quantity = []; $sundry_subtotal = []; $total_amount_pay = 0; $staff_guarantor_to_pay = 0; $one_off_discount_memo = ""; $one_off_discount = 0; $treatment_balance = 0; $balance = 0; $hospital_to_pay = 0; $patient_category_to_pay = 0; $optic_item = []; $optic_quantity = []; $optic_subtotal = []; $return_payment_methods = []; $patient_to_pay = 0; $central_billing_payments = DB::table('central_billing_deposits') ->where('receipt_number', $receipt_number)->get(); foreach($central_billing_payments as $item){ $item_ids = explode(",", $item->items_ids); $item_amounts = explode(",", $item->items_amounts); $items_quantity = explode(",", $item->items_quantity); $patient_to_pay += $item->patient_amount_paid; $patient_category_to_pay += $item->patient_category_amount_paid; for ($i = 0; $i < count($item_ids); $i++) { $total_amount_pay += $item_amounts[$i]; if($item_ids[$i] != 0) { switch ($item->tag_id) { case 1: case 3: // Drugs $treatment_item[] = $item_ids[$i]; $treatment_quantity[] = $items_quantity[$i]; $treatment_subtotal[] = $item_amounts[$i]; break; case 2: // Investigations $investigation_ids_array[] = $item_ids[$i]; $investigation_amounts_array[] = $item_amounts[$i]; break; case 4: // Procedures $procedure_ids_array[] = $item_ids[$i]; $procedure_amounts_array[] = $item_amounts[$i]; break; case 5: // Sundries $sundry_item[] = $item_ids[$i]; $sundry_quantity[] = $items_quantity[$i]; $sundry_subtotal[] = $item_amounts[$i]; break; case 6: case 8: // Consultation $service_prices_array[] = $item_amounts[$i]; $service_ids_array[] = $item_ids[$i]; break; case 19: // Optics $optic_item[] = $item_ids[$i]; $optic_quantity[] = $items_quantity[$i]; $optic_subtotal[] = $item_amounts[$i]; break; } } } } $patient = Patient::find($central_billing_payments[0]->patient_id); $receipt_date = $central_billing_payments[0]->created_at; $patient_category_id_to_pay = $patient->category_id; $patient_account_pay_with = 0; $family_account_pay_with = 0; $is_invoice = $patient_category_to_pay > 0; $cashier = $central_billing_payments[0]->created_by; $compact_values = compact('hospital_information', 'receipt_date', 'receipt_number', 'patient', 'is_invoice', 'service_prices_array', 'service_ids_array', 'patient_account_pay_with', 'family_account_pay_with', 'procedure_ids_array', 'procedure_amounts_array', 'investigation_ids_array', 'investigation_amounts_array', 'treatment_item', 'treatment_quantity', 'treatment_subtotal', 'total_amount_pay', 'staff_guarantor_to_pay', 'one_off_discount_memo', 'one_off_discount', 'sundry_item', 'sundry_quantity', 'sundry_subtotal', 'treatment_balance', 'balance', 'hospital_to_pay', 'patient_to_pay', 'patient_category_to_pay', 'patient_category_id_to_pay', 'optic_item', 'optic_quantity', 'optic_subtotal', 'return_payment_methods', 'cashier'); } if (is_cashier_receipt_type_print_html()) { if ($receipt_json) { return view('patient_finance::patient_finance.receipts.central_billing', $compact_values); } else { return view('patient_finance::patient_finance.receipts.central_billing_reprint', $compact_values); } } else { // so we first have to set a session and then go back to the payment page // on the payment page we can then set a JS variable that can help redirect us to our pdf print session()->put('print_cashier_receipt_pdf', 1); session()->put('print_cashier_receipt_pdf_details', $compact_values); session()->put('is_central_billing_receipt', 1); return redirect('/patient_finance/make_deposit_central_billing'); } } public function store_deposit_central_billing(Request $request){ // reset the services to remove 0 items $pre_service_ids = $request->service_id; foreach ($pre_service_ids as $key => $value) { if ($value == 0) { unset($pre_service_ids[$key]); } } $request->service_id = array_values($pre_service_ids); $wallets_amount = 0; if ($request->has('patient_account_pay_with')) { if ($request->patient_account_pay_with > $request->patient_account_balance) { // amount is more than is on the patient account flash('Patient account balance is less than the current bill.')->error(); return redirect()->back()->withInput(); } $wallets_amount += $request->patient_account_pay_with; } if ($request->has('family_account_pay_with')) { $this_patient_to_pay = $request->family_account_pay_with; $this_family_account_balance = $request->family_account_balance; $family_account_credit_limit = $request->family_account_credit_limit; $wallets_amount += $request->family_account_pay_with; if (!is_family_consumption_allowed($this_patient_to_pay, $this_family_account_balance, $family_account_credit_limit)) { return redirect()->back()->withInput(); } } $request->merge(['wallets_amount' => $wallets_amount]); //check if patient is a dependant of another patient e.g staff with a discount. If so, let Neo enter the matrix if ($request->has('dependants_balance')) { $original_dependants_balance = $request->dependants_balance; //make the main patient's patient category override the category of the dependant $patient_is_a_dependant_of = patient_is_a_dependant_of($request->patient_id); $main_patient_patient_category = get_name($patient_is_a_dependant_of, "id", "category_id", "patients"); $request->merge(['patient_category_id' => $main_patient_patient_category]); //consume item by item to figure out the new category_to_pay values if ($request->has('services_category_to_pay')) { //if new dependants balance is -ve, make it zero $new_dependants_balance = $request->dependants_balance < 0 ? 0 : $request->dependants_balance; $request->merge(['dependants_balance' => $new_dependants_balance]); //now do this $original_services_cat_to_pay = $request->services_category_to_pay; if($request->services_category_to_pay > $request->dependants_balance){ $services_category_to_pay = $request->services_category_to_pay - $request->dependants_balance; } else{ $services_category_to_pay = 0; } $request->merge(['services_category_to_pay' => $services_category_to_pay]); //recalculate the dependants balance $new_dependants_balance = $request->dependants_balance - $original_services_cat_to_pay; //if new dependants balance is -ve, make it zero $new_dependants_balance = $new_dependants_balance < 0 ? 0 : $new_dependants_balance; $request->merge(['dependants_balance' => $new_dependants_balance]); } if ($request->has('procedures_category_to_pay')) { $original_procedures_cat_to_pay = $request->procedures_category_to_pay; if($request->procedures_category_to_pay > $request->dependants_balance){ $procedures_category_to_pay = $request->procedures_category_to_pay - $request->dependants_balance; } else{ $procedures_category_to_pay = 0; } $request->merge(['procedures_category_to_pay' => $procedures_category_to_pay]); //recalculate the dependants balance $new_dependants_balance = $request->dependants_balance - $original_procedures_cat_to_pay; //if new dependants balance is -ve, make it zero $new_dependants_balance = $new_dependants_balance < 0 ? 0 : $new_dependants_balance; $request->merge(['dependants_balance' => $new_dependants_balance]); } if ($request->has('investigations_category_to_pay')) { $original_invs_cat_to_pay = $request->investigations_category_to_pay; if($request->investigations_category_to_pay > $request->dependants_balance){ $investigations_category_to_pay = $request->investigations_category_to_pay - $request->dependants_balance; } else{ $investigations_category_to_pay = 0; } $request->merge(['investigations_category_to_pay' => $investigations_category_to_pay]); //recalculate the dependants balance $new_dependants_balance = $request->dependants_balance - $original_invs_cat_to_pay; //if new dependants balance is -ve, make it zero $new_dependants_balance = $new_dependants_balance < 0 ? 0 : $new_dependants_balance; $request->merge(['dependants_balance' => $new_dependants_balance]); } if ($request->has('treatment_category_to_pay')) { $original_treatment_cat_to_pay = $request->treatment_category_to_pay; if($request->treatment_category_to_pay > $request->dependants_balance){ $treatment_category_to_pay = $request->treatment_category_to_pay - $request->dependants_balance; } else{ $treatment_category_to_pay = 0; } $request->merge(['treatment_category_to_pay' => $treatment_category_to_pay]); //recalculate the dependants balance $new_dependants_balance = $request->dependants_balance - $original_treatment_cat_to_pay; //if new dependants balance is -ve, make it zero $new_dependants_balance = $new_dependants_balance < 0 ? 0 : $new_dependants_balance; $request->merge(['dependants_balance' => $new_dependants_balance]); } if ($request->has('sundries_category_to_pay')) { $original_sundries_cat_to_pay = $request->sundries_category_to_pay; if($request->sundries_category_to_pay > $request->dependants_balance){ $sundries_category_to_pay = $request->sundries_category_to_pay - $request->dependants_balance; } else{ $sundries_category_to_pay = 0; } $request->merge(['sundries_category_to_pay' => $sundries_category_to_pay]); //recalculate the dependants balance $new_dependants_balance = $request->dependants_balance - $original_sundries_cat_to_pay; //if new dependants balance is -ve, make it zero $new_dependants_balance = $new_dependants_balance < 0 ? 0 : $new_dependants_balance; $request->merge(['dependants_balance' => $new_dependants_balance]); } if ($request->has('optics_category_to_pay')) { $original_optics_cat_to_pay = $request->optics_category_to_pay; if($request->optics_category_to_pay > $request->dependants_balance){ $optics_category_to_pay = $request->optics_category_to_pay - $request->dependants_balance; } else{ $optics_category_to_pay = 0; } $request->merge(['optics_category_to_pay' => $optics_category_to_pay]); //recalculate the dependants balance $new_dependants_balance = $request->dependants_balance - $original_optics_cat_to_pay; //if new dependants balance is -ve, make it zero $new_dependants_balance = $new_dependants_balance < 0 ? 0 : $new_dependants_balance; $request->merge(['dependants_balance' => $new_dependants_balance]); } //recalculate the patient category to pay if ($request->patient_category_to_pay > $original_dependants_balance) { if ($original_dependants_balance < 0) { $patient_category_to_pay = $request->patient_category_to_pay; } else{ $patient_category_to_pay = $request->patient_category_to_pay - $original_dependants_balance; } } else { $patient_category_to_pay = 0; } $request->merge(['patient_category_to_pay' => $patient_category_to_pay]); } $deposit = new CentralBillingDeposit; // begin transaction DB::beginTransaction(); try { // send data to be saved in respective tables and get the is_invoice flag and receipt number $return_data_array = explode(",", $deposit->storePayment($request)); // all good so we can commit DB::commit(); } catch (\Exception | \Error $exception) { DB::rollback(); DB::table('system_errors')->insert([ 'error_message' => $exception->getMessage(), 'error_trace' => $exception->getTraceAsString(), 'logged_in_user' => Auth::id() ]); flash("An error occurred. Please try again or contact Stre@mline Support")->error(); return back()->withInput(); } // is_invoice flag $is_invoice = $return_data_array[0]; // receipt number $receipt_number = $return_data_array[1]; if ($request->has('patient_account_pay_with')) { record_patient_accounts_consumption($request->patient_id, $request->episode_id, $request->patient_account_pay_with, 11, $receipt_number); } if ($request->has('family_account_pay_with')) { record_family_consumption($request->patient_id, $request->episode_id, $request->family_account_id, $request->family_account_pay_with, 'Central Billing', $receipt_number); } // for the staff guarantor, if debt plan id is not 0, add receipt number to it if ($request->debt_plan_id != 0) { DB::table('debt_plan')->where('id', $request->debt_plan_id) ->update(['receipt_number' => $receipt_number]); } // details to be used for receipt $hospital_information = HospitalInformation::first(); $total_amount_pay = $request->total_amount_pay; $staff_guarantor_to_pay = $request->staff_guarantor_to_pay; $patient_to_pay = $request->patient_to_pay; $hospital_to_pay = $request->hospital_to_pay_general_discount; $balance = $request->balance; $patient_category_to_pay = $request->patient_category_to_pay; $patient_category_id_to_pay = $request->patient_category_id; $one_off_discount = $request->one_off_discount; $one_off_discount_memo = $request->one_off_discount_memo; $patient_account_pay_with = $request->patient_account_pay_with ?? 0; $family_account_pay_with = $request->family_account_pay_with ?? 0; $amount_paid_from_wallets = $patient_account_pay_with + $family_account_pay_with; $patient_category_id = get_name($request->patient_id, "id", "category_id", "patients"); $does_patient_category_have_threshold = does_patient_category_have_threshold($patient_category_id); $patient_is_a_dependant_of = patient_is_a_dependant_of($request->patient_id); // save for service arrays $service_prices_array = []; $service_ids_array = []; if(count($request->service_id) > 0 && $request->service_id[0] != 0){ $service_prices_array = $request->service_amount; $service_ids_array = $request->service_id; $services_prices_array_summation = array_sum($service_prices_array); //record patient dependants consumption for services if ($does_patient_category_have_threshold || !is_null($patient_is_a_dependant_of)) { $this->record_dependant_consumption($request, $patient_is_a_dependant_of, $request->service_id, $service_prices_array, $services_prices_array_summation, 6, "Dependant-Consultation", $receipt_number); } } // save for procedures $procedure_ids_array = []; $procedure_amounts_array = []; if($request->procedure_ids){ $procedure_ids_array = explode(",", $request->procedure_ids); $procedure_amounts_array = $request->procedure_amount; $procedure_amounts_array_summation = array_sum($procedure_amounts_array); //record patient dependants consumption for procedures if ($does_patient_category_have_threshold || !is_null($patient_is_a_dependant_of)) { $this->record_dependant_consumption($request, $patient_is_a_dependant_of, $procedure_ids_array, $procedure_amounts_array, $procedure_amounts_array_summation, 4, "Dependant-Procedures", $receipt_number); } } // save for investigations $investigation_ids_array = []; $investigation_amounts_array = []; if($request->investigation_ids){ $investigation_ids_array = explode(",", $request->investigation_ids); $investigation_amounts_array = $request->investigation_amount; $investigation_amounts_array_summation = array_sum($investigation_amounts_array); //record patient dependants consumption for investigations if ($does_patient_category_have_threshold || !is_null($patient_is_a_dependant_of)) { $this->record_dependant_consumption($request, $patient_is_a_dependant_of, $investigation_ids_array, $investigation_amounts_array, $investigation_amounts_array_summation, 2, "Dependant-Investigations", $receipt_number); } } // save for treatment $treatment_item = []; $treatment_quantity = []; $treatment_subtotal = []; if($request->treatment_item){ $treatment_item = $request->treatment_item; $treatment_quantity = $request->treatment_quantity; $treatment_subtotal = $request->treatment_subtotal; $treatment_subtotal_array_summation = array_sum($treatment_subtotal); //record patient dependants consumption for treatment if ($does_patient_category_have_threshold || !is_null($patient_is_a_dependant_of)) { $this->record_dependant_consumption($request, $patient_is_a_dependant_of, $request->treatment_item, $treatment_subtotal, $treatment_subtotal_array_summation, 3, "Dependant-Treatment", $receipt_number); } } // save for sundries $sundry_item = []; $sundry_quantity = 0; $sundry_subtotal = 0; if($request->sundry_ids){ $sundry_item = explode(",", $request->sundry_ids); $sundry_quantity = $request->sundry_quantity; $sundry_subtotal = $request->sundry_subtotal; $sundry_subtotal_array_summation = array_sum($sundry_subtotal); //record patient dependants consumption for sundries if ($does_patient_category_have_threshold || !is_null($patient_is_a_dependant_of)) { $this->record_dependant_consumption($request, $patient_is_a_dependant_of, $sundry_item, $sundry_subtotal, $sundry_subtotal_array_summation, 5, "Dependant-Sundries", $receipt_number); } } // save for optics $optic_item = []; $optic_quantity = 0; $optic_subtotal = 0; if($request->optic_ids){ $optic_item = explode(",", $request->optic_ids); $optic_quantity = $request->optic_quantity; $optic_subtotal = $request->optic_subtotal; $optic_subtotal_array_summation = array_sum($optic_subtotal); //record patient dependants consumption for sundries if ($does_patient_category_have_threshold || !is_null($patient_is_a_dependant_of)) { $this->record_dependant_consumption($request, $patient_is_a_dependant_of, $optic_item, $optic_subtotal, $optic_subtotal_array_summation, 19, "Optical Items", $receipt_number); } } $patient = Patient::find($request->patient_id); $transaction_date = $request->transaction_date ?? date('Y-m-d'); $receipt_date = ($transaction_date != 0) ? $transaction_date . " " . date('H:i:s') : date('Y-m-d H:i:s'); $treatment_balance = $request->treatment_balance; $return_payment_methods = $this->register_payment_method($request->patient_id, $request->episode_id, $request->original_cash_to_pay, $request->payment_method, $request->payment_methods_amount, $request->ref_number, $request->ref_number_added_by, $receipt_number, 11, Auth::id(), $amount_paid_from_wallets); $cashier = Auth::id(); $compact_values = compact('hospital_information', 'receipt_date', 'receipt_number', 'patient', 'is_invoice', 'service_prices_array', 'service_ids_array', 'procedure_ids_array', 'procedure_amounts_array', 'investigation_ids_array', 'investigation_amounts_array', 'treatment_item', 'treatment_quantity', 'treatment_subtotal', 'family_account_pay_with', 'total_amount_pay', 'staff_guarantor_to_pay', 'one_off_discount_memo', 'one_off_discount', 'sundry_item', 'sundry_quantity', 'sundry_subtotal', 'treatment_balance', 'patient_account_pay_with', 'cashier', 'balance', 'hospital_to_pay', 'patient_to_pay', 'patient_category_to_pay', 'patient_category_id_to_pay', 'return_payment_methods', 'optic_item', 'optic_quantity', 'optic_subtotal'); $this->save_receipt_json($receipt_number, json_encode($compact_values)); if (is_cashier_receipt_type_print_html()) { return redirect('/patient_finance/reprint_central_billing/' . $receipt_number); } else { // so we first have to set a session and then go back to the payment page // on the payment page we can then set a JS variable that can help redirect us to our pdf print session()->put('print_cashier_receipt_pdf', 1); session()->put('print_cashier_receipt_pdf_details', $compact_values); session()->put('is_central_billing_receipt', 1); return redirect('/patient_finance/make_deposit_central_billing'); } } public function getServiceCostCentralBilling(Request $request): string { $patient_id = $request->patient_id; $service_id = $request->service_id; $price_list_category_id = $request->price_list_category; $response_text = ""; // get the service from table $service = Services::where(['id' => $service_id])->first(); if ($price_list_category_id && $price_list_category_id != 0){ $price_list_category = is_null($service->price_list_category) ? [] : explode(',', $service->price_list_category); $price_list_price = is_null($service->price_list_price) ? [] : explode(',', $service->price_list_price); $key = array_search($price_list_category_id, $price_list_category); $service_price = $price_list_price[$key] ?? 0; } else { $service_price = $service->non_insured_price; } $response_text .= ""; $response_text .= ""; $response_text .= ""; return $response_text; } public function print_central_billing() { $hospitalInfo = HospitalInformation::find(1); $patient_id = session()->get('patient_id'); $episode_id = session()->get('episode_id'); $episode = PatientEpisode::find($episode_id); $patient = Patient::find($patient_id); $staff_guarantors = DB::table('users')->pluck('first_name', 'id')->toArray(); foreach ($staff_guarantors as $key => $value) { $full_name = $value . " " . get_name($key, "id", "last_name", "users"); $staff_guarantors[$key] = $full_name; } $categories = DB::table('patient_categories')->where('available', 1)->pluck('name', 'id'); $drug_categories = DB::table('drug_categories')->get(); $marital_statuses = DB::table('marital_statuses')->pluck('name', 'id'); $payment_arrangement = DB::table('debt_plan_arrangements')->pluck('name', 'id')->toArray(); $payment_arrangement = ['' => '- select -'] + $payment_arrangement; $discount = PatientDiscount::where(['patient_category'=>$patient->category_id])->first(); $discount_options = DiscountCategories::where(['patient_category' => $patient->category_id])->pluck('name','id')->toArray(); $discount_options = ['' => '- select -'] + $discount_options; // get services $service_items = Services::where('available', 1)->orderby('name','asc')->get(); // check for any used services that were recorded from th clinical side $used_services = OrderedService::where(['patient_id' => $episode->patient_id, 'episode_id' => $episode->id, 'payment_status' => 0])->get(); // get procedures $ordered_procedures = OrderedProcedure::where(['episode_id' => $episode_id,'payment_status' => '0'])->get(); // get investigations $ordered_investigations = OrderedInvestigation::where(['episode_id' => $episode_id,'payment_status' => '0', 'inpatient_bill_generated' => 0])->get(); // get ordered treatment $treatment = Treatment::where(['payment_status' => 0, 'episode_id' => $episode_id])->first(); // get sundries $ordered_sundries = OrderedSundry::where(['episode_id' => $episode_id,'payment_status' => '0'])->get(); // get eyeglasses $ordered_optics = OrderedEyeGlasses::where(['episode_id' => $episode_id,'payment_status' => '0'])->get(); // check for doctor's consultation $doc_consultation_id = get_consultation_service_id_allocated_at_episode_consultation($patient_id, $episode_id); $paid_service_items = []; $paid_services = ServiceDeposit::where(['episode_id' => $episode_id])->orderBy('created_at', 'desc')->get(); $invoiced_services = PatientCategoryInvoice::where('episode_id', $episode_id)->whereIn('tag_id', [6, 8])->get(); foreach ($paid_services as $service) { $paid_service_items = array_merge($paid_service_items, explode(",", $service->items_ids)); } foreach ($invoiced_services as $service) { $paid_service_items = array_merge($paid_service_items, explode(",", $service->items_ids)); } if (in_array($doc_consultation_id, $paid_service_items)) { $doc_consultation_id = false; } $data = [ 'patient_id' => $patient_id, 'episode_id' => $episode_id, 'doc_consultation_id' => $doc_consultation_id, 'patient' => $patient, 'episode' => $episode, 'categories' => $categories, 'drug_categories' => $drug_categories, 'marital_statuses' => $marital_statuses, 'discount' => $discount, 'discount_options' => $discount_options, 'ordered_sundries' => $ordered_sundries, 'used_services' => $used_services, 'hospitalInfo' => $hospitalInfo, 'payment_arrangement' => $payment_arrangement, 'staff_guarantors' => $staff_guarantors, 'service_items' => $service_items, 'ordered_procedures' => $ordered_procedures, 'ordered_investigations' => $ordered_investigations, 'ordered_optics' => $ordered_optics, 'treatment' => $treatment, ]; $pdf = SnappyPDF::loadView("patient_finance::patient_finance.print_central_billing", $data) ->setOrientation('portrait') ->setPaper('a4') ->setOption('margin-bottom', 5) ->setOption('margin-top', 5) ->setOption('footer-html', 'Printed On ' . date('d-M-Y h:ia') . ''); return $pdf->inline('Patient Billing.pdf'); } public function make_inpatient_deposit(){ $patient_id = session()->get('patient_id'); $episode_id = session()->get('episode_id'); $inpatient_bill = InpatientBill::where(['patient_id' => $patient_id,'episode_id' => $episode_id]) ->select('id', 'amount_to_pay', 'updated_at', 'inpatient_info_id')->first(); $is_bill_updated = false; try { $inpatient_info_updated = get_name($inpatient_bill->inpatient_info_id, 'id', 'updated_at', 'inpatient_info'); $is_bill_updated = !Carbon::parse($inpatient_info_updated)->greaterThan($inpatient_bill->updated_at); } catch (\Exception) {} $patient = Patient::find($patient_id); $discount = PatientDiscount::where(['patient_category'=>$patient->category_id])->first(); $inpatient_deposits = ServiceDeposit::where(['patient_id' => $patient_id,'episode_id' => $episode_id,'service_type' => 'Inpatient_Deposit'])->get(); $ward_discount_deposits = InpatientWardDiscounts::where(['patient_id' => $patient_id,'episode_id' => $episode_id])->get(); $debt_plan_payments = DebtPlan::where(['patient_id' => $patient_id,'episode_id' => $episode_id, 'tag_id' => 10])->get(); $inpatient_category_payments = PatientCategoryInvoice::where('patient_id', $patient_id) ->where('episode_id', $episode_id) ->whereIn('tag_id', [10,16]) ->get(); $patient_payment_methods = PatientPaymentMethod::pluck('name', 'id'); $patient_payment_methods_options = ""; foreach ($patient_payment_methods as $key => $value) { $patient_payment_methods_options .= ''; } $payment_arrangement = DB::table('debt_plan_arrangements')->pluck('name', 'id')->toArray(); $payment_arrangement = ['' => '- select -'] + $payment_arrangement; $staff_guarantors = DB::table('users')->pluck('first_name', 'id')->toArray(); foreach ($staff_guarantors as $key => $value) { $full_name = $value . " " . get_name($key, "id", "last_name", "users"); $staff_guarantors[$key] = $full_name; } $non_staff_guarantors = DB::table('non_staff_guarantors')->pluck('first_name', 'id')->toArray(); foreach ($non_staff_guarantors as $key => $value) { $full_guarantor_name = $value . " " . get_name($key, "id", "last_name", "non_staff_guarantors"); $non_staff_guarantors[$key] = $full_guarantor_name; } $chi_to_pay = 0; $uncleared_chi_amount = 0; if ($inpatient_bill) { // get the insurance amount to be paid $insurance_claims = DB::table('insurance_claims')->where('inpatient_outpatient', 1) ->where('order_id', $inpatient_bill->id)->whereNull('deleted_at')->get(); foreach($insurance_claims as $claim) { $item_ids = explode(",", $claim->item_ids); $tariff_amounts = explode(",", $claim->tariff_amounts); $item_authorisations = explode(",", $claim->is_item_authorisation_required); $plan_limit_error = explode(",", $claim->plan_limit_error); $benefit_waiting_period_error = explode(",", $claim->benefit_waiting_period_error); $benefit_limit_error = explode(",", $claim->benefit_limit_error); $item_limit_error = explode(",", $claim->item_limit_error); for ($i = 0; $i < count($item_ids); $i++) { if ($item_authorisations[$i] == 1 || $claim->is_authorisation_required == 1 || $claim->plan_validity_error == 1 || $benefit_waiting_period_error[$i] == 1 || $plan_limit_error[$i] == 1 || $benefit_limit_error[$i] == 1 || $item_limit_error[$i] == 1) { $uncleared_chi_amount += $tariff_amounts[$i]; } else { $chi_to_pay += $tariff_amounts[$i]; } } } } return view('patient_finance::patient_finance.make_inpatient_deposit', compact('inpatient_bill', 'inpatient_deposits', 'patient', 'discount', 'ward_discount_deposits', 'inpatient_category_payments', 'patient_payment_methods_options', 'payment_arrangement', 'staff_guarantors', 'patient_id', 'episode_id', 'debt_plan_payments', 'non_staff_guarantors', 'uncleared_chi_amount', 'chi_to_pay', 'is_bill_updated')); } public function store_inpatient_deposit(Request $request){ $wallets_amount = $dependant_amount_consumed = 0; if ($request->has('patient_account_pay_with')) { if ($request->patient_account_pay_with > $request->patient_account_balance) { // amount is more than is on the patient account flash('Patient account balance is less than the current bill.')->error(); return redirect()->back()->withInput(); } $wallets_amount += $request->patient_account_pay_with; } if ($request->has('family_account_pay_with')) { $this_patient_to_pay = $request->family_account_pay_with; $this_family_account_balance = $request->family_account_balance; $family_account_credit_limit = $request->family_account_credit_limit; $wallets_amount += $request->family_account_pay_with; if (!is_family_consumption_allowed($this_patient_to_pay, $this_family_account_balance, $family_account_credit_limit)) { return redirect()->back()->withInput(); } } $request->merge(['wallets_amount' => $wallets_amount]); //check if patient is a dependant of another patient e.g main patient with a discount. If so, let Neo enter the matrix $dependant_patient_category_amount_to_invoice = 0; if ($request->has('dependants_balance')) { $original_dependants_balance = $request->dependants_balance; //balance of this dependants group $original_patient_to_pay = $request->original_patient_to_pay; //amount they have paid with cash field //if new dependants balance is -ve, make it zero $new_dependants_balance = $request->dependants_balance < 0 ? 0 : $request->dependants_balance; $request->merge(['dependants_balance' => $new_dependants_balance]); //recalculate the patient category to pay if ($request->patient_category_amount > $original_dependants_balance) { if ($original_dependants_balance < 0) { $dependant_patient_category_amount_to_invoice = $request->patient_category_amount;// - $request->patient_to_pay; } else{ $dependant_patient_category_amount_to_invoice = $request->patient_category_amount - $original_dependants_balance;// - $request->patient_to_pay; } } else { $dependant_patient_category_amount_to_invoice = 0; } //make the main patient's patient category override the category of the dependant $patient_is_a_dependant_of = patient_is_a_dependant_of($request->patient_id); $main_patient_patient_category = get_name($patient_is_a_dependant_of, "id", "category_id", "patients"); $request->merge(['patient_category_id' => $main_patient_patient_category]); //depedant amount consumed $dependant_amount_consumed = $request->patient_category_amount; } // generate receipt number $receipt_number = $this->receiptService->createReceipt("inpatient_deposit"); // for the staff guarantor, if debt plan id is not 0, add receipt number to it if ($request->debt_plan_id != 0) { DB::table('debt_plan')->where('id', $request->debt_plan_id) ->update(['receipt_number' => $receipt_number]); } $patient_id = $request->patient_id; $episode_id = $request->episode_id; $transaction_date = 0; $patient_to_pay = $request->patient_to_pay; $is_invoice = ($request->patient_category_amount) ? 1 : 0; $patient_category_amount = $request->patient_category_amount ?? 0; $patient_account_pay_with = $request->patient_account_pay_with ?? 0; $family_account_pay_with = $request->family_account_pay_with ?? 0; $staff_guarantor_to_pay = $request->staff_guarantor_to_pay ?? 0; $amount_paid_from_wallets = $patient_account_pay_with + $family_account_pay_with; $ward_discount_amount = $request->ward_discount_amount ?? 0; $total_amount_paid = $patient_to_pay + $ward_discount_amount + $patient_category_amount + $staff_guarantor_to_pay; $inpatient_ward_info = InpatientInfo::where(['patient_id' => $patient_id,'episode_id' => $episode_id])->first(); if ($request->has('patient_account_pay_with')) { record_patient_accounts_consumption($patient_id, $episode_id, $request->patient_account_pay_with, 10, $receipt_number); } if ($request->has('family_account_pay_with')) { record_family_consumption($patient_id, $episode_id, $request->family_account_id, $request->family_account_pay_with, 'Inpatient Deposits', $receipt_number); } if ($patient_to_pay > 0) { // create a new service payment record DepositHelpers::service_payment($patient_id,$episode_id,'Inpatient_Deposit',$patient_to_pay,'Inpatient_Deposit',$receipt_number,$patient_to_pay,'Inpatient_Deposit', $request->wallets_amount); } // update the amount_to_pay $inpatient_bill = InpatientBill::where(['patient_id' => $patient_id,'episode_id' => $episode_id])->first(); if ($inpatient_bill) { $previous_amount_to_pay = $inpatient_bill->amount_to_pay; $balance_amount_to_pay = $previous_amount_to_pay - $total_amount_paid; $inpatient_bill->amount_to_pay = $balance_amount_to_pay; $inpatient_bill->chi_to_pay = $request->chi_to_pay ?? 0; $inpatient_bill->uncleared_chi_amount = $request->uncleared_chi_amount ?? 0; $inpatient_bill->amount_paid = $inpatient_bill->amount_paid + $patient_to_pay; $inpatient_bill->amount_paid = ($dependant_amount_consumed > 0) ? ($inpatient_bill->amount_paid + $dependant_amount_consumed) : $inpatient_bill->amount_paid; //dependants $inpatient_bill->invoices_amount = $inpatient_bill->invoices_amount + ($ward_discount_amount + $patient_category_amount + $staff_guarantor_to_pay); $inpatient_bill->save(); } else { $inpatient_bill = new InpatientBill(); $inpatient_bill->amount_paid = $patient_to_pay; $inpatient_bill->amount_paid = ($dependant_amount_consumed > 0) ? ($inpatient_bill->amount_paid + $dependant_amount_consumed) : $inpatient_bill->amount_paid; //dependants $inpatient_bill->inpatient_info_id = $inpatient_ward_info->id; $inpatient_bill->patient_id = $patient_id; $inpatient_bill->episode_id = $episode_id; $inpatient_bill->amount_to_pay = 0; $inpatient_bill->save(); $previous_amount_to_pay = 0; $balance_amount_to_pay = 0; $total_amount_paid = 0; } if ($ward_discount_amount > 0) { $inpatient_ward_discount = new InpatientWardDiscounts(); $inpatient_ward_discount->patient_id = $patient_id; $inpatient_ward_discount->episode_id = $episode_id; $inpatient_ward_discount->inpatient_info_id = $inpatient_ward_info->id; $inpatient_ward_discount->ward_id = $inpatient_ward_info->ward_id; $inpatient_ward_discount->amount = $ward_discount_amount; $inpatient_ward_discount->receipt_number = $receipt_number; $inpatient_ward_discount->created_by = Auth::id(); $inpatient_ward_discount->updated_by = Auth::id(); $inpatient_ward_discount->save(); } // save the patient category to pay amount if($request->has('dependants_balance') && ($dependant_amount_consumed > 0)){ $patient_category_id = $request->patient_category_id; //get_name($patient_id, 'id', 'category_id', 'patients'); $transaction_date = $request->transaction_date ?? date('Y-m-d'); DepositHelpers::patient_category_invoice($patient_category_id, $patient_id, $episode_id, $patient_category_amount, $inpatient_ward_info->id,$receipt_number,16,null, $dependant_patient_category_amount_to_invoice, $transaction_date, ''); $dependant_amount_consumed = $patient_category_amount; }elseif ($patient_category_amount > 0) { $patient_category_id = get_name($patient_id, 'id', 'category_id', 'patients'); $transaction_date = $request->transaction_date ?? date('Y-m-d'); DepositHelpers::patient_category_invoice($patient_category_id, $patient_id, $episode_id, $patient_category_amount, $inpatient_ward_info->id,$receipt_number,10,null, $patient_category_amount, $transaction_date, ''); } $this->inpatientFinanceService->split_inpatient_paid_cash_deposit($patient_id, $episode_id, $receipt_number, $patient_to_pay); // update the track_receipt table to include created receipt $track_receipts = new TrackReceipt; $track_receipts->created_by = Auth::id(); $track_receipts->reason = 'Inpatient_Deposit'; $track_receipts->save(); /*====== if the patient is a dependant of a certain patient category patient, do this =====*/ $patient_category_id = get_name($request->patient_id, "id", "category_id", "patients"); $does_patient_category_have_threshold = does_patient_category_have_threshold($patient_category_id); $patient_is_a_dependant_of = patient_is_a_dependant_of($request->patient_id); if ($does_patient_category_have_threshold || !is_null($patient_is_a_dependant_of)) { $tag_id = 10; $dependant_amount_consumed = $patient_category_amount; $this->record_dependant_consumption($request, $patient_is_a_dependant_of, ["Inpatient-deposit"], [$patient_category_amount], $dependant_amount_consumed, $tag_id, "Dependant-Services", $receipt_number); //update the amount to pay $total_amount_consumed = 0; $this_inpatient_episode_consumptions = DependantsConsumption::where(['dependant_patient_id' => $patient_id, 'episode_id' => $episode_id, 'items_ids' => "Inpatient-deposit"])->get(); if (count($this_inpatient_episode_consumptions) > 0) { foreach ($this_inpatient_episode_consumptions as $inpatient_consumption) { $total_amount_consumed += $inpatient_consumption->amount_consumed; } $updated_amt_to_pay = $inpatient_bill->original_bill - $total_amount_consumed; } } /*===============end of that dependant functionality =======================*/ // details to be used for receipt $hospital_information = HospitalInformation::first(); $patient = Patient::find($patient_id); $receipt_date = ($transaction_date != 0) ? $transaction_date . " " . date('H:i:s') : date('Y-m-d H:i:s'); $item_name = ($is_invoice == 1) ? "Inpatient Invoice Amount" : "Inpatient Cash Deposit"; $return_payment_methods = $this->register_payment_method($patient_id, $episode_id, $request->original_cash_to_pay, $request->payment_method, $request->payment_methods_amount, $request->ref_number, $request->ref_number_added_by, $receipt_number, 10, Auth::id(), $amount_paid_from_wallets); flash("Inpatient deposit made successful")->success(); $compact_values = compact('hospital_information', 'patient_to_pay', 'patient', 'receipt_date', 'receipt_number', 'item_name', 'is_invoice', 'previous_amount_to_pay', 'ward_discount_amount', 'patient_category_amount', 'return_payment_methods', 'patient_account_pay_with', 'family_account_pay_with', 'staff_guarantor_to_pay', 'total_amount_paid', 'balance_amount_to_pay'); $this->save_receipt_json($receipt_number, json_encode($compact_values)); if (is_cashier_receipt_type_print_html()) { return redirect('/patient_finance/print_inpatient_deposit_receipt/' . $receipt_number); } else { // so we first have to set a session and then go back to the payment page // on the payment page we can then set a JS variable that can help redirect us to our pdf print session()->put('print_cashier_receipt_pdf', 1); // since we want to reduce the amount of duplicate code, we shall send all items to one file for printing $data = [ "hospital_information" => $compact_values["hospital_information"], "receipt_date" => $compact_values["receipt_date"], "receipt_number" => $compact_values["receipt_number"], "one_off_discount_memo" => 0, "one_off_discount" => 0, "patient" => $compact_values["patient"], "is_invoice" => $compact_values["is_invoice"], "items" => [$compact_values["item_name"]], "item_quantity" => [1], "item_subtotal" => [$total_amount_paid], "patient_category_id_to_pay" => $patient->category_id, "total_amount_pay" => $previous_amount_to_pay, "staff_guarantor_to_pay" => $compact_values["staff_guarantor_to_pay"], "patient_unpaid_debts" => 0, "donor_to_pay" => 0, "balance" => $balance_amount_to_pay, "family_account_pay_with" => $family_account_pay_with, "hospital_to_pay" => 0, "patient_to_pay" => $compact_values["patient_to_pay"], "patient_category_to_pay" => $compact_values["patient_category_amount"], "return_payment_methods" => $compact_values["return_payment_methods"], "treatment_balance" => 0, "item_table" => "inpatient", "ward_discount_amount" => $compact_values["ward_discount_amount"], "discounts_applied" => 0, "patient_account_pay_with" => $patient_account_pay_with ]; session()->put('print_cashier_receipt_pdf_details', $data); return redirect('/patient_finance/make_inpatient_deposit'); } } public function print_inpatient_deposit_receipt($receipt_number){ $patient_id = session()->get('patient_id'); $patient = Patient::find($patient_id); $is_invoice = 0; // check if track_receipts has details $receipt_json = get_name((int)$receipt_number, 'id', 'receipt_json', 'track_receipts'); if ($receipt_json) { $compact_values = json_decode($receipt_json, true); $compact_values["patient"] = Patient::find($compact_values["patient"]["id"]); $compact_values["hospital_information"] = HospitalInformation::first(); $compact_values["discounts_applied"] = 0; $compact_values["item_name"] = $compact_values["item_name"] ?? "Inpatient Amount"; $compact_values["is_invoice"] = $compact_values["is_invoice"] ?? 0; $compact_values["total_amount_paid"] = $compact_values["total_amount_paid"] ?? $compact_values["patient_to_pay"]; $compact_values["previous_amount_to_pay"] = $compact_values["previous_amount_to_pay"] ?? $compact_values["patient_to_pay"]; $compact_values["balance_amount_to_pay"] = $compact_values["balance_amount_to_pay"] ?? ($compact_values["previous_amount_to_pay"] - $compact_values["patient_to_pay"]); } else { $patient_to_pay = ServiceDeposit::where('receipt_number', $receipt_number)->sum('patient_amount_paid'); $ward_discount_amount = InpatientWardDiscounts::where('receipt_number', $receipt_number)->sum('amount'); $patient_category_amount = PatientCategoryInvoice::where('receipt_number', $receipt_number)->sum('patient_amount'); if ($patient_to_pay > 0) { $receipt_date = get_name($receipt_number, 'receipt_number', 'created_at', 'service_deposits'); } elseif($ward_discount_amount > 0) { $receipt_date = get_name($receipt_number, 'receipt_number', 'created_at', 'inpatient_ward_discounts'); } elseif($patient_category_amount > 0) { $is_invoice = 1; $receipt_date = get_name($receipt_number, 'receipt_number', 'created_at', 'patient_category_invoices'); } else { $receipt_date = date('Y-m-d H:i:s'); } $total_amount_paid = $patient_to_pay + $ward_discount_amount + $patient_category_amount; $hospital_information = HospitalInformation::first(); $return_payment_methods = []; $patient_account_pay_with = 0; $family_account_pay_with = 0; $previous_amount_to_pay = 0; $balance_amount_to_pay = 0; $item_name = ($is_invoice == 1) ? "Inpatient Invoice Amount" : "Inpatient Cash Deposit"; $compact_values = compact('hospital_information', 'patient_to_pay', 'patient', 'receipt_date', 'receipt_number', 'item_name', 'is_invoice', 'balance_amount_to_pay', 'ward_discount_amount', 'patient_category_amount', 'return_payment_methods', 'item_name', 'patient_account_pay_with', 'family_account_pay_with', 'total_amount_paid', 'previous_amount_to_pay'); } if (is_cashier_receipt_type_print_html()) { return view('patient_finance::patient_finance.receipts.inpatient_deposit', $compact_values); } else { // so we first have to set a session and then go back to the payment page // on the payment page we can then set a JS variable that can help redirect us to our pdf print session()->put('print_cashier_receipt_pdf', 1); // since we want to reduce the amount of duplicate code, we shall send all items to one file for printing $data = [ "hospital_information" => $compact_values["hospital_information"], "receipt_date" => $compact_values["receipt_date"], "receipt_number" => $compact_values["receipt_number"], "one_off_discount_memo" => 0, "one_off_discount" => 0, "patient" => $compact_values["patient"], "is_invoice" => $compact_values["is_invoice"], "items" => [$compact_values["item_name"]], "item_quantity" => [1], "item_subtotal" => [$compact_values["total_amount_paid"]], "patient_category_id_to_pay" => $patient->category_id, "total_amount_pay" => $compact_values["previous_amount_to_pay"], "staff_guarantor_to_pay" => 0, "patient_unpaid_debts" => 0, "donor_to_pay" => 0, "balance" => $compact_values["balance_amount_to_pay"], "patient_account_pay_with" => $compact_values["patient_account_pay_with"], "family_account_pay_with" => $compact_values["family_account_pay_with"], "hospital_to_pay" => 0, "patient_to_pay" => $compact_values["patient_to_pay"], "patient_category_to_pay" => $compact_values["patient_category_amount"], "return_payment_methods" => $compact_values["return_payment_methods"], "treatment_balance" => 0, "item_table" => "inpatient", "ward_discount_amount" => $compact_values["ward_discount_amount"], "discounts_applied" => 0 ]; session()->put('print_cashier_receipt_pdf_details', $data); return redirect('/patient_finance/make_inpatient_deposit'); } } public static function register_payment_method($patient_id, $episode_id, $original_cash_to_pay, $payment_method, $payment_methods_amount, $ref_number, $ref_number_added_by, $receipt_number, $tag_id, $user_id, $amount_paid_from_wallets): array { $not_cash_to_pay = 0; // paid be other methods $return_payment_methods = []; if (!is_null($payment_method) && count($payment_method) > 0 && $payment_method[0] != 0) { for ($i = 0; $i < count($payment_method); $i++) { if (isset($payment_method[$i]) && isset($payment_methods_amount[$i]) && $payment_method[$i] != 0 && $payment_methods_amount[$i] != 0) { $not_cash_to_pay += $payment_methods_amount[$i]; $return_payment_methods[$payment_method[$i]] = $payment_methods_amount[$i]; $payment_method_trans = new PaymentMethodsTransaction(); $payment_method_trans->patient_id = $patient_id; $payment_method_trans->episode_id = $episode_id; $payment_method_trans->receipt_number = $receipt_number; $payment_method_trans->tag_id = $tag_id; $payment_method_trans->payment_method_id = $payment_method[$i]; $payment_method_trans->amount = $payment_methods_amount[$i]; $payment_method_trans->ref_number = $ref_number[$i]; $payment_method_trans->ref_number_added_by = $ref_number_added_by[$i]; $payment_method_trans->created_by = $user_id; $payment_method_trans->save(); } } $cash_to_pay = $original_cash_to_pay - $not_cash_to_pay; } else { $cash_to_pay = $original_cash_to_pay; } // since some cash was got from the patient wallets, subtract that amount $cash_to_pay -= $amount_paid_from_wallets; // input for cash if ($cash_to_pay > 0) { $payment_method_trans = new PaymentMethodsTransaction(); $payment_method_trans->patient_id = $patient_id; $payment_method_trans->episode_id = $episode_id; $payment_method_trans->receipt_number = $receipt_number; $payment_method_trans->tag_id = $tag_id; $payment_method_trans->payment_method_id = 0; $payment_method_trans->amount = $cash_to_pay; $payment_method_trans->created_by = $user_id; $payment_method_trans->save(); $return_payment_methods[0] = $cash_to_pay; } return $return_payment_methods; } public function customer_episode_statement($episode_id) { $hospital_information = HospitalInformation::find(1); $patient_id = session()->get('patient_id'); $episode = PatientEpisode::find($episode_id); $patient = Patient::find($patient_id); // check for any used services that were recorded from th clinical side $used_services = OrderedService::where(['patient_id' => $episode->patient_id, 'episode_id' => $episode->id, 'payment_status' => 0, 'inpatient_bill_generated' => 0])->get(); // get procedures $ordered_procedures = OrderedProcedure::where(['episode_id' => $episode_id,'payment_status' => '0', 'inpatient_bill_generated' => 0])->get(); // get investigations $ordered_investigations = OrderedInvestigation::where(['episode_id' => $episode_id,'payment_status' => '0', 'inpatient_bill_generated' => 0])->get(); // get ordered treatment $treatments = Treatment::where(['payment_status' => 0, 'episode_id' => $episode_id])->get(); // get sundries $ordered_sundries = OrderedSundry::where(['episode_id' => $episode_id,'payment_status' => '0', 'inpatient_bill_generated' => 0])->get(); // get payments $paid_services = ServiceDeposit::where('episode_id', $episode_id) ->where('patient_id', $patient_id) ->where('service_type', '!=', 'Inpatient_Deposit') ->get(); $paid_procedures = ProcedureDeposit::where('episode_id', $episode_id) ->where('patient_id', $patient_id) ->get(); $paid_investigations = InvestigationDeposit::where('episode_id', $episode_id) ->where('patient_id', $patient_id) ->get(); $paid_treatments = TreatmentDeposits::where('episode_id', $episode_id) ->where('patient_id', $patient_id) ->get(); $paid_sundries = SundryDeposit::where('episode_id', $episode_id) ->where('patient_id', $patient_id) ->get(); $invoiced_services = PatientCategoryInvoice::where('episode_id', $episode_id) ->whereIn('tag_id', [6,7,8]) ->get(); $invoiced_procedures = PatientCategoryInvoice::where('episode_id', $episode_id) ->where('tag_id', 4) ->get(); $invoiced_investigations = PatientCategoryInvoice::where('episode_id', $episode_id) ->where('tag_id', 2) ->get(); $invoiced_treatments = PatientCategoryInvoice::where('episode_id', $episode_id) ->where('tag_id', 3) ->get(); $invoiced_sundries = PatientCategoryInvoice::where('episode_id', $episode_id) ->where('tag_id', 5) ->get(); // get inpatient fees $inpatient_info = InpatientBill::where(['patient_id'=>$patient_id, 'episode_id'=>$episode_id])->first(['amount_to_pay', 'original_bill']); if ($inpatient_info) { $inpatient_to_pay = $inpatient_info->original_bill; } else { $inpatient_to_pay = 0; } // get inpatient fees paid $inpatient_deposits = ServiceDeposit::where(['patient_id' => $patient_id,'episode_id' => $episode_id,'service_type' => 'Inpatient_Deposit'])->get(); // check for doctor's consultation $doc_consultation_id = get_consultation_service_id_allocated_at_episode_consultation($patient_id, $episode_id); $is_doc_consul_fee_paid = is_doc_consul_fee_paid($episode_id, $doc_consultation_id); $patient_debts = DB::table('debtors') ->where('patient_id', $patient_id) ->where('episode_id', $episode_id) ->get(); $categories = DB::table('patient_categories')->where('available', 1)->pluck('name', 'id'); $data = [ 'patient_id' => $patient_id, 'episode_id' => $episode_id, 'patient' => $patient, 'episode' => $episode, 'used_services' => $used_services, 'paid_services' => $paid_services, 'invoiced_services' => $invoiced_services, 'paid_sundries' => $paid_sundries, 'invoiced_sundries' => $invoiced_sundries, 'ordered_sundries' => $ordered_sundries, 'hospital_information' => $hospital_information, 'ordered_procedures' => $ordered_procedures, 'paid_procedures' => $paid_procedures, 'invoiced_procedures' => $invoiced_procedures, 'ordered_investigations' => $ordered_investigations, 'paid_investigations' => $paid_investigations, 'invoiced_investigations' => $invoiced_investigations, 'treatments' => $treatments, 'paid_treatments' => $paid_treatments, 'invoiced_treatments' => $invoiced_treatments, 'inpatient_deposits' => $inpatient_deposits, 'doc_consultation_id' => $doc_consultation_id, 'is_doc_consul_fee_paid' => $is_doc_consul_fee_paid, 'inpatient_to_pay' => $inpatient_to_pay, 'patient_debts' => $patient_debts, 'categories' => $categories, ]; $pdf = SnappyPDF::loadView("patient_finance::patient_finance.customer_episode_statement", $data) ->setOrientation('portrait') ->setPaper('a4') ->setOption('margin-bottom', 5) ->setOption('margin-top', 5) ->setOption('footer-html', 'Printed On ' . date('d-M-Y h:ia') . ''); return $pdf->inline('Customer Episode Statement.pdf'); } public function customer_statement() { $patient_id = session()->get('patient_id'); $patient = Patient::find($patient_id); // check for any used services that were recorded from th clinical side $used_services = OrderedService::where(['patient_id' => $patient_id, 'payment_status' => 0, 'inpatient_bill_generated' => 0])->get(); // get procedures $ordered_procedures = OrderedProcedure::where(['patient_id' => $patient_id,'payment_status' => '0', 'inpatient_bill_generated' => 0])->get(); // get investigations $ordered_investigations = OrderedInvestigation::where(['patient_id' => $patient_id,'payment_status' => '0', 'inpatient_bill_generated' => 0])->get(); // get ordered treatment $treatments = Treatment::where(['payment_status' => 0, 'patient_id' => $patient_id])->get(); // get sundries $ordered_sundries = OrderedSundry::where(['patient_id' => $patient_id,'payment_status' => '0', 'inpatient_bill_generated' => 0])->get(); // get payments $paid_services = ServiceDeposit::where('patient_id', $patient_id) ->where('service_type', '!=', 'Inpatient_Deposit') ->get(); $paid_procedures = ProcedureDeposit::where('patient_id', $patient_id) ->get(); $paid_investigations = InvestigationDeposit::where('patient_id', $patient_id) ->get(); $paid_treatments = TreatmentDeposits::where('patient_id', $patient_id) ->get(); $paid_sundries = SundryDeposit::where('patient_id', $patient_id) ->get(); $invoiced_services = PatientCategoryInvoice::where('patient_id', $patient_id) ->whereIn('tag_id', [6,7,8]) ->get(); $invoiced_procedures = PatientCategoryInvoice::where('patient_id', $patient_id) ->where('tag_id', 4) ->get(); $invoiced_investigations = PatientCategoryInvoice::where('patient_id', $patient_id) ->where('tag_id', 2) ->get(); $invoiced_treatments = PatientCategoryInvoice::where('patient_id', $patient_id) ->where('tag_id', 3) ->get(); $invoiced_sundries = PatientCategoryInvoice::where('patient_id', $patient_id) ->where('tag_id', 5) ->get(); // get inpatient fees $inpatient_infos = InpatientBill::where(['patient_id'=>$patient_id])->get(['amount_to_pay', 'original_bill']); $inpatient_to_pay = 0; foreach ($inpatient_infos as $inpatient_info) { $inpatient_to_pay += $inpatient_info->original_bill; } // get inpatient fees paid $inpatient_deposits = ServiceDeposit::where(['patient_id' => $patient_id, 'service_type' => 'Inpatient_Deposit'])->get(); // check for doctor's consultation $patient_episodes = DB::table('patient_episodes')->where('patient_id', $patient_id) ->get(['id']); $doc_consultation_id = []; foreach ($patient_episodes as $patient_episode) { $episode_id = $patient_episode->id; $current_doc_consultation_id = get_consultation_service_id_allocated_at_episode_consultation($patient_id, $episode_id); if (!is_doc_consul_fee_paid($episode_id, $current_doc_consultation_id)) { $doc_consultation_id[] = $current_doc_consultation_id; } } $patient_debts = DB::table('debtors') ->where('patient_id', $patient_id) ->get(); $data = [ 'patient_id' => $patient_id, 'patient' => $patient, 'used_services' => $used_services, 'paid_services' => $paid_services, 'invoiced_services' => $invoiced_services, 'paid_sundries' => $paid_sundries, 'invoiced_sundries' => $invoiced_sundries, 'ordered_sundries' => $ordered_sundries, 'ordered_procedures' => $ordered_procedures, 'paid_procedures' => $paid_procedures, 'invoiced_procedures' => $invoiced_procedures, 'ordered_investigations' => $ordered_investigations, 'paid_investigations' => $paid_investigations, 'invoiced_investigations' => $invoiced_investigations, 'treatments' => $treatments, 'paid_treatments' => $paid_treatments, 'invoiced_treatments' => $invoiced_treatments, 'inpatient_deposits' => $inpatient_deposits, 'doc_consultation_id' => $doc_consultation_id, 'inpatient_to_pay' => $inpatient_to_pay, 'patient_debts' => $patient_debts, ]; $pdf = SnappyPDF::loadView("patient_finance::patient_finance.customer_statement", $data) ->setOrientation('portrait') ->setPaper('a4') ->setOption('margin-bottom', 5) ->setOption('margin-top', 5) ->setOption('footer-html', 'Printed On ' . date('d-M-Y h:ia') . ''); return $pdf->inline('Customer Statement.pdf'); } public function does_patient_have_unpaid_items($patient_id, $current_episode_id) { $return_values = "none"; $total_debts = 0; $patient_episodes = DB::table('patient_episodes')->where('patient_id', $patient_id) ->whereNotIn('id', [$current_episode_id]) ->get(['id', 'patient_id']); foreach ($patient_episodes as $episode) { $used_services = OrderedService::where(['patient_id' => $episode->patient_id, 'episode_id' => $episode->id, 'payment_status' => 0])->get(); $investigations = $this->check_investigations_payment($episode->id); $prescription = $this->check_prescription_payment($episode->id); $procedures_paid = $this->check_procedures_payment($episode->id); $sundries_paid = $this->check_sundry_payment($episode->id); if (count($used_services) > 0 || $investigations === 0 || $prescription === 0 || $procedures_paid === 0 || $sundries_paid === 0) { $return_values = "some bills"; break; } // handle the debts $debts = Debtor::where(['episode_id' => $episode->id, 'payment_status' => 0])->get(); foreach ($debts as $debt) { if ($debt->balance_remaining) { $total_debts += $debt->balance_remaining; } else { $total_debts += $debt->balance; } } } if (is_patient_debt_reminder_enabled()) { return $return_values . "," . $total_debts; } else { return "none,0"; } } public function pay_all_patient_bills($patient_id) { $patient = Patient::find($patient_id); $discount = PatientDiscount::where(['patient_category'=>$patient->category_id])->first(); if (isset($discount->pay_later) && $discount->pay_later == 1) { flash("Pay later patients can not be billed from collective bills.")->success(); return redirect('/patient_finance/home'); } // check for any used services that were recorded from th clinical side $ordered_services = OrderedService::where(['patient_id' => $patient_id, 'payment_status' => 0])->get()->toArray(); // get procedures $ordered_procedures = OrderedProcedure::where(['patient_id' => $patient_id,'payment_status' => '0'])->get(); // get investigations $ordered_investigations = OrderedInvestigation::where(['patient_id' => $patient_id,'payment_status' => '0', 'inpatient_bill_generated' => 0])->get(); // get ordered treatment $treatments = Treatment::where(['patient_id' => $patient_id, 'payment_status' => 0])->get(); // get sundries $ordered_sundries = OrderedSundry::where(['patient_id' => $patient_id,'payment_status' => '0'])->get(); $patient_episodes = DB::table('patient_episodes')->where('patient_id', $patient_id)->get(['id', 'created_at']); $doctor_consuls = []; foreach ($patient_episodes as $patient_episode) { $doc_consultation_id = get_consultation_service_id_allocated_at_episode_consultation($patient_id, $patient_episode->id); if ($doc_consultation_id != null && !is_doc_consul_fee_paid($patient_episode->id, $doc_consultation_id)) { $ordered_services[] = [ // make id negative to separate it from the ordered services "id" => -$patient_episode->id, "episode_id" => $patient_episode->id, "service_id" => $doc_consultation_id, "quantity" => 1, "created_at" => $patient_episode->created_at, ]; } } $patient_payment_methods = PatientPaymentMethod::pluck('name', 'id'); $patient_payment_methods_options = ""; foreach ($patient_payment_methods as $key => $value) { $patient_payment_methods_options .= ''; } // get inpatient fees $inpatient_infos = InpatientBill::where(['patient_id'=>$patient_id])->get(['amount_to_pay', 'updated_at']); $patient_debts = DB::table('debtors') ->where('patient_id', $patient_id) ->get(); return view('patient_finance::patient_finance.pay_all_patient_bills', compact('patient_id', 'patient', 'ordered_services', 'ordered_sundries', 'patient_debts', 'ordered_procedures', 'ordered_investigations', 'treatments', 'doctor_consuls', 'patient_payment_methods_options', 'inpatient_infos')); } public function save_pay_all_patient_bills(Request $request) { $patient_id = session()->get('patient_id'); if ($request->has('family_account_id') && !is_null($request->family_account_id)) { $this_patient_to_pay = $request->patient_to_pay; $this_family_account_balance = $request->family_account_balance; if (($this_patient_to_pay > $this_family_account_balance) && !can_family_accounts_consume_more_than_balance()) { flash('Family account balance is less than the current bill.')->error(); return redirect()->back(); } } $deposit = new CollectiveBillsDeposit; // send data to be saved in respective tables and get the receipt number $receipt_number = $deposit->store_payment($request); // details to be used for receipt $hospital_information = HospitalInformation::first(); $total_amount_pay = $request->total_amount_pay; $patient_to_pay = $request->patient_to_pay; $hospital_to_pay = $request->hospital_to_pay_general_discount; $balance = $request->balance; $one_off_discount = $request->one_off_discount; $one_off_discount_memo = $request->one_off_discount_memo; $service_prices_array = []; $service_ids_array = []; $procedure_ids_array = []; $procedure_amounts_array = []; $investigation_ids_array = []; $investigation_amounts_array = []; $treatment_item = []; $treatment_quantity = []; $treatment_subtotal = []; $sundry_item = []; $sundry_quantity = []; $sundry_subtotal = []; $inpatient_to_pay = 0; $debt_to_pay = 0; // save for service arrays if($request->service_id){ $service_prices_array = $request->service_amount; $service_ids_array = $request->service_id; $services_prices_array_summation = array_sum($service_prices_array); if ($request->has('family_account_id') && !is_null($request->family_account_id)) { record_family_consumption($patient_id, 0, $request->family_account_id, $services_prices_array_summation, 'Services', $receipt_number); } } // save for procedures if($request->procedure_id){ $procedure_ids_array = $request->procedure_id; $procedure_amounts_array = $request->procedure_amount; $procedure_amounts_array_summation = array_sum($procedure_amounts_array); if ($request->has('family_account_id') && !is_null($request->family_account_id)) { record_family_consumption($patient_id, 0, $request->family_account_id, $procedure_amounts_array_summation, 'Procedures', $receipt_number); } } // save for investigations if($request->investigation_ids){ $investigation_ids_array = $request->investigation_ids; $investigation_amounts_array = $request->investigation_amount; $investigation_amounts_array_summation = array_sum($investigation_amounts_array); if ($request->has('family_account_id') && !is_null($request->family_account_id)) { record_family_consumption($patient_id, 0, $request->family_account_id, $investigation_amounts_array_summation, 'Investigations', $receipt_number); } } // save for treatment if($request->treatment_id){ $treatment_item = $request->drug_id; $treatment_quantity = $request->treatment_quantity; $treatment_subtotal = $request->drug_amount; $treatment_subtotal_array_summation = array_sum($treatment_subtotal); if ($request->has('family_account_id') && !is_null($request->family_account_id)) { record_family_consumption($patient_id, 0, $request->family_account_id, $treatment_subtotal_array_summation, 'Treatment', $receipt_number); } } // save for sundries if($request->sundry_id){ $sundry_item = $request->sundry_id; $sundry_quantity = $request->sundry_quantity; $sundry_subtotal = $request->sundry_subtotal; $sundry_subtotal_array_summation = array_sum($sundry_subtotal); if ($request->has('family_account_id') && !is_null($request->family_account_id)) { record_family_consumption($patient_id, 0, $request->family_account_id, $sundry_subtotal_array_summation, 'Sundries', $receipt_number); } } if (isset($request->inpatient_to_pay) && $request->inpatient_to_pay > 0) { $inpatient_to_pay = $request->inpatient_to_pay; } if (isset($request->debt_to_pay) && $request->debt_to_pay > 0) { $debt_to_pay = $request->debt_to_pay; } $patient = Patient::find($patient_id); $receipt_date = date('Y-m-d H:i:s'); $return_payment_methods = $this->register_payment_method($patient_id, 0, $request->original_cash_to_pay, $request->payment_method, $request->payment_methods_amount, $request->ref_number, $request->ref_number_added_by, $receipt_number, 12, Auth::id(), 0); $compact_values = compact('hospital_information', 'receipt_date', 'receipt_number', 'patient', 'service_prices_array', 'service_ids_array', 'procedure_ids_array', 'procedure_amounts_array', 'investigation_ids_array', 'investigation_amounts_array', 'treatment_item', 'treatment_quantity', 'treatment_subtotal', 'total_amount_pay', 'sundry_item', 'sundry_quantity', 'sundry_subtotal', 'one_off_discount_memo', 'one_off_discount', 'balance', 'hospital_to_pay', 'patient_to_pay', 'return_payment_methods', 'debt_to_pay', 'inpatient_to_pay'); $this->save_receipt_json($receipt_number, json_encode($compact_values)); return view('patient_finance::patient_finance.receipts.collective_bills', $compact_values); } public function print_item_receipt_pdf() { $data = session()->get("print_cashier_receipt_pdf_details"); // add check for when the people try to reload the page if (!$data) { return redirect('/patient_finance/home'); } // lest i forget Thy love for me session()->forget('print_cashier_receipt_pdf'); session()->forget('print_cashier_receipt_pdf_details'); if (session()->get("is_central_billing_receipt") == 1) { // redirect for central billing from here to its file $receipt_file = "patient_finance::patient_finance.receipts.central_billing_pdf"; session()->forget('is_central_billing_receipt'); } else { // normal receipt for the other points $receipt_file = "patient_finance::patient_finance.receipts.print_pdf_receipt"; } $pdf = SnappyPDF::loadView($receipt_file, $data) ->setOrientation('portrait') ->setPaper('a4') ->setOption('margin-bottom', 5) ->setOption('margin-top', 5) ->setOption('footer-html', '© ' . date('Y') . ' Stre@mline'); return $pdf->inline('Patient Receipt' . date(" d-m-y h:ia") . '.pdf'); } public function view_inpatient_bill($patient_id, $episode_id) { session()->put(['patient_id' => $patient_id]); session()->put(['episode_id' => $episode_id]); return redirect('inpatient_billing'); } public function record_dependant_consumption(Request $request, $dependant_of, $items_array, $items_prices_array, $amount_consumed, $tag_id, $reason, $receipt_number) { $unpaid_balance = 0; $balance_of_main_patient = get_balance_from_category_threshold($dependant_of); if ($balance_of_main_patient < 0) { $unpaid_balance = $amount_consumed; } elseif ($amount_consumed > $balance_of_main_patient){ $unpaid_balance = $amount_consumed - $balance_of_main_patient; } $patient_depedant_record = new DependantsConsumption; $patient_depedant_record->dependant_patient_id = $request->patient_id; $patient_depedant_record->episode_id = $request->episode_id; $patient_depedant_record->dependant_patient_category = get_name($request->patient_id, "id", "category_id", "patients"); $patient_depedant_record->main_patient_id = $dependant_of; $patient_depedant_record->main_patient_category = get_name($dependant_of, "id", "category_id", "patients"); $patient_depedant_record->amount_consumed = $amount_consumed; $patient_depedant_record->unpaid_balance = $unpaid_balance; $patient_depedant_record->items_ids = implode(",", $items_array); $patient_depedant_record->items_prices = implode(",", $items_prices_array); $patient_depedant_record->tag_id = $tag_id; $patient_depedant_record->reason = substr($reason, 10); //now remove the substring "Dependant-" used for discounts table $patient_depedant_record->receipt_number = $receipt_number; $patient_depedant_record->created_by = Auth::id(); $patient_depedant_record->save(); //store the amount consumed in the discounts table so that it comes in the P&L report $discount_amount = $amount_consumed-$unpaid_balance; DepositHelpers::discount($discount_amount, $request->patient_id, $request->episode_id, $receipt_number, $reason); } public function save_receipt_json($receipt_number, $receipt_json) { $receipt_id = (int)$receipt_number; $receipt = TrackReceipt::find($receipt_id); if ($receipt) { $receipt->receipt_json = $receipt_json; $receipt->save(); } } /* save new non staff guarantor */ public function save_new_non_staff_guarantor(Request $request) { $new_non_staff_guarantor = new NonStaffGuarantor; $new_non_staff_guarantor->first_name = $request->first_name; $new_non_staff_guarantor->last_name = $request->last_name; $new_non_staff_guarantor->gender = $request->gender; $new_non_staff_guarantor->marital_status = $request->marital_status; $new_non_staff_guarantor->date_of_birth = Carbon::createFromFormat('d/m/Y', $request->date_of_birth)->toDateString(); $new_non_staff_guarantor->contact = $request->contact; $new_non_staff_guarantor->residence = $request->residence; $new_non_staff_guarantor->created_by = Auth::id(); $new_non_staff_guarantor->save(); return $new_non_staff_guarantor->id; } public function dynamic_save_of_services_on_central_billing(Request $request) { $unclean_service_ids_array = $request->service_ids; $already_ordered = DB::table('ordered_services')->where(['patient_id' => $request->patient_id, 'episode_id' => $request->episode_id, 'payment_status' => 0 ,'inpatient_bill_generated' => 0])->first(); if ($already_ordered) { $service_ids_array = explode(",", $already_ordered->service_id); $quantity_array = explode(",", $already_ordered->quantity); $service_amounts_array = explode(",", $already_ordered->service_amount); $service_performed_array = explode(",", $already_ordered->performed); $service_performed_id_array = explode(",", $already_ordered->performed_id); $service_order = Orderedservice::find($already_ordered->id); } else { $quantity_array = $service_performed_array = $service_performed_id_array = $service_amounts_array = $service_ids_array = []; $service_order = new Orderedservice; } for ($i=0; $i < count($unclean_service_ids_array) ; $i++) { if ($unclean_service_ids_array[$i] != 0) { $quantity_array[] = 1; $service_ids_array[] = $unclean_service_ids_array[$i]; $service_amounts_array[] = get_name($unclean_service_ids_array[$i++], 'id', 'non_insured_price', 'services'); $service_performed_array[] = ""; $service_performed_id_array[] = ""; } } if (count($service_ids_array) > 0) { $service_order->patient_id = $request->patient_id; $service_order->episode_id = $request->episode_id; $service_order->payment_status = 0; $service_order->service_id = implode(",", $service_ids_array); $service_order->quantity = implode(",", $quantity_array); $service_order->service_amount = implode(",", $service_amounts_array); $service_order->performed = implode(",", $service_performed_array); $service_order->performed_id = implode(",", $service_performed_id_array); $service_order->created_by = Auth::id(); $service_order->save(); } } public function clean_patient_category_invoices_income_accounts($start, $end) { //$start = "2022-01-01"; //$end = "2022-09-16"; $patient_category_invoices = DB::table('patient_category_invoices')->whereBetween('created_at', [$start, $end])->get(); if (count($patient_category_invoices)) { foreach ($patient_category_invoices as $single_record) { $items_array = explode(",", $single_record->items_ids); $tag_id = $single_record->tag_id; $income_accounts_array = $cost_of_goods_account_array = $inventory_account_array = []; for ($i = 0; $i < count($items_array); $i++) { //Consultations / Services if ($tag_id == 6 || $tag_id == 8 || $tag_id == 7 || $tag_id == 10) { $income_account = get_name($items_array[$i], 'id', 'account_id', 'services'); $income_account = is_numeric($income_account) ? $income_account : 7; $income_accounts_array[] = $income_account; } //Drugs / Treatments elseif ($tag_id == 3 || $tag_id == 1) { $income_account = get_name($items_array[$i], 'id', 'account_id', 'drugs'); $income_account = is_numeric($income_account) ? $income_account : 4; $income_accounts_array[] = $income_account; $inventory_account = get_name($items_array[$i], 'id', 'inventory_account', 'drugs'); $inventory_account_array[] = $inventory_account; $cost_of_goods_account = get_name($items_array[$i], 'id', 'cost_of_goods_account', 'drugs'); $cost_of_goods_account_array[] = $cost_of_goods_account; $cost_price_array[$i] = get_latest_inventory_cost_price($items_array[$i], 1, Carbon::today()->toDateString()); } //Sundries elseif ($tag_id == 5) { $income_account = get_name($items_array[$i], 'id', 'account_id', 'sundries'); $income_account = is_numeric($income_account) ? $income_account : 12; $income_accounts_array[] = $income_account; $inventory_account = get_name($items_array[$i], 'id', 'inventory_account', 'sundries'); $inventory_account_array[] = $inventory_account; $cost_of_goods_account = get_name($items_array[$i], 'id', 'cost_of_goods_account', 'sundries'); $cost_of_goods_account_array[] = $cost_of_goods_account; $cost_price_array[$i] = get_latest_inventory_cost_price($items_array[$i], 2, Carbon::today()->toDateString()); } //Investigations elseif ($tag_id == 2) { $income_account = get_name($items_array[$i], 'id', 'account_id', 'investigations'); $income_account = is_numeric($income_account) ? $income_account : 11; $income_accounts_array[] = $income_account; } //Procedures elseif ($tag_id == 4) { $income_account = get_name($items_array[$i], 'id', 'account_id', 'procedures'); $income_account = is_numeric($income_account) ? $income_account : 8; $income_accounts_array[] = $income_account; } } $income_accounts_string = is_null($income_accounts_array) ? "" : implode(',', $income_accounts_array); $cost_of_goods_accounts_string = is_null($cost_of_goods_account_array) ? null : implode(',', $cost_of_goods_account_array); $inventory_accounts_string = is_null($inventory_account_array) ? null : implode(',', $inventory_account_array); $existing_patient_category_invoice = PatientCategoryInvoice::withTrashed()->find($single_record->id); $existing_patient_category_invoice->income_account = $income_accounts_string; if ($tag_id == 3 || $tag_id == 1 || $tag_id == 5) { $existing_patient_category_invoice->cost_of_goods_account = $cost_of_goods_accounts_string; $existing_patient_category_invoice->inventory_account = $inventory_accounts_string; } $existing_patient_category_invoice->update(); } } return "patient category invoice accounts cleaned"; } public function make_chi_deposits($item_type) { $patient_id = session()->get('patient_id'); $episode_id = session()->get('episode_id'); $patient = Patient::find($patient_id); $service_claims = []; $procedure_claims = []; $investigation_claims = []; $treatment_claims = []; $sundries_claims = []; if ($item_type == 0) { $service_claims = InsuranceClaim::where('episode_id', $episode_id)->where('item_type', 1)->where('claim_status', 1)->get(); $procedure_claims = InsuranceClaim::where('episode_id', $episode_id)->where('item_type', 2)->where('claim_status', 1)->get(); $investigation_claims = InsuranceClaim::where('episode_id', $episode_id)->where('item_type', 3)->where('claim_status', 1)->get(); $treatment_claims = InsuranceClaim::where('episode_id', $episode_id)->where('item_type', 4)->where('claim_status', 1)->get(); $sundries_claims = InsuranceClaim::where('episode_id', $episode_id)->where('item_type', 5)->where('claim_status', 1)->get(); } elseif ($item_type == 1) { $service_claims = InsuranceClaim::where('episode_id', $episode_id)->where('item_type', 1)->where('claim_status', 1)->get(); } elseif ($item_type == 2) { $procedure_claims = InsuranceClaim::where('episode_id', $episode_id)->where('item_type', 2)->where('claim_status', 1)->get(); } elseif ($item_type == 3) { $investigation_claims = InsuranceClaim::where('episode_id', $episode_id)->where('item_type', 3)->where('claim_status', 1)->get(); } elseif ($item_type == 4) { $treatment_claims = InsuranceClaim::where('episode_id', $episode_id)->where('item_type', 4)->where('claim_status', 1)->get(); } elseif ($item_type == 5) { $sundries_claims = InsuranceClaim::where('episode_id', $episode_id)->where('item_type', 5)->where('claim_status', 1)->get(); } $paid_claims = CHIDeposit::where('episode_id', $episode_id)->where('point_of_deposit', $item_type)->get(); $patient_payment_methods = PatientPaymentMethod::pluck('name', 'id'); $patient_payment_methods_options = ""; foreach ($patient_payment_methods as $key => $value) { $patient_payment_methods_options .= ''; } $discount = PatientDiscount::where(['patient_category'=>$patient->category_id])->first(); $payment_arrangement = DB::table('debt_plan_arrangements')->pluck('name', 'id')->toArray(); $payment_arrangement = ['' => '- select -'] + $payment_arrangement; $staff_guarantors = DB::table('users')->pluck('first_name', 'id')->toArray(); $non_staff_guarantors = DB::table('non_staff_guarantors')->pluck('first_name', 'id')->toArray(); foreach ($non_staff_guarantors as $key => $value) { $full_guarantor_name = $value . " " . get_name($key, "id", "last_name", "non_staff_guarantors"); $non_staff_guarantors[$key] = $full_guarantor_name; } return view('patient_finance::patient_finance.make_chi_deposits', compact('patient', 'service_claims', 'payment_arrangement', 'procedure_claims', 'investigation_claims', 'treatment_claims', 'sundries_claims', 'patient_id', 'episode_id', 'patient_payment_methods_options', 'discount','staff_guarantors', 'non_staff_guarantors','item_type', 'paid_claims')); } public function store_chi_deposits(Request $request) { $wallets_amount = 0; if ($request->has('patient_account_pay_with')) { if ($request->patient_account_pay_with > $request->patient_account_balance) { // amount is more than is on the patient account flash('Patient account balance is less than the current bill.')->error(); return redirect()->back()->withInput(); } $wallets_amount += $request->patient_account_pay_with; } if ($request->has('family_account_pay_with')) { $this_patient_to_pay = $request->family_account_pay_with; $this_family_account_balance = $request->family_account_balance; $family_account_credit_limit = $request->family_account_credit_limit; $wallets_amount += $request->family_account_pay_with; if (!is_family_consumption_allowed($this_patient_to_pay, $this_family_account_balance, $family_account_credit_limit)) { return redirect()->back()->withInput(); } } $request->merge(['wallets_amount' => $wallets_amount]); if ($request->has('dependants_balance')) { $original_dependants_balance = $request->dependants_balance; //make the main patient's patient category override the category of the dependant $patient_is_a_dependant_of = patient_is_a_dependant_of($request->patient_id); $main_patient_patient_category = get_name($patient_is_a_dependant_of, "id", "category_id", "patients"); $request->merge(['patient_category_id' => $main_patient_patient_category]); //consume item by item to figure out the new category_to_pay values if ($request->has('services_category_to_pay')) { //if new dependants balance is -ve, make it zero $new_dependants_balance = $request->dependants_balance < 0 ? 0 : $request->dependants_balance; $request->merge(['dependants_balance' => $new_dependants_balance]); //now do this $original_services_cat_to_pay = $request->services_category_to_pay; if($request->services_category_to_pay > $request->dependants_balance){ $services_category_to_pay = $request->services_category_to_pay - $request->dependants_balance; } else{ $services_category_to_pay = 0; } $request->merge(['services_category_to_pay' => $services_category_to_pay]); //recalculate the dependants balance $new_dependants_balance = $request->dependants_balance - $original_services_cat_to_pay; //if new dependants balance is -ve, make it zero $new_dependants_balance = $new_dependants_balance < 0 ? 0 : $new_dependants_balance; $request->merge(['dependants_balance' => $new_dependants_balance]); } if ($request->has('procedures_category_to_pay')) { $original_procedures_cat_to_pay = $request->procedures_category_to_pay; if($request->procedures_category_to_pay > $request->dependants_balance){ $procedures_category_to_pay = $request->procedures_category_to_pay - $request->dependants_balance; } else{ $procedures_category_to_pay = 0; } $request->merge(['procedures_category_to_pay' => $procedures_category_to_pay]); //recalculate the dependants balance $new_dependants_balance = $request->dependants_balance - $original_procedures_cat_to_pay; //if new dependants balance is -ve, make it zero $new_dependants_balance = $new_dependants_balance < 0 ? 0 : $new_dependants_balance; $request->merge(['dependants_balance' => $new_dependants_balance]); } if ($request->has('investigations_category_to_pay')) { $original_invs_cat_to_pay = $request->investigations_category_to_pay; if($request->investigations_category_to_pay > $request->dependants_balance){ $investigations_category_to_pay = $request->investigations_category_to_pay - $request->dependants_balance; } else{ $investigations_category_to_pay = 0; } $request->merge(['investigations_category_to_pay' => $investigations_category_to_pay]); //recalculate the dependants balance $new_dependants_balance = $request->dependants_balance - $original_invs_cat_to_pay; //if new dependants balance is -ve, make it zero $new_dependants_balance = $new_dependants_balance < 0 ? 0 : $new_dependants_balance; $request->merge(['dependants_balance' => $new_dependants_balance]); } if ($request->has('treatment_category_to_pay')) { $original_treatment_cat_to_pay = $request->treatment_category_to_pay; if($request->treatment_category_to_pay > $request->dependants_balance){ $treatment_category_to_pay = $request->treatment_category_to_pay - $request->dependants_balance; } else{ $treatment_category_to_pay = 0; } $request->merge(['treatment_category_to_pay' => $treatment_category_to_pay]); //recalculate the dependants balance $new_dependants_balance = $request->dependants_balance - $original_treatment_cat_to_pay; //if new dependants balance is -ve, make it zero $new_dependants_balance = $new_dependants_balance < 0 ? 0 : $new_dependants_balance; $request->merge(['dependants_balance' => $new_dependants_balance]); } if ($request->has('sundries_category_to_pay')) { $original_sundries_cat_to_pay = $request->sundries_category_to_pay; if($request->sundries_category_to_pay > $request->dependants_balance){ $sundries_category_to_pay = $request->sundries_category_to_pay - $request->dependants_balance; } else{ $sundries_category_to_pay = 0; } $request->merge(['sundries_category_to_pay' => $sundries_category_to_pay]); //recalculate the dependants balance $new_dependants_balance = $request->dependants_balance - $original_sundries_cat_to_pay; //if new dependants balance is -ve, make it zero $new_dependants_balance = $new_dependants_balance < 0 ? 0 : $new_dependants_balance; $request->merge(['dependants_balance' => $new_dependants_balance]); } //recalculate the patient category to pay if ($request->patient_category_to_pay > $original_dependants_balance) { if ($original_dependants_balance < 0) { $patient_category_to_pay = $request->patient_category_to_pay; } else{ $patient_category_to_pay = $request->patient_category_to_pay - $original_dependants_balance; } } else { $patient_category_to_pay = 0; } $request->merge(['patient_category_to_pay' => $patient_category_to_pay]); } $deposit = new CHIDeposits(); // begin transaction DB::beginTransaction(); try { // send data to be saved in respective tables and get the is_invoice flag and receipt number $receipt_number = $deposit->store_payment($request); // all good so we can commit DB::commit(); } catch (\Exception | \Error $exception) { DB::rollback(); DB::table('system_errors')->insert([ 'error_message' => $exception->getMessage(), 'error_trace' => $exception->getTraceAsString(), 'logged_in_user' => Auth::id() ]); flash("An error occurred. Please try again or contact Stre@mline Support")->error(); return back()->withInput(); } if ($request->has('patient_account_pay_with')) { record_patient_accounts_consumption($request->patient_id, $request->episode_id, $request->patient_account_pay_with, 18, $receipt_number); } if ($request->has('family_account_pay_with')) { record_family_consumption($request->patient_id, $request->episode_id, $request->family_account_id, $request->family_account_pay_with, 'CHI Deposits', $receipt_number); } // for the staff guarantor, if debt plan id is not 0, add receipt number to it if ($request->debt_plan_id != 0) { DB::table('debt_plan')->where('id', $request->debt_plan_id) ->update(['receipt_number' => $receipt_number]); } // details to be used for receipt $hospital_information = HospitalInformation::first(); $chi_to_pay = $request->chi_to_pay; $patient_co_payment = $request->patient_co_payment; $patient_to_pay = $request->patient_to_pay; $hospital_to_pay = $request->hospital_to_pay_general_discount; $balance = $request->balance; $patient_category_to_pay = $request->patient_category_to_pay; $patient_category_id_to_pay = $request->patient_category_id; $one_off_discount = $request->one_off_discount; $one_off_discount_memo = $request->one_off_discount_memo; $patient_account_pay_with = $request->patient_account_pay_with ?? 0; $family_account_pay_with = $request->family_account_pay_with ?? 0; $amount_paid_from_wallets = $patient_account_pay_with + $family_account_pay_with; $total_amount_pay = $request->total_amount_pay; $staff_guarantor_to_pay = $request->staff_guarantor_to_pay; $cashier = Auth::id(); $patient_category_id = get_name($request->patient_id, "id", "category_id", "patients"); $does_patient_category_have_threshold = does_patient_category_have_threshold($patient_category_id); $patient_is_a_dependant_of = patient_is_a_dependant_of($request->patient_id); // save for service arrays $service_ids_array = []; $service_quantity = []; $service_co_payment_amount_array = []; $service_chi_amount_array = []; if($request->service_id){ $service_ids_array = $request->service_id; $service_quantity = $request->service_quantity; $service_co_payment_amount_array = $request->service_co_pay; $service_chi_amount_array = $request->service_chi_to_pay; //record patient dependants consumption for services if ($does_patient_category_have_threshold || !is_null($patient_is_a_dependant_of)) { $this->record_dependant_consumption($request, $patient_is_a_dependant_of, $service_ids_array, $service_co_payment_amount_array, array_sum($service_co_payment_amount_array), 6, "Dependant-Consultation", $receipt_number); } } // save for procedures $procedure_ids_array = []; $procedure_co_payment_amount_array = []; $procedure_chi_amount_array = []; if($request->procedure_id){ $procedure_ids_array = $request->procedure_id; $procedure_co_payment_amount_array = $request->procedure_co_pay; $procedure_chi_amount_array = $request->procedure_chi_to_pay; //record patient dependants consumption for procedures if ($does_patient_category_have_threshold || !is_null($patient_is_a_dependant_of)) { $this->record_dependant_consumption($request, $patient_is_a_dependant_of, $procedure_ids_array, $procedure_co_payment_amount_array, array_sum($procedure_co_payment_amount_array), 4, "Dependant-Procedures", $receipt_number); } } // save for investigations $investigation_ids_array = []; $investigation_co_payment_amount_array = []; $investigation_chi_amount_array = []; if($request->investigation_id){ $investigation_ids_array = $request->investigation_id; $investigation_co_payment_amount_array = $request->investigation_co_pay; $investigation_chi_amount_array = $request->investigation_chi_to_pay; //record patient dependants consumption for investigations if ($does_patient_category_have_threshold || !is_null($patient_is_a_dependant_of)) { $this->record_dependant_consumption($request, $patient_is_a_dependant_of, $investigation_ids_array, $investigation_co_payment_amount_array, array_sum($investigation_co_payment_amount_array), 2, "Dependant-Investigations", $receipt_number); } } // save for treatment $treatment_item = []; $treatment_quantity = []; $treatment_co_payment_amount_array = []; $treatment_chi_amount_array = []; if($request->treatment_item_id){ $treatment_item = $request->treatment_item_id; $treatment_quantity = $request->treatment_quantity; $treatment_co_payment_amount_array = $request->treatment_co_pay; $treatment_chi_amount_array = $request->treatment_chi_to_pay; //record patient dependants consumption for treatment if ($does_patient_category_have_threshold || !is_null($patient_is_a_dependant_of)) { $this->record_dependant_consumption($request, $patient_is_a_dependant_of, $treatment_item, $treatment_co_payment_amount_array, array_sum($treatment_co_payment_amount_array), 3, "Dependant-Treatment", $receipt_number); } } // save for sundries $sundry_item = []; $sundry_quantity = []; $sundry_chi_amount_array = []; $sundry_co_payment_amount_array = []; if($request->sundries_id){ $sundry_item = $request->sundries_id; $sundry_quantity = $request->sundries_quantity; $sundry_co_payment_amount_array = $request->sundries_co_pay; $sundry_chi_amount_array = $request->sundries_chi_to_pay; //record patient dependants consumption for sundries if ($does_patient_category_have_threshold || !is_null($patient_is_a_dependant_of)) { $this->record_dependant_consumption($request, $patient_is_a_dependant_of, $sundry_item, $sundry_co_payment_amount_array, array_sum($sundry_co_payment_amount_array), 5, "Dependant-Sundries", $receipt_number); } } $patient = Patient::find($request->patient_id); $receipt_date = date('Y-m-d H:i:s'); $return_payment_methods = $this->register_payment_method($request->patient_id, $request->episode_id, $request->original_cash_to_pay, $request->payment_method, $request->payment_methods_amount, $request->ref_number, $request->ref_number_added_by, $receipt_number, 18, Auth::id(), $amount_paid_from_wallets); $compact_values = compact('hospital_information', 'receipt_date', 'receipt_number', 'patient', 'service_co_payment_amount_array', 'service_ids_array', 'service_chi_amount_array', 'total_amount_pay', 'procedure_ids_array', 'procedure_co_payment_amount_array', 'procedure_chi_amount_array','one_off_discount','one_off_discount_memo', 'investigation_ids_array', 'investigation_co_payment_amount_array', 'investigation_chi_amount_array', 'staff_guarantor_to_pay', 'treatment_item', 'treatment_quantity', 'treatment_co_payment_amount_array', 'treatment_chi_amount_array', 'family_account_pay_with', 'service_quantity', 'chi_to_pay', 'patient_co_payment', 'balance', 'hospital_to_pay', 'patient_to_pay', 'return_payment_methods', 'patient_category_to_pay', 'patient_category_id_to_pay', 'sundry_item', 'sundry_quantity', 'sundry_co_payment_amount_array', 'sundry_chi_amount_array', 'patient_account_pay_with', 'cashier'); $this->save_receipt_json($receipt_number, json_encode($compact_values)); if (is_cashier_receipt_type_print_html()) { return redirect('/patient_finance/print_chi_deposit/' . $receipt_number); } else { // so we first have to set a session and then go back to the payment page // on the payment page we can then set a JS variable that can help redirect us to our pdf print session()->put('print_cashier_receipt_pdf', 1); session()->put('print_cashier_receipt_pdf_details', $compact_values); session()->put('is_central_billing_receipt', 1); return redirect('/patient_finance/make_chi_deposits/' . $request->item_type); } } public function print_chi_deposit($receipt_number) { // check if track_receipts has details $receipt_json = get_name((int)$receipt_number, 'id', 'receipt_json', 'track_receipts'); $compact_values = json_decode($receipt_json, true); $compact_values["patient"] = Patient::find($compact_values["patient"]["id"]); $compact_values["hospital_information"] = HospitalInformation::first(); if (is_cashier_receipt_type_print_html()) { $compact_values["staff_guarantor_to_pay"] = $compact_values["staff_guarantor_to_pay"] ?? 0; return view('patient_finance::patient_finance.receipts.chi_deposit', $compact_values); } else { // so we first have to set a session and then go back to the payment page // on the payment page we can then set a JS variable that can help redirect us to our pdf print session()->put('print_cashier_receipt_pdf', 1); session()->put('print_cashier_receipt_pdf_details', $compact_values); return redirect('/patient_finance/make_chi_deposits/' . get_name($receipt_number, 'receipt_number', 'point_of_deposit', 'chi_deposits')); } } public function print_chi_deposit_pdf() { $data = session()->get("print_cashier_receipt_pdf_details"); // add check for when the people try to reload the page if (!$data) { return redirect('/patient_finance/home'); } // lest i forget Thy love for me session()->forget('print_cashier_receipt_pdf'); session()->forget('print_cashier_receipt_pdf_details'); $pdf = SnappyPDF::loadView("patient_finance::patient_finance.receipts.chi_deposit_pdf", $data) ->setOrientation('portrait') ->setPaper('a4') ->setOption('margin-bottom', 5) ->setOption('margin-top', 5) ->setOption('footer-html', '© ' . date('Y') . ' Stre@mline'); return $pdf->inline('Patient Receipt' . date(" d-m-y h:ia") . '.pdf'); } }