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 if (isset($_POST["Payment$CountRow"]) && $_POST["Payment$CountRow"] * 1 > 0) {
65 if (trim(formData('type_name')) == 'insurance') {
66 if (trim(formData("HiddenIns$CountRow")) == 1) {
70 if (trim(formData("HiddenIns$CountRow")) == 2) {
74 if (trim(formData("HiddenIns$CountRow")) == 3) {
77 } elseif (trim(formData('type_name')) == 'patient') {
82 $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"))));
83 sqlStatement("insert into ar_activity set " .
84 "pid = '" . trim(formData('hidden_patient_code')) .
85 "', encounter = '" . trim(formData("HiddenEncounter$CountRow")) .
86 "', sequence_no = '" . $sequence_no['increment'] .
87 "', code_type = '" . trim(formData("HiddenCodetype$CountRow")) .
88 "', code = '" . trim(formData("HiddenCode$CountRow")) .
89 "', modifier = '" . trim(formData("HiddenModifier$CountRow")) .
90 "', payer_type = '" . trim(formData("HiddenIns$CountRow")) .
91 "', post_time = '" . trim($created_time) .
92 "', post_user = '" . trim($user_id) .
93 "', session_id = '" . trim(formData('payment_id')) .
94 "', modified_time = '" . trim($created_time) .
95 "', pay_amount = '" . trim(formData("Payment$CountRow")) .
96 "', adj_amount = '" . 0 .
97 "', account_code = '" . "$AccountCode" .
103 if (isset($_POST["AdjAmount$CountRow"]) && $_POST["AdjAmount$CountRow"] * 1 != 0) {
104 if (trim(formData('type_name')) == 'insurance') {
105 $AdjustString = "Ins adjust Ins" . trim(formData("HiddenIns$CountRow"));
107 } elseif (trim(formData('type_name')) == 'patient') {
108 $AdjustString = "Pt adjust";
113 $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"))));
114 sqlStatement("insert into ar_activity set " .
115 "pid = '" . trim(formData('hidden_patient_code')) .
116 "', encounter = '" . trim(formData("HiddenEncounter$CountRow")) .
117 "', sequence_no = '" . $sequence_no['increment'] .
118 "', code_type = '" . trim(formData("HiddenCodetype$CountRow")) .
119 "', code = '" . trim(formData("HiddenCode$CountRow")) .
120 "', modifier = '" . trim(formData("HiddenModifier$CountRow")) .
121 "', payer_type = '" . trim(formData("HiddenIns$CountRow")) .
122 "', post_time = '" . trim($created_time) .
123 "', post_user = '" . trim($user_id) .
124 "', session_id = '" . trim(formData('payment_id')) .
125 "', modified_time = '" . trim($created_time) .
126 "', pay_amount = '" . 0 .
127 "', adj_amount = '" . trim(formData("AdjAmount$CountRow")) .
128 "', memo = '" . "$AdjustString" .
129 "', account_code = '" . "$AccountCode" .
135 if (isset($_POST["Deductible$CountRow"]) && $_POST["Deductible$CountRow"] * 1 > 0) {
137 $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"))));
138 sqlStatement("insert into ar_activity set " .
139 "pid = '" . trim(formData('hidden_patient_code')) .
140 "', encounter = '" . trim(formData("HiddenEncounter$CountRow")) .
141 "', sequence_no = '" . $sequence_no['increment'] .
142 "', code_type = '" . trim(formData("HiddenCodetype$CountRow")) .
143 "', code = '" . trim(formData("HiddenCode$CountRow")) .
144 "', modifier = '" . trim(formData("HiddenModifier$CountRow")) .
145 "', payer_type = '" . trim(formData("HiddenIns$CountRow")) .
146 "', post_time = '" . trim($created_time) .
147 "', post_user = '" . trim($user_id) .
148 "', session_id = '" . trim(formData('payment_id')) .
149 "', modified_time = '" . trim($created_time) .
150 "', pay_amount = '" . 0 .
151 "', adj_amount = '" . 0 .
152 "', memo = '" . "Deductible $" . trim(formData("Deductible$CountRow")) .
153 "', account_code = '" . "Deduct" .
159 if (isset($_POST["Takeback$CountRow"]) && $_POST["Takeback$CountRow"] * 1 > 0) {
161 $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"))));
162 sqlStatement("insert into ar_activity set " .
163 "pid = '" . trim(formData('hidden_patient_code')) .
164 "', encounter = '" . trim(formData("HiddenEncounter$CountRow")) .
165 "', sequence_no = '" . $sequence_no['increment'] .
166 "', code_type = '" . trim(formData("HiddenCodetype$CountRow")) .
167 "', code = '" . trim(formData("HiddenCode$CountRow")) .
168 "', modifier = '" . trim(formData("HiddenModifier$CountRow")) .
169 "', payer_type = '" . trim(formData("HiddenIns$CountRow")) .
170 "', post_time = '" . trim($created_time) .
171 "', post_user = '" . trim($user_id) .
172 "', session_id = '" . trim(formData('payment_id')) .
173 "', modified_time = '" . trim($created_time) .
174 "', pay_amount = '" . trim(formData("Takeback$CountRow")) * -1 .
175 "', adj_amount = '" . 0 .
176 "', account_code = '" . "Takeback" .
182 if (isset($_POST["FollowUp$CountRow"]) && $_POST["FollowUp$CountRow"] == 'y') {
184 $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"))));
185 sqlStatement("insert into ar_activity set " .
186 "pid = '" . trim(formData('hidden_patient_code')) .
187 "', encounter = '" . trim(formData("HiddenEncounter$CountRow")) .
188 "', sequence_no = '" . $sequence_no['increment'] .
189 "', code_type = '" . trim(formData("HiddenCodetype$CountRow")) .
190 "', code = '" . trim(formData("HiddenCode$CountRow")) .
191 "', modifier = '" . trim(formData("HiddenModifier$CountRow")) .
192 "', payer_type = '" . trim(formData("HiddenIns$CountRow")) .
193 "', post_time = '" . trim($created_time) .
194 "', post_user = '" . trim($user_id) .
195 "', session_id = '" . trim(formData('payment_id')) .
196 "', modified_time = '" . trim($created_time) .
197 "', pay_amount = '" . 0 .
198 "', adj_amount = '" . 0 .
199 "', follow_up = '" . "y" .
200 "', follow_up_note = '" . trim(formData("FollowUpReason$CountRow")) .
206 if ($Affected == 'yes') {
207 if (trim(formData('type_name')) != 'patient') {
208 $ferow = sqlQuery("select last_level_closed from form_encounter where
209 pid ='" . trim(formData('hidden_patient_code')) . "' and encounter='" . trim(formData("HiddenEncounter$CountRow")) . "'");
210 //multiple charges can come.
211 if ($ferow['last_level_closed'] < trim(formData("HiddenIns$CountRow"))) {
212 sqlStatement("update form_encounter set last_level_closed='" . trim(formData("HiddenIns$CountRow")) . "' where
213 pid ='" . trim(formData('hidden_patient_code')) . "' and encounter='" . trim(formData("HiddenEncounter$CountRow")) . "'");
214 //last_level_closed gets increased.
215 //-----------------------------------
216 // Determine the next insurance level to be billed.
217 $ferow = sqlQuery("SELECT date, last_level_closed " .
218 "FROM form_encounter WHERE " .
219 "pid = '" . trim(formData('hidden_patient_code')) . "' AND encounter = '" . trim(formData("HiddenEncounter$CountRow")) . "'");
220 $date_of_service = substr($ferow['date'], 0, 10);
221 $new_payer_type = 0 +
$ferow['last_level_closed'];
222 if ($new_payer_type <= 3 && !empty($ferow['last_level_closed']) ||
$new_payer_type == 0) {
226 $new_payer_id = SLEOB
::arGetPayerID(trim(formData('hidden_patient_code')), $date_of_service, $new_payer_type);
227 if ($new_payer_id > 0) {
228 SLEOB
::arSetupSecondary(trim(formData('hidden_patient_code')), trim(formData("HiddenEncounter$CountRow")), 0);
231 //-----------------------------------
236 //===============================================================================
237 // Delete rows, with logging, for the specified table using the
238 // specified WHERE clause. Borrowed from deleter.php.
240 function row_delete($table, $where)
242 $tres = sqlStatement("SELECT * FROM " . escape_table_name($table) . " WHERE $where");
244 while ($trow = sqlFetchArray($tres)) {
246 foreach ($trow as $key => $value) {
247 if (! $value ||
$value == '0000-00-00 00:00:00') {
255 $logstring .= $key . "='" . addslashes($value) . "'";
258 EventAuditLogger
::instance()->newEvent("delete", $_SESSION['authUser'], $_SESSION['authProvider'], 1, "$table: $logstring");
263 $query = "DELETE FROM " . escape_table_name($table) . " WHERE $where";
264 sqlStatement($query);
268 // Deactivate rows, with logging, for the specified table using the
269 // specified SET and WHERE clauses. Borrowed from deleter.php.
271 function row_modify($table, $set, $where)
273 if (sqlQuery("SELECT * FROM " . escape_table_name($table) . " WHERE $where")) {
274 EventAuditLogger
::instance()->newEvent(
276 $_SESSION['authUser'],
277 $_SESSION['authProvider'],
281 $query = "UPDATE $table SET $set WHERE $where";
282 sqlStatement($query);
286 //===============================================================================