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