6 * @author Eldho Chacko <eldho@zhservices.com>
7 * @author Paul Simon K <paul@zhservices.com>
8 * @author Stephen Waite <stephen.waite@cmsvt.com>
9 * @author Rod Roark <rod@sunsetsystems.com>
10 * @copyright Copyright (c) 2010 Z&H Consultancy Services Private Limited <sam@zhservices.com>
11 * @copyright Copyright (c) 2018 Stephen Waite <stephen.waite@cmsvt.com>
12 * @copyright Copyright (c) 2020 Rod Roark <rod@sunsetsystems.com>
13 * @link https://www.open-emr.org
14 * @license https://github.com/openemr/openemr/blob/master/LICENSE GNU General Public License 3
17 use OpenEMR\Billing\SLEOB
;
18 use OpenEMR\Common\Logging\EventAuditLogger
;
20 // Post a payment to the payments table.
22 function frontPayment($patient_id, $encounter, $method, $source, $amount1, $amount2, $timestamp, $auth = "")
26 $auth = $_SESSION['authUser'];
30 "SELECT date FROM form_encounter WHERE " .
31 "encounter=? and pid=?",
32 array($encounter,$patient_id)
34 //the manipulation is done to insert the amount paid into payments table in correct order to show in front receipts report,
35 //if the payment is for today's encounter it will be shown in the report under today field and otherwise shown as previous
36 $tmprowArray = explode(' ', $tmprow['date']);
37 if (date('Y-m-d') == $tmprowArray[0]) {
49 $payid = sqlInsert("INSERT INTO payments ( " .
50 "pid, encounter, dtime, user, method, source, amount1, amount2 " .
51 ") VALUES ( ?, ?, ?, ?, ?, ?, ?, ?)", array($patient_id,$encounter,$timestamp,$auth,$method,$source,$amount1,$amount2));
55 //===============================================================================
56 //This section handles the common functins of payment screens.
57 //===============================================================================
58 function DistributionInsert($CountRow, $created_time, $user_id)
60 //Function inserts the distribution.Payment,Adjustment,Deductible,Takeback & Follow up reasons are inserted as seperate rows.
61 //It automatically pushes to next insurance for billing.
62 //In the screen a drop down of Ins1,Ins2,Ins3,Pat are given.The posting can be done for any level.
64 // watch for payments less than $1, thanks @snailwell
65 if (isset($_POST["Payment$CountRow"]) && (floatval($_POST["Payment$CountRow"]) > 0)) {
66 if (trim(formData('type_name')) == 'insurance') {
67 if (trim(formData("HiddenIns$CountRow")) == 1) {
71 if (trim(formData("HiddenIns$CountRow")) == 2) {
75 if (trim(formData("HiddenIns$CountRow")) == 3) {
78 } elseif (trim(formData('type_name')) == 'patient') {
83 $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"))));
84 sqlStatement("insert into ar_activity set " .
85 "pid = '" . trim(formData('hidden_patient_code')) .
86 "', encounter = '" . trim(formData("HiddenEncounter$CountRow")) .
87 "', sequence_no = '" . $sequence_no['increment'] .
88 "', code_type = '" . trim(formData("HiddenCodetype$CountRow")) .
89 "', code = '" . trim(formData("HiddenCode$CountRow")) .
90 "', modifier = '" . trim(formData("HiddenModifier$CountRow")) .
91 "', payer_type = '" . trim(formData("HiddenIns$CountRow")) .
92 "', post_time = '" . trim($created_time) .
93 "', post_user = '" . trim($user_id) .
94 "', session_id = '" . trim(formData('payment_id')) .
95 "', modified_time = '" . trim($created_time) .
96 "', pay_amount = '" . trim(formData("Payment$CountRow")) .
97 "', adj_amount = '" . 0 .
98 "', account_code = '" . "$AccountCode" .
104 if (!empty($_POST["AdjAmount$CountRow"]) && (floatval($_POST["AdjAmount$CountRow"] ??
null)) != 0) {
105 if (trim(formData('type_name')) == 'insurance') {
106 $AdjustString = "Ins adjust Ins" . trim(formData("HiddenIns$CountRow"));
108 } elseif (trim(formData('type_name')) == 'patient') {
109 $AdjustString = "Pt adjust";
114 $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"))));
115 sqlStatement("insert into ar_activity set " .
116 "pid = '" . trim(formData('hidden_patient_code')) .
117 "', encounter = '" . trim(formData("HiddenEncounter$CountRow")) .
118 "', sequence_no = '" . $sequence_no['increment'] .
119 "', code_type = '" . trim(formData("HiddenCodetype$CountRow")) .
120 "', code = '" . trim(formData("HiddenCode$CountRow")) .
121 "', modifier = '" . trim(formData("HiddenModifier$CountRow")) .
122 "', payer_type = '" . trim(formData("HiddenIns$CountRow")) .
123 "', post_time = '" . trim($created_time) .
124 "', post_user = '" . trim($user_id) .
125 "', session_id = '" . trim(formData('payment_id')) .
126 "', modified_time = '" . trim($created_time) .
127 "', pay_amount = '" . 0 .
128 "', adj_amount = '" . trim(formData("AdjAmount$CountRow")) .
129 "', memo = '" . "$AdjustString" .
130 "', account_code = '" . "$AccountCode" .
136 if (!empty($_POST["Deductible$CountRow"]) && (floatval($_POST["Deductible$CountRow"] ??
null)) > 0) {
138 $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"))));
139 sqlStatement("insert into ar_activity set " .
140 "pid = '" . trim(formData('hidden_patient_code')) .
141 "', encounter = '" . trim(formData("HiddenEncounter$CountRow")) .
142 "', sequence_no = '" . $sequence_no['increment'] .
143 "', code_type = '" . trim(formData("HiddenCodetype$CountRow")) .
144 "', code = '" . trim(formData("HiddenCode$CountRow")) .
145 "', modifier = '" . trim(formData("HiddenModifier$CountRow")) .
146 "', payer_type = '" . trim(formData("HiddenIns$CountRow")) .
147 "', post_time = '" . trim($created_time) .
148 "', post_user = '" . trim($user_id) .
149 "', session_id = '" . trim(formData('payment_id')) .
150 "', modified_time = '" . trim($created_time) .
151 "', pay_amount = '" . 0 .
152 "', adj_amount = '" . 0 .
153 "', memo = '" . "Deductible $" . trim(formData("Deductible$CountRow")) .
154 "', account_code = '" . "Deduct" .
160 if (!empty($_POST["Takeback$CountRow"]) && (floatval($_POST["Takeback$CountRow"] ??
null)) > 0) {
162 $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"))));
163 sqlStatement("insert into ar_activity set " .
164 "pid = '" . trim(formData('hidden_patient_code')) .
165 "', encounter = '" . trim(formData("HiddenEncounter$CountRow")) .
166 "', sequence_no = '" . $sequence_no['increment'] .
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" .
183 if (isset($_POST["FollowUp$CountRow"]) && $_POST["FollowUp$CountRow"] == 'y') {
185 $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"))));
186 sqlStatement("insert into ar_activity set " .
187 "pid = '" . trim(formData('hidden_patient_code')) .
188 "', encounter = '" . trim(formData("HiddenEncounter$CountRow")) .
189 "', sequence_no = '" . $sequence_no['increment'] .
190 "', code_type = '" . trim(formData("HiddenCodetype$CountRow")) .
191 "', code = '" . trim(formData("HiddenCode$CountRow")) .
192 "', modifier = '" . trim(formData("HiddenModifier$CountRow")) .
193 "', payer_type = '" . trim(formData("HiddenIns$CountRow")) .
194 "', post_time = '" . trim($created_time) .
195 "', post_user = '" . trim($user_id) .
196 "', session_id = '" . trim(formData('payment_id')) .
197 "', modified_time = '" . trim($created_time) .
198 "', pay_amount = '" . 0 .
199 "', adj_amount = '" . 0 .
200 "', follow_up = '" . "y" .
201 "', follow_up_note = '" . trim(formData("FollowUpReason$CountRow")) .
207 if ($Affected == 'yes') {
208 if (trim(formData('type_name')) != 'patient') {
209 $ferow = sqlQuery("select last_level_closed from form_encounter where
210 pid ='" . trim(formData('hidden_patient_code')) . "' and encounter='" . trim(formData("HiddenEncounter$CountRow")) . "'");
211 //multiple charges can come.
212 if ($ferow['last_level_closed'] < trim(formData("HiddenIns$CountRow"))) {
213 //last_level_closed gets increased. unless a follow up is required.
214 // in which case we'll allow secondary to be re setup to current setup.
215 // just not advancing last closed.
216 $tmp = ((!empty($_POST["Payment$CountRow"]) ?
floatval($_POST["Payment$CountRow"]) : null) +
(!empty($_POST["AdjAmount$CountRow"]) ?
floatval($_POST["AdjAmount$CountRow"]) : null));
217 if ((empty($_POST["FollowUp$CountRow"]) ||
($_POST["FollowUp$CountRow"] != 'y')) && $tmp !== 0) {
218 sqlStatement("update form_encounter set last_level_closed='" .
219 trim(formData("HiddenIns$CountRow")) .
220 "' where pid ='" . trim(formData('hidden_patient_code')) .
221 "' and encounter='" . trim(formData("HiddenEncounter$CountRow")) . "'");
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) {
234 $new_payer_id = SLEOB
::arGetPayerID(trim(formData('hidden_patient_code')), $date_of_service, $new_payer_type);
235 if ($new_payer_id > 0) {
236 SLEOB
::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 " . escape_table_name($table) . " WHERE $where");
252 while ($trow = sqlFetchArray($tres)) {
254 foreach ($trow as $key => $value) {
255 if (! $value ||
$value == '0000-00-00 00:00:00') {
263 $logstring .= $key . "='" . addslashes($value) . "'";
266 EventAuditLogger
::instance()->newEvent("delete", $_SESSION['authUser'], $_SESSION['authProvider'], 1, "$table: $logstring");
271 $query = "DELETE FROM " . escape_table_name($table) . " WHERE $where";
272 sqlStatement($query);
276 // Deactivate rows, with logging, for the specified table using the
277 // specified SET and WHERE clauses. Borrowed from deleter.php.
279 function row_modify($table, $set, $where)
281 if (sqlQuery("SELECT * FROM " . escape_table_name($table) . " WHERE $where")) {
282 EventAuditLogger
::instance()->newEvent(
284 $_SESSION['authUser'],
285 $_SESSION['authProvider'],
289 $query = "UPDATE $table SET $set WHERE $where";
290 sqlStatement($query);
294 //===============================================================================