added lists for drug attributes
[openemr.git] / interface / drugs / dispense_drug.php
blob57bc9ad2859d2513d430b14dc0afeb1a00680c67
1 <?php
2 // Copyright (C) 2006 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 require_once("../globals.php");
10 require_once("$srcdir/acl.inc");
11 require_once("drugs.inc.php");
12 require_once($GLOBALS['fileroot'] . "/library/classes/class.phpmailer.php");
13 require_once($GLOBALS['fileroot'] . "/library/classes/class.ezpdf.php");
15 function send_email($subject, $body) {
16 $recipient = $GLOBALS['practice_return_email_path'];
17 $mail = new PHPMailer();
18 $mail->SetLanguage("en", $GLOBALS['fileroot'] . "/library/" );
19 $mail->From = $recipient;
20 $mail->FromName = 'In-House Pharmacy';
21 $mail->isMail();
22 $mail->Host = "localhost";
23 $mail->Mailer = "mail";
24 $mail->Body = $body;
25 $mail->Subject = $subject;
26 $mail->AddAddress($recipient);
27 if(!$mail->Send()) {
28 die("There has been a mail error sending to " . $recipient .
29 " " . $mail->ErrorInfo);
33 $sale_id = $_REQUEST['sale_id'];
34 $drug_id = $_REQUEST['drug_id'];
35 $prescription_id = $_REQUEST['prescription'];
36 $quantity = $_REQUEST['quantity'];
37 $fee = $_REQUEST['fee'];
38 $user = $_SESSION['authUser'];
40 if (!acl_check('admin', 'drugs')) die("Not authorized!");
42 if (!$drug_id ) $drug_id = 0;
43 if (!$prescription_id) $prescription_id = 0;
44 if (!$quantity ) $quantity = 0;
45 if (!$fee ) $fee = 0.00;
47 $inventory_id = 0;
48 $bad_lot_list = '';
49 $today = date('Y-m-d');
51 // If there is no sale_id then this is a new dispensation.
53 if (! $sale_id) {
54 // Post the order and update inventory, deal with errors.
56 if ($drug_id) {
57 $sale_id = sellDrug($drug_id, $quantity, $fee, $pid, 0, $prescription_id, $today, $user);
58 if (!$sale_id) die("Inventory is not available for this order.");
60 /******************************************************************
61 $res = sqlStatement("SELECT * FROM drug_inventory WHERE " .
62 "drug_id = '$drug_id' AND on_hand > 0 AND destroy_date IS NULL " .
63 "ORDER BY expiration, inventory_id");
64 while ($row = sqlFetchArray($res)) {
65 if ($row['expiration'] > $today && $row['on_hand'] >= $quantity) {
66 break;
68 $tmp = $row['lot_number'];
69 if (! $tmp) $tmp = '[missing lot number]';
70 if ($bad_lot_list) $bad_lot_list .= ', ';
71 $bad_lot_list .= $tmp;
74 if ($bad_lot_list) {
75 send_email("Lot destruction needed",
76 "The following lot(s) are expired or too small to fill prescription " .
77 "$prescription_id and should be destroyed: $bad_lot_list\n");
80 if (! $row) {
81 die("Inventory is not available for this order.");
84 $inventory_id = $row['inventory_id'];
86 sqlStatement("UPDATE drug_inventory SET " .
87 "on_hand = on_hand - $quantity " .
88 "WHERE inventory_id = $inventory_id");
90 $rowsum = sqlQuery("SELECT sum(on_hand) AS sum FROM drug_inventory WHERE " .
91 "drug_id = '$drug_id' AND on_hand > '$quantity' AND expiration > CURRENT_DATE");
92 $rowdrug = sqlQuery("SELECT * FROM drugs WHERE " .
93 "drug_id = '$drug_id'");
94 if ($rowsum['sum'] <= $rowdrug['reorder_point']) {
95 send_email("Drug re-order required",
96 "Drug '" . $rowdrug['name'] . "' has reached its reorder point.\n");
99 // TBD: Set and check a reorder notification date so we don't
100 // send zillions of redundant emails.
101 ******************************************************************/
103 } // end if $drug_id
105 /*******************************************************************
106 $sale_id = sqlInsert("INSERT INTO drug_sales ( " .
107 "drug_id, inventory_id, prescription_id, pid, user, sale_date, quantity, fee " .
108 ") VALUES ( " .
109 "'$drug_id', '$inventory_id', '$prescription_id', '$pid', '$user', '$today',
110 '$quantity', '$fee' " .
111 ")");
112 *******************************************************************/
114 if (!$sale_id) die("Internal error, no drug ID specified!");
116 } // end if not $sale_id
118 // Generate the bottle label for the sale identified by $sale_id.
120 // Get details for what we guess is the primary facility.
121 $frow = sqlQuery("SELECT * FROM facility " .
122 "ORDER BY billing_location DESC, accepts_assignment DESC, id LIMIT 1");
124 // Get everything else.
125 $row = sqlQuery("SELECT " .
126 "s.pid, s.quantity, s.prescription_id, " .
127 "i.manufacturer, i.lot_number, i.expiration, " .
128 "d.name, d.ndc_number, d.form, d.size, d.unit, " .
129 "r.date_modified, r.dosage, r.route, r.interval, r.substitute, r.refills, " .
130 "p.fname, p.lname, p.mname, " .
131 "u.fname AS ufname, u.mname AS umname, u.lname AS ulname " .
132 "FROM drug_sales AS s, drug_inventory AS i, drugs AS d, " .
133 "prescriptions AS r, patient_data AS p, users AS u WHERE " .
134 "s.sale_id = '$sale_id' AND " .
135 "i.inventory_id = s.inventory_id AND " .
136 "d.drug_id = i.drug_id AND " .
137 "r.id = s.prescription_id AND " .
138 "p.pid = s.pid AND " .
139 "u.id = r.provider_id");
141 $dconfig = $GLOBALS['oer_config']['druglabels'];
143 $header_text = $row['ufname'] . ' ' . $row['umname'] . ' ' . $row['ulname'] . "\n" .
144 $frow['street'] . "\n" .
145 $frow['city'] . ', ' . $frow['state'] . ' ' . $frow['postal_code'] .
146 ' ' . $frow['phone'] . "\n";
147 if ($dconfig['disclaimer']) $header_text .= $dconfig['disclaimer'] . "\n";
149 $label_text = $row['fname'] . ' ' . $row['lname'] . ' ' . $row['date_modified'] .
150 ' RX#' . sprintf('%06u', $row['prescription_id']) . "\n" .
151 $row['name'] . ' ' . $row['size'] . ' ' .
152 $unit_array[$row['unit']] . ' QTY ' .
153 $row['quantity'] . "\n" .
154 'Take ' . $row['dosage'] . ' ' . $form_array[$row['form']] .
155 ($row['dosage'] > 1 ? 's ' : ' ') .
156 // $interval_array_verbose[$row['interval']] . ' ' .
157 $interval_array[$row['interval']] . ' ' .
158 // $route_array_verbose[$row['route']] . "\n" .
159 $route_array[$row['route']] . "\n" .
160 'Lot ' . $row['lot_number'] . ' Exp ' . $row['expiration'] . "\n" .
161 'NDC ' . $row['ndc_number'] . ' ' . $row['manufacturer'];
163 // if ($row['refills']) {
164 // // Find out how many times this prescription has been filled/refilled.
165 // $refills_row = sqlQuery("SELECT count(*) AS count FROM drug_sales " .
166 // "WHERE prescription_id = '" . $row['prescription_id'] .
167 // "' AND quantity > 0");
168 // $label_text .= ($refills_row['count'] - 1) . ' of ' . $row['refills'] . ' refills';
169 // }
171 // We originally went for PDF output on the theory that output formatting
172 // would be more controlled. However the clumisness of invoking a PDF
173 // viewer from the browser becomes intolerable in a POS environment, and
174 // printing HTML is much faster and easier if the browser's page setup is
175 // configured properly.
177 if (false) { // if PDF output is desired
178 $pdf =& new Cezpdf($dconfig['paper_size']);
179 $pdf->ezSetMargins($dconfig['top'],$dconfig['bottom'],$dconfig['left'],$dconfig['right']);
180 $pdf->selectFont($GLOBALS['fileroot'] . "/library/fonts/Helvetica.afm");
181 $pdf->ezSetDy(20); // dunno why we have to do this...
182 $pdf->ezText($header_text, 7, array('justification'=>'center'));
183 if(!empty($dconfig['logo'])) {
184 $pdf->ezSetDy(-5); // add space (move down) before the image
185 $pdf->ezImage($dconfig['logo'], 0, 180, '', 'left');
186 $pdf->ezSetDy(8); // reduce space (move up) after the image
188 $pdf->ezText($label_text, 9, array('justification'=>'center'));
189 $pdf->ezStream();
191 else { // HTML output
193 <html>
194 <head>
195 <? html_header_show();?>
196 <style type="text/css">
197 body {
198 font-family: sans-serif;
199 font-size: 9pt;
200 font-weight: normal;
202 .labtop {
203 color: #000000;
204 font-family: sans-serif;
205 font-size: 7pt;
206 font-weight: normal;
207 text-align: center;
208 padding-bottom: 1pt;
210 .labbot {
211 color: #000000;
212 font-family: sans-serif;
213 font-size: 9pt;
214 font-weight: normal;
215 text-align: center;
216 padding-top: 2pt;
218 </style>
219 <title><?php xl('Prescription Label','e') ; ?></title>
220 </head>
221 <body leftmargin='0' topmargin='0' marginwidth='0' marginheight='0'>
222 <center>
223 <table border='0' cellpadding='0' cellspacing='0' style='width: 200pt'>
224 <tr><td class="labtop" nowrap>
225 <?php echo nl2br($header_text); ?>
226 </td></tr>
227 <tr><td style='background-color: #000000; height: 5pt;'></td></tr>
228 <tr><td class="labbot" nowrap>
229 <?php echo nl2br($label_text); ?>
230 </td></tr>
231 </table>
232 </center>
233 <script language="JavaScript">
234 window.print();
235 </script>
236 </body>
237 </html>
238 <?php