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($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 if (empty($recipient)) return;
18 $mail = new PHPMailer();
19 $mail->SetLanguage("en", $GLOBALS['fileroot'] . "/library/" );
20 $mail->From
= $recipient;
21 $mail->FromName
= 'In-House Pharmacy';
23 $mail->Host
= "localhost";
24 $mail->Mailer
= "mail";
26 $mail->Subject
= $subject;
27 $mail->AddAddress($recipient);
29 error_log("There has been a mail error sending to " . $recipient .
30 " " . $mail->ErrorInfo
);
34 $sale_id = $_REQUEST['sale_id'];
35 $drug_id = $_REQUEST['drug_id'];
36 $prescription_id = $_REQUEST['prescription'];
37 $quantity = $_REQUEST['quantity'];
38 $fee = $_REQUEST['fee'];
39 $user = $_SESSION['authUser'];
41 if (!acl_check('admin', 'drugs')) die("Not authorized!");
43 if (!$drug_id ) $drug_id = 0;
44 if (!$prescription_id) $prescription_id = 0;
45 if (!$quantity ) $quantity = 0;
46 if (!$fee ) $fee = 0.00;
50 $today = date('Y-m-d');
52 // If there is no sale_id then this is a new dispensation.
55 // Post the order and update inventory, deal with errors.
58 $sale_id = sellDrug($drug_id, $quantity, $fee, $pid, 0, $prescription_id, $today, $user);
59 if (!$sale_id) die("Inventory is not available for this order.");
61 /******************************************************************
62 $res = sqlStatement("SELECT * FROM drug_inventory WHERE " .
63 "drug_id = '$drug_id' AND on_hand > 0 AND destroy_date IS NULL " .
64 "ORDER BY expiration, inventory_id");
65 while ($row = sqlFetchArray($res)) {
66 if ($row['expiration'] > $today && $row['on_hand'] >= $quantity) {
69 $tmp = $row['lot_number'];
70 if (! $tmp) $tmp = '[missing lot number]';
71 if ($bad_lot_list) $bad_lot_list .= ', ';
72 $bad_lot_list .= $tmp;
76 send_email("Lot destruction needed",
77 "The following lot(s) are expired or too small to fill prescription " .
78 "$prescription_id and should be destroyed: $bad_lot_list\n");
82 die("Inventory is not available for this order.");
85 $inventory_id = $row['inventory_id'];
87 sqlStatement("UPDATE drug_inventory SET " .
88 "on_hand = on_hand - $quantity " .
89 "WHERE inventory_id = $inventory_id");
91 $rowsum = sqlQuery("SELECT sum(on_hand) AS sum FROM drug_inventory WHERE " .
92 "drug_id = '$drug_id' AND on_hand > '$quantity' AND expiration > CURRENT_DATE");
93 $rowdrug = sqlQuery("SELECT * FROM drugs WHERE " .
94 "drug_id = '$drug_id'");
95 if ($rowsum['sum'] <= $rowdrug['reorder_point']) {
96 send_email("Drug re-order required",
97 "Drug '" . $rowdrug['name'] . "' has reached its reorder point.\n");
100 // TBD: Set and check a reorder notification date so we don't
101 // send zillions of redundant emails.
102 ******************************************************************/
106 /*******************************************************************
107 $sale_id = sqlInsert("INSERT INTO drug_sales ( " .
108 "drug_id, inventory_id, prescription_id, pid, user, sale_date, quantity, fee " .
110 "'$drug_id', '$inventory_id', '$prescription_id', '$pid', '$user', '$today',
111 '$quantity', '$fee' " .
113 *******************************************************************/
115 if (!$sale_id) die("Internal error, no drug ID specified!");
117 } // end if not $sale_id
119 // Generate the bottle label for the sale identified by $sale_id.
121 // Get details for what we guess is the primary facility.
122 $frow = sqlQuery("SELECT * FROM facility " .
123 "ORDER BY billing_location DESC, accepts_assignment DESC, id LIMIT 1");
125 // Get everything else.
126 $row = sqlQuery("SELECT " .
127 "s.pid, s.quantity, s.prescription_id, " .
128 "i.manufacturer, i.lot_number, i.expiration, " .
129 "d.name, d.ndc_number, d.form, d.size, d.unit, " .
130 "r.date_modified, r.dosage, r.route, r.interval, r.substitute, r.refills, " .
131 "p.fname, p.lname, p.mname, " .
132 "u.fname AS ufname, u.mname AS umname, u.lname AS ulname " .
133 "FROM drug_sales AS s, drug_inventory AS i, drugs AS d, " .
134 "prescriptions AS r, patient_data AS p, users AS u WHERE " .
135 "s.sale_id = '$sale_id' AND " .
136 "i.inventory_id = s.inventory_id AND " .
137 "d.drug_id = i.drug_id AND " .
138 "r.id = s.prescription_id AND " .
139 "p.pid = s.pid AND " .
140 "u.id = r.provider_id");
142 $dconfig = $GLOBALS['oer_config']['druglabels'];
144 $header_text = $row['ufname'] . ' ' . $row['umname'] . ' ' . $row['ulname'] . "\n" .
145 $frow['street'] . "\n" .
146 $frow['city'] . ', ' . $frow['state'] . ' ' . $frow['postal_code'] .
147 ' ' . $frow['phone'] . "\n";
148 if ($dconfig['disclaimer']) $header_text .= $dconfig['disclaimer'] . "\n";
150 $label_text = $row['fname'] . ' ' . $row['lname'] . ' ' . $row['date_modified'] .
151 ' RX#' . sprintf('%06u', $row['prescription_id']) . "\n" .
152 $row['name'] . ' ' . $row['size'] . ' ' .
153 $unit_array[$row['unit']] . ' QTY ' .
154 $row['quantity'] . "\n" .
155 'Take ' . $row['dosage'] . ' ' . $form_array[$row['form']] .
156 ($row['dosage'] > 1 ?
's ' : ' ') .
157 // $interval_array_verbose[$row['interval']] . ' ' .
158 $interval_array[$row['interval']] . ' ' .
159 // $route_array_verbose[$row['route']] . "\n" .
160 $route_array[$row['route']] . "\n" .
161 'Lot ' . $row['lot_number'] . ' Exp ' . $row['expiration'] . "\n" .
162 'NDC ' . $row['ndc_number'] . ' ' . $row['manufacturer'];
164 // if ($row['refills']) {
165 // // Find out how many times this prescription has been filled/refilled.
166 // $refills_row = sqlQuery("SELECT count(*) AS count FROM drug_sales " .
167 // "WHERE prescription_id = '" . $row['prescription_id'] .
168 // "' AND quantity > 0");
169 // $label_text .= ($refills_row['count'] - 1) . ' of ' . $row['refills'] . ' refills';
172 // We originally went for PDF output on the theory that output formatting
173 // would be more controlled. However the clumisness of invoking a PDF
174 // viewer from the browser becomes intolerable in a POS environment, and
175 // printing HTML is much faster and easier if the browser's page setup is
176 // configured properly.
178 if (false) { // if PDF output is desired
179 $pdf =& new Cezpdf($dconfig['paper_size']);
180 $pdf->ezSetMargins($dconfig['top'],$dconfig['bottom'],$dconfig['left'],$dconfig['right']);
181 $pdf->selectFont($GLOBALS['fileroot'] . "/library/fonts/Helvetica.afm");
182 $pdf->ezSetDy(20); // dunno why we have to do this...
183 $pdf->ezText($header_text, 7, array('justification'=>'center'));
184 if(!empty($dconfig['logo'])) {
185 $pdf->ezSetDy(-5); // add space (move down) before the image
186 $pdf->ezImage($dconfig['logo'], 0, 180, '', 'left');
187 $pdf->ezSetDy(8); // reduce space (move up) after the image
189 $pdf->ezText($label_text, 9, array('justification'=>'center'));
192 else { // HTML output
196 <?
html_header_show();?
>
197 <style type
="text/css">
199 font
-family
: sans
-serif
;
205 font
-family
: sans
-serif
;
213 font
-family
: sans
-serif
;
220 <title
><?php
xl('Prescription Label','e') ; ?
></title
>
222 <body leftmargin
='0' topmargin
='0' marginwidth
='0' marginheight
='0'>
224 <table border
='0' cellpadding
='0' cellspacing
='0' style
='width: 200pt'>
225 <tr
><td
class="labtop" nowrap
>
226 <?php
echo nl2br($header_text); ?
>
228 <tr
><td style
='background-color: #000000; height: 5pt;'></td
></tr
>
229 <tr
><td
class="labbot" nowrap
>
230 <?php
echo nl2br($label_text); ?
>
234 <script language
="JavaScript">