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