mirror of
https://gitlab.com/signalytic/client-external/streamline/streamline-emr.git
synced 2026-09-11 18:51:31 +00:00
365 lines
16 KiB
PHP
365 lines
16 KiB
PHP
<?php
|
|
|
|
namespace Modules\PatientDiscounts\Http\Controllers;
|
|
|
|
use Barryvdh\Snappy\Facades\SnappyPdf;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Contracts\View\View;
|
|
use Illuminate\Http\RedirectResponse;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Routing\Controller;
|
|
use Illuminate\Support\Facades\Auth;
|
|
use Illuminate\Support\Facades\DB;
|
|
use Modules\PatientFinance\Http\Controllers\PatientFinanceController;
|
|
use Streamline\Models\ChartOfAccount;
|
|
use Streamline\Models\Debtor;
|
|
use Streamline\Models\DebtorPayment;
|
|
use Streamline\Models\DebtPlan;
|
|
use Streamline\Models\DebtPlanPaymentStaff;
|
|
use Streamline\Models\HospitalInformation;
|
|
use Streamline\Models\Patient;
|
|
use Streamline\Models\PatientPaymentMethod;
|
|
use Streamline\Models\Payment;
|
|
use Streamline\Models\User;
|
|
use Streamline\Services\PatientDiscounts\DebtorsService;
|
|
use Streamline\Services\PatientDiscounts\DebtPlanService;
|
|
use Streamline\Services\ReceiptService;
|
|
use Streamline\Services\UserService;
|
|
|
|
class PatientDebtorsController extends Controller {
|
|
public function __construct(
|
|
protected ReceiptService $receiptService,
|
|
protected DebtorsService $debtorsService,
|
|
protected DebtPlanService $debtPlanService,
|
|
protected UserService $userService
|
|
) {}
|
|
|
|
public function receive_debtor_payment($string): View {
|
|
$string_array = explode(",", $string);
|
|
$debts = DB::table('debtors')->where('id', $string_array[0])->first();
|
|
|
|
$debt_payments = DB::table('debtor_payments')->whereNull('deleted_at')
|
|
->where('debt_id', $string_array[0])->get();
|
|
|
|
$patient_payment_methods = PatientPaymentMethod::pluck('name', 'id');
|
|
$patient_payment_methods_options = "";
|
|
foreach ($patient_payment_methods as $key => $value) {
|
|
$patient_payment_methods_options .= '<option value="' . $key . '">' . $value . '</option>';
|
|
}
|
|
|
|
$expense_accounts = ChartOfAccount::where('type', 2)->pluck('name', 'id')->prepend('-select-', '')->toArray();
|
|
|
|
return view('patient_discounts::patient_debtors.receive_debtor_payment', compact('debts', 'debt_payments', 'patient_payment_methods_options', 'expense_accounts'));
|
|
}
|
|
|
|
public function write_off_debts(Request $request): string {
|
|
$new_receipt_number = $this->receiptService->createReceipt("Debt Plan Payment Update");
|
|
|
|
$balance = $request->amount_owed - $request->amount;
|
|
$debtorPayment = $this->debtorsService->createDebtorPayment($request->debt_id, $new_receipt_number, $request->amount, Carbon::today()->toDateString(),
|
|
$balance, '', $request->amount);
|
|
|
|
if ($debtorPayment) {
|
|
$this->debtorsService->clearDebt($request->debt_id);
|
|
$message = "Payment Received Successfully";
|
|
} else {
|
|
$message = "Payment Not Received";
|
|
}
|
|
|
|
$payment = new Payment;
|
|
$payment->item_id = "";
|
|
$payment->vendor = "N/A";
|
|
$payment->unit_cost = $request->amount;
|
|
$payment->amount = $request->amount;
|
|
$payment->quantity = 1;
|
|
$payment->memo = $request->write_off_memo ?? "N/A";
|
|
$payment->account_balance = 0;
|
|
$payment->transaction_id = $new_receipt_number;
|
|
$payment->account_id = 0;
|
|
$payment->created_by = Auth::id();
|
|
$payment->expense_date = now();
|
|
$payment->expense_account = $request->expense_account;
|
|
|
|
$payment->save();
|
|
|
|
return $message;
|
|
}
|
|
|
|
public function process_debtor_payment(Request $request): View|RedirectResponse
|
|
{
|
|
$new_receipt_number = $this->receiptService->createReceipt('Debtor Payment');
|
|
$patient_id = $request->patient_id;
|
|
|
|
$debtorPayment = $this->debtorsService->createDebtorPayment($request->debt_id, $new_receipt_number, $request->amount_paid, $request->date,
|
|
$request->balance, $request->comment, 0);
|
|
|
|
if ($debtorPayment) {
|
|
$this->debtorsService->clearDebt($request->debt_id);
|
|
record_cash_credits_to_daily_collection_account($request->amount_paid, $new_receipt_number, 'Patient Debt Payment');
|
|
flash("Payment Received Successfully")->success();
|
|
} else {
|
|
flash("Payment Not Received")->error();
|
|
return back();
|
|
}
|
|
|
|
$return_payment_methods = PatientFinanceController::register_payment_method($patient_id, 0, $request->original_cash_to_pay,
|
|
$request->payment_method, $request->payment_methods_amount, array_fill(0, count($request->payment_methods_amount ?? []), null), array_fill(0, count($request->payment_methods_amount ?? []), 0), $new_receipt_number, 13, Auth::id(), 0);
|
|
|
|
return $this->debtorPaymentReceipt($request, $new_receipt_number, $return_payment_methods);
|
|
}
|
|
|
|
public function debtorPaymentReceipt(Request $request, $new_receipt_number, $payment_methods): View
|
|
{
|
|
$patient = Patient::where(['id' => $request->patient_id])->first();
|
|
$hospital_information = HospitalInformation::first();
|
|
$compact_values = compact('hospital_information', 'request', 'new_receipt_number', 'patient', 'payment_methods');
|
|
|
|
return view('patient_discounts::patient_debtors.debtor_payment', $compact_values);
|
|
}
|
|
|
|
public function history_debtor_payments($debtor_id): View
|
|
{
|
|
$hospital_information = HospitalInformation::first();
|
|
$payments = DebtorPayment::where('debt_id', $debtor_id)->get();
|
|
$debtor = Debtor::where('id', $debtor_id)->first();
|
|
$patient_id = $debtor->patient_id;
|
|
return view('patient_discounts::patient_debtors.history_debtor_payments', compact('payments', 'hospital_information', 'patient_id', 'debtor_id'));
|
|
}
|
|
|
|
public function reverse_debtor_payment($payment_id): RedirectResponse
|
|
{
|
|
$payment = DebtorPayment::find($payment_id);
|
|
|
|
$amount_being_removed = $payment->amount_paid;
|
|
$receipt_number = $payment->receipt_number;
|
|
|
|
$this->debtorsService->reverseDebtPayment($payment_id);
|
|
|
|
// reverse any patient payment methods
|
|
DB::table('payment_methods_transactions')
|
|
->where('receipt_number', $receipt_number)
|
|
->delete();
|
|
|
|
record_cash_debits_to_daily_collection_account($amount_being_removed, $this->receiptService->createReceipt('N/A'), "Patient Debt Payment Cancellation");
|
|
|
|
return redirect('patient_debtors/debtors');
|
|
}
|
|
|
|
public function print_debtor_receipt_pdf_details(Request $request)
|
|
{
|
|
$hospital_information = HospitalInformation::find(1);
|
|
|
|
$data = [
|
|
'hospital_information' => $hospital_information,
|
|
'comment' => $request->comment,
|
|
'amount_owed' => $request->amount_owed,
|
|
'amount_paid' => $request->amount_paid,
|
|
'balance' => $request->balance,
|
|
'payment_methods' => unserialize($request->payment_methods),
|
|
'receipt_number' => $request->receipt_number,
|
|
'patient_id' => $request->patient_id
|
|
];
|
|
|
|
$pdf = SnappyPDF::loadView("patient_discounts::patient_debtors.print_debtor_receipt_pdf_details", $data)
|
|
->setOrientation('portrait')
|
|
->setPaper('a4')
|
|
->setOption('margin-bottom', 5)
|
|
->setOption('margin-top', 5)
|
|
->setOption('footer-html', '<i>© ' . date('Y') . ' Stre@mline</i>');
|
|
|
|
return $pdf->inline('Patient Receipt' . date(" d-m-y h:ia") . '.pdf');
|
|
}
|
|
|
|
public function reprint_debtor_receipt_pdf_details(Request $request)
|
|
{
|
|
$hospital_information = HospitalInformation::find(1);
|
|
|
|
if (isset($request->is_single_print) && is_numeric($request->is_single_print) && $request->is_single_print > 0) {
|
|
$debt_payments = DebtorPayment::where('id', $request->is_single_print)->get();
|
|
} else {
|
|
$debt_payments = DebtorPayment::where('debt_id', $request->debt_id)->get();
|
|
}
|
|
|
|
$data = [
|
|
'hospital_information' => $hospital_information,
|
|
'debt_payments' => $debt_payments,
|
|
'patient_id' => $request->patient_id,
|
|
];
|
|
|
|
$pdf = SnappyPDF::loadView("patient_discounts::patient_debtors.reprint_debtor_receipt_pdf_details", $data)
|
|
->setOrientation('portrait')
|
|
->setPaper('a4')
|
|
->setOption('margin-bottom', 5)
|
|
->setOption('margin-top', 5)
|
|
->setOption('footer-html', '<i>© ' . date('Y') . ' Stre@mline</i>');
|
|
|
|
return $pdf->inline('Patient Receipt' . date(" d-m-y h:ia") . '.pdf');
|
|
}
|
|
|
|
public function debtors(Request $request) {
|
|
$users = User::get();
|
|
$debtors = queryDateFilter('debtors', $request);
|
|
$display = dateLabelSetter($request);
|
|
|
|
return view('patient_discounts::patient_debtors.debtorsReport', compact('debtors', 'display', 'users'));
|
|
}
|
|
|
|
public function debtors_patient_search($patient_id) {
|
|
$users = User::get();
|
|
|
|
$debtors = DB::table('debtors')
|
|
->where('patient_id', $patient_id)
|
|
->whereNull('deleted_at')
|
|
->get();
|
|
|
|
$display = "Showing debt records for " . get_full_name($patient_id, 'id', 'first_name', 'last_name', 'patients')
|
|
. " (" . get_name($patient_id, 'id', 'number', 'patients') . ")";
|
|
|
|
return view('patient_discounts::patient_debtors.debtorsReport', compact('debtors', 'display', 'users'));
|
|
}
|
|
|
|
public function receive_debt_plan_payment_staff(Request $request) {
|
|
$debts = DB::table('debt_plan')->where('id', $request->debt_plan_id)->groupBy('staff_guarantor')->first();
|
|
$debt_with_balances = DB::table('debt_plan_payment_staffs')->where('debt_plan_id', $request->debt_plan_id)->get();
|
|
$expense_accounts = ChartOfAccount::where('type', 2)->pluck('name', 'id')->prepend('-select-', '')->toArray();
|
|
$users_array = $this->userService->pluckUserFullName();
|
|
|
|
return view('patient_discounts::patient_debtors.debt_plan_payments_staff', compact('debts', 'debt_with_balances',
|
|
'request', 'expense_accounts', 'users_array'));
|
|
}
|
|
|
|
public function process_debt_plan_payment_staff(Request $request) {
|
|
$new_receipt_number = $this->receiptService->createReceipt('Debt Plan');
|
|
|
|
$payment = $this->debtPlanService->createDebtPlanPayment(
|
|
$request->amount_paid, $request->balance, $request->payment_id,
|
|
$request->comment, $new_receipt_number, $request->date
|
|
);
|
|
|
|
if ($payment) {
|
|
record_cash_credits_to_daily_collection_account($request->amount_paid, $new_receipt_number, 'Debt Plan Payment');
|
|
$this->debtPlanService->clearDebtPlan($request->payment_id, $request->amount_paid);
|
|
flash("Payment Received Successfully")->success();
|
|
return $this->debtPlanPaymentReceipt($request, $new_receipt_number);
|
|
} else {
|
|
flash("Payment Not Received")->error();
|
|
return redirect()->back();
|
|
}
|
|
}
|
|
|
|
public function debtPlanPaymentReceipt(Request $request, $new_receipt_number)
|
|
{
|
|
$patient = Patient::where(['id' => $request->patient_id])->first();
|
|
$hospital_information = HospitalInformation::first();
|
|
$compact_values = compact('hospital_information', 'request', 'new_receipt_number', 'patient');
|
|
|
|
return view('patient_discounts::patient_debtors.debt_plan_payment', $compact_values);
|
|
}
|
|
|
|
public function write_off_debt_plan(Request $request): string
|
|
{
|
|
$receipt_number = $this->receiptService->createReceipt("Patient Debt Plan Write Off");
|
|
$today = Carbon::today()->toDateTimeString();
|
|
|
|
$payment = $this->debtPlanService->createDebtPlanPayment(
|
|
$request->amount, ($request->amount_owed - $request->amount), $request->payment_id,
|
|
$request->comment, $receipt_number, $today, $request->amount,
|
|
);
|
|
|
|
if ($payment) {
|
|
$message = "Payment Received Successfully";
|
|
$this->debtPlanService->clearDebtPlan($request->payment_id, $request->amount);
|
|
|
|
$payment = new Payment;
|
|
$payment->item_id = "";
|
|
$payment->vendor = "N/A";
|
|
$payment->unit_cost = $request->amount;
|
|
$payment->amount = $request->amount;
|
|
$payment->quantity = 1;
|
|
$payment->memo = $request->write_off_memo ?? "N/A";
|
|
$payment->account_balance = 0;
|
|
$payment->transaction_id = $receipt_number;
|
|
$payment->account_id = 0;
|
|
$payment->created_by = Auth::id();
|
|
$payment->expense_date = now();
|
|
$payment->expense_account = $request->expense_account;
|
|
$payment->save();
|
|
} else {
|
|
$message = "Payment Not Received";
|
|
}
|
|
|
|
return $message;
|
|
}
|
|
|
|
public function debt_plan_payment($id)
|
|
{
|
|
$payment = DebtPlanPaymentStaff::where(['id' => $id])->first();
|
|
$hospital_information = HospitalInformation::first();
|
|
$compact_values = compact('hospital_information', 'payment');
|
|
|
|
return view('patient_discounts::patient_debtors.debt_plan_payment_detail', $compact_values);
|
|
}
|
|
|
|
public function debt_plan_receipt(Request $request)
|
|
{
|
|
$hospital_information = HospitalInformation::first();
|
|
$patient = Patient::where(['id' => $request->patient_id])->first();
|
|
$debt_plan = DB::table('debt_plan')->where('id', $request->debt_plan_id)->first();
|
|
return view('patient_discounts::patient_debtors.patient_debt_plan', compact('debt_plan', 'hospital_information', 'patient'));
|
|
}
|
|
|
|
public function staff_guarantors(Request $request)
|
|
{
|
|
$users = DB::table('users')->get()->toArray();
|
|
$start = Carbon::parse($request->start_date)->startOfDay()->toDateTimeString();
|
|
$end = Carbon::parse($request->end_date)->endOfDay()->toDateTimeString();
|
|
|
|
if ($request->staff_member == "ALL STAFF") {
|
|
$debts = DB::table('debt_plan')->whereBetween('created_at', [$start, $end])->get()->toArray();
|
|
} else {
|
|
$debts = DB::table('debt_plan')
|
|
->where('staff_guarantor', $request->staff_member)->whereBetween('created_at', [$start, $end])->get()->toArray();
|
|
}
|
|
|
|
$display = dateLabelSetter($request);
|
|
return view('patient_discounts::patient_debtors.staffGuarantors', compact('users', 'debts', 'display', 'request'));
|
|
}
|
|
|
|
public function debt_plan_patient_search($patient_id) {
|
|
$users = DB::table('users')->get()->toArray();
|
|
|
|
$debts = DebtPlan::where('patient_id', $patient_id)->get();
|
|
|
|
$display = "Showing debt plan records of " . get_full_name($patient_id, 'id', 'first_name', 'last_name', 'patients')
|
|
. " (" . get_name($patient_id, 'id', 'number', 'patients') . ")";
|
|
|
|
return view('patient_discounts::patient_debtors.staffGuarantors', compact('users', 'debts', 'display'));
|
|
}
|
|
|
|
public function debt_plan_guarantor_agreement($id) {
|
|
$debt_plan = DebtPlan::find($id);
|
|
|
|
if ($debt_plan) {
|
|
// just get the first person for now but would be better to list all as well as the dates
|
|
$user_id = explode(",", $debt_plan->staff_guarantor)[0];
|
|
|
|
if ($debt_plan->guarantor_type == 0) {
|
|
$guarantor_name = get_full_name($user_id, 'id', 'first_name', 'last_name', 'non_staff_guarantors');
|
|
} else {
|
|
$guarantor_name = get_full_name($user_id, 'id', 'first_name', 'last_name', 'users');
|
|
}
|
|
|
|
$amount_to_pay = $debt_plan->staff_guarantor_to_pay;
|
|
$amount_owed = $debt_plan->amount_owed;
|
|
$patient_names = get_full_name($debt_plan->patient_id, 'id', 'first_name', 'last_name', 'patients');
|
|
$payment_date = $debt_plan->first_installment_date;
|
|
$arrangement = $debt_plan->debt_plan_arrangement;
|
|
|
|
return view('patient_discounts::patient_debtors.debt_plan_guarantor_agreement', compact('amount_to_pay', 'patient_names', 'payment_date', 'arrangement', 'guarantor_name', 'amount_owed'));
|
|
} else {
|
|
return redirect('home');
|
|
}
|
|
}
|
|
|
|
}
|