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