Commit generated changelog for 7.0.2.1 (#7458)
[openemr.git] / interface / drugs / dispense_drug.php
blob0ab93db509e17ab39cf122a3aca31bf91587069d
1 <?php
3 // Copyright (C) 2006 Rod Roark <rod@sunsetsystems.com>
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
10 require_once("../globals.php");
11 require_once("drugs.inc.php");
12 require_once("$srcdir/options.inc.php");
14 use OpenEMR\Common\Acl\AclMain;
15 use OpenEMR\Common\Twig\TwigContainer;
16 use OpenEMR\Services\FacilityService;
17 use PHPMailer\PHPMailer\PHPMailer;
19 $facilityService = new FacilityService();
21 function send_email($subject, $body)
23 $recipient = $GLOBALS['practice_return_email_path'];
24 if (empty($recipient)) {
25 return;
28 $mail = new PHPMailer();
29 $mail->From = $recipient;
30 $mail->FromName = 'In-House Pharmacy';
31 $mail->isMail();
32 $mail->Host = "localhost";
33 $mail->Mailer = "mail";
34 $mail->Body = $body;
35 $mail->Subject = $subject;
36 $mail->AddAddress($recipient);
37 if (!$mail->Send()) {
38 error_log('There has been a mail error sending to' . " " . errorLogEscape($recipient .
39 " " . $mail->ErrorInfo));
43 $sale_id = $_REQUEST['sale_id'];
44 $drug_id = $_REQUEST['drug_id'];
45 $prescription_id = $_REQUEST['prescription'];
46 $quantity = $_REQUEST['quantity'];
47 $fee = $_REQUEST['fee'];
48 $user = $_SESSION['authUser'];
49 $encounter = $_SESSION['encounter'] ?? 0;
51 if (!AclMain::aclCheckCore('admin', 'drugs')) {
52 echo (new TwigContainer(null, $GLOBALS['kernel']))->getTwig()->render('core/unauthorized.html.twig', ['pageTitle' => xl("Dispense Drug")]);
53 exit;
56 if (!$drug_id) {
57 $drug_id = 0;
60 if (!$prescription_id) {
61 $prescription_id = 0;
64 if (!$quantity) {
65 $quantity = 0;
68 if (!$fee) {
69 $fee = 0.00;
72 $inventory_id = 0;
73 $bad_lot_list = '';
74 $today = date('Y-m-d');
76 // If there is no sale_id then this is a new dispensation.
78 if (! $sale_id) {
79 // Post the order and update inventory, deal with errors.
81 if ($drug_id) {
82 $sale_id = sellDrug($drug_id, $quantity, $fee, $pid, $encounter, $prescription_id, $today, $user);
83 if (!$sale_id) {
84 die(xlt('Inventory is not available for this order.'));
87 /******************************************************************
88 $res = sqlStatement("SELECT * FROM drug_inventory WHERE " .
89 "drug_id = '$drug_id' AND on_hand > 0 AND destroy_date IS NULL " .
90 "ORDER BY expiration, inventory_id");
91 while ($row = sqlFetchArray($res)) {
92 if ($row['expiration'] > $today && $row['on_hand'] >= $quantity) {
93 break;
95 $tmp = $row['lot_number'];
96 if (! $tmp) $tmp = '[missing lot number]';
97 if ($bad_lot_list) $bad_lot_list .= ', ';
98 $bad_lot_list .= $tmp;
101 if ($bad_lot_list) {
102 send_email("Lot destruction needed",
103 "The following lot(s) are expired or too small to fill prescription " .
104 "$prescription_id and should be destroyed: $bad_lot_list\n");
107 if (! $row) {
108 die("Inventory is not available for this order.");
111 $inventory_id = $row['inventory_id'];
113 sqlStatement("UPDATE drug_inventory SET " .
114 "on_hand = on_hand - $quantity " .
115 "WHERE inventory_id = $inventory_id");
117 $rowsum = sqlQuery("SELECT sum(on_hand) AS sum FROM drug_inventory WHERE " .
118 "drug_id = '$drug_id' AND on_hand > '$quantity' AND expiration > CURRENT_DATE");
119 $rowdrug = sqlQuery("SELECT * FROM drugs WHERE " .
120 "drug_id = '$drug_id'");
121 if ($rowsum['sum'] <= $rowdrug['reorder_point']) {
122 send_email("Drug re-order required",
123 "Drug '" . $rowdrug['name'] . "' has reached its reorder point.\n");
126 // TBD: Set and check a reorder notification date so we don't
127 // send zillions of redundant emails.
128 ******************************************************************/
129 } // end if $drug_id
131 /*******************************************************************
132 $sale_id = sqlInsert("INSERT INTO drug_sales ( " .
133 "drug_id, inventory_id, prescription_id, pid, user, sale_date, quantity, fee " .
134 ") VALUES ( " .
135 "'$drug_id', '$inventory_id', '$prescription_id', '$pid', '$user', '$today',
136 '$quantity', '$fee' " .
137 ")");
138 *******************************************************************/
140 if (!$sale_id) {
141 die(xlt('Internal error, no drug ID specified!'));
143 } // end if not $sale_id
145 // Generate the bottle label for the sale identified by $sale_id.
147 // Get details for what we guess is the primary facility.
148 $frow = $facilityService->getPrimaryBusinessEntity(array("useLegacyImplementation" => true));
150 // Get everything else.
151 $row = sqlQuery("SELECT " .
152 "s.pid, s.quantity, s.prescription_id, " .
153 "i.manufacturer, i.lot_number, i.expiration, " .
154 "d.name, d.ndc_number, d.form, d.size, d.unit, " .
155 "r.date_modified, r.dosage, r.route, r.interval, r.substitute, r.refills, " .
156 "p.fname, p.lname, p.mname, " .
157 "u.fname AS ufname, u.mname AS umname, u.lname AS ulname " .
158 "FROM drug_sales AS s, drug_inventory AS i, drugs AS d, " .
159 "prescriptions AS r, patient_data AS p, users AS u WHERE " .
160 "s.sale_id = ? AND " .
161 "i.inventory_id = s.inventory_id AND " .
162 "d.drug_id = i.drug_id AND " .
163 "r.id = s.prescription_id AND " .
164 "p.pid = s.pid AND " .
165 "u.id = r.provider_id", array($sale_id));
167 $dconfig = $GLOBALS['oer_config']['druglabels'];
169 $header_text = $row['ufname'] . ' ' . $row['umname'] . ' ' . $row['ulname'] . "\n" .
170 $frow['street'] . "\n" .
171 $frow['city'] . ', ' . $frow['state'] . ' ' . $frow['postal_code'] .
172 ' ' . $frow['phone'] . "\n";
173 if ($dconfig['disclaimer']) {
174 $header_text .= $dconfig['disclaimer'] . "\n";
177 $label_text = $row['fname'] . ' ' . $row['lname'] . ' ' . $row['date_modified'] .
178 ' RX#' . sprintf('%06u', $row['prescription_id']) . "\n" .
179 $row['name'] . ' ' . $row['size'] . ' ' .
180 generate_display_field(array('data_type' => '1','list_id' => 'drug_units'), $row['unit']) . ' ' .
181 xl('QTY') . ' ' . $row['quantity'] . "\n" .
182 xl('Take') . ' ' . $row['dosage'] . ' ' .
183 generate_display_field(array('data_type' => '1','list_id' => 'drug_form'), $row['form']) .
184 ($row['dosage'] > 1 ? 's ' : ' ') .
185 generate_display_field(array('data_type' => '1','list_id' => 'drug_interval'), $row['interval']) .
186 ' ' .
187 generate_display_field(array('data_type' => '1','list_id' => 'drug_route'), $row['route']) .
188 "\n" . xl('Lot', '', '', ' ') . $row['lot_number'] . xl('Exp', '', ' ', ' ') . $row['expiration'] . "\n" .
189 xl('NDC', '', '', ' ') . $row['ndc_number'] . ' ' . $row['manufacturer'];
191 // if ($row['refills']) {
192 // // Find out how many times this prescription has been filled/refilled.
193 // $refills_row = sqlQuery("SELECT count(*) AS count FROM drug_sales " .
194 // "WHERE prescription_id = '" . $row['prescription_id'] .
195 // "' AND quantity > 0");
196 // $label_text .= ($refills_row['count'] - 1) . ' of ' . $row['refills'] . ' refills';
197 // }
199 // We originally went for PDF output on the theory that output formatting
200 // would be more controlled. However the clumisness of invoking a PDF
201 // viewer from the browser becomes intolerable in a POS environment, and
202 // printing HTML is much faster and easier if the browser's page setup is
203 // configured properly.
205 if (false) { // if PDF output is desired
206 $pdf = new Cezpdf($dconfig['paper_size']);
207 $pdf->ezSetMargins($dconfig['top'], $dconfig['bottom'], $dconfig['left'], $dconfig['right']);
208 $pdf->selectFont('Helvetica');
209 $pdf->ezSetDy(20); // dunno why we have to do this...
210 $pdf->ezText($header_text, 7, array('justification' => 'center'));
211 if (!empty($dconfig['logo'])) {
212 $pdf->ezSetDy(-5); // add space (move down) before the image
213 $pdf->ezImage($dconfig['logo'], 0, 180, '', 'left');
214 $pdf->ezSetDy(8); // reduce space (move up) after the image
217 $pdf->ezText($label_text, 9, array('justification' => 'center'));
218 $pdf->ezStream();
219 } else { // HTML output
221 <html>
222 <script src="<?php echo $webroot ?>/interface/main/tabs/js/include_opener.js"></script>
223 <head>
224 <style>
225 body {
226 font-family: sans-serif;
227 font-size: 9pt;
228 font-weight: normal;
230 .labtop {
231 color: #000000;
232 font-family: sans-serif;
233 font-size: 7pt;
234 font-weight: normal;
235 text-align: center;
236 padding-bottom: 1pt;
238 .labbot {
239 color: #000000;
240 font-family: sans-serif;
241 font-size: 9pt;
242 font-weight: normal;
243 text-align: center;
244 padding-top: 2pt;
246 </style>
247 <title><?php echo xlt('Prescription Label') ; ?></title>
248 </head>
249 <body leftmargin='0' topmargin='0' marginwidth='0' marginheight='0'>
250 <center>
251 <table border='0' cellpadding='0' cellspacing='0' style='width: 200pt'>
252 <tr><td class="labtop" nowrap>
253 <?php echo nl2br(text($header_text)); ?>
254 </td></tr>
255 <tr><td style='background-color: #000000; height: 5pt;'></td></tr>
256 <tr><td class="labbot" nowrap>
257 <?php echo nl2br(text($label_text)); ?>
258 </td></tr>
259 </table>
260 </center>
261 <script>
262 var win = top.printLogPrint ? top : opener.top;
263 win.printLogPrint(window);
264 </script>
265 </body>
266 </html>
267 <?php