Changed position and removed extraneous line from _print_prescription
[openemr.git] / controllers / C_Prescription.class.php
blobb45f7bb028fd323a7c1e891896e7badcbbff66fe
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 //the pharmacy has no default or default is print
150 return $this->_print_prescription($p, $dummy);
153 return;
157 function _print_prescription($p, & $toFile) {
158 require_once ($GLOBALS['fileroot'] . "/library/classes/class.ezpdf.php");
159 $pdf =& new Cezpdf("LETTER");
160 $pdf->ezSetMargins(72,30,50,30);
161 $pdf->selectFont($GLOBALS['fileroot'] . "/library/fonts/Helvetica.afm");
163 if(!empty($this->pconfig['logo'])) {
164 $pdf->ezImage($this->pconfig['logo'],"","","none","left");
166 $pdf->ezText($p->get_prescription_display(),10);
167 if(!empty($this->pconfig['signature'])) {
168 $pdf->ezImage($this->pconfig['signature'],"","","none","left");
171 $pdf->ezText("\n\n\n\nSignature:________________________________",10);
174 if(!empty($toFile))
176 $toFile = $pdf->ezOutput();
178 else
180 $pdf->ezStream();
181 // $pdf->ezStream(array('compress' => 0)); // for testing with uncompressed output
183 return;
186 function _email_prescription($p,$email) {
187 if (empty($email)) {
188 $this->assign("process_result","Email could not be sent, the address supplied: '$email' was empty or invalid.");
189 return;
191 require($GLOBALS['fileroot'] . "/library/classes/class.phpmailer.php");
192 $mail = new PHPMailer();
193 $mail->SetLanguage("en",$GLOBALS['fileroot'] . "/library/" );
194 //this is a temporary config item until the rest of the per practice billing settings make their way in
195 $mail->From = $GLOBALS['practice_return_email_path'];
196 $mail->FromName = $p->provider->get_name_display();
197 $mail->isMail();
198 $mail->Host = "localhost";
199 $mail->Mailer = "mail";
200 $text_body = $p->get_prescription_display();
201 $mail->Body = $text_body;
202 $mail->Subject = "Prescription for: " . $p->patient->get_name_display();
203 $mail->AddAddress($email);
204 if($mail->Send()) {
205 $this->assign("process_result","Email was successfully sent to: " . $email);
206 return;
208 else {
209 $this->assign("process_result","There has been a mail error sending to " . $_POST['email_to'] . " " . $mail->ErrorInfo);
210 return;
214 function do_lookup() {
215 if ($_POST['process'] != "true")
216 return;
217 $list = array();
218 if (!empty($_POST['drug'])) {
219 $list = @RxList::get_list($_POST['drug']);
221 if (is_array($list)) {
222 $list = array_flip($list);
223 $this->assign("drug_options",$list);
224 $this->assign("drug_values",array_keys($list));
226 else {
227 $this->assign("NO_RESULTS","No results found for: " .$_POST['drug'] . "<br />");
229 //print_r($_POST);
230 //$this->assign("PROCESS","");
232 $_POST['process'] = "";
235 function _fax_prescription($p,$faxNum)
237 $err = "Sent fax";
238 //strip - ,(, ), and ws
239 $faxNum = preg_replace("/(-*)(\(*)(\)*)(\s*)/","",$faxNum);
240 //validate the number
242 if(!empty($faxNum) && is_numeric($faxNum))
244 //get the sendfax command and execute it
245 $cmd = $this->pconfig['sendfax'];
246 // prepend any prefix to the fax number
247 $pref=$this->pconfig['prefix'];
248 $faxNum=$pref.$faxNum;
249 if(empty($cmd))
251 $err .= " Send fax not set in includes/config.php";
252 break;
254 else
256 //generate file to fax
257 $faxFile = "Failed";
258 $this->_print_prescription($p, $faxFile);
259 if(empty($faxFile))
261 $err .= " _print_prescription returned empty file";
262 break;
264 $fileName = dirname(__FILE__)."/../documents/".$p->get_id()
265 .$p->get_patient_id()."_fax_.pdf";
266 //print "filename is $fileName";
267 touch($fileName); // php bug
268 $handle = fopen($fileName,"w");
269 if(!$handle)
271 $err .= " Failed to open file $fileName to write fax to";
272 break;
274 if(fwrite($handle, $faxFile) === false)
276 $err .= " Failed to write data to $fileName";
277 break;
279 fclose($handle);
280 $args = " -n -d $faxNum $fileName";
281 //print "command is $cmd $args<br>";
282 exec($cmd . $args);
286 else
288 $err = "bad fax number passed to function";
290 if($err)
292 $this->assign("process_result",$err);