added upcoming appointments to patient summary
[openemr.git] / controllers / C_Prescription.class.php
blob3923cf9c50afc19d46cd160bfd2bbd0e557b6e0d
1 <?php
3 require_once ($GLOBALS['fileroot'] . "/library/classes/Controller.class.php");
4 require_once($GLOBALS['fileroot'] ."/library/classes/Prescription.class.php");
5 require_once($GLOBALS['fileroot'] ."/library/classes/Provider.class.php");
6 require_once($GLOBALS['fileroot'] ."/library/classes/RXList.class.php");
8 class C_Prescription extends Controller {
10 var $template_mod;
11 var $pconfig;
13 function C_Prescription($template_mod = "general") {
14 parent::Controller();
15 $this->template_mod = $template_mod;
16 $this->assign("FORM_ACTION", $GLOBALS['webroot']."/controller.php?" . $_SERVER['QUERY_STRING']);
17 $this->assign("TOP_ACTION", $GLOBALS['webroot']."/controller.php?" . "prescription" . "&");
18 $this->assign("STYLE", $GLOBALS['style']);
19 $this->pconfig = $GLOBALS['oer_config']['prescriptions'];
22 function default_action() {
23 $this->assign("prescription",$this->prescriptions[0]);
24 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_edit.html");
27 function edit_action($id = "",$patient_id="",$p_obj = null) {
29 if ($p_obj != null && get_class($p_obj) == "prescription") {
30 $this->prescriptions[0] = $p_obj;
32 elseif (get_class($this->prescriptions[0]) != "prescription" ) {
33 $this->prescriptions[0] = new Prescription($id);
36 if (!empty($patient_id)) {
37 $this->prescriptions[0]->set_patient_id($patient_id);
39 $this->default_action();
42 function list_action($id,$sort = "") {
43 if (empty($id)) {
44 $this->function_argument_error();
45 exit;
47 if (!empty($sort)) {
48 $this->assign("prescriptions", Prescription::prescriptions_factory($id,$sort));
50 else {
51 $this->assign("prescriptions", Prescription::prescriptions_factory($id));
53 //print_r(Prescription::prescriptions_factory($id));
54 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_list.html");
57 function block_action($id,$sort = "") {
58 if (empty($id)) {
59 $this->function_argument_error();
60 exit;
62 if (!empty($sort)) {
63 $this->assign("prescriptions", Prescription::prescriptions_factory($id,$sort));
65 else {
66 $this->assign("prescriptions", Prescription::prescriptions_factory($id));
68 //print_r(Prescription::prescriptions_factory($id));
69 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_block.html");
72 function lookup_action() {
73 $this->do_lookup();
74 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_lookup.html");
77 function edit_action_process() {
78 if ($_POST['process'] != "true")
79 return;
80 //print_r($_POST);
82 $this->prescriptions[0] = new Prescription($_POST['id']);
83 parent::populate_object($this->prescriptions[0]);
84 //echo $this->prescriptions[0]->toString(true);
85 $this->prescriptions[0]->persist();
86 $_POST['process'] = "";
87 return $this->send_action($this->prescriptions[0]->id);
90 function send_action($id) {
91 $_POST['process'] = "true";
92 if(empty($id)) {
93 $this->function_argument_error();
96 $this->assign("prescription",new Prescription($id));
97 $this->_state = false;
98 return $this->fetch($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_send.html");
101 function send_action_process($id) {
102 $dummy = ""; // Added by Rod to avoid run-time warnings
103 if ($_POST['process'] != "true")
104 return;
105 if(empty($id)) {
106 $this->function_argument_error();
108 $p = new Prescription($id);
109 switch ($_POST['submit']) {
111 case "Print":
112 // The following statement added by Rod.
113 // Looking at Controller.class.php, it appears that _state is set to false
114 // to indicate that no further HTML is to be generated.
115 $this->_state = false; // Added by Rod - see Controller.class.php
116 return $this->_print_prescription($p, $dummy);
117 break;
118 case "Email":
119 return $this->_email_prescription($p,$_POST['email_to']);
120 break;
121 case "Fax":
122 //this is intended to be the hook for the hylafax code we already have that hasn't worked its way into the tree yet.
123 //$this->assign("process_result","No fax server is currently setup.");
124 return $this->_fax_prescription($p,$_POST['fax_to']);
125 break;
126 case "Auto Send":
127 $pharmacy_id = $_POST['pharmacy_id'];
128 //echo "auto sending to : " . $_POST['pharmacy_id'];
129 $phar = new Pharmacy($_POST['pharmacy_id']);
130 //print_r($phar);
131 if ($phar->get_transmit_method() == TRANSMIT_PRINT) {
132 return $this->_print_prescription($p, $dummy);
134 elseif ($phar->get_transmit_method() == TRANSMIT_EMAIL) {
135 $email = $phar->get_email();
136 if (!empty($email)) {
137 return $this->_email_prescription($p,$phar->get_email());
139 //else print it
141 elseif ($phar->get_transmit_method() == TRANSMIT_FAX) {
142 $faxNum= $phar->get_fax();
143 if(!empty($faxNum)) {
144 Return $this->_fax_prescription ($p,$faxNum);
146 // return $this->assign("process_result","No fax server is currently setup.");
147 // else default is printing,
149 else {
150 //the pharmacy has no default or default is print
151 return $this->_print_prescription($p, $dummy);
153 break;
156 return;
160 function _print_prescription($p, & $toFile) {
161 require_once ($GLOBALS['fileroot'] . "/library/classes/class.ezpdf.php");
162 $pdf =& new Cezpdf($GLOBALS['oer_config']['prescriptions']['paper_size']);
163 $pdf->ezSetMargins($GLOBALS['oer_config']['prescriptions']['top']
164 ,$GLOBALS['oer_config']['prescriptions']['bottom']
165 ,$GLOBALS['oer_config']['prescriptions']['left']
166 ,$GLOBALS['oer_config']['prescriptions']['right']
169 $pdf->selectFont($GLOBALS['fileroot'] . "/library/fonts/Helvetica.afm");
171 if(!empty($this->pconfig['logo'])) {
172 $pdf->ezImage($this->pconfig['logo'],"","","none","left");
174 $pdf->ezText($p->get_prescription_display(),10);
175 if($this->pconfig['use_signature'] == true ) {
176 $pdf->ezImage($this->pconfig['signature'],"","","none","left");
178 else{
179 $pdf->ezText("\n\n\n\nSignature:________________________________",10);
182 if(!empty($toFile))
184 $toFile = $pdf->ezOutput();
186 else
188 $pdf->ezStream();
189 // $pdf->ezStream(array('compress' => 0)); // for testing with uncompressed output
191 return;
194 function _email_prescription($p,$email) {
195 if (empty($email)) {
196 $this->assign("process_result","Email could not be sent, the address supplied: '$email' was empty or invalid.");
197 return;
199 require($GLOBALS['fileroot'] . "/library/classes/class.phpmailer.php");
200 $mail = new PHPMailer();
201 $mail->SetLanguage("en",$GLOBALS['fileroot'] . "/library/" );
202 //this is a temporary config item until the rest of the per practice billing settings make their way in
203 $mail->From = $GLOBALS['practice_return_email_path'];
204 $mail->FromName = $p->provider->get_name_display();
205 $mail->isMail();
206 $mail->Host = "localhost";
207 $mail->Mailer = "mail";
208 $text_body = $p->get_prescription_display();
209 $mail->Body = $text_body;
210 $mail->Subject = "Prescription for: " . $p->patient->get_name_display();
211 $mail->AddAddress($email);
212 if($mail->Send()) {
213 $this->assign("process_result","Email was successfully sent to: " . $email);
214 return;
216 else {
217 $this->assign("process_result","There has been a mail error sending to " . $_POST['email_to'] . " " . $mail->ErrorInfo);
218 return;
222 function do_lookup() {
223 if ($_POST['process'] != "true")
224 return;
225 $list = array();
226 if (!empty($_POST['drug'])) {
227 $list = @RxList::get_list($_POST['drug']);
229 if (is_array($list)) {
230 $list = array_flip($list);
231 $this->assign("drug_options",$list);
232 $this->assign("drug_values",array_keys($list));
234 else {
235 $this->assign("NO_RESULTS","No results found for: " .$_POST['drug'] . "<br />");
237 //print_r($_POST);
238 //$this->assign("PROCESS","");
240 $_POST['process'] = "";
243 function _fax_prescription($p,$faxNum)
245 $err = "Sent fax";
246 //strip - ,(, ), and ws
247 $faxNum = preg_replace("/(-*)(\(*)(\)*)(\s*)/","",$faxNum);
248 //validate the number
250 if(!empty($faxNum) && is_numeric($faxNum))
252 //get the sendfax command and execute it
253 $cmd = $this->pconfig['sendfax'];
254 // prepend any prefix to the fax number
255 $pref=$this->pconfig['prefix'];
256 $faxNum=$pref.$faxNum;
257 if(empty($cmd))
259 $err .= " Send fax not set in includes/config.php";
260 break;
262 else
264 //generate file to fax
265 $faxFile = "Failed";
266 $this->_print_prescription($p, $faxFile);
267 if(empty($faxFile))
269 $err .= " _print_prescription returned empty file";
270 break;
272 $fileName = dirname(__FILE__)."/../documents/".$p->get_id()
273 .$p->get_patient_id()."_fax_.pdf";
274 //print "filename is $fileName";
275 touch($fileName); // php bug
276 $handle = fopen($fileName,"w");
277 if(!$handle)
279 $err .= " Failed to open file $fileName to write fax to";
280 break;
282 if(fwrite($handle, $faxFile) === false)
284 $err .= " Failed to write data to $fileName";
285 break;
287 fclose($handle);
288 $args = " -n -d $faxNum $fileName";
289 //print "command is $cmd $args<br>";
290 exec($cmd . $args);
294 else
296 $err = "bad fax number passed to function";
298 if($err)
300 $this->assign("process_result",$err);