mirror of
https://gitlab.com/signalytic/client-external/streamline/streamline-emr.git
synced 2026-09-11 18:51:31 +00:00
850 lines
38 KiB
PHP
Executable File
850 lines
38 KiB
PHP
Executable File
<?php
|
|
|
|
namespace Modules\PatientFinance\Http\Controllers;
|
|
|
|
use Carbon\Carbon;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Streamline\Models\ChartOfAccount;
|
|
use Streamline\Models\Drug;
|
|
use Streamline\Models\EyeGlasses;
|
|
use Streamline\Models\EyeGlassesDeposits;
|
|
use Streamline\Models\FinancePointTag;
|
|
use Streamline\Models\InpatientBill;
|
|
use Streamline\Models\InsuranceClaim;
|
|
use Streamline\Models\InvestigationDeposit;
|
|
use Streamline\Models\OrderedEyeGlasses;
|
|
use Streamline\Models\OrderedInvestigation;
|
|
use Streamline\Models\OrderedProcedure;
|
|
use Streamline\Models\OrderedService;
|
|
use Streamline\Models\OrderedSundry;
|
|
use Streamline\Models\PatientCategoryInvoice;
|
|
use Streamline\Models\ProcedureDeposit;
|
|
use Streamline\Models\ServiceDeposit;
|
|
use Streamline\Models\Sundry;
|
|
use Streamline\Models\SundryDeposit;
|
|
use Streamline\Models\Treatment;
|
|
use Streamline\Models\TreatmentDeposits;
|
|
use Streamline\Services\WardManagement\InpatientFinanceService;
|
|
|
|
class CancelPatientTransactionsController extends Controller {
|
|
|
|
public function __construct(
|
|
protected InpatientFinanceService $inpatientFinanceService
|
|
){}
|
|
|
|
public function cancel_receipt($billing_point, $deposit_id) {
|
|
$billing_points_array = [
|
|
2 => "investigation_deposits", 3 => "treatment_deposits",
|
|
4 => "procedure_deposits", 5 => "sundries_deposits",
|
|
8 => "service_deposits", 10 => "service_deposits", 11 => "central_billing_deposits", 16 => "eye_glasses_deposits"
|
|
];
|
|
|
|
$deposit_details = DB::table($billing_points_array[$billing_point])
|
|
->find($deposit_id);
|
|
|
|
return view('patient_finance::cancel_patient_transactions.cancel_receipt', compact('deposit_details', 'billing_point'));
|
|
}
|
|
|
|
public function cancel_invoice($billing_point, $deposit_id) {
|
|
$deposit_details = DB::table('patient_category_invoices')
|
|
->find($deposit_id);
|
|
|
|
return view('patient_finance::cancel_patient_transactions.cancel_invoice', compact('deposit_details', 'billing_point'));
|
|
}
|
|
|
|
public function confirm_cancel_receipt(Request $request) {
|
|
$receipt_number = "";
|
|
$patient_id = 0;
|
|
$episode_id = 0;
|
|
$originally_billed_by = 0;
|
|
$originally_billed_on = 0;
|
|
$billing_type = 1;
|
|
|
|
if (isset($request->is_invoice) && $request->is_invoice ==1) {
|
|
$billing_type = 2;
|
|
$invoice_deposit = PatientCategoryInvoice::find($request->deposit_id);
|
|
$items = explode(",", $invoice_deposit->items_ids);
|
|
$quantities = explode(",", $invoice_deposit->items_quantity);
|
|
$receipt_number = $invoice_deposit->receipt_number;
|
|
$patient_id = $invoice_deposit->patient_id;
|
|
$episode_id = $invoice_deposit->episode_id;
|
|
$originally_billed_by = $invoice_deposit->created_by;
|
|
$originally_billed_on = $invoice_deposit->created_at;
|
|
$amount_cancelled = $invoice_deposit->patient_amount;
|
|
$order_ids_array = explode(",", $invoice_deposit->order_ids);
|
|
|
|
if ($request->billing_point == 3) {
|
|
for($i = 0; $i < count($order_ids_array); $i++){
|
|
Treatment::where(['id' => $order_ids_array[$i]])->update(['payment_status' => 0]);
|
|
}
|
|
|
|
DB::table('treatment_deposits')
|
|
->where('receipt_number', $receipt_number)
|
|
->delete();
|
|
} elseif ($request->billing_point == 2) {
|
|
for($i = 0; $i < count($order_ids_array); $i++){
|
|
OrderedInvestigation::where(['id' => $order_ids_array[$i]])->update(['payment_status' => 0]);
|
|
}
|
|
|
|
DB::table('investigation_deposits')
|
|
->where('receipt_number', $receipt_number)
|
|
->delete();
|
|
} elseif ($request->billing_point == 4) {
|
|
for($i = 0; $i < count($order_ids_array); $i++){
|
|
OrderedProcedure::where(['id' => $order_ids_array[$i]])->update(['payment_status' => 0]);
|
|
}
|
|
|
|
DB::table('procedure_deposits')
|
|
->where('receipt_number', $receipt_number)
|
|
->delete();
|
|
} elseif ($request->billing_point == 5) {
|
|
for($i = 0; $i < count($order_ids_array); $i++){
|
|
OrderedSundry::where(['id' => $order_ids_array[$i]])->update(['payment_status' => 0]);
|
|
}
|
|
|
|
DB::table('sundries_deposits')
|
|
->where('receipt_number', $receipt_number)
|
|
->delete();
|
|
} elseif ($request->billing_point == 8) {
|
|
for($i = 0; $i < count($order_ids_array); $i++){
|
|
OrderedService::where(['id' => $order_ids_array[$i]])->update(['payment_status' => 0]);
|
|
}
|
|
|
|
DB::table('service_deposits')
|
|
->where('receipt_number', $receipt_number)
|
|
->delete();
|
|
} elseif ($request->billing_point == 10) {
|
|
DB::table('service_deposits')
|
|
->where('receipt_number', $receipt_number)
|
|
->delete();
|
|
|
|
$inpatient_info = InpatientBill::where(['patient_id' => $patient_id,'episode_id' => $episode_id])->first();
|
|
|
|
if ($inpatient_info) {
|
|
$inpatient_info->amount_to_pay = $inpatient_info->amount_to_pay + $amount_cancelled;
|
|
$inpatient_info->save();
|
|
}
|
|
|
|
DB::table('inpatient_ward_discounts')
|
|
->where('receipt_number', $receipt_number)
|
|
->delete();
|
|
} elseif ($request->billing_point == 16) {
|
|
for($i = 0; $i < count($order_ids_array); $i++){
|
|
OrderedEyeGlasses::where(['id' => $order_ids_array[$i]])->update(['payment_status' => 0]);
|
|
}
|
|
|
|
DB::table('eye_glasses_deposits')
|
|
->where('receipt_number', $receipt_number)
|
|
->delete();
|
|
}
|
|
} else {
|
|
if ($request->billing_point == 3) {
|
|
$treatment_deposit = TreatmentDeposits::find($request->deposit_id);
|
|
$items = explode(",", $treatment_deposit->treatment_items);
|
|
$amounts = explode(",", $treatment_deposit->treatment_subtotals);
|
|
$treatment_quantities = explode(",", $treatment_deposit->treatment_quantities);
|
|
$receipt_number = $treatment_deposit->receipt_number;
|
|
$patient_id = $treatment_deposit->patient_id;
|
|
$episode_id = $treatment_deposit->episode_id;
|
|
$originally_billed_by = $treatment_deposit->created_by;
|
|
$originally_billed_on = $treatment_deposit->created_at;
|
|
$order_ids_array = explode(",", $treatment_deposit->order_ids);
|
|
|
|
// reverse the chart of accounts
|
|
for($i = 0; $i < count($items); $i++){
|
|
$this->update_coa_balance($items[$i], $amounts[$i], "drugs");
|
|
$this->add_drug_stock($items[$i], $treatment_quantities[$i] ?? 0);
|
|
//reverse the batches too
|
|
reverse_dispensed_item_batches(1, $items[$i], $treatment_quantities[$i] ?? 0, $patient_id, 'treatments', $treatment_deposit->treatment_id);
|
|
}
|
|
|
|
for($i = 0; $i < count($order_ids_array); $i++){
|
|
Treatment::where(['id' => $order_ids_array[$i]])->update(['payment_status' => 0]);
|
|
}
|
|
|
|
DB::table('treatment_deposits')
|
|
->where('id', $request->deposit_id)
|
|
->delete();
|
|
} elseif ($request->billing_point == 2) {
|
|
$investigation_deposit = InvestigationDeposit::find($request->deposit_id);
|
|
$items = explode(",", $investigation_deposit->investigation_items);
|
|
$amounts = explode(",", $investigation_deposit->investigation_amounts);
|
|
$receipt_number = $investigation_deposit->receipt_number;
|
|
$patient_id = $investigation_deposit->patient_id;
|
|
$episode_id = $investigation_deposit->episode_id;
|
|
$originally_billed_by = $investigation_deposit->created_by;
|
|
$originally_billed_on = $investigation_deposit->created_at;
|
|
$order_ids_array = explode(",", $investigation_deposit->order_ids);
|
|
|
|
// reverse the chart of accounts
|
|
for($i = 0; $i < count($items); $i++){
|
|
$this->update_coa_balance($items[$i], $amounts[$i], "investigations");
|
|
}
|
|
|
|
for($i = 0; $i < count($order_ids_array); $i++){
|
|
OrderedInvestigation::where(['id' => $order_ids_array[$i]])->update(['payment_status' => 0]);
|
|
}
|
|
|
|
DB::table('investigation_deposits')
|
|
->where('id', $request->deposit_id)
|
|
->delete();
|
|
} elseif ($request->billing_point == 4) {
|
|
$procedure_deposit = ProcedureDeposit::find($request->deposit_id);
|
|
$items = explode(",", $procedure_deposit->procedure_items);
|
|
$amounts = explode(",", $procedure_deposit->procedure_amounts);
|
|
$receipt_number = $procedure_deposit->receipt_number;
|
|
$patient_id = $procedure_deposit->patient_id;
|
|
$episode_id = $procedure_deposit->episode_id;
|
|
$originally_billed_by = $procedure_deposit->created_by;
|
|
$originally_billed_on = $procedure_deposit->created_at;
|
|
$order_ids_array = explode(",", $procedure_deposit->order_ids);
|
|
|
|
for($i = 0; $i < count($order_ids_array); $i++){
|
|
OrderedProcedure::where(['id' => $order_ids_array[$i]])->update(['payment_status' => 0]);
|
|
}
|
|
|
|
// reverse the chart of accounts
|
|
for($i = 0; $i < count($items); $i++){
|
|
$this->update_coa_balance($items[$i], $amounts[$i], "procedures");
|
|
}
|
|
|
|
DB::table('procedure_deposits')
|
|
->where('id', $request->deposit_id)
|
|
->delete();
|
|
} elseif ($request->billing_point == 5) {
|
|
$sundry_deposit = SundryDeposit::find($request->deposit_id);
|
|
$items = explode(",", $sundry_deposit->sundry_items);
|
|
$amounts = explode(",", $sundry_deposit->sundry_subtotals);
|
|
$quantities = explode(",", $sundry_deposit->sundry_quantity);
|
|
$receipt_number = $sundry_deposit->receipt_number;
|
|
$patient_id = $sundry_deposit->patient_id;
|
|
$episode_id = $sundry_deposit->episode_id;
|
|
$originally_billed_by = $sundry_deposit->created_by;
|
|
$originally_billed_on = $sundry_deposit->created_at;
|
|
$order_ids_array = explode(",", $sundry_deposit->order_ids);
|
|
|
|
for($i = 0; $i < count($order_ids_array); $i++){
|
|
OrderedSundry::where(['id' => $order_ids_array[$i]])->update(['payment_status' => 0]);
|
|
}
|
|
|
|
// reverse the chart of accounts
|
|
for($i = 0; $i < count($items); $i++){
|
|
$this->update_coa_balance($items[$i], $amounts[$i], "sundries");
|
|
$this->add_sundry_stock($items[$i], $quantities[$i] ?? 0);
|
|
//reverse the batches too
|
|
reverse_dispensed_item_batches(2, $items[$i], $quantities[$i] ?? 0, $patient_id, 'ordered_sundries', null);
|
|
}
|
|
|
|
DB::table('sundries_deposits')
|
|
->where('id', $request->deposit_id)
|
|
->delete();
|
|
} elseif ($request->billing_point == 8) {
|
|
$service_deposit = ServiceDeposit::find($request->deposit_id);
|
|
$items = explode(",", $service_deposit->items_ids);
|
|
$amounts = explode(",", $service_deposit->items_amounts);
|
|
$receipt_number = $service_deposit->receipt_number;
|
|
$patient_id = $service_deposit->patient_id;
|
|
$episode_id = $service_deposit->episode_id;
|
|
$originally_billed_by = $service_deposit->created_by;
|
|
$originally_billed_on = $service_deposit->created_at;
|
|
$order_ids_array = explode(",", $service_deposit->order_ids);
|
|
|
|
for($i = 0; $i < count($order_ids_array); $i++){
|
|
OrderedService::where(['id' => $order_ids_array[$i]])->update(['payment_status' => 0]);
|
|
}
|
|
|
|
// reverse the chart of accounts
|
|
for($i = 0; $i < count($items); $i++){
|
|
$this->update_coa_balance($items[$i], $amounts[$i], "services");
|
|
}
|
|
|
|
DB::table('service_deposits')
|
|
->where('id', $request->deposit_id)
|
|
->delete();
|
|
} elseif ($request->billing_point == 10) {
|
|
$service_deposit = ServiceDeposit::find($request->deposit_id);
|
|
$amount_cancelled = $service_deposit->patient_amount_paid;
|
|
$receipt_number = $service_deposit->receipt_number;
|
|
$patient_id = $service_deposit->patient_id;
|
|
$episode_id = $service_deposit->episode_id;
|
|
$originally_billed_by = $service_deposit->created_by;
|
|
$originally_billed_on = $service_deposit->created_at;
|
|
|
|
DB::table('service_deposits')
|
|
->where('id', $request->deposit_id)
|
|
->delete();
|
|
|
|
$inpatient_info = InpatientBill::where(['patient_id' => $patient_id,'episode_id' => $episode_id])->first();
|
|
|
|
if ($inpatient_info) {
|
|
$inpatient_info->amount_to_pay = $inpatient_info->amount_to_pay + $amount_cancelled;
|
|
$inpatient_info->amount_paid = $inpatient_info->amount_paid - $amount_cancelled;
|
|
$inpatient_info->save();
|
|
}
|
|
|
|
DB::table('inpatient_ward_discounts')
|
|
->where('receipt_number', $receipt_number)
|
|
->delete();
|
|
$this->inpatientFinanceService->unsplit_inpatient_deposit($patient_id, $episode_id, $receipt_number, $amount_cancelled);
|
|
} elseif ($request->billing_point == 16) {
|
|
$optic_deposit = EyeGlassesDeposits::find($request->deposit_id);
|
|
$items = explode(",", $optic_deposit->items);
|
|
$amounts = explode(",", $optic_deposit->subtotals);
|
|
$quantities = explode(",", $optic_deposit->quantity);
|
|
$receipt_number = $optic_deposit->receipt_number;
|
|
$patient_id = $optic_deposit->patient_id;
|
|
$episode_id = $optic_deposit->episode_id;
|
|
$originally_billed_by = $optic_deposit->created_by;
|
|
$originally_billed_on = $optic_deposit->created_at;
|
|
$order_ids_array = explode(",", $optic_deposit->order_ids);
|
|
|
|
for ($i = 0; $i < count($order_ids_array); $i++) {
|
|
OrderedEyeGlasses::where(['id' => $order_ids_array[$i]])->update(['payment_status' => 0]);
|
|
}
|
|
|
|
// reverse the chart of accounts
|
|
for ($i = 0; $i < count($items); $i++) {
|
|
$this->update_coa_balance($items[$i], $amounts[$i], "eye_glasses");
|
|
$this->add_optics_stock($items[$i], $quantities[$i] ?? 0);
|
|
}
|
|
|
|
DB::table('eye_glasses_deposits')
|
|
->where('id', $request->deposit_id)
|
|
->delete();
|
|
}
|
|
}
|
|
|
|
DB::table('cancel_patient_transactions')->insert([
|
|
"patient_id" => $patient_id,
|
|
"episode_id" => $episode_id,
|
|
"billing_point" => $request->billing_point,
|
|
"originally_billed_by" => $originally_billed_by,
|
|
"originally_billed_on" => $originally_billed_on,
|
|
"cancellation_reason" => $request->cancellation_reason,
|
|
"receipt_number" => $receipt_number,
|
|
"billing_type" => $billing_type,
|
|
"created_by" => auth()->id(),
|
|
"created_at" => date('Y-m-d H:i:s'),
|
|
"updated_at" => date('Y-m-d H:i:s')
|
|
]);
|
|
|
|
// delete donor discounts
|
|
DB::table('donor_discount_details')
|
|
->where('receipt_number', $receipt_number)
|
|
->delete();
|
|
|
|
// remove any debts associated
|
|
DB::table('debtors')
|
|
->where('receipt_number', $receipt_number)
|
|
->delete();
|
|
|
|
// remove any debt plan associated
|
|
DB::table('debt_plan')
|
|
->where('receipt_number', $receipt_number)
|
|
->delete();
|
|
|
|
// delete the discounts
|
|
DB::table('discounts')
|
|
->where('receipt_number', $receipt_number)
|
|
->delete();
|
|
|
|
// delete the patient category invoice
|
|
DB::table('patient_category_invoices')
|
|
->where('receipt_number', $receipt_number)
|
|
->where('tag_id', $request->billing_point)
|
|
->delete();
|
|
|
|
// delete the patient payment methods
|
|
DB::table('payment_methods_transactions')
|
|
->where('receipt_number', $receipt_number)
|
|
->delete();
|
|
|
|
// delete any records in the central billing
|
|
DB::table('central_billing_deposits')
|
|
->where('receipt_number', $receipt_number)
|
|
->where('tag_id', $request->billing_point)
|
|
->delete();
|
|
|
|
// delete any records in the central billing
|
|
DB::table('patient_one_off_discounts')
|
|
->where('receipt_number', $receipt_number)
|
|
->delete();
|
|
|
|
// remove receipt number from staff performed
|
|
DB::table('staff_performed_services')
|
|
->where('patient_receipt_number', $receipt_number)
|
|
->update(['patient_receipt_number' => null]);
|
|
|
|
if ($request->patient_amount_paid > 0) {
|
|
revert_cash_credits_to_daily_collection_account($request->patient_amount_paid, $receipt_number, true);
|
|
}
|
|
|
|
// check if this receipt has any family/patient account consumptions attached and remove the refund amount
|
|
reverse_family_consumption_record($receipt_number, 0, true);
|
|
reverse_patient_account_consumption_record($receipt_number, 0, true);
|
|
reverse_dependant_consumption_record($receipt_number, 0, true);
|
|
|
|
flash("Patient transaction has been cancelled")->success();
|
|
|
|
return redirect('/patient_finance/home');
|
|
}
|
|
|
|
public function cancel_central_billing($receipt_number) {
|
|
$deposit_details = DB::table('central_billing_deposits')
|
|
->where('receipt_number', $receipt_number)
|
|
->get();
|
|
|
|
return view('patient_finance::cancel_patient_transactions.cancel_central_billing', compact('deposit_details'));
|
|
}
|
|
|
|
public function confirm_cancel_central_billing_receipt(Request $request) {
|
|
$receipt_number = $request->receipt_number;
|
|
|
|
$deposit_details = DB::table('central_billing_deposits')
|
|
->where('receipt_number', $receipt_number)
|
|
->get();
|
|
|
|
$patient_id = $deposit_details[0]->patient_id;
|
|
$episode_id = $deposit_details[0]->episode_id;
|
|
$originally_billed_by = $deposit_details[0]->created_by;
|
|
$originally_billed_on = $deposit_details[0]->created_at;
|
|
|
|
foreach ($deposit_details as $deposit_detail) {
|
|
$items = explode(",", $deposit_detail->items_ids);
|
|
$amounts = explode(",", $deposit_detail->items_amounts);
|
|
$order_ids_array = explode(",", $deposit_detail->order_ids);
|
|
$items_quantity_array = explode(",", $deposit_detail->items_quantity);
|
|
|
|
if ($deposit_detail->tag_id == 2) {
|
|
// investigations
|
|
for($i = 0; $i < count($order_ids_array); $i++){
|
|
OrderedInvestigation::where(['id' => $order_ids_array[$i]])->update(['payment_status' => 0]);
|
|
}
|
|
|
|
for($i = 0; $i < count($items); $i++){
|
|
$this->update_coa_balance($items[$i], $amounts[$i], "investigations");
|
|
}
|
|
|
|
DB::table('investigation_deposits')
|
|
->where('receipt_number', $receipt_number)
|
|
->delete();
|
|
} else if ($deposit_detail->tag_id == 3) {
|
|
// treatments
|
|
for($i = 0; $i < count($order_ids_array); $i++){
|
|
Treatment::where(['id' => $order_ids_array[$i]])->update(['payment_status' => 0]);
|
|
}
|
|
|
|
DB::table('treatment_deposits')
|
|
->where('receipt_number', $receipt_number)
|
|
->delete();
|
|
|
|
// reverse the chart of accounts
|
|
for($i = 0; $i < count($items); $i++){
|
|
$this->update_coa_balance($items[$i], $amounts[$i], "drugs");
|
|
$this->add_drug_stock($items[$i], $items_quantity_array[$i] ?? 0);
|
|
//reverse the batches too
|
|
$treatment = Treatment::where(['patient_id' => $patient_id, 'episode_id' => $episode_id])->first();
|
|
if ($treatment) {
|
|
reverse_dispensed_item_batches(1, $items[$i], $items_quantity_array[$i] ?? 0, $patient_id, 'treatments', $treatment->id);
|
|
}
|
|
}
|
|
} else if ($deposit_detail->tag_id == 4) {
|
|
// procedures
|
|
|
|
for($i = 0; $i < count($order_ids_array); $i++){
|
|
OrderedProcedure::where(['id' => $order_ids_array[$i]])->update(['payment_status' => 0]);
|
|
}
|
|
|
|
// reverse the chart of accounts
|
|
for($i = 0; $i < count($items); $i++){
|
|
$this->update_coa_balance($items[$i], $amounts[$i], "procedures");
|
|
}
|
|
|
|
DB::table('procedure_deposits')
|
|
->where('receipt_number', $receipt_number)
|
|
->delete();
|
|
} else if ($deposit_detail->tag_id == 5) {
|
|
// sundries
|
|
for($i = 0; $i < count($order_ids_array); $i++){
|
|
OrderedSundry::where(['id' => $order_ids_array[$i]])->update(['payment_status' => 0]);
|
|
}
|
|
|
|
// reverse the chart of accounts
|
|
for($i = 0; $i < count($items); $i++){
|
|
$this->update_coa_balance($items[$i], $amounts[$i], "sundries");
|
|
$this->add_sundry_stock($items[$i], $items_quantity_array[$i] ?? 0);
|
|
//reverse the batches too
|
|
reverse_dispensed_item_batches(2, $items[$i], $items_quantity_array[$i] ?? 0, $patient_id, 'ordered_sundries', null);
|
|
}
|
|
|
|
DB::table('sundries_deposits')
|
|
->where('receipt_number', $receipt_number)
|
|
->delete();
|
|
} else if ($deposit_detail->tag_id == 8) {
|
|
// services/consultations
|
|
for($i = 0; $i < count($order_ids_array); $i++){
|
|
OrderedService::where(['id' => $order_ids_array[$i]])->update(['payment_status' => 0]);
|
|
}
|
|
// check for doctor's consultation
|
|
$doc_consultation_id = get_consultation_service_id_allocated_at_episode_consultation($patient_id, $episode_id);
|
|
|
|
// reverse the chart of accounts
|
|
for($i = 0; $i < count($items); $i++){
|
|
$this->update_coa_balance($items[$i], $amounts[$i], "services");
|
|
}
|
|
|
|
DB::table('service_deposits')
|
|
->where('receipt_number', $receipt_number)
|
|
->delete();
|
|
} else if ($deposit_detail->tag_id == 16) {
|
|
// eye glasses
|
|
for($i = 0; $i < count($order_ids_array); $i++){
|
|
OrderedEyeGlasses::where(['id' => $order_ids_array[$i]])->update(['payment_status' => 0]);
|
|
}
|
|
|
|
// reverse the chart of accounts
|
|
for($i = 0; $i < count($items); $i++){
|
|
$this->update_coa_balance($items[$i], $amounts[$i], "eye_glasses");
|
|
$this->add_optics_stock($items[$i], $items_quantity_array[$i] ?? 0);
|
|
}
|
|
|
|
DB::table('eye_glasses_deposits')
|
|
->where('receipt_number', $receipt_number)
|
|
->delete();
|
|
}
|
|
}
|
|
|
|
DB::table('cancel_patient_transactions')->insert([
|
|
"patient_id" => $patient_id,
|
|
"episode_id" => $episode_id,
|
|
"billing_point" => 11,
|
|
"originally_billed_by" => $originally_billed_by,
|
|
"originally_billed_on" => $originally_billed_on,
|
|
"cancellation_reason" => $request->cancellation_reason,
|
|
"receipt_number" => $receipt_number,
|
|
"created_by" => auth()->id(),
|
|
"created_at" => date('Y-m-d H:i:s'),
|
|
"updated_at" => date('Y-m-d H:i:s')
|
|
]);
|
|
|
|
// delete donor discounts
|
|
DB::table('donor_discount_details')
|
|
->where('receipt_number', $receipt_number)
|
|
->delete();
|
|
|
|
// remove any debts associated
|
|
DB::table('debtors')
|
|
->where('receipt_number', $receipt_number)
|
|
->delete();
|
|
|
|
// remove any debt plan associated
|
|
DB::table('debt_plan')
|
|
->where('receipt_number', $receipt_number)
|
|
->delete();
|
|
|
|
// delete the discounts
|
|
DB::table('discounts')
|
|
->where('receipt_number', $receipt_number)
|
|
->delete();
|
|
|
|
// delete the patient category invoice
|
|
DB::table('patient_category_invoices')
|
|
->where('receipt_number', $receipt_number)
|
|
->delete();
|
|
|
|
// delete the patient payment methods
|
|
DB::table('payment_methods_transactions')
|
|
->where('receipt_number', $receipt_number)
|
|
->delete();
|
|
|
|
// delete any records in the central billing
|
|
DB::table('central_billing_deposits')
|
|
->where('receipt_number', $receipt_number)
|
|
->delete();
|
|
|
|
// remove receipt number from staff performed
|
|
DB::table('staff_performed_services')
|
|
->where('patient_receipt_number', $receipt_number)
|
|
->update(['patient_receipt_number' => null]);
|
|
|
|
// delete any records in the central billing
|
|
DB::table('patient_one_off_discounts')
|
|
->where('receipt_number', $receipt_number)
|
|
->delete();
|
|
|
|
if ($request->patient_amount_paid > 0) {
|
|
revert_cash_credits_to_daily_collection_account($request->patient_amount_paid, $receipt_number, true);
|
|
}
|
|
|
|
// check if this receipt has any family/patient account consumptions attached and remove the refund amount
|
|
reverse_family_consumption_record($receipt_number, 0, true);
|
|
reverse_patient_account_consumption_record($receipt_number, 0, true);
|
|
|
|
flash("Central billing patient transaction has been cancelled")->success();
|
|
|
|
return redirect('/patient_finance/home');
|
|
}
|
|
|
|
public function update_coa_balance ($item_id, $amount, $table) {
|
|
$account_id = get_name($item_id, "id", "account_id", $table);
|
|
$account_balance = get_name($account_id, "id", "balance", "chart_of_accounts");
|
|
|
|
$new_balance = $account_balance - $amount;
|
|
ChartOfAccount::where(['id' => $account_id])->update(['balance' => $new_balance]);
|
|
}
|
|
|
|
public function cancelled_patient_transactions_report(Request $request) {
|
|
$search_text = "";
|
|
|
|
if (isset($request->user_id) && $request->user_id != 0) {
|
|
$cancellations = DB::table('cancel_patient_transactions')
|
|
->where('created_by', $request->user_id)->get();
|
|
|
|
$search_text = "Showing results for " . get_full_name($request->user_id, 'id', 'first_name', 'last_name', 'users');
|
|
} else {
|
|
if($request->search_by == 1){
|
|
// custom date
|
|
$start_date = Carbon::createFromFormat('d/m/Y', $request->reg_date)->startOfDay()->toDateTimeString();
|
|
$end_date = Carbon::createFromFormat('d/m/Y', $request->reg_date)->endOfDay()->toDateTimeString();
|
|
|
|
$search_text = "Showing cancellations made between " . streamline_date_plain($start_date) . " and " . streamline_date_plain($end_date);
|
|
} elseif($request->search_by == 2){
|
|
// custom date range
|
|
$start_date = Carbon::createFromFormat('d/m/Y', $request->start_date)->startOfDay()->toDateTimeString();
|
|
$end_date = Carbon::createFromFormat('d/m/Y', $request->end_date)->endOfDay()->toDateTimeString();
|
|
|
|
$search_text = "Showing cancellations made between " . streamline_date_plain($start_date) . " and " . streamline_date_plain($end_date);
|
|
} else {
|
|
// last 24 hours
|
|
$start_date = Carbon::now()->subDay()->toDateTimeString();
|
|
$end_date = Carbon::now()->toDateTimeString();
|
|
|
|
$search_text = "Showing cancellations in the past 24 hours";
|
|
}
|
|
|
|
$cancellations = DB::table('cancel_patient_transactions')
|
|
->whereBetween('created_at', [$start_date, $end_date])->get();
|
|
}
|
|
|
|
$tags = FinancePointTag::pluck('name', 'id');
|
|
|
|
return view('patient_finance::cancel_patient_transactions.cancelled_patient_transactions_report', compact('search_text', 'cancellations', 'tags'));
|
|
}
|
|
|
|
public function add_drug_stock($drug_id, $amount_returned) {
|
|
if (get_inventory_reduction_point() == 2) {
|
|
$drug = Drug::withTrashed()->find($drug_id);
|
|
$current_stock = $drug->pharmacy_stock;
|
|
$new_stock = $current_stock + $amount_returned;
|
|
$drug->pharmacy_stock = $new_stock;
|
|
$drug->update();
|
|
}
|
|
}
|
|
|
|
public function add_sundry_stock($sundry_id, $amount_returned) {
|
|
if (get_inventory_reduction_point() == 2) {
|
|
$sundry = Sundry::withTrashed()->find($sundry_id);
|
|
$current_stock = $sundry->pharmacy_stock;
|
|
$new_stock = $current_stock + $amount_returned;
|
|
$sundry->pharmacy_stock = $new_stock;
|
|
$sundry->update();
|
|
}
|
|
}
|
|
|
|
public function add_optics_stock($id, $amount_returned) {
|
|
if (get_inventory_reduction_point() == 2) {
|
|
$optic = EyeGlasses::withTrashed()->find($id);
|
|
$current_stock = $optic->store_stock;
|
|
$new_stock = $current_stock - $amount_returned;
|
|
$optic->store_stock = $new_stock;
|
|
$optic->update();
|
|
}
|
|
}
|
|
|
|
public function cancel_chi_deposit($receipt_number) {
|
|
$deposit_details = DB::table('chi_deposits')
|
|
->where('receipt_number', $receipt_number)
|
|
->get();
|
|
|
|
return view('patient_finance::cancel_patient_transactions.cancel_chi_deposit', compact('deposit_details'));
|
|
}
|
|
|
|
public function confirm_cancel_chi_deposit(Request $request) {
|
|
$receipt_number = $request->receipt_number;
|
|
|
|
$deposit_details = DB::table('chi_deposits')
|
|
->where('receipt_number', $receipt_number)
|
|
->get();
|
|
|
|
$patient_id = $deposit_details[0]->patient_id;
|
|
$episode_id = $deposit_details[0]->episode_id;
|
|
$originally_billed_by = $deposit_details[0]->created_by;
|
|
$originally_billed_on = $deposit_details[0]->created_at;
|
|
|
|
foreach ($deposit_details as $deposit_detail) {
|
|
$items = explode(",", $deposit_detail->items_ids);
|
|
$order_ids_array = explode(",", $deposit_detail->order_ids);
|
|
$items_quantity_array = explode(",", $deposit_detail->items_quantity);
|
|
|
|
if ($deposit_detail->tag_id == 2) {
|
|
// investigations
|
|
for($i = 0; $i < count($order_ids_array); $i++){
|
|
OrderedInvestigation::where(['id' => $order_ids_array[$i]])->update(['payment_status' => 0]);
|
|
|
|
$this->undo_insurance_claim($order_ids_array[$i], 3);
|
|
}
|
|
|
|
DB::table('investigation_deposits')
|
|
->where('receipt_number', $receipt_number)
|
|
->delete();
|
|
} else if ($deposit_detail->tag_id == 3) {
|
|
// treatments
|
|
for($i = 0; $i < count($order_ids_array); $i++){
|
|
Treatment::where(['id' => $order_ids_array[$i]])->update(['payment_status' => 0]);
|
|
|
|
$this->undo_insurance_claim($order_ids_array[$i], 4);
|
|
}
|
|
|
|
DB::table('treatment_deposits')
|
|
->where('receipt_number', $receipt_number)
|
|
->delete();
|
|
|
|
// reverse the chart of accounts
|
|
for($i = 0; $i < count($items); $i++){
|
|
$this->add_drug_stock($items[$i], $items_quantity_array[$i] ?? 0);
|
|
//reverse the batches too
|
|
$treatment = Treatment::where(['patient_id' => $patient_id, 'episode_id' => $episode_id])->first();
|
|
if ($treatment) {
|
|
reverse_dispensed_item_batches(1, $items[$i], $items_quantity_array[$i] ?? 0, $patient_id, 'treatments', $treatment->id);
|
|
}
|
|
}
|
|
} else if ($deposit_detail->tag_id == 4) {
|
|
// procedures
|
|
for($i = 0; $i < count($order_ids_array); $i++){
|
|
OrderedProcedure::where(['id' => $order_ids_array[$i]])->update(['payment_status' => 0]);
|
|
|
|
$this->undo_insurance_claim($order_ids_array[$i], 2);
|
|
}
|
|
|
|
DB::table('procedure_deposits')
|
|
->where('receipt_number', $receipt_number)
|
|
->delete();
|
|
} else if ($deposit_detail->tag_id == 5) {
|
|
// sundries
|
|
for($i = 0; $i < count($order_ids_array); $i++){
|
|
OrderedSundry::where(['id' => $order_ids_array[$i]])->update(['payment_status' => 0]);
|
|
|
|
$this->undo_insurance_claim($order_ids_array[$i], 5);
|
|
}
|
|
|
|
// reverse the chart of accounts
|
|
for($i = 0; $i < count($items); $i++){
|
|
$this->add_sundry_stock($items[$i], $items_quantity_array[$i] ?? 0);
|
|
//reverse the batches too
|
|
reverse_dispensed_item_batches(2, $items[$i], $items_quantity_array[$i] ?? 0, $patient_id, 'ordered_sundries', null);
|
|
}
|
|
|
|
DB::table('sundries_deposits')
|
|
->where('receipt_number', $receipt_number)
|
|
->delete();
|
|
} else if ($deposit_detail->tag_id == 8) {
|
|
// services/consultations
|
|
for($i = 0; $i < count($order_ids_array); $i++){
|
|
OrderedService::where(['id' => $order_ids_array[$i]])->update(['payment_status' => 0]);
|
|
|
|
$this->undo_insurance_claim($order_ids_array[$i], 1);
|
|
}
|
|
|
|
DB::table('service_deposits')
|
|
->where('receipt_number', $receipt_number)
|
|
->delete();
|
|
}
|
|
}
|
|
|
|
DB::table('cancel_patient_transactions')->insert([
|
|
"patient_id" => $patient_id,
|
|
"episode_id" => $episode_id,
|
|
"billing_point" => 18,
|
|
"originally_billed_by" => $originally_billed_by,
|
|
"originally_billed_on" => $originally_billed_on,
|
|
"cancellation_reason" => $request->cancellation_reason,
|
|
"receipt_number" => $receipt_number,
|
|
"created_by" => auth()->id(),
|
|
"created_at" => date('Y-m-d H:i:s'),
|
|
"updated_at" => date('Y-m-d H:i:s')
|
|
]);
|
|
|
|
// remove any debts associated
|
|
DB::table('debtors')
|
|
->where('receipt_number', $receipt_number)
|
|
->delete();
|
|
|
|
// remove any debt plan associated
|
|
DB::table('debt_plan')
|
|
->where('receipt_number', $receipt_number)
|
|
->delete();
|
|
|
|
// delete the discounts
|
|
DB::table('discounts')
|
|
->where('receipt_number', $receipt_number)
|
|
->delete();
|
|
|
|
// delete the patient payment methods
|
|
DB::table('payment_methods_transactions')
|
|
->where('receipt_number', $receipt_number)
|
|
->delete();
|
|
|
|
// delete any records in the central billing
|
|
DB::table('chi_deposits')
|
|
->where('receipt_number', $receipt_number)
|
|
->delete();
|
|
|
|
// remove receipt number from staff performed
|
|
DB::table('staff_performed_services')
|
|
->where('patient_receipt_number', $receipt_number)
|
|
->update(['patient_receipt_number' => null]);
|
|
|
|
// delete any records in the central billing
|
|
DB::table('patient_one_off_discounts')
|
|
->where('receipt_number', $receipt_number)
|
|
->delete();
|
|
|
|
if ($request->patient_amount_paid > 0) {
|
|
revert_cash_credits_to_daily_collection_account($request->patient_amount_paid, $receipt_number, true);
|
|
}
|
|
|
|
// check if this receipt has any family/patient account consumptions attached and remove the refund amount
|
|
reverse_family_consumption_record($receipt_number, 0, true);
|
|
reverse_patient_account_consumption_record($receipt_number, 0, true);
|
|
|
|
flash("CHI billing patient transaction has been cancelled")->success();
|
|
|
|
return redirect('/patient_finance/home');
|
|
}
|
|
|
|
public function undo_insurance_claim($order_id, $item_type) {
|
|
$claim = InsuranceClaim::where('order_id', $order_id)
|
|
->where('item_type', $item_type)
|
|
->first();
|
|
|
|
if ($claim) {
|
|
$service_ids = explode(",", $claim->service_ids);
|
|
$tariff_amounts = explode(",", $claim->tariff_amounts);
|
|
$benefit_ids_array = explode(",", $claim->benefit_ids);
|
|
$patient_id = $claim->patient_id;
|
|
$insurance_member = DB::table('insurance_members')->where('patient_id', $patient_id)->select('family_id', 'group_id')->first();
|
|
|
|
for($i = 0; $i < count($service_ids); $i++) {
|
|
// check if consumption was registered and reverse
|
|
if ($tariff_amounts[$i] > 0) {
|
|
remove_insurance_item_consumption($patient_id, $insurance_member->family_id, $benefit_ids_array[$i], $service_ids[$i], $claim->item_type, $tariff_amounts[$i]);
|
|
}
|
|
}
|
|
|
|
// finally generate a new claim
|
|
generate_insurance_claim($order_id, $claim->item_type);
|
|
}
|
|
}
|
|
}
|