support more document types
[openemr.git] / controllers / C_Prescription.class.php
blob71711076f041d9138c0f3dffa8a9808f520d489f
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'];
21 // Make an array of drug IDs and names for the template.
22 $drug_array = array(0 => "-- or select from inventory --");
23 $res = sqlStatement("SELECT * FROM drugs ORDER BY name");
24 while ($row = sqlFetchArray($res)) {
25 $drug_array[$row['drug_id']] = $row['name'];
26 if ($row['ndc_number']) {
27 $drug_array[$row['drug_id']] .= ' [' . $row['ndc_number'] . ']';
29 // TBD: JavaScript variables for setting default options.
31 $this->assign("DRUG_ARRAY", $drug_array);
35 function default_action() {
36 $this->assign("prescription",$this->prescriptions[0]);
37 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_edit.html");
40 function edit_action($id = "",$patient_id="",$p_obj = null) {
42 if ($p_obj != null && get_class($p_obj) == "prescription") {
43 $this->prescriptions[0] = $p_obj;
45 elseif (get_class($this->prescriptions[0]) != "prescription" ) {
46 $this->prescriptions[0] = new Prescription($id);
49 if (!empty($patient_id)) {
50 $this->prescriptions[0]->set_patient_id($patient_id);
52 $this->default_action();
55 function list_action($id,$sort = "") {
56 if (empty($id)) {
57 $this->function_argument_error();
58 exit;
60 if (!empty($sort)) {
61 $this->assign("prescriptions", Prescription::prescriptions_factory($id,$sort));
63 else {
64 $this->assign("prescriptions", Prescription::prescriptions_factory($id));
66 //print_r(Prescription::prescriptions_factory($id));
67 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_list.html");
70 function block_action($id,$sort = "") {
71 if (empty($id)) {
72 $this->function_argument_error();
73 exit;
75 if (!empty($sort)) {
76 $this->assign("prescriptions", Prescription::prescriptions_factory($id,$sort));
78 else {
79 $this->assign("prescriptions", Prescription::prescriptions_factory($id));
81 //print_r(Prescription::prescriptions_factory($id));
82 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_block.html");
85 function lookup_action() {
86 $this->do_lookup();
87 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_lookup.html");
90 function edit_action_process() {
91 if ($_POST['process'] != "true")
92 return;
93 //print_r($_POST);
95 $this->prescriptions[0] = new Prescription($_POST['id']);
96 parent::populate_object($this->prescriptions[0]);
97 //echo $this->prescriptions[0]->toString(true);
98 $this->prescriptions[0]->persist();
99 $_POST['process'] = "";
100 return $this->send_action($this->prescriptions[0]->id);
103 function send_action($id) {
104 $_POST['process'] = "true";
105 if(empty($id)) {
106 $this->function_argument_error();
109 $this->assign("prescription",new Prescription($id));
110 $this->_state = false;
111 return $this->fetch($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_send.html");
114 function send_action_process($id) {
115 $dummy = ""; // Added by Rod to avoid run-time warnings
116 if ($_POST['process'] != "true")
117 return;
118 if(empty($id)) {
119 $this->function_argument_error();
121 $p = new Prescription($id);
122 switch ($_POST['submit']) {
124 case "Print":
125 // The following statement added by Rod.
126 // Looking at Controller.class.php, it appears that _state is set to false
127 // to indicate that no further HTML is to be generated.
128 $this->_state = false; // Added by Rod - see Controller.class.php
129 return $this->_print_prescription($p, $dummy);
130 break;
131 case "Email":
132 return $this->_email_prescription($p,$_POST['email_to']);
133 break;
134 case "Fax":
135 //this is intended to be the hook for the hylafax code we already have that hasn't worked its way into the tree yet.
136 //$this->assign("process_result","No fax server is currently setup.");
137 return $this->_fax_prescription($p,$_POST['fax_to']);
138 break;
139 case "Auto Send":
140 $pharmacy_id = $_POST['pharmacy_id'];
141 //echo "auto sending to : " . $_POST['pharmacy_id'];
142 $phar = new Pharmacy($_POST['pharmacy_id']);
143 //print_r($phar);
144 if ($phar->get_transmit_method() == TRANSMIT_PRINT) {
145 return $this->_print_prescription($p, $dummy);
147 elseif ($phar->get_transmit_method() == TRANSMIT_EMAIL) {
148 $email = $phar->get_email();
149 if (!empty($email)) {
150 return $this->_email_prescription($p,$phar->get_email());
152 //else print it
154 elseif ($phar->get_transmit_method() == TRANSMIT_FAX) {
155 $faxNum= $phar->get_fax();
156 if(!empty($faxNum)) {
157 Return $this->_fax_prescription ($p,$faxNum);
159 // return $this->assign("process_result","No fax server is currently setup.");
160 // else default is printing,
162 else {
163 //the pharmacy has no default or default is print
164 return $this->_print_prescription($p, $dummy);
166 break;
169 return;
173 function _print_prescription($p, & $toFile) {
174 require_once ($GLOBALS['fileroot'] . "/library/classes/class.ezpdf.php");
175 $pdf =& new Cezpdf($GLOBALS['oer_config']['prescriptions']['paper_size']);
176 $pdf->ezSetMargins($GLOBALS['oer_config']['prescriptions']['top']
177 ,$GLOBALS['oer_config']['prescriptions']['bottom']
178 ,$GLOBALS['oer_config']['prescriptions']['left']
179 ,$GLOBALS['oer_config']['prescriptions']['right']
182 $pdf->selectFont($GLOBALS['fileroot'] . "/library/fonts/Helvetica.afm");
184 if(!empty($this->pconfig['logo'])) {
185 $pdf->ezImage($this->pconfig['logo'],"","","none","left");
187 $pdf->ezText($p->get_prescription_display(),10);
188 if($this->pconfig['use_signature'] == true ) {
189 $pdf->ezImage($this->pconfig['signature'],"","","none","left");
191 else{
192 $pdf->ezText("\n\n\n\nSignature:________________________________",10);
195 if(!empty($toFile))
197 $toFile = $pdf->ezOutput();
199 else
201 $pdf->ezStream();
202 // $pdf->ezStream(array('compress' => 0)); // for testing with uncompressed output
204 return;
207 function _email_prescription($p,$email) {
208 if (empty($email)) {
209 $this->assign("process_result","Email could not be sent, the address supplied: '$email' was empty or invalid.");
210 return;
212 require($GLOBALS['fileroot'] . "/library/classes/class.phpmailer.php");
213 $mail = new PHPMailer();
214 $mail->SetLanguage("en",$GLOBALS['fileroot'] . "/library/" );
215 //this is a temporary config item until the rest of the per practice billing settings make their way in
216 $mail->From = $GLOBALS['practice_return_email_path'];
217 $mail->FromName = $p->provider->get_name_display();
218 $mail->isMail();
219 $mail->Host = "localhost";
220 $mail->Mailer = "mail";
221 $text_body = $p->get_prescription_display();
222 $mail->Body = $text_body;
223 $mail->Subject = "Prescription for: " . $p->patient->get_name_display();
224 $mail->AddAddress($email);
225 if($mail->Send()) {
226 $this->assign("process_result","Email was successfully sent to: " . $email);
227 return;
229 else {
230 $this->assign("process_result","There has been a mail error sending to " . $_POST['email_to'] . " " . $mail->ErrorInfo);
231 return;
235 function do_lookup() {
236 if ($_POST['process'] != "true")
237 return;
238 $list = array();
239 if (!empty($_POST['drug'])) {
240 $list = @RxList::get_list($_POST['drug']);
242 if (is_array($list)) {
243 $list = array_flip($list);
244 $this->assign("drug_options",$list);
245 $this->assign("drug_values",array_keys($list));
247 else {
248 $this->assign("NO_RESULTS","No results found for: " .$_POST['drug'] . "<br />");
250 //print_r($_POST);
251 //$this->assign("PROCESS","");
253 $_POST['process'] = "";
256 function _fax_prescription($p,$faxNum)
258 $err = "Sent fax";
259 //strip - ,(, ), and ws
260 $faxNum = preg_replace("/(-*)(\(*)(\)*)(\s*)/","",$faxNum);
261 //validate the number
263 if(!empty($faxNum) && is_numeric($faxNum))
265 //get the sendfax command and execute it
266 $cmd = $this->pconfig['sendfax'];
267 // prepend any prefix to the fax number
268 $pref=$this->pconfig['prefix'];
269 $faxNum=$pref.$faxNum;
270 if(empty($cmd))
272 $err .= " Send fax not set in includes/config.php";
273 break;
275 else
277 //generate file to fax
278 $faxFile = "Failed";
279 $this->_print_prescription($p, $faxFile);
280 if(empty($faxFile))
282 $err .= " _print_prescription returned empty file";
283 break;
285 $fileName = dirname(__FILE__)."/../documents/".$p->get_id()
286 .$p->get_patient_id()."_fax_.pdf";
287 //print "filename is $fileName";
288 touch($fileName); // php bug
289 $handle = fopen($fileName,"w");
290 if(!$handle)
292 $err .= " Failed to open file $fileName to write fax to";
293 break;
295 if(fwrite($handle, $faxFile) === false)
297 $err .= " Failed to write data to $fileName";
298 break;
300 fclose($handle);
301 $args = " -n -d $faxNum $fileName";
302 //print "command is $cmd $args<br>";
303 exec($cmd . $args);
307 else
309 $err = "bad fax number passed to function";
311 if($err)
313 $this->assign("process_result",$err);