resolved conflicts

This commit is contained in:
2025-03-31 03:46:05 +03:00
6454 changed files with 1520539 additions and 9 deletions
@@ -0,0 +1,317 @@
<?php
namespace Modules\PatientFinance\Services;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\DB;
use Streamline\Models\Drug;
use Streamline\Models\InsuranceClaim;
use Streamline\Models\OrderedInvestigation;
use Streamline\Models\OrderedProcedure;
use Streamline\Models\OrderedService;
use Streamline\Models\OrderedSundry;
use Streamline\Models\StaffPerformedService;
use Streamline\Models\Sundry;
use Streamline\Models\TrackReceipt;
use Streamline\Models\Treatment;
class CHIDeposits {
public function store_payment(Request $request) {
$patient_id = $request->patient_id;
$episode_id = $request->episode_id;
// we need to calculate the wallet amount from the wallets used so that it is not added to the calculations
$wallets_amount = $request->wallets_amount;
$services_wallet_amount = 0;
$procedures_wallet_amount = 0;
$investigations_wallet_amount = 0;
$treatment_wallet_amount = 0;
$sundries_wallet_amount = 0;
if ($request->service_co_pay > 0 && $wallets_amount > 0) {
if ($request->service_co_pay > $wallets_amount) {
$services_wallet_amount = $wallets_amount;
$wallets_amount = 0;
} else {
$services_wallet_amount = $request->service_co_pay;
$wallets_amount -= $request->service_co_pay;
}
}
if ($request->procedure_co_pay > 0 && $wallets_amount > 0) {
if ($request->procedure_co_pay > $wallets_amount) {
$procedures_wallet_amount = $wallets_amount;
$wallets_amount = 0;
} else {
$procedures_wallet_amount = $request->procedure_co_pay;
$wallets_amount -= $request->procedure_co_pay;
}
}
if ($request->investigation_co_pay > 0 && $wallets_amount > 0) {
if ($request->investigation_co_pay > $wallets_amount) {
$investigations_wallet_amount = $wallets_amount;
$wallets_amount = 0;
} else {
$investigations_wallet_amount = $request->investigation_co_pay;
$wallets_amount -= $request->investigation_co_pay;
}
}
if ($request->treatment_co_pay > 0 && $wallets_amount > 0) {
if ($request->treatment_co_pay > $wallets_amount) {
$treatment_wallet_amount = $wallets_amount;
$wallets_amount = 0;
} else {
$treatment_wallet_amount = $request->treatment_co_pay;
$wallets_amount -= $request->treatment_co_pay;
}
}
if ($request->sundries_co_pay > 0 && $wallets_amount > 0) {
if ($request->sundries_co_pay > $wallets_amount) {
$sundries_wallet_amount = $wallets_amount;
} else {
$sundries_wallet_amount = $request->sundries_co_pay;
}
}
// update the track_receipt table to include created receipt
$track_receipts = new TrackReceipt;
$track_receipts->created_by = Auth::id();
$track_receipts->reason = "CHI Deposits";
$track_receipts->save();
$receipt_number = sprintf("%04u", $track_receipts->id);
// add patient to debtors if balance is more than 0
if ($request->service_co_payment_balance > 0){
DepositHelpers::debt($patient_id,$episode_id,$request->service_co_payment_balance,$receipt_number,8);
}
if ($request->procedure_co_payment_balance > 0){
DepositHelpers::debt($patient_id,$episode_id,$request->procedure_co_payment_balance,$receipt_number,4);
}
if ($request->investigation_co_payment_balance > 0){
DepositHelpers::debt($patient_id,$episode_id,$request->investigation_co_payment_balance,$receipt_number,2);
}
if ($request->treatment_co_payment_balance > 0){
DepositHelpers::debt($patient_id,$episode_id,$request->treatment_co_payment_balance,$receipt_number,3);
}
if ($request->sundries_co_payment_balance > 0){
DepositHelpers::debt($patient_id,$episode_id,$request->sundries_co_payment_balance,$receipt_number,5);
}
// 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, $episode_id,$receipt_number,18);
}
// check if services have been submitted
if($request->service_id){
$order_ids_array = $request->services_order_ids;
DepositHelpers::chi_deposits($patient_id, $episode_id, implode(',', $request->service_id), $receipt_number, 8, $request->item_type, implode(',', $request->service_quantity), implode(',', $request->service_chi_to_pay), implode(',', $request->service_co_pay), $request->service_co_payment_total, $request->service_co_payment_balance, implode(',', $request->services_order_ids), $request->service_chi_to_pay_total);
DepositHelpers::service_payment($patient_id, $episode_id, implode(',', $request->service_id), implode(',', $request->service_amount_to_pay),"Services", $receipt_number, $request->service_co_payment_total, implode(',', $request->services_order_ids), $services_wallet_amount);
// 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]);
}
if($request->pay_later == 1 && isset($request->services_category_to_pay) && $request->services_category_to_pay > 0){
DepositHelpers::patient_category_invoice($request->patient_category_id, $patient_id,$episode_id,implode(',', $request->service_amount_to_pay),
implode(',', $request->service_id),$receipt_number,8,null, $request->services_category_to_pay, date('Y-m-d'), implode(',', $request->services_order_ids));
}
}
if($request->procedure_id){
$order_ids_array = $request->procedures_order_ids;
DepositHelpers::chi_deposits($patient_id, $episode_id, implode(',', $request->procedure_id), $receipt_number, 4, $request->item_type, null, implode(',', $request->procedure_chi_to_pay), implode(',', $request->procedure_co_pay), $request->procedure_co_payment_total, $request->procedure_co_payment_balance, implode(",", $order_ids_array), $request->procedure_chi_to_pay_total);
DepositHelpers::procedures_payment($patient_id, $episode_id, implode(',', $request->procedure_id), implode(',', $request->procedure_amount_to_pay), $discount_status, $receipt_number, $request->procedure_co_payment_total, implode(',', $request->procedures_order_ids), $procedures_wallet_amount);
// 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]);
// update the receipts for staff payments
StaffPerformedService::whereIn('id', explode(",", get_name($order_ids_array[$i], 'id', 'performed_id', 'ordered_procedures')))->update(['patient_receipt_number' => $receipt_number]);
}
if($request->pay_later == 1 && isset($request->procedures_category_to_pay) && $request->procedures_category_to_pay > 0){
DepositHelpers::patient_category_invoice($request->patient_category_id, $patient_id,$episode_id,implode(',', $request->procedure_amount_to_pay),
implode(',', $request->procedure_id), $receipt_number,4,null, $request->procedures_category_to_pay, date('Y-m-d'), implode(',', $request->procedures_order_ids));
}
}
if($request->investigation_id){
$order_ids_array = $request->investigations_order_ids;
DepositHelpers::chi_deposits($patient_id, $episode_id, implode(',', $request->investigation_id), $receipt_number, 2, $request->item_type, null, implode(',', $request->investigation_chi_to_pay), implode(',', $request->investigation_co_pay), $request->investigation_co_payment_total, $request->investigation_co_payment_balance, implode(',', $request->investigations_order_ids), $request->investigation_chi_to_pay_total);
DepositHelpers::investigation_payment($patient_id, $episode_id, implode(',', $request->investigation_id), implode(',', $request->investigation_amount_to_pay), $discount_status, $receipt_number, $request->investigation_co_payment_total, implode(',', $request->investigations_order_ids), $investigations_wallet_amount);
// 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->pay_later == 1 && isset($request->investigations_category_to_pay) && $request->investigations_category_to_pay > 0){
DepositHelpers::patient_category_invoice($request->patient_category_id, $patient_id,$episode_id,implode(',', $request->investigation_amount_to_pay),
implode(',', $request->investigation_id), $receipt_number,2,null, $request->investigations_category_to_pay, date('Y-m-d'), implode(',', $request->investigations_order_ids));
}
}
if($request->treatment_item_id){
$treatment_order_ids = $request->treatment_order_ids;
$treatment_items_array = $request->treatment_item_id;
$treatment_quantity_array = $request->treatment_quantity;
// update inventory balance for each treatment item
if (get_inventory_reduction_point() == 2) {
for($i = 0; $i < count($treatment_items_array); $i++){
$drug = Drug::withTrashed()->find($treatment_items_array[$i]);
$current_stock = $drug->pharmacy_stock;
$new_stock = $current_stock - $treatment_quantity_array[$i];
$drug->pharmacy_stock = $new_stock;
$drug->update();
/**** batch tracking */
$auto_calculated_batches_used = get_pharmacy_batch_watcher_ids_to_use_based_on_needed_quantity($treatment_items_array[$i], 1, $treatment_quantity_array[$i]);
foreach ($auto_calculated_batches_used as $batch_number => $batch_qty) {
reduce_batch_item_stock(1,$treatment_items_array[$i],$batch_number,'drugs','pharmacy_stock',$batch_qty,'treatments','Drug',$treatment_order_ids[0],$patient_id);
// reduce_batch_item_from_pharmacy(1, $treatment_items_array[$i], $batch_number, $batch_qty, "treatments", null, $patient_id);
}
/*** end batch tracking logic **/
}
}
DepositHelpers::chi_deposits($patient_id, $episode_id, implode(',', $request->treatment_item_id), $receipt_number, 3, $request->item_type, implode(',', $request->treatment_quantity),
implode(',', $request->treatment_chi_to_pay), implode(',', $request->treatment_co_pay), $request->treatment_co_payment_total, $request->treatment_co_payment_balance, implode(',', $request->treatment_order_ids), $request->treatment_chi_to_pay_total);
DepositHelpers::treatment_payment($patient_id, $episode_id, $treatment_order_ids[0], implode(',', $request->treatment_item_id), implode(',', $request->treatment_quantity), implode(',', $request->treatment_amount_to_pay), implode(',', $request->treatment_amount_to_pay),
$discount_status, $receipt_number, $request->treatment_co_payment_total, implode(',', $request->treatment_order_ids), $treatment_wallet_amount);
// update the payment status for the ordered id to paid
for($i = 0; $i < count($treatment_order_ids); $i++){
Treatment::where(['id' => $treatment_order_ids[$i]])->update(['payment_status' => 1]);
}
if($request->pay_later == 1 && isset($request->treatment_category_to_pay) && $request->treatment_category_to_pay > 0){
DepositHelpers::patient_category_invoice($request->patient_category_id, $patient_id,$episode_id,implode(',', $request->treatment_amount_to_pay),
implode(',', $request->treatment_item_id),$receipt_number,3,implode(',', $request->treatment_quantity), $request->treatment_category_to_pay, date('Y-m-d'), implode(",", $request->treatment_order_ids));
}
}
if($request->sundries_id){
$order_ids_array = $request->sundries_order_ids;
$sundry_items_array = $request->sundries_id;
$sundry_quantity_array = $request->sundries_quantity;
// update the chart of accounts balance for each treatment item
if (get_inventory_reduction_point() == 2) {
for($i = 0; $i < count($sundry_items_array); $i++){
$sundry = Sundry::withTrashed()->find($sundry_items_array[$i]);
$current_stock = $sundry->store_stock;
$new_stock = $current_stock - $sundry_quantity_array[$i];
$sundry->store_stock = $new_stock;
$sundry->update();
/**** batch tracking */
$auto_calculated_batches_used = get_pharmacy_batch_watcher_ids_to_use_based_on_needed_quantity($sundry_items_array[$i], 2, $sundry_quantity_array[$i]);
foreach ($auto_calculated_batches_used as $batch_number => $batch_qty) {
reduce_batch_item_stock(2, $sundry_items_array[$i], $batch_number, 'sundries', 'pharmacy_stock', $batch_qty, 'ordered_sundries', 'Sundry', $order_ids_array[0], $patient_id);
// reduce_batch_item_from_pharmacy(2, $sundry_items_array[$i], $batch_number, $batch_qty, "ordered_sundries", null, $patient_id);
}
/*** end batch tracking logic **/
}
}
DepositHelpers::chi_deposits($patient_id, $episode_id, implode(',', $request->sundries_id), $receipt_number, 5, $request->item_type, implode(',', $request->sundries_quantity), implode(',', $request->sundries_chi_to_pay), implode(',', $request->sundries_co_pay), $request->sundries_co_payment_total, $request->sundries_co_payment_balance, implode(',', $request->sundries_order_ids), $request->sundries_chi_to_pay_total);
DepositHelpers::sundries_payment($patient_id, $episode_id, implode(',', $request->sundries_id),implode(',', $request->sundries_quantity), implode(',', $request->sundries_amount_to_pay), implode(',', $request->sundries_amount_to_pay), $discount_status, $receipt_number, $request->sundries_co_payment_total, implode(',', $request->sundries_order_ids), $sundries_wallet_amount);
// 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->pay_later == 1 && isset($request->sundries_category_to_pay) && $request->sundries_category_to_pay > 0){
DepositHelpers::patient_category_invoice($request->patient_category_id, $patient_id,$episode_id,implode(',', $request->sundries_amount_to_pay),
implode(',', $request->sundries_id), $receipt_number,5,implode(',', $request->sundries_quantity), $request->sundries_category_to_pay, date('Y-m-d'), implode(',', $request->sundries_order_ids));
}
}
// register one off discount
if ($request->one_off_discount > 0){
DepositHelpers::patient_one_off_discount($patient_id,$episode_id,$request->one_off_discount,18,$request->one_off_discount_memo, $receipt_number);
}
// get to the claims and reorder the amounts according to what has been paid
$claim_ids = $request->claim_id;
foreach ($claim_ids as $claim_id) {
$claim = InsuranceClaim::find($claim_id);
if ($claim) {
$claim->claim_status = 2;
$primary_plan_claim_total = 0;
$item_ids = explode(",", $claim->item_ids);
$tariff_amounts = explode(",", $claim->tariff_amounts);
$co_payment_amounts = explode(",", $claim->co_payment_amounts);
$item_cash_amounts = explode(",", $claim->item_cash_amounts);
$is_authorisation_required = $claim->is_authorisation_required;
$item_authorisations = explode(",", $claim->is_item_authorisation_required);
$benefit_ids_array = explode(",", $claim->benefit_ids);
$plan_limit_error = explode(",", $claim->plan_limit_error);
$benefit_waiting_period_error = explode(",", $claim->benefit_waiting_period_error);
$benefit_limit_error = explode(",", $claim->benefit_limit_error);
$item_limit_error = explode(",", $claim->item_limit_error);
$insurance_member = DB::table('insurance_members')->where('patient_id', $patient_id)->select('family_id', 'group_id')->first();
if ($claim->item_type == 4) {
$purchased_elsewhere = explode(",", get_name($claim->order_id, 'id', 'purchased_elsewhere', 'treatments'));
}
for($i = 0; $i < count($item_ids); $i++) {
if ($item_authorisations[$i] == 1 || $is_authorisation_required == 1 || $claim->plan_validity_error == 1 ||
$benefit_waiting_period_error[$i] == 1 || $plan_limit_error[$i] == 1 || $benefit_limit_error[$i] == 1 || $item_limit_error[$i] == 1) {
$co_payment_amounts[$i] = $item_cash_amounts[$i];
$tariff_amounts[$i] = 0;
}
// if it is treatment then check the order for purchased elsewhere and remove amounts
if ($claim->item_type == 4 && isset($purchased_elsewhere[$i]) && $purchased_elsewhere[$i] == 1) {
$co_payment_amounts[$i] = 0;
$tariff_amounts[$i] = 0;
}
$primary_plan_claim_total += $tariff_amounts[$i];
// register the item consumption
if ($tariff_amounts[$i] > 0) {
register_insurance_item_consumption($patient_id, $insurance_member->family_id, $insurance_member->group_id, $claim->plan_id, $benefit_ids_array[$i], $item_ids[$i], $claim->item_type, $tariff_amounts[$i]);
}
}
$claim->primary_plan_claim_total = $primary_plan_claim_total;
$claim->co_payment_amounts = implode(",", $co_payment_amounts);
$claim->tariff_amounts = implode(",", $tariff_amounts);
$claim->save();
}
}
return $receipt_number;
}
}
@@ -0,0 +1,503 @@
<?php
namespace Modules\PatientFinance\Services;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Streamline\Models\ChartOfAccount;
use Streamline\Models\Drug;
use Streamline\Models\EyeGlasses;
use Streamline\Models\OrderedEyeGlasses;
use Streamline\Models\OrderedInvestigation;
use Streamline\Models\OrderedProcedure;
use Streamline\Models\OrderedService;
use Streamline\Models\OrderedSundry;
use Streamline\Models\StaffPerformedService;
use Streamline\Models\Sundry;
use Streamline\Models\TrackReceipt;
use Streamline\Models\Treatment;
class CentralBillingDeposit {
public function storePayment(Request $request): string
{
// global inserts for values - i'm never gonna remember what i meant here :(
$patient_id = $request->patient_id;
$episode_id = $request->episode_id;
// we need to calculate the wallet amount from the wallets used so that it is not added to the calculations
$wallets_amount = $request->wallets_amount;
$services_wallet_amount = 0;
$procedures_wallet_amount = 0;
$investigations_wallet_amount = 0;
$treatment_wallet_amount = 0;
$sundries_wallet_amount = 0;
$optics_wallet_amount = 0;
if ($request->services_patient_to_pay_edit > 0 && $wallets_amount > 0) {
if ($request->services_patient_to_pay_edit > $wallets_amount) {
$services_wallet_amount = $wallets_amount;
$wallets_amount = 0;
} else {
$services_wallet_amount = $request->services_patient_to_pay_edit;
$wallets_amount -= $request->services_patient_to_pay_edit;
}
}
if ($request->procedures_patient_to_pay_edit > 0 && $wallets_amount > 0) {
if ($request->procedures_patient_to_pay_edit > $wallets_amount) {
$procedures_wallet_amount = $wallets_amount;
$wallets_amount = 0;
} else {
$procedures_wallet_amount = $request->procedures_patient_to_pay_edit;
$wallets_amount -= $request->procedures_patient_to_pay_edit;
}
}
if ($request->investigations_patient_to_pay_edit > 0 && $wallets_amount > 0) {
if ($request->investigations_patient_to_pay_edit > $wallets_amount) {
$investigations_wallet_amount = $wallets_amount;
$wallets_amount = 0;
} else {
$investigations_wallet_amount = $request->investigations_patient_to_pay_edit;
$wallets_amount -= $request->investigations_patient_to_pay_edit;
}
}
if ($request->treatment_patient_to_pay_edit > 0 && $wallets_amount > 0) {
if ($request->treatment_patient_to_pay_edit > $wallets_amount) {
$treatment_wallet_amount = $wallets_amount;
$wallets_amount = 0;
} else {
$treatment_wallet_amount = $request->treatment_patient_to_pay_edit;
$wallets_amount -= $request->treatment_patient_to_pay_edit;
}
}
if ($request->sundries_patient_to_pay_edit > 0 && $wallets_amount > 0) {
if ($request->sundries_patient_to_pay_edit > $wallets_amount) {
$sundries_wallet_amount = $wallets_amount;
$wallets_amount = 0;
} else {
$sundries_wallet_amount = $request->sundries_patient_to_pay_edit;
$wallets_amount -= $request->sundries_patient_to_pay_edit;
}
}
if ($request->optics_patient_to_pay_edit > 0 && $wallets_amount > 0) {
if ($request->optics_patient_to_pay_edit > $wallets_amount) {
$optics_wallet_amount = $wallets_amount;
} else {
$optics_wallet_amount = $request->optics_patient_to_pay_edit;
}
}
// update the track_receipt table to include created receipt
$track_receipts = new TrackReceipt;
$track_receipts->created_by = Auth::id();
$track_receipts->reason = "Central Billing";
$track_receipts->save();
$receipt_number = sprintf("%04u", $track_receipts->id);
// 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, 11);
}
// add patient to debtors if balance is more than 0
if ($request->services_patient_to_pay_balance > 0){
DepositHelpers::debt($patient_id,$episode_id,$request->services_patient_to_pay_balance,$receipt_number,8);
}
if ($request->procedures_patient_to_pay_balance > 0){
DepositHelpers::debt($patient_id,$episode_id,$request->procedures_patient_to_pay_balance,$receipt_number,4);
}
if ($request->investigations_patient_to_pay_balance > 0){
DepositHelpers::debt($patient_id,$episode_id,$request->investigations_patient_to_pay_balance,$receipt_number,2);
}
if ($request->treatment_patient_to_pay_balance > 0){
DepositHelpers::debt($patient_id,$episode_id,$request->treatment_patient_to_pay_balance,$receipt_number,3);
}
if ($request->sundries_patient_to_pay_balance > 0){
DepositHelpers::debt($patient_id,$episode_id,$request->sundries_patient_to_pay_balance,$receipt_number,5);
}
if ($request->optics_patient_to_pay_balance > 0){
DepositHelpers::debt($patient_id,$episode_id,$request->optics_patient_to_pay_balance,$receipt_number,19);
}
// 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, $episode_id,$receipt_number,11);
}
$is_invoice = 0;
$filtered_services = [];
// check if services have been submitted
if(count($request->service_id) > 0 && $request->service_id[0] != 0){
$service_ids_array = $request->service_id;
$service_prices_array = $request->service_amount;
$service_type_array = $request->service_type;
$services_order_id_array = $request->services_order_id;
$services_patient_to_pay_edit = $request->services_patient_to_pay_edit;
for($i = 0; $i < count($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]);
$filtered_services[$service_type_array[$i]][$services_order_id_array[$i]]["service_id"][] = $service_ids_array[$i];
$filtered_services[$service_type_array[$i]][$services_order_id_array[$i]]["service_price"][] = $service_prices_array[$i];
if ($service_prices_array[$i] > $services_patient_to_pay_edit) {
$filtered_services[$service_type_array[$i]][$services_order_id_array[$i]]["patient_to_pay"][] = $services_patient_to_pay_edit;
$services_patient_to_pay_edit = 0;
} else {
$filtered_services[$service_type_array[$i]][$services_order_id_array[$i]]["patient_to_pay"][] = $service_prices_array[$i];
$services_patient_to_pay_edit -= $service_prices_array[$i];
}
}
}
// 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;
//services
if($request->service_amount){
$services_order_id = is_array($request->services_order_id) ? $request->services_order_id : [];
DepositHelpers::patient_category_invoice($request->patient_category_id, $patient_id,$episode_id,implode(',', $request->service_amount),
implode(',', $request->service_id),$receipt_number,8,null, $request->services_category_to_pay, $request->transaction_date, implode(",", $services_order_id));
// for co-payment people with some cash
if ($request->services_patient_to_pay_edit > 0) {
foreach ($filtered_services as $key => $filtered_service) {
foreach ($filtered_service as $order_id => $filtered_service_details) {
if (array_sum($filtered_service_details['patient_to_pay']) > $services_wallet_amount) {
$differential_wallet_amount = $services_wallet_amount;
$services_wallet_amount = 0;
} else {
$differential_wallet_amount = array_sum($filtered_service_details['patient_to_pay']);
$services_wallet_amount -= array_sum($filtered_service_details['patient_to_pay']);
}
DepositHelpers::service_payment($patient_id,$episode_id,implode(',', $filtered_service_details['service_id']),implode(',', $filtered_service_details['service_price']),$key,$receipt_number,array_sum($filtered_service_details['patient_to_pay']), $order_id, $differential_wallet_amount);
}
}
}
}
//procedures
if($request->procedure_ids){
DepositHelpers::patient_category_invoice($request->patient_category_id, $patient_id,$episode_id,implode(',', $request->procedure_amount),
$request->procedure_ids, $receipt_number,4,null, $request->procedures_category_to_pay, $request->transaction_date, $request->procedure_order_ids);
// for co-payment people with some cash
if ($request->procedures_patient_to_pay_edit > 0) {
DepositHelpers::procedures_payment($patient_id, $episode_id, $request->procedure_ids, implode(',', $request->procedure_amount), $discount_status, $receipt_number, $request->procedures_patient_to_pay_edit, $request->procedure_order_ids, $procedures_wallet_amount);
}
}
//investigations
if($request->investigation_ids){
DepositHelpers::patient_category_invoice($request->patient_category_id, $patient_id,$episode_id,implode(',', $request->investigation_amount),
$request->investigation_ids, $receipt_number,2,null, $request->investigations_category_to_pay, $request->transaction_date, $request->investigation_order_ids);
// for co-payment people with some cash
if ($request->investigations_patient_to_pay_edit > 0) {
DepositHelpers::investigation_payment($patient_id, $episode_id, $request->investigation_ids, implode(',', $request->investigation_amount), $discount_status, $receipt_number, $request->investigations_patient_to_pay_edit, $request->investigation_order_ids, $investigations_wallet_amount);
}
}
//treatments
if($request->treatment_item){
DepositHelpers::patient_category_invoice($request->patient_category_id, $patient_id,$episode_id,implode(',', $request->treatment_subtotal),
implode(',', $request->treatment_item),$receipt_number,3,implode(',', $request->treatment_quantity), $request->treatment_category_to_pay, $request->transaction_date, implode(",", $request->treatment_id));
// for co-payment people with some cash
if ($request->treatment_patient_to_pay_edit > 0) {
DepositHelpers::treatment_payment($patient_id, $episode_id, $request->treatment_id[0],implode(',', $request->treatment_item),implode(',', $request->treatment_quantity),implode(',', $request->treatment_amount),implode(',',
$request->treatment_subtotal), $discount_status, $receipt_number, $request->treatment_patient_to_pay_edit, implode(",", $request->treatment_id), $treatment_wallet_amount);
}
}
//sundries
if($request->sundry_ids){
DepositHelpers::patient_category_invoice($request->patient_category_id, $patient_id,$episode_id,implode(',', $request->sundry_subtotal),
$request->sundry_ids, $receipt_number,5,implode(',', $request->sundry_quantity), $request->sundries_category_to_pay, $request->transaction_date, $request->sundry_order_ids);
// for co-payment people with some cash
if ($request->sundries_patient_to_pay_edit > 0) {
DepositHelpers::sundries_payment($patient_id, $episode_id, $request->sundry_ids, implode(',', $request->sundry_quantity),
implode(',', $request->sundry_amount), implode(',', $request->sundry_subtotal), $discount_status, $receipt_number, $request->sundries_patient_to_pay_edit, $request->sundry_order_ids, $sundries_wallet_amount);
}
}
//optics
if($request->optic_ids){
DepositHelpers::patient_category_invoice($request->patient_category_id, $patient_id,$episode_id,implode(',', $request->optic_subtotal),
$request->optic_ids, $receipt_number,19,implode(',', $request->optic_quantity), $request->optics_category_to_pay, $request->transaction_date, $request->optic_order_ids);
// for co-payment people with some cash
if ($request->optics_patient_to_pay_edit > 0) {
DepositHelpers::eye_glasses_payment($patient_id, $episode_id, $request->optic_ids, implode(',', $request->optic_quantity),
implode(',', $request->optic_amount), implode(',', $request->optic_subtotal), $discount_status, $receipt_number, $request->optics_patient_to_pay_edit, $request->optic_order_ids, $optics_wallet_amount);
}
}
}
// time to get down to the specifics
foreach ($filtered_services as $key => $filtered_service) {
foreach ($filtered_service as $order_id => $filtered_service_details) {
// create a new central billing record
DepositHelpers::central_billing_payments($patient_id, $episode_id,implode(',', $filtered_service_details['service_price']),implode(',', $filtered_service_details['service_id']),$receipt_number,8,null,array_sum($filtered_service_details['patient_to_pay']),$request->services_category_to_pay, $order_id);
// check if no invoice has been generated and insert into correct table
if($is_invoice == 0){
if (array_sum($filtered_service_details['patient_to_pay']) > $services_wallet_amount) {
$differential_wallet_amount = $services_wallet_amount;
$services_wallet_amount = 0;
} else {
$differential_wallet_amount = array_sum($filtered_service_details['patient_to_pay']);
$services_wallet_amount -= array_sum($filtered_service_details['patient_to_pay']);
}
DepositHelpers::service_payment($patient_id,$episode_id,implode(',', $filtered_service_details['service_id']),implode(',', $filtered_service_details['service_price']),$key,$receipt_number,array_sum($filtered_service_details['patient_to_pay']), $order_id, $differential_wallet_amount);
}
// update the payment status for the ordered ids to paid
OrderedService::where(['id' => $order_id])->update(['payment_status' => 1]);
// update the receipts for staff payments
StaffPerformedService::whereIn('id', explode(",", get_name($order_id, 'id', 'performed_id', 'ordered_services')))
->update(['patient_receipt_number' => $receipt_number]);
}
}
if($request->procedure_ids){
$procedure_ids_array = explode(",", $request->procedure_ids);
$order_ids_array = explode(",", $request->procedure_order_ids);
$procedure_amounts_array = $request->procedure_amount;
// 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]);
}
// create a new central billing record
DepositHelpers::central_billing_payments($patient_id, $episode_id,implode(',', $procedure_amounts_array),implode(',', $procedure_ids_array),$receipt_number,4,null,$request->procedures_patient_to_pay_edit,$request->procedures_category_to_pay, $request->procedure_order_ids);
// check if no invoice has been generated and insert into correct table
if($is_invoice == 0){
DepositHelpers::procedures_payment($patient_id, $episode_id, implode(',', $procedure_ids_array), implode(',', $procedure_amounts_array), $discount_status, $receipt_number, $request->procedures_patient_to_pay_edit, $request->procedure_order_ids, $procedures_wallet_amount);
}
// 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]);
// update the receipts for staff payments
StaffPerformedService::whereIn('id', explode(",", get_name($order_ids_array[$i], 'id', 'performed_id', 'ordered_procedures')))->update(['patient_receipt_number' => $receipt_number]);
}
}
if($request->investigation_ids){
$investigation_ids_array = explode(",", $request->investigation_ids);
$order_ids_array = explode(",", $request->investigation_order_ids);
$investigation_amounts_array = $request->investigation_amount;
// update the chart of accounts balance for each service item
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]);
}
// create a new central billing record
DepositHelpers::central_billing_payments($patient_id, $episode_id,implode(',', $investigation_amounts_array),implode(',', $investigation_ids_array),$receipt_number,2,null,$request->investigations_patient_to_pay_edit,$request->investigations_category_to_pay, $request->investigation_order_ids);
if($is_invoice == 0){
DepositHelpers::investigation_payment($patient_id, $episode_id, implode(',', $investigation_ids_array), implode(',', $investigation_amounts_array), $discount_status, $receipt_number, $request->investigations_patient_to_pay_edit, $request->investigation_order_ids, $investigations_wallet_amount);
}
// 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->treatment_item){
$treatment_id_array = $request->treatment_id;
$treatment_items_array = $request->treatment_item;
$treatment_subtotals_array = $request->treatment_subtotal;
$treatment_amounts_array = $request->treatment_amount;
$treatment_quantity_array = $request->treatment_quantity;
// 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");
if (get_inventory_reduction_point() == 2) {
$drug = Drug::withTrashed()->find($treatment_items_array[$i]);
$current_stock = $drug->pharmacy_stock;
$new_stock = $current_stock - $treatment_quantity_array[$i];
$drug->pharmacy_stock = $new_stock;
$drug->update();
/**** batch tracking */
$auto_calculated_batches_used = get_pharmacy_batch_watcher_ids_to_use_based_on_needed_quantity($treatment_items_array[$i], 1, $treatment_quantity_array[$i]);
foreach ($auto_calculated_batches_used as $batch_number => $batch_qty) {
reduce_batch_item_stock(1,$treatment_items_array[$i],$batch_number,'drugs','pharmacy_stock',$batch_qty,'treatments','Drug',$treatment_id_array[0],$patient_id);
// reduce_batch_item_from_pharmacy(1, $treatment_items_array[$i], $batch_number, $batch_qty, "treatments", $treatment_id_array[0], $patient_id);
}
/*** end batch tracking logic **/
}
$new_balance = $account_balance + $treatment_subtotals_array[$i];
ChartOfAccount::where(['id' => $treatment_account])->update(['balance' => $new_balance]);
}
// create a new central billing record
DepositHelpers::central_billing_payments($patient_id, $episode_id,implode(',', $treatment_subtotals_array),implode(',',
$treatment_items_array),$receipt_number,3,implode(',', $treatment_quantity_array),$request->treatment_patient_to_pay_edit,$request->treatment_category_to_pay, implode(",", $treatment_id_array));
if($is_invoice == 0){
if($request->pos == "1"){
DepositHelpers::treatment_payment($patient_id, $episode_id, $treatment_id_array[0],implode(',', $treatment_items_array), implode(',', $treatment_quantity_array),implode(',', $treatment_amounts_array),implode(',',
$treatment_subtotals_array), $discount_status, $receipt_number, $request->treatment_total, implode(",", $treatment_id_array), $treatment_wallet_amount);
// create a new central billing record
DepositHelpers::central_billing_payments($patient_id, $episode_id,implode(',', $treatment_subtotals_array),implode(',',
$treatment_items_array),$receipt_number,3,implode(',', $treatment_quantity_array),$request->treatment_total, 0, implode(",", $treatment_id_array));
}else{
DepositHelpers::treatment_payment($patient_id, $episode_id, $treatment_id_array[0],implode(',', $treatment_items_array),implode(',', $treatment_quantity_array),implode(',', $treatment_amounts_array),implode(',', $treatment_subtotals_array),
$discount_status, $receipt_number, $request->treatment_patient_to_pay_edit, implode(",", $treatment_id_array), $treatment_wallet_amount);
}
}
}
if ($request->treatment_id) {
$treatment_id_array = $request->treatment_id;
// update the payment status for the ordered id to paid
for($i = 0; $i < count($treatment_id_array); $i++){
Treatment::where(['id' => $treatment_id_array[$i]])->update(['payment_status' => 1]);
}
}
if($request->sundry_ids){
$order_ids_array = explode(",", $request->sundry_order_ids);
$sundry_items_array = explode(",", $request->sundry_ids);
$sundry_subtotals_array = $request->sundry_subtotal;
$sundry_amounts_array = $request->sundry_amount;
$sundry_quantity_array = $request->sundry_quantity;
// 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");
//changed and set it to reduce sundries at payment only
$sundry = Sundry::withTrashed()->find($sundry_items_array[$i]);
$current_stock = $sundry->pharmacy_stock;
$new_stock = $current_stock - $sundry_quantity_array[$i];
$sundry->pharmacy_stock = $new_stock;
$sundry->update();
/**** batch tracking */
$auto_calculated_batches_used = get_pharmacy_batch_watcher_ids_to_use_based_on_needed_quantity($sundry_items_array[$i], 2, $sundry_quantity_array[$i]);
foreach ($auto_calculated_batches_used as $batch_number => $batch_qty) {
reduce_batch_item_stock(2, $sundry_items_array[$i], $batch_number, 'sundries', 'pharmacy_stock', $batch_qty, 'ordered_sundries', 'Sundry', $order_ids_array[0], $patient_id);
// reduce_batch_item_from_pharmacy(2, $sundry_items_array[$i], $batch_number, $batch_qty, "ordered_sundries", $order_ids_array[0], $patient_id);
}
/*** end batch tracking logic **/
$new_balance = $account_balance + $sundry_subtotals_array[$i];
ChartOfAccount::where(['id' => $sundry_account])->update(['balance' => $new_balance]);
}
// create a new central billing record
DepositHelpers::central_billing_payments($patient_id, $episode_id,implode(',', $sundry_subtotals_array),implode(',', $sundry_items_array),$receipt_number,5,implode(',', $sundry_quantity_array),$request->sundries_patient_to_pay_edit,$request->sundries_category_to_pay, $request->sundry_order_ids);
if($is_invoice == 0){
DepositHelpers::sundries_payment($patient_id, $episode_id, implode(',', $sundry_items_array),implode(',', $sundry_quantity_array), implode(',', $sundry_amounts_array), implode(',', $sundry_subtotals_array), $discount_status, $receipt_number, $request->sundries_patient_to_pay_edit, $request->sundry_order_ids, $sundries_wallet_amount);
}
// 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->optic_ids){
$order_ids_array = explode(",", $request->optic_order_ids);
$optic_items_array = explode(",", $request->optic_ids);
$optic_subtotals_array = $request->optic_subtotal;
$optic_amounts_array = $request->optic_amount;
$optic_quantity_array = $request->optic_quantity;
// update the chart of accounts balance for each item
for($i = 0; $i < count($optic_items_array); $i++){
$optic_account = get_name($optic_items_array[$i], "id", "account_id", "eye_glasses");
$account_balance = get_name($optic_account, "id", "balance", "chart_of_accounts");
$optic = EyeGlasses::withTrashed()->find($optic_items_array[$i]);
$current_stock = $optic->pharmacy_stock;
$new_stock = $current_stock - $optic_quantity_array[$i];
$optic->pharmacy_stock = $new_stock;
/**** batch tracking */
$auto_calculated_batches_used = get_lab_batches_to_use_based_on_needed_quantity($optic_items_array[$i], 7, $optic_quantity_array[$i],'pharmacy_stock');
foreach ($auto_calculated_batches_used as $batch_number => $batch_qty) {
reduce_batch_item_from_pharmacy(7, $optic_items_array[$i], $batch_number, $batch_qty, "ordered_eye_glasses", $order_ids_array[0], $patient_id);
}
/*** end batch tracking logic **/
$optic->update();
$new_balance = $account_balance + $optic_subtotals_array[$i];
ChartOfAccount::where(['id' => $optic_account])->update(['balance' => $new_balance]);
}
// create a new central billing record
DepositHelpers::central_billing_payments($patient_id, $episode_id,implode(',', $optic_subtotals_array),implode(',', $optic_items_array),$receipt_number,19,implode(',', $optic_quantity_array),$request->optics_patient_to_pay_edit,$request->optics_category_to_pay, $request->optic_order_ids);
if($is_invoice == 0){
DepositHelpers::eye_glasses_payment($patient_id, $episode_id, implode(',', $optic_items_array),implode(',', $optic_quantity_array), implode(',', $optic_amounts_array), implode(',', $optic_subtotals_array), $discount_status, $receipt_number, $request->optics_patient_to_pay_edit, $request->optic_order_ids, $optics_wallet_amount);
}
// update the payment status for the ordered ids to paid
for($i = 0; $i < count($order_ids_array); $i++){
OrderedEyeGlasses::where(['id' => $order_ids_array[$i]])->update(['payment_status' => 1]);
}
}
// register one off discount
if ($request->one_off_discount > 0){
DepositHelpers::patient_one_off_discount($patient_id,$episode_id,$request->one_off_discount,11,$request->one_off_discount_memo, $receipt_number);
}
return $is_invoice . "," . $receipt_number;
}
}
@@ -0,0 +1,291 @@
<?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;
}
}
@@ -0,0 +1,614 @@
<?php
namespace Modules\PatientFinance\Services;
use Carbon\Carbon;
use Illuminate\Support\Facades\Auth;
use Streamline\Models\CentralBillingDeposits;
use Streamline\Models\ChartOfAccount;
use Streamline\Models\CHIDeposit;
use Streamline\Models\CollectiveBillsDeposits;
use Streamline\Models\EyeGlassesDeposits;
use Streamline\Models\InvestigationDeposit;
use Streamline\Models\Patient;
use Streamline\Models\DonorDiscountDetail;
use Streamline\Models\Debtor;
use Streamline\Models\Discount;
use Streamline\Models\PatientCategoryInvoice;
use Streamline\Models\ServiceDeposit;
use Streamline\Models\ProcedureDeposit;
use Streamline\Models\SundryDeposit;
use Streamline\Models\TreatmentDeposits;
use Streamline\Models\PatientOneOffDiscount;
class DepositHelpers
{
/**
* Create new donor discount details record
*
* @param $patient_id
* @param $episode_id
* @param $receipt_number
* @param $patient_to_pay
* @param $donor_to_pay
* @param $hospital_to_pay
* @param $donor_discount_category_id
* @param $donor_id
* @param $tag_id
*/
public static function donor_discount_details($patient_id, $episode_id, $receipt_number, $patient_to_pay, $donor_to_pay, $hospital_to_pay, $donor_discount_category_id, $donor_id, $tag_id)
{
// get the patient category for current patient
$patient = Patient::find($patient_id);
// create new donor discount details object
$new_donor_discount_details = new DonorDiscountDetail;
$new_donor_discount_details->patient_id = $patient_id;
$new_donor_discount_details->episode_id = $episode_id;
$new_donor_discount_details->patient_category = $patient->category_id;
$new_donor_discount_details->receipt_number = $receipt_number;
$new_donor_discount_details->patient_to_pay = $patient_to_pay;
$new_donor_discount_details->donor_to_pay = $donor_to_pay;
$new_donor_discount_details->hospital_to_pay = $hospital_to_pay;
$new_donor_discount_details->donor_discount_category_id = $donor_discount_category_id;
$new_donor_discount_details->donor_id = $donor_id;
$new_donor_discount_details->tag_id = $tag_id;
$new_donor_discount_details->created_by = Auth::id();
$new_donor_discount_details->save();
}
/**
* Create new debtor record
*
* @param $patient_id
* @param $episode_id
* @param $balance
* @param $receipt_number
* @param $tag_id
*/
public static function debt($patient_id, $episode_id, $balance, $receipt_number, $tag_id) {
$income_account = '';
$inventory_account_id = ChartOfAccount::where('type', 10)->pluck('id')->first();
$cost_of_good_account_id = ChartOfAccount::where('type', 7)->pluck('id')->first();
if ($tag_id == 6 || $tag_id == 8 || $tag_id == 7 || $tag_id == 10) {
//Consultations / Services
$income_account = 7;
} elseif ($tag_id == 3 || $tag_id == 1) {
//Drugs / Treatments
$income_account = 4;
} elseif ($tag_id == 5) {
//Sundries
$income_account = 12;
} elseif ($tag_id == 2) {
//Investigations
$income_account = 11;
} elseif ($tag_id == 4) {
//Procedures
$income_account = 8;
}
$new_debtor = new Debtor;
$new_debtor->patient_id = $patient_id;
$new_debtor->episode_id = $episode_id;
$new_debtor->balance = $balance;
$new_debtor->receipt_number = $receipt_number;
$new_debtor->tag_id = $tag_id;
$new_debtor->income_account = $income_account;
//Drugs / Treatments
if ($tag_id == 3 || $tag_id == 1) {
$new_debtor->inventory_account = $inventory_account_id;
$new_debtor->cost_of_goods_account = $cost_of_good_account_id;
}
//Sundries
elseif ($tag_id == 5) {
$new_debtor->inventory_account = $inventory_account_id;
$new_debtor->cost_of_goods_account = $cost_of_good_account_id;
}
$new_debtor->created_by = Auth::id();
$new_debtor->save();
}
/**
* Create new discount record
*
* @param $discount_amount
* @param $patient_id
* @param $episode_id
* @param $receipt_number
* @param $reason
*/
public static function discount($discount_amount, $patient_id, $episode_id, $receipt_number, $reason) {
// get patient for the category
$patient = Patient::find($patient_id);
$new_discount = new Discount;
$new_discount->discount_amount = $discount_amount;
$new_discount->patient_id = $patient_id;
$new_discount->episode_id = $episode_id;
$new_discount->reason = $reason;
$new_discount->patient_category = $patient->category_id;
//====== check if the patient is a dependant of another patient and overide category ========//
$patient_is_a_dependant_of = patient_is_a_dependant_of($patient_id);
$does_patient_category_have_threshold = does_patient_category_have_threshold($patient->category_id);
if($does_patient_category_have_threshold || !is_null($patient_is_a_dependant_of)){
$new_discount->patient_category = get_name($patient_is_a_dependant_of, "id","category_id","patients");
}
//==============//
$new_discount->receipt_number = $receipt_number;
$new_discount->created_by = Auth::id();
$new_discount->save();
}
/**
* Create new patient category invoice record
*
* @param $patient_category_id
* @param $patient_id
* @param $episode_id
* @param $amounts_string
* @param $items_string
* @param $receipt_number
* @param $tag_id
* @param $quantity
* @param $patient_amount
* @param $transaction_date
*/
public static function patient_category_invoice($patient_category_id, $patient_id, $episode_id, $amounts_string, $items_string, $receipt_number, $tag_id, $quantity, $patient_amount, $transaction_date, $order_ids) {
$income_account = $cost_of_goods_account = $inventory_account = '';
$items_array = explode(',', $items_string);
$cost_price_array = $income_accounts_array = $cost_of_goods_account_array = $inventory_account_array = [];
for ($i = 0; $i < count($items_array); $i++) {
//Inpatient Deposits
if ($tag_id == 10) {
$income_account = 7;
}
//Consultations / Services
if ($tag_id == 6 || $tag_id == 8 || $tag_id == 7) {
$income_account = get_name($items_array[$i], 'id', 'account_id', 'services');
$income_account = is_numeric($income_account) ? $income_account : 7;
$income_accounts_array[] = $income_account;
}
//Drugs / Treatments
elseif ($tag_id == 3 || $tag_id == 1) {
$income_account = get_name($items_array[$i], 'id', 'account_id', 'drugs');
$income_account = is_numeric($income_account) ? $income_account : 4;
$income_accounts_array[] = $income_account;
$inventory_account = get_name($items_array[$i], 'id', 'inventory_account', 'drugs');
$inventory_account_array[] = $inventory_account;
$cost_of_goods_account = get_name($items_array[$i], 'id', 'cost_of_goods_account', 'drugs');
$cost_of_goods_account_array[] = $cost_of_goods_account;
$cost_price_array[$i] = get_latest_inventory_cost_price($items_array[$i], 1, Carbon::today()->toDateString());
}
//Sundries
elseif ($tag_id == 5) {
$income_account = get_name($items_array[$i], 'id', 'account_id', 'sundries');
$income_account = is_numeric($income_account) ? $income_account : 12;
$income_accounts_array[] = $income_account;
$inventory_account = get_name($items_array[$i], 'id', 'inventory_account', 'sundries');
$inventory_account_array[] = $inventory_account;
$cost_of_goods_account = get_name($items_array[$i], 'id', 'cost_of_goods_account', 'sundries');
$cost_of_goods_account_array[] = $cost_of_goods_account;
$cost_price_array[$i] = get_latest_inventory_cost_price($items_array[$i], 2, Carbon::today()->toDateString());
}
//Investigations
elseif ($tag_id == 2) {
$income_account = get_name($items_array[$i], 'id', 'account_id', 'investigations');
$income_account = is_numeric($income_account) ? $income_account : 11;
$income_accounts_array[] = $income_account;
}
//Procedures
elseif ($tag_id == 4) {
$income_account = get_name($items_array[$i], 'id', 'account_id', 'procedures');
$income_account = is_numeric($income_account) ? $income_account : 8;
$income_accounts_array[] = $income_account;
}
}
$cost_price_string = implode(',', $cost_price_array);
$income_accounts_string = is_null($income_accounts_array) ? "" : implode(',', $income_accounts_array);
$cost_of_goods_accounts_string = is_null($cost_of_goods_account_array) ? null : implode(',', $cost_of_goods_account_array);
$inventory_accounts_string = is_null($inventory_account_array) ? null : implode(',', $inventory_account_array);
// check if the transaction date is set
if ($transaction_date == "") {
$transaction_date = date('Y-m-d');
}
$new_patient_category_invoice = new PatientCategoryInvoice;
$new_patient_category_invoice->patient_id = $patient_id;
$new_patient_category_invoice->episode_id = $episode_id;
$new_patient_category_invoice->patient_category = $patient_category_id;
$new_patient_category_invoice->items_amounts = $amounts_string;
$new_patient_category_invoice->items_ids = $items_string;
$new_patient_category_invoice->items_quantity = isset($quantity) ? (is_string($quantity) ? $quantity : implode(',', $quantity)) : '';
$new_patient_category_invoice->patient_amount = $patient_amount;
$new_patient_category_invoice->receipt_number = $receipt_number;
$new_patient_category_invoice->tag_id = $tag_id;
$new_patient_category_invoice->created_by = Auth::id();
$new_patient_category_invoice->transaction_date = $transaction_date . " " . date('H:i:s');
$new_patient_category_invoice->order_ids = $order_ids;
$new_patient_category_invoice->income_account = $income_accounts_string; //$income_account;
if ($tag_id == 3 || $tag_id == 1) {
//treatments or drugs
$new_patient_category_invoice->inventory_account = $inventory_accounts_string; //$inventory_account;
$new_patient_category_invoice->cost_of_goods_account = $cost_of_goods_accounts_string; //$cost_of_goods_account;
$new_patient_category_invoice->cost_price = $cost_price_string;
} elseif ($tag_id == 5) {
//sundries
$new_patient_category_invoice->inventory_account = $inventory_accounts_string; //$inventory_account;
$new_patient_category_invoice->cost_of_goods_account = $cost_of_goods_accounts_string; //$cost_of_goods_account;
$new_patient_category_invoice->cost_price = $cost_price_string;
}
$new_patient_category_invoice->save();
}
/**
* Pay for service items and add them to the service deposits table
*
* @param $patient_id
* @param $episode_id
* @param $service_items_string
* @param $amounts_string
* @param $service_type
* @param $receipt_number
* @param $patient_amount_paid
*/
public static function service_payment($patient_id, $episode_id, $service_items_string, $amounts_string, $service_type, $receipt_number, $patient_amount_paid, $order_ids, $wallets_amount)
{
$new_service_deposit = new ServiceDeposit;
$chart_of_account_array = [];
$id_array = explode(',', $service_items_string);
for ($i = 0; $i < count($id_array); $i++) {
$account_id = get_name($id_array[$i], 'id', 'account_id', 'services');
$chart_of_account_array[$i] = $account_id;
}
$chart_of_account_string = implode(',', $chart_of_account_array);
// if payment is inpatient, use inpatient slug
if ($service_items_string == 'Inpatient_Deposit') {
$inpatient_deposits_account_id = get_name("inpatient_deposits", "slug", "id", "chart_of_accounts");
if (is_numeric($inpatient_deposits_account_id)) {
$chart_of_account_string = $inpatient_deposits_account_id;
} else {
flash('The inpatient deposit chart of account is missing. Please contact Stre@mline support immediately!')->error();
}
}
$new_service_deposit->patient_id = $patient_id;
$new_service_deposit->episode_id = $episode_id;
$new_service_deposit->items_ids = $service_items_string;
$new_service_deposit->items_amounts = $amounts_string;
$new_service_deposit->patient_amount_paid = $patient_amount_paid;
$new_service_deposit->service_type = $service_type;
$new_service_deposit->receipt_number = $receipt_number;
$new_service_deposit->current_chart_of_accounts = $chart_of_account_string;
$new_service_deposit->order_ids = $order_ids;
$new_service_deposit->created_by = Auth::id();
$new_service_deposit->save();
record_cash_credits_to_daily_collection_account(($patient_amount_paid - $wallets_amount), $receipt_number, 'Services Patient Payment');
split_service_payment($new_service_deposit->id);
}
/**
* Pay for investigation items and add them to the investigations deposits table
*
* @param $patient_id
* @param $episode_id
* @param $investigation_id
* @param $investigation_prices
* @param $discount_status
* @param $receipt_number
* @param $patient_amount_paid
*/
public static function investigation_payment($patient_id, $episode_id, $investigation_id, $investigation_prices, $discount_status, $receipt_number, $patient_amount_paid, $order_ids, $wallets_amount)
{
$new_investigation_deposit = new InvestigationDeposit;
$chart_of_account_array = [];
$id_array = explode(',', $investigation_id);
for ($i = 0; $i < count($id_array); $i++) {
$account_id = get_name($id_array[$i], 'id', 'account_id', 'investigations');
$chart_of_account_array[$i] = $account_id;
}
$chart_of_account_string = implode(',', $chart_of_account_array);
$new_investigation_deposit->patient_id = $patient_id;
$new_investigation_deposit->episode_id = $episode_id;
$new_investigation_deposit->investigation_items = $investigation_id;
$new_investigation_deposit->investigation_amounts = $investigation_prices;
$new_investigation_deposit->discount_status = $discount_status;
$new_investigation_deposit->receipt_number = $receipt_number;
$new_investigation_deposit->patient_amount_paid = $patient_amount_paid;
$new_investigation_deposit->current_chart_of_accounts = $chart_of_account_string;
$new_investigation_deposit->order_ids = $order_ids;
$new_investigation_deposit->created_by = Auth::id();
$new_investigation_deposit->save();
record_cash_credits_to_daily_collection_account(($patient_amount_paid - $wallets_amount), $receipt_number, 'Investigation Patient Payment');
split_investigation_payment($new_investigation_deposit->id);
}
/**
* Pay for treatment items and add them to the treatment deposits table
*
* @param $patient_id
* @param $episode_id
* @param $treatment_id
* @param $treatment_items
* @param $treatment_quantities
* @param $treatment_amounts
* @param $treatment_subtotals
* @param $discount_status
* @param $receipt_number
* @param $patient_amount_paid
*/
public static function treatment_payment($patient_id, $episode_id, $treatment_id, $treatment_items, $treatment_quantities, $treatment_amounts,
$treatment_subtotals, $discount_status, $receipt_number, $patient_amount_paid, $order_ids, $wallets_amount)
{
$new_treatment_deposit = new TreatmentDeposits;
$id_array = explode(',', $treatment_items);
$cost_price_array = $chart_account_array = [];
$inventory_account = $cost_of_goods_account = '';
for ($i = 0; $i < count($id_array); $i++) {
$chart_account_array[$i] = $account_id = get_name($id_array[$i], 'id', 'account_id', 'drugs');
$inventory_account = get_name($id_array[$i], 'id', 'inventory_account', 'drugs');
$cost_of_goods_account = get_name($id_array[$i], 'id', 'cost_of_goods_account', 'drugs');
$cost_price = get_name($id_array[$i], 'id', 'cost_price', 'drugs');
$cost_price_array[$i] = $cost_price;
}
$cost_price_string = implode(',', $cost_price_array);
$chart_account_string = implode(',', $chart_account_array);
$new_treatment_deposit->patient_id = $patient_id;
$new_treatment_deposit->episode_id = $episode_id;
$new_treatment_deposit->treatment_id = $treatment_id;
$new_treatment_deposit->treatment_items = $treatment_items;
$new_treatment_deposit->treatment_quantities = $treatment_quantities;
$new_treatment_deposit->treatment_amounts = $treatment_amounts;
$new_treatment_deposit->treatment_subtotals = $treatment_subtotals;
$new_treatment_deposit->discount_status = $discount_status;
$new_treatment_deposit->receipt_number = $receipt_number;
$new_treatment_deposit->patient_amount_paid = $patient_amount_paid;
$new_treatment_deposit->current_chart_of_accounts = $chart_account_string;
$new_treatment_deposit->cost_of_goods_account = $cost_of_goods_account;
$new_treatment_deposit->inventory_account = $inventory_account;
$new_treatment_deposit->cost_price = $cost_price_string;
$new_treatment_deposit->order_ids = $order_ids;
$new_treatment_deposit->created_by = Auth::id();
$new_treatment_deposit->save();
record_cash_credits_to_daily_collection_account(($patient_amount_paid - $wallets_amount), $receipt_number, 'Treatment Patient Payment');
split_treatment_payment($new_treatment_deposit->id);
}
/**
* Pay for procedure items and add them to the procedure deposits table
*
* @param $patient_id
* @param $episode_id
* @param $procedures_items_string
* @param $procedure_prices_string
* @param $discount_status
* @param $receipt_number
* @param $patient_amount_paid
*/
public static function procedures_payment($patient_id, $episode_id, $procedures_items_string, $procedure_prices_string, $discount_status,
$receipt_number, $patient_amount_paid, $order_ids, $wallets_amount)
{
$new_procedure_deposit = new ProcedureDeposit;
$chart_of_account_array = [];
$id_array = explode(',', $procedures_items_string);
for ($i = 0; $i < count($id_array); $i++) {
$account_id = get_name($id_array[$i], 'id', 'account_id', 'procedures');
$chart_of_account_array[$i] = $account_id;
}
$chart_of_account_string = implode(',', $chart_of_account_array);
$new_procedure_deposit->patient_id = $patient_id;
$new_procedure_deposit->episode_id = $episode_id;
$new_procedure_deposit->procedure_items = $procedures_items_string;
$new_procedure_deposit->procedure_amounts = $procedure_prices_string;
$new_procedure_deposit->patient_amount_paid = $patient_amount_paid;
$new_procedure_deposit->payment_status = 1; //1 to mean paid
$new_procedure_deposit->receipt_number = $receipt_number;
$new_procedure_deposit->discount_status = $discount_status;
$new_procedure_deposit->current_chart_of_accounts = $chart_of_account_string;
$new_procedure_deposit->order_ids = $order_ids;
$new_procedure_deposit->created_by = Auth::id();
$new_procedure_deposit->save();
record_cash_credits_to_daily_collection_account(($patient_amount_paid - $wallets_amount), $receipt_number, 'Procedure Patient Payment');
split_procedure_payment($new_procedure_deposit->id);
}
/**
* Pay for sundries and add them to the sundry deposits table
*
* @param $patient_id
* @param $episode_id
* @param $sundry_items_string
* @param $sundry_quantity_string
* @param $sundry_prices_string
* @param $sundry_subtotal_string
* @param $discount_status
* @param $receipt_number
* @param $patient_amount_paid
*/
public static function sundries_payment($patient_id, $episode_id, $sundry_items_string, $sundry_quantity_string, $sundry_prices_string, $sundry_subtotal_string,
$discount_status, $receipt_number, $patient_amount_paid, $order_ids, $wallets_amount)
{
$new_sundry_deposit = new SundryDeposit;
$account_id = $inventory_account = $cost_of_goods_account = '';
$id_array = explode(',', $sundry_items_string);
$cost_price_array = $chart_of_account_array = [];
for ($i = 0; $i < count($id_array); $i++) {
$account_id = get_name($id_array[$i], 'id', 'account_id', 'sundries');
$inventory_account = get_name($id_array[$i], 'id', 'inventory_account', 'sundries');
$cost_of_goods_account = get_name($id_array[$i], 'id', 'cost_of_goods_account', 'sundries');
$cost_price = get_name($id_array[$i], 'id', 'cost_price', 'sundries');
$chart_of_account_array[$i] = $account_id;
$cost_price_array[$i] = $cost_price;
}
$chart_of_account_string = implode(',', $chart_of_account_array);
$cost_price_string = implode(',', $cost_price_array);
$new_sundry_deposit->patient_id = $patient_id;
$new_sundry_deposit->episode_id = $episode_id;
$new_sundry_deposit->sundry_items = $sundry_items_string;
$new_sundry_deposit->sundry_quantity = $sundry_quantity_string;
$new_sundry_deposit->sundry_amounts = $sundry_prices_string;
$new_sundry_deposit->sundry_subtotals = $sundry_subtotal_string;
$new_sundry_deposit->patient_amount_paid = $patient_amount_paid;
$new_sundry_deposit->receipt_number = $receipt_number;
$new_sundry_deposit->discount_status = $discount_status;
$new_sundry_deposit->created_by = Auth::id();
$new_sundry_deposit->current_chart_of_accounts = $chart_of_account_string;
$new_sundry_deposit->cost_of_goods_account = $cost_of_goods_account;
$new_sundry_deposit->inventory_account = $inventory_account;
$new_sundry_deposit->cost_price = $cost_price_string;
$new_sundry_deposit->order_ids = $order_ids;
$new_sundry_deposit->save();
record_cash_credits_to_daily_collection_account(($patient_amount_paid - $wallets_amount), $receipt_number, 'Sundries Patient Payment');
split_sundry_payment($new_sundry_deposit->id);
}
public static function eye_glasses_payment($patient_id, $episode_id, $items_string, $quantity_string, $prices_string, $subtotal_string,
$discount_status, $receipt_number, $patient_amount_paid, $order_ids, $wallets_amount) {
$new_deposit = new EyeGlassesDeposits;
$inventory_account = $cost_of_goods_account = '';
$id_array = explode(',', $items_string);
$cost_price_array = $chart_of_account_array = [];
for ($i = 0; $i < count($id_array); $i++) {
$account_id = get_name($id_array[$i], 'id', 'account_id', 'eye_glasses');
$inventory_account = get_name($id_array[$i], 'id', 'inventory_account', 'eye_glasses');
$cost_of_goods_account = get_name($id_array[$i], 'id', 'cost_of_goods_account', 'eye_glasses');
$cost_price = get_name($id_array[$i], 'id', 'buying_price', 'eye_glasses');
$chart_of_account_array[$i] = $account_id;
$cost_price_array[$i] = $cost_price;
}
$chart_of_account_string = implode(',', $chart_of_account_array);
$cost_price_string = implode(',', $cost_price_array);
$new_deposit->patient_id = $patient_id;
$new_deposit->episode_id = $episode_id;
$new_deposit->items = $items_string;
$new_deposit->quantity = $quantity_string;
$new_deposit->amounts = $prices_string;
$new_deposit->subtotals = $subtotal_string;
$new_deposit->patient_amount_paid = $patient_amount_paid;
$new_deposit->receipt_number = $receipt_number;
$new_deposit->discount_status = $discount_status;
$new_deposit->current_chart_of_accounts = $chart_of_account_string;
$new_deposit->cost_of_goods_account = $cost_of_goods_account;
$new_deposit->inventory_account = $inventory_account;
$new_deposit->cost_price = $cost_price_string;
$new_deposit->order_ids = $order_ids;
$new_deposit->created_by = Auth::id();
$new_deposit->save();
record_cash_credits_to_daily_collection_account(($patient_amount_paid - $wallets_amount), $receipt_number, 'Eye Glasses Patient Payment');
split_optics_payment($new_deposit->id);
}
public static function central_billing_payments($patient_id, $episode_id, $amounts_string, $items_string, $receipt_number, $tag_id, $quantity, $patient_amount, $pat_cat_paid, $order_ids) {
$central = new CentralBillingDeposits;
$central->patient_id = $patient_id;
$central->episode_id = $episode_id;
$central->items_amounts = $amounts_string;
$central->items_ids = $items_string;
$central->items_quantity = $quantity;
$central->patient_amount_paid = $patient_amount;
$central->patient_category_amount_paid = $pat_cat_paid;
$central->receipt_number = $receipt_number;
$central->tag_id = $tag_id;
$central->order_ids = $order_ids;
$central->created_by = Auth::id();
$central->save();
}
public static function chi_deposits($patient_id, $episode_id, $items_string, $receipt_number, $tag_id, $point_of_deposit, $quantity, $chi_to_pay,
$patient_co_payment, $patient_amount_paid, $patient_balance, $order_ids, $chi_amount_total) {
$chi = new CHIDeposit();
$chi->patient_id = $patient_id;
$chi->episode_id = $episode_id;
$chi->items_ids = $items_string;
$chi->items_quantity = $quantity;
$chi->chi_to_pay = $chi_to_pay;
$chi->patient_co_payment = $patient_co_payment;
$chi->patient_amount_paid = $patient_amount_paid;
$chi->chi_amount_total = $chi_amount_total;
$chi->patient_balance = $patient_balance;
$chi->receipt_number = $receipt_number;
$chi->tag_id = $tag_id;
$chi->point_of_deposit = $point_of_deposit;
$chi->order_ids = $order_ids;
$chi->created_by = Auth::id();
$chi->save();
}
public static function collective_bills_payments($patient_id, $amounts_string, $items_string, $receipt_number, $tag_id, $quantity, $patient_amount, $order_ids)
{
$collective = new CollectiveBillsDeposits();
$collective->patient_id = $patient_id;
$collective->items_ids = $items_string;
$collective->items_amounts = $amounts_string;
$collective->items_quantity = $quantity;
$collective->patient_amount_paid = $patient_amount;
$collective->receipt_number = $receipt_number;
$collective->tag_id = $tag_id;
$collective->order_ids = $order_ids;
$collective->created_by = Auth::id();
$collective->save();
}
public static function patient_one_off_discount($patient_id, $episode_id, $amount_discounted, $tag_id, $memo, $receipt_number)
{
$discount = new PatientOneOffDiscount();
$discount->patient_id = $patient_id;
$discount->episode_id = $episode_id;
$discount->amount_discounted = $amount_discounted;
$discount->tag_id = $tag_id;
$discount->memo = $memo;
$discount->receipt_number = $receipt_number;
$discount->created_by = Auth::id();
$discount->save();
}
}
@@ -0,0 +1,133 @@
<?php
namespace Modules\PatientFinance\Services;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Streamline\Models\ChartOfAccount;
use Streamline\Models\EyeGlasses;
use Streamline\Models\OrderedEyeGlasses;
use Streamline\Models\TrackReceipt;
class EyeGlassesDeposit {
public function store_payment(Request $request): string
{
$patient_id = $request->patient_id;
$episode_id = $request->episode_id;
$optical_ids_array = explode(",", $request->optical_ids);
$order_ids_array = explode(",", $request->order_ids);
$optical_subtotals_array = $request->optical_subtotal;
$optical_amounts_array = $request->optical_amount;
$optical_quantity_array = $request->quantity;
$tag_id = 19;
// update the track_receipt table to include created receipt
$track_receipts = new TrackReceipt;
$track_receipts->created_by = Auth::id();
$track_receipts->reason = 'Eye Glasses';
$track_receipts->save();
$receipt_number = sprintf("%04u", $track_receipts->id);
// update the chart of accounts balance for each item
for ($i = 0; $i < count($optical_ids_array); $i++) {
$optical_account = get_name($optical_ids_array[$i], "id", "account_id", "eye_glasses");
$account_balance = get_name($optical_account, "id", "balance", "chart_of_accounts");
$optical = EyeGlasses::withTrashed()->find($optical_ids_array[$i]);
$current_stock = $optical->pharmacy_stock;
$new_stock = $current_stock - $optical_quantity_array[$i];
$optical->pharmacy_stock = $new_stock;
/**** batch tracking */
$auto_calculated_batches_used = get_lab_batches_to_use_based_on_needed_quantity($optical_ids_array[$i], 7, $optical_quantity_array[$i],'pharmacy_stock');
foreach ($auto_calculated_batches_used as $batch_number => $batch_qty) {
reduce_batch_item_from_pharmacy(7, $optical_ids_array[$i], $batch_number, $batch_qty, "ordered_eye_glasses", $order_ids_array[0], $patient_id);
}
/*** end batch tracking logic **/
$optical->update();
try {
$new_balance = $account_balance + $optical_subtotals_array[$i];
ChartOfAccount::where(['id' => $optical_account])->update(['balance' => $new_balance]);
} catch (\Exception $exception) {
// raise error and go back to warn the person
}
}
// 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);
}
// 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);
}
// 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);
}
// 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;
// add to discounts table
DepositHelpers::discount($request->hospital_to_pay_general_discount, $patient_id, $episode_id, $receipt_number, 'Optical Items');
}
// 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(',', $optical_subtotals_array),
implode(',', $optical_ids_array), $receipt_number, $tag_id,
implode(',', $optical_quantity_array),
$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::eye_glasses_payment(
$patient_id, $episode_id,
implode(',', $optical_ids_array),
implode(',', $optical_quantity_array),
implode(',', $optical_amounts_array),
implode(',', $optical_subtotals_array),
$discount_status, $receipt_number, $request->patient_to_pay, implode(",", $order_ids_array), $request->wallets_amount
);
}
} else {
// create a new optics payment record
DepositHelpers::eye_glasses_payment(
$patient_id, $episode_id,
implode(',', $optical_ids_array),
implode(',', $optical_quantity_array),
implode(',', $optical_amounts_array),
implode(',', $optical_subtotals_array),
$discount_status, $receipt_number, $request->patient_to_pay, implode(",", $order_ids_array), $request->wallets_amount
);
}
// update the payment status for the ordered ids to paid
for ($i = 0; $i < count($order_ids_array); $i++) {
OrderedEyeGlasses::where(['id' => $order_ids_array[$i]])->update(['payment_status' => 1]);
}
return $is_invoice . "," . $receipt_number;
}
}
@@ -0,0 +1,90 @@
<?php
namespace Modules\PatientFinance\Services;
use Illuminate\Http\Request;
use Streamline\Models\ChartOfAccount;
use Streamline\Models\TrackReceipt;
use Streamline\Models\OrderedInvestigation;
use Modules\PatientFinance\Services\DepositHelpers;
class InvestigationsDeposit {
public function store_payment(Request $request) {
$patient_id = $request->patient_id;
$episode_id = $request->episode_id;
$investigation_ids_array = explode(",", $request->investigation_ids);
$order_ids_array = explode(",", $request->order_ids);
$investigation_amounts_array = $request->investigation_amount;
$tag_id = 2;
// update the track_receipt table to include created receipt
$track_receipts = new TrackReceipt;
$track_receipts->created_by = auth()->user()->id;
$track_receipts->reason = 'Investigations';
$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($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]);
}
// 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);
}
// 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);
}
// 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);
}
// 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;
// add to discounts table
DepositHelpers::discount($request->hospital_to_pay_general_discount,$patient_id, $episode_id,$receipt_number,'Investigations');
}
// 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(',', $investigation_amounts_array),implode(',', $investigation_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::investigation_payment($patient_id, $episode_id, implode(',', $investigation_ids_array), implode(',', $investigation_amounts_array), $discount_status, $receipt_number, $request->patient_to_pay, implode(",", $order_ids_array), $request->wallets_amount);
}
} else {
// create a new investigation payment record
DepositHelpers::investigation_payment($patient_id, $episode_id, implode(',', $investigation_ids_array), implode(',', $investigation_amounts_array), $discount_status, $receipt_number, $request->patient_to_pay, implode(",", $order_ids_array), $request->wallets_amount);
}
// 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]);
}
return $is_invoice . "," . $receipt_number;
}
}
@@ -0,0 +1,94 @@
<?php
namespace Modules\PatientFinance\Services;
use Illuminate\Http\Request;
use Streamline\Models\ChartOfAccount;
use Streamline\Models\StaffPerformedService;
use Streamline\Models\TrackReceipt;
use Streamline\Models\OrderedProcedure;
use Modules\PatientFinance\Services\DepositHelpers;
class ProceduresDeposit {
public function store_payment(Request $request){
$patient_id = $request->patient_id;
$episode_id = $request->episode_id;
$procedure_ids_array = explode(",", $request->procedure_ids);
$order_ids_array = explode(",", $request->order_ids);
$procedure_amounts_array = $request->procedure_amount;
$tag_id = 4;
// update the track_receipt table to include created receipt
$track_receipts = new TrackReceipt;
$track_receipts->created_by = auth()->user()->id;
$track_receipts->reason = 'Procedures';
$track_receipts->save();
$receipt_number = sprintf("%04u", $track_receipts->id);
// 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]);
}
// 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);
}
// 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);
}
// 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);
}
// 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;
// add to discounts table
DepositHelpers::discount($request->hospital_to_pay_general_discount,$patient_id, $episode_id,$receipt_number,'Procedures');
}
// 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(',', $procedure_amounts_array),implode(',', $procedure_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::procedures_payment($patient_id, $episode_id, implode(',', $procedure_ids_array), implode(',', $procedure_amounts_array), $discount_status, $receipt_number, $request->patient_to_pay, implode(",", $order_ids_array), $request->wallets_amount);
}
} else {
// create a new investigation payment record
DepositHelpers::procedures_payment($patient_id, $episode_id, implode(',', $procedure_ids_array), implode(',', $procedure_amounts_array), $discount_status, $receipt_number, $request->patient_to_pay, implode(",", $order_ids_array), $request->wallets_amount);
}
// 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]);
// update the receipts for staff payments
StaffPerformedService::whereIn('id', explode(",", get_name($order_ids_array[$i], 'id', 'performed_id', 'ordered_procedures')))->update(['patient_receipt_number' => $receipt_number]);
}
return $is_invoice . "," . $receipt_number;
}
}
@@ -0,0 +1,101 @@
<?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;
}
}
@@ -0,0 +1,134 @@
<?php
namespace Modules\PatientFinance\Services;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;
use DB;
use Streamline\Models\Patient;
use Streamline\Models\PatientEpisode;
use Streamline\Models\Services;
use Streamline\Models\ChartOfAccount;
use Streamline\Models\TrackReceipt;
use Streamline\Models\OrderedSundry;
use Modules\PatientFinance\Services\DepositHelpers;
class SundriesDeposit
{
public function store_payment(Request $request)
{
$patient_id = $request->patient_id;
$episode_id = $request->episode_id;
$sundry_ids_array = explode(",", $request->sundry_ids);
$order_ids_array = explode(",", $request->order_ids);
$sundry_subtotals_array = $request->sundry_subtotal;
$sundry_amounts_array = $request->sundry_amount;
$sundry_quantity_array = $request->quantity;
$tag_id = 5;
// update the track_receipt table to include created receipt
$track_receipts = new TrackReceipt;
$track_receipts->created_by = auth()->user()->id;
$track_receipts->reason = 'Sundries';
$track_receipts->save();
$receipt_number = sprintf("%04u", $track_receipts->id);
// update the chart of accounts balance for each sundry item
for ($i = 0; $i < count($sundry_ids_array); $i++) {
$sundry_account = get_name($sundry_ids_array[$i], "id", "account_id", "sundries");
$account_balance = get_name($sundry_account, "id", "balance", "chart_of_accounts");
//removed and set it to reduce sundries at payment only
$sundry = \Streamline\Models\Sundry::withTrashed()->find($sundry_ids_array[$i]);
$current_stock = $sundry->pharmacy_stock;
$new_stock = $current_stock - $sundry_quantity_array[$i];
$sundry->pharmacy_stock = $new_stock;
$sundry->update();
/**** batch tracking */
$auto_calculated_batches_used = get_lab_batches_to_use_based_on_needed_quantity($sundry_ids_array[$i],2,$sundry_quantity_array[$i],'pharmacy_stock');
foreach ($auto_calculated_batches_used as $batch_number => $batch_qty) {
reduce_batch_item_stock(2,$sundry_ids_array[$i],$batch_number,'sundries','pharmacy_stock',$batch_qty,'ordered_sundries','Sundries',$order_ids_array[0],$patient_id);
// reduce_batch_item_from_pharmacy(2, $sundry_ids_array[$i], $batch_number, $batch_qty, "ordered_sundries", null, $patient_id);
}
/*** end batch tracking logic **/
$new_balance = $account_balance + $sundry_subtotals_array[$i];
ChartOfAccount::where(['id' => $sundry_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);
}
// 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);
}
// 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);
}
// 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;
// add to discounts table
DepositHelpers::discount($request->hospital_to_pay_general_discount, $patient_id, $episode_id, $receipt_number, 'Sundries');
}
// 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(',', $sundry_subtotals_array),
implode(',', $sundry_ids_array), $receipt_number, $tag_id,
implode(',', $sundry_quantity_array),
$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::sundries_payment(
$patient_id, $episode_id,
implode(',', $sundry_ids_array),
implode(',', $sundry_quantity_array),
implode(',', $sundry_amounts_array),
implode(',', $sundry_subtotals_array),
$discount_status, $receipt_number, $request->patient_to_pay, implode(",", $order_ids_array), $request->wallets_amount
);
}
} else {
// create a new sundries payment record
DepositHelpers::sundries_payment(
$patient_id, $episode_id,
implode(',', $sundry_ids_array),
implode(',', $sundry_quantity_array),
implode(',', $sundry_amounts_array),
implode(',', $sundry_subtotals_array),
$discount_status, $receipt_number, $request->patient_to_pay, implode(",", $order_ids_array), $request->wallets_amount
);
}
// 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]);
}
return $is_invoice . "," . $receipt_number;
}
}
@@ -0,0 +1,142 @@
<?php
namespace Modules\PatientFinance\Services;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Streamline\Models\ChartOfAccount;
use Streamline\Models\Drug;
use Streamline\Models\TrackReceipt;
use Streamline\Models\Treatment;
class TreatmentDeposit
{
public function store_payment(Request $request)
{
$patient_id = $request->patient_id;
$episode_id = $request->episode_id;
$treatment_id_array = is_array($request->treatment_id) ? $request->treatment_id : [];
$treatment_items_array = $request->treatment_item;
$treatment_subtotals_array = $request->treatment_subtotal;
$treatment_amounts_array = $request->treatment_amount;
$treatment_quantity_array = $request->treatment_quantity;
$tag_id = 3;
// update the track_receipt table to include created receipt
$track_receipts = new TrackReceipt;
$track_receipts->created_by = Auth::id();
$track_receipts->reason = 'Treatment';
$track_receipts->save();
$receipt_number = sprintf("%04u", $track_receipts->id);
// is_invoice flag for receipt purposes. Default is 0 for no
$is_invoice = 0;
if (is_array($treatment_items_array)) {
// update the chart of accounts balance for each treatment item
for ($i = 0; $i < count($treatment_items_array); $i++) {
$drug_account = get_name($treatment_items_array[$i], "id", "account_id", "drugs");
$account_balance = get_name($drug_account, "id", "balance", "chart_of_accounts");
if (get_inventory_reduction_point() == 2) {
$drug = Drug::withTrashed()->find($treatment_items_array[$i]);
$current_stock = $drug->pharmacy_stock;
$new_stock = $current_stock - $treatment_quantity_array[$i];
$drug->pharmacy_stock = $new_stock;
$drug->update();
//remember to check which batch has been used to ensure double entry
/**** batch tracking */
$auto_calculated_batches_used = get_pharmacy_batch_watcher_ids_to_use_based_on_needed_quantity($treatment_items_array[$i], 1, $treatment_quantity_array[$i]);
foreach ($auto_calculated_batches_used as $batch_number => $batch_qty) {
reduce_batch_item_stock(1,$treatment_items_array[$i],$batch_number,'drugs','pharmacy_stock',$batch_qty,'treatments','Drug',$treatment_id_array[0],$patient_id);
// reduce_batch_item_from_pharmacy(1, $treatment_items_array[$i], $batch_number, $batch_qty, "treatments", $treatment_id_array[0], $patient_id);
}
/*** end batch tracking logic **/
}
$new_balance = $account_balance + $treatment_subtotals_array[$i];
ChartOfAccount::where(['id' => $drug_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);
}
// 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);
}
// 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);
}
// 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;
// add to discounts table
DepositHelpers::discount($request->hospital_to_pay_general_discount, $patient_id, $episode_id, $receipt_number, 'Treatment');
}
// 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(',', $treatment_subtotals_array),
implode(',', $treatment_items_array),
$receipt_number, $tag_id, $treatment_quantity_array, $request->patient_category_to_pay, date('Y-m-d'), implode(",", $treatment_id_array)
);
// for co-payment people with some cash
if ($request->patient_to_pay > 0) {
DepositHelpers::treatment_payment(
$patient_id, $episode_id, $treatment_id_array[0],
implode(',', $treatment_items_array),
implode(',', $treatment_quantity_array),
implode(',', $treatment_amounts_array),
implode(',', $treatment_subtotals_array),
$discount_status, $receipt_number, $request->patient_to_pay, implode(",", $treatment_id_array), $request->wallets_amount
);
}
} else {
// create a new treatment payment record
DepositHelpers::treatment_payment(
$patient_id, $episode_id, $treatment_id_array[0],
implode(',', $treatment_items_array),
implode(',', $treatment_quantity_array),
implode(',', $treatment_amounts_array),
implode(',', $treatment_subtotals_array),
$discount_status, $receipt_number, $request->patient_to_pay, implode(",", $treatment_id_array), $request->wallets_amount
);
}
} else {
$receipt_number = 0;
}
// update the payment status for the ordered id to paid
for ($i = 0; $i < count($treatment_id_array); $i++) {
Treatment::where(['id' => $treatment_id_array[$i]])->update(['payment_status' => 1]);
}
// check if there are some treatment orders that were fully purchased elsewhere
// and clear them in case setting of 'treatments must be paid before dispensation' is active
if (isset($request->all_records_purchased_elsewhere)) {
for ($i = 0; $i < count($request->all_records_purchased_elsewhere); $i++) {
Treatment::where(['id' => $request->all_records_purchased_elsewhere[$i]])->update(['payment_status' => 1]);
}
}
return $is_invoice . "," . $receipt_number;
}
}