tweaking label layout
[openemr.git] / interface / drugs / dispense_drug.php
blobd792e035d12b5e82adebbf73e77f95ec3a4057a0
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 // Get details for what we guess is the primary facility.
111 $frow = sqlQuery("SELECT * FROM facility " .
112 "ORDER BY billing_location DESC, accepts_assignment DESC, id LIMIT 1");
114 // Get everything else.
115 $row = sqlQuery("SELECT " .
116 "s.pid, s.quantity, s.prescription_id, " .
117 "i.manufacturer, i.lot_number, i.expiration, " .
118 "d.name, d.ndc_number, d.form, d.size, d.unit, " .
119 "r.date_modified, r.dosage, r.route, r.interval, r.substitute, r.refills, " .
120 "p.fname, p.lname, p.mname, " .
121 "u.fname AS ufname, u.mname AS umname, u.lname AS ulname " .
122 "FROM drug_sales AS s, drug_inventory AS i, drugs AS d, " .
123 "prescriptions AS r, patient_data AS p, users AS u WHERE " .
124 "s.sale_id = '$sale_id' AND " .
125 "i.inventory_id = s.inventory_id AND " .
126 "d.drug_id = i.drug_id AND " .
127 "r.id = s.prescription_id AND " .
128 "p.pid = s.pid AND " .
129 "u.id = r.provider_id");
131 $dconfig = $GLOBALS['oer_config']['druglabels'];
132 $pdf =& new Cezpdf($dconfig['paper_size']);
133 $pdf->ezSetMargins($dconfig['top'],$dconfig['bottom'],$dconfig['left'],$dconfig['right']);
134 $pdf->selectFont($GLOBALS['fileroot'] . "/library/fonts/Helvetica.afm");
136 $header_text = $row['ufname'] . ' ' . $row['umname'] . ' ' . $row['ulname'] . "\n" .
137 $frow['street'] . "\n" .
138 $frow['city'] . ', ' . $frow['state'] . ' ' . $frow['postal_code'] .
139 ' ' . $frow['phone'] . "\n";
140 if ($dconfig['disclaimer']) $header_text .= $dconfig['disclaimer'] . "\n";
142 $pdf->ezSetDy(20); // dunno why we have to do this...
143 $pdf->ezText($header_text, 7, array('justification'=>'center'));
145 if(!empty($dconfig['logo'])) {
146 $pdf->ezSetDy(-5); // add space (move down) before the image
147 $pdf->ezImage($dconfig['logo'], 0, 180, '', 'left');
148 $pdf->ezSetDy(8); // reduce space (move up) after the image
151 $label_text = $row['fname'] . ' ' . $row['lname'] . ' ' . $row['date_modified'] .
152 ' RX#' . sprintf('%06u', $row['prescription_id']) . "\n" .
153 $row['name'] . ' ' . $row['size'] . ' ' .
154 $unit_array[$row['unit']] . ' QTY ' .
155 $row['quantity'] . "\n" .
156 'Take ' . $row['dosage'] . ' ' . $form_array[$row['form']] .
157 ($row['dosage'] > 1 ? 's ' : ' ') .
158 $interval_array_verbose[$row['interval']] . ' ' .
159 $route_array_verbose[$row['route']] . "\n" .
160 'Lot ' . $row['lot_number'] . ' Exp ' . $row['expiration'] . "\n" .
161 'NDC ' . $row['ndc_number'] . ' ' . $row['manufacturer'];
163 /****
164 if ($row['refills']) {
165 // Find out how many times this prescription has been filled/refilled.
166 // Is this right? Perhaps we should instead sum the dispensed quantities
167 // and reconcile with the prescription quantities.
168 $refills_row = sqlQuery("SELECT count(*) AS count FROM drug_sales " .
169 "WHERE prescription_id = '" . $row['prescription_id'] .
170 "' AND quantity > 0");
171 $label_text .= ($refills_row['count'] - 1) . ' of ' . $row['refills'] . ' refills';
173 ****/
175 $pdf->ezText($label_text, 9, array('justification'=>'center'));
177 $pdf->ezStream();