more support for dispensing of in-house drugs
[openemr.git] / interface / drugs / dispense_drug.php
blob6e7ddb8a25661cc70ea1d3ecc5d09c576139c04c
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");
14 function send_email($subject, $body) {
15 $recipient = $GLOBALS['practice_return_email_path'];
16 $mail = new PHPMailer();
17 $mail->SetLanguage("en", $GLOBALS['fileroot'] . "/library/" );
18 $mail->From = $recipient;
19 $mail->FromName = 'In-House Pharmacy';
20 $mail->isMail();
21 $mail->Host = "localhost";
22 $mail->Mailer = "mail";
23 $mail->Body = $body;
24 $mail->Subject = $subject;
25 $mail->AddAddress($recipient);
26 if(!$mail->Send()) {
27 $die("There has been a mail error sending to " . $recipient .
28 " " . $mail->ErrorInfo);
32 $sales_id = $_REQUEST['sales_id'];
33 $drug_id = $_REQUEST['drug_id'];
34 $prescription_id = $_REQUEST['prescription'];
35 $quantity = $_REQUEST['quantity'];
36 $fee = $_REQUEST['fee'];
37 $user = $_SESSION['authUser'];
39 if (!acl_check('admin', 'drugs')) die("Not authorized!");
41 if (!$drug_id ) $drug_id = 0;
42 if (!$prescription_id) $prescription_id = 0;
43 if (!$quantity ) $quantity = 0;
44 if (!$fee ) $fee = 0.00;
46 $inventory_id = 0;
47 $bad_lot_list = '';
48 $today = date('Y-m-d');
50 if (! $sales_id) {
51 // Find and update inventory, deal with errors.
53 if ($drug_id) {
54 $res = sqlStatement("SELECT * FROM drug_inventory WHERE " .
55 "drug_id = '$drug_id' AND on_hand > 0 " .
56 "ORDER BY expiration, inventory_id");
57 while ($row = sqlFetchArray($res)) {
58 if ($row['expiration'] > $today && $row['on_hand'] >= $quantity) {
59 break;
61 $tmp = $row['lot_number'];
62 if (! $tmp) $tmp = '[missing lot number]';
63 if ($bad_lot_list) $bad_lot_list .= ', ';
64 $bad_lot_list .= $tmp;
67 if ($bad_lot_list) {
68 send_email("Lot destruction needed",
69 "The following lot(s) are expired or too small to fill prescription " .
70 "$prescription_id and should be destroyed: $bad_lot_list\n");
73 if (! $row) {
74 die("Inventory is not available for this order.");
77 $inventory_id = $row['inventory_id'];
79 sqlStatement("UPDATE drug_inventory SET " .
80 "on_hand = on_hand - $quantity " .
81 "WHERE inventory_id = $inventory_id");
83 $rowsum = sqlQuery("SELECT sum(on_hand) AS sum FROM drug_inventory WHERE " .
84 "drug_id = '$drug_id' AND on_hand > '$quantity' AND expiration > CURRENT_DATE");
85 $rowdrug = sqlQuery("SELECT * FROM drugs WHERE " .
86 "drug_id = '$drug_id'");
87 if ($rowsum['sum'] <= $rowdrug['reorder_point']) {
88 send_email("Drug re-order required",
89 "Drug '" . $rowdrug['name'] . "' has reached its reorder point.\n");
92 // TBD: Set and check a reorder notification date so we don't
93 // send zillions of redundant emails.
97 $sales_id = sqlInsert("INSERT INTO drug_sales ( " .
98 "drug_id, inventory_id, prescription_id, pid, user, sale_date, quantity, fee " .
99 ") VALUES ( " .
100 "'$drug_id', '$inventory_id', '$prescription_id', '$pid', '$user', '$today',
101 '$quantity', '$fee' " .
102 ")");
104 echo "Inventory has been updated. Here we will send a PDF for the bottle label.\n";
107 // TBD: Generate the bottle label PDF for the sale identified by $sales_id.