forked from openemr/openemr
-
Notifications
You must be signed in to change notification settings - Fork 0
/
C_Prescription.class.php
525 lines (470 loc) · 19.3 KB
/
C_Prescription.class.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
<?php
require_once($GLOBALS['fileroot'] . "/library/classes/Controller.class.php");
require_once($GLOBALS['fileroot'] . "/library/classes/Prescription.class.php");
require_once($GLOBALS['fileroot'] . "/library/classes/Provider.class.php");
require_once($GLOBALS['fileroot'] . "/library/classes/RXList.class.php");
class C_Prescription extends Controller {
var $template_mod;
var $pconfig;
function C_Prescription($template_mod = "general") {
parent::Controller();
$this->template_mod = $template_mod;
$this->assign("FORM_ACTION", $GLOBALS['webroot']."/controller.php?" . $_SERVER['QUERY_STRING']);
$this->assign("TOP_ACTION", $GLOBALS['webroot']."/controller.php?" . "prescription" . "&");
$this->assign("STYLE", $GLOBALS['style']);
$this->pconfig = $GLOBALS['oer_config']['prescriptions'];
if ($GLOBALS['inhouse_pharmacy']) {
// Make an array of drug IDs and selectors for the template.
$drug_array = array(0 => "-- or select from inventory --");
$drug_attributes = '';
$res = sqlStatement("SELECT * FROM drugs ORDER BY selector");
while ($row = sqlFetchArray($res)) {
$drug_array[$row['drug_id']] = $row['selector'];
if ($row['ndc_number']) {
$drug_array[$row['drug_id']] .= ' [' . $row['ndc_number'] . ']';
}
if ($drug_attributes) $drug_attributes .= ',';
$drug_attributes .= "['" .
$row['name'] . "'," . // 0
$row['form'] . ",'" . // 1
$row['dosage'] . "'," . // 2
$row['size'] . "," . // 3
$row['unit'] . "," . // 4
$row['route'] . "," . // 5
$row['period'] . "," . // 6
$row['substitute'] . "," . // 7
$row['quantity'] . "," . // 8
$row['refills'] . "," . // 9
$row['per_refill'] . "]"; // 10
}
$this->assign("DRUG_ARRAY", $drug_array);
$this->assign("DRUG_ATTRIBUTES", $drug_attributes);
}
}
function default_action() {
$this->assign("prescription",$this->prescriptions[0]);
$this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_edit.html");
}
function edit_action($id = "",$patient_id="",$p_obj = null) {
if ($p_obj != null && get_class($p_obj) == "prescription") {
$this->prescriptions[0] = $p_obj;
}
elseif (get_class($this->prescriptions[0]) != "prescription" ) {
$this->prescriptions[0] = new Prescription($id);
}
if (!empty($patient_id)) {
$this->prescriptions[0]->set_patient_id($patient_id);
}
$this->default_action();
}
function list_action($id,$sort = "") {
if (empty($id)) {
$this->function_argument_error();
exit;
}
if (!empty($sort)) {
$this->assign("prescriptions", Prescription::prescriptions_factory($id,$sort));
}
else {
$this->assign("prescriptions", Prescription::prescriptions_factory($id));
}
//print_r(Prescription::prescriptions_factory($id));
$this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_list.html");
}
function block_action($id,$sort = "") {
if (empty($id)) {
$this->function_argument_error();
exit;
}
if (!empty($sort)) {
$this->assign("prescriptions", Prescription::prescriptions_factory($id,$sort));
}
else {
$this->assign("prescriptions", Prescription::prescriptions_factory($id));
}
//print_r(Prescription::prescriptions_factory($id));
$this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_block.html");
}
function lookup_action() {
$this->do_lookup();
$this->display($GLOBALS['template_dir'] . "prescription/" . $this->template_mod . "_lookup.html");
}
function edit_action_process() {
if ($_POST['process'] != "true")
return;
//print_r($_POST);
$this->prescriptions[0] = new Prescription($_POST['id']);
parent::populate_object($this->prescriptions[0]);
//echo $this->prescriptions[0]->toString(true);
$this->prescriptions[0]->persist();
$_POST['process'] = "";
// If the "Prescribe and Dispense" button was clicked, then
// redisplay as in edit_action() but also replicate the fee and
// include a piece of javascript to call dispense().
//
if ($_POST['disp_button']) {
$this->assign("DISP_QUANTITY", $_POST['disp_quantity']);
$this->assign("DISP_FEE", $_POST['disp_fee']);
$this->assign("ENDING_JAVASCRIPT", "dispense();");
$this->_state = false;
return $this->edit_action($this->prescriptions[0]->id);
}
return $this->send_action($this->prescriptions[0]->id);
}
function send_action($id) {
$_POST['process'] = "true";
if(empty($id)) {
$this->function_argument_error();
}
$rx = new Prescription($id);
// Populate pharmacy info if the patient has a default pharmacy.
// Probably the Prescription object should handle this instead, but
// doing it there will require more careful research and testing.
$prow = sqlQuery("SELECT pt.pharmacy_id FROM prescriptions AS rx, " .
"patient_data AS pt WHERE rx.id = '$id' AND pt.pid = rx.patient_id");
if ($prow['pharmacy_id']) {
$rx->pharmacy->set_id($prow['pharmacy_id']);
$rx->pharmacy->populate();
}
$this->assign("prescription", $rx);
$this->_state = false;
return $this->fetch($GLOBALS['template_dir'] . "prescription/" .
$this->template_mod . "_send.html");
}
function multiprint_header(& $pdf, $p) {
//print header
$pdf->ezImage($GLOBALS['fileroot'] . '/interface/pic/Rx.png','','50','','center','');
$pdf->ezColumnsStart(array('num'=>2, 'gap'=>10));
$res = sqlQuery("SELECT concat('<b>',f.name,'</b>\n',f.street,'\n',f.city,', ',f.state,' ',f.postal_code,'\nTel:',f.phone) addr FROM users JOIN facility AS f ON f.name = users.facility where users.id ='"
. mysql_real_escape_string($p->provider->id) . "'");
$pdf->ezText($res['addr'],12);
$my_y = $pdf->y;
$pdf->ezNewPage();
$pdf->ezText('<b>' . $p->provider->get_name_display() . '</b>',12);
$pdf->ezText('<b>DEA:</b>' . $p->provider->federal_drug_id,12);
$pdf->ezColumnsStop();
if ($my_y < $pdf->y){
$pdf->ezSetY($my_y);
}
$pdf->ezText('',10);
$pdf->setLineStyle(1);
$pdf->ezColumnsStart(array('num'=>2));
$pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
$pdf->ezText('<b>Patient Name & Address</b>',6);
$pdf->ezText($p->patient->get_name_display(),10);
$res = sqlQuery("SELECT concat(street,'\n',city,', ',state,' ',postal_code,'\n',if(phone_home!='',phone_home,if(phone_cell!='',phone_cell,if(phone_biz!='',phone_biz,'')))) addr from patient_data where pid =". mysql_real_escape_string ($p->patient->id));
$pdf->ezText($res['addr']);
$my_y = $pdf->y;
$pdf->ezNewPage();
$pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
$pdf->ezText('<b>Date of Birth</b>',6);
$pdf->ezText($p->patient->date_of_birth,10);
$pdf->ezText('');
$pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
$pdf->ezText('<b>Medical Record #</b>',6);
$pdf->ezText(str_pad($p->patient->get_id(), 10, "0", STR_PAD_LEFT),10);
$pdf->ezColumnsStop();
if ($my_y < $pdf->y){
$pdf->ezSetY($my_y);
}
$pdf->ezText('');
$pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
$pdf->ezText('<b>Prescriptions</b>',6);
$pdf->ezText('',10);
}
function multiprint_footer(& $pdf){
if($this->pconfig['use_signature'] == true ) {
$pdf->ezImage($this->pconfig['signature'],"","","none","left");
}
else{
$pdf->ezText("\n\n\n\nSignature:________________________________\nDate: " . date('Y-m-d'),12);
}
}
function get_prescription_body_text($p) {
$body = '<b>Rx: ' . $p->get_drug() . ' ' . $p->get_size() . ' ' . $p->get_unit_display()
. ' [' . $p->form_array[$p->get_form()] . "]</b> <i>"
. $p->substitute_array[$p->get_substitute()] . "</i>\n"
. '<b>Disp #:</b> <u>' . $p->get_quantity() . "</u>\n"
. '<b>Sig:</b> ' . $p->get_dosage() . ' ' . $p->form_array[$p->get_form()] .' '
. $p->route_array[$p->get_route()] . ' ' . $p->interval_array[$p->get_interval()] . "\n";
if ($p->get_refills() > 0) {
$body .= "\n<b>Refills:</b> <u>" . $p->get_refills() . " of quantity " . $p->get_per_refill() . "</u>\n";
}
else {
$body .= "\n<b>Refills:</b> <u>0 (Zero)</u>\n";
}
$note = $p->get_note();
if ($note != '') {
$body .= "\n$note\n";
}
return $body;
}
function multiprint_body(& $pdf, $p){
$pdf->ez['leftMargin'] += $pdf->ez['leftMargin'];
$pdf->ez['rightMargin'] += $pdf->ez['rightMargin'];
$d = $this->get_prescription_body_text($p);
if ( $pdf->ezText($d,10,array(),1) ) {
$pdf->ez['leftMargin'] -= $pdf->ez['leftMargin'];
$pdf->ez['rightMargin'] -= $pdf->ez['rightMargin'];
$this->multiprint_footer($pdf, $p);
$pdf->ezNewPage();
$this->multiprint_header($pdf, $p);
$pdf->ez['leftMargin'] += $pdf->ez['leftMargin'];
$pdf->ez['rightMargin'] += $pdf->ez['rightMargin'];
}
$my_y = $pdf->y;
$pdf->ezText($d,10);
$pdf->setColor(.9,.9,.9);
$pdf->filledRectangle($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin']-$pdf->ez['leftMargin'],$my_y - $pdf->y);
$pdf->setColor(0,0,0);
$pdf->ezSetY($my_y);
$pdf->ezText($d,10);
$pdf->ez['leftMargin'] = $GLOBALS['oer_config']['prescriptions']['left'];
$pdf->ez['rightMargin'] = $GLOBALS['oer_config']['prescriptions']['right'];
$pdf->ezText('');
$pdf->line($pdf->ez['leftMargin'],$pdf->y,$pdf->ez['pageWidth']-$pdf->ez['rightMargin'],$pdf->y);
$pdf->ezText('');
}
function multiprint_action($id = "") {
$_POST['process'] = "true";
if(empty($id)) {
$this->function_argument_error();
}
require_once ($GLOBALS['fileroot'] . "/library/classes/class.ezpdf.php");
$pdf =& new Cezpdf($GLOBALS['oer_config']['prescriptions']['paper_size']);
$pdf->ezSetMargins($GLOBALS['oer_config']['prescriptions']['top']
,$GLOBALS['oer_config']['prescriptions']['bottom']
,$GLOBALS['oer_config']['prescriptions']['left']
,$GLOBALS['oer_config']['prescriptions']['right']
);
$pdf->selectFont($GLOBALS['fileroot'] . "/library/fonts/Helvetica.afm");
$print_header = true;
//print prescriptions body
$this->_state = false; // Added by Rod - see Controller.class.php
$ids = preg_split('/::/', substr($id,1,strlen($id) - 2), -1, PREG_SPLIT_NO_EMPTY);
foreach ($ids as $id) {
$p = new Prescription($id);
if ($print_header == true) {
$this->multiprint_header($pdf, $p);
$print_header = false;
}
$this->multiprint_body($pdf, $p);
}
$this->multiprint_footer($pdf);
$pdf->ezStream();
return;
}
function send_action_process($id) {
$dummy = ""; // Added by Rod to avoid run-time warnings
if ($_POST['process'] != "true")
return;
if(empty($id)) {
$this->function_argument_error();
}
$p = new Prescription($id);
switch ($_POST['submit']) {
case "Print":
// The following statement added by Rod.
// Looking at Controller.class.php, it appears that _state is set to false
// to indicate that no further HTML is to be generated.
$this->_state = false; // Added by Rod - see Controller.class.php
return $this->_print_prescription($p, $dummy);
break;
case "Email":
return $this->_email_prescription($p,$_POST['email_to']);
break;
case "Fax":
//this is intended to be the hook for the hylafax code we already have that hasn't worked its way into the tree yet.
//$this->assign("process_result","No fax server is currently setup.");
return $this->_fax_prescription($p,$_POST['fax_to']);
break;
case "Auto Send":
$pharmacy_id = $_POST['pharmacy_id'];
//echo "auto sending to : " . $_POST['pharmacy_id'];
$phar = new Pharmacy($_POST['pharmacy_id']);
//print_r($phar);
if ($phar->get_transmit_method() == TRANSMIT_PRINT) {
return $this->_print_prescription($p, $dummy);
}
elseif ($phar->get_transmit_method() == TRANSMIT_EMAIL) {
$email = $phar->get_email();
if (!empty($email)) {
return $this->_email_prescription($p,$phar->get_email());
}
//else print it
}
elseif ($phar->get_transmit_method() == TRANSMIT_FAX) {
$faxNum= $phar->get_fax();
if(!empty($faxNum)) {
Return $this->_fax_prescription ($p,$faxNum);
}
// return $this->assign("process_result","No fax server is currently setup.");
// else default is printing,
}
else {
//the pharmacy has no default or default is print
return $this->_print_prescription($p, $dummy);
}
break;
}
return;
}
function _print_prescription($p, & $toFile) {
require_once ($GLOBALS['fileroot'] . "/library/classes/class.ezpdf.php");
$pdf =& new Cezpdf($GLOBALS['oer_config']['prescriptions']['paper_size']);
$pdf->ezSetMargins($GLOBALS['oer_config']['prescriptions']['top']
,$GLOBALS['oer_config']['prescriptions']['bottom']
,$GLOBALS['oer_config']['prescriptions']['left']
,$GLOBALS['oer_config']['prescriptions']['right']
);
$pdf->selectFont($GLOBALS['fileroot'] . "/library/fonts/Helvetica.afm");
$this->multiprint_header($pdf, $p);
$this->multiprint_body($pdf, $p);
$this->multiprint_footer($pdf);
if(!empty($toFile))
{
$toFile = $pdf->ezOutput();
}
else
{
$pdf->ezStream();
// $pdf->ezStream(array('compress' => 0)); // for testing with uncompressed output
}
return;
}
function _print_prescription_old($p, & $toFile) {
require_once ($GLOBALS['fileroot'] . "/library/classes/class.ezpdf.php");
$pdf =& new Cezpdf($GLOBALS['oer_config']['prescriptions']['paper_size']);
$pdf->ezSetMargins($GLOBALS['oer_config']['prescriptions']['top']
,$GLOBALS['oer_config']['prescriptions']['bottom']
,$GLOBALS['oer_config']['prescriptions']['left']
,$GLOBALS['oer_config']['prescriptions']['right']
);
$pdf->selectFont($GLOBALS['fileroot'] . "/library/fonts/Helvetica.afm");
if(!empty($this->pconfig['logo'])) {
$pdf->ezImage($this->pconfig['logo'],"","","none","left");
}
$pdf->ezText($p->get_prescription_display(),10);
if($this->pconfig['use_signature'] == true ) {
$pdf->ezImage($this->pconfig['signature'],"","","none","left");
}
else{
$pdf->ezText("\n\n\n\nSignature:________________________________",10);
}
if(!empty($toFile))
{
$toFile = $pdf->ezOutput();
}
else
{
$pdf->ezStream();
// $pdf->ezStream(array('compress' => 0)); // for testing with uncompressed output
}
return;
}
function _email_prescription($p,$email) {
if (empty($email)) {
$this->assign("process_result","Email could not be sent, the address supplied: '$email' was empty or invalid.");
return;
}
require($GLOBALS['fileroot'] . "/library/classes/class.phpmailer.php");
$mail = new PHPMailer();
$mail->SetLanguage("en",$GLOBALS['fileroot'] . "/library/" );
//this is a temporary config item until the rest of the per practice billing settings make their way in
$mail->From = $GLOBALS['practice_return_email_path'];
$mail->FromName = $p->provider->get_name_display();
$mail->isMail();
$mail->Host = "localhost";
$mail->Mailer = "mail";
$text_body = $p->get_prescription_display();
$mail->Body = $text_body;
$mail->Subject = "Prescription for: " . $p->patient->get_name_display();
$mail->AddAddress($email);
if($mail->Send()) {
$this->assign("process_result","Email was successfully sent to: " . $email);
return;
}
else {
$this->assign("process_result","There has been a mail error sending to " . $_POST['email_to'] . " " . $mail->ErrorInfo);
return;
}
}
function do_lookup() {
if ($_POST['process'] != "true")
return;
$list = array();
if (!empty($_POST['drug'])) {
$list = @RxList::get_list($_POST['drug']);
}
if (is_array($list)) {
$list = array_flip($list);
$this->assign("drug_options",$list);
$this->assign("drug_values",array_keys($list));
}
else {
$this->assign("NO_RESULTS","No results found for: " .$_POST['drug'] . "<br />");
}
//print_r($_POST);
//$this->assign("PROCESS","");
$_POST['process'] = "";
}
function _fax_prescription($p,$faxNum)
{
$err = "Sent fax";
//strip - ,(, ), and ws
$faxNum = preg_replace("/(-*)(\(*)(\)*)(\s*)/","",$faxNum);
//validate the number
if(!empty($faxNum) && is_numeric($faxNum))
{
//get the sendfax command and execute it
$cmd = $this->pconfig['sendfax'];
// prepend any prefix to the fax number
$pref=$this->pconfig['prefix'];
$faxNum=$pref.$faxNum;
if(empty($cmd))
{
$err .= " Send fax not set in includes/config.php";
break;
}
else
{
//generate file to fax
$faxFile = "Failed";
$this->_print_prescription($p, $faxFile);
if(empty($faxFile))
{
$err .= " _print_prescription returned empty file";
break;
}
$fileName = dirname(__FILE__)."/../documents/".$p->get_id()
.$p->get_patient_id()."_fax_.pdf";
//print "filename is $fileName";
touch($fileName); // php bug
$handle = fopen($fileName,"w");
if(!$handle)
{
$err .= " Failed to open file $fileName to write fax to";
break;
}
if(fwrite($handle, $faxFile) === false)
{
$err .= " Failed to write data to $fileName";
break;
}
fclose($handle);
$args = " -n -d $faxNum $fileName";
//print "command is $cmd $args<br>";
exec($cmd . $args);
}
}
else
{
$err = "bad fax number passed to function";
}
if($err)
{
$this->assign("process_result",$err);
}
}
}
?>