Fix for web site change courtesy of info from whimmel.
[openemr.git] / library / billing.inc
blobaac4b4d59b1b6db22d37db7bfdf102c3e8d33eef
1 <?php
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 = '$encounter' and pid='$pid' and activity=1 order by code_type, date ASC");
20         for($iter=0; $row=sqlFetchArray($res); $iter++)
21         {
22                 $all[$iter] = $row;
23         }
24         return $all;
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
70 //   already exists.
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
77 //   already exists.
78 // * EOB posting reopens an encounter for billing a secondary payer.  Create
79 //   a claims row.
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)
91   if (!$newversion) {
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);
97     if (!$row) return 0;
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'];
104   }
106   $claimset = "";
107   $billset = "";
108   if (empty($payer_id) || $payer_id < 0) $payer_id = 0;
110   if ($status >= 0) {
111     $claimset .= ", status = '$status'";
112     if ($status > 1) {
113       $billset .= ", billed = 1";
114       if ($status == 2) $billset  .= ", bill_date = NOW()";
115     } else {
116       $billset .= ", billed = 0";
117     }
118   }
119   if ($bill_process >= 0) {
120     $claimset .= ", bill_process = '$bill_process'";
121     $billset  .= ", bill_process = '$bill_process'";
122   }
123   if ($process_file) {
124     $claimset .= ", process_file = '$process_file', process_time = NOW()";
125     $billset  .= ", process_file = '$process_file', process_date = NOW()";
126   }
127   if ($target) {
128     $claimset .= ", target = '$target'";
129     $billset  .= ", target = '$target'";
130   }
131   if ($payer_id >= 0) {
132     $claimset .= ", payer_id = '$payer_id', payer_type = '$payer_type'";
133     $billset  .= ", payer_id = '$payer_id'";
134   }
135   if ($partner_id >= 0) {
136     $claimset .= ", x12_partner_id = '$partner_id'";
137     $billset  .= ", x12_partner_id = '$partner_id'";
138   }
140   if ($billset) {
141     $billset = substr($billset, 2);
142     sqlStatement("UPDATE billing SET $billset WHERE " .
143       "encounter = '$encounter_id' AND pid='$patient_id' AND activity = 1");
144   }
146   // If a new claim version is requested, insert its row.
147   //
148   if ($newversion) {
149     /****
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'";
167     ****/
168     $sql = "INSERT INTO claims SET " .
169       "patient_id = '$patient_id', " .
170       "encounter_id = '$encounter_id', " .
171       "bill_time = NOW() $claimset";
172     sqlStatement($sql);
173   }
175   // Otherwise update the existing claim row.
176   //
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'] . "'");
183   }
185   // Whenever a claim is marked billed, update A/R accordingly.
186   //
187   if ($status == 2) {
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'");
193       }
194     }
195     else {
196       $ws = new WSClaim($patient_id, $encounter_id);
197     }
198   }
200   return 1;
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 " .
208     "billed = 1");
209   $count = $row['count'];
210   if (!$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'];
214   }
215   return $count ? true : false;