Files
streamline-emr/docker/statistics/Modules/PatientFinance/Services/CollectiveBillsDeposit.php
T
2025-03-31 03:46:05 +03:00

292 lines
16 KiB
PHP
Executable File

<?php
namespace Modules\PatientFinance\Services;
use Illuminate\Http\Request;
use Streamline\Models\ChartOfAccount;
use Streamline\Models\OrderedInvestigation;
use Streamline\Models\OrderedProcedure;
use Streamline\Models\OrderedSundry;
use Streamline\Models\TrackReceipt;
use Streamline\Models\Treatment;
use Modules\PatientFinance\Http\Controllers\PatientFinanceController;
use Illuminate\Support\Facades\DB;
use Streamline\Models\InpatientBill;
class CollectiveBillsDeposit
{
public function store_payment(Request $request)
{
$patient_id = session()->get('patient_id');
// update the track_receipt table to include created receipt
$track_receipts = new TrackReceipt;
$track_receipts->created_by = auth()->user()->id;
$track_receipts->reason = "Collective Billing";
$track_receipts->save();
$receipt_number = sprintf("%04u", $track_receipts->id);
// add patient to debtors if balance is more than 0
if ($request->balance > 0) {
DepositHelpers::debt($patient_id, 0, $request->balance, $receipt_number, 12);
}
// patient category discount flag
$discount_status = 0;
// check if patient has a discount amount more than zero and pay later is not available to them
// this discount amount is represented as hospital to pay general
if ($request->hospital_to_pay_general_discount > 0) {
// change discount status
$discount_status = 1;
DepositHelpers::discount($request->hospital_to_pay_general_discount, $patient_id, 0, $receipt_number, 12);
}
// check if services have been submitted
if ($request->service_id) {
$service_ids_array = $request->service_id;
$service_prices_array = $request->service_amount;
$order_ids_array = $request->service_order_id;
$service_episode_ids_array = $request->service_episode_id;
$service_deposits_ids_array = [];
$service_deposits_prices_array = [];
$actual_service_ids_array = [];
$actual_service_prices_array = [];
for ($i = 0; $i < count($service_prices_array); $i++) {
if ($service_ids_array[$i] != 0) {
$actual_service_ids_array[] = $service_ids_array[$i];
$actual_service_prices_array[] = $service_prices_array[$i];
// update the chart of accounts balance for each service item
$service_account = get_name($service_ids_array[$i], "id", "account_id", "services");
$account_balance = get_name($service_account, "id", "balance", "chart_of_accounts");
$new_balance = $account_balance + (int)$service_prices_array[$i];
ChartOfAccount::where(['id' => $service_account])->update(['balance' => $new_balance]);
// we have to add items in the deposit tables according to episodes
// so we have to order/group them as such
$service_deposits_ids_array[$service_episode_ids_array[$i]][] = $service_ids_array[$i];
$service_deposits_prices_array[$service_episode_ids_array[$i]][] = $service_prices_array[$i];
}
}
// now run through the grouped episode items and add them to deposit table
foreach ($service_deposits_ids_array as $key => $value) {
DepositHelpers::service_payment($patient_id, $key, implode(',', $value), implode(',', $service_deposits_prices_array[$key]), "Services", $receipt_number, array_sum($service_deposits_prices_array[$key]), implode(",", $order_ids_array), $request->wallets_amount);
}
// create a new collective bills record
DepositHelpers::collective_bills_payments($patient_id, implode(',', $actual_service_prices_array), implode(',', $actual_service_ids_array), $receipt_number, 8, null, $request->patient_to_pay, implode(",", $order_ids_array));
for ($i = 0; $i < count($order_ids_array); $i++) {
// doctor consultation order ids are negative so the others are the real order ids
if ($order_ids_array[$i] > 0) {
\Streamline\Models\OrderedService::where(['id' => $order_ids_array[$i]])->update(['payment_status' => 1]);
}
}
}
if ($request->procedure_id) {
$procedure_ids_array = $request->procedure_id;
$order_ids_array = $request->procedure_order_id;
$procedure_amounts_array = $request->procedure_amount;
$procedure_episode_ids_array = $request->procedure_episode_id;
$procedure_deposits_ids_array = [];
$procedure_deposits_prices_array = [];
// update the chart of accounts balance for each procedure item
for ($i = 0; $i < count($procedure_ids_array); $i++) {
$procedure_account = get_name($procedure_ids_array[$i], "id", "account_id", "procedures");
$account_balance = get_name($procedure_account, "id", "balance", "chart_of_accounts");
$new_balance = $account_balance + $procedure_amounts_array[$i];
ChartOfAccount::where(['id' => $procedure_account])->update(['balance' => $new_balance]);
// we have to add items in the deposit tables according to episodes
// so we have to order/group them as such
$procedure_deposits_ids_array[$procedure_episode_ids_array[$i]][] = $procedure_ids_array[$i];
$procedure_deposits_prices_array[$procedure_episode_ids_array[$i]][] = $procedure_amounts_array[$i];
}
// now run through the grouped episode items and add them to deposit table
foreach ($procedure_deposits_ids_array as $key => $value) {
DepositHelpers::procedures_payment($patient_id, $key, implode(',', $value), implode(',', $procedure_deposits_prices_array[$key]), $discount_status, $receipt_number, array_sum($procedure_deposits_prices_array[$key]), implode(",", $order_ids_array), $request->wallets_amount);
}
// create a new collective bills record
DepositHelpers::collective_bills_payments($patient_id, implode(',', $procedure_amounts_array), implode(',', $procedure_ids_array), $receipt_number, 4, null, $request->patient_to_pay, implode(",", $order_ids_array));
// update the payment status for the ordered ids to paid
for ($i = 0; $i < count($order_ids_array); $i++) {
OrderedProcedure::where(['id' => $order_ids_array[$i]])->update(['payment_status' => 1]);
}
}
if ($request->investigation_ids) {
$investigation_ids_array = $request->investigation_ids;
$order_ids_array = $request->investigation_order_id;
$investigation_amounts_array = $request->investigation_amount;
$investigation_episode_ids_array = $request->investigation_episode_id;
$investigation_deposits_ids_array = [];
$investigation_deposits_prices_array = [];
// update the chart of accounts balance for each investigation
for ($i = 0; $i < count($investigation_ids_array); $i++) {
$investigation_account = get_name($investigation_ids_array[$i], "id", "account_id", "investigations");
$account_balance = get_name($investigation_account, "id", "balance", "chart_of_accounts");
$new_balance = $account_balance + $investigation_amounts_array[$i];
ChartOfAccount::where(['id' => $investigation_account])->update(['balance' => $new_balance]);
// we have to add items in the deposit tables according to episodes
// so we have to order/group them as such
$investigation_deposits_ids_array[$investigation_episode_ids_array[$i]][] = $investigation_ids_array[$i];
$investigation_deposits_prices_array[$investigation_episode_ids_array[$i]][] = $investigation_amounts_array[$i];
}
// now run through the grouped episode items and add them to deposit table
foreach ($investigation_deposits_ids_array as $key => $value) {
DepositHelpers::investigation_payment($patient_id, $key, implode(',', $value), implode(',', $investigation_deposits_prices_array[$key]), $discount_status, $receipt_number, array_sum($investigation_deposits_prices_array[$key]), implode(",", $order_ids_array), $request->wallets_amount);
}
// create a new collective bills record
DepositHelpers::collective_bills_payments($patient_id, implode(',', $investigation_amounts_array), implode(',', $investigation_ids_array), $receipt_number, 2, null, $request->patient_to_pay, implode(",", $order_ids_array));
// update the payment status for the ordered ids to paid
for ($i = 0; $i < count($order_ids_array); $i++) {
OrderedInvestigation::where(['id' => $order_ids_array[$i]])->update(['payment_status' => 1]);
}
}
if ($request->drug_id) {
$treatment_items_array = $request->drug_id;
$treatment_subtotals_array = $request->drug_amount;
$treatment_amounts_array = $request->treatment_amount;
$treatment_quantity_array = $request->treatment_quantity;
$treatment_ids = $request->drug_order_id;
$treatment_episode_ids_array = $request->drug_episode_id;
$treatment_deposits_ids_array = [];
$treatment_deposits_prices_array = [];
$treatment_deposits_quantity_array = [];
$treatment_deposits_single_prices_array = [];
// update the chart of accounts balance for each treatment item
for ($i = 0; $i < count($treatment_items_array); $i++) {
$treatment_account = get_name($treatment_items_array[$i], "id", "account_id", "drugs");
$account_balance = get_name($treatment_account, "id", "balance", "chart_of_accounts");
$new_balance = $account_balance + $treatment_subtotals_array[$i];
ChartOfAccount::where(['id' => $treatment_account])->update(['balance' => $new_balance]);
// we have to add items in the deposit tables according to episodes
// so we have to order/group them as such
$treatment_deposits_ids_array[$treatment_episode_ids_array[$i]][] = $treatment_items_array[$i];
$treatment_deposits_prices_array[$treatment_episode_ids_array[$i]][] = $treatment_subtotals_array[$i];
$treatment_deposits_quantity_array[$treatment_episode_ids_array[$i]][] = $treatment_quantity_array[$i];
$treatment_deposits_single_prices_array[$treatment_episode_ids_array[$i]][] = $treatment_amounts_array[$i];
}
// now run through the grouped episode items and add them to deposit table
foreach ($treatment_deposits_ids_array as $key => $value) {
DepositHelpers::treatment_payment($patient_id, $key, 0, implode(',', $value), implode(',', $treatment_deposits_quantity_array[$key]), implode(',', $treatment_deposits_single_prices_array[$key]), implode(
',',
$treatment_deposits_prices_array[$key]
), $discount_status, $receipt_number, array_sum($treatment_deposits_prices_array[$key]), implode(",", $treatment_ids), $request->wallets_amount);
}
DepositHelpers::collective_bills_payments($patient_id, implode(',', $treatment_subtotals_array), implode(
',',
$treatment_items_array
), $receipt_number, 3, implode(',', $treatment_quantity_array), $request->patient_to_pay, implode(",", $treatment_ids));
foreach ($treatment_ids as $treatment_id) {
Treatment::where(['id' => $treatment_id])->update(['payment_status' => 1]);
}
}
if ($request->sundry_id) {
$order_ids_array = $request->sundry_order_id;
$sundry_items_array = $request->sundry_id;
$sundry_subtotals_array = $request->sundry_subtotal;
$sundry_amounts_array = $request->sundry_amount;
$sundry_quantity_array = $request->sundry_quantity;
$sundry_episode_ids_array = $request->sundry_episode_id;
$sundry_deposits_ids_array = [];
$sundry_deposits_quantity_array = [];
$sundry_deposits_single_prices_array = [];
$sundry_deposits_prices_array = [];
// update the chart of accounts balance for each treatment item
for ($i = 0; $i < count($sundry_items_array); $i++) {
$sundry_account = get_name($sundry_items_array[$i], "id", "account_id", "sundries");
$account_balance = get_name($sundry_account, "id", "balance", "chart_of_accounts");
$new_balance = $account_balance + $sundry_subtotals_array[$i];
ChartOfAccount::where(['id' => $sundry_account])->update(['balance' => $new_balance]);
// we have to add items in the deposit tables according to episodes
// so we have to order/group them as such
$sundry_deposits_ids_array[$sundry_episode_ids_array[$i]][] = $sundry_items_array[$i];
$sundry_deposits_quantity_array[$sundry_episode_ids_array[$i]][] = $sundry_quantity_array[$i];
$sundry_deposits_single_prices_array[$sundry_episode_ids_array[$i]][] = $sundry_amounts_array[$i];
$sundry_deposits_prices_array[$sundry_episode_ids_array[$i]][] = $sundry_subtotals_array[$i];
}
// now run through the grouped episode items and add them to deposit table
foreach ($sundry_deposits_ids_array as $key => $value) {
DepositHelpers::sundries_payment($patient_id, $key, implode(',', $value), implode(',', $sundry_deposits_quantity_array[$key]), implode(',', $sundry_deposits_single_prices_array[$key]), implode(',', $sundry_deposits_prices_array[$key]), $discount_status, $receipt_number, array_sum($sundry_deposits_prices_array[$key]), implode(",", $order_ids_array), $request->wallets_amount);
}
// create a new collective bills record
DepositHelpers::collective_bills_payments($patient_id, implode(',', $sundry_subtotals_array), implode(',', $sundry_items_array), $receipt_number, 5, implode(',', $sundry_quantity_array), $request->patient_to_pay, implode(",", $order_ids_array));
// update the payment status for the ordered ids to paid
for ($i = 0; $i < count($order_ids_array); $i++) {
OrderedSundry::where(['id' => $order_ids_array[$i]])->update(['payment_status' => 1]);
}
}
if ($request->inpatient_to_pay > 0) {
$inpatient_to_pay_left = $request->inpatient_to_pay;
$patient_episodes = DB::table('patient_episodes')->where('patient_id', $patient_id)
->get(['id', 'patient_id']);
foreach ($patient_episodes as $episode) {
if ($inpatient_to_pay_left == 0) {
break;
}
// update the amount_to_pay
$inpatient_info = InpatientBill::where(['patient_id' => $patient_id, 'episode_id' => $episode->id])->first();
if ($inpatient_info) {
if ($inpatient_to_pay_left > $inpatient_info->amount_to_pay) {
$inpatient_to_pay_left = $inpatient_to_pay_left - $inpatient_info->amount_to_pay;
$amount_to_pay = $inpatient_info->amount_to_pay;
} else {
$amount_to_pay = $inpatient_to_pay_left;
$inpatient_to_pay_left = 0;
}
if ($inpatient_info) {
$inpatient_info->amount_to_pay = $inpatient_info->amount_to_pay - $amount_to_pay;
$inpatient_info->save();
}
DepositHelpers::service_payment($patient_id, $episode->id, 'Inpatient_Deposit', $amount_to_pay, 'Inpatient_Deposit', $receipt_number, $amount_to_pay, "", $request->wallets_amount);
}
}
}
// register one off discount
if ($request->one_off_discount > 0) {
DepositHelpers::patient_one_off_discount($patient_id, 0, $request->one_off_discount, 12, $request->one_off_discount_memo, $receipt_number);
}
return $receipt_number;
}
}