3 require_once("xmlrpc.inc");
4 require_once(dirname(__FILE__
) . "/../sql.inc");
5 require_once(dirname(__FILE__
) . "/../../includes/config.php");
6 require_once(dirname(__FILE__
) . "/../billing.inc");
8 if ($argv[1] != "bill") {
9 echo "This script can only be accessed as a CLI program.\n";
10 echo "To execute from the command line run 'php -q process_bills.php bill' .\n";
15 $db = $GLOBALS['adodb']['db'];
16 $sql = "SELECT * from billing WHERE bill_process = 1 or bill_process = 5 group by pid,encounter" ;
17 $results = $db->Execute($sql);
20 echo "There was an error with the database.\n";
24 if ($results->RecordCount() == 0) {
25 echo "No bills queued for processing.\n";
28 while (!$results->EOF) {
29 $ta['key'] = $results->fields['pid'] . "-" . $results->fields['encounter'];
30 $ta['bill_process'] = $results->fields['bill_process'];
31 $ta['format'] = $results->fields['target'];
37 $sql = "SELECT * from claims WHERE " .
38 "( bill_process = 1 or bill_process = 5) AND " .
39 "status > 0 AND status < 4";
40 $res = sqlStatement($sql);
44 while ($row = sqlFetchArray($res)) {
45 $crow = sqlQuery("SELECT count(*) AS count FROM claims WHERE " .
46 "patient_id = '" . $row['patient_id'] . "' AND " .
47 "encounter_id = '" . $row['encounter_id'] . "' AND " .
48 "version > '" . $row['version'] . "'");
49 if ($crow['count']) continue;
52 $ta['key'] = $row['patient_id'] . "-" . $row['encounter_id'];
53 $ta['bill_process'] = $row['bill_process'];
54 $ta['format'] = $row['target'];
58 if (empty($billkeys)) {
59 echo "No bills queued for processing.\n";
63 foreach ($billkeys as $billkey) {
64 $tmp = split("-", $billkey['key']);
65 $patient_id = $tmp[0];
67 $name = "FreeB.Bill.process";
68 $format = $billkey['format'];
71 $format = $GLOBALS['oer_config']['freeb']['default_format'];
74 if ($format == "hcfa" ||
$format == "ub92") {
80 echo "Creating job for: " . $billkey['key'] . " as $format returning $file_type.\n";
81 $args = array(new xmlrpcval($billkey['key'], XMLRPCSTRING
),
82 new xmlrpcval($format),new xmlrpcval($file_type));
84 $f = new xmlrpcmsg($name,$args);
85 $c = new xmlrpc_client("/RPC2", "localhost", 18081);
88 if (!$r) die("send failed");
91 if (!$r->faultCode()) {
92 $presult = $v->scalarval();
93 echo "Claim for PatientID: $patient_id, Encounter: $billkey[key] " .
94 "processed successfully. Results are in file:\n " . basename("/" .
98 $sql = "UPDATE billing set process_date = now(), bill_process = 2, process_file = '" .
99 basename("/" . $presult) . "' where encounter = $encounter AND pid = '" .
101 $results = $db->Execute($sql);
103 echo "There was an error with the database.\n";
104 echo $db->ErrorMsg() . "\n";
108 if (!updateClaim(false, $patient_id, $encounter, -1, -1, 2, basename("/" . $presult))) {
109 echo "Internal error: failed to update claim $patient_id-$encounter\n";
112 else { // everything worked
113 $fconfig = $GLOBALS['oer_config']['freeb'];
114 $fbase = basename("/" . $presult);
115 $fname = $fconfig['claim_file_dir'] . $fbase;
116 // If we need to copy PDFs, do it.
117 if ($file_type == 'pdf' && $fconfig['copy_pdfs_to']) {
118 $ifh = fopen($fname, 'rb');
119 $ofh = fopen($fconfig['copy_pdfs_to'] . $fbase, 'w');
120 while ($ifh && $ofh && !feof($ifh)) {
121 fwrite($ofh, fread($ifh, 8192));
125 // chmod($fconfig['copy_pdfs_to'] . $fbase, 0666);
127 if ($billkey['bill_process'] == 5) {
128 //the bill was generated without an error now print it
129 $estring = $fconfig['print_command'] . " -P " . $fconfig['printer_name'] .
130 " " . $fconfig['printer_extras'] . " " . $fname;
131 $rstring = exec(escapeshellcmd($estring));
136 $presult = "Code: " . $r->faultCode() . " Reason '" .$r->faultString()."'<BR>";
137 echo "Claim for PatientID: $patient_id, Encounter: $billkey[key] failed due to: \n " .
138 basename("/" . $presult) . "\n";
141 $sql = "UPDATE billing set process_date = now(), bill_process = 3, process_file = '" .
142 mysql_real_escape_string(basename("/" . $presult)) .
143 "' where encounter = $encounter AND pid = '" . $patient_id . "'";
144 $results = $db->Execute($sql);
146 echo "There was an error with the database.\n";
147 echo $db->ErrorMsg() . "\n";
151 if (!updateClaim(false, $patient_id, $encounter, -1, -1, 3, basename("/" . $presult))) {
152 echo "Internal error: failed to update claim $patient_id-$encounter\n";