minor improvement to tabs style
[openemr.git] / library / billrep.inc
blobb95a462540ea9d14682569b24120c5a10e951788
1 <?php
2     require_once(dirname(__FILE__) . "/../interface/reports/report.inc.php");
3     function GenerateTheQueryPart()
4      {
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          {
13             foreach($_REQUEST['final_this_page_criteria'] as $criteria_key => $criteria_value)
14              {
15               $criteria_value=PrepareSearchItem($criteria_value); // this escapes for sql
16               $SplitArray=array();
17               //---------------------------------------------------------
18               if(strpos($criteria_value,"billing.billed = '1'")!== false)
19                {
20                 $billstring .= ' AND '.$criteria_value;
21                }
22               elseif(strpos($criteria_value,"billing.billed = '0'")!== false)
23                {
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'))";
26                }
27               elseif(strpos($criteria_value,"billing.billed = '7'")!== false)
28                {
29                 $billstring .= ' AND '."billing.bill_process = '7'";
30                }
31               //---------------------------------------------------------
32               elseif(strpos($criteria_value,"billing.id = 'null'")!== false)
33                {
34                 $billstring .= ' AND '."billing.id is null";
35                }
36               //---------------------------------------------------------
37               elseif(strpos($criteria_value,"billing.id = 'not null'")!== false)
38                {
39                 $billstring .= ' AND '."billing.id is not null";
40                }
41               //---------------------------------------------------------
42               elseif(strpos($criteria_value,"patient_data.fname")!== false)
43                {
44                 $SplitArray=explode(' like ',$criteria_value);
45                 $query_part .= " AND ($criteria_value or patient_data.lname like ".$SplitArray[1].")";
46                }
47               //---------------------------------------------------------
48               elseif(strpos($criteria_value,"billing.authorized")!== false)
49                {
50                 $auth = ' AND '.$criteria_value;
51                }
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].')';
59                }
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].')';
66                }
67               //---------------------------------------------------------
68               elseif(strpos($criteria_value,"insurance_data.provider = '1'")!== false)
69                {
70                 $query_part .= ' AND '."insurance_data.provider > '0' and insurance_data.date <= form_encounter.date";
71                }
72               elseif(strpos($criteria_value,"insurance_data.provider = '0'")!== false)
73                {
74                 $query_part .= ' AND '."(insurance_data.provider = '0' or insurance_data.date > form_encounter.date)";
75                }
76               //---------------------------------------------------------
77               else
78                {
79                 $query_part .= ' AND '.$criteria_value;
80                }
81               }
82          }
83      }
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")
87     {
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.
93         //
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";
107             //echo $sql;
108         $res = sqlStatement($sql,array($code_type));
109         $all = False;
110         for($iter=0; $row=sqlFetchArray($res); $iter++)
111         {
112             $all[$iter] = $row;
113         }
115         return $all;
116     }
117     function getBillsBetweenReport( $code_type,
118         $cols = "id,date,pid,code_type,code,user,authorized,x12_partner_id")
119     {
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.
125         //
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";
139             //echo $sql;
140         $res = sqlStatement($sql,array($code_type));
141         $all = False;
142         for($iter=0; $row=sqlFetchArray($res); $iter++)
143         {
144             $all[$iter] = $row;
145         }
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++)
151         {
152             $all[$iter] = $row;
153         }
155         return $all;
156     }
157     function getBillsListBetween( $code_type,
158         $cols = "billing.id, form_encounter.date, billing.pid, billing.code_type, billing.code, billing.user")
159     {
160         GenerateTheQueryPart();
161         global $query_part,$billstring,$auth;
162         // See above comment in getBillsBetween().
163         //
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' ".
171             $auth  .
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));
178         $string = "( ";
179         for($iter=0; $row=sqlFetchArray($res); $iter++)
180         {
181             $string .= $row{"id"}.",";
182         }
183         $string = substr($string,0,strlen($string)-1);
184         $string .= ")";
185         return $string;
186     }
188     function billCodesList($list,$skip = "()") {
189         if ($list == "()")
190             return;
192         if ($skip == "()")
193             sqlStatement("update billing set billed=1 where id in $list");
194         else
195             sqlStatement("update billing set billed=1 where id in $list and id not in $skip");
197         return;
198     }
200     function ReturnOFXSql()
201      {
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' "
213           . "$auth "
214           . "$billstring  $query_part  "
215           . "order by billing.pid,billing.encounter";
217          return $sql;
218      }