2 // Copyright (C) 2006 Rod Roark <rod@sunsetsystems.com>
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
;
15 use PHPMailer\PHPMailer\PHPMailer
;
17 $facilityService = new FacilityService();
19 function send_email($subject, $body)
21 $recipient = $GLOBALS['practice_return_email_path'];
22 if (empty($recipient)) {
26 $mail = new PHPMailer();
27 $mail->From
= $recipient;
28 $mail->FromName
= 'In-House Pharmacy';
30 $mail->Host
= "localhost";
31 $mail->Mailer
= "mail";
33 $mail->Subject
= $subject;
34 $mail->AddAddress($recipient);
36 error_log(xl('There has been a mail error sending to', '', '', ' ') . $recipient .
37 " " . $mail->ErrorInfo
);
41 $sale_id = $_REQUEST['sale_id'];
42 $drug_id = $_REQUEST['drug_id'];
43 $prescription_id = $_REQUEST['prescription'];
44 $quantity = $_REQUEST['quantity'];
45 $fee = $_REQUEST['fee'];
46 $user = $_SESSION['authUser'];
48 if (!acl_check('admin', 'drugs')) {
49 die(xl('Not authorized'));
56 if (!$prescription_id) {
70 $today = date('Y-m-d');
72 // If there is no sale_id then this is a new dispensation.
75 // Post the order and update inventory, deal with errors.
78 $sale_id = sellDrug($drug_id, $quantity, $fee, $pid, 0, $prescription_id, $today, $user);
80 die(xlt('Inventory is not available for this order.'));
83 /******************************************************************
84 $res = sqlStatement("SELECT * FROM drug_inventory WHERE " .
85 "drug_id = '$drug_id' AND on_hand > 0 AND destroy_date IS NULL " .
86 "ORDER BY expiration, inventory_id");
87 while ($row = sqlFetchArray($res)) {
88 if ($row['expiration'] > $today && $row['on_hand'] >= $quantity) {
91 $tmp = $row['lot_number'];
92 if (! $tmp) $tmp = '[missing lot number]';
93 if ($bad_lot_list) $bad_lot_list .= ', ';
94 $bad_lot_list .= $tmp;
98 send_email("Lot destruction needed",
99 "The following lot(s) are expired or too small to fill prescription " .
100 "$prescription_id and should be destroyed: $bad_lot_list\n");
104 die("Inventory is not available for this order.");
107 $inventory_id = $row['inventory_id'];
109 sqlStatement("UPDATE drug_inventory SET " .
110 "on_hand = on_hand - $quantity " .
111 "WHERE inventory_id = $inventory_id");
113 $rowsum = sqlQuery("SELECT sum(on_hand) AS sum FROM drug_inventory WHERE " .
114 "drug_id = '$drug_id' AND on_hand > '$quantity' AND expiration > CURRENT_DATE");
115 $rowdrug = sqlQuery("SELECT * FROM drugs WHERE " .
116 "drug_id = '$drug_id'");
117 if ($rowsum['sum'] <= $rowdrug['reorder_point']) {
118 send_email("Drug re-order required",
119 "Drug '" . $rowdrug['name'] . "' has reached its reorder point.\n");
122 // TBD: Set and check a reorder notification date so we don't
123 // send zillions of redundant emails.
124 ******************************************************************/
127 /*******************************************************************
128 $sale_id = sqlInsert("INSERT INTO drug_sales ( " .
129 "drug_id, inventory_id, prescription_id, pid, user, sale_date, quantity, fee " .
131 "'$drug_id', '$inventory_id', '$prescription_id', '$pid', '$user', '$today',
132 '$quantity', '$fee' " .
134 *******************************************************************/
137 die(xlt('Internal error, no drug ID specified!'));
139 } // end if not $sale_id
141 // Generate the bottle label for the sale identified by $sale_id.
143 // Get details for what we guess is the primary facility.
144 $frow = $facilityService->getPrimaryBusinessEntity(array("useLegacyImplementation" => true));
146 // Get everything else.
147 $row = sqlQuery("SELECT " .
148 "s.pid, s.quantity, s.prescription_id, " .
149 "i.manufacturer, i.lot_number, i.expiration, " .
150 "d.name, d.ndc_number, d.form, d.size, d.unit, " .
151 "r.date_modified, r.dosage, r.route, r.interval, r.substitute, r.refills, " .
152 "p.fname, p.lname, p.mname, " .
153 "u.fname AS ufname, u.mname AS umname, u.lname AS ulname " .
154 "FROM drug_sales AS s, drug_inventory AS i, drugs AS d, " .
155 "prescriptions AS r, patient_data AS p, users AS u WHERE " .
156 "s.sale_id = ? AND " .
157 "i.inventory_id = s.inventory_id AND " .
158 "d.drug_id = i.drug_id AND " .
159 "r.id = s.prescription_id AND " .
160 "p.pid = s.pid AND " .
161 "u.id = r.provider_id", array($sale_id));
163 $dconfig = $GLOBALS['oer_config']['druglabels'];
165 $header_text = $row['ufname'] . ' ' . $row['umname'] . ' ' . $row['ulname'] . "\n" .
166 $frow['street'] . "\n" .
167 $frow['city'] . ', ' . $frow['state'] . ' ' . $frow['postal_code'] .
168 ' ' . $frow['phone'] . "\n";
169 if ($dconfig['disclaimer']) {
170 $header_text .= $dconfig['disclaimer'] . "\n";
173 $label_text = $row['fname'] . ' ' . $row['lname'] . ' ' . $row['date_modified'] .
174 ' RX#' . sprintf('%06u', $row['prescription_id']) . "\n" .
175 $row['name'] . ' ' . $row['size'] . ' ' .
176 generate_display_field(array('data_type'=>'1','list_id'=>'drug_units'), $row['unit']) .
177 xl('QTY', '', ' ', ' ') . $row['quantity'] . "\n" .
178 xl('Take', '', '', ' ') . $row['dosage'] . ' ' .
179 generate_display_field(array('data_type'=>'1','list_id'=>'drug_form'), $row['form']) .
180 ($row['dosage'] > 1 ?
's ' : ' ') .
181 generate_display_field(array('data_type'=>'1','list_id'=>'drug_interval'), $row['interval']) .
183 generate_display_field(array('data_type'=>'1','list_id'=>'drug_route'), $row['route']) .
184 "\n" . xl('Lot', '', '', ' ') . $row['lot_number'] . xl('Exp', '', ' ', ' ') . $row['expiration'] . "\n" .
185 xl('NDC', '', '', ' ') . $row['ndc_number'] . ' ' . $row['manufacturer'];
187 // if ($row['refills']) {
188 // // Find out how many times this prescription has been filled/refilled.
189 // $refills_row = sqlQuery("SELECT count(*) AS count FROM drug_sales " .
190 // "WHERE prescription_id = '" . $row['prescription_id'] .
191 // "' AND quantity > 0");
192 // $label_text .= ($refills_row['count'] - 1) . ' of ' . $row['refills'] . ' refills';
195 // We originally went for PDF output on the theory that output formatting
196 // would be more controlled. However the clumisness of invoking a PDF
197 // viewer from the browser becomes intolerable in a POS environment, and
198 // printing HTML is much faster and easier if the browser's page setup is
199 // configured properly.
201 if (false) { // if PDF output is desired
202 $pdf = new Cezpdf($dconfig['paper_size']);
203 $pdf->ezSetMargins($dconfig['top'], $dconfig['bottom'], $dconfig['left'], $dconfig['right']);
204 $pdf->selectFont('Helvetica');
205 $pdf->ezSetDy(20); // dunno why we have to do this...
206 $pdf->ezText($header_text, 7, array('justification'=>'center'));
207 if (!empty($dconfig['logo'])) {
208 $pdf->ezSetDy(-5); // add space (move down) before the image
209 $pdf->ezImage($dconfig['logo'], 0, 180, '', 'left');
210 $pdf->ezSetDy(8); // reduce space (move up) after the image
213 $pdf->ezText($label_text, 9, array('justification'=>'center'));
215 } else { // HTML output
218 <script type
="text/javascript" src
="<?php echo $webroot ?>/interface/main/tabs/js/include_opener.js"></script
>
220 <?php
html_header_show();?
>
221 <style type
="text/css">
223 font
-family
: sans
-serif
;
229 font
-family
: sans
-serif
;
237 font
-family
: sans
-serif
;
244 <title
><?php
echo xlt('Prescription Label') ; ?
></title
>
246 <body leftmargin
='0' topmargin
='0' marginwidth
='0' marginheight
='0'>
248 <table border
='0' cellpadding
='0' cellspacing
='0' style
='width: 200pt'>
249 <tr
><td
class="labtop" nowrap
>
250 <?php
echo nl2br(text($header_text)); ?
>
252 <tr
><td style
='background-color: #000000; height: 5pt;'></td
></tr
>
253 <tr
><td
class="labbot" nowrap
>
254 <?php
echo nl2br(text($label_text)); ?
>
258 <script language
="JavaScript">
259 var win
= top
.printLogPrint ? top
: opener
.top
;
260 win
.printLogPrint(window
);