mirror of
https://gitlab.com/signalytic/client-external/streamline/streamline-emr.git
synced 2026-09-11 18:51:31 +00:00
101 lines
5.0 KiB
PHP
Executable File
101 lines
5.0 KiB
PHP
Executable File
<?php
|
|
|
|
namespace Modules\PatientFinance\Services;
|
|
|
|
|
|
use Illuminate\Http\Request;
|
|
use Streamline\Models\ChartOfAccount;
|
|
use Streamline\Models\OrderedService;
|
|
use Streamline\Models\PatientEpisode;
|
|
use Streamline\Models\StaffPerformedService;
|
|
use Streamline\Models\TrackReceipt;
|
|
|
|
class ServicesDeposit {
|
|
|
|
public function store_payment(Request $request){
|
|
|
|
$patient_id = $request->patient_id;
|
|
$episode_id = $request->episode_id;
|
|
$order_ids_array = is_array($request->order_ids) ? $request->order_ids : [];
|
|
$service_ids_array = $request->service_id;
|
|
|
|
$service_prices_array = $request->service_amount;
|
|
$service_type = $request->service_type;
|
|
$tag_id = $request->tag_id;
|
|
|
|
// update the track_receipt table to include created receipt
|
|
$track_receipts = new TrackReceipt;
|
|
$track_receipts->created_by = auth()->user()->id;
|
|
$track_receipts->reason = $service_type;
|
|
$track_receipts->save();
|
|
$receipt_number = sprintf("%04u", $track_receipts->id);
|
|
|
|
// update the chart of accounts balance for each service item
|
|
for($i = 0; $i < count($service_ids_array); $i++){
|
|
$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]);
|
|
}
|
|
|
|
// check if donor discounts have been applied using null and insert into table
|
|
if($request->donor_discount){
|
|
DepositHelpers::donor_discount_details($patient_id, $episode_id, $receipt_number, $request->patient_discount_amount, $request->donor_to_pay, $request->hospital_to_pay_selected_discount, $request->donor_discount, $request->donor_id, $tag_id);
|
|
}
|
|
|
|
// register one off discount
|
|
if ($request->one_off_discount > 0){
|
|
DepositHelpers::patient_one_off_discount($patient_id,$episode_id,$request->one_off_discount,$tag_id,$request->one_off_discount_memo, $receipt_number);
|
|
}
|
|
|
|
// add patient to debtors if balance is more than 0
|
|
if ($request->balance > 0){
|
|
DepositHelpers::debt($patient_id,$episode_id,$request->balance,$receipt_number,$tag_id);
|
|
}
|
|
|
|
// 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){
|
|
DepositHelpers::discount($request->hospital_to_pay_general_discount,$patient_id, $episode_id,$receipt_number,$service_type);
|
|
}
|
|
|
|
// is_invoice flag for receipt purposes. Default is 0 for no
|
|
$is_invoice = 0;
|
|
|
|
// check if patient category is of type pay later to generate invoice for that category or to add to normal service payments table
|
|
if($request->pay_later == 1){
|
|
// change invoice flag to show that it is an invoice
|
|
$is_invoice = 1;
|
|
// create a new invoice
|
|
DepositHelpers::patient_category_invoice($request->patient_category_id, $patient_id,$episode_id,implode(',', $service_prices_array),implode(',', $service_ids_array),$receipt_number,$tag_id,null, $request->patient_category_to_pay, date('Y-m-d'), implode(",", $order_ids_array));
|
|
|
|
// for co-payment people with some cash
|
|
if ($request->patient_to_pay > 0) {
|
|
DepositHelpers::service_payment($patient_id,$episode_id,implode(',', $service_ids_array),implode(',', $service_prices_array),$service_type,$receipt_number,$request->patient_to_pay, implode(",", $order_ids_array), $request->wallets_amount);
|
|
}
|
|
} else {
|
|
// create a new service payment record
|
|
DepositHelpers::service_payment($patient_id,$episode_id,implode(',', $service_ids_array),implode(',', $service_prices_array),$service_type,$receipt_number,$request->patient_to_pay, implode(",", $order_ids_array), $request->wallets_amount);
|
|
}
|
|
|
|
// check if service is a co-payment and change payment status to paid
|
|
if ($service_type == 'Co_Payment') {
|
|
$update_patient_episode = PatientEpisode::find($episode_id);
|
|
$update_patient_episode->co_payment = 1;
|
|
$update_patient_episode->updated_by = auth()->user()->id;
|
|
$update_patient_episode->update();
|
|
}
|
|
|
|
// update the payment status for the ordered ids to paid
|
|
for ($i = 0; $i < count($order_ids_array); $i++) {
|
|
OrderedService::where(['id' => $order_ids_array[$i]])->update(['payment_status' => 1]);
|
|
|
|
// update the receipts for staff payments
|
|
StaffPerformedService::whereIn('id', explode(",", get_name($order_ids_array[$i], 'id', 'performed_id', 'ordered_services')))->update(['patient_receipt_number' => $receipt_number]);
|
|
}
|
|
|
|
return $is_invoice . "," . $receipt_number;
|
|
}
|
|
|
|
} |