Internationalization: some more documentation fixes
[openemr.git] / library / billrep.inc
blobfea558b7f2944edf3c66c2bcdf136bd6eb450f96
1 <?php
2     require_once("{$GLOBALS['srcdir']}/sql.inc");
3     function GenerateTheQueryPart()
4      {
5         global $query_part,$billstring,$auth;
6         //Search Criteria section.
7         $billstring='';
8         $auth='';
9         $query_part='';
10         if(isset($_REQUEST['final_this_page_criteria']))
11          {
12             foreach($_REQUEST['final_this_page_criteria'] as $criteria_key => $criteria_value)
13              {
14               $criteria_value=PrepareSearchItem($criteria_value);
15               $SplitArray=array();
16               //---------------------------------------------------------
17               if(strpos($criteria_value,"billing.billed = '1'")!== false)
18                {
19                 $billstring .= ' AND '.$criteria_value;
20                }
21               elseif(strpos($criteria_value,"billing.billed = '0'")!== false)
22                {
23                 //3 is an error condition
24                 $billstring .= ' AND '."(billing.billed = '0' or (billing.billed = '1' and billing.bill_process = '3'))";
25                }
26               elseif(strpos($criteria_value,"billing.billed = '7'")!== false)
27                {
28                 $billstring .= ' AND '."billing.bill_process = '7'";
29                }
30               //---------------------------------------------------------
31               elseif(strpos($criteria_value,"billing.id = 'null'")!== false)
32                {
33                 $billstring .= ' AND '."billing.id is null";
34                }
35               //---------------------------------------------------------
36               elseif(strpos($criteria_value,"billing.id = 'not null'")!== false)
37                {
38                 $billstring .= ' AND '."billing.id is not null";
39                }
40               //---------------------------------------------------------
41               elseif(strpos($criteria_value,"patient_data.fname")!== false)
42                {
43                 $SplitArray=split(' like ',$criteria_value);
44                 $query_part .= " AND ($criteria_value or patient_data.lname like ".$SplitArray[1].")";
45                }
46               //---------------------------------------------------------
47               elseif(strpos($criteria_value,"billing.authorized")!== false)
48                {
49                 $auth = ' AND '.$criteria_value;
50                }
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].')';
57                }
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].')';
64                }
65               //---------------------------------------------------------
66               elseif(strpos($criteria_value,"insurance_data.provider = '1'")!== false)
67                {
68                 $query_part .= ' AND '."insurance_data.provider > '0' and insurance_data.date <= form_encounter.date";
69                }
70               elseif(strpos($criteria_value,"insurance_data.provider = '0'")!== false)
71                {
72                 $query_part .= ' AND '."(insurance_data.provider = '0' or insurance_data.date > form_encounter.date)";
73                }
74               //---------------------------------------------------------
75               else
76                {
77                 $query_part .= ' AND '.$criteria_value;
78                }
79               }
80          }
81      }
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")
85     {
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.
91         //
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";
105             //echo $sql;
106         $res = sqlStatement($sql);
108         for($iter=0; $row=sqlFetchArray($res); $iter++)
109         {
110             $all[$iter] = $row;
111         }
113         return $all;
114     }
115     function getBillsListBetween( $code_type,
116         $cols = "billing.id, form_encounter.date, billing.pid, billing.code_type, billing.code, billing.user")
117     {
118         GenerateTheQueryPart();
119         global $query_part,$billstring,$auth;
120         // See above comment in getBillsBetween().
121         //
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' ".
129             $auth  .
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);
136         $string = "( ";
137         for($iter=0; $row=sqlFetchArray($res); $iter++)
138         {
139             $string .= $row{"id"}.",";
140         }
141         $string = substr($string,0,strlen($string)-1);
142         $string .= ")";
143         return $string;
144     }
146     function billCodesList($list,$skip = "()") {
147         if ($list == "()")
148             return;
150         if ($skip == "()")
151             sqlStatement("update billing set billed=1 where id in $list");
152         else
153             sqlStatement("update billing set billed=1 where id in $list and id not in $skip");
155         return;
156     }
157     
158     function ReturnOFXSql()
159      {
160         GenerateTheQueryPart();
161         global $query_part,$billstring,$auth;
162         
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' "
171           . "$auth " 
172           . "$billstring  $query_part  " 
173           . "order by billing.pid,billing.encounter";
174           
175          return $sql;
176      }