2 require_once(dirname(__FILE__) . "/../interface/reports/report.inc.php");
3 function GenerateTheQueryPart()
5 global $query_part,$query_part2,$billstring,$auth;
6 //Search Criteria section.
11 if(isset($_REQUEST['final_this_page_criteria']))
13 foreach($_REQUEST['final_this_page_criteria'] as $criteria_key => $criteria_value)
15 $criteria_value=PrepareSearchItem($criteria_value); // this escapes for sql
17 //---------------------------------------------------------
18 if(strpos($criteria_value,"billing.billed = '1'")!== false)
20 $billstring .= ' AND '.$criteria_value;
22 elseif(strpos($criteria_value,"billing.billed = '0'")!== false)
24 //3 is an error condition
25 $billstring .= ' AND '."(billing.billed is null or billing.billed = '0' or (billing.billed = '1' and billing.bill_process = '3'))";
27 elseif(strpos($criteria_value,"billing.billed = '7'")!== false)
29 $billstring .= ' AND '."billing.bill_process = '7'";
31 //---------------------------------------------------------
32 elseif(strpos($criteria_value,"billing.id = 'null'")!== false)
34 $billstring .= ' AND '."billing.id is null";
36 //---------------------------------------------------------
37 elseif(strpos($criteria_value,"billing.id = 'not null'")!== false)
39 $billstring .= ' AND '."billing.id is not null";
41 //---------------------------------------------------------
42 elseif(strpos($criteria_value,"patient_data.fname")!== false)
44 $SplitArray=explode(' like ',$criteria_value);
45 $query_part .= " AND ($criteria_value or patient_data.lname like ".$SplitArray[1].")";
47 //---------------------------------------------------------
48 elseif(strpos($criteria_value,"billing.authorized")!== false)
50 $auth = ' AND '.$criteria_value;
52 //---------------------------------------------------------
53 elseif(strpos($criteria_value,"form_encounter.pid")!== false)
54 {//comes like '781,780'
55 $SplitArray=explode(" = '",$criteria_value);//comes like 781,780'
56 $SplitArray[1]=substr($SplitArray[1], 0, -1);//comes like 781,780
57 $query_part .= ' AND form_encounter.pid in ('.$SplitArray[1].')';
58 $query_part2 .= ' AND pid in ('.$SplitArray[1].')';
60 //---------------------------------------------------------
61 elseif(strpos($criteria_value,"form_encounter.encounter")!== false)
62 {//comes like '781,780'
63 $SplitArray=explode(" = '",$criteria_value);//comes like 781,780'
64 $SplitArray[1]=substr($SplitArray[1], 0, -1);//comes like 781,780
65 $query_part .= ' AND form_encounter.encounter in ('.$SplitArray[1].')';
67 //---------------------------------------------------------
68 elseif(strpos($criteria_value,"insurance_data.provider = '1'")!== false)
70 $query_part .= ' AND '."insurance_data.provider > '0' and insurance_data.date <= form_encounter.date";
72 elseif(strpos($criteria_value,"insurance_data.provider = '0'")!== false)
74 $query_part .= ' AND '."(insurance_data.provider = '0' or insurance_data.date > form_encounter.date)";
76 //---------------------------------------------------------
79 $query_part .= ' AND '.$criteria_value;
84 //date must be in nice format (e.g. 2002-07-11)
85 function getBillsBetween( $code_type,
86 $cols = "id,date,pid,code_type,code,user,authorized,x12_partner_id")
88 GenerateTheQueryPart();
89 global $query_part,$billstring,$auth;
90 // Selecting by the date in the billing table is wrong, because that is
91 // just the data entry date; instead we want to go by the encounter date
92 // which is the date in the form_encounter table.
94 $sql = "SELECT distinct form_encounter.date AS enc_date, form_encounter.pid AS enc_pid, " .
95 "form_encounter.encounter AS enc_encounter, form_encounter.provider_id AS enc_provider_id, billing.* " .
96 "FROM form_encounter " .
97 "LEFT OUTER JOIN billing ON " .
98 "billing.encounter = form_encounter.encounter AND " .
99 "billing.pid = form_encounter.pid AND " .
100 "billing.code_type LIKE ? AND " .
101 "billing.activity = 1 " .
102 "LEFT OUTER JOIN patient_data on patient_data.pid = form_encounter.pid " .
103 "LEFT OUTER JOIN claims on claims.patient_id = form_encounter.pid and claims.encounter_id = form_encounter.encounter " .
104 "LEFT OUTER JOIN insurance_data on insurance_data.pid = form_encounter.pid and insurance_data.type = 'primary' ".
105 "WHERE 1=1 $query_part " . " $auth " ." $billstring " .
106 "ORDER BY form_encounter.encounter, form_encounter.pid, billing.code_type, billing.code ASC";
108 $res = sqlStatement($sql,array($code_type));
110 for($iter=0; $row=sqlFetchArray($res); $iter++)
117 function getBillsBetweenReport( $code_type,
118 $cols = "id,date,pid,code_type,code,user,authorized,x12_partner_id")
120 GenerateTheQueryPart();
121 global $query_part,$query_part2,$billstring,$auth;
122 // Selecting by the date in the billing table is wrong, because that is
123 // just the data entry date; instead we want to go by the encounter date
124 // which is the date in the form_encounter table.
126 $sql = "SELECT distinct form_encounter.date AS enc_date, form_encounter.pid AS enc_pid, " .
127 "form_encounter.encounter AS enc_encounter, form_encounter.provider_id AS enc_provider_id, billing.* " .
128 "FROM form_encounter " .
129 "LEFT OUTER JOIN billing ON " .
130 "billing.encounter = form_encounter.encounter AND " .
131 "billing.pid = form_encounter.pid AND " .
132 "billing.code_type LIKE ? AND " .
133 "billing.activity = 1 " .
134 "LEFT OUTER JOIN patient_data on patient_data.pid = form_encounter.pid " .
135 "LEFT OUTER JOIN claims on claims.patient_id = form_encounter.pid and claims.encounter_id = form_encounter.encounter " .
136 "LEFT OUTER JOIN insurance_data on insurance_data.pid = form_encounter.pid and insurance_data.type = 'primary' ".
137 "WHERE 1=1 $query_part " . " $auth " ." $billstring " .
138 "ORDER BY form_encounter.encounter, form_encounter.pid, billing.code_type, billing.code ASC";
140 $res = sqlStatement($sql,array($code_type));
142 for($iter=0; $row=sqlFetchArray($res); $iter++)
147 $query = sqlStatement("SELECT pid, 'COPAY' AS code_type, pay_amount AS code, date(post_time) AS date ".
148 "FROM ar_activity where 1=1 $query_part2 and payer_type=0 and account_code='PCP'");
149 //new fees screen copay gives account_code='PCP' openemr payment screen copay gives code='CO-PAY'
150 for($iter; $row=sqlFetchArray($query); $iter++)
157 function getBillsListBetween( $code_type,
158 $cols = "billing.id, form_encounter.date, billing.pid, billing.code_type, billing.code, billing.user")
160 GenerateTheQueryPart();
161 global $query_part,$billstring,$auth;
162 // See above comment in getBillsBetween().
164 $sql = "select distinct $cols " .
165 "from form_encounter, billing, patient_data, claims, insurance_data where " .
166 "billing.encounter = form_encounter.encounter and " .
167 "billing.pid = form_encounter.pid and " .
168 "patient_data.pid = form_encounter.pid and " .
169 "claims.patient_id = form_encounter.pid and claims.encounter_id = form_encounter.encounter and ".
170 "insurance_data.pid = form_encounter.pid and insurance_data.type = 'primary' ".
172 $billstring . $query_part . " and " .
173 "billing.code_type like ? and " .
174 "billing.activity = 1 " .
175 "order by billing.pid, billing.date ASC";
177 $res = sqlStatement($sql,array($code_type));
179 for($iter=0; $row=sqlFetchArray($res); $iter++)
181 $string .= $row{"id"}.",";
183 $string = substr($string,0,strlen($string)-1);
188 function billCodesList($list,$skip = "()") {
193 sqlStatement("update billing set billed=1 where id in $list");
195 sqlStatement("update billing set billed=1 where id in $list and id not in $skip");
200 function ReturnOFXSql()
202 GenerateTheQueryPart();
203 global $query_part,$billstring,$auth;
205 $sql = "SELECT distinct billing.*, concat(patient_data.fname, ' ', patient_data.lname) as name from billing "
206 . "join patient_data on patient_data.pid = billing.pid "
207 . "join form_encounter on "
208 . "billing.encounter = form_encounter.encounter AND "
209 . "billing.pid = form_encounter.pid "
210 . "join claims on claims.patient_id = form_encounter.pid and claims.encounter_id = form_encounter.encounter "
211 . "join insurance_data on insurance_data.pid = form_encounter.pid and insurance_data.type = 'primary' "
212 . "where billed = '1' "
214 . "$billstring $query_part "
215 . "order by billing.pid,billing.encounter";