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