2 require_once("{$GLOBALS['srcdir']}/sql.inc");
3 function GenerateTheQueryPart()
5 global $query_part,$billstring,$auth;
6 //Search Criteria section.
10 if(isset($_REQUEST['final_this_page_criteria']))
12 foreach($_REQUEST['final_this_page_criteria'] as $criteria_key => $criteria_value)
14 $criteria_value=PrepareSearchItem($criteria_value);
16 //---------------------------------------------------------
17 if(strpos($criteria_value,"billing.billed = '1'")!== false)
19 $billstring .= ' AND '.$criteria_value;
21 elseif(strpos($criteria_value,"billing.billed = '0'")!== false)
23 //3 is an error condition
24 $billstring .= ' AND '."(billing.billed = '0' or (billing.billed = '1' and billing.bill_process = '3'))";
26 elseif(strpos($criteria_value,"billing.billed = '7'")!== false)
28 $billstring .= ' AND '."billing.bill_process = '7'";
30 //---------------------------------------------------------
31 elseif(strpos($criteria_value,"billing.id = 'null'")!== false)
33 $billstring .= ' AND '."billing.id is null";
35 //---------------------------------------------------------
36 elseif(strpos($criteria_value,"billing.id = 'not null'")!== false)
38 $billstring .= ' AND '."billing.id is not null";
40 //---------------------------------------------------------
41 elseif(strpos($criteria_value,"patient_data.fname")!== false)
43 $SplitArray=split(' like ',$criteria_value);
44 $query_part .= " AND ($criteria_value or patient_data.lname like ".$SplitArray[1].")";
46 //---------------------------------------------------------
47 elseif(strpos($criteria_value,"billing.authorized")!== false)
49 $auth = ' AND '.$criteria_value;
51 //---------------------------------------------------------
52 elseif(strpos($criteria_value,"form_encounter.pid")!== false)
53 {//comes like '781,780'
54 $SplitArray=split(" = '",$criteria_value);//comes like 781,780'
55 $SplitArray[1]=substr($SplitArray[1], 0, -1);//comes like 781,780
56 $query_part .= ' AND form_encounter.pid in ('.$SplitArray[1].')';
58 //---------------------------------------------------------
59 elseif(strpos($criteria_value,"form_encounter.encounter")!== false)
60 {//comes like '781,780'
61 $SplitArray=split(" = '",$criteria_value);//comes like 781,780'
62 $SplitArray[1]=substr($SplitArray[1], 0, -1);//comes like 781,780
63 $query_part .= ' AND form_encounter.encounter in ('.$SplitArray[1].')';
65 //---------------------------------------------------------
66 elseif(strpos($criteria_value,"insurance_data.provider = '1'")!== false)
68 $query_part .= ' AND '."insurance_data.provider > '0' and insurance_data.date <= form_encounter.date";
70 elseif(strpos($criteria_value,"insurance_data.provider = '0'")!== false)
72 $query_part .= ' AND '."(insurance_data.provider = '0' or insurance_data.date > form_encounter.date)";
74 //---------------------------------------------------------
77 $query_part .= ' AND '.$criteria_value;
82 //date must be in nice format (e.g. 2002-07-11)
83 function getBillsBetween( $code_type,
84 $cols = "id,date,pid,code_type,code,user,authorized,x12_partner_id")
86 GenerateTheQueryPart();
87 global $query_part,$billstring,$auth;
88 // Selecting by the date in the billing table is wrong, because that is
89 // just the data entry date; instead we want to go by the encounter date
90 // which is the date in the form_encounter table.
92 $sql = "SELECT distinct form_encounter.date AS enc_date, form_encounter.pid AS enc_pid, " .
93 "form_encounter.encounter AS enc_encounter, form_encounter.provider_id AS enc_provider_id, billing.* " .
94 "FROM form_encounter " .
95 "LEFT OUTER JOIN billing ON " .
96 "billing.encounter = form_encounter.encounter AND " .
97 "billing.pid = form_encounter.pid AND " .
98 "billing.code_type LIKE '$code_type' AND " .
99 "billing.activity = 1 " .
100 "LEFT OUTER JOIN patient_data on patient_data.pid = form_encounter.pid " .
101 "LEFT OUTER JOIN claims on claims.patient_id = form_encounter.pid and claims.encounter_id = form_encounter.encounter " .
102 "LEFT OUTER JOIN insurance_data on insurance_data.pid = form_encounter.pid and insurance_data.type = 'primary' ".
103 "WHERE 1=1 $query_part " . " $auth " ." $billstring " .
104 "ORDER BY form_encounter.encounter, form_encounter.pid, billing.code_type, billing.code ASC";
106 $res = sqlStatement($sql);
108 for($iter=0; $row=sqlFetchArray($res); $iter++)
115 function getBillsListBetween( $code_type,
116 $cols = "billing.id, form_encounter.date, billing.pid, billing.code_type, billing.code, billing.user")
118 GenerateTheQueryPart();
119 global $query_part,$billstring,$auth;
120 // See above comment in getBillsBetween().
122 $sql = "select distinct $cols " .
123 "from form_encounter, billing, patient_data, claims, insurance_data where " .
124 "billing.encounter = form_encounter.encounter and " .
125 "billing.pid = form_encounter.pid and " .
126 "patient_data.pid = form_encounter.pid and " .
127 "claims.patient_id = form_encounter.pid and claims.encounter_id = form_encounter.encounter and ".
128 "insurance_data.pid = form_encounter.pid and insurance_data.type = 'primary' ".
130 $billstring . $query_part . " and " .
131 "billing.code_type like '$code_type' and " .
132 "billing.activity = 1 " .
133 "order by billing.pid, billing.date ASC";
135 $res = sqlStatement($sql);
137 for($iter=0; $row=sqlFetchArray($res); $iter++)
139 $string .= $row{"id"}.",";
141 $string = substr($string,0,strlen($string)-1);
146 function billCodesList($list,$skip = "()") {
151 sqlStatement("update billing set billed=1 where id in $list");
153 sqlStatement("update billing set billed=1 where id in $list and id not in $skip");
158 function ReturnOFXSql()
160 GenerateTheQueryPart();
161 global $query_part,$billstring,$auth;
163 $sql = "SELECT distinct billing.*, concat(patient_data.fname, ' ', patient_data.lname) as name from billing "
164 . "join patient_data on patient_data.pid = billing.pid "
165 . "join form_encounter on "
166 . "billing.encounter = form_encounter.encounter AND "
167 . "billing.pid = form_encounter.pid "
168 . "join claims on claims.patient_id = form_encounter.pid and claims.encounter_id = form_encounter.encounter "
169 . "join insurance_data on insurance_data.pid = form_encounter.pid and insurance_data.type = 'primary' "
170 . "where billed = '1' "
172 . "$billstring $query_part "
173 . "order by billing.pid,billing.encounter";