mirror of
https://gitlab.com/signalytic/client-external/streamline/streamline-emr.git
synced 2026-09-11 18:51:31 +00:00
288 lines
14 KiB
PHP
Executable File
288 lines
14 KiB
PHP
Executable File
<?php
|
|
|
|
namespace Modules\PatientFinance\Http\Controllers;
|
|
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Streamline\Models\HospitalInformation;
|
|
use Streamline\Models\InvestigationDeposit;
|
|
use Streamline\Models\Patient;
|
|
use Streamline\Models\PatientRefunds;
|
|
use Streamline\Models\PaymentMethodsTransaction;
|
|
use Streamline\Models\ProcedureDeposit;
|
|
use Streamline\Models\ServiceDeposit;
|
|
use Streamline\Models\SundryDeposit;
|
|
use Streamline\Models\TrackReceipt;
|
|
use Streamline\Models\TreatmentDeposits;
|
|
use Streamline\Services\WardManagement\InpatientFinanceService;
|
|
|
|
class PatientRefundController extends Controller {
|
|
|
|
public function __construct(
|
|
protected InpatientFinanceService $inpatientFinanceService
|
|
){}
|
|
|
|
public function choose_refund_point()
|
|
{
|
|
|
|
$episode_id = session()->get('episode_id');
|
|
|
|
// exclude central billing so that they have to go and cancel it instead
|
|
$exclude_receipts = DB::table('central_billing_deposits')->where('episode_id', $episode_id)->distinct()->pluck('receipt_number')->toArray();
|
|
|
|
// check if consultation or other services is available for refund
|
|
$service_items = ServiceDeposit::where(['episode_id' => $episode_id])->whereNotIn('receipt_number', $exclude_receipts)->whereNull('received')->get(['service_type']);
|
|
|
|
$service_deposits_types = [];
|
|
|
|
foreach ($service_items as $item) {
|
|
$service_deposits_types[] = $item->service_type;
|
|
}
|
|
|
|
if (in_array('Consultation', $service_deposits_types)) {
|
|
$consultations = 1;
|
|
} else {
|
|
$consultations = 0;
|
|
}
|
|
|
|
if (in_array('Co_Payment', $service_deposits_types)) {
|
|
$co_payment = 1;
|
|
} else {
|
|
$co_payment = 0;
|
|
}
|
|
|
|
if (in_array('Inpatient_Deposit', $service_deposits_types)) {
|
|
$inpatient_deposits = 1;
|
|
} else {
|
|
$inpatient_deposits = 0;
|
|
}
|
|
|
|
if (in_array('Service', $service_deposits_types) || in_array('Other_service', $service_deposits_types) || in_array('Services', $service_deposits_types)) {
|
|
$other_services = 1;
|
|
} else {
|
|
$other_services = 0;
|
|
}
|
|
|
|
// check if treatments is available for refund
|
|
$prescription = TreatmentDeposits::where(['episode_id' => $episode_id])->whereNotIn('receipt_number', $exclude_receipts)->whereNull('received')->count() > 0 ? 1 : 0;
|
|
|
|
// check if investigations is available for refund
|
|
$investigations = InvestigationDeposit::where(['episode_id' => $episode_id])->whereNotIn('receipt_number', $exclude_receipts)->whereNull('received')->count() ? 1 : 0;
|
|
|
|
// check if procedures is available for refund
|
|
$procedures_paid = ProcedureDeposit::where(['episode_id' => $episode_id])->whereNotIn('receipt_number', $exclude_receipts)->whereNull('received')->count() ? 1 : 0;
|
|
|
|
// check if sundries is available for refund
|
|
$sundries_paid = SundryDeposit::where(['episode_id' => $episode_id])->whereNotIn('receipt_number', $exclude_receipts)->whereNull('received')->count() ? 1 : 0;
|
|
|
|
return view("patient_finance::patient_refund.choose_refund_point", compact("consultations", "other_services", "prescription", "investigations", "procedures_paid", "inpatient_deposits", "sundries_paid", "co_payment"));
|
|
}
|
|
|
|
public function register_refund_details(Request $request) {
|
|
$episode_id = session()->get('episode_id');
|
|
$refund_point = $request->refund_point;
|
|
// exclude central billing so that they have to go and cancel it instead
|
|
$exclude_receipts = DB::table('central_billing_deposits')->where('episode_id', $episode_id)->distinct()->pluck('receipt_number')->toArray();
|
|
|
|
switch ($refund_point) {
|
|
case 1:
|
|
// Consultation
|
|
$services = ServiceDeposit::where(['episode_id' => $episode_id, 'service_type' => 'Consultation'])->whereNotIn('receipt_number', $exclude_receipts)->whereNull('received')->orderBy('created_at', 'desc')->get();
|
|
$compact = compact('services', 'refund_point');
|
|
break;
|
|
case 2:
|
|
// Treatment
|
|
$paid_treatments = TreatmentDeposits::where(['episode_id' => $episode_id])->whereNotIn('receipt_number', $exclude_receipts)->whereNull('received')->get();
|
|
$compact = compact('paid_treatments', 'refund_point');
|
|
break;
|
|
case 3:
|
|
// Investigations
|
|
$investigations = InvestigationDeposit::where(['episode_id' => $episode_id])->whereNotIn('receipt_number', $exclude_receipts)->whereNull('received')->orderBy('created_at', 'desc')->get();
|
|
$compact = compact('investigations', 'refund_point');
|
|
break;
|
|
case 4:
|
|
// Procedures
|
|
$procedures = ProcedureDeposit::where(['episode_id' => $episode_id])->whereNotIn('receipt_number', $exclude_receipts)->whereNull('received')->orderBy('created_at', 'desc')->get();
|
|
$compact = compact('procedures', 'refund_point');
|
|
break;
|
|
case 5:
|
|
// Other Services
|
|
$services = ServiceDeposit::where(['episode_id' => $episode_id, 'service_type' => 'Services'])->whereNotIn('receipt_number', $exclude_receipts)->whereNull('received')->orderBy('created_at', 'desc')->get();
|
|
$compact = compact('services', 'refund_point');
|
|
break;
|
|
case 6:
|
|
// Inpatient Deposits
|
|
$inpatient_fees = ServiceDeposit::where(['episode_id' => $episode_id, 'service_type' => 'Inpatient_Deposit'])->whereNotIn('receipt_number', $exclude_receipts)->whereNull('received')->orderBy('created_at', 'desc')->get();
|
|
$compact = compact('inpatient_fees', 'refund_point');
|
|
break;
|
|
case 7:
|
|
// Sundries
|
|
$paid_sundries = SundryDeposit::where(['episode_id' => $episode_id])->whereNotIn('receipt_number', $exclude_receipts)->whereNull('received')->get();
|
|
$compact = compact('paid_sundries', 'refund_point');
|
|
break;
|
|
case 8:
|
|
// Co-Payment
|
|
$services = ServiceDeposit::where(['episode_id' => $episode_id, 'service_type' => 'Co_Payment'])->whereNotIn('receipt_number', $exclude_receipts)->whereNull('received')->orderBy('created_at', 'desc')->get();
|
|
$compact = compact('services', 'refund_point');
|
|
break;
|
|
default:
|
|
$compact = compact('episode_id');
|
|
break;
|
|
}
|
|
|
|
return view("patient_finance::patient_refund.register_refund_details", $compact);
|
|
}
|
|
|
|
public function save_refund_details(Request $request)
|
|
{
|
|
$patient_id = session()->get('patient_id');
|
|
$episode_id = session()->get('episode_id');
|
|
|
|
$refund_point = $request->refund_point;
|
|
$deposit_id = $request->id;
|
|
$maximum_refund_amount = $request->maximum_refund_amount;
|
|
$refund_amount = $request->refund_amount;
|
|
$new_patient_amount_paid = $maximum_refund_amount - $refund_amount;
|
|
$refund_reason = $request->refund_reason;
|
|
$original_receipt_number = "";
|
|
$refund_date = Carbon::parse($request->refund_date)->format('Y-m-d');
|
|
|
|
// update the track_receipt table to include created receipt
|
|
$track_receipts = new TrackReceipt;
|
|
$track_receipts->created_by = Auth::id();
|
|
$track_receipts->reason = 'Patient Refunds';
|
|
$track_receipts->save();
|
|
$receipt_number = sprintf("%04u", $track_receipts->id);
|
|
|
|
if ($refund_point == 1) {
|
|
// Consultation
|
|
$consultation = ServiceDeposit::find($deposit_id);
|
|
$original_receipt_number = $consultation->receipt_number;
|
|
$consultation->patient_amount_paid = $new_patient_amount_paid;
|
|
$consultation->refund_amount = $consultation->refund_amount + $refund_amount;
|
|
$consultation->update();
|
|
if ($request->reverse_doctor_fees == 1) {
|
|
reverse_staff_fee_payment($patient_id, $episode_id, 3);
|
|
}
|
|
|
|
split_service_payment($deposit_id);
|
|
} elseif ($refund_point == 2) {
|
|
// Treatment
|
|
$treatment = TreatmentDeposits::find($deposit_id);
|
|
$original_receipt_number = $treatment->receipt_number;
|
|
$treatment->patient_amount_paid = $new_patient_amount_paid;
|
|
$treatment->refund_amount = $treatment->refund_amount + $refund_amount;
|
|
$treatment->update();
|
|
|
|
split_treatment_payment($deposit_id);
|
|
} elseif ($refund_point == 3) {
|
|
// Investigation
|
|
$investigation = InvestigationDeposit::find($deposit_id);
|
|
$original_receipt_number = $investigation->receipt_number;
|
|
$investigation->patient_amount_paid = $new_patient_amount_paid;
|
|
$investigation->refund_amount = $investigation->refund_amount + $refund_amount;
|
|
$investigation->update();
|
|
if ($request->reverse_doctor_fees == 1) {
|
|
reverse_staff_fee_payment($patient_id, $episode_id, 2);
|
|
}
|
|
|
|
split_investigation_payment($deposit_id);
|
|
} elseif ($refund_point == 4) {
|
|
// Procedures
|
|
$procedures = ProcedureDeposit::find($deposit_id);
|
|
$original_receipt_number = $procedures->receipt_number;
|
|
$procedures->patient_amount_paid = $new_patient_amount_paid;
|
|
$procedures->refund_amount = $procedures->refund_amount + $refund_amount;
|
|
$procedures->update();
|
|
if ($request->reverse_doctor_fees == 1) {
|
|
reverse_staff_fee_payment($patient_id, $episode_id, 1);
|
|
}
|
|
|
|
split_procedure_payment($deposit_id);
|
|
} elseif ($refund_point == 5 || $refund_point == 6 || $refund_point == 8) {
|
|
// Other Services - Inpatient Deposits - CoPayment
|
|
$other_services = ServiceDeposit::find($deposit_id);
|
|
$original_receipt_number = $other_services->receipt_number;
|
|
$other_services->patient_amount_paid = $new_patient_amount_paid;
|
|
$other_services->refund_amount = $other_services->refund_amount + $refund_amount;
|
|
$other_services->update();
|
|
|
|
if ($refund_point == 6) {
|
|
$this->inpatientFinanceService->unsplit_inpatient_deposit($patient_id, $episode_id, $other_services->receipt_number, $refund_amount);
|
|
} else {
|
|
split_service_payment($deposit_id);
|
|
}
|
|
} elseif ($refund_point == 7) {
|
|
// Sundries
|
|
$sundries = SundryDeposit::find($deposit_id);
|
|
$original_receipt_number = $sundries->receipt_number;
|
|
$sundries->patient_amount_paid = $new_patient_amount_paid;
|
|
$sundries->refund_amount = $sundries->refund_amount + $refund_amount;
|
|
$sundries->update();
|
|
|
|
split_sundry_payment($deposit_id);
|
|
}
|
|
|
|
revert_cash_credits_to_daily_collection_account($refund_amount, $original_receipt_number, false);
|
|
|
|
// check if this receipt has any family/patient account consumptions attached and remove the refund amount
|
|
reverse_family_consumption_record($original_receipt_number, $refund_amount, false);
|
|
reverse_patient_account_consumption_record($original_receipt_number, $refund_amount, false);
|
|
reverse_dependant_consumption_record($original_receipt_number, $refund_amount, false);
|
|
|
|
// reduce the patient paid amount in the payment methods
|
|
$payment_methods = DB::table('payment_methods_transactions')
|
|
->where('receipt_number', $original_receipt_number)->get();
|
|
|
|
if (count($payment_methods) > 0) {
|
|
$pm_new_patient_amount_paid = $refund_amount;
|
|
|
|
foreach ($payment_methods as $payment_method) {
|
|
$payment_methods_update = PaymentMethodsTransaction::find($payment_method->id);
|
|
|
|
if ($pm_new_patient_amount_paid > $payment_method->amount) {
|
|
$pm_new_patient_amount_paid = $pm_new_patient_amount_paid - $payment_method->amount;
|
|
$payment_methods_update->amount = 0;
|
|
} else {
|
|
$payment_methods_update->amount = $payment_method->amount - $pm_new_patient_amount_paid;
|
|
$pm_new_patient_amount_paid = 0;
|
|
}
|
|
|
|
$payment_methods_update->update();
|
|
|
|
if ($pm_new_patient_amount_paid == 0) {
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
// create patient refund record
|
|
$refund = new PatientRefunds();
|
|
|
|
$refund->original_patient_amount_paid = $maximum_refund_amount;
|
|
$refund->patient_id = $patient_id;
|
|
$refund->episode_id = $episode_id;
|
|
$refund->refund_amount = $refund_amount;
|
|
$refund->refund_reason = $refund_reason;
|
|
$refund->refund_date = $refund_date;
|
|
$refund->refund_point = $refund_point;
|
|
$refund->deposit_id = $deposit_id;
|
|
$refund->receipt_number = $receipt_number;
|
|
$refund->created_by = Auth::id();
|
|
|
|
if ($refund->save()) {
|
|
// details to be used for receipt
|
|
$hospital_information = HospitalInformation::first();
|
|
$patient = Patient::find($patient_id);
|
|
$receipt_date = date('Y-m-d h:i:s');
|
|
|
|
return view("patient_finance::patient_refund.receipt", compact('hospital_information', 'receipt_number', 'patient', 'receipt_date', 'refund_amount', 'refund_reason', 'original_receipt_number'));
|
|
} else {
|
|
flash("Unable to process refund. Please try again later")->error();
|
|
return back()->withInput();
|
|
}
|
|
}
|
|
}
|