Update of P.R #1612 by Daniel Pflieger to 5.0.2
[openemr.git] / library / payment.inc.php
blob757d3a8e7d936c6e5dbb7562fbcb8116d66a01f8
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(
38 "SELECT date FROM form_encounter WHERE " .
39 "encounter=? and pid=?",
40 array($encounter,$patient_id)
42 //the manipulation is done to insert the amount paid into payments table in correct order to show in front receipts report,
43 //if the payment is for today's encounter it will be shown in the report under today field and otherwise shown as previous
44 $tmprowArray=explode(' ', $tmprow['date']);
45 if (date('Y-m-d')==$tmprowArray[0]) {
46 if ($amount1==0) {
47 $amount1=$amount2;
48 $amount2=0;
50 } else {
51 if ($amount2==0) {
52 $amount2=$amount1;
53 $amount1=0;
57 $payid = sqlInsert("INSERT INTO payments ( " .
58 "pid, encounter, dtime, user, method, source, amount1, amount2 " .
59 ") VALUES ( ?, ?, ?, ?, ?, ?, ?, ?)", array($patient_id,$encounter,$timestamp,$auth,$method,$source,$amount1,$amount2));
60 return $payid;
63 //===============================================================================
64 //This section handles the common functins of payment screens.
65 //===============================================================================
66 function DistributionInsert($CountRow, $created_time, $user_id)
68 //Function inserts the distribution.Payment,Adjustment,Deductible,Takeback & Follow up reasons are inserted as seperate rows.
69 //It automatically pushes to next insurance for billing.
70 //In the screen a drop down of Ins1,Ins2,Ins3,Pat are given.The posting can be done for any level.
71 $Affected='no';
72 if (isset($_POST["Payment$CountRow"]) && $_POST["Payment$CountRow"]*1>0) {
73 if (trim(formData('type_name'))=='insurance') {
74 if (trim(formData("HiddenIns$CountRow"))==1) {
75 $AccountCode="IPP";
78 if (trim(formData("HiddenIns$CountRow"))==2) {
79 $AccountCode="ISP";
82 if (trim(formData("HiddenIns$CountRow"))==3) {
83 $AccountCode="ITP";
85 } elseif (trim(formData('type_name'))=='patient') {
86 $AccountCode="PP";
89 sqlBeginTrans();
90 $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"))));
91 sqlStatement("insert into ar_activity set " .
92 "pid = '" . trim(formData('hidden_patient_code')) .
93 "', encounter = '" . trim(formData("HiddenEncounter$CountRow")) .
94 "', sequence_no = '" . $sequence_no['increment'] .
95 "', code_type = '" . trim(formData("HiddenCodetype$CountRow")) .
96 "', code = '" . trim(formData("HiddenCode$CountRow")) .
97 "', modifier = '" . trim(formData("HiddenModifier$CountRow")) .
98 "', payer_type = '" . trim(formData("HiddenIns$CountRow")) .
99 "', post_time = '" . trim($created_time) .
100 "', post_user = '" . trim($user_id) .
101 "', session_id = '" . trim(formData('payment_id')) .
102 "', modified_time = '" . trim($created_time) .
103 "', pay_amount = '" . trim(formData("Payment$CountRow")) .
104 "', adj_amount = '" . 0 .
105 "', account_code = '" . "$AccountCode" .
106 "'");
107 sqlCommitTrans();
108 $Affected='yes';
111 if (isset($_POST["AdjAmount$CountRow"]) && $_POST["AdjAmount$CountRow"]*1!=0) {
112 if (trim(formData('type_name'))=='insurance') {
113 $AdjustString="Ins adjust Ins".trim(formData("HiddenIns$CountRow"));
114 $AccountCode="IA";
115 } elseif (trim(formData('type_name'))=='patient') {
116 $AdjustString="Pt adjust";
117 $AccountCode="PA";
120 sqlBeginTrans();
121 $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"))));
122 sqlInsert("insert into ar_activity set " .
123 "pid = '" . trim(formData('hidden_patient_code')) .
124 "', encounter = '" . trim(formData("HiddenEncounter$CountRow")) .
125 "', sequence_no = '" . $sequence_no['increment'] .
126 "', code_type = '" . trim(formData("HiddenCodetype$CountRow")) .
127 "', code = '" . trim(formData("HiddenCode$CountRow")) .
128 "', modifier = '" . trim(formData("HiddenModifier$CountRow")) .
129 "', payer_type = '" . trim(formData("HiddenIns$CountRow")) .
130 "', post_time = '" . trim($created_time) .
131 "', post_user = '" . trim($user_id) .
132 "', session_id = '" . trim(formData('payment_id')) .
133 "', modified_time = '" . trim($created_time) .
134 "', pay_amount = '" . 0 .
135 "', adj_amount = '" . trim(formData("AdjAmount$CountRow")) .
136 "', memo = '" . "$AdjustString" .
137 "', account_code = '" . "$AccountCode" .
138 "'");
139 sqlCommitTrans();
140 $Affected='yes';
143 if (isset($_POST["Deductible$CountRow"]) && $_POST["Deductible$CountRow"]*1>0) {
144 sqlBeginTrans();
145 $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"))));
146 sqlInsert("insert into ar_activity set " .
147 "pid = '" . trim(formData('hidden_patient_code')) .
148 "', encounter = '" . trim(formData("HiddenEncounter$CountRow")) .
149 "', sequence_no = '" . $sequence_no['increment'] .
150 "', code_type = '" . trim(formData("HiddenCodetype$CountRow")) .
151 "', code = '" . trim(formData("HiddenCode$CountRow")) .
152 "', modifier = '" . trim(formData("HiddenModifier$CountRow")) .
153 "', payer_type = '" . trim(formData("HiddenIns$CountRow")) .
154 "', post_time = '" . trim($created_time) .
155 "', post_user = '" . trim($user_id) .
156 "', session_id = '" . trim(formData('payment_id')) .
157 "', modified_time = '" . trim($created_time) .
158 "', pay_amount = '" . 0 .
159 "', adj_amount = '" . 0 .
160 "', memo = '" . "Deductible $".trim(formData("Deductible$CountRow")) .
161 "', account_code = '" . "Deduct" .
162 "'");
163 sqlCommitTrans();
164 $Affected='yes';
167 if (isset($_POST["Takeback$CountRow"]) && $_POST["Takeback$CountRow"]*1>0) {
168 sqlBeginTrans();
169 $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"))));
170 sqlInsert("insert into ar_activity set " .
171 "pid = '" . trim(formData('hidden_patient_code')) .
172 "', encounter = '" . trim(formData("HiddenEncounter$CountRow")) .
173 "', sequence_no = '" . $sequence_no['increment'] .
174 "', code_type = '" . trim(formData("HiddenCodetype$CountRow")) .
175 "', code = '" . trim(formData("HiddenCode$CountRow")) .
176 "', modifier = '" . trim(formData("HiddenModifier$CountRow")) .
177 "', payer_type = '" . trim(formData("HiddenIns$CountRow")) .
178 "', post_time = '" . trim($created_time) .
179 "', post_user = '" . trim($user_id) .
180 "', session_id = '" . trim(formData('payment_id')) .
181 "', modified_time = '" . trim($created_time) .
182 "', pay_amount = '" . trim(formData("Takeback$CountRow"))*-1 .
183 "', adj_amount = '" . 0 .
184 "', account_code = '" . "Takeback" .
185 "'");
186 sqlCommitTrans();
187 $Affected='yes';
190 if (isset($_POST["FollowUp$CountRow"]) && $_POST["FollowUp$CountRow"]=='y') {
191 sqlBeginTrans();
192 $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"))));
193 sqlInsert("insert into ar_activity set " .
194 "pid = '" . trim(formData('hidden_patient_code')) .
195 "', encounter = '" . trim(formData("HiddenEncounter$CountRow")) .
196 "', sequence_no = '" . $sequence_no['increment'] .
197 "', code_type = '" . trim(formData("HiddenCodetype$CountRow")) .
198 "', code = '" . trim(formData("HiddenCode$CountRow")) .
199 "', modifier = '" . trim(formData("HiddenModifier$CountRow")) .
200 "', payer_type = '" . trim(formData("HiddenIns$CountRow")) .
201 "', post_time = '" . trim($created_time) .
202 "', post_user = '" . trim($user_id) .
203 "', session_id = '" . trim(formData('payment_id')) .
204 "', modified_time = '" . trim($created_time) .
205 "', pay_amount = '" . 0 .
206 "', adj_amount = '" . 0 .
207 "', follow_up = '" . "y" .
208 "', follow_up_note = '" . trim(formData("FollowUpReason$CountRow")) .
209 "'");
210 sqlCommitTrans();
211 $Affected='yes';
214 if ($Affected=='yes') {
215 if (trim(formData('type_name'))!='patient') {
216 $ferow = sqlQuery("select last_level_closed from form_encounter where
217 pid ='".trim(formData('hidden_patient_code'))."' and encounter='".trim(formData("HiddenEncounter$CountRow"))."'");
218 //multiple charges can come.
219 if ($ferow['last_level_closed']<trim(formData("HiddenIns$CountRow"))) {
220 sqlStatement("update form_encounter set last_level_closed='".trim(formData("HiddenIns$CountRow"))."' where
221 pid ='".trim(formData('hidden_patient_code'))."' and encounter='".trim(formData("HiddenEncounter$CountRow"))."'");
222 //last_level_closed gets increased.
223 //-----------------------------------
224 // Determine the next insurance level to be billed.
225 $ferow = sqlQuery("SELECT date, last_level_closed " .
226 "FROM form_encounter WHERE " .
227 "pid = '".trim(formData('hidden_patient_code'))."' AND encounter = '".trim(formData("HiddenEncounter$CountRow"))."'");
228 $date_of_service = substr($ferow['date'], 0, 10);
229 $new_payer_type = 0 + $ferow['last_level_closed'];
230 if ($new_payer_type <= 3 && !empty($ferow['last_level_closed']) || $new_payer_type == 0) {
231 ++$new_payer_type;
234 $new_payer_id = arGetPayerID(trim(formData('hidden_patient_code')), $date_of_service, $new_payer_type);
235 if ($new_payer_id>0) {
236 arSetupSecondary(trim(formData('hidden_patient_code')), trim(formData("HiddenEncounter$CountRow")), 0);
239 //-----------------------------------
244 //===============================================================================
245 // Delete rows, with logging, for the specified table using the
246 // specified WHERE clause. Borrowed from deleter.php.
248 function row_delete($table, $where)
250 $tres = sqlStatement("SELECT * FROM $table WHERE $where");
251 $count = 0;
252 while ($trow = sqlFetchArray($tres)) {
253 $logstring = "";
254 foreach ($trow as $key => $value) {
255 if (! $value || $value == '0000-00-00 00:00:00') {
256 continue;
259 if ($logstring) {
260 $logstring .= " ";
263 $logstring .= $key . "='" . addslashes($value) . "'";
266 newEvent("delete", $_SESSION['authUser'], $_SESSION['authProvider'], 1, "$table: $logstring");
267 ++$count;
270 if ($count) {
271 $query = "DELETE FROM $table WHERE $where";
272 sqlStatement($query);
275 //===============================================================================