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