Modularize frontPayment() function in front_payment.php script to allow use with...
[openemr.git] / library / payment.inc.php
blobeb44fa2ab6ca197e934d09c79114d8b8fc1fbe68
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";
93 sqlStatement("insert into ar_activity set " .
94 "pid = '" . trim(formData('hidden_patient_code' )) .
95 "', encounter = '" . trim(formData("HiddenEncounter$CountRow" )) .
96 "', code_type = '" . trim(formData("HiddenCodetype$CountRow" )) .
97 "', code = '" . trim(formData("HiddenCode$CountRow" )) .
98 "', modifier = '" . trim(formData("HiddenModifier$CountRow" )) .
99 "', payer_type = '" . trim(formData("HiddenIns$CountRow" )) .
100 "', post_time = '" . trim($created_time ) .
101 "', post_user = '" . trim($user_id ) .
102 "', session_id = '" . trim(formData('payment_id')) .
103 "', modified_time = '" . trim($created_time ) .
104 "', pay_amount = '" . trim(formData("Payment$CountRow" )) .
105 "', adj_amount = '" . 0 .
106 "', account_code = '" . "$AccountCode" .
107 "'");
108 $Affected='yes';
110 if (isset($_POST["AdjAmount$CountRow"]) && $_POST["AdjAmount$CountRow"]*1!=0)
112 if(trim(formData('type_name' ))=='insurance')
114 $AdjustString="Ins adjust Ins".trim(formData("HiddenIns$CountRow" ));
115 $AccountCode="IA";
117 elseif(trim(formData('type_name' ))=='patient')
119 $AdjustString="Pt adjust";
120 $AccountCode="PA";
124 idSqlStatement("insert into ar_activity set " .
125 "pid = '" . trim(formData('hidden_patient_code' )) .
126 "', encounter = '" . trim(formData("HiddenEncounter$CountRow" )) .
127 "', code_type = '" . trim(formData("HiddenCodetype$CountRow" )) .
128 "', code = '" . trim(formData("HiddenCode$CountRow" )) .
129 "', modifier = '" . trim(formData("HiddenModifier$CountRow" )) .
130 "', payer_type = '" . trim(formData("HiddenIns$CountRow" )) .
131 "', post_time = '" . trim($created_time ) .
132 "', post_user = '" . trim($user_id ) .
133 "', session_id = '" . trim(formData('payment_id')) .
134 "', modified_time = '" . trim($created_time ) .
135 "', pay_amount = '" . 0 .
136 "', adj_amount = '" . trim(formData("AdjAmount$CountRow" )) .
137 "', memo = '" . "$AdjustString" .
138 "', account_code = '" . "$AccountCode" .
139 "'");
140 $Affected='yes';
142 if (isset($_POST["Deductible$CountRow"]) && $_POST["Deductible$CountRow"]*1>0)
144 idSqlStatement("insert into ar_activity set " .
145 "pid = '" . trim(formData('hidden_patient_code' )) .
146 "', encounter = '" . trim(formData("HiddenEncounter$CountRow" )) .
147 "', code_type = '" . trim(formData("HiddenCodetype$CountRow" )) .
148 "', code = '" . trim(formData("HiddenCode$CountRow" )) .
149 "', modifier = '" . trim(formData("HiddenModifier$CountRow" )) .
150 "', payer_type = '" . trim(formData("HiddenIns$CountRow" )) .
151 "', post_time = '" . trim($created_time ) .
152 "', post_user = '" . trim($user_id ) .
153 "', session_id = '" . trim(formData('payment_id')) .
154 "', modified_time = '" . trim($created_time ) .
155 "', pay_amount = '" . 0 .
156 "', adj_amount = '" . 0 .
157 "', memo = '" . "Deductable $".trim(formData("Deductible$CountRow" )) .
158 "', account_code = '" . "Deduct" .
159 "'");
160 $Affected='yes';
162 if (isset($_POST["Takeback$CountRow"]) && $_POST["Takeback$CountRow"]*1>0)
164 idSqlStatement("insert into ar_activity set " .
165 "pid = '" . trim(formData('hidden_patient_code' )) .
166 "', encounter = '" . trim(formData("HiddenEncounter$CountRow" )) .
167 "', code_type = '" . trim(formData("HiddenCodetype$CountRow" )) .
168 "', code = '" . trim(formData("HiddenCode$CountRow" )) .
169 "', modifier = '" . trim(formData("HiddenModifier$CountRow" )) .
170 "', payer_type = '" . trim(formData("HiddenIns$CountRow" )) .
171 "', post_time = '" . trim($created_time ) .
172 "', post_user = '" . trim($user_id ) .
173 "', session_id = '" . trim(formData('payment_id')) .
174 "', modified_time = '" . trim($created_time ) .
175 "', pay_amount = '" . trim(formData("Takeback$CountRow" ))*-1 .
176 "', adj_amount = '" . 0 .
177 "', account_code = '" . "Takeback" .
178 "'");
179 $Affected='yes';
181 if (isset($_POST["FollowUp$CountRow"]) && $_POST["FollowUp$CountRow"]=='y')
183 idSqlStatement("insert into ar_activity set " .
184 "pid = '" . trim(formData('hidden_patient_code' )) .
185 "', encounter = '" . trim(formData("HiddenEncounter$CountRow" )) .
186 "', code_type = '" . trim(formData("HiddenCodetype$CountRow" )) .
187 "', code = '" . trim(formData("HiddenCode$CountRow" )) .
188 "', modifier = '" . trim(formData("HiddenModifier$CountRow" )) .
189 "', payer_type = '" . trim(formData("HiddenIns$CountRow" )) .
190 "', post_time = '" . trim($created_time ) .
191 "', post_user = '" . trim($user_id ) .
192 "', session_id = '" . trim(formData('payment_id')) .
193 "', modified_time = '" . trim($created_time ) .
194 "', pay_amount = '" . 0 .
195 "', adj_amount = '" . 0 .
196 "', follow_up = '" . "y" .
197 "', follow_up_note = '" . trim(formData("FollowUpReason$CountRow" )) .
198 "'");
199 $Affected='yes';
201 if($Affected=='yes')
203 if(trim(formData('type_name' ))!='patient')
205 $ferow = sqlQuery("select last_level_closed from form_encounter where
206 pid ='".trim(formData('hidden_patient_code' ))."' and encounter='".trim(formData("HiddenEncounter$CountRow" ))."'");
207 //multiple charges can come.
208 if($ferow['last_level_closed']<trim(formData("HiddenIns$CountRow" )))
210 sqlStatement("update form_encounter set last_level_closed='".trim(formData("HiddenIns$CountRow" ))."' where
211 pid ='".trim(formData('hidden_patient_code' ))."' and encounter='".trim(formData("HiddenEncounter$CountRow" ))."'");
212 //last_level_closed gets increased.
213 //-----------------------------------
214 // Determine the next insurance level to be billed.
215 $ferow = sqlQuery("SELECT date, last_level_closed " .
216 "FROM form_encounter WHERE " .
217 "pid = '".trim(formData('hidden_patient_code' ))."' AND encounter = '".trim(formData("HiddenEncounter$CountRow" ))."'");
218 $date_of_service = substr($ferow['date'], 0, 10);
219 $new_payer_type = 0 + $ferow['last_level_closed'];
220 if ($new_payer_type <= 3 && !empty($ferow['last_level_closed']) || $new_payer_type == 0)
221 ++$new_payer_type;
222 $new_payer_id = arGetPayerID(trim(formData('hidden_patient_code' )), $date_of_service, $new_payer_type);
223 if($new_payer_id>0)
225 arSetupSecondary(trim(formData('hidden_patient_code' )), trim(formData("HiddenEncounter$CountRow" )),0);
227 //-----------------------------------
232 //===============================================================================
233 // Delete rows, with logging, for the specified table using the
234 // specified WHERE clause. Borrowed from deleter.php.
236 function row_delete($table, $where) {
237 $tres = sqlStatement("SELECT * FROM $table WHERE $where");
238 $count = 0;
239 while ($trow = sqlFetchArray($tres)) {
240 $logstring = "";
241 foreach ($trow as $key => $value) {
242 if (! $value || $value == '0000-00-00 00:00:00') continue;
243 if ($logstring) $logstring .= " ";
244 $logstring .= $key . "='" . addslashes($value) . "'";
246 newEvent("delete", $_SESSION['authUser'], $_SESSION['authProvider'], 1, "$table: $logstring");
247 ++$count;
249 if ($count) {
250 $query = "DELETE FROM $table WHERE $where";
251 sqlStatement($query);
254 //===============================================================================