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, $notecodes='')
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, notecodes) values (" .
34 "NOW(), ?, ?, ?, ?, ?, ?, ?, ?, 1, ?, ?, ?, ?, ?, ?, ?, ?)";
35 return sqlInsert($sql, array($encounter_id,$code_type,$code,$code_text,$pid,$authorized,$_SESSION['authId'],$_SESSION['authProvider'],$billed,$provider,$modifier,$units,$fee,$ndc_info,$justify,$notecodes));
38 function authorizeBilling($id, $authorized = "1")
40 sqlQuery("update billing set authorized = '$authorized' where id = '$id'");
43 function deleteBilling($id)
45 sqlStatement("update billing set activity = 0 where id = '$id'");
48 function clearBilling($id)
50 sqlStatement("update billing set justify = '' where id = '$id'");
53 // This function supports the Billing page (billing_process.php), freeb
54 // processing (process_bills.php), and initiation of secondary processing
55 // (sl_eob.inc.php). It is called in the following situations:
57 // * billing_process.php sets bill_time, bill_process, payer and target on
58 // queueing a claim for freeb processing. Create claims row.
59 // * billing_process.php sets claim status to 2, and payer, on marking a
60 // claim as billed without actually generating any billing. Create a
61 // claims row. In this case bill_process will remain at 0 and process_time
62 // and process_file will not be set.
63 // * billing_process.php sets bill_process, payer, target and x12 partner
64 // before calling gen_x12_837. Create a claims row.
65 // * billing_process.php sets claim status to 2 (billed), bill_process to 2,
66 // process_time and process_file after calling gen_x12_837. Claims row
68 // * process_bills.php sets bill_process to 2, process_time, and process_file
69 // after invoking freeb to process a claim. Claims row already exists.
70 // * billing_process.php sets claim status to 2 (billed) after creating
71 // an electronic freeb batch (hcfa-only with recent changes). Claims
72 // row already exists.
73 // * EOB posting updates claim status to mark a payer as done. Claims row
75 // * EOB posting reopens an encounter for billing a secondary payer. Create
78 // $newversion should be passed to us to indicate if a new claims row
79 // is to be generated, otherwise one must already exist. The payer, if
80 // passed in for the latter case, must match the existing claim.
82 // Currently on the billing page the user can select any of the patient's
83 // payers. That logic will tailor the payer choices to the encounter date.
85 function updateClaim($newversion, $patient_id, $encounter_id, $payer_id=-1, $payer_type=-1,
86 $status=-1, $bill_process=-1, $process_file='', $target='', $partner_id=-1,$crossover=0)
89 $sql = "SELECT * FROM claims WHERE patient_id = '$patient_id' AND " .
90 "encounter_id = '$encounter_id' AND status > 0 AND status < 4 ";
91 if ($payer_id >= 0) $sql .= "AND payer_id = '$payer_id' ";
92 $sql .= "ORDER BY version DESC LIMIT 1";
93 $row = sqlQuery($sql);
95 if ($payer_id < 0) $payer_id = $row['payer_id'];
96 if ($status < 0) $status = $row['status'];
97 if ($bill_process < 0) $bill_process = $row['bill_process'];
98 if ($partner_id < 0) $partner_id = $row['x12_partner_id'];
99 if (!$process_file ) $process_file = $row['process_file'];
100 if (!$target ) $target = $row['target'];
105 if (empty($payer_id) || $payer_id < 0) $payer_id = 0;
107 if ($status==7) {//$status==7 is the claim denial case.
108 $claimset .= ", status = '$status'";
110 elseif ($status >= 0) {
111 $claimset .= ", status = '$status'";
113 $billset .= ", billed = 1";
114 if ($status == 2) $billset .= ", bill_date = NOW()";
116 $billset .= ", billed = 0";
119 if ($status==7) {//$status==7 is the claim denial case.
120 $billset .= ", bill_process = '$status'";
122 elseif ($bill_process >= 0) {
123 $claimset .= ", bill_process = '$bill_process'";
124 $billset .= ", bill_process = '$bill_process'";
126 if ($status==7) {//$status==7 is the claim denial case.
127 $claimset .= ", process_file = '$process_file'";//Denial reason code is stored here
129 elseif ($process_file) {
130 $claimset .= ", process_file = '$process_file', process_time = NOW()";
131 $billset .= ", process_file = '$process_file', process_date = NOW()";
134 $claimset .= ", target = '$target'";
135 $billset .= ", target = '$target'";
137 if ($payer_id >= 0) {
138 $claimset .= ", payer_id = '$payer_id', payer_type = '$payer_type'";
139 $billset .= ", payer_id = '$payer_id'";
141 if ($partner_id >= 0) {
142 $claimset .= ", x12_partner_id = '$partner_id'";
143 $billset .= ", x12_partner_id = '$partner_id'";
147 $billset = substr($billset, 2);
148 sqlStatement("UPDATE billing SET $billset WHERE " .
149 "encounter = '$encounter_id' AND pid='$patient_id' AND activity = 1");
152 // If a new claim version is requested, insert its row.
156 $payer_id = ($payer_id < 0) ? $row['payer_id'] : $payer_id;
157 $bill_process = ($bill_process < 0) ? $row['bill_process'] : $bill_process;
158 $process_file = ($process_file) ? $row['process_file'] : $process_file;
159 $target = ($target) ? $row['target'] : $target;
160 $partner_id = ($partner_id < 0) ? $row['x12_partner_id'] : $partner_id;
161 $sql = "INSERT INTO claims SET " .
162 "patient_id = '$patient_id', " .
163 "encounter_id = '$encounter_id', " .
164 "bill_time = UNIX_TIMESTAMP(NOW()), " .
165 "payer_id = '$payer_id', " .
166 "status = '$status', " .
167 "payer_type = '" . $row['payer_type'] . "', " .
168 "bill_process = '$bill_process', " .
169 "process_time = '" . $row['process_time'] . "', " .
170 "process_file = '$process_file', " .
171 "target = '$target', " .
172 "x12_partner_id = '$partner_id'";
176 $sql = "INSERT INTO claims SET " .
177 "patient_id = '$patient_id', " .
178 "encounter_id = '$encounter_id', " .
179 "bill_time = NOW() $claimset";
182 {//Claim automatic forward case.
184 $sql = "INSERT INTO claims SET " .
185 "patient_id = '$patient_id', " .
186 "encounter_id = '$encounter_id', " .
187 "bill_time = NOW(), status=$status";
194 // Otherwise update the existing claim row.
196 else if ($claimset) {
197 $claimset = substr($claimset, 2);
198 sqlStatement("UPDATE claims SET $claimset WHERE " .
199 "patient_id = '$patient_id' AND encounter_id = '$encounter_id' AND " .
200 // "payer_id = '" . $row['payer_id'] . "' AND " .
201 "version = '" . $row['version'] . "'");
204 // Whenever a claim is marked billed, update A/R accordingly.
207 if ($GLOBALS['oer_config']['ws_accounting']['enabled'] === 2) {
208 if ($payer_type > 0) {
209 sqlStatement("UPDATE form_encounter SET " .
210 "last_level_billed = '$payer_type' WHERE " .
211 "pid = '$patient_id' AND encounter = '$encounter_id'");
215 $ws = new WSClaim($patient_id, $encounter_id);
222 // Determine if anything in a visit has been billed.
224 function isEncounterBilled($pid, $encounter) {
225 $row = sqlQuery("SELECT count(*) AS count FROM billing WHERE " .
226 "pid = '$pid' AND encounter = '$encounter' AND activity = 1 AND " .
228 $count = $row['count'];
230 $row = sqlQuery("SELECT count(*) AS count FROM drug_sales WHERE " .
231 "pid = '$pid' AND encounter = '$encounter' AND billed = 1");
232 $count = $row['count'];
234 return $count ? true : false;
237 // Get the co-pay amount that is effective on the given date.
238 // Or if no insurance on that date, return -1.
240 function getCopay($patient_id, $encdate) {
241 $tmp = sqlQuery("SELECT provider, copay FROM insurance_data " .
242 "WHERE pid = '$patient_id' AND type = 'primary' " .
243 "AND date <= '$encdate' ORDER BY date DESC LIMIT 1");
244 if ($tmp['provider']) return sprintf('%01.2f', 0 + $tmp['copay']);
248 // Get the total co-pay amount paid by the patient for an encounter
249 function getPatientCopay($patient_id, $encounter) {
250 $resMoneyGot = sqlStatement("SELECT sum(pay_amount) as PatientPay FROM ar_activity where ".
251 "pid = ? and encounter = ? and payer_type=0 and account_code='PCP'",
252 array($patient_id,$encounter));
253 //new fees screen copay gives account_code='PCP'
254 $rowMoneyGot = sqlFetchArray($resMoneyGot);
255 $Copay=$rowMoneyGot['PatientPay'];