2 require_once("{$GLOBALS['srcdir']}/sql.inc");
3 require_once(dirname(__FILE__) . "/../interface/reports/report.inc.php");
4 function GenerateTheQueryPart()
6 global $query_part,$query_part2,$billstring,$auth;
7 //Search Criteria section.
12 if(isset($_REQUEST['final_this_page_criteria']))
14 foreach($_REQUEST['final_this_page_criteria'] as $criteria_key => $criteria_value)
16 $criteria_value=PrepareSearchItem($criteria_value); // this escapes for sql
18 //---------------------------------------------------------
19 if(strpos($criteria_value,"billing.billed = '1'")!== false)
21 $billstring .= ' AND '.$criteria_value;
23 elseif(strpos($criteria_value,"billing.billed = '0'")!== false)
25 //3 is an error condition
26 $billstring .= ' AND '."(billing.billed is null or billing.billed = '0' or (billing.billed = '1' and billing.bill_process = '3'))";
28 elseif(strpos($criteria_value,"billing.billed = '7'")!== false)
30 $billstring .= ' AND '."billing.bill_process = '7'";
32 //---------------------------------------------------------
33 elseif(strpos($criteria_value,"billing.id = 'null'")!== false)
35 $billstring .= ' AND '."billing.id is null";
37 //---------------------------------------------------------
38 elseif(strpos($criteria_value,"billing.id = 'not null'")!== false)
40 $billstring .= ' AND '."billing.id is not null";
42 //---------------------------------------------------------
43 elseif(strpos($criteria_value,"patient_data.fname")!== false)
45 $SplitArray=explode(' like ',$criteria_value);
46 $query_part .= " AND ($criteria_value or patient_data.lname like ".$SplitArray[1].")";
48 //---------------------------------------------------------
49 elseif(strpos($criteria_value,"billing.authorized")!== false)
51 $auth = ' AND '.$criteria_value;
53 //---------------------------------------------------------
54 elseif(strpos($criteria_value,"form_encounter.pid")!== false)
55 {//comes like '781,780'
56 $SplitArray=explode(" = '",$criteria_value);//comes like 781,780'
57 $SplitArray[1]=substr($SplitArray[1], 0, -1);//comes like 781,780
58 $query_part .= ' AND form_encounter.pid in ('.$SplitArray[1].')';
59 $query_part2 .= ' AND pid in ('.$SplitArray[1].')';
61 //---------------------------------------------------------
62 elseif(strpos($criteria_value,"form_encounter.encounter")!== false)
63 {//comes like '781,780'
64 $SplitArray=explode(" = '",$criteria_value);//comes like 781,780'
65 $SplitArray[1]=substr($SplitArray[1], 0, -1);//comes like 781,780
66 $query_part .= ' AND form_encounter.encounter in ('.$SplitArray[1].')';
68 //---------------------------------------------------------
69 elseif(strpos($criteria_value,"insurance_data.provider = '1'")!== false)
71 $query_part .= ' AND '."insurance_data.provider > '0' and insurance_data.date <= form_encounter.date";
73 elseif(strpos($criteria_value,"insurance_data.provider = '0'")!== false)
75 $query_part .= ' AND '."(insurance_data.provider = '0' or insurance_data.date > form_encounter.date)";
77 //---------------------------------------------------------
80 $query_part .= ' AND '.$criteria_value;
85 //date must be in nice format (e.g. 2002-07-11)
86 function getBillsBetween( $code_type,
87 $cols = "id,date,pid,code_type,code,user,authorized,x12_partner_id")
89 GenerateTheQueryPart();
90 global $query_part,$billstring,$auth;
91 // Selecting by the date in the billing table is wrong, because that is
92 // just the data entry date; instead we want to go by the encounter date
93 // which is the date in the form_encounter table.
95 $sql = "SELECT distinct form_encounter.date AS enc_date, form_encounter.pid AS enc_pid, " .
96 "form_encounter.encounter AS enc_encounter, form_encounter.provider_id AS enc_provider_id, billing.* " .
97 "FROM form_encounter " .
98 "LEFT OUTER JOIN billing ON " .
99 "billing.encounter = form_encounter.encounter AND " .
100 "billing.pid = form_encounter.pid AND " .
101 "billing.code_type LIKE ? AND " .
102 "billing.activity = 1 " .
103 "LEFT OUTER JOIN patient_data on patient_data.pid = form_encounter.pid " .
104 "LEFT OUTER JOIN claims on claims.patient_id = form_encounter.pid and claims.encounter_id = form_encounter.encounter " .
105 "LEFT OUTER JOIN insurance_data on insurance_data.pid = form_encounter.pid and insurance_data.type = 'primary' ".
106 "WHERE 1=1 $query_part " . " $auth " ." $billstring " .
107 "ORDER BY form_encounter.encounter, form_encounter.pid, billing.code_type, billing.code ASC";
109 $res = sqlStatement($sql,array($code_type));
111 for($iter=0; $row=sqlFetchArray($res); $iter++)
118 function getBillsBetweenReport( $code_type,
119 $cols = "id,date,pid,code_type,code,user,authorized,x12_partner_id")
121 GenerateTheQueryPart();
122 global $query_part,$query_part2,$billstring,$auth;
123 // Selecting by the date in the billing table is wrong, because that is
124 // just the data entry date; instead we want to go by the encounter date
125 // which is the date in the form_encounter table.
127 $sql = "SELECT distinct form_encounter.date AS enc_date, form_encounter.pid AS enc_pid, " .
128 "form_encounter.encounter AS enc_encounter, form_encounter.provider_id AS enc_provider_id, billing.* " .
129 "FROM form_encounter " .
130 "LEFT OUTER JOIN billing ON " .
131 "billing.encounter = form_encounter.encounter AND " .
132 "billing.pid = form_encounter.pid AND " .
133 "billing.code_type LIKE ? AND " .
134 "billing.activity = 1 " .
135 "LEFT OUTER JOIN patient_data on patient_data.pid = form_encounter.pid " .
136 "LEFT OUTER JOIN claims on claims.patient_id = form_encounter.pid and claims.encounter_id = form_encounter.encounter " .
137 "LEFT OUTER JOIN insurance_data on insurance_data.pid = form_encounter.pid and insurance_data.type = 'primary' ".
138 "WHERE 1=1 $query_part " . " $auth " ." $billstring " .
139 "ORDER BY form_encounter.encounter, form_encounter.pid, billing.code_type, billing.code ASC";
141 $res = sqlStatement($sql,array($code_type));
143 for($iter=0; $row=sqlFetchArray($res); $iter++)
148 $query = sqlStatement("SELECT pid, 'COPAY' AS code_type, pay_amount AS code, date(post_time) AS date ".
149 "FROM ar_activity where 1=1 $query_part2 and payer_type=0 and account_code='PCP'");
150 //new fees screen copay gives account_code='PCP' openemr payment screen copay gives code='CO-PAY'
151 for($iter; $row=sqlFetchArray($query); $iter++)
158 function getBillsListBetween( $code_type,
159 $cols = "billing.id, form_encounter.date, billing.pid, billing.code_type, billing.code, billing.user")
161 GenerateTheQueryPart();
162 global $query_part,$billstring,$auth;
163 // See above comment in getBillsBetween().
165 $sql = "select distinct $cols " .
166 "from form_encounter, billing, patient_data, claims, insurance_data where " .
167 "billing.encounter = form_encounter.encounter and " .
168 "billing.pid = form_encounter.pid and " .
169 "patient_data.pid = form_encounter.pid and " .
170 "claims.patient_id = form_encounter.pid and claims.encounter_id = form_encounter.encounter and ".
171 "insurance_data.pid = form_encounter.pid and insurance_data.type = 'primary' ".
173 $billstring . $query_part . " and " .
174 "billing.code_type like ? and " .
175 "billing.activity = 1 " .
176 "order by billing.pid, billing.date ASC";
178 $res = sqlStatement($sql,array($code_type));
180 for($iter=0; $row=sqlFetchArray($res); $iter++)
182 $string .= $row{"id"}.",";
184 $string = substr($string,0,strlen($string)-1);
189 function billCodesList($list,$skip = "()") {
194 sqlStatement("update billing set billed=1 where id in $list");
196 sqlStatement("update billing set billed=1 where id in $list and id not in $skip");
201 function ReturnOFXSql()
203 GenerateTheQueryPart();
204 global $query_part,$billstring,$auth;
206 $sql = "SELECT distinct billing.*, concat(patient_data.fname, ' ', patient_data.lname) as name from billing "
207 . "join patient_data on patient_data.pid = billing.pid "
208 . "join form_encounter on "
209 . "billing.encounter = form_encounter.encounter AND "
210 . "billing.pid = form_encounter.pid "
211 . "join claims on claims.patient_id = form_encounter.pid and claims.encounter_id = form_encounter.encounter "
212 . "join insurance_data on insurance_data.pid = form_encounter.pid and insurance_data.type = 'primary' "
213 . "where billed = '1' "
215 . "$billstring $query_part "
216 . "order by billing.pid,billing.encounter";