added ending dates of service
[openemr.git] / library / freeb / process_bills.php
blob1e5a10ef5b6e35a7db3928d902001eb0926a1e40
1 <?php
2 set_time_limit(0);
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";
11 exit;
14 /****
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);
18 $billkeys = array();
19 if (!$results) {
20 echo "There was an error with the database.\n";
21 echo $db->ErrorMsg();
22 exit;
24 if ($results->RecordCount() == 0) {
25 echo "No bills queued for processing.\n";
26 exit;
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'];
32 $billkeys[] = $ta;
33 $results->MoveNext();
35 ****/
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);
42 $billkeys = array();
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;
51 $ta = array();
52 $ta['key'] = $row['patient_id'] . "-" . $row['encounter_id'];
53 $ta['bill_process'] = $row['bill_process'];
54 $ta['format'] = $row['target'];
55 $billkeys[] = $ta;
58 if (empty($billkeys)) {
59 echo "No bills queued for processing.\n";
60 exit;
63 foreach ($billkeys as $billkey) {
64 $tmp = split("-", $billkey['key']);
65 $patient_id = $tmp[0];
66 $encounter = $tmp[1];
67 $name = "FreeB.Bill.process";
68 $format = $billkey['format'];
70 if (empty($format)) {
71 $format = $GLOBALS['oer_config']['freeb']['default_format'];
73 $file_type = "txt";
74 if ($format == "hcfa" || $format == "ub92") {
75 $file_type = "pdf";
77 else {
78 $file_type = "edi";
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);
86 $c->setDebug(0);
87 $r = $c->send($f);
88 if (!$r) die("send failed");
89 $v = $r->value();
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("/" .
95 $presult) . "\n";
97 /****
98 $sql = "UPDATE billing set process_date = now(), bill_process = 2, process_file = '" .
99 basename("/" . $presult) . "' where encounter = $encounter AND pid = '" .
100 $patient_id . "'";
101 $results = $db->Execute($sql);
102 if (!$results) {
103 echo "There was an error with the database.\n";
104 echo $db->ErrorMsg() . "\n";
106 ****/
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));
123 fclose($ofh);
124 fclose($ifh);
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));
135 else {
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";
140 /****
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);
145 if (!$results) {
146 echo "There was an error with the database.\n";
147 echo $db->ErrorMsg() . "\n";
149 ****/
151 if (!updateClaim(false, $patient_id, $encounter, -1, -1, 3, basename("/" . $presult))) {
152 echo "Internal error: failed to update claim $patient_id-$encounter\n";
158 echo "\n\n";