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; } }