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