Merge pull request #1154 for user interface improvements of left nav and main title
[openemr.git] / interface / drugs / dispense_drug.php
blob033d3cb9668b13f912fc47fa8c7c3070f74c9985
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("$srcdir/options.inc.php");
14 use OpenEMR\Services\FacilityService;
16 $facilityService = new FacilityService();
18 function send_email($subject, $body)
20 $recipient = $GLOBALS['practice_return_email_path'];
21 if (empty($recipient)) {
22 return;
25 $mail = new PHPMailer();
26 $mail->From = $recipient;
27 $mail->FromName = 'In-House Pharmacy';
28 $mail->isMail();
29 $mail->Host = "localhost";
30 $mail->Mailer = "mail";
31 $mail->Body = $body;
32 $mail->Subject = $subject;
33 $mail->AddAddress($recipient);
34 if (!$mail->Send()) {
35 error_log(xl('There has been a mail error sending to', '', '', ' ') . $recipient .
36 " " . $mail->ErrorInfo);
40 $sale_id = $_REQUEST['sale_id'];
41 $drug_id = $_REQUEST['drug_id'];
42 $prescription_id = $_REQUEST['prescription'];
43 $quantity = $_REQUEST['quantity'];
44 $fee = $_REQUEST['fee'];
45 $user = $_SESSION['authUser'];
47 if (!acl_check('admin', 'drugs')) {
48 die(xl('Not authorized'));
51 if (!$drug_id) {
52 $drug_id = 0;
55 if (!$prescription_id) {
56 $prescription_id = 0;
59 if (!$quantity) {
60 $quantity = 0;
63 if (!$fee) {
64 $fee = 0.00;
67 $inventory_id = 0;
68 $bad_lot_list = '';
69 $today = date('Y-m-d');
71 // If there is no sale_id then this is a new dispensation.
73 if (! $sale_id) {
74 // Post the order and update inventory, deal with errors.
76 if ($drug_id) {
77 $sale_id = sellDrug($drug_id, $quantity, $fee, $pid, 0, $prescription_id, $today, $user);
78 if (!$sale_id) {
79 die(xlt('Inventory is not available for this order.'));
82 /******************************************************************
83 $res = sqlStatement("SELECT * FROM drug_inventory WHERE " .
84 "drug_id = '$drug_id' AND on_hand > 0 AND destroy_date IS NULL " .
85 "ORDER BY expiration, inventory_id");
86 while ($row = sqlFetchArray($res)) {
87 if ($row['expiration'] > $today && $row['on_hand'] >= $quantity) {
88 break;
90 $tmp = $row['lot_number'];
91 if (! $tmp) $tmp = '[missing lot number]';
92 if ($bad_lot_list) $bad_lot_list .= ', ';
93 $bad_lot_list .= $tmp;
96 if ($bad_lot_list) {
97 send_email("Lot destruction needed",
98 "The following lot(s) are expired or too small to fill prescription " .
99 "$prescription_id and should be destroyed: $bad_lot_list\n");
102 if (! $row) {
103 die("Inventory is not available for this order.");
106 $inventory_id = $row['inventory_id'];
108 sqlStatement("UPDATE drug_inventory SET " .
109 "on_hand = on_hand - $quantity " .
110 "WHERE inventory_id = $inventory_id");
112 $rowsum = sqlQuery("SELECT sum(on_hand) AS sum FROM drug_inventory WHERE " .
113 "drug_id = '$drug_id' AND on_hand > '$quantity' AND expiration > CURRENT_DATE");
114 $rowdrug = sqlQuery("SELECT * FROM drugs WHERE " .
115 "drug_id = '$drug_id'");
116 if ($rowsum['sum'] <= $rowdrug['reorder_point']) {
117 send_email("Drug re-order required",
118 "Drug '" . $rowdrug['name'] . "' has reached its reorder point.\n");
121 // TBD: Set and check a reorder notification date so we don't
122 // send zillions of redundant emails.
123 ******************************************************************/
124 } // end if $drug_id
126 /*******************************************************************
127 $sale_id = sqlInsert("INSERT INTO drug_sales ( " .
128 "drug_id, inventory_id, prescription_id, pid, user, sale_date, quantity, fee " .
129 ") VALUES ( " .
130 "'$drug_id', '$inventory_id', '$prescription_id', '$pid', '$user', '$today',
131 '$quantity', '$fee' " .
132 ")");
133 *******************************************************************/
135 if (!$sale_id) {
136 die(xlt('Internal error, no drug ID specified!'));
138 } // end if not $sale_id
140 // Generate the bottle label for the sale identified by $sale_id.
142 // Get details for what we guess is the primary facility.
143 $frow = $facilityService->getPrimaryBusinessEntity(array("useLegacyImplementation" => true));
145 // Get everything else.
146 $row = sqlQuery("SELECT " .
147 "s.pid, s.quantity, s.prescription_id, " .
148 "i.manufacturer, i.lot_number, i.expiration, " .
149 "d.name, d.ndc_number, d.form, d.size, d.unit, " .
150 "r.date_modified, r.dosage, r.route, r.interval, r.substitute, r.refills, " .
151 "p.fname, p.lname, p.mname, " .
152 "u.fname AS ufname, u.mname AS umname, u.lname AS ulname " .
153 "FROM drug_sales AS s, drug_inventory AS i, drugs AS d, " .
154 "prescriptions AS r, patient_data AS p, users AS u WHERE " .
155 "s.sale_id = ? AND " .
156 "i.inventory_id = s.inventory_id AND " .
157 "d.drug_id = i.drug_id AND " .
158 "r.id = s.prescription_id AND " .
159 "p.pid = s.pid AND " .
160 "u.id = r.provider_id", array($sale_id));
162 $dconfig = $GLOBALS['oer_config']['druglabels'];
164 $header_text = $row['ufname'] . ' ' . $row['umname'] . ' ' . $row['ulname'] . "\n" .
165 $frow['street'] . "\n" .
166 $frow['city'] . ', ' . $frow['state'] . ' ' . $frow['postal_code'] .
167 ' ' . $frow['phone'] . "\n";
168 if ($dconfig['disclaimer']) {
169 $header_text .= $dconfig['disclaimer'] . "\n";
172 $label_text = $row['fname'] . ' ' . $row['lname'] . ' ' . $row['date_modified'] .
173 ' RX#' . sprintf('%06u', $row['prescription_id']) . "\n" .
174 $row['name'] . ' ' . $row['size'] . ' ' .
175 generate_display_field(array('data_type'=>'1','list_id'=>'drug_units'), $row['unit']) .
176 xl('QTY', '', ' ', ' ') . $row['quantity'] . "\n" .
177 xl('Take', '', '', ' ') . $row['dosage'] . ' ' .
178 generate_display_field(array('data_type'=>'1','list_id'=>'drug_form'), $row['form']) .
179 ($row['dosage'] > 1 ? 's ' : ' ') .
180 generate_display_field(array('data_type'=>'1','list_id'=>'drug_interval'), $row['interval']) .
181 ' ' .
182 generate_display_field(array('data_type'=>'1','list_id'=>'drug_route'), $row['route']) .
183 "\n" . xl('Lot', '', '', ' ') . $row['lot_number'] . xl('Exp', '', ' ', ' ') . $row['expiration'] . "\n" .
184 xl('NDC', '', '', ' ') . $row['ndc_number'] . ' ' . $row['manufacturer'];
186 // if ($row['refills']) {
187 // // Find out how many times this prescription has been filled/refilled.
188 // $refills_row = sqlQuery("SELECT count(*) AS count FROM drug_sales " .
189 // "WHERE prescription_id = '" . $row['prescription_id'] .
190 // "' AND quantity > 0");
191 // $label_text .= ($refills_row['count'] - 1) . ' of ' . $row['refills'] . ' refills';
192 // }
194 // We originally went for PDF output on the theory that output formatting
195 // would be more controlled. However the clumisness of invoking a PDF
196 // viewer from the browser becomes intolerable in a POS environment, and
197 // printing HTML is much faster and easier if the browser's page setup is
198 // configured properly.
200 if (false) { // if PDF output is desired
201 $pdf = new Cezpdf($dconfig['paper_size']);
202 $pdf->ezSetMargins($dconfig['top'], $dconfig['bottom'], $dconfig['left'], $dconfig['right']);
203 $pdf->selectFont('Helvetica');
204 $pdf->ezSetDy(20); // dunno why we have to do this...
205 $pdf->ezText($header_text, 7, array('justification'=>'center'));
206 if (!empty($dconfig['logo'])) {
207 $pdf->ezSetDy(-5); // add space (move down) before the image
208 $pdf->ezImage($dconfig['logo'], 0, 180, '', 'left');
209 $pdf->ezSetDy(8); // reduce space (move up) after the image
212 $pdf->ezText($label_text, 9, array('justification'=>'center'));
213 $pdf->ezStream();
214 } else { // HTML output
216 <html>
217 <script type="text/javascript" src="<?php echo $webroot ?>/interface/main/tabs/js/include_opener.js"></script>
218 <head>
219 <?php html_header_show();?>
220 <style type="text/css">
221 body {
222 font-family: sans-serif;
223 font-size: 9pt;
224 font-weight: normal;
226 .labtop {
227 color: #000000;
228 font-family: sans-serif;
229 font-size: 7pt;
230 font-weight: normal;
231 text-align: center;
232 padding-bottom: 1pt;
234 .labbot {
235 color: #000000;
236 font-family: sans-serif;
237 font-size: 9pt;
238 font-weight: normal;
239 text-align: center;
240 padding-top: 2pt;
242 </style>
243 <title><?php echo xlt('Prescription Label') ; ?></title>
244 </head>
245 <body leftmargin='0' topmargin='0' marginwidth='0' marginheight='0'>
246 <center>
247 <table border='0' cellpadding='0' cellspacing='0' style='width: 200pt'>
248 <tr><td class="labtop" nowrap>
249 <?php echo nl2br(text($header_text)); ?>
250 </td></tr>
251 <tr><td style='background-color: #000000; height: 5pt;'></td></tr>
252 <tr><td class="labbot" nowrap>
253 <?php echo nl2br(text($label_text)); ?>
254 </td></tr>
255 </table>
256 </center>
257 <script language="JavaScript">
258 var win = top.printLogPrint ? top : opener.top;
259 win.printLogPrint(window);
260 </script>
261 </body>
262 </html>
263 <?php