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