file table.sql was added on branch rel-310 on 2009-07-31 19:39:59 +0000
[openemr.git] / library / freeb / process_bills.php
blob04f1614856e97acd5d5b17f4e40cb17323c12a2c
1 <?php
2 set_time_limit(0);
3 require ("xmlrpc.inc");
4 require (dirname(__FILE__) . "/../sql.inc");
5 require (dirname(__FILE__) . "/../../includes/config.php");
7 if ($argv[1] != "bill") {
8 echo "This script can only be accessed as a CLI program.\n";
9 echo "To execute from the command line run 'php -q process_bills.php bill' .\n";
10 exit;
13 $db = $GLOBALS['adodb']['db'];
15 //Used for testing purposed to send the same transactions through again and again
16 //$sql = "update billing set billed = 0, bill_process = 0, bill_date = null, process_date = null, process_file = null";
17 //$results = $db->Execute($sql);
19 $sql = "SELECT * from billing WHERE bill_process = 1 or bill_process = 5 group by pid,encounter" ;
21 $results = $db->Execute($sql);
23 $billkeys = array();
25 if (!$results) {
26 echo "There was an error with the database.\n";
27 echo $db->ErrorMsg();
28 exit;
31 if ($results->RecordCount() == 0) {
32 echo "No bills queued for processing.\n";
33 exit;
36 while (!$results->EOF) {
37 $ta['key'] = $results->fields['pid'] . "-" . $results->fields['encounter'];
38 $ta['bill_process'] = $results->fields['bill_process'];
39 $ta['format'] = $results->fields['target'];
40 $billkeys[] = $ta;
41 $results->MoveNext();
44 foreach ($billkeys as $billkey) {
45 $tmp = split("-",$billkey['key']);
46 $patient_id = $tmp[0];
47 $encounter = $tmp[1];
48 $name = "FreeB.Bill.process";
50 $format = $billkey['format'];
52 if (empty($format)) {
53 $format = $GLOBALS['oer_config']['freeb']['default_format'];
55 $file_type = "txt";
56 if ($format == "hcfa") {
57 $file_type = "pdf";
59 else {
60 $file_type = "edi";
62 echo "Creating job for: " . $billkey['key'] . " as $format returning $file_type.\n";
63 $args = array(new xmlrpcval($billkey['key'],XMLRPCSTRING), new xmlrpcval($format),new xmlrpcval($file_type));
65 $f=new xmlrpcmsg($name,$args);
66 //print "<pre>" . htmlentities($f->serialize()) . "</pre>\n";
67 $c=new xmlrpc_client("/RPC2", "localhost",18081);
68 $c->setDebug(0);
69 $r=$c->send($f);
71 if (!$r) { die("send failed"); }
72 $v=$r->value();
73 if (!$r->faultCode()) {
74 //print "<HR>I got this value back for $name<BR>";
75 $presult = $v->scalarval();
76 echo "Claim for PatientID: $patient_id, Encounter: $billkey[key] processed successfully. Results are in file:\n " . basename("/" . $presult) . "\n";
77 $sql = "UPDATE billing set process_date = now(), bill_process = 2, process_file = '" . basename("/" . $presult) . "' where encounter = $encounter AND pid = '" . $patient_id . "'";
78 $results = $db->Execute($sql);
79 if (!$results) {
80 echo "There was an error with the database.\n";
81 echo $db->ErrorMsg() . "\n";
83 elseif ($billkey['bill_process'] == 5) {
84 //the bill was generated without an error now print it
85 $fconfig = $GLOBALS['oer_config']['freeb'];
86 $fname = $fconfig['claim_file_dir'] . basename("/" . $presult);
87 $estring = $fconfig['print_command'] . " -P " . $fconfig['printer_name'] . " " . $fconfig['printer_extras'] . " " . $fname;
88 //echo $estring . "<br>";
89 $rstring = exec(escapeshellcmd($estring));
91 //echo "<PRE>" . htmlentities($r->serialize()). "</PRE>\n";
93 else {
94 //print "<HR>Fault: ";
95 $presult = "Code: " . $r->faultCode() . " Reason '" .$r->faultString()."'<BR>";
96 echo "Claim for PatientID: $patient_id, Encounter: $billkey[key] failed due to: \n " . basename("/" . $presult) . "\n";
97 $sql = "UPDATE billing set process_date = now(), bill_process = 3, process_file = '" . mysql_real_escape_string(basename("/" . $presult)) . "' where encounter = $encounter AND pid = '" . $patient_id . "'";
98 $results = $db->Execute($sql);
99 if (!$results) {
100 echo "There was an error with the database.\n";
101 echo $db->ErrorMsg() . "\n";
106 echo "\n\n";