Fully responsive globals.php with vertical menu (#2460)
[openemr.git] / library / billrep.inc
blob80ff522f1a31cf06b0538abdd3b0cc11a7265a14
1 <?php
2 require_once(dirname(__FILE__) . "/../interface/reports/report.inc.php");
4 function GenerateTheQueryPart()
6     global $query_part,$query_part2,$billstring,$auth;
7     //Search Criteria section.
8     $billstring='';
9     $auth='';
10     $query_part='';
11     $query_part2='';
12     if (isset($_REQUEST['final_this_page_criteria'])) {
13         foreach ($_REQUEST['final_this_page_criteria'] as $criteria_key => $criteria_value) {
14             $criteria_value=PrepareSearchItem($criteria_value); // this escapes for sql
15             $SplitArray=array();
16           //---------------------------------------------------------
17             if (strpos($criteria_value, "billing.billed = '1'")!== false) {
18                 $billstring .= ' AND '.$criteria_value;
19             } elseif (strpos($criteria_value, "billing.billed = '0'")!== false) {
20                 //3 is an error condition
21                 $billstring .= ' AND '."(billing.billed is null or billing.billed = '0' or (billing.billed = '1' and billing.bill_process = '3'))";
22             } elseif (strpos($criteria_value, "billing.billed = '7'")!== false) {
23                 $billstring .= ' AND '."billing.bill_process = '7'";
24             } //---------------------------------------------------------
25             elseif (strpos($criteria_value, "billing.id = 'null'")!== false) {
26                 $billstring .= ' AND '."billing.id is null";
27             } //---------------------------------------------------------
28             elseif (strpos($criteria_value, "billing.id = 'not null'")!== false) {
29                 $billstring .= ' AND '."billing.id is not null";
30             } //---------------------------------------------------------
31             elseif (strpos($criteria_value, "patient_data.fname")!== false) {
32                 $SplitArray=explode(' like ', $criteria_value);
33                 $query_part .= " AND ($criteria_value or patient_data.lname like ".$SplitArray[1].")";
34             } //---------------------------------------------------------
35             elseif (strpos($criteria_value, "billing.authorized")!== false) {
36                 $auth = ' AND '.$criteria_value;
37             } //---------------------------------------------------------
38             elseif (strpos($criteria_value, "form_encounter.pid")!== false) {//comes like '781,780'
39                 $SplitArray=explode(" = '", $criteria_value);//comes like 781,780'
40                 $SplitArray[1]=substr($SplitArray[1], 0, -1);//comes like 781,780
41                 $query_part .= ' AND form_encounter.pid in ('.$SplitArray[1].')';
42                 $query_part2 .= ' AND pid in ('.$SplitArray[1].')';
43             } //---------------------------------------------------------
44             elseif (strpos($criteria_value, "form_encounter.encounter")!== false) {//comes like '781,780'
45                 $SplitArray=explode(" = '", $criteria_value);//comes like 781,780'
46                 $SplitArray[1]=substr($SplitArray[1], 0, -1);//comes like 781,780
47                 $query_part .= ' AND form_encounter.encounter in ('.$SplitArray[1].')';
48             } //---------------------------------------------------------
49             elseif (strpos($criteria_value, "insurance_data.provider = '1'")!== false) {
50                 $query_part .= ' AND '."insurance_data.provider > '0' and insurance_data.date <= form_encounter.date";
51             } elseif (strpos($criteria_value, "insurance_data.provider = '0'")!== false) {
52                 $query_part .= ' AND '."(insurance_data.provider = '0' or insurance_data.date > form_encounter.date)";
53             } //---------------------------------------------------------
54             else {
55                 $query_part .= ' AND '.$criteria_value;
56             }
57         }
58     }
60     //date must be in nice format (e.g. 2002-07-11)
61 function getBillsBetween(
62     $code_type,
63     $cols = "id,date,pid,code_type,code,user,authorized,x12_partner_id"
64 ) {
66     GenerateTheQueryPart();
67     global $query_part,$billstring,$auth;
68     // Selecting by the date in the billing table is wrong, because that is
69     // just the data entry date; instead we want to go by the encounter date
70     // which is the date in the form_encounter table.
71     //
72     $sql = "SELECT distinct form_encounter.date AS enc_date, form_encounter.pid AS enc_pid, " .
73     "form_encounter.encounter AS enc_encounter, form_encounter.provider_id AS enc_provider_id, billing.* " .
74     "FROM form_encounter " .
75     "LEFT OUTER JOIN billing ON " .
76     "billing.encounter = form_encounter.encounter AND " .
77     "billing.pid = form_encounter.pid AND " .
78     "billing.code_type LIKE ? AND " .
79     "billing.activity = 1 " .
80     "LEFT OUTER JOIN patient_data on patient_data.pid = form_encounter.pid " .
81     "LEFT OUTER JOIN claims on claims.patient_id = form_encounter.pid and claims.encounter_id = form_encounter.encounter " .
82     "LEFT OUTER JOIN insurance_data on insurance_data.pid = form_encounter.pid and insurance_data.type = 'primary' ".
83     "WHERE 1=1 $query_part  " . " $auth " ." $billstring " .
84     "ORDER BY form_encounter.encounter, form_encounter.pid, billing.code_type, billing.code ASC";
85     //echo $sql;
86     $res = sqlStatement($sql, array($code_type));
87     $all = false;
88     for ($iter=0; $row=sqlFetchArray($res); $iter++) {
89         $all[$iter] = $row;
90     }
92     return $all;
94 function getBillsBetweenReport(
95     $code_type,
96     $cols = "id,date,pid,code_type,code,user,authorized,x12_partner_id"
97 ) {
99     GenerateTheQueryPart();
100     global $query_part,$query_part2,$billstring,$auth;
101     // Selecting by the date in the billing table is wrong, because that is
102     // just the data entry date; instead we want to go by the encounter date
103     // which is the date in the form_encounter table.
104     //
105     $sql = "SELECT distinct form_encounter.date AS enc_date, form_encounter.pid AS enc_pid, " .
106     "form_encounter.encounter AS enc_encounter, form_encounter.provider_id AS enc_provider_id, billing.* " .
107     "FROM form_encounter " .
108     "LEFT OUTER JOIN billing ON " .
109     "billing.encounter = form_encounter.encounter AND " .
110     "billing.pid = form_encounter.pid AND " .
111     "billing.code_type LIKE ? AND " .
112     "billing.activity = 1 " .
113     "LEFT OUTER JOIN patient_data on patient_data.pid = form_encounter.pid " .
114     "LEFT OUTER JOIN claims on claims.patient_id = form_encounter.pid and claims.encounter_id = form_encounter.encounter " .
115     "LEFT OUTER JOIN insurance_data on insurance_data.pid = form_encounter.pid and insurance_data.type = 'primary' ".
116     "WHERE 1=1 $query_part  " . " $auth " ." $billstring " .
117     "ORDER BY form_encounter.encounter, form_encounter.pid, billing.code_type, billing.code ASC";
118     //echo $sql;
119     $res = sqlStatement($sql, array($code_type));
120     $all = false;
121     for ($iter=0; $row=sqlFetchArray($res); $iter++) {
122         $all[$iter] = $row;
123     }
125     $query = sqlStatement("SELECT pid, 'COPAY' AS code_type, pay_amount AS code, date(post_time) AS date ".
126     "FROM ar_activity where 1=1 $query_part2 and payer_type=0 and account_code='PCP'");
127     //new fees screen copay gives account_code='PCP' openemr payment screen copay gives code='CO-PAY'
128     for ($iter; $row=sqlFetchArray($query); $iter++) {
129         $all[$iter] = $row;
130     }
132     return $all;
134 function getBillsListBetween(
135     $code_type,
136     $cols = "billing.id, form_encounter.date, billing.pid, billing.code_type, billing.code, billing.user"
137 ) {
139     GenerateTheQueryPart();
140     global $query_part,$billstring,$auth;
141     // See above comment in getBillsBetween().
142     //
143     $sql = "select distinct $cols " .
144     "from form_encounter, billing, patient_data, claims, insurance_data where " .
145     "billing.encounter = form_encounter.encounter and " .
146     "billing.pid = form_encounter.pid and " .
147     "patient_data.pid = form_encounter.pid and " .
148     "claims.patient_id = form_encounter.pid and claims.encounter_id = form_encounter.encounter and ".
149     "insurance_data.pid = form_encounter.pid and insurance_data.type = 'primary' ".
150     $auth  .
151     $billstring . $query_part . " and " .
152     "billing.code_type like ? and " .
153     "billing.activity = 1 " .
154     "order by billing.pid, billing.date ASC";
156     $res = sqlStatement($sql, array($code_type));
157     $array = array();
158     for ($iter=0; $row=sqlFetchArray($res); $iter++) {
159         array_push($array, $row{"id"});
160     }
162     return $array;
165 function billCodesList($list, $skip = [])
167     if (empty($list)) {
168         return;
169     }
171     $sqlBindArray = array_diff($list, $skip);
172     if (empty($sqlBindArray)) {
173         return;
174     }
176     $in = str_repeat('?,', count($sqlBindArray) - 1) . '?';
177     sqlStatement("update billing set billed=1 where id in ($in)", $sqlBindArray);
179     return;
182 function ReturnOFXSql()
184     GenerateTheQueryPart();
185     global $query_part,$billstring,$auth;
187     $sql = "SELECT distinct billing.*, concat(patient_data.fname, ' ', patient_data.lname) as name from billing "
188     . "join patient_data on patient_data.pid = billing.pid "
189     . "join form_encounter on "
190     . "billing.encounter = form_encounter.encounter AND "
191     . "billing.pid = form_encounter.pid "
192     . "join claims on claims.patient_id = form_encounter.pid and claims.encounter_id = form_encounter.encounter "
193     . "join insurance_data on insurance_data.pid = form_encounter.pid and insurance_data.type = 'primary' "
194     . "where billed = '1' "
195     . "$auth "
196     . "$billstring  $query_part  "
197     . "order by billing.pid,billing.encounter";
199     return $sql;