tranlsation function xl()
[openemr.git] / interface / billing / billing_process.php
blob2cff1a5a9cdfa843ae108835ed83ff65c0aa692b
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 500 is almost definitely an error
33 if (filesize($fname) > 500) {
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_ub92_print'])) {
132 $sql .= " bill_process = 5, target = 'ub92', ";
133 } else if (isset($ar['bn_ub92'])) {
134 $sql .= " bill_process = 1, target = 'ub92', ";
135 } else if (isset($ar['bn_x12'])) {
136 $sql .= " bill_process = 1, target = '" . $target . "', x12_partner_id = '" . mysql_real_escape_string($claim_array['partner']) . "', ";
137 } else if (isset($ar['bn_mark'])) {
138 $sql .= " billed = 1, ";
139 $mark_only = true;
142 $sql .= " payer_id = '$payer' where encounter = " . $encounter . " and pid = " . $patient_id;
144 //echo $sql;
145 $result = $db->Execute($sql);
147 if(!$result) {
148 $bill_info[] = "Claim $claimid could not be queued due to error:" . $db->ErrorMsg() . "\n";
150 else {
151 // wtf is mark_as_billed? nothing sets it! -- Rod
152 // if($ar['mark_as_billed'] == 1) {
153 if($mark_only) {
154 $bill_info[] = "Claim $claimid was marked as billed only.\n";
156 else {
157 $bill_info[] = "Claim $claimid was queued successfully.\n";
160 if ($mark_only) {
161 //send claim to the web services code to sync to external system if enabled
162 //remeber that in openemr it is only the encounter and patient id that make a group of
163 //billing line items unique so they can be grouped or associated as 1 claim
165 $ws = new WSClaim($patient_id,$encounter);
174 <html>
175 <head>
178 <link rel=stylesheet href="<?echo $css_header;?>" type="text/css">
180 </head>
181 <body <?echo $top_bg_line;?> topmargin=0 rightmargin=0 leftmargin=2 bottommargin=0 marginwidth=2 marginheight=0>
182 <br><p><h3>Billing queue results:</h3><a href="billing_report.php">back</a><ul>
184 foreach ($bill_info as $infoline) {
185 echo nl2br($infoline);
188 </ul></p>
189 </body>
190 </html>