3 // require_once("{$GLOBALS['srcdir']}/sql.inc");
4 require_once(dirname(__FILE__) . "/sql.inc");
6 function getBillingById ($id, $cols = "*")
8 return sqlQuery("select $cols from billing where id='$id' and activity=1 order by date DESC limit 0,1");
11 function getBillingByPid ($pid, $cols = "*")
13 return sqlQuery("select $cols from billing where pid ='$pid' and activity=1 order by date DESC limit 0,1");
16 function getBillingByEncounter ($pid,$encounter, $cols = "code_type, code, code_text")
18 $res = sqlStatement("select $cols from billing where encounter = ? and pid=? and activity=1 order by code_type, date ASC", array($encounter,$pid) );
20 for($iter=0; $row=sqlFetchArray($res); $iter++)
27 function addBilling($encounter_id, $code_type, $code, $code_text, $pid,
28 $authorized="0", $provider, $modifier="", $units="", $fee="0.00",
29 $ndc_info='', $justify='', $billed=0)
31 $sql = "insert into billing (date, encounter, code_type, code, code_text, " .
32 "pid, authorized, user, groupname, activity, billed, provider_id, " .
33 "modifier, units, fee, ndc_info, justify) values (" .
34 "NOW(), '$encounter_id', '$code_type', '$code', '$code_text', '$pid', " .
35 "'$authorized', '" . $_SESSION['authId'] . "', '" .
36 $_SESSION['authProvider'] . "', 1, $billed, $provider, '$modifier', '$units', " .
37 "'$fee', '$ndc_info', '$justify')";
38 return sqlInsert($sql);
41 function authorizeBilling($id, $authorized = "1")
43 sqlQuery("update billing set authorized = '$authorized' where id = '$id'");
46 function deleteBilling($id)
48 sqlStatement("update billing set activity = 0 where id = '$id'");
51 function clearBilling($id)
53 sqlStatement("update billing set justify = '' where id = '$id'");
56 // This function supports the Billing page (billing_process.php), freeb
57 // processing (process_bills.php), and initiation of secondary processing
58 // (sl_eob.inc.php). It is called in the following situations:
60 // * billing_process.php sets bill_time, bill_process, payer and target on
61 // queueing a claim for freeb processing. Create claims row.
62 // * billing_process.php sets claim status to 2, and payer, on marking a
63 // claim as billed without actually generating any billing. Create a
64 // claims row. In this case bill_process will remain at 0 and process_time
65 // and process_file will not be set.
66 // * billing_process.php sets bill_process, payer, target and x12 partner
67 // before calling gen_x12_837. Create a claims row.
68 // * billing_process.php sets claim status to 2 (billed), bill_process to 2,
69 // process_time and process_file after calling gen_x12_837. Claims row
71 // * process_bills.php sets bill_process to 2, process_time, and process_file
72 // after invoking freeb to process a claim. Claims row already exists.
73 // * billing_process.php sets claim status to 2 (billed) after creating
74 // an electronic freeb batch (hcfa-only with recent changes). Claims
75 // row already exists.
76 // * EOB posting updates claim status to mark a payer as done. Claims row
78 // * EOB posting reopens an encounter for billing a secondary payer. Create
81 // $newversion should be passed to us to indicate if a new claims row
82 // is to be generated, otherwise one must already exist. The payer, if
83 // passed in for the latter case, must match the existing claim.
85 // Currently on the billing page the user can select any of the patient's
86 // payers. That logic will tailor the payer choices to the encounter date.
88 function updateClaim($newversion, $patient_id, $encounter_id, $payer_id=-1, $payer_type=-1,
89 $status=-1, $bill_process=-1, $process_file='', $target='', $partner_id=-1,$crossover=0)
92 $sql = "SELECT * FROM claims WHERE patient_id = '$patient_id' AND " .
93 "encounter_id = '$encounter_id' AND status > 0 AND status < 4 ";
94 if ($payer_id >= 0) $sql .= "AND payer_id = '$payer_id' ";
95 $sql .= "ORDER BY version DESC LIMIT 1";
96 $row = sqlQuery($sql);
98 if ($payer_id < 0) $payer_id = $row['payer_id'];
99 if ($status < 0) $status = $row['status'];
100 if ($bill_process < 0) $bill_process = $row['bill_process'];
101 if ($partner_id < 0) $partner_id = $row['x12_partner_id'];
102 if (!$process_file ) $process_file = $row['process_file'];
103 if (!$target ) $target = $row['target'];
108 if (empty($payer_id) || $payer_id < 0) $payer_id = 0;
110 if ($status==7) {//$status==7 is the claim denial case.
111 $claimset .= ", status = '$status'";
113 elseif ($status >= 0) {
114 $claimset .= ", status = '$status'";
116 $billset .= ", billed = 1";
117 if ($status == 2) $billset .= ", bill_date = NOW()";
119 $billset .= ", billed = 0";
122 if ($status==7) {//$status==7 is the claim denial case.
123 $billset .= ", bill_process = '$status'";
125 elseif ($bill_process >= 0) {
126 $claimset .= ", bill_process = '$bill_process'";
127 $billset .= ", bill_process = '$bill_process'";
129 if ($status==7) {//$status==7 is the claim denial case.
130 $claimset .= ", process_file = '$process_file'";//Denial reason code is stored here
132 elseif ($process_file) {
133 $claimset .= ", process_file = '$process_file', process_time = NOW()";
134 $billset .= ", process_file = '$process_file', process_date = NOW()";
137 $claimset .= ", target = '$target'";
138 $billset .= ", target = '$target'";
140 if ($payer_id >= 0) {
141 $claimset .= ", payer_id = '$payer_id', payer_type = '$payer_type'";
142 $billset .= ", payer_id = '$payer_id'";
144 if ($partner_id >= 0) {
145 $claimset .= ", x12_partner_id = '$partner_id'";
146 $billset .= ", x12_partner_id = '$partner_id'";
150 $billset = substr($billset, 2);
151 sqlStatement("UPDATE billing SET $billset WHERE " .
152 "encounter = '$encounter_id' AND pid='$patient_id' AND activity = 1");
155 // If a new claim version is requested, insert its row.
159 $payer_id = ($payer_id < 0) ? $row['payer_id'] : $payer_id;
160 $bill_process = ($bill_process < 0) ? $row['bill_process'] : $bill_process;
161 $process_file = ($process_file) ? $row['process_file'] : $process_file;
162 $target = ($target) ? $row['target'] : $target;
163 $partner_id = ($partner_id < 0) ? $row['x12_partner_id'] : $partner_id;
164 $sql = "INSERT INTO claims SET " .
165 "patient_id = '$patient_id', " .
166 "encounter_id = '$encounter_id', " .
167 "bill_time = UNIX_TIMESTAMP(NOW()), " .
168 "payer_id = '$payer_id', " .
169 "status = '$status', " .
170 "payer_type = '" . $row['payer_type'] . "', " .
171 "bill_process = '$bill_process', " .
172 "process_time = '" . $row['process_time'] . "', " .
173 "process_file = '$process_file', " .
174 "target = '$target', " .
175 "x12_partner_id = '$partner_id'";
179 $sql = "INSERT INTO claims SET " .
180 "patient_id = '$patient_id', " .
181 "encounter_id = '$encounter_id', " .
182 "bill_time = NOW() $claimset";
185 {//Claim automatic forward case.
187 $sql = "INSERT INTO claims SET " .
188 "patient_id = '$patient_id', " .
189 "encounter_id = '$encounter_id', " .
190 "bill_time = NOW(), status=$status";
197 // Otherwise update the existing claim row.
199 else if ($claimset) {
200 $claimset = substr($claimset, 2);
201 sqlStatement("UPDATE claims SET $claimset WHERE " .
202 "patient_id = '$patient_id' AND encounter_id = '$encounter_id' AND " .
203 // "payer_id = '" . $row['payer_id'] . "' AND " .
204 "version = '" . $row['version'] . "'");
207 // Whenever a claim is marked billed, update A/R accordingly.
210 if ($GLOBALS['oer_config']['ws_accounting']['enabled'] === 2) {
211 if ($payer_type > 0) {
212 sqlStatement("UPDATE form_encounter SET " .
213 "last_level_billed = '$payer_type' WHERE " .
214 "pid = '$patient_id' AND encounter = '$encounter_id'");
218 $ws = new WSClaim($patient_id, $encounter_id);
225 // Determine if anything in a visit has been billed.
227 function isEncounterBilled($pid, $encounter) {
228 $row = sqlQuery("SELECT count(*) AS count FROM billing WHERE " .
229 "pid = '$pid' AND encounter = '$encounter' AND activity = 1 AND " .
231 $count = $row['count'];
233 $row = sqlQuery("SELECT count(*) AS count FROM drug_sales WHERE " .
234 "pid = '$pid' AND encounter = '$encounter' AND billed = 1");
235 $count = $row['count'];
237 return $count ? true : false;