Ubuntu Package modification - Added configuration of max_input_vars setting
[openemr.git] / library / billing.inc
blob504509819c259196cb64872e4df4d35f4f0328bc
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 = ? and pid=? and activity=1 order by code_type, date ASC", array($encounter,$pid) );
20     $all=array();
21     for($iter=0; $row=sqlFetchArray($res); $iter++)
22     {
23         $all[$iter] = $row;
24     }
25     return $all;
28 function addBilling($encounter_id, $code_type, $code, $code_text, $pid,
29   $authorized="0", $provider, $modifier="", $units="", $fee="0.00",
30   $ndc_info='', $justify='', $billed=0, $notecodes='')
32   $sql = "insert into billing (date, encounter, code_type, code, code_text, " .
33     "pid, authorized, user, groupname, activity, billed, provider_id, " .
34     "modifier, units, fee, ndc_info, justify, notecodes) values (" .
35     "NOW(), ?, ?, ?, ?, ?, ?, ?, ?, 1, ?, ?, ?, ?, ?, ?, ?, ?)";
36   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));
39 function authorizeBilling($id, $authorized = "1")
41     sqlQuery("update billing set authorized = '$authorized' where id = '$id'");
44 function deleteBilling($id)
46     sqlStatement("update billing set activity = 0 where id = '$id'");
49 function clearBilling($id)
51     sqlStatement("update billing set justify = '' where id = '$id'");
54 // This function supports the Billing page (billing_process.php), freeb
55 // processing (process_bills.php), and initiation of secondary processing
56 // (sl_eob.inc.php).  It is called in the following situations:
58 // * billing_process.php sets bill_time, bill_process, payer and target on
59 //   queueing a claim for freeb processing.  Create claims row.
60 // * billing_process.php sets claim status to 2, and payer, on marking a
61 //   claim as billed without actually generating any billing.  Create a
62 //   claims row.  In this case bill_process will remain at 0 and process_time
63 //   and process_file will not be set.
64 // * billing_process.php sets bill_process, payer, target and x12 partner
65 //   before calling gen_x12_837.  Create a claims row.
66 // * billing_process.php sets claim status to 2 (billed), bill_process to 2,
67 //   process_time and process_file after calling gen_x12_837.  Claims row
68 //   already exists.
69 // * process_bills.php sets bill_process to 2, process_time, and process_file
70 //   after invoking freeb to process a claim.  Claims row already exists.
71 // * billing_process.php sets claim status to 2 (billed) after creating
72 //   an electronic freeb batch (hcfa-only with recent changes).  Claims
73 //   row already exists.
74 // * EOB posting updates claim status to mark a payer as done.  Claims row
75 //   already exists.
76 // * EOB posting reopens an encounter for billing a secondary payer.  Create
77 //   a claims row.
79 // $newversion should be passed to us to indicate if a new claims row
80 // is to be generated, otherwise one must already exist.  The payer, if
81 // passed in for the latter case, must match the existing claim.
83 // Currently on the billing page the user can select any of the patient's
84 // payers.  That logic will tailor the payer choices to the encounter date.
86 function updateClaim($newversion, $patient_id, $encounter_id, $payer_id=-1, $payer_type=-1,
87   $status=-1, $bill_process=-1, $process_file='', $target='', $partner_id=-1,$crossover=0)
89   if (!$newversion) {
90     $sql = "SELECT * FROM claims WHERE patient_id = '$patient_id' AND " .
91       "encounter_id = '$encounter_id' AND status > 0 AND status < 4 ";
92     if ($payer_id >= 0) $sql .= "AND payer_id = '$payer_id' ";
93     $sql .= "ORDER BY version DESC LIMIT 1";
94     $row = sqlQuery($sql);
95     if (!$row) return 0;
96     if ($payer_id     < 0) $payer_id     = $row['payer_id'];
97     if ($status       < 0) $status       = $row['status'];
98     if ($bill_process < 0) $bill_process = $row['bill_process'];
99     if ($partner_id   < 0) $partner_id   = $row['x12_partner_id'];
100     if (!$process_file   ) $process_file = $row['process_file'];
101     if (!$target         ) $target       = $row['target'];
102   }
104   $claimset = "";
105   $billset = "";
106   if (empty($payer_id) || $payer_id < 0) $payer_id = 0;
108   if ($status==7) {//$status==7 is the claim denial case.
109     $claimset .= ", status = '$status'";
110   }
111   elseif ($status >= 0) {
112     $claimset .= ", status = '$status'";
113     if ($status > 1) {
114       $billset .= ", billed = 1";
115       if ($status == 2) $billset  .= ", bill_date = NOW()";
116     } else {
117       $billset .= ", billed = 0";
118     }
119   }
120   if ($status==7) {//$status==7 is the claim denial case.
121     $billset  .= ", bill_process = '$status'";
122   }
123   elseif ($bill_process >= 0) {
124     $claimset .= ", bill_process = '$bill_process'";
125     $billset  .= ", bill_process = '$bill_process'";
126   }
127   if ($status==7) {//$status==7 is the claim denial case.
128     $claimset  .= ", process_file = '$process_file'";//Denial reason code is stored here
129   }
130   elseif ($process_file) {
131     $claimset .= ", process_file = '$process_file', process_time = NOW()";
132     $billset  .= ", process_file = '$process_file', process_date = NOW()";
133   }
134   if ($target) {
135     $claimset .= ", target = '$target'";
136     $billset  .= ", target = '$target'";
137   }
138   if ($payer_id >= 0) {
139     $claimset .= ", payer_id = '$payer_id', payer_type = '$payer_type'";
140     $billset  .= ", payer_id = '$payer_id'";
141   }
142   if ($partner_id >= 0) {
143     $claimset .= ", x12_partner_id = '$partner_id'";
144     $billset  .= ", x12_partner_id = '$partner_id'";
145   }
147   if ($billset) {
148     $billset = substr($billset, 2);
149     sqlStatement("UPDATE billing SET $billset WHERE " .
150       "encounter = '$encounter_id' AND pid='$patient_id' AND activity = 1");
151   }
153   // If a new claim version is requested, insert its row.
154   //
155   if ($newversion) {
156     /****
157     $payer_id = ($payer_id < 0) ? $row['payer_id'] : $payer_id;
158     $bill_process = ($bill_process < 0) ? $row['bill_process'] : $bill_process;
159     $process_file = ($process_file) ? $row['process_file'] : $process_file;
160     $target = ($target) ? $row['target'] : $target;
161     $partner_id = ($partner_id < 0) ? $row['x12_partner_id'] : $partner_id;
162     $sql = "INSERT INTO claims SET " .
163       "patient_id = '$patient_id', " .
164       "encounter_id = '$encounter_id', " .
165       "bill_time = UNIX_TIMESTAMP(NOW()), " .
166       "payer_id = '$payer_id', " .
167       "status = '$status', " .
168       "payer_type = '" . $row['payer_type'] . "', " .
169       "bill_process = '$bill_process', " .
170       "process_time = '" . $row['process_time'] . "', " .
171       "process_file = '$process_file', " .
172       "target = '$target', " .
173       "x12_partner_id = '$partner_id'";
174     ****/
175     if($crossover<>1)
176     {
177     $sql = "INSERT INTO claims SET " .
178       "patient_id = '$patient_id', " .
179       "encounter_id = '$encounter_id', " .
180       "bill_time = NOW() $claimset";
181       }
182      else
183      {//Claim automatic forward case.
184      
185      $sql = "INSERT INTO claims SET " .
186       "patient_id = '$patient_id', " .
187       "encounter_id = '$encounter_id', " .
188       "bill_time = NOW(), status=$status";
189      
190      }
191     
192     sqlStatement($sql);
193   }
195   // Otherwise update the existing claim row.
196   //
197   else if ($claimset) {
198     $claimset = substr($claimset, 2);
199     sqlStatement("UPDATE claims SET $claimset WHERE " .
200       "patient_id = '$patient_id' AND encounter_id = '$encounter_id' AND " .
201       // "payer_id = '" . $row['payer_id'] . "' AND " .
202       "version = '" . $row['version'] . "'");
203   }
205   // Whenever a claim is marked billed, update A/R accordingly.
206   //
207   if ($status == 2) {
208     if ($GLOBALS['oer_config']['ws_accounting']['enabled'] === 2) {
209       if ($payer_type > 0) {
210         sqlStatement("UPDATE form_encounter SET " .
211           "last_level_billed = '$payer_type' WHERE " .
212           "pid = '$patient_id' AND encounter = '$encounter_id'");
213       }
214     }
215     else {
216       $ws = new WSClaim($patient_id, $encounter_id);
217     }
218   }
220   return 1;
223 // Determine if anything in a visit has been billed.
225 function isEncounterBilled($pid, $encounter) {
226   $row = sqlQuery("SELECT count(*) AS count FROM billing WHERE " .
227     "pid = '$pid' AND encounter = '$encounter' AND activity = 1 AND " .
228     "billed = 1");
229   $count = $row['count'];
230   if (!$count) {
231     $row = sqlQuery("SELECT count(*) AS count FROM drug_sales WHERE " .
232       "pid = '$pid' AND encounter = '$encounter' AND billed = 1");
233     $count = $row['count'];
234   }
235   return $count ? true : false;
238 // Get the co-pay amount that is effective on the given date.
239 // Or if no insurance on that date, return -1.
241 function getCopay($patient_id, $encdate) {
242  $tmp = sqlQuery("SELECT provider, copay FROM insurance_data " .
243    "WHERE pid = '$patient_id' AND type = 'primary' " .
244    "AND date <= '$encdate' ORDER BY date DESC LIMIT 1");
245  if ($tmp['provider']) return sprintf('%01.2f', 0 + $tmp['copay']);
246  return 0;
249 // Get the total co-pay amount paid by the patient for an encounter
250 function getPatientCopay($patient_id, $encounter) {
251         $resMoneyGot = sqlStatement("SELECT sum(pay_amount) as PatientPay FROM ar_activity where ".
252           "pid = ? and encounter = ? and payer_type=0 and account_code='PCP'",
253           array($patient_id,$encounter));
254          //new fees screen copay gives account_code='PCP'
255         $rowMoneyGot = sqlFetchArray($resMoneyGot);
256         $Copay=$rowMoneyGot['PatientPay'];
257         return $Copay*-1;