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)
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;
111 $claimset .= ", status = '$status'";
113 $billset .= ", billed = 1";
114 if ($status == 2) $billset .= ", bill_date = NOW()";
116 $billset .= ", billed = 0";
119 if ($bill_process >= 0) {
120 $claimset .= ", bill_process = '$bill_process'";
121 $billset .= ", bill_process = '$bill_process'";
124 $claimset .= ", process_file = '$process_file', process_time = NOW()";
125 $billset .= ", process_file = '$process_file', process_date = NOW()";
128 $claimset .= ", target = '$target'";
129 $billset .= ", target = '$target'";
131 if ($payer_id >= 0) {
132 $claimset .= ", payer_id = '$payer_id', payer_type = '$payer_type'";
133 $billset .= ", payer_id = '$payer_id'";
135 if ($partner_id >= 0) {
136 $claimset .= ", x12_partner_id = '$partner_id'";
137 $billset .= ", x12_partner_id = '$partner_id'";
141 $billset = substr($billset, 2);
142 sqlStatement("UPDATE billing SET $billset WHERE " .
143 "encounter = '$encounter_id' AND pid='$patient_id' AND activity = 1");
146 // If a new claim version is requested, insert its row.
150 $payer_id = ($payer_id < 0) ? $row['payer_id'] : $payer_id;
151 $bill_process = ($bill_process < 0) ? $row['bill_process'] : $bill_process;
152 $process_file = ($process_file) ? $row['process_file'] : $process_file;
153 $target = ($target) ? $row['target'] : $target;
154 $partner_id = ($partner_id < 0) ? $row['x12_partner_id'] : $partner_id;
155 $sql = "INSERT INTO claims SET " .
156 "patient_id = '$patient_id', " .
157 "encounter_id = '$encounter_id', " .
158 "bill_time = UNIX_TIMESTAMP(NOW()), " .
159 "payer_id = '$payer_id', " .
160 "status = '$status', " .
161 "payer_type = '" . $row['payer_type'] . "', " .
162 "bill_process = '$bill_process', " .
163 "process_time = '" . $row['process_time'] . "', " .
164 "process_file = '$process_file', " .
165 "target = '$target', " .
166 "x12_partner_id = '$partner_id'";
168 $sql = "INSERT INTO claims SET " .
169 "patient_id = '$patient_id', " .
170 "encounter_id = '$encounter_id', " .
171 "bill_time = NOW() $claimset";
175 // Otherwise update the existing claim row.
177 else if ($claimset) {
178 $claimset = substr($claimset, 2);
179 sqlStatement("UPDATE claims SET $claimset WHERE " .
180 "patient_id = '$patient_id' AND encounter_id = '$encounter_id' AND " .
181 // "payer_id = '" . $row['payer_id'] . "' AND " .
182 "version = '" . $row['version'] . "'");
185 // Whenever a claim is marked billed, update A/R accordingly.
188 if ($GLOBALS['oer_config']['ws_accounting']['enabled'] === 2) {
189 if ($payer_type > 0) {
190 sqlStatement("UPDATE form_encounter SET " .
191 "last_level_billed = '$payer_type' WHERE " .
192 "pid = '$patient_id' AND encounter = '$encounter_id'");
196 $ws = new WSClaim($patient_id, $encounter_id);
203 // Determine if anything in a visit has been billed.
205 function isEncounterBilled($pid, $encounter) {
206 $row = sqlQuery("SELECT count(*) AS count FROM billing WHERE " .
207 "pid = '$pid' AND encounter = '$encounter' AND activity = 1 AND " .
209 $count = $row['count'];
211 $row = sqlQuery("SELECT count(*) AS count FROM drug_sales WHERE " .
212 "pid = '$pid' AND encounter = '$encounter' AND billed = 1");
213 $count = $row['count'];
215 return $count ? true : false;