added lists for drug attributes
[openemr.git] / interface / drugs / drugs.inc.php
bloba06901e42f0a617ac4e5a4fc00c94e4a358f3d38
1 <?php
2 // Copyright (C) 2006, 2008 Rod Roark <rod@sunsetsystems.com>
3 //
4 // This program is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU General Public License
6 // as published by the Free Software Foundation; either version 2
7 // of the License, or (at your option) any later version.
9 function load_drug_attributes($id) {
10 $arr = array(0 => '');
11 $res = sqlStatement("SELECT * FROM list_options WHERE list_id = '$id' ORDER BY seq");
12 while ($row = sqlFetchArray($res)) $arr[$row['option_id']] = $row['title'];
13 return $arr;
16 $form_array = load_drug_attributes('drug_form');
17 $unit_array = load_drug_attributes('drug_units');
18 $route_array = load_drug_attributes('drug_route');
19 $interval_array = load_drug_attributes('drug_interval');
21 // These were adapted from library/classes/Prescription.class.php:
23 // $form_array = array('', xl('suspension'), xl('tablet'), xl('capsule'), xl('solution'), xl('tsp'),
24 // xl('ml'), xl('units'), xl('inhalations'), xl('gtts(drops)'));
25 // $unit_array = array('', 'mg', 'mg/1cc', 'mg/2cc', 'mg/3cc', 'mg/4cc',
26 // 'mg/5cc', 'grams', 'mcg');
27 // $route_array = array('', xl('Per Oris'), xl('Per Rectum'), xl('To Skin'),
28 // xl('To Affected Area'), xl('Sublingual'), xl('OS'), xl('OD'), xl('OU'), xl('SQ'), xl('IM'), xl('IV'),
29 // xl('Per Nostril'));
30 // $interval_array = array('', 'b.i.d.', 't.i.d.', 'q.i.d.', 'q.3h', 'q.4h',
31 // 'q.5h', 'q.6h', 'q.8h', 'q.d.');
32 // $interval_array_verbose = array('',
33 // xl('twice daily'),
34 // xl('3 times daily'),
35 // xl('4 times daily'),
36 // xl('every 3 hours'),
37 // xl('every 4 hours'),
38 // xl('every 5 hours'),
39 // xl('every 6 hours'),
40 // xl('every 8 hours'),
41 // xl('daily'));
42 // $route_array_verbose = array('',
43 // xl('by mouth'),
44 // xl('rectally'),
45 // xl('to skin'),
46 // xl('to affected area'),
47 // xl('under tongue'),
48 // xl('in left eye'),
49 // xl('in right eye'),
50 // xl('in each eye'),
51 // xl('subcutaneously'),
52 // xl('intramuscularly'),
53 // xl('intravenously'),
54 // xl('in nostril'));
56 $substitute_array = array('', xl('Allowed'), xl('Not Allowed'));
58 function send_drug_email($subject, $body) {
59 require_once ($GLOBALS['srcdir'] . "/classes/class.phpmailer.php");
60 $recipient = $GLOBALS['practice_return_email_path'];
61 $mail = new PHPMailer();
62 $mail->SetLanguage("en", $GLOBALS['fileroot'] . "/library/" );
63 $mail->From = $recipient;
64 $mail->FromName = 'In-House Pharmacy';
65 $mail->isMail();
66 $mail->Host = "localhost";
67 $mail->Mailer = "mail";
68 $mail->Body = $body;
69 $mail->Subject = $subject;
70 $mail->AddAddress($recipient);
71 if(!$mail->Send()) {
72 die("There has been a mail error sending to " . $recipient .
73 " " . $mail->ErrorInfo);
77 function sellDrug($drug_id, $quantity, $fee, $patient_id=0, $encounter_id=0,
78 $prescription_id=0, $sale_date='', $user='') {
80 if (empty($patient_id)) $patient_id = $GLOBALS['pid'];
81 if (empty($sale_date)) $sale_date = date('Y-m-d');
82 if (empty($user)) $user = $_SESSION['authUser'];
84 // Find and update inventory, deal with errors.
86 $res = sqlStatement("SELECT * FROM drug_inventory WHERE " .
87 // "drug_id = '$drug_id' AND on_hand > 0 AND destroy_date IS NULL " .
88 "drug_id = '$drug_id' AND destroy_date IS NULL " .
89 "ORDER BY expiration, inventory_id");
90 $rowsleft = mysql_num_rows($res);
91 $bad_lot_list = '';
92 while ($row = sqlFetchArray($res)) {
93 if ($row['expiration'] > $sale_date && $row['on_hand'] >= $quantity) {
94 break;
96 if ($row['on_hand'] > 0) {
97 $tmp = $row['lot_number'];
98 if (! $tmp) $tmp = '[missing lot number]';
99 if ($bad_lot_list) $bad_lot_list .= ', ';
100 $bad_lot_list .= $tmp;
102 if (! --$rowsleft) break; // to retain the last $row
105 if ($bad_lot_list) {
106 send_drug_email("Lot destruction needed",
107 "The following lot(s) are expired or too small to fill the order for " .
108 "patient $patient_id and should be destroyed: $bad_lot_list\n");
111 if (! $row) return 0; // No undestroyed lots exist
113 $inventory_id = $row['inventory_id'];
115 sqlStatement("UPDATE drug_inventory SET " .
116 "on_hand = on_hand - $quantity " .
117 "WHERE inventory_id = $inventory_id");
119 $rowsum = sqlQuery("SELECT sum(on_hand) AS sum FROM drug_inventory WHERE " .
120 "drug_id = '$drug_id' AND on_hand > '$quantity' AND expiration > CURRENT_DATE");
121 $rowdrug = sqlQuery("SELECT * FROM drugs WHERE " .
122 "drug_id = '$drug_id'");
123 if ($rowsum['sum'] <= $rowdrug['reorder_point']) {
124 send_drug_email("Product re-order required",
125 "Product '" . $rowdrug['name'] . "' has reached its reorder point.\n");
128 // TBD: Maybe set and check a reorder notification date so we don't
129 // send zillions of redundant emails.
131 return sqlInsert("INSERT INTO drug_sales ( " .
132 "drug_id, inventory_id, prescription_id, pid, encounter, user, " .
133 "sale_date, quantity, fee ) VALUES ( " .
134 "'$drug_id', '$inventory_id', '$prescription_id', '$patient_id', " .
135 "'$encounter_id', '$user', '$sale_date', '$quantity', '$fee' )");