Added access controls for encounter categories
[openemr.git] / library / payment.inc.php
blob8f30c6f239738e2c89b7800bc543efd307cda457
1 <?php
2 // +-----------------------------------------------------------------------------+
3 // Copyright (C) 2010 Z&H Consultancy Services Private Limited <sam@zhservices.com>
4 //
5 //
6 // This program is free software; you can redistribute it and/or
7 // modify it under the terms of the GNU General Public License
8 // as published by the Free Software Foundation; either version 2
9 // of the License, or (at your option) any later version.
12 // This program is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
18 // A copy of the GNU General Public License is included along with this program:
19 // openemr/interface/login/GnuGPL.html
20 // For more information write to the Free Software
21 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
23 // Author: Eldho Chacko <eldho@zhservices.com>
24 // Paul Simon K <paul@zhservices.com>
26 // +------------------------------------------------------------------------------+
28 // Post a payment to the payments table.
30 function frontPayment($patient_id, $encounter, $method, $source, $amount1, $amount2, $timestamp, $auth="")
33 if (empty($auth)) {
34 $auth=$_SESSION['authUser'];
37 $tmprow = sqlQuery("SELECT date FROM form_encounter WHERE " .
38 "encounter=? and pid=?",
39 array($encounter,$patient_id));
40 //the manipulation is done to insert the amount paid into payments table in correct order to show in front receipts report,
41 //if the payment is for today's encounter it will be shown in the report under today field and otherwise shown as previous
42 $tmprowArray=explode(' ',$tmprow['date']);
43 if(date('Y-m-d')==$tmprowArray[0])
45 if($amount1==0)
47 $amount1=$amount2;
48 $amount2=0;
51 else
53 if($amount2==0)
55 $amount2=$amount1;
56 $amount1=0;
59 $payid = sqlInsert("INSERT INTO payments ( " .
60 "pid, encounter, dtime, user, method, source, amount1, amount2 " .
61 ") VALUES ( ?, ?, ?, ?, ?, ?, ?, ?)", array($patient_id,$encounter,$timestamp,$auth,$method,$source,$amount1,$amount2) );
62 return $payid;
65 //===============================================================================
66 //This section handles the common functins of payment screens.
67 //===============================================================================
68 function DistributionInsert($CountRow,$created_time,$user_id)
70 //Function inserts the distribution.Payment,Adjustment,Deductible,Takeback & Follow up reasons are inserted as seperate rows.
71 //It automatically pushes to next insurance for billing.
72 //In the screen a drop down of Ins1,Ins2,Ins3,Pat are given.The posting can be done for any level.
73 $Affected='no';
74 if (isset($_POST["Payment$CountRow"]) && $_POST["Payment$CountRow"]*1>0)
76 if(trim(formData('type_name' ))=='insurance')
78 if(trim(formData("HiddenIns$CountRow" ))==1)
80 $AccountCode="IPP";
82 if(trim(formData("HiddenIns$CountRow" ))==2)
84 $AccountCode="ISP";
86 if(trim(formData("HiddenIns$CountRow" ))==3)
88 $AccountCode="ITP";
91 elseif(trim(formData('type_name' ))=='patient')
93 $AccountCode="PP";
96 sqlBeginTrans();
97 $sequence_no = sqlQuery( "SELECT IFNULL(MAX(sequence_no),0) + 1 AS increment FROM ar_activity WHERE pid = ? AND encounter = ?", array(trim(formData('hidden_patient_code' )), trim(formData("HiddenEncounter$CountRow" ))));
98 sqlStatement("insert into ar_activity set " .
99 "pid = '" . trim(formData('hidden_patient_code' )) .
100 "', encounter = '" . trim(formData("HiddenEncounter$CountRow" )) .
101 "', sequence_no = '" . $sequence_no['increment'] .
102 "', code_type = '" . trim(formData("HiddenCodetype$CountRow" )) .
103 "', code = '" . trim(formData("HiddenCode$CountRow" )) .
104 "', modifier = '" . trim(formData("HiddenModifier$CountRow" )) .
105 "', payer_type = '" . trim(formData("HiddenIns$CountRow" )) .
106 "', post_time = '" . trim($created_time ) .
107 "', post_user = '" . trim($user_id ) .
108 "', session_id = '" . trim(formData('payment_id')) .
109 "', modified_time = '" . trim($created_time ) .
110 "', pay_amount = '" . trim(formData("Payment$CountRow" )) .
111 "', adj_amount = '" . 0 .
112 "', account_code = '" . "$AccountCode" .
113 "'");
114 sqlCommitTrans();
115 $Affected='yes';
117 if (isset($_POST["AdjAmount$CountRow"]) && $_POST["AdjAmount$CountRow"]*1!=0)
119 if(trim(formData('type_name' ))=='insurance')
121 $AdjustString="Ins adjust Ins".trim(formData("HiddenIns$CountRow" ));
122 $AccountCode="IA";
124 elseif(trim(formData('type_name' ))=='patient')
126 $AdjustString="Pt adjust";
127 $AccountCode="PA";
130 sqlBeginTrans();
131 $sequence_no = sqlQuery( "SELECT IFNULL(MAX(sequence_no),0) + 1 AS increment FROM ar_activity WHERE pid = ? AND encounter = ?", array(trim(formData('hidden_patient_code' )), trim(formData("HiddenEncounter$CountRow" ))));
132 sqlInsert("insert into ar_activity set " .
133 "pid = '" . trim(formData('hidden_patient_code' )) .
134 "', encounter = '" . trim(formData("HiddenEncounter$CountRow" )) .
135 "', sequence_no = '" . $sequence_no['increment'] .
136 "', code_type = '" . trim(formData("HiddenCodetype$CountRow" )) .
137 "', code = '" . trim(formData("HiddenCode$CountRow" )) .
138 "', modifier = '" . trim(formData("HiddenModifier$CountRow" )) .
139 "', payer_type = '" . trim(formData("HiddenIns$CountRow" )) .
140 "', post_time = '" . trim($created_time ) .
141 "', post_user = '" . trim($user_id ) .
142 "', session_id = '" . trim(formData('payment_id')) .
143 "', modified_time = '" . trim($created_time ) .
144 "', pay_amount = '" . 0 .
145 "', adj_amount = '" . trim(formData("AdjAmount$CountRow" )) .
146 "', memo = '" . "$AdjustString" .
147 "', account_code = '" . "$AccountCode" .
148 "'");
149 sqlCommitTrans();
150 $Affected='yes';
152 if (isset($_POST["Deductible$CountRow"]) && $_POST["Deductible$CountRow"]*1>0)
154 sqlBeginTrans();
155 $sequence_no = sqlQuery( "SELECT IFNULL(MAX(sequence_no),0) + 1 AS increment FROM ar_activity WHERE pid = ? AND encounter = ?", array(trim(formData('hidden_patient_code' )), trim(formData("HiddenEncounter$CountRow" ))));
156 sqlInsert("insert into ar_activity set " .
157 "pid = '" . trim(formData('hidden_patient_code' )) .
158 "', encounter = '" . trim(formData("HiddenEncounter$CountRow" )) .
159 "', sequence_no = '" . $sequence_no['increment'] .
160 "', code_type = '" . trim(formData("HiddenCodetype$CountRow" )) .
161 "', code = '" . trim(formData("HiddenCode$CountRow" )) .
162 "', modifier = '" . trim(formData("HiddenModifier$CountRow" )) .
163 "', payer_type = '" . trim(formData("HiddenIns$CountRow" )) .
164 "', post_time = '" . trim($created_time ) .
165 "', post_user = '" . trim($user_id ) .
166 "', session_id = '" . trim(formData('payment_id')) .
167 "', modified_time = '" . trim($created_time ) .
168 "', pay_amount = '" . 0 .
169 "', adj_amount = '" . 0 .
170 "', memo = '" . "Deductible $".trim(formData("Deductible$CountRow" )) .
171 "', account_code = '" . "Deduct" .
172 "'");
173 sqlCommitTrans();
174 $Affected='yes';
176 if (isset($_POST["Takeback$CountRow"]) && $_POST["Takeback$CountRow"]*1>0)
178 sqlBeginTrans();
179 $sequence_no = sqlQuery( "SELECT IFNULL(MAX(sequence_no),0) + 1 AS increment FROM ar_activity WHERE pid = ? AND encounter = ?", array(trim(formData('hidden_patient_code' )), trim(formData("HiddenEncounter$CountRow" ))));
180 sqlInsert("insert into ar_activity set " .
181 "pid = '" . trim(formData('hidden_patient_code' )) .
182 "', encounter = '" . trim(formData("HiddenEncounter$CountRow" )) .
183 "', sequence_no = '" . $sequence_no['increment'] .
184 "', code_type = '" . trim(formData("HiddenCodetype$CountRow" )) .
185 "', code = '" . trim(formData("HiddenCode$CountRow" )) .
186 "', modifier = '" . trim(formData("HiddenModifier$CountRow" )) .
187 "', payer_type = '" . trim(formData("HiddenIns$CountRow" )) .
188 "', post_time = '" . trim($created_time ) .
189 "', post_user = '" . trim($user_id ) .
190 "', session_id = '" . trim(formData('payment_id')) .
191 "', modified_time = '" . trim($created_time ) .
192 "', pay_amount = '" . trim(formData("Takeback$CountRow" ))*-1 .
193 "', adj_amount = '" . 0 .
194 "', account_code = '" . "Takeback" .
195 "'");
196 sqlCommitTrans();
197 $Affected='yes';
199 if (isset($_POST["FollowUp$CountRow"]) && $_POST["FollowUp$CountRow"]=='y')
201 sqlBeginTrans();
202 $sequence_no = sqlQuery( "SELECT IFNULL(MAX(sequence_no),0) + 1 AS increment FROM ar_activity WHERE pid = ? AND encounter = ?", array(trim(formData('hidden_patient_code' )), trim(formData("HiddenEncounter$CountRow" ))));
203 sqlInsert("insert into ar_activity set " .
204 "pid = '" . trim(formData('hidden_patient_code' )) .
205 "', encounter = '" . trim(formData("HiddenEncounter$CountRow" )) .
206 "', sequence_no = '" . $sequence_no['increment'] .
207 "', code_type = '" . trim(formData("HiddenCodetype$CountRow" )) .
208 "', code = '" . trim(formData("HiddenCode$CountRow" )) .
209 "', modifier = '" . trim(formData("HiddenModifier$CountRow" )) .
210 "', payer_type = '" . trim(formData("HiddenIns$CountRow" )) .
211 "', post_time = '" . trim($created_time ) .
212 "', post_user = '" . trim($user_id ) .
213 "', session_id = '" . trim(formData('payment_id')) .
214 "', modified_time = '" . trim($created_time ) .
215 "', pay_amount = '" . 0 .
216 "', adj_amount = '" . 0 .
217 "', follow_up = '" . "y" .
218 "', follow_up_note = '" . trim(formData("FollowUpReason$CountRow" )) .
219 "'");
220 sqlCommitTrans();
221 $Affected='yes';
223 if($Affected=='yes')
225 if(trim(formData('type_name' ))!='patient')
227 $ferow = sqlQuery("select last_level_closed from form_encounter where
228 pid ='".trim(formData('hidden_patient_code' ))."' and encounter='".trim(formData("HiddenEncounter$CountRow" ))."'");
229 //multiple charges can come.
230 if($ferow['last_level_closed']<trim(formData("HiddenIns$CountRow" )))
232 sqlStatement("update form_encounter set last_level_closed='".trim(formData("HiddenIns$CountRow" ))."' where
233 pid ='".trim(formData('hidden_patient_code' ))."' and encounter='".trim(formData("HiddenEncounter$CountRow" ))."'");
234 //last_level_closed gets increased.
235 //-----------------------------------
236 // Determine the next insurance level to be billed.
237 $ferow = sqlQuery("SELECT date, last_level_closed " .
238 "FROM form_encounter WHERE " .
239 "pid = '".trim(formData('hidden_patient_code' ))."' AND encounter = '".trim(formData("HiddenEncounter$CountRow" ))."'");
240 $date_of_service = substr($ferow['date'], 0, 10);
241 $new_payer_type = 0 + $ferow['last_level_closed'];
242 if ($new_payer_type <= 3 && !empty($ferow['last_level_closed']) || $new_payer_type == 0)
243 ++$new_payer_type;
244 $new_payer_id = arGetPayerID(trim(formData('hidden_patient_code' )), $date_of_service, $new_payer_type);
245 if($new_payer_id>0)
247 arSetupSecondary(trim(formData('hidden_patient_code' )), trim(formData("HiddenEncounter$CountRow" )),0);
249 //-----------------------------------
254 //===============================================================================
255 // Delete rows, with logging, for the specified table using the
256 // specified WHERE clause. Borrowed from deleter.php.
258 function row_delete($table, $where)
260 $tres = sqlStatement("SELECT * FROM $table WHERE $where");
261 $count = 0;
262 while ($trow = sqlFetchArray($tres)) {
263 $logstring = "";
264 foreach ($trow as $key => $value) {
265 if (! $value || $value == '0000-00-00 00:00:00') continue;
266 if ($logstring) $logstring .= " ";
267 $logstring .= $key . "='" . addslashes($value) . "'";
269 newEvent("delete", $_SESSION['authUser'], $_SESSION['authProvider'], 1, "$table: $logstring");
270 ++$count;
272 if ($count) {
273 $query = "DELETE FROM $table WHERE $where";
274 sqlStatement($query);
277 //===============================================================================