some portal work
[openemr.git] / library / daysheet.inc.php
blob33a537873322330c6a5aadf78e460e59259391aa
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
35 use OpenEMR\Billing\BillingReport;
37 function array_natsort($aryData, $strIndex, $strSortBy, $strSortType = false)
39 // if the parameters are invalid
40 if (!is_array($aryData) || !$strIndex || !$strSortBy) {
41 // return the array
42 return $aryData;
45 // create our temporary arrays
46 $arySort = $aryResult = array();
47 // loop through the array
48 foreach ($aryData as $aryRow) {
49 // set up the value in the array
50 $arySort[$aryRow[$strIndex]] = $aryRow[$strSortBy];
53 // apply the natural sort
54 natsort($arySort);
55 // if the sort type is descending
56 if ($strSortType=="desc") {
57 // reverse the array
58 arsort($arySort);
61 // loop through the sorted and original data
62 foreach ($arySort as $arySortKey => $arySorted) {
63 foreach ($aryData as $aryOriginal) {
64 // if the key matches
65 if ($aryOriginal[$strIndex]==$arySortKey) {
66 // add it to the output array
67 array_push($aryResult, $aryOriginal);
72 // return the return
73 return $aryResult;
76 function GenerateTheQueryPart()
78 global $query_part,$query_part2,$query_part_day,$query_part_day1,$billstring,$auth;
79 //Search Criteria section.
80 $billstring='';
81 $auth='';
82 $query_part='';
83 $query_part_day='';
84 $query_part_day1='';
85 $query_part2='';
87 if (isset($_REQUEST['final_this_page_criteria'])) {
88 foreach ($_REQUEST['final_this_page_criteria'] as $criteria_key => $criteria_value) {
89 $criteria_value = BillingReport::PrepareSearchItem($criteria_value); // this escapes for sql
90 $SplitArray=array();
91 //---------------------------------------------------------
92 if (strpos($criteria_value, "billing.billed = '1'")!== false) {
93 $billstring .= ' AND '.$criteria_value;
94 } elseif (strpos($criteria_value, "billing.billed = '0'")!== false) {
95 //3 is an error condition
96 $billstring .= ' AND '."(billing.billed is null or billing.billed = '0' or (billing.billed = '1' and billing.bill_process = '3'))";
97 } elseif (strpos($criteria_value, "billing.billed = '7'")!== false) {
98 $billstring .= ' AND '."billing.bill_process = '7'";
99 } elseif (strpos($criteria_value, "billing.id = 'null'")!== false) {
100 $billstring .= ' AND '."billing.id is null";
101 } elseif (strpos($criteria_value, "billing.id = 'not null'")!== false) {
102 $billstring .= ' AND '."billing.id is not null";
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 } elseif (strpos($criteria_value, "billing.authorized")!== false) {
107 $auth = ' AND '.$criteria_value;
108 } elseif (strpos($criteria_value, "form_encounter.pid")!== false) {//comes like '781,780'
109 $SplitArray=explode(" = '", $criteria_value);//comes like 781,780'
110 $SplitArray[1]=substr($SplitArray[1], 0, -1);//comes like 781,780
111 $query_part .= ' AND form_encounter.pid in ('.$SplitArray[1].')';
112 $query_part2 .= ' AND pid in ('.$SplitArray[1].')';
113 } elseif (strpos($criteria_value, "form_encounter.encounter")!== false) {//comes like '781,780'
114 $SplitArray=explode(" = '", $criteria_value);//comes like 781,780'
115 $SplitArray[1]=substr($SplitArray[1], 0, -1);//comes like 781,780
116 $query_part .= ' AND form_encounter.encounter in ('.$SplitArray[1].')';
117 } elseif (strpos($criteria_value, "insurance_data.provider = '1'")!== false) {
118 $query_part .= ' AND '."insurance_data.provider > '0' and insurance_data.date <= form_encounter.date";
119 } elseif (strpos($criteria_value, "insurance_data.provider = '0'")!== false) {
120 $query_part .= ' AND '."(insurance_data.provider = '0' or insurance_data.date > form_encounter.date)";
121 } else {
122 $query_part .= ' AND '.$criteria_value;
124 if (substr($criteria_value, 1, 8) === 'form_enc') {
125 $query_part_day .= ' AND ' . '(ar_activity.post_time'. substr($criteria_value, 20) ;
128 if (substr($criteria_value, 1, 12) === 'billing.date') {
129 $query_part_day .= ' AND ' . '(ar_activity.post_time'. substr($criteria_value, 13) ;
132 if (substr($criteria_value, 1, 14) === 'claims.process') {
133 $query_part_day .= ' AND ' . '(ar_activity.post_time'. substr($criteria_value, 20) ;
136 if (substr($criteria_value, 0, 12) === 'billing.user') {
137 $query_part_day .= ' AND ' . 'ar_activity.post_user'. substr($criteria_value, 12) ;
140 if (substr($criteria_value, 1, 8) === 'form_enc') {
141 $query_part_day1 .= ' AND ' . '(dtime'. substr($query_part, 25, 58) ;
144 if (substr($criteria_value, 1, 12) === 'billing.date') {
145 $query_part_day1 .= ' AND ' . '(dtime'. substr($query_part, 18, 58) ;
148 if (substr($criteria_value, 1, 14) === 'claims.process') {
149 $query_part_day1 .= ' AND ' . '(dtime'. substr($query_part, 25, 58) ;
155 //date must be in nice format (e.g. 2002-07-11)
157 function getBillsBetweendayReport(
158 $code_type,
159 $cols = "id,date,pid,code_type,code,user,authorized,x12_partner_id"
162 GenerateTheQueryPart();
163 global $query_part,$query_part2,$query_part_day,$query_part_day1,$billstring,$auth;
165 $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', " .
166 "form_encounter.encounter AS enc_encounter, form_encounter.provider_id AS enc_provider_id, billing.* , date(billing.date) as date " .
167 "FROM form_encounter " .
168 "LEFT OUTER JOIN billing ON " .
169 "billing.encounter = form_encounter.encounter AND " .
170 "billing.pid = form_encounter.pid AND " .
171 "billing.code_type LIKE ? AND " .
172 "billing.activity = 1 " .
173 "LEFT OUTER JOIN patient_data ON patient_data.pid = form_encounter.pid " .
174 "LEFT OUTER JOIN claims on claims.patient_id = form_encounter.pid and claims.encounter_id = form_encounter.encounter " .
175 "LEFT OUTER JOIN insurance_data on insurance_data.pid = form_encounter.pid and insurance_data.type = 'primary' ".
176 "WHERE 1=1 $query_part AND billing.fee!=0 " . " $auth " ." $billstring " .
177 "ORDER BY fulname ASC, date ASC, pid ";
179 $res = sqlStatement($sql, array($code_type));
180 $all = false;
181 for ($iter=0; $row=sqlFetchArray($res); $iter++) {
182 $all[$iter] = $row;
185 $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,".
186 "ar_activity.encounter AS encounter_ar, concat(lname, ' ', fname) as 'fulname', lname as 'last', fname as 'first', ar_activity.payer_type AS payer,".
187 "ar_activity.session_id AS sesid, ar_activity.account_code AS paytype, ar_activity.post_user AS user, ar_activity.memo AS reason,".
188 "ar_activity.adj_amount AS pat_adjust_dollar, providerid as 'provider_id' ".
189 "FROM ar_activity LEFT OUTER JOIN patient_data ON patient_data.pid = ar_activity.pid " .
190 "where 1=1 $query_part_day AND payer_type=0 ORDER BY fulname ASC, date ASC, pid");
192 for ($iter; $row=sqlFetchArray($query); $iter++) {
193 $all[$iter] = $row;
196 $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,".
197 "ar_activity.encounter AS encounter_ar, concat(lname, ' ', fname) as 'fulname', lname as 'last', fname as 'first', ar_activity.payer_type AS payer,".
198 "ar_activity.session_id AS sesid, ar_activity.account_code AS paytype, ar_activity.post_user AS user, ar_activity.memo AS reason,".
199 "ar_activity.adj_amount AS ins_adjust_dollar, providerid as 'provider_id' ".
200 "FROM ar_activity LEFT OUTER JOIN patient_data ON patient_data.pid = ar_activity.pid " .
201 "where 1=1 $query_part_day AND payer_type!=0 ORDER BY fulname ASC, date ASC, pid");
203 for ($iter; $row=sqlFetchArray($query); $iter++) {
204 $all[$iter] = $row;
207 return $all;