Percent-based price levels (#2577)
[openemr.git] / library / daysheet.inc.php
blob17a6bd0d7073cd2e2b734b90042fc9085a8af318
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 https://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 } elseif (strpos($criteria_value, "billing.id = 'null'")!== false) {
97 $billstring .= ' AND '."billing.id is null";
98 } elseif (strpos($criteria_value, "billing.id = 'not null'")!== false) {
99 $billstring .= ' AND '."billing.id is not null";
100 } elseif (strpos($criteria_value, "patient_data.fname")!== false) {
101 $SplitArray=explode(' like ', $criteria_value);
102 $query_part .= " AND ($criteria_value or patient_data.lname like ".$SplitArray[1].")";
103 } elseif (strpos($criteria_value, "billing.authorized")!== false) {
104 $auth = ' AND '.$criteria_value;
105 } elseif (strpos($criteria_value, "form_encounter.pid")!== false) {//comes like '781,780'
106 $SplitArray=explode(" = '", $criteria_value);//comes like 781,780'
107 $SplitArray[1]=substr($SplitArray[1], 0, -1);//comes like 781,780
108 $query_part .= ' AND form_encounter.pid in ('.$SplitArray[1].')';
109 $query_part2 .= ' AND pid in ('.$SplitArray[1].')';
110 } elseif (strpos($criteria_value, "form_encounter.encounter")!== 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.encounter in ('.$SplitArray[1].')';
114 } elseif (strpos($criteria_value, "insurance_data.provider = '1'")!== false) {
115 $query_part .= ' AND '."insurance_data.provider > '0' and insurance_data.date <= form_encounter.date";
116 } elseif (strpos($criteria_value, "insurance_data.provider = '0'")!== false) {
117 $query_part .= ' AND '."(insurance_data.provider = '0' or insurance_data.date > form_encounter.date)";
118 } else {
119 $query_part .= ' AND '.$criteria_value;
121 if (substr($criteria_value, 1, 8) === 'form_enc') {
122 $query_part_day .= ' AND ' . '(ar_activity.post_time'. substr($criteria_value, 20) ;
125 if (substr($criteria_value, 1, 12) === 'billing.date') {
126 $query_part_day .= ' AND ' . '(ar_activity.post_time'. substr($criteria_value, 13) ;
129 if (substr($criteria_value, 1, 14) === 'claims.process') {
130 $query_part_day .= ' AND ' . '(ar_activity.post_time'. substr($criteria_value, 20) ;
133 if (substr($criteria_value, 0, 12) === 'billing.user') {
134 $query_part_day .= ' AND ' . 'ar_activity.post_user'. substr($criteria_value, 12) ;
137 if (substr($criteria_value, 1, 8) === 'form_enc') {
138 $query_part_day1 .= ' AND ' . '(dtime'. substr($query_part, 25, 58) ;
141 if (substr($criteria_value, 1, 12) === 'billing.date') {
142 $query_part_day1 .= ' AND ' . '(dtime'. substr($query_part, 18, 58) ;
145 if (substr($criteria_value, 1, 14) === 'claims.process') {
146 $query_part_day1 .= ' AND ' . '(dtime'. substr($query_part, 25, 58) ;
152 //date must be in nice format (e.g. 2002-07-11)
154 function getBillsBetweendayReport(
155 $code_type,
156 $cols = "id,date,pid,code_type,code,user,authorized,x12_partner_id"
159 GenerateTheQueryPart();
160 global $query_part,$query_part2,$query_part_day,$query_part_day1,$billstring,$auth;
162 $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', " .
163 "form_encounter.encounter AS enc_encounter, form_encounter.provider_id AS enc_provider_id, billing.* , date(billing.date) as date " .
164 "FROM form_encounter " .
165 "LEFT OUTER JOIN billing ON " .
166 "billing.encounter = form_encounter.encounter AND " .
167 "billing.pid = form_encounter.pid AND " .
168 "billing.code_type LIKE ? AND " .
169 "billing.activity = 1 " .
170 "LEFT OUTER JOIN patient_data ON patient_data.pid = form_encounter.pid " .
171 "LEFT OUTER JOIN claims on claims.patient_id = form_encounter.pid and claims.encounter_id = form_encounter.encounter " .
172 "LEFT OUTER JOIN insurance_data on insurance_data.pid = form_encounter.pid and insurance_data.type = 'primary' ".
173 "WHERE 1=1 $query_part AND billing.fee!=0 " . " $auth " ." $billstring " .
174 "ORDER BY fulname ASC, date ASC, pid ";
176 $res = sqlStatement($sql, array($code_type));
177 $all = false;
178 for ($iter=0; $row=sqlFetchArray($res); $iter++) {
179 $all[$iter] = $row;
182 $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,".
183 "ar_activity.encounter AS encounter_ar, concat(lname, ' ', fname) as 'fulname', lname as 'last', fname as 'first', ar_activity.payer_type AS payer,".
184 "ar_activity.session_id AS sesid, ar_activity.account_code AS paytype, ar_activity.post_user AS user, ar_activity.memo AS reason,".
185 "ar_activity.adj_amount AS pat_adjust_dollar, providerid as 'provider_id' ".
186 "FROM ar_activity LEFT OUTER JOIN patient_data ON patient_data.pid = ar_activity.pid " .
187 "where 1=1 $query_part_day AND payer_type=0 ORDER BY fulname ASC, date ASC, pid");
189 for ($iter; $row=sqlFetchArray($query); $iter++) {
190 $all[$iter] = $row;
193 $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,".
194 "ar_activity.encounter AS encounter_ar, concat(lname, ' ', fname) as 'fulname', lname as 'last', fname as 'first', ar_activity.payer_type AS payer,".
195 "ar_activity.session_id AS sesid, ar_activity.account_code AS paytype, ar_activity.post_user AS user, ar_activity.memo AS reason,".
196 "ar_activity.adj_amount AS ins_adjust_dollar, providerid as 'provider_id' ".
197 "FROM ar_activity LEFT OUTER JOIN patient_data ON patient_data.pid = ar_activity.pid " .
198 "where 1=1 $query_part_day AND payer_type!=0 ORDER BY fulname ASC, date ASC, pid");
200 for ($iter; $row=sqlFetchArray($query); $iter++) {
201 $all[$iter] = $row;
204 return $all;