another minor fix to prior commit
[openemr.git] / library / payment.inc.php
blob8601460d2a4f24e53c9904c0f1fb907d3c48eb98
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.
22 //
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="") {
32 if (empty($auth)) {
33 $auth=$_SESSION['authUser'];
36 $tmprow = sqlQuery("SELECT date FROM form_encounter WHERE " .
37 "encounter=? and pid=?",
38 array($encounter,$patient_id));
39 //the manipulation is done to insert the amount paid into payments table in correct order to show in front receipts report,
40 //if the payment is for today's encounter it will be shown in the report under today field and otherwise shown as previous
41 $tmprowArray=explode(' ',$tmprow['date']);
42 if(date('Y-m-d')==$tmprowArray[0])
44 if($amount1==0)
46 $amount1=$amount2;
47 $amount2=0;
50 else
52 if($amount2==0)
54 $amount2=$amount1;
55 $amount1=0;
58 $payid = sqlInsert("INSERT INTO payments ( " .
59 "pid, encounter, dtime, user, method, source, amount1, amount2 " .
60 ") VALUES ( ?, ?, ?, ?, ?, ?, ?, ?)", array($patient_id,$encounter,$timestamp,$auth,$method,$source,$amount1,$amount2) );
61 return $payid;
64 //===============================================================================
65 //This section handles the common functins of payment screens.
66 //===============================================================================
67 function DistributionInsert($CountRow,$created_time,$user_id)
68 {//Function inserts the distribution.Payment,Adjustment,Deductable,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)
74 if(trim(formData('type_name' ))=='insurance')
76 if(trim(formData("HiddenIns$CountRow" ))==1)
78 $AccountCode="IPP";
80 if(trim(formData("HiddenIns$CountRow" ))==2)
82 $AccountCode="ISP";
84 if(trim(formData("HiddenIns$CountRow" ))==3)
86 $AccountCode="ITP";
89 elseif(trim(formData('type_name' ))=='patient')
91 $AccountCode="PP";
94 sqlBeginTrans();
95 $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" ))));
96 sqlStatement("insert into ar_activity set " .
97 "pid = '" . trim(formData('hidden_patient_code' )) .
98 "', encounter = '" . trim(formData("HiddenEncounter$CountRow" )) .
99 "', sequence_no = '" . $sequence_no['increment'] .
100 "', code_type = '" . trim(formData("HiddenCodetype$CountRow" )) .
101 "', code = '" . trim(formData("HiddenCode$CountRow" )) .
102 "', modifier = '" . trim(formData("HiddenModifier$CountRow" )) .
103 "', payer_type = '" . trim(formData("HiddenIns$CountRow" )) .
104 "', post_time = '" . trim($created_time ) .
105 "', post_user = '" . trim($user_id ) .
106 "', session_id = '" . trim(formData('payment_id')) .
107 "', modified_time = '" . trim($created_time ) .
108 "', pay_amount = '" . trim(formData("Payment$CountRow" )) .
109 "', adj_amount = '" . 0 .
110 "', account_code = '" . "$AccountCode" .
111 "'");
112 sqlCommitTrans();
113 $Affected='yes';
115 if (isset($_POST["AdjAmount$CountRow"]) && $_POST["AdjAmount$CountRow"]*1!=0)
117 if(trim(formData('type_name' ))=='insurance')
119 $AdjustString="Ins adjust Ins".trim(formData("HiddenIns$CountRow" ));
120 $AccountCode="IA";
122 elseif(trim(formData('type_name' ))=='patient')
124 $AdjustString="Pt adjust";
125 $AccountCode="PA";
128 sqlBeginTrans();
129 $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" ))));
130 sqlInsert("insert into ar_activity set " .
131 "pid = '" . trim(formData('hidden_patient_code' )) .
132 "', encounter = '" . trim(formData("HiddenEncounter$CountRow" )) .
133 "', sequence_no = '" . $sequence_no['increment'] .
134 "', code_type = '" . trim(formData("HiddenCodetype$CountRow" )) .
135 "', code = '" . trim(formData("HiddenCode$CountRow" )) .
136 "', modifier = '" . trim(formData("HiddenModifier$CountRow" )) .
137 "', payer_type = '" . trim(formData("HiddenIns$CountRow" )) .
138 "', post_time = '" . trim($created_time ) .
139 "', post_user = '" . trim($user_id ) .
140 "', session_id = '" . trim(formData('payment_id')) .
141 "', modified_time = '" . trim($created_time ) .
142 "', pay_amount = '" . 0 .
143 "', adj_amount = '" . trim(formData("AdjAmount$CountRow" )) .
144 "', memo = '" . "$AdjustString" .
145 "', account_code = '" . "$AccountCode" .
146 "'");
147 sqlCommitTrans();
148 $Affected='yes';
150 if (isset($_POST["Deductible$CountRow"]) && $_POST["Deductible$CountRow"]*1>0)
152 sqlBeginTrans();
153 $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" ))));
154 sqlInsert("insert into ar_activity set " .
155 "pid = '" . trim(formData('hidden_patient_code' )) .
156 "', encounter = '" . trim(formData("HiddenEncounter$CountRow" )) .
157 "', sequence_no = '" . $sequence_no['increment'] .
158 "', code_type = '" . trim(formData("HiddenCodetype$CountRow" )) .
159 "', code = '" . trim(formData("HiddenCode$CountRow" )) .
160 "', modifier = '" . trim(formData("HiddenModifier$CountRow" )) .
161 "', payer_type = '" . trim(formData("HiddenIns$CountRow" )) .
162 "', post_time = '" . trim($created_time ) .
163 "', post_user = '" . trim($user_id ) .
164 "', session_id = '" . trim(formData('payment_id')) .
165 "', modified_time = '" . trim($created_time ) .
166 "', pay_amount = '" . 0 .
167 "', adj_amount = '" . 0 .
168 "', memo = '" . "Deductable $".trim(formData("Deductible$CountRow" )) .
169 "', account_code = '" . "Deduct" .
170 "'");
171 sqlCommitTrans();
172 $Affected='yes';
174 if (isset($_POST["Takeback$CountRow"]) && $_POST["Takeback$CountRow"]*1>0)
176 sqlBeginTrans();
177 $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" ))));
178 sqlInsert("insert into ar_activity set " .
179 "pid = '" . trim(formData('hidden_patient_code' )) .
180 "', encounter = '" . trim(formData("HiddenEncounter$CountRow" )) .
181 "', sequence_no = '" . $sequence_no['increment'] .
182 "', code_type = '" . trim(formData("HiddenCodetype$CountRow" )) .
183 "', code = '" . trim(formData("HiddenCode$CountRow" )) .
184 "', modifier = '" . trim(formData("HiddenModifier$CountRow" )) .
185 "', payer_type = '" . trim(formData("HiddenIns$CountRow" )) .
186 "', post_time = '" . trim($created_time ) .
187 "', post_user = '" . trim($user_id ) .
188 "', session_id = '" . trim(formData('payment_id')) .
189 "', modified_time = '" . trim($created_time ) .
190 "', pay_amount = '" . trim(formData("Takeback$CountRow" ))*-1 .
191 "', adj_amount = '" . 0 .
192 "', account_code = '" . "Takeback" .
193 "'");
194 sqlCommitTrans();
195 $Affected='yes';
197 if (isset($_POST["FollowUp$CountRow"]) && $_POST["FollowUp$CountRow"]=='y')
199 sqlBeginTrans();
200 $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" ))));
201 sqlInsert("insert into ar_activity set " .
202 "pid = '" . trim(formData('hidden_patient_code' )) .
203 "', encounter = '" . trim(formData("HiddenEncounter$CountRow" )) .
204 "', sequence_no = '" . $sequence_no['increment'] .
205 "', code_type = '" . trim(formData("HiddenCodetype$CountRow" )) .
206 "', code = '" . trim(formData("HiddenCode$CountRow" )) .
207 "', modifier = '" . trim(formData("HiddenModifier$CountRow" )) .
208 "', payer_type = '" . trim(formData("HiddenIns$CountRow" )) .
209 "', post_time = '" . trim($created_time ) .
210 "', post_user = '" . trim($user_id ) .
211 "', session_id = '" . trim(formData('payment_id')) .
212 "', modified_time = '" . trim($created_time ) .
213 "', pay_amount = '" . 0 .
214 "', adj_amount = '" . 0 .
215 "', follow_up = '" . "y" .
216 "', follow_up_note = '" . trim(formData("FollowUpReason$CountRow" )) .
217 "'");
218 sqlCommitTrans();
219 $Affected='yes';
221 if($Affected=='yes')
223 if(trim(formData('type_name' ))!='patient')
225 $ferow = sqlQuery("select last_level_closed from form_encounter where
226 pid ='".trim(formData('hidden_patient_code' ))."' and encounter='".trim(formData("HiddenEncounter$CountRow" ))."'");
227 //multiple charges can come.
228 if($ferow['last_level_closed']<trim(formData("HiddenIns$CountRow" )))
230 sqlStatement("update form_encounter set last_level_closed='".trim(formData("HiddenIns$CountRow" ))."' where
231 pid ='".trim(formData('hidden_patient_code' ))."' and encounter='".trim(formData("HiddenEncounter$CountRow" ))."'");
232 //last_level_closed gets increased.
233 //-----------------------------------
234 // Determine the next insurance level to be billed.
235 $ferow = sqlQuery("SELECT date, last_level_closed " .
236 "FROM form_encounter WHERE " .
237 "pid = '".trim(formData('hidden_patient_code' ))."' AND encounter = '".trim(formData("HiddenEncounter$CountRow" ))."'");
238 $date_of_service = substr($ferow['date'], 0, 10);
239 $new_payer_type = 0 + $ferow['last_level_closed'];
240 if ($new_payer_type <= 3 && !empty($ferow['last_level_closed']) || $new_payer_type == 0)
241 ++$new_payer_type;
242 $new_payer_id = arGetPayerID(trim(formData('hidden_patient_code' )), $date_of_service, $new_payer_type);
243 if($new_payer_id>0)
245 arSetupSecondary(trim(formData('hidden_patient_code' )), trim(formData("HiddenEncounter$CountRow" )),0);
247 //-----------------------------------
252 //===============================================================================
253 // Delete rows, with logging, for the specified table using the
254 // specified WHERE clause. Borrowed from deleter.php.
256 function row_delete($table, $where) {
257 $tres = sqlStatement("SELECT * FROM $table WHERE $where");
258 $count = 0;
259 while ($trow = sqlFetchArray($tres)) {
260 $logstring = "";
261 foreach ($trow as $key => $value) {
262 if (! $value || $value == '0000-00-00 00:00:00') continue;
263 if ($logstring) $logstring .= " ";
264 $logstring .= $key . "='" . addslashes($value) . "'";
266 newEvent("delete", $_SESSION['authUser'], $_SESSION['authProvider'], 1, "$table: $logstring");
267 ++$count;
269 if ($count) {
270 $query = "DELETE FROM $table WHERE $where";
271 sqlStatement($query);
274 //===============================================================================