php fixes for #1486 #1471 and #1469
[openemr.git] / library / daysheet.inc.php
blob1444450fa1a955a6879ff3351bb846b03da13a38
1 <?php
2 /**
3 * library/daysheet.inc.php Functions used in the end of day report.
5 * Functions for Generating an End of Day report
8 * Copyright (C) 2014-2015 Terry Hill <terry@lillysystems.com>
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)
36 // if the parameters are invalid
37 if (!is_array($aryData) || !$strIndex || !$strSortBy) {
38 // return the array
39 return $aryData;
42 // create our temporary arrays
43 $arySort = $aryResult = array();
44 // loop through the array
45 foreach ($aryData as $aryRow) {
46 // set up the value in the array
47 $arySort[$aryRow[$strIndex]] = $aryRow[$strSortBy];
50 // apply the natural sort
51 natsort($arySort);
52 // if the sort type is descending
53 if ($strSortType=="desc") {
54 // reverse the array
55 arsort($arySort);
58 // loop through the sorted and original data
59 foreach ($arySort as $arySortKey => $arySorted) {
60 foreach ($aryData as $aryOriginal) {
61 // if the key matches
62 if ($aryOriginal[$strIndex]==$arySortKey) {
63 // add it to the output array
64 array_push($aryResult, $aryOriginal);
69 // return the return
70 return $aryResult;
73 function GenerateTheQueryPart()
75 global $query_part,$query_part2,$query_part_day,$query_part_day1,$billstring,$auth;
76 //Search Criteria section.
77 $billstring='';
78 $auth='';
79 $query_part='';
80 $query_part_day='';
81 $query_part_day1='';
82 $query_part2='';
84 if (isset($_REQUEST['final_this_page_criteria'])) {
85 foreach ($_REQUEST['final_this_page_criteria'] as $criteria_key => $criteria_value) {
86 $criteria_value=PrepareSearchItem($criteria_value); // this escapes for sql
87 $SplitArray=array();
88 //---------------------------------------------------------
89 if (strpos($criteria_value, "billing.billed = '1'")!== false) {
90 $billstring .= ' AND '.$criteria_value;
91 } elseif (strpos($criteria_value, "billing.billed = '0'")!== false) {
92 //3 is an error condition
93 $billstring .= ' AND '."(billing.billed is null or billing.billed = '0' or (billing.billed = '1' and billing.bill_process = '3'))";
94 } elseif (strpos($criteria_value, "billing.billed = '7'")!== false) {
95 $billstring .= ' AND '."billing.bill_process = '7'";
96 } //---------------------------------------------------------
97 elseif (strpos($criteria_value, "billing.id = 'null'")!== false) {
98 $billstring .= ' AND '."billing.id is null";
99 } //---------------------------------------------------------
100 elseif (strpos($criteria_value, "billing.id = 'not null'")!== false) {
101 $billstring .= ' AND '."billing.id is not null";
102 } //---------------------------------------------------------
103 elseif (strpos($criteria_value, "patient_data.fname")!== false) {
104 $SplitArray=explode(' like ', $criteria_value);
105 $query_part .= " AND ($criteria_value or patient_data.lname like ".$SplitArray[1].")";
106 } //---------------------------------------------------------
107 elseif (strpos($criteria_value, "billing.authorized")!== false) {
108 $auth = ' AND '.$criteria_value;
109 } //---------------------------------------------------------
110 elseif (strpos($criteria_value, "form_encounter.pid")!== false) {//comes like '781,780'
111 $SplitArray=explode(" = '", $criteria_value);//comes like 781,780'
112 $SplitArray[1]=substr($SplitArray[1], 0, -1);//comes like 781,780
113 $query_part .= ' AND form_encounter.pid in ('.$SplitArray[1].')';
114 $query_part2 .= ' AND pid in ('.$SplitArray[1].')';
115 } //---------------------------------------------------------
116 elseif (strpos($criteria_value, "form_encounter.encounter")!== false) {//comes like '781,780'
117 $SplitArray=explode(" = '", $criteria_value);//comes like 781,780'
118 $SplitArray[1]=substr($SplitArray[1], 0, -1);//comes like 781,780
119 $query_part .= ' AND form_encounter.encounter in ('.$SplitArray[1].')';
120 } //---------------------------------------------------------
121 elseif (strpos($criteria_value, "insurance_data.provider = '1'")!== false) {
122 $query_part .= ' AND '."insurance_data.provider > '0' and insurance_data.date <= form_encounter.date";
123 } elseif (strpos($criteria_value, "insurance_data.provider = '0'")!== false) {
124 $query_part .= ' AND '."(insurance_data.provider = '0' or insurance_data.date > form_encounter.date)";
125 } //---------------------------------------------------------
126 else {
127 $query_part .= ' AND '.$criteria_value;
129 if (substr($criteria_value, 1, 8) === 'form_enc') {
130 $query_part_day .= ' AND ' . '(ar_activity.post_time'. substr($criteria_value, 20) ;
133 if (substr($criteria_value, 1, 12) === 'billing.date') {
134 $query_part_day .= ' AND ' . '(ar_activity.post_time'. substr($criteria_value, 13) ;
137 if (substr($criteria_value, 1, 14) === 'claims.process') {
138 $query_part_day .= ' AND ' . '(ar_activity.post_time'. substr($criteria_value, 20) ;
141 if (substr($criteria_value, 0, 12) === 'billing.user') {
142 $query_part_day .= ' AND ' . 'ar_activity.post_user'. substr($criteria_value, 12) ;
145 if (substr($criteria_value, 1, 8) === 'form_enc') {
146 $query_part_day1 .= ' AND ' . '(dtime'. substr($query_part, 25, 58) ;
149 if (substr($criteria_value, 1, 12) === 'billing.date') {
150 $query_part_day1 .= ' AND ' . '(dtime'. substr($query_part, 18, 58) ;
153 if (substr($criteria_value, 1, 14) === 'claims.process') {
154 $query_part_day1 .= ' AND ' . '(dtime'. substr($query_part, 25, 58) ;
160 //date must be in nice format (e.g. 2002-07-11)
162 function getBillsBetweendayReport(
163 $code_type,
164 $cols = "id,date,pid,code_type,code,user,authorized,x12_partner_id"
167 GenerateTheQueryPart();
168 global $query_part,$query_part2,$query_part_day,$query_part_day1,$billstring,$auth;
170 $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', " .
171 "form_encounter.encounter AS enc_encounter, form_encounter.provider_id AS enc_provider_id, billing.* , date(billing.date) as date " .
172 "FROM form_encounter " .
173 "LEFT OUTER JOIN billing ON " .
174 "billing.encounter = form_encounter.encounter AND " .
175 "billing.pid = form_encounter.pid AND " .
176 "billing.code_type LIKE ? AND " .
177 "billing.activity = 1 " .
178 "LEFT OUTER JOIN patient_data ON patient_data.pid = form_encounter.pid " .
179 "LEFT OUTER JOIN claims on claims.patient_id = form_encounter.pid and claims.encounter_id = form_encounter.encounter " .
180 "LEFT OUTER JOIN insurance_data on insurance_data.pid = form_encounter.pid and insurance_data.type = 'primary' ".
181 "WHERE 1=1 $query_part AND billing.fee!=0 " . " $auth " ." $billstring " .
182 "ORDER BY fulname ASC, date ASC, pid ";
184 $res = sqlStatement($sql, array($code_type));
185 $all = false;
186 for ($iter=0; $row=sqlFetchArray($res); $iter++) {
187 $all[$iter] = $row;
190 $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,".
191 "ar_activity.encounter AS encounter_ar, concat(lname, ' ', fname) as 'fulname', lname as 'last', fname as 'first', ar_activity.payer_type AS payer,".
192 "ar_activity.session_id AS sesid, ar_activity.account_code AS paytype, ar_activity.post_user AS user, ar_activity.memo AS reason,".
193 "ar_activity.adj_amount AS pat_adjust_dollar, providerid as 'provider_id' ".
194 "FROM ar_activity LEFT OUTER JOIN patient_data ON patient_data.pid = ar_activity.pid " .
195 "where 1=1 $query_part_day AND payer_type=0 ORDER BY fulname ASC, date ASC, pid");
197 for ($iter; $row=sqlFetchArray($query); $iter++) {
198 $all[$iter] = $row;
201 $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,".
202 "ar_activity.encounter AS encounter_ar, concat(lname, ' ', fname) as 'fulname', lname as 'last', fname as 'first', ar_activity.payer_type AS payer,".
203 "ar_activity.session_id AS sesid, ar_activity.account_code AS paytype, ar_activity.post_user AS user, ar_activity.memo AS reason,".
204 "ar_activity.adj_amount AS ins_adjust_dollar, providerid as 'provider_id' ".
205 "FROM ar_activity LEFT OUTER JOIN patient_data ON patient_data.pid = ar_activity.pid " .
206 "where 1=1 $query_part_day AND payer_type!=0 ORDER BY fulname ASC, date ASC, pid");
208 for ($iter; $row=sqlFetchArray($query); $iter++) {
209 $all[$iter] = $row;
212 return $all;