Initial import.
[openemr.git] / interface / billing / billing_process.php
blobd1e72ba5b0064137a3144d70a2d8f40a972783d9
1 <?php
2 include_once("../globals.php");
4 include_once("$srcdir/patient.inc");
5 include_once("$srcdir/billrep.inc");
6 include_once(dirname(__FILE__) . "/../../library/classes/WSClaim.class.php");
8 $fconfig = $GLOBALS['oer_config']['freeb'];
9 $bill_info = array();
11 if (isset($_POST['bn_electronic_file']) && !empty($_POST['claims'])) {
13 if (empty($_POST['claims'])) {
14 $bill_info[] = "No claims were selected for inclusion.";
17 $efile = array();
19 foreach ($_POST['claims'] as $claim) {
20 if (isset($claim['bill'])) {
21 if (substr($claim['file'],strlen($claim['file']) -4) == '.pdf') {
22 $fname = substr($claim['file'],0,-4);
24 else {
25 $fname = $claim['file'];
27 $fname = preg_replace("[/]","",$fname);
28 $fname = preg_replace("[\.\.]","",$fname);
29 $fname = preg_replace("[\\\\]","",$fname);
30 $fname = $fconfig['claim_file_dir'] . $fname;
31 if (file_exists($fname)) {
32 //less than 700 is almost definetely an error
33 if (filesize($fname) > 700) {
34 $bill_info[] = "Added: " . $fname . "\n";
35 $ta = array();
36 $ta["data"] = file_get_contents($fname);
37 $ta["size"] = filesize($fname);
38 $efile[] = $ta;
40 else {
41 $bill_info[] = "May have an error:" . $fname . "\n";
44 else {
45 $bill_info[] = "Not found:" . $fname . "\n";
50 if (!empty($efile)) {
51 $db = $GLOBALS['adodb']['db'];
52 $error = false;
53 foreach ($_POST['claims'] as $claimid => $claim) {
54 if (isset($claim['bill'])) {
55 $tmpvars = split("-",$claimid);
56 $pid = $tmpvars[0];
57 $encounter = $tmpvars[1];
58 if (!empty($encounter) && !empty($pid)) {
59 $sql = "UPDATE billing set billed = 1 where encounter = '" . $encounter .
60 "' and pid = '" . $pid . "' and activity != 0";
61 $result = $db->execute($sql);
62 if(!$result) {
63 $error = true;
64 $bill_info[] = "Marking claim $claimid had a db error:" . $db->ErrorMsg() . "\n";
67 //send claim to the web services code to sync to external system if enabled
68 //remeber that in openemr it is only the encounter and patient id that make a group of
69 //billing line items unique so they can be grouped or associated as 1 claim
70 $ws = new WSClaim($pid, $encounter);
74 if (!$error) {
75 header("Pragma: public");
76 header("Expires: 0");
77 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
78 header("Content-Type: application/force-download");
79 header("Content-Disposition: attachment; filename=". date("Y-m-d") . "-billing_batch.txt");
80 header("Content-Description: File Transfer");
82 $data = "";
83 $size = 0;
85 foreach ($efile as $file) {
86 $data .= $file['data'];
87 $size += $file['size'];
90 header("Content-Length: " . $size);
91 echo $data;
92 exit;
97 else {
98 process_form($_POST);
102 function process_form($ar) {
103 global $bill_info;
105 $db = $GLOBALS['adodb']['db'];
107 if (empty($ar['claims'])) {
108 $ar['claims'] = array();
110 foreach ($ar['claims'] as $claimid => $claim_array) {
112 $ta = split("-",$claimid);
113 $patient_id = $ta[0];
114 $encounter = $ta[1];
115 $payer = $claim_array['payer'];
117 if (isset($claim_array['bill'])) {
118 $sql = "SELECT x.processing_format from x12_partners as x where x.id =" . $db->qstr($claim_array['partner']);
119 $result = $db->Execute($sql);
120 $target = "x12";
121 if ($result && !$result->EOF) {
122 $target = $result->fields['processing_format'];
125 $sql = "UPDATE billing set bill_date = NOW(), ";
127 if (isset($ar['bn_hcfa_print'])) {
128 $sql .= " bill_process = 5, target = 'hcfa', ";
129 } else if (isset($ar['bn_hcfa'])) {
130 $sql .= " bill_process = 1, target = 'hcfa', ";
131 } else if (isset($ar['bn_x12'])) {
132 $sql .= " bill_process = 1, target = '" . $target . "', x12_partner_id = '" . mysql_real_escape_string($claim_array['partner']) . "', ";
133 } else if (isset($ar['bn_mark'])) {
134 $sql .= " billed = 1, ";
135 $mark_only = true;
138 $sql .= " payer_id = '$payer' where encounter = " . $encounter . " and pid = " . $patient_id;
140 //echo $sql;
141 $result = $db->Execute($sql);
143 if(!$result) {
144 $bill_info[] = "Claim $claimid could not be queued due to error:" . $db->ErrorMsg() . "\n";
146 else {
147 // wtf is mark_as_billed? nothing sets it! -- Rod
148 // if($ar['mark_as_billed'] == 1) {
149 if($mark_only) {
150 $bill_info[] = "Claim $claimid was marked as billed only.\n";
152 else {
153 $bill_info[] = "Claim $claimid was queued successfully.\n";
156 if ($mark_only) {
157 //send claim to the web services code to sync to external system if enabled
158 //remeber that in openemr it is only the encounter and patient id that make a group of
159 //billing line items unique so they can be grouped or associated as 1 claim
161 $ws = new WSClaim($patient_id,$encounter);
170 <html>
171 <head>
174 <link rel=stylesheet href="<?echo $css_header;?>" type="text/css">
176 </head>
177 <body <?echo $top_bg_line;?> topmargin=0 rightmargin=0 leftmargin=2 bottommargin=0 marginwidth=2 marginheight=0>
178 <br><p><h3>Billing queue results:</h3><a href="billing_report.php">back</a><ul>
180 foreach ($bill_info as $infoline) {
181 echo nl2br($infoline);
184 </ul></p>
185 </body>
186 </html>