minor bug fix
[openemr.git] / library / billrep.inc
blob3739b109365698ff77387a39ab778eb887e25036
1 <?php
2     require_once("{$GLOBALS['srcdir']}/sql.inc");
3     require_once(dirname(__FILE__) . "/../interface/reports/report.inc.php");
4     function GenerateTheQueryPart()
5      {
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          {
14             foreach($_REQUEST['final_this_page_criteria'] as $criteria_key => $criteria_value)
15              {
16               $criteria_value=PrepareSearchItem($criteria_value); // this escapes for sql
17               $SplitArray=array();
18               //---------------------------------------------------------
19               if(strpos($criteria_value,"billing.billed = '1'")!== false)
20                {
21                 $billstring .= ' AND '.$criteria_value;
22                }
23               elseif(strpos($criteria_value,"billing.billed = '0'")!== false)
24                {
25                 //3 is an error condition
26                 $billstring .= ' AND '."(billing.billed = '0' or (billing.billed = '1' and billing.bill_process = '3'))";
27                }
28               elseif(strpos($criteria_value,"billing.billed = '7'")!== false)
29                {
30                 $billstring .= ' AND '."billing.bill_process = '7'";
31                }
32               //---------------------------------------------------------
33               elseif(strpos($criteria_value,"billing.id = 'null'")!== false)
34                {
35                 $billstring .= ' AND '."billing.id is null";
36                }
37               //---------------------------------------------------------
38               elseif(strpos($criteria_value,"billing.id = 'not null'")!== false)
39                {
40                 $billstring .= ' AND '."billing.id is not null";
41                }
42               //---------------------------------------------------------
43               elseif(strpos($criteria_value,"patient_data.fname")!== false)
44                {
45                 $SplitArray=split(' like ',$criteria_value);
46                 $query_part .= " AND ($criteria_value or patient_data.lname like ".$SplitArray[1].")";
47                }
48               //---------------------------------------------------------
49               elseif(strpos($criteria_value,"billing.authorized")!== false)
50                {
51                 $auth = ' AND '.$criteria_value;
52                }
53               //---------------------------------------------------------
54               elseif(strpos($criteria_value,"form_encounter.pid")!== false)
55                {//comes like '781,780'
56                 $SplitArray=split(" = '",$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].')';
60                }
61               //---------------------------------------------------------
62               elseif(strpos($criteria_value,"form_encounter.encounter")!== false)
63                {//comes like '781,780'
64                 $SplitArray=split(" = '",$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].')';
67                }
68               //---------------------------------------------------------
69               elseif(strpos($criteria_value,"insurance_data.provider = '1'")!== false)
70                {
71                 $query_part .= ' AND '."insurance_data.provider > '0' and insurance_data.date <= form_encounter.date";
72                }
73               elseif(strpos($criteria_value,"insurance_data.provider = '0'")!== false)
74                {
75                 $query_part .= ' AND '."(insurance_data.provider = '0' or insurance_data.date > form_encounter.date)";
76                }
77               //---------------------------------------------------------
78               else
79                {
80                 $query_part .= ' AND '.$criteria_value;
81                }
82               }
83          }
84      }
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")
88     {
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.
94         //
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";
108             //echo $sql;
109         $res = sqlStatement($sql,array($code_type));
111         for($iter=0; $row=sqlFetchArray($res); $iter++)
112         {
113             $all[$iter] = $row;
114         }
116         return $all;
117     }
118     function getBillsBetweenReport( $code_type,
119         $cols = "id,date,pid,code_type,code,user,authorized,x12_partner_id")
120     {
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.
126         //
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";
140             //echo $sql;
141         $res = sqlStatement($sql,array($code_type));
143         for($iter=0; $row=sqlFetchArray($res); $iter++)
144         {
145             $all[$iter] = $row;
146         }
147         
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++)
152         {
153             $all[$iter] = $row;
154         }
156         return $all;
157     }
158     function getBillsListBetween( $code_type,
159         $cols = "billing.id, form_encounter.date, billing.pid, billing.code_type, billing.code, billing.user")
160     {
161         GenerateTheQueryPart();
162         global $query_part,$billstring,$auth;
163         // See above comment in getBillsBetween().
164         //
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' ".
172             $auth  .
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));
179         $string = "( ";
180         for($iter=0; $row=sqlFetchArray($res); $iter++)
181         {
182             $string .= $row{"id"}.",";
183         }
184         $string = substr($string,0,strlen($string)-1);
185         $string .= ")";
186         return $string;
187     }
189     function billCodesList($list,$skip = "()") {
190         if ($list == "()")
191             return;
193         if ($skip == "()")
194             sqlStatement("update billing set billed=1 where id in $list");
195         else
196             sqlStatement("update billing set billed=1 where id in $list and id not in $skip");
198         return;
199     }
200     
201     function ReturnOFXSql()
202      {
203         GenerateTheQueryPart();
204         global $query_part,$billstring,$auth;
205         
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' "
214           . "$auth " 
215           . "$billstring  $query_part  " 
216           . "order by billing.pid,billing.encounter";
217           
218          return $sql;
219      }