refinements for in-house drugs
[openemr.git] / interface / drugs / dispense_drug.php
blobd78511e054837f1c35639eccb2cef8dd76025c61
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($GLOBALS['fileroot'] . "/library/classes/class.phpmailer.php");
13 require_once($GLOBALS['fileroot'] . "/library/classes/class.ezpdf.php");
15 function send_email($subject, $body) {
16 $recipient = $GLOBALS['practice_return_email_path'];
17 $mail = new PHPMailer();
18 $mail->SetLanguage("en", $GLOBALS['fileroot'] . "/library/" );
19 $mail->From = $recipient;
20 $mail->FromName = 'In-House Pharmacy';
21 $mail->isMail();
22 $mail->Host = "localhost";
23 $mail->Mailer = "mail";
24 $mail->Body = $body;
25 $mail->Subject = $subject;
26 $mail->AddAddress($recipient);
27 if(!$mail->Send()) {
28 $die("There has been a mail error sending to " . $recipient .
29 " " . $mail->ErrorInfo);
33 $sale_id = $_REQUEST['sale_id'];
34 $drug_id = $_REQUEST['drug_id'];
35 $prescription_id = $_REQUEST['prescription'];
36 $quantity = $_REQUEST['quantity'];
37 $fee = $_REQUEST['fee'];
38 $user = $_SESSION['authUser'];
40 if (!acl_check('admin', 'drugs')) die("Not authorized!");
42 if (!$drug_id ) $drug_id = 0;
43 if (!$prescription_id) $prescription_id = 0;
44 if (!$quantity ) $quantity = 0;
45 if (!$fee ) $fee = 0.00;
47 $inventory_id = 0;
48 $bad_lot_list = '';
49 $today = date('Y-m-d');
51 // If there is no sale_id then this is a new dispensation.
53 if (! $sale_id) {
54 // Find and update inventory, deal with errors.
56 if ($drug_id) {
57 $res = sqlStatement("SELECT * FROM drug_inventory WHERE " .
58 "drug_id = '$drug_id' AND on_hand > 0 " .
59 "ORDER BY expiration, inventory_id");
60 while ($row = sqlFetchArray($res)) {
61 if ($row['expiration'] > $today && $row['on_hand'] >= $quantity) {
62 break;
64 $tmp = $row['lot_number'];
65 if (! $tmp) $tmp = '[missing lot number]';
66 if ($bad_lot_list) $bad_lot_list .= ', ';
67 $bad_lot_list .= $tmp;
70 if ($bad_lot_list) {
71 send_email("Lot destruction needed",
72 "The following lot(s) are expired or too small to fill prescription " .
73 "$prescription_id and should be destroyed: $bad_lot_list\n");
76 if (! $row) {
77 die("Inventory is not available for this order.");
80 $inventory_id = $row['inventory_id'];
82 sqlStatement("UPDATE drug_inventory SET " .
83 "on_hand = on_hand - $quantity " .
84 "WHERE inventory_id = $inventory_id");
86 $rowsum = sqlQuery("SELECT sum(on_hand) AS sum FROM drug_inventory WHERE " .
87 "drug_id = '$drug_id' AND on_hand > '$quantity' AND expiration > CURRENT_DATE");
88 $rowdrug = sqlQuery("SELECT * FROM drugs WHERE " .
89 "drug_id = '$drug_id'");
90 if ($rowsum['sum'] <= $rowdrug['reorder_point']) {
91 send_email("Drug re-order required",
92 "Drug '" . $rowdrug['name'] . "' has reached its reorder point.\n");
95 // TBD: Set and check a reorder notification date so we don't
96 // send zillions of redundant emails.
100 $sale_id = sqlInsert("INSERT INTO drug_sales ( " .
101 "drug_id, inventory_id, prescription_id, pid, user, sale_date, quantity, fee " .
102 ") VALUES ( " .
103 "'$drug_id', '$inventory_id', '$prescription_id', '$pid', '$user', '$today',
104 '$quantity', '$fee' " .
105 ")");
108 // Generate the bottle label PDF for the sale identified by $sale_id.
110 $row = sqlQuery("SELECT " .
111 "s.pid, s.quantity, s.prescription_id, " .
112 "i.manufacturer, i.lot_number, i.expiration, " .
113 "d.name, d.ndc_number, d.form, d.size, d.unit, " .
114 "r.date_modified, r.dosage, r.route, r.interval, r.substitute, r.refills, " .
115 "p.fname, p.lname, p.mname " .
116 "FROM drug_sales AS s, drug_inventory AS i, drugs AS d, " .
117 "prescriptions AS r, patient_data AS p WHERE " .
118 "s.sale_id = '$sale_id' AND " .
119 "i.inventory_id = s.inventory_id AND " .
120 "d.drug_id = i.drug_id AND " .
121 "r.id = s.prescription_id AND " .
122 "p.pid = s.pid");
124 $label_text = 'RX# ' . $row['prescription_id'] . ' ' .
125 $row['fname'] . ' ' . $row['lname'] . ' ' . $row['date_modified'] . "\n" .
126 $row['name'] . ' ' . $row['size'] . ' ' .
127 $unit_array[$row['unit']] . ' QTY ' .
128 $row['quantity'] . "\n" .
129 'NDC ' . $row['ndc_number'] . ' Lot ' . $row['lot_number'] . ' ' .
130 $row['manufacturer'] . "\n" .
131 'Take ' . $row['dosage'] . ' ' . $form_array[$row['form']] .
132 ($row['dosage'] > 1 ? 's ' : ' ') .
133 $interval_array_verbose[$row['interval']] . ' ' .
134 $route_array_verbose[$row['route']] . ".\n";
136 if ($row['refills']) {
137 // Find out how many times this prescription has been filled/refilled.
138 // Is this right? Perhaps we should instead sum the dispensed quantities
139 // and reconcile with the prescription quantities.
140 $refills_row = sqlQuery("SELECT count(*) AS count FROM drug_sales " .
141 "WHERE prescription_id = '" . $row['prescription_id'] .
142 "' AND quantity > 0");
143 $label_text .= ($refills_row['count'] - 1) . ' of ' . $row['refills'] . ' refills';
146 $dconfig = $GLOBALS['oer_config']['druglabels'];
147 $pdf =& new Cezpdf($dconfig['paper_size']);
148 $pdf->ezSetMargins($dconfig['top'],$dconfig['bottom'],$dconfig['left'],$dconfig['right']);
149 $pdf->selectFont($GLOBALS['fileroot'] . "/library/fonts/Helvetica.afm");
150 if(!empty($dconfig['logo'])) {
151 $pdf->ezImage($dconfig['logo']);
153 $pdf->ezText($label_text, 10);
154 $pdf->ezStream();