Unable to Dispense in OPENEMR 6.1 and 7.0

Hello people, nice to be here. I did a recent installation of openEMR 6.1.0 I noticed that after setting up the pharmacy to dispense drugs and sell (actually the drugs are free) I’m unable to successfully dispense drugs. I keep getting an error message:

“Internal error: the referenced encounter no longer exists. 1 0”

So i installed the latest patch for 6.1.0 (1). Thesame error keeps occurring. The have inputted their drugs, entered lots. But the app does not dispense.

I deployed a version 6.0.0 as a training platform and version 6.1.0 as Production. Surprisingly, the training platform dispenses normally.
I have also upgrade to 7.0.0 and installed the first patch. Now the vitals form only display blood pressure after entry, most of the other fields do not appear in the summary.

Am i doing something wrong? Please any help on resolving this?

OpenEMR Version
I’m using OpenEMR version 6.1.0
PHP Version 7.4.30

I’m using: Chrome

Operating System
I’m using: Ubuntu 18 as Server

Did you check the logs?
Was there anything pertinent in them?
Please paste them here (surround with three backticks (```) for readability.
You can also turn on User Debugging under Administration->Globals->Logging User Debugging Options=>All

Hi @techednig,

This is how you should go about this issue.

Disclaimer: I am on V6.1.0.

locate dispense_drug.php below


On line 48, include below line of code

$encounter = $_SESSION[“encounter”];

On line 81, change 0 to $encounter, see below.


$sale_id = sellDrug($drug_id, $quantity, $fee, $pid, 0, $prescription_id, $today, $user);


$sale_id = sellDrug($drug_id, $quantity, $fee, $pid, $encounter, $prescription_id, $today, $user);

Save and retry.

The error,

Internal error: the referenced encounter no longer exists. 1 0

1 is the PID, whereas 0 is the encounter which has been initialized to 0. However, there’s no encounter ID of 0 in the database. So what you need to do is to get the current encounter and dispense the drug accordingly.

Hope this helps for you. Because it helped me.


Thank you KKappiah,

I tried your suggestion on version 6.1.0 and Version 7.0 (Patch 1). They both worked fine. So i want to believe your solution solves the dispense problem between version 6.1 and 7.0 (patch 1).

I discovered another challenge on the vitals form, i will open a new thread for it.

Thank you

1 Like

i tried as just what u said and now i have a new error in V7
Inventory is not available for this order

Please help

Hello @Natt_SL_Limited,

You’re receiving this message because your inventory doesnt have the amount of drugs you’re dispensing.

Check your inventory if you have adequate stock for the specific item/drug you’re dispensing.

Adding the drug/item to the inventory doesnt mean you can start dispensing.

Please follow this link to see the implementation.

Thanks very much for your response, i did exactly what is stated on the thread but i got this error still.
I dont know if i placed the code the right way


You’ve done the right thing. Everything looks good.

The only problem here is the expiry date of the drugs. Make sure the drug you intend to dispense has not expired. From your screenshot it looks like you entered an invalid date.

Edit the lot by entering 2023-01-01 for the expiry date or set the expiry date into the future.

You don’t want to be unconsciously prescribe expired drugs to your patients.

1 Like

when i entered the date it comes up with 00.00 i dont know why

Hi @Natt_SL_Limited - welcome to the forum!
One thing that @kkappiah mentioned reminded me of a very obscure step in the dispensary module that is often omitted: a template needs to be made for every sig of the medication that will be administered.
See the main Pharmacy/ Dispensary wiki documents ,

and also in the document it refers to, see the ‘Templates’ section of part 1 of the doc, ’ Using OpenEMR Inventory-Dispensary Module for Non- Pharmaceutical Products’.

Not sure if this addresses your problem, but hope it fixes you up!
Best- Harley


@kkappiah thanks very much it works fine now.
the problem was the date as i was using d/m/y

1 Like

@htuck thanks very much for the help

You’re welcome, glad you got it working!
Best- Harley

1 Like

I have tried this solution `$encounter = $_SESSION[‘encounter’] ?? 0;

if (!AclMain::aclCheckCore(‘admin’, ‘drugs’)) {
echo (new TwigContainer(null, $GLOBALS[‘kernel’]))->getTwig()->render(‘core/unauthorized.html.twig’, [‘pageTitle’ => xl(“Dispense Drug”)]);

if (!$drug_id) {
$drug_id = 0;

if (!$prescription_id) {
$prescription_id = 0;

if (!$quantity) {
$quantity = 0;

if (!$fee) {
$fee = 0.00;

$inventory_id = 0;
$bad_lot_list = ‘’;
$today = date(‘Y-m-d’);

// If there is no sale_id then this is a new dispensation.
if (! $sale_id) {
// Post the order and update inventory, deal with errors.
if ($drug_id) {
$sale_id = sellDrug($drug_id, $quantity, $fee, $pid, $encounter, $prescription_id, $today, $user);
if (!$sale_id) {
die(xlt(‘Inventory is not available for this order.’));
}`but i keep getting the same error. Kindly assist. I am using the following openemr version:
Version Number

7.0.0 (2)

Sorry, I have identified the issue, I had not picked an encounter and that is why I could not dispense

This bug is still there in 7.0.2. I am struggling with it. It is giving this error on save and dispense. But alone saving it does well.