fix for attendance form
[openemr.git] / library / daysheet.inc.php
blobe92f0e05e1ce4573f6a431e2303d98427e96811b
1 <?php
2 /**
3 * library/daysheet.inc.php Functions used in the end of day report.
4 *
5 * Functions for Generating an End of Day report
6 *
7 *
8 * Copyright (C) 2014-2015 Terry Hill <terry@lillysystems.com>
9 *
10 * LICENSE: This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License
12 * as published by the Free Software Foundation; either version 3
13 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program. If not, see <http://opensource.org/licenses/gpl-license.php>;.
21 * @package OpenEMR
22 * @author Terry Hill <terry@lillysystems.com>
23 * @link http://www.open-emr.org
26 /**
27 * @return Returns the array sorted as required
28 * @param $aryData Array containing data to sort
29 * @param $strIndex Name of column to use as an index
30 * @param $strSortBy Column to sort the array by
31 * @param $strSortType String containing either asc or desc [default to asc]
32 * @desc Naturally sorts an array using by the column $strSortBy
34 function array_natsort($aryData, $strIndex, $strSortBy, $strSortType=false) {
35 // if the parameters are invalid
36 if (!is_array($aryData) || !$strIndex || !$strSortBy)
37 // return the array
38 return $aryData;
39 // create our temporary arrays
40 $arySort = $aryResult = array();
41 // loop through the array
42 foreach ($aryData as $aryRow)
43 // set up the value in the array
44 $arySort[$aryRow[$strIndex]] = $aryRow[$strSortBy];
45 // apply the natural sort
46 natsort($arySort);
47 // if the sort type is descending
48 if ($strSortType=="desc")
49 // reverse the array
50 arsort($arySort);
51 // loop through the sorted and original data
52 foreach ($arySort as $arySortKey => $arySorted)
53 foreach ($aryData as $aryOriginal)
54 // if the key matches
55 if ($aryOriginal[$strIndex]==$arySortKey)
56 // add it to the output array
57 array_push($aryResult, $aryOriginal);
58 // return the return
59 return $aryResult;
62 function GenerateTheQueryPart()
64 global $query_part,$query_part2,$query_part_day,$query_part_day1,$billstring,$auth;
65 //Search Criteria section.
66 $billstring='';
67 $auth='';
68 $query_part='';
69 $query_part_day='';
70 $query_part_day1='';
71 $query_part2='';
73 if(isset($_REQUEST['final_this_page_criteria']))
75 foreach($_REQUEST['final_this_page_criteria'] as $criteria_key => $criteria_value)
78 $criteria_value=PrepareSearchItem($criteria_value); // this escapes for sql
79 $SplitArray=array();
80 //---------------------------------------------------------
81 if(strpos($criteria_value,"billing.billed = '1'")!== false)
83 $billstring .= ' AND '.$criteria_value;
85 elseif(strpos($criteria_value,"billing.billed = '0'")!== false)
87 //3 is an error condition
88 $billstring .= ' AND '."(billing.billed is null or billing.billed = '0' or (billing.billed = '1' and billing.bill_process = '3'))";
90 elseif(strpos($criteria_value,"billing.billed = '7'")!== false)
92 $billstring .= ' AND '."billing.bill_process = '7'";
94 //---------------------------------------------------------
95 elseif(strpos($criteria_value,"billing.id = 'null'")!== false)
97 $billstring .= ' AND '."billing.id is null";
99 //---------------------------------------------------------
100 elseif(strpos($criteria_value,"billing.id = 'not null'")!== false)
102 $billstring .= ' AND '."billing.id is not null";
104 //---------------------------------------------------------
105 elseif(strpos($criteria_value,"patient_data.fname")!== false)
107 $SplitArray=explode(' like ',$criteria_value);
108 $query_part .= " AND ($criteria_value or patient_data.lname like ".$SplitArray[1].")";
110 //---------------------------------------------------------
111 elseif(strpos($criteria_value,"billing.authorized")!== false)
113 $auth = ' AND '.$criteria_value;
115 //---------------------------------------------------------
116 elseif(strpos($criteria_value,"form_encounter.pid")!== false)
117 {//comes like '781,780'
118 $SplitArray=explode(" = '",$criteria_value);//comes like 781,780'
119 $SplitArray[1]=substr($SplitArray[1], 0, -1);//comes like 781,780
120 $query_part .= ' AND form_encounter.pid in ('.$SplitArray[1].')';
121 $query_part2 .= ' AND pid in ('.$SplitArray[1].')';
123 //---------------------------------------------------------
124 elseif(strpos($criteria_value,"form_encounter.encounter")!== false)
125 {//comes like '781,780'
126 $SplitArray=explode(" = '",$criteria_value);//comes like 781,780'
127 $SplitArray[1]=substr($SplitArray[1], 0, -1);//comes like 781,780
128 $query_part .= ' AND form_encounter.encounter in ('.$SplitArray[1].')';
130 //---------------------------------------------------------
131 elseif(strpos($criteria_value,"insurance_data.provider = '1'")!== false)
133 $query_part .= ' AND '."insurance_data.provider > '0' and insurance_data.date <= form_encounter.date";
135 elseif(strpos($criteria_value,"insurance_data.provider = '0'")!== false)
137 $query_part .= ' AND '."(insurance_data.provider = '0' or insurance_data.date > form_encounter.date)";
139 //---------------------------------------------------------
140 else
142 $query_part .= ' AND '.$criteria_value;
144 if (substr($criteria_value,1,8) === 'form_enc') {
145 $query_part_day .= ' AND ' . '(ar_activity.post_time'. substr($criteria_value,20) ;
147 if (substr($criteria_value,1,12) === 'billing.date') {
148 $query_part_day .= ' AND ' . '(ar_activity.post_time'. substr($criteria_value,13) ;
150 if (substr($criteria_value,1,14) === 'claims.process') {
151 $query_part_day .= ' AND ' . '(ar_activity.post_time'. substr($criteria_value,20) ;
153 if (substr($criteria_value,0,12) === 'billing.user') {
154 $query_part_day .= ' AND ' . 'ar_activity.post_user'. substr($criteria_value,12) ;
156 if (substr($criteria_value,1,8) === 'form_enc') {
157 $query_part_day1 .= ' AND ' . '(dtime'. substr($query_part,25,58) ;
159 if (substr($criteria_value,1,12) === 'billing.date') {
160 $query_part_day1 .= ' AND ' . '(dtime'. substr($query_part,18,58) ;
162 if (substr($criteria_value,1,14) === 'claims.process') {
163 $query_part_day1 .= ' AND ' . '(dtime'. substr($query_part,25,58) ;
170 //date must be in nice format (e.g. 2002-07-11)
172 function getBillsBetweendayReport( $code_type,
173 $cols = "id,date,pid,code_type,code,user,authorized,x12_partner_id")
175 GenerateTheQueryPart();
176 global $query_part,$query_part2,$query_part_day,$query_part_day1,$billstring,$auth;
178 $sql = "SELECT distinct form_encounter.pid AS enc_pid, form_encounter.date AS enc_date, concat(lname, ' ', fname) as 'fulname', lname as 'last', fname as 'first', " .
179 "form_encounter.encounter AS enc_encounter, form_encounter.provider_id AS enc_provider_id, billing.* , date(billing.date) as date " .
180 "FROM form_encounter " .
181 "LEFT OUTER JOIN billing ON " .
182 "billing.encounter = form_encounter.encounter AND " .
183 "billing.pid = form_encounter.pid AND " .
184 "billing.code_type LIKE ? AND " .
185 "billing.activity = 1 " .
186 "LEFT OUTER JOIN patient_data ON patient_data.pid = form_encounter.pid " .
187 "LEFT OUTER JOIN claims on claims.patient_id = form_encounter.pid and claims.encounter_id = form_encounter.encounter " .
188 "LEFT OUTER JOIN insurance_data on insurance_data.pid = form_encounter.pid and insurance_data.type = 'primary' ".
189 "WHERE 1=1 $query_part AND billing.fee!=0 " . " $auth " ." $billstring " .
190 "ORDER BY fulname ASC, date ASC, pid ";
192 $res = sqlStatement($sql,array($code_type));
193 $all = False;
194 for($iter=0; $row=sqlFetchArray($res); $iter++)
196 $all[$iter] = $row;
199 $query = sqlStatement("SELECT ar_activity.pid as pid, 'Patient Payment' AS code_type, ar_activity.pay_amount AS pat_code, date(ar_activity.post_time) AS date,".
200 "ar_activity.encounter AS encounter_ar, concat(lname, ' ', fname) as 'fulname', lname as 'last', fname as 'first', ar_activity.payer_type AS payer,".
201 "ar_activity.session_id AS sesid, ar_activity.account_code AS paytype, ar_activity.post_user AS user, ar_activity.memo AS reason,".
202 "ar_activity.adj_amount AS pat_adjust_dollar, providerid as 'provider_id' ".
203 "FROM ar_activity LEFT OUTER JOIN patient_data ON patient_data.pid = ar_activity.pid " .
204 "where 1=1 $query_part_day AND payer_type=0 ORDER BY fulname ASC, date ASC, pid");
206 for($iter; $row=sqlFetchArray($query); $iter++)
208 $all[$iter] = $row;
211 $query = sqlStatement("SELECT ar_activity.pid as pid, 'Insurance Payment' AS code_type, ar_activity.pay_amount AS ins_code, date(ar_activity.post_time) AS date,".
212 "ar_activity.encounter AS encounter_ar, concat(lname, ' ', fname) as 'fulname', lname as 'last', fname as 'first', ar_activity.payer_type AS payer,".
213 "ar_activity.session_id AS sesid, ar_activity.account_code AS paytype, ar_activity.post_user AS user, ar_activity.memo AS reason,".
214 "ar_activity.adj_amount AS ins_adjust_dollar, providerid as 'provider_id' ".
215 "FROM ar_activity LEFT OUTER JOIN patient_data ON patient_data.pid = ar_activity.pid " .
216 "where 1=1 $query_part_day AND payer_type!=0 ORDER BY fulname ASC, date ASC, pid");
218 for($iter; $row=sqlFetchArray($query); $iter++)
220 $all[$iter] = $row;
223 return $all;