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
{
13 function C_Prescription($template_mod = "general") {
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 = "") {
44 $this->function_argument_error();
48 $this->assign("prescriptions", Prescription
::prescriptions_factory($id,$sort));
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 = "") {
59 $this->function_argument_error();
63 $this->assign("prescriptions", Prescription
::prescriptions_factory($id,$sort));
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() {
74 $this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod
. "_lookup.html");
77 function edit_action_process() {
78 if ($_POST['process'] != "true")
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";
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")
106 $this->function_argument_error();
108 $p = new Prescription($id);
109 switch ($_POST['submit']) {
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);
119 return $this->_email_prescription($p,$_POST['email_to']);
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']);
127 $pharmacy_id = $_POST['pharmacy_id'];
128 //echo "auto sending to : " . $_POST['pharmacy_id'];
129 $phar = new Pharmacy($_POST['pharmacy_id']);
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());
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);
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);
176 $toFile = $pdf->ezOutput();
181 // $pdf->ezStream(array('compress' => 0)); // for testing with uncompressed output
186 function _email_prescription($p,$email) {
188 $this->assign("process_result","Email could not be sent, the address supplied: '$email' was empty or invalid.");
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();
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);
205 $this->assign("process_result","Email was successfully sent to: " . $email);
209 $this->assign("process_result","There has been a mail error sending to " . $_POST['email_to'] . " " . $mail->ErrorInfo
);
214 function do_lookup() {
215 if ($_POST['process'] != "true")
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));
227 $this->assign("NO_RESULTS","No results found for: " .$_POST['drug'] . "<br />");
230 //$this->assign("PROCESS","");
232 $_POST['process'] = "";
235 function _fax_prescription($p,$faxNum)
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;
251 $err .= " Send fax not set in includes/config.php";
256 //generate file to fax
258 $this->_print_prescription($p, $faxFile);
261 $err .= " _print_prescription returned empty file";
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");
271 $err .= " Failed to open file $fileName to write fax to";
274 if(fwrite($handle, $faxFile) === false)
276 $err .= " Failed to write data to $fileName";
280 $args = " -n -d $faxNum $fileName";
281 //print "command is $cmd $args<br>";
288 $err = "bad fax number passed to function";
292 $this->assign("process_result",$err);