From 6da1cf832ba97db5ffebd47272015f2ecfa8c918 Mon Sep 17 00:00:00 2001 From: sunsetsystems Date: Fri, 14 Apr 2006 18:29:40 +0000 Subject: [PATCH] more support for dispensing of in-house drugs --- controllers/C_Prescription.class.php | 21 ++++-- interface/drugs/dispense_drug.php | 109 +++++++++++++++++++++++++++++++ library/classes/Prescription.class.php | 14 +++- sql/2_8_1-to-2_8_2_upgrade.sql | 25 ++++++- sql/database.sql | 27 ++++++-- templates/prescription/general_edit.html | 41 ++++++++++-- 6 files changed, 219 insertions(+), 18 deletions(-) create mode 100644 interface/drugs/dispense_drug.php diff --git a/controllers/C_Prescription.class.php b/controllers/C_Prescription.class.php index 3923cf9c5..71711076f 100644 --- a/controllers/C_Prescription.class.php +++ b/controllers/C_Prescription.class.php @@ -1,9 +1,9 @@ assign("TOP_ACTION", $GLOBALS['webroot']."/controller.php?" . "prescription" . "&"); $this->assign("STYLE", $GLOBALS['style']); $this->pconfig = $GLOBALS['oer_config']['prescriptions']; + + // Make an array of drug IDs and names for the template. + $drug_array = array(0 => "-- or select from inventory --"); + $res = sqlStatement("SELECT * FROM drugs ORDER BY name"); + while ($row = sqlFetchArray($res)) { + $drug_array[$row['drug_id']] = $row['name']; + if ($row['ndc_number']) { + $drug_array[$row['drug_id']] .= ' [' . $row['ndc_number'] . ']'; + } + // TBD: JavaScript variables for setting default options. + } + $this->assign("DRUG_ARRAY", $drug_array); + } function default_action() { diff --git a/interface/drugs/dispense_drug.php b/interface/drugs/dispense_drug.php new file mode 100644 index 000000000..6e7ddb8a2 --- /dev/null +++ b/interface/drugs/dispense_drug.php @@ -0,0 +1,109 @@ + + // + // This program is free software; you can redistribute it and/or + // modify it under the terms of the GNU General Public License + // as published by the Free Software Foundation; either version 2 + // of the License, or (at your option) any later version. + + require_once("../globals.php"); + require_once("$srcdir/acl.inc"); + require_once("drugs.inc.php"); + require_once($GLOBALS['fileroot'] . "/library/classes/class.phpmailer.php"); + + function send_email($subject, $body) { + $recipient = $GLOBALS['practice_return_email_path']; + $mail = new PHPMailer(); + $mail->SetLanguage("en", $GLOBALS['fileroot'] . "/library/" ); + $mail->From = $recipient; + $mail->FromName = 'In-House Pharmacy'; + $mail->isMail(); + $mail->Host = "localhost"; + $mail->Mailer = "mail"; + $mail->Body = $body; + $mail->Subject = $subject; + $mail->AddAddress($recipient); + if(!$mail->Send()) { + $die("There has been a mail error sending to " . $recipient . + " " . $mail->ErrorInfo); + } + } + + $sales_id = $_REQUEST['sales_id']; + $drug_id = $_REQUEST['drug_id']; + $prescription_id = $_REQUEST['prescription']; + $quantity = $_REQUEST['quantity']; + $fee = $_REQUEST['fee']; + $user = $_SESSION['authUser']; + + if (!acl_check('admin', 'drugs')) die("Not authorized!"); + + 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 (! $sales_id) { + // Find and update inventory, deal with errors. + // + if ($drug_id) { + $res = sqlStatement("SELECT * FROM drug_inventory WHERE " . + "drug_id = '$drug_id' AND on_hand > 0 " . + "ORDER BY expiration, inventory_id"); + while ($row = sqlFetchArray($res)) { + if ($row['expiration'] > $today && $row['on_hand'] >= $quantity) { + break; + } + $tmp = $row['lot_number']; + if (! $tmp) $tmp = '[missing lot number]'; + if ($bad_lot_list) $bad_lot_list .= ', '; + $bad_lot_list .= $tmp; + } + + if ($bad_lot_list) { + send_email("Lot destruction needed", + "The following lot(s) are expired or too small to fill prescription " . + "$prescription_id and should be destroyed: $bad_lot_list\n"); + } + + if (! $row) { + die("Inventory is not available for this order."); + } + + $inventory_id = $row['inventory_id']; + + sqlStatement("UPDATE drug_inventory SET " . + "on_hand = on_hand - $quantity " . + "WHERE inventory_id = $inventory_id"); + + $rowsum = sqlQuery("SELECT sum(on_hand) AS sum FROM drug_inventory WHERE " . + "drug_id = '$drug_id' AND on_hand > '$quantity' AND expiration > CURRENT_DATE"); + $rowdrug = sqlQuery("SELECT * FROM drugs WHERE " . + "drug_id = '$drug_id'"); + if ($rowsum['sum'] <= $rowdrug['reorder_point']) { + send_email("Drug re-order required", + "Drug '" . $rowdrug['name'] . "' has reached its reorder point.\n"); + } + + // TBD: Set and check a reorder notification date so we don't + // send zillions of redundant emails. + + } + + $sales_id = sqlInsert("INSERT INTO drug_sales ( " . + "drug_id, inventory_id, prescription_id, pid, user, sale_date, quantity, fee " . + ") VALUES ( " . + "'$drug_id', '$inventory_id', '$prescription_id', '$pid', '$user', '$today', + '$quantity', '$fee' " . + ")"); + + echo "Inventory has been updated. Here we will send a PDF for the bottle label.\n"; + } + + // TBD: Generate the bottle label PDF for the sale identified by $sales_id. + +?> diff --git a/library/classes/Prescription.class.php b/library/classes/Prescription.class.php index 2812ae789..1d011999e 100644 --- a/library/classes/Prescription.class.php +++ b/library/classes/Prescription.class.php @@ -102,6 +102,7 @@ class Prescription extends ORDataObject { var $refills; var $per_refill; + var $drug_id; /** * Constructor sets all Prescription attributes to their default value @@ -132,6 +133,9 @@ class Prescription extends ORDataObject { $this->date_modified = date("Y-m-d"); $this->per_refill = 0; $this->note = ""; + + $this->drug_id = 0; + for($i=0;$i<21;$i++) { $this->refills_array[$i] = sprintf("%02d",$i); } @@ -181,7 +185,8 @@ class Prescription extends ORDataObject { ."Interval: " .$this->interval_array[$this->interval]. "\n" ."Substitute: " . $this->substitute_array[$this->substitute]. "\n" ."Refills: " . $this->refills. "\n" - ."Per Refill: " . $this->per_refill; + ."Per Refill: " . $this->per_refill . "\n" + ."Drug ID: " . $this->drug_id; if ($html) { return nl2br($string); @@ -408,6 +413,13 @@ class Prescription extends ORDataObject { } } + function set_drug_id($drug_id) { + $this->drug_id = $drug_id; + } + function get_drug_id() { + return $this->drug_id; + } + function get_prescription_display() { $pconfig = $GLOBALS['oer_config']['prescriptions']; diff --git a/sql/2_8_1-to-2_8_2_upgrade.sql b/sql/2_8_1-to-2_8_2_upgrade.sql index e11980b70..ba91489cc 100644 --- a/sql/2_8_1-to-2_8_2_upgrade.sql +++ b/sql/2_8_1-to-2_8_2_upgrade.sql @@ -286,9 +286,10 @@ ALTER TABLE form_vitals CREATE TABLE drugs ( drug_id int(11) NOT NULL auto_increment, name varchar(255) NOT NULL, - ndc_number varchar(255) NOT NULL DEFAULT '', + ndc_number varchar(20) NOT NULL DEFAULT '', on_order int(11) NOT NULL DEFAULT 0, reorder_point int(11) NOT NULL DEFAULT 0, + last_notify date NOT NULL DEFAULT '0000-00-00', reactions text NOT NULL DEFAULT '', form int(3) NOT NULL DEFAULT 0, dosage varchar(10) NOT NULL DEFAULT '', @@ -303,10 +304,28 @@ CREATE TABLE drugs ( ) TYPE=MyISAM; CREATE TABLE drug_inventory ( + inventory_id int(11) NOT NULL auto_increment, drug_id int(11) NOT NULL, - lot_number varchar(255) NOT NULL DEFAULT '', + lot_number varchar(20) NOT NULL DEFAULT '', expiration date DEFAULT NULL, manufacturer varchar(255) NOT NULL DEFAULT '', on_hand int(11) NOT NULL DEFAULT 0, - PRIMARY KEY (drug_id, lot_number) + last_notify date NOT NULL DEFAULT '0000-00-00', + PRIMARY KEY (inventory_id) ) TYPE=MyISAM; + +CREATE TABLE drug_sales ( + sale_id int(11) NOT NULL auto_increment, + drug_id int(11) NOT NULL, + inventory_id int(11) NOT NULL, + prescription_id int(11) NOT NULL DEFAULT 0, + pid int(11) NOT NULL DEFAULT 0, + user varchar(255) NOT NULL DEFAULT '', + sale_date date NOT NULL, + quantity int(11) NOT NULL DEFAULT 0, + fee decimal(7,2) NOT NULL DEFAULT 0.00, + PRIMARY KEY (sale_id) +) TYPE=MyISAM; + +ALTER TABLE prescriptions + ADD drug_id int(11) NOT NULL DEFAULT 0; diff --git a/sql/database.sql b/sql/database.sql index 953f93926..fbd45a581 100644 --- a/sql/database.sql +++ b/sql/database.sql @@ -27576,6 +27576,7 @@ CREATE TABLE `prescriptions` ( `provider_id` int(11) default NULL, `start_date` date default NULL, `drug` varchar(150) default NULL, + `drug_id` int(11) NOT NULL DEFAULT 0, `form` int(3) default NULL, `dosage` varchar(10) default NULL, `quantity` int(11) default NULL, @@ -28322,14 +28323,15 @@ CREATE TABLE `array` ( `array_value` longtext ) TYPE=MyISAM; -## Drug information database. +## Drug information. CREATE TABLE drugs ( drug_id int(11) NOT NULL auto_increment, name varchar(255) NOT NULL, - ndc_number varchar(255) NOT NULL DEFAULT '', + ndc_number varchar(20) NOT NULL DEFAULT '', on_order int(11) NOT NULL DEFAULT 0, reorder_point int(11) NOT NULL DEFAULT 0, + last_notify date NOT NULL DEFAULT '0000-00-00', reactions text NOT NULL DEFAULT '', form int(3) NOT NULL DEFAULT 0, dosage varchar(10) NOT NULL DEFAULT '', @@ -28343,13 +28345,26 @@ CREATE TABLE drugs ( PRIMARY KEY (drug_id) ) TYPE=MyISAM; -## Drug inventory database. - CREATE TABLE drug_inventory ( + inventory_id int(11) NOT NULL auto_increment, drug_id int(11) NOT NULL, - lot_number varchar(255) NOT NULL DEFAULT '', + lot_number varchar(20) NOT NULL DEFAULT '', expiration date DEFAULT NULL, manufacturer varchar(255) NOT NULL DEFAULT '', on_hand int(11) NOT NULL DEFAULT 0, - PRIMARY KEY (drug_id, lot_number) + last_notify date NOT NULL DEFAULT '0000-00-00', + PRIMARY KEY (inventory_id) +) TYPE=MyISAM; + +CREATE TABLE drug_sales ( + sale_id int(11) NOT NULL auto_increment, + drug_id int(11) NOT NULL, + inventory_id int(11) NOT NULL, + prescription_id int(11) NOT NULL DEFAULT 0, + pid int(11) NOT NULL DEFAULT 0, + user varchar(255) NOT NULL DEFAULT '', + sale_date date NOT NULL, + quantity int(11) NOT NULL DEFAULT 0, + fee decimal(7,2) NOT NULL DEFAULT 0.00, + PRIMARY KEY (sale_id) ) TYPE=MyISAM; diff --git a/templates/prescription/general_edit.html b/templates/prescription/general_edit.html index a7f2e17d4..919d237d9 100644 --- a/templates/prescription/general_edit.html +++ b/templates/prescription/general_edit.html @@ -21,6 +21,18 @@ textarea { } --> + + {/literal} @@ -36,19 +48,31 @@ textarea { Provider {html_options name="provider_id" options=$prescription->provider->utility_provider_array() selected=$prescription->provider->get_id()} + + Drug + +{if $DRUG_ARRAY} + +   in-house + + {html_options name="drug_id" options=$DRUG_ARRAY selected=$prescription->get_drug_id()} + + +{/if} + Quantity @@ -65,7 +89,7 @@ textarea { Take - in + in @@ -86,11 +110,20 @@ textarea { Notes - + - + + + {if $DRUG_ARRAY}{if $prescription->id} +           + + + units, $ + + {/if}{/if} + -- 2.11.4.GIT