Hide dashboard card 2 (#7423)
[openemr.git] / library / payment.inc.php
blob031608204c59d5e976a7f8728b36a5372cedcde0
1 <?php
3 /**
5 * @package OpenEMR
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 = "")
25 if (empty($auth)) {
26 $auth = $_SESSION['authUser'];
29 $tmprow = sqlQuery(
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]) {
38 if ($amount1 == 0) {
39 $amount1 = $amount2;
40 $amount2 = 0;
42 } else {
43 if ($amount2 == 0) {
44 $amount2 = $amount1;
45 $amount1 = 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));
52 return $payid;
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.
63 $Affected = 'no';
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) {
68 $AccountCode = "IPP";
71 if (trim(formData("HiddenIns$CountRow")) == 2) {
72 $AccountCode = "ISP";
75 if (trim(formData("HiddenIns$CountRow")) == 3) {
76 $AccountCode = "ITP";
78 } elseif (trim(formData('type_name')) == 'patient') {
79 $AccountCode = "PP";
82 sqlBeginTrans();
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" .
99 "'");
100 sqlCommitTrans();
101 $Affected = 'yes';
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"));
107 $AccountCode = "IA";
108 } elseif (trim(formData('type_name')) == 'patient') {
109 $AdjustString = "Pt adjust";
110 $AccountCode = "PA";
113 sqlBeginTrans();
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" .
131 "'");
132 sqlCommitTrans();
133 $Affected = 'yes';
136 if (!empty($_POST["Deductible$CountRow"]) && (floatval($_POST["Deductible$CountRow"] ?? null)) > 0) {
137 sqlBeginTrans();
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" .
155 "'");
156 sqlCommitTrans();
157 $Affected = 'yes';
160 if (!empty($_POST["Takeback$CountRow"]) && (floatval($_POST["Takeback$CountRow"] ?? null)) > 0) {
161 sqlBeginTrans();
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" .
178 "'");
179 sqlCommitTrans();
180 $Affected = 'yes';
183 if (isset($_POST["FollowUp$CountRow"]) && $_POST["FollowUp$CountRow"] == 'y') {
184 sqlBeginTrans();
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")) .
202 "'");
203 sqlCommitTrans();
204 $Affected = 'yes';
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) {
231 ++$new_payer_type;
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");
251 $count = 0;
252 while ($trow = sqlFetchArray($tres)) {
253 $logstring = "";
254 foreach ($trow as $key => $value) {
255 if (! $value || $value == '0000-00-00 00:00:00') {
256 continue;
259 if ($logstring) {
260 $logstring .= " ";
263 $logstring .= $key . "='" . addslashes($value) . "'";
266 EventAuditLogger::instance()->newEvent("delete", $_SESSION['authUser'], $_SESSION['authProvider'], 1, "$table: $logstring");
267 ++$count;
270 if ($count) {
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(
283 "deactivate",
284 $_SESSION['authUser'],
285 $_SESSION['authProvider'],
287 "$table: $where"
289 $query = "UPDATE $table SET $set WHERE $where";
290 sqlStatement($query);
294 //===============================================================================